diff --git a/src/renderer/renderer_dx12.cpp b/src/renderer/renderer_dx12.cpp index 9de7fd0..d5bc789 100644 --- a/src/renderer/renderer_dx12.cpp +++ b/src/renderer/renderer_dx12.cpp @@ -981,8 +981,9 @@ static void emit_text_glyphs(DrawBatch *batch, Renderer *r, //////////////////////////////// // Flush helper: issues a draw call for accumulated vertices, then resets batch -static void flush_batch(Renderer *r, DrawBatch *batch, UINT buf_idx, ID3D12DescriptorHeap *tex_heap = nullptr) { - if (batch->index_count == 0) return; +static void flush_batch(Renderer *r, DrawBatch *batch, UINT buf_idx, U32 *flush_index_start, ID3D12DescriptorHeap *tex_heap = nullptr) { + U32 draw_index_count = batch->index_count - *flush_index_start; + if (draw_index_count == 0) return; r->command_list->SetPipelineState(r->pipeline_state); r->command_list->SetGraphicsRootSignature(r->root_signature); @@ -1010,10 +1011,9 @@ static void flush_batch(Renderer *r, DrawBatch *batch, UINT buf_idx, ID3D12Descr ibv.Format = DXGI_FORMAT_R32_UINT; r->command_list->IASetIndexBuffer(&ibv); - r->command_list->DrawIndexedInstanced(batch->index_count, 1, 0, 0, 0); + r->command_list->DrawIndexedInstanced(draw_index_count, 1, *flush_index_start, 0, 0); - batch->vertex_count = 0; - batch->index_count = 0; + *flush_index_start = batch->index_count; } //////////////////////////////// @@ -1143,17 +1143,18 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) { // Track which texture is currently bound (0 = font, 1 = icon) int bound_texture = 0; + U32 flush_index_start = 0; auto bind_font = [&]() { if (bound_texture != 0) { - flush_batch(r, &batch, buf_idx, r->srv_heap); + flush_batch(r, &batch, buf_idx, &flush_index_start, r->srv_heap); bound_texture = 0; } }; auto bind_icon = [&]() { if (bound_texture != 1 && r->icon_srv_heap) { - flush_batch(r, &batch, buf_idx); + flush_batch(r, &batch, buf_idx, &flush_index_start); bound_texture = 1; } }; @@ -1212,7 +1213,7 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) { case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: { // Flush before changing scissor ID3D12DescriptorHeap *heap = bound_texture == 1 ? r->icon_srv_heap : r->srv_heap; - flush_batch(r, &batch, buf_idx, heap); + flush_batch(r, &batch, buf_idx, &flush_index_start, heap); D3D12_RECT clip = {}; clip.left = (LONG)Max(bb.x, 0.f); clip.top = (LONG)Max(bb.y, 0.f); @@ -1225,7 +1226,7 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) { case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END: { ID3D12DescriptorHeap *heap = bound_texture == 1 ? r->icon_srv_heap : r->srv_heap; - flush_batch(r, &batch, buf_idx, heap); + flush_batch(r, &batch, buf_idx, &flush_index_start, heap); D3D12_RECT full_scissor = { 0, 0, (LONG)r->width, (LONG)r->height }; r->command_list->RSSetScissorRects(1, &full_scissor); } break; @@ -1272,7 +1273,7 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) { // Flush remaining ID3D12DescriptorHeap *heap = bound_texture == 1 ? r->icon_srv_heap : r->srv_heap; - flush_batch(r, &batch, buf_idx, heap); + flush_batch(r, &batch, buf_idx, &flush_index_start, heap); } barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET; diff --git a/src/renderer/renderer_metal.mm b/src/renderer/renderer_metal.mm index 37dd008..0bde0cb 100644 --- a/src/renderer/renderer_metal.mm +++ b/src/renderer/renderer_metal.mm @@ -700,19 +700,20 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) { // Track which texture is currently bound (0 = font, 1 = icon) int bound_texture = 0; + U32 flush_index_start = 0; auto flush_batch = [&]() { - if (batch.index_count == 0) return; + U32 index_count = batch.index_count - flush_index_start; + if (index_count == 0) return; [encoder setVertexBuffer:r->vertex_buffers[buf_idx] offset:0 atIndex:0]; [encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle - indexCount:batch.index_count + indexCount:index_count indexType:MTLIndexTypeUInt32 indexBuffer:r->index_buffers[buf_idx] - indexBufferOffset:0]; + indexBufferOffset:flush_index_start * sizeof(U32)]; - batch.vertex_count = 0; - batch.index_count = 0; + flush_index_start = batch.index_count; }; auto bind_font_texture = [&]() {