From 455e574b721d6485402eb78f095bd913afea4e1b Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:13:05 -0600 Subject: [PATCH] d3d12: ExecuteIndirect with DISPATCH_MESH records as DispatchMesh does (RT-INDIRECT-DISPATCH-MESH-PARITY) GraphicsCommandList::record_indirect for IndirectKind::dispatch_mesh differed from DispatchMesh and the indirect DRAW/DRAW_INDEXED path in three ways: - it tested viewport 0 only, so a call with viewport 0 empty and another viewport set recorded nothing; it now uses any_nonempty_viewport(); - it did not set draw.viewports, so the replay drew with viewport 0 and scissor 0 alone; it now sets draw_viewports(); - it had no mesh_ok check, so a root signature larger than a mesh shader's 16-dword window recorded; it is now a recording error, as for DispatchMesh. The root-window check and its direct-heap notes move into one helper that both paths call. d3d12_mesh_test records each case; each check fails with its part of the fix reverted. Found by kevincommits on #1608. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_com.cpp | 52 ++++++++++++++++++++------------ runtime/test/d3d12_mesh_test.cpp | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4a61ffadb..90c2729a0 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -555,26 +555,8 @@ class GraphicsCommandList final d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); if (!graphics_pipeline_ || !graphics_.layout) return check(false); - // A mesh shader reads the root arguments from s16 through a 16-dword window (RootArgumentLayout::mesh_ok). - if (!graphics_.layout->mesh_ok) { - stub("DispatchMesh(root signature larger than a mesh shader's root window)"); + if (!mesh_root_window_ok("DispatchMesh(root signature larger than a mesh shader's root window)")) return check(false); - } - // The directly indexed heaps follow the arguments (graphics_user_data). The window shows a mesh shader push - // dwords 8..23 only (s16..s31), so a signature that fills it leaves a heap where the mesh stage cannot read. - { - constexpr std::uint32_t window_end = rt::rootsig::stage_base(rt::rootsig::Stage::Pixel) + - rt::rootsig::kMaxUserSgprs - - rt::rootsig::stage_base(rt::rootsig::Stage::Mesh); - const auto flags = graphics_.layout->flags; - const bool resource = flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; - const bool sampler = flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; - const std::uint32_t end = graphics_.layout->graphics_arguments_end; - if (resource && end >= window_end) - note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the resource heap"); - if (sampler && end + (resource ? 1u : 0u) >= window_end) - note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the sampler heap"); - } if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; @@ -2037,6 +2019,30 @@ class GraphicsCommandList final return draw_viewports_; } + // A mesh shader reads the root arguments from s16 through a 16-dword window (RootArgumentLayout::mesh_ok): a + // larger signature is a recording error for DispatchMesh and for ExecuteIndirect with DISPATCH_MESH alike + // (`refused` is logged once). The directly indexed heaps follow the arguments (graphics_user_data). The window + // shows a mesh shader push dwords 8..23 only (s16..s31), so a signature that fills it leaves a heap where the mesh + // stage cannot read: noted once, the draw still records. + bool mesh_root_window_ok(const char* refused) const { + if (!graphics_.layout->mesh_ok) { + stub(refused); + return false; + } + constexpr std::uint32_t window_end = rt::rootsig::stage_base(rt::rootsig::Stage::Pixel) + + rt::rootsig::kMaxUserSgprs - + rt::rootsig::stage_base(rt::rootsig::Stage::Mesh); + const auto flags = graphics_.layout->flags; + const bool resource = flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED; + const bool sampler = flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED; + const std::uint32_t end = graphics_.layout->graphics_arguments_end; + if (resource && end >= window_end) + note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the resource heap"); + if (sampler && end + (resource ? 1u : 0u) >= window_end) + note_once("mesh direct heaps: no SGPR left in the mesh window (s16..s31) for the sampler heap"); + return true; + } + // LEONIDA_ASYNC_PIPELINES=1: the variant this draw needs (by the targets it binds) starts building on the compile // worker now, before the list is submitted (request_pipeline_build). void request_draw_pipeline() const { @@ -2061,11 +2067,17 @@ class GraphicsCommandList final if (!terminal_only) return skip(stats::compound); if (!graphics_pipeline_) return skip(stats::pipeline); if (!graphics_.layout) return skip(stats::root_layout); - if (!(viewport_.width > 0) || !(viewport_.height > 0)) return skip(stats::viewport); + // RT-INDIRECT-DISPATCH-MESH-PARITY: the same root-window check, viewport test and viewport/scissor arrays + // as DispatchMesh and the indirect DRAW/DRAW_INDEXED path (found by @kevincommits on #1608). + if (!mesh_root_window_ok("ExecuteIndirect(DISPATCH_MESH, root signature larger than a mesh shader's root " + "window)")) + return check(false); + if (!any_nonempty_viewport()) return skip(stats::viewport); leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, 0, 0, 0, 0, 0}; note_direct_heap_indexing(graphics_.layout); draw.mesh = true; + draw.viewports = draw_viewports(); draw.depth_bounds = depth_bounds_; request_draw_pipeline(); const bool recorded = list_.draw_indirect(std::move(draw), args); diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 7b529546b..b04e9e08c 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -15,6 +15,9 @@ // invalid_draw); a mesh grid beyond D3D12's limits fails at Close; an empty grid records nothing. // - A DISPATCH_MESH command signature is created only with the extension; ExecuteIndirect with it draws as // DispatchMesh (one record {2, 1, 1} fills two cells), and without graphics state it records nothing and closes. +// It records as DispatchMesh does (RT-INDIRECT-DISPATCH-MESH-PARITY): a root signature wider than the mesh window +// fails Close, a non-empty viewport other than viewport 0 is enough, and the viewport and scissor arrays go +// with the draw. // The shaders are GLSL written to the translator's contract (test/glsl/ms_draw.mesh, ps_draw.frag, vs_fullscreen.vert). // No game data. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation. #include "vk_test_device.h" @@ -690,6 +693,55 @@ int main(int argc, char** argv) { list->SetGraphicsRootSignature(root.p); list->ExecuteIndirect(sig.p, 1, upload.p, 0, nullptr, 0); EXPECT(list->Close() == S_OK); + + // RT-INDIRECT-DISPATCH-MESH-PARITY (@kevincommits, #1608): ExecuteIndirect with DISPATCH_MESH records as + // DispatchMesh does. Recording only; nothing here is executed. + std::vector commands; // owns the payloads `recorded` points at + const auto indirect_draws = [&](std::vector& out) { + out.clear(); + commands.clear(); + if (!rt::d3d12::recorded_commands(list.p, commands)) return false; + for (const auto& command : commands) + if (const auto* recorded = std::get_if(&command)) + out.push_back(recorded->get()); + return true; + }; + std::vector recorded; + // A root signature the mesh window (s16..s31) cannot hold is a recording error, as for DispatchMesh. + Ref wide; + EXPECT(wide_root_signature(device.p, wide)); + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(wide.p); + list->RSSetViewports(1, &viewport); + list->ExecuteIndirect(sig.p, 1, upload.p, 0, nullptr, 0); + EXPECT(list->Close() == E_INVALIDARG); + // Viewport 0 empty, viewport 1 not: the draw is recorded (any_nonempty_viewport), as DispatchMesh's. + D3D12_VIEWPORT two[2] = {viewport, viewport}; + two[0].Width = 0; + const D3D12_RECT two_scissors[2] = {scissor, scissor}; + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(root.p); + list->RSSetViewports(2, two); + list->RSSetScissorRects(2, two_scissors); + list->ExecuteIndirect(sig.p, 1, upload.p, 0, nullptr, 0); + list6->DispatchMesh(1, 1, 1); // the direct call, for comparison + EXPECT(list->Close() == S_OK); + EXPECT(indirect_draws(recorded)); + EXPECT(recorded.size() == 1 && recorded[0]->state.mesh); + // The viewport and scissor arrays travel with it (draw.viewports), not viewport 0 alone. + const auto* arrays = recorded[0]->state.viewports.get(); + EXPECT(arrays && arrays->viewport_count == 2 && arrays->scissor_count == 2); + EXPECT(arrays->viewports[0].width == 0 && arrays->viewports[1].width == viewport.Width); + // Every viewport empty: still nothing recorded, and the list closes. + two[1].Width = 0; + EXPECT(SUCCEEDED(list->Reset(allocator.p, mesh_pso.p))); + list->SetGraphicsRootSignature(root.p); + list->RSSetViewports(2, two); + list->ExecuteIndirect(sig.p, 1, upload.p, 0, nullptr, 0); + EXPECT(list->Close() == S_OK); + EXPECT(indirect_draws(recorded) && recorded.empty()); + std::printf("ExecuteIndirect DISPATCH_MESH records as DispatchMesh: root window checked, viewport 1 of 2 " + "drawn, viewport and scissor arrays kept\n"); } EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr)));