Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions runtime/test/d3d12_mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<leonida::Command> commands; // owns the payloads `recorded` points at
const auto indirect_draws = [&](std::vector<const leonida::IndirectRasterPayload*>& 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<leonida::IndirectRasterDraw>(&command))
out.push_back(recorded->get());
return true;
};
std::vector<const leonida::IndirectRasterPayload*> recorded;
// A root signature the mesh window (s16..s31) cannot hold is a recording error, as for DispatchMesh.
Ref<ID3D12RootSignature> 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)));

Expand Down
Loading