From 06cbd602c11fc4598561219606119353e1a804bc Mon Sep 17 00:00:00 2001 From: nmzik Date: Sun, 2 Aug 2026 21:53:19 +0200 Subject: [PATCH] graphics: preserve indirect instance state --- .../command_processor/commandProcessor.h | 9 ++++--- src/graphics/guest_gpu/graphicsRun.cpp | 25 +++++++++++++------ src/graphics/host_gpu/renderer/renderDraw.cpp | 12 ++------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/graphics/guest_gpu/command_processor/commandProcessor.h b/src/graphics/guest_gpu/command_processor/commandProcessor.h index 018598c1b..77e5e3283 100644 --- a/src/graphics/guest_gpu/command_processor/commandProcessor.h +++ b/src/graphics/guest_gpu/command_processor/commandProcessor.h @@ -83,8 +83,7 @@ public: uint32_t first_instance = 0); void DrawIndexOffset(uint32_t index_offset, uint32_t index_count, uint32_t flags); void DrawIndexAuto(uint32_t index_count, uint32_t flags, - uint32_t render_target_slice_offset = 0, uint32_t instance_count = 1, - uint32_t first_vertex = 0, uint32_t first_instance = 0); + uint32_t render_target_slice_offset = 0); void DrawIndirect(uint32_t data_offset, uint32_t draw_initiator, bool indexed); void DrawIndirectMulti(uint32_t data_offset, uint32_t max_count_or_count, const volatile uint32_t* count_addr, uint32_t stride_in_bytes, @@ -152,6 +151,9 @@ private: uint32_t interrupt_context_id); void ProcessPm4(Pm4Execution& execution, size_t stop_depth); void SuspendPm4(); + void SubmitNonIndexedDraw(uint32_t vertex_count, uint32_t flags, + uint32_t render_target_slice_offset, uint32_t first_vertex, + uint32_t first_instance); CommandScheduler& GetScheduler() const { return m_renderer.GetCommandScheduler(); } RenderCommandBuffer& CurrentBuffer() { return GetScheduler().Current(); } @@ -168,7 +170,8 @@ private: uint64_t m_index_base_addr = 0; uint64_t m_draw_indirect_args_base_addr = 0; uint64_t m_dispatch_indirect_args_base_addr = 0; - uint32_t m_num_instances = 1; + // Persistent draw state: indirect draws update it for subsequent draws. + uint32_t m_num_instances = 1; uint32_t m_de_count = 0; uint32_t m_ce_count = 0; diff --git a/src/graphics/guest_gpu/graphicsRun.cpp b/src/graphics/guest_gpu/graphicsRun.cpp index 2aac8dab6..44b49ffe2 100644 --- a/src/graphics/guest_gpu/graphicsRun.cpp +++ b/src/graphics/guest_gpu/graphicsRun.cpp @@ -983,8 +983,9 @@ void CommandProcessor::DrawIndirect(uint32_t data_offset, uint32_t draw_initiato args.start_vertex_location, args.start_instance_location); } } - DrawIndexAuto(args.vertex_count_per_instance, 0, 0, args.instance_count, - args.start_vertex_location, args.start_instance_location); + m_num_instances = args.instance_count; + SubmitNonIndexedDraw(args.vertex_count_per_instance, 0, 0, args.start_vertex_location, + args.start_instance_location); return; } @@ -1024,6 +1025,7 @@ void CommandProcessor::DrawIndirect(uint32_t data_offset, uint32_t draw_initiato } } + m_num_instances = args.instance_count; DrawIndex(index_count, index_addr, 0, 1, args.instance_count, nullptr, 0, static_cast(args.base_vertex_location), args.start_instance_location); } @@ -1081,8 +1083,9 @@ void CommandProcessor::DrawIndirectMulti(uint32_t data_offset, uint32_t max_coun args->start_vertex_location, args->start_instance_location); } } - DrawIndexAuto(args->vertex_count_per_instance, 0, 0, args->instance_count, - args->start_vertex_location, args->start_instance_location); + m_num_instances = args->instance_count; + SubmitNonIndexedDraw(args->vertex_count_per_instance, 0, 0, args->start_vertex_location, + args->start_instance_location); continue; } @@ -1123,6 +1126,7 @@ void CommandProcessor::DrawIndirectMulti(uint32_t data_offset, uint32_t max_coun } } + m_num_instances = args->instance_count; DrawIndex(index_count, index_addr, 0, 1, args->instance_count, nullptr, 0, static_cast(args->base_vertex_location), args->start_instance_location); } @@ -1214,12 +1218,17 @@ void CommandProcessor::DispatchIndirect(uint32_t data_offset, uint32_t mode) { } void CommandProcessor::DrawIndexAuto(uint32_t index_count, uint32_t flags, - uint32_t render_target_slice_offset, uint32_t instance_count, - uint32_t first_vertex, uint32_t first_instance) { + uint32_t render_target_slice_offset) { + SubmitNonIndexedDraw(index_count, flags, render_target_slice_offset, 0, 0); +} + +void CommandProcessor::SubmitNonIndexedDraw(uint32_t vertex_count, uint32_t flags, + uint32_t render_target_slice_offset, + uint32_t first_vertex, uint32_t first_instance) { CheckBuffer(); - m_renderer.GetRenderExecutor().DrawAuto(m_submit_id, CurrentBuffer(), index_count, flags, - render_target_slice_offset, instance_count, + m_renderer.GetRenderExecutor().DrawAuto(m_submit_id, CurrentBuffer(), vertex_count, flags, + render_target_slice_offset, m_num_instances, first_vertex, first_instance); } diff --git a/src/graphics/host_gpu/renderer/renderDraw.cpp b/src/graphics/host_gpu/renderer/renderDraw.cpp index 66e21e513..d12b5ed29 100644 --- a/src/graphics/host_gpu/renderer/renderDraw.cpp +++ b/src/graphics/host_gpu/renderer/renderDraw.cpp @@ -1159,7 +1159,7 @@ void RenderExecutor::DrawIndex(uint64_t submit_id, RenderCommandBuffer& buffer, reinterpret_cast(index_addr)); Common::LockGuard lock(m_context.GetMutex()); - if (index_count == 0) { + if (index_count == 0 || instance_count == 0) { return; } @@ -1229,10 +1229,6 @@ void RenderExecutor::DrawIndex(uint64_t submit_id, RenderCommandBuffer& buffer, EXIT_NOT_IMPLEMENTED(flags != 0); EXIT_NOT_IMPLEMENTED(type != 1); - if (instance_count == 0) { - instance_count = 1; - } - const DrawCallInfo draw {"DrawIndex", CommandBufferDebugOp::DrawIndex, index_count, flags, instance_count, first_instance}; @@ -1292,7 +1288,7 @@ void RenderExecutor::DrawAuto(uint64_t submit_id, RenderCommandBuffer& buffer, u index_count, flags, first_vertex, instance_count, first_instance); Common::LockGuard lock(m_context.GetMutex()); - if (index_count == 0) { + if (index_count == 0 || instance_count == 0) { return; } @@ -1330,10 +1326,6 @@ void RenderExecutor::DrawAuto(uint64_t submit_id, RenderCommandBuffer& buffer, u hw_check(buffer); EXIT_NOT_IMPLEMENTED(flags != 0); - if (instance_count == 0) { - instance_count = 1; - } - const DrawCallInfo draw {"DrawIndexAuto", CommandBufferDebugOp::DrawIndexAuto, index_count, flags, instance_count, first_instance};