From f381cee82f7e5a75b11b30c5daf6dca19e5b7158 Mon Sep 17 00:00:00 2001 From: Patrick Dawson Date: Fri, 30 Aug 2024 22:17:41 +0200 Subject: [PATCH] Metal: bind index buffer with offset --- drivers/metal/metal_objects.h | 1 + drivers/metal/metal_objects.mm | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/metal/metal_objects.h b/drivers/metal/metal_objects.h index f0a3e85f88ba..97f33bb1e84f 100644 --- a/drivers/metal/metal_objects.h +++ b/drivers/metal/metal_objects.h @@ -227,6 +227,7 @@ class API_AVAILABLE(macos(11.0), ios(14.0)) MDCommandBuffer { id encoder = nil; id __unsafe_unretained index_buffer = nil; // Buffer is owned by RDD. MTLIndexType index_type = MTLIndexTypeUInt16; + uint32_t index_offset = 0; LocalVector __unsafe_unretained> vertex_buffers; LocalVector vertex_offsets; // clang-format off diff --git a/drivers/metal/metal_objects.mm b/drivers/metal/metal_objects.mm index c66b683e236c..abdcccf00c68 100644 --- a/drivers/metal/metal_objects.mm +++ b/drivers/metal/metal_objects.mm @@ -717,6 +717,7 @@ render.index_buffer = rid::get(p_buffer); render.index_type = p_format == RDD::IndexBufferFormat::INDEX_BUFFER_FORMAT_UINT16 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32; + render.index_offset = p_offset; } void MDCommandBuffer::render_draw_indexed(uint32_t p_index_count, @@ -729,13 +730,16 @@ id enc = render.encoder; + uint32_t index_offset = render.index_offset; + index_offset += p_first_index * (render.index_type == MTLIndexTypeUInt16 ? sizeof(uint16_t) : sizeof(uint32_t)); + [enc drawIndexedPrimitives:render.pipeline->raster_state.render_primitive indexCount:p_index_count indexType:render.index_type indexBuffer:render.index_buffer - indexBufferOffset:p_vertex_offset + indexBufferOffset:index_offset instanceCount:p_instance_count - baseVertex:p_first_index + baseVertex:p_vertex_offset baseInstance:p_first_instance]; }