From 41ba252bb30dbcf39285e9fe8ee7fd748e7db2cd Mon Sep 17 00:00:00 2001 From: mrp1no <137657338+mrp1no@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:32:19 +0100 Subject: [PATCH 1/2] d3d12: CreateCommandSignature checks root arguments against the root signature, as the host does d3d12_command_signature_root_test compares CreateCommandSignature with a root signature on the Vulkan-backed device and on the host runtime: root constants (parameter index, offset, count), root CBV/SRV/UAV, vertex and index buffer views, their order and repeats, draws and dispatches, strides, and whether a refused call writes its output. That is 159 values, of which 98 differed on main (RTX 3060, driver 610.88). The layer accepted root arguments without looking at the root signature, accepted a root signature with no root argument, and cleared the output on a refusal. The host refuses all of these: - a root signature with no root argument; - a root argument whose parameter doesn't exist or is of another type (a view must name the same kind of root descriptor); - constants that don't fit their parameter or set no value; - root arguments out of parameter order, or overlapping or decreasing ranges of one parameter's constants; - a vertex buffer slot of 32 or more, a slot twice, a vertex buffer view with a dispatch, and an index buffer view twice, with a plain draw or with a dispatch. These vertex and index buffer rules hold without a root signature too. A refused call now leaves *out alone. The root signature model keeps the parameter's D3D12 type (parse_rts0 folded CBV/SRV/UAV into one kind), and RootArgumentLayout carries it, so the command signature can tell the three views apart. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 12 + runtime/CMakeLists.txt | 8 + runtime/shims/d3d12_com.h | 2 + runtime/shims/d3d12_vulkan_device.cpp | 61 +++- runtime/src/root_signature.cpp | 1 + runtime/src/root_signature.h | 4 + .../d3d12_command_signature_root_test.cpp | 305 ++++++++++++++++++ 7 files changed, 390 insertions(+), 3 deletions(-) create mode 100644 runtime/test/d3d12_command_signature_root_test.cpp diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8749bd331d..c651722746 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3193,6 +3193,18 @@ Not implemented yet: one Vulkan indirect call. Use a compute expansion pass that writes a Vulkan-shaped indirect buffer, or `VK_NV_device_generated_commands` on NVIDIA. Start with the simple signatures; the trace will show which signatures the game creates. +- `CreateCommandSignature` refuses with `E_INVALIDARG` what the host runtime refuses, with or without a root + signature, and a refused call leaves the output as it was. `d3d12_command_signature_root_test` compares 159 + values (RTX 3060, driver 610.88) beyond `compare_invalid_arguments`' signatures without one. The rules it + measured: + - A root signature is passed only with at least one root argument (constants, CBV, SRV or UAV); with none it + is refused. + - A root argument names a parameter of its type (root constants for `CONSTANT`, the same kind of root + descriptor for a view), and constants fit their parameter, at least one value. + - Root arguments come in increasing parameter order. Constants into one parameter come in increasing ranges + that don't overlap. + - A vertex buffer view uses a slot below 32, each slot once, and not with a dispatch. An index buffer view comes + once and not with a plain draw or a dispatch. Both rules hold without a root signature too. - Predication (`SetPredication`) maps to `VK_EXT_conditional_rendering` (D3D-18). Done: `GraphicsCommandList:: SetPredication` records a `Predication` command (`runtime/src/command_list.h`; offset 8-byte aligned, op `EQUAL_ZERO` or `NOT_EQUAL_ZERO`, a null buffer turns it off). The replay (`runtime/src/vulkan_replay.h`) begins diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 58153376e4..5cf5b7ddcf 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1875,6 +1875,14 @@ if(TARGET Vulkan::Headers) add_test(NAME d3d12_pipeline_library_host COMMAND d3d12_pipeline_library_host_test) set_tests_properties(d3d12_pipeline_library_host PROPERTIES SKIP_RETURN_CODE 77) + # CreateCommandSignature with a root signature (root constants, CBV/SRV/UAV, VB/IB views), compared with the host. + add_executable(d3d12_command_signature_root_test test/d3d12_command_signature_root_test.cpp) + target_link_libraries(d3d12_command_signature_root_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) + target_compile_definitions(d3d12_command_signature_root_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(d3d12_command_signature_root_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME d3d12_command_signature_root COMMAND d3d12_command_signature_root_test) + set_tests_properties(d3d12_command_signature_root PROPERTIES SKIP_RETURN_CODE 77) + # Milestone M0: a window, 60 cleared frames presented through the proxy on Vulkan. add_executable(d3d12_present_test test/d3d12_present_test.cpp) target_link_libraries(d3d12_present_test PRIVATE gta6rt_d3d12_com user32 gdi32 dxgi) diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index db5552b7f0..8f9aec11ce 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -352,6 +352,8 @@ struct RootArgumentLayout { std::uint32_t user_offset; // graphics: first dword; unused for a parameter with no dwords std::uint32_t constant_count; // constants only std::uint32_t compute_offset = rt::rootsig::kNoSgpr; // compute: first user SGPR + // D3D12_ROOT_PARAMETER_TYPE (tells a root CBV, SRV and UAV apart), or UINT32_MAX when it isn't known. + std::uint32_t d3d_type = 0xFFFFFFFFu; }; std::vector parameters; bool graphics_ok = true; diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 1316fefaad..42780342b4 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5211,12 +5211,24 @@ class Device final HRESULT STDMETHODCALLTYPE CreateCommandSignature(const D3D12_COMMAND_SIGNATURE_DESC* desc, ID3D12RootSignature* root_signature, REFIID riid, void** out) override { - if (out) *out = nullptr; + // A refused call leaves *out as it was, as the host does (d3d12_command_signature_root_test). // The host refuses these with E_INVALIDARG (compare_invalid_arguments, "command signature:"): one node, a // known type per argument, exactly one draw or dispatch and it comes last, a root signature for root // arguments, and a stride that is a multiple of 4 and holds every argument. if (!desc || desc->NumArgumentDescs == 0 || !desc->pArgumentDescs || (desc->NodeMask & ~1u)) return E_INVALIDARG; + // With a root signature (d3d12_command_signature_root_test, RTX 3060 / 610.88), the host also wants: at + // least one root argument (a root signature with none is refused), each naming a parameter of the right + // type (root constants for CONSTANT, the same root CBV/SRV/UAV for a view), constants that fit their + // parameter (at least one value), and root arguments in increasing parameter order, constants into one + // parameter in increasing ranges that don't overlap. Vertex buffer views need a slot below 32, each slot + // at most once, and no dispatch; an index buffer view comes at most once and not with a plain draw or a + // dispatch. The vertex and index buffer rules hold without a root signature too. + const RootArgumentLayout* layout = root_signature ? root_signature_layout(root_signature) : nullptr; + bool any_root_argument = false; + std::uint64_t root_position = 0; // where the last root argument ended: parameter << 33 | end of its range + std::uint32_t vertex_slots = 0; + UINT index_buffers = 0; UINT64 size = 0; // 64-bit: a large Num32BitValuesToSet must not wrap past ByteStride for (UINT i = 0; i < desc->NumArgumentDescs; ++i) { const D3D12_INDIRECT_ARGUMENT_DESC& arg = desc->pArgumentDescs[i]; @@ -5241,9 +5253,51 @@ class Device final const bool root_argument = !draw && arg.Type != D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW && arg.Type != D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW; if (draw != (i + 1 == desc->NumArgumentDescs) || (root_argument && !root_signature)) return E_INVALIDARG; - } + if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW) { + const UINT slot = arg.VertexBuffer.Slot; + if (slot >= D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT || (vertex_slots >> slot & 1u)) + return E_INVALIDARG; + vertex_slots |= 1u << slot; + } + if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW && ++index_buffers > 1) return E_INVALIDARG; + if (!root_argument) continue; + any_root_argument = true; + if (!layout) continue; // a root signature from another device: nothing to check against + // Every root argument type keeps its parameter index in the same place (the union's first member). + const UINT index = arg.Constant.RootParameterIndex; + if (index >= layout->parameters.size()) return E_INVALIDARG; + const RootArgumentLayout::Parameter& parameter = layout->parameters[index]; + std::uint64_t first = 0, end = 1; // a view takes its whole parameter + if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT) { + first = arg.Constant.DestOffsetIn32BitValues; + end = first + arg.Constant.Num32BitValuesToSet; + if (parameter.type != rt::rootsig::ParamType::Constants || end == first || + end > parameter.constant_count) + return E_INVALIDARG; + } else if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW || + arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW || + arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW) { + // D3D12_ROOT_PARAMETER_TYPE_CBV/SRV/UAV are 2/3/4, in the order of these argument types. + const std::uint32_t wanted = + D3D12_ROOT_PARAMETER_TYPE_CBV + (arg.Type - D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW); + if (parameter.type != rt::rootsig::ParamType::Descriptor || + (parameter.d3d_type != 0xFFFFFFFFu && parameter.d3d_type != wanted)) + return E_INVALIDARG; + } + // Parameter order, then the constants' ranges: each root argument starts where the last one ended or + // later (end values fit in 33 bits: a 32-bit offset plus a 32-bit count). + const std::uint64_t start = std::uint64_t(index) << 33 | first; + if (start < root_position) return E_INVALIDARG; + root_position = std::uint64_t(index) << 33 | end; + } + if (root_signature && !any_root_argument) return E_INVALIDARG; + const D3D12_INDIRECT_ARGUMENT_TYPE last = desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type; + // Measured against DISPATCH and DRAW; with DISPATCH_MESH (E_NOTIMPL below) the host's answer is not. + if ((vertex_slots || index_buffers) && last == D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH) return E_INVALIDARG; + if (index_buffers && last == D3D12_INDIRECT_ARGUMENT_TYPE_DRAW) return E_INVALIDARG; if (desc->ByteStride % 4 || desc->ByteStride < size) return E_INVALIDARG; if (!out) return E_UNEXPECTED; // the host's answer for a valid description and no output + *out = nullptr; leonida::IndirectKind kind; switch (desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type) { case D3D12_INDIRECT_ARGUMENT_TYPE_DRAW: kind = leonida::IndirectKind::draw; break; @@ -5449,7 +5503,8 @@ class Device final break; } layout.parameters.push_back({parsed.sig.params[i].type, first, parsed.sig.params[i].num_values, - compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr}); + compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr, + parsed.sig.params[i].d3d_type}); } register_static_samplers(parsed.sig); auto* signature = new RootSignature( diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index a0eb96cced..ea603b71be 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp @@ -360,6 +360,7 @@ static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { } RootParameter p; p.visibility = static_cast(vis); + p.d3d_type = dtype; if (dtype == 1) { p.type = ParamType::Constants; if (!u32_at(poff, p.shader_register) || !u32_at(poff + 4, p.register_space) || diff --git a/runtime/src/root_signature.h b/runtime/src/root_signature.h index 3769629400..891e6f6c0a 100644 --- a/runtime/src/root_signature.h +++ b/runtime/src/root_signature.h @@ -84,6 +84,10 @@ struct RootParameter { uint32_t descriptor_space = 0; // Descriptor table: std::vector ranges; + // D3D12_ROOT_PARAMETER_TYPE as the blob stores it (0 table, 1 constants, 2/3/4 CBV/SRV/UAV), which `type` + // folds into three kinds; set by parse_rts0, kUnknownD3dType for a parameter built another way. + static constexpr uint32_t kUnknownD3dType = 0xFFFFFFFFu; + uint32_t d3d_type = kUnknownD3dType; }; struct RootSignature { diff --git a/runtime/test/d3d12_command_signature_root_test.cpp b/runtime/test/d3d12_command_signature_root_test.cpp new file mode 100644 index 0000000000..cf776fbcaf --- /dev/null +++ b/runtime/test/d3d12_command_signature_root_test.cpp @@ -0,0 +1,305 @@ +// CreateCommandSignature with a root signature on the Vulkan-backed device, compared with the host PC runtime. +// compare_invalid_arguments (d3d12_vulkan_device_test) covers signatures without one; this covers the root arguments +// a signature can only have with one: root constants (parameter index, DestOffsetIn32BitValues, Num32BitValuesToSet), +// root CBV/SRV/UAV addresses (index and parameter type), vertex buffer slots and the index buffer view, alone and +// combined, with draws and dispatches, and a stride too small for them. Both devices get the same root signature, made +// from one blob serialized by the host runtime. Every HRESULT is recorded, and whether a refused call wrote its +// output. The two records must be the same, step by step. A step the host runtime faults on is reported and not +// compared. No shaders or game data. Skips (77) without a Vulkan GPU, and without a host D3D12 runtime (it serializes +// the root signature). Set LEONIDA_D3D49_VERBOSE=1 to print every step. +#include "vk_test_device.h" +#include "../shims/d3d12_com.h" + +#include +#include +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ +} while (0) + +namespace { + +template +struct Ref { + T* p = nullptr; + ~Ref() { reset(); } + void reset() { + if (p) p->Release(); + p = nullptr; + } + T* operator->() const { return p; } +}; + +constexpr std::uint64_t kFaulted = 0xFA017EDull; // the call raised an exception +void* const kSentinel = reinterpret_cast(std::uintptr_t(0x5E17E1)); + +// The root signature both devices get: parameter +// 0 4 root constants (b0) 3 root UAV (u0) +// 1 root CBV (b1) 4 descriptor table, one SRV range (t1) +// 2 root SRV (t0) 5 1 root constant (b2) +// with the input assembler allowed, so it serves draws and dispatches alike. +std::vector serialize_root_signature(HMODULE d3d12) { + const auto serialize = reinterpret_cast( + reinterpret_cast(GetProcAddress(d3d12, "D3D12SerializeRootSignature"))); + if (!serialize) return {}; + const D3D12_DESCRIPTOR_RANGE range{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 1, 0, 0}; + D3D12_ROOT_PARAMETER params[6]{}; + params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + params[0].Constants = {0, 0, 4}; + params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; + params[1].Descriptor = {1, 0}; + params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV; + params[2].Descriptor = {0, 0}; + params[3].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; + params[3].Descriptor = {0, 0}; + params[4].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; + params[4].DescriptorTable = {1, &range}; + params[5].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + params[5].Constants = {2, 0, 1}; + for (auto& p : params) p.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; + const D3D12_ROOT_SIGNATURE_DESC desc{6, params, 0, nullptr, + D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT}; + ID3DBlob* blob = nullptr; + ID3DBlob* error = nullptr; + std::vector out; + if (SUCCEEDED(serialize(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error)) && blob) { + const auto* bytes = static_cast(blob->GetBufferPointer()); + out.assign(bytes, bytes + blob->GetBufferSize()); + } + if (blob) blob->Release(); + if (error) error->Release(); + return out; +} + +D3D12_INDIRECT_ARGUMENT_DESC arg(D3D12_INDIRECT_ARGUMENT_TYPE type) { + D3D12_INDIRECT_ARGUMENT_DESC a{}; + a.Type = type; + return a; +} +D3D12_INDIRECT_ARGUMENT_DESC constant(UINT index, UINT offset, UINT count) { + D3D12_INDIRECT_ARGUMENT_DESC a = arg(D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT); + a.Constant = {index, offset, count}; + return a; +} +D3D12_INDIRECT_ARGUMENT_DESC view(D3D12_INDIRECT_ARGUMENT_TYPE type, UINT index) { + D3D12_INDIRECT_ARGUMENT_DESC a = arg(type); + a.ConstantBufferView.RootParameterIndex = index; // the same field for CBV, SRV and UAV + return a; +} +D3D12_INDIRECT_ARGUMENT_DESC cbv(UINT index) { return view(D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW, index); } +D3D12_INDIRECT_ARGUMENT_DESC srv(UINT index) { return view(D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW, index); } +D3D12_INDIRECT_ARGUMENT_DESC uav(UINT index) { return view(D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW, index); } +D3D12_INDIRECT_ARGUMENT_DESC vb(UINT slot) { + D3D12_INDIRECT_ARGUMENT_DESC a = arg(D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW); + a.VertexBuffer.Slot = slot; + return a; +} +const D3D12_INDIRECT_ARGUMENT_DESC kDraw = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW); // 16 bytes +const D3D12_INDIRECT_ARGUMENT_DESC kDrawIndexed = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED); // 20 bytes +const D3D12_INDIRECT_ARGUMENT_DESC kDispatch = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH); // 12 bytes +const D3D12_INDIRECT_ARGUMENT_DESC kIndexBuffer = arg(D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW); // 16 bytes + +struct Step { + std::string name; + UINT stride; + std::vector args; + bool with_root_signature = true; +}; + +std::vector steps() { + std::vector s; + const auto add = [&](std::string name, UINT stride, std::initializer_list args, + bool with_root_signature = true) { + s.push_back({std::move(name), stride, args, with_root_signature}); + }; + add("draw only", 16, {kDraw}); + add("dispatch only", 12, {kDispatch}); + // Root constants into parameter 0 (4 values) and 5 (1 value). + add("constant p0 [0, 1), draw", 20, {constant(0, 0, 1), kDraw}); + add("constant p0 [0, 4), draw", 32, {constant(0, 0, 4), kDraw}); + add("constant p0 [3, 4), draw", 20, {constant(0, 3, 1), kDraw}); + add("constant p0 [3, 5): past its 4 values", 24, {constant(0, 3, 2), kDraw}); + add("constant p0 [4, 5): offset at the end", 20, {constant(0, 4, 1), kDraw}); + add("constant p0 [0, 5): one value too many", 36, {constant(0, 0, 5), kDraw}); + add("constant p0, no values", 16, {constant(0, 0, 0), kDraw}); + add("constant p0 offset 0xffffffff", 20, {constant(0, 0xffffffffu, 1), kDraw}); + add("constant p5 [0, 1), draw", 20, {constant(5, 0, 1), kDraw}); + add("constant p5 [0, 2): past its 1 value", 24, {constant(5, 0, 2), kDraw}); + add("constant p6: no such parameter", 20, {constant(6, 0, 1), kDraw}); + add("constant p0xffffffff", 20, {constant(0xffffffffu, 0, 1), kDraw}); + add("constant p1: a root CBV", 20, {constant(1, 0, 1), kDraw}); + add("constant p4: a descriptor table", 20, {constant(4, 0, 1), kDraw}); + add("constant p0, dispatch", 16, {constant(0, 0, 1), kDispatch}); + add("constants p0 [0, 2) and [2, 4)", 32, {constant(0, 0, 2), constant(0, 2, 2), kDraw}); + add("constants p0 [0, 2) and [1, 3): overlapping", 32, {constant(0, 0, 2), constant(0, 1, 2), kDraw}); + add("constants p0 [0, 1) twice", 24, {constant(0, 0, 1), constant(0, 0, 1), kDraw}); + add("constants p0 and p5", 24, {constant(0, 0, 1), constant(5, 0, 1), kDraw}); + // Root descriptor addresses. + add("CBV p1, draw", 24, {cbv(1), kDraw}); + add("CBV p0: root constants", 24, {cbv(0), kDraw}); + add("CBV p2: a root SRV", 24, {cbv(2), kDraw}); + add("CBV p3: a root UAV", 24, {cbv(3), kDraw}); + add("CBV p4: a descriptor table", 24, {cbv(4), kDraw}); + add("CBV p6: no such parameter", 24, {cbv(6), kDraw}); + add("SRV p2, draw", 24, {srv(2), kDraw}); + add("SRV p1: a root CBV", 24, {srv(1), kDraw}); + add("SRV p3: a root UAV", 24, {srv(3), kDraw}); + add("UAV p3, dispatch", 20, {uav(3), kDispatch}); + add("UAV p2: a root SRV", 20, {uav(2), kDispatch}); + add("UAV p4: a descriptor table", 20, {uav(4), kDispatch}); + add("CBV p1 twice", 32, {cbv(1), cbv(1), kDraw}); + add("UAV p3, draw", 24, {uav(3), kDraw}); + add("SRV p2, dispatch", 20, {srv(2), kDispatch}); + add("CBV p1, dispatch", 20, {cbv(1), kDispatch}); + add("CBV p1, SRV p2 and constants p0, draw", 48, {cbv(1), srv(2), constant(0, 0, 4), kDraw}); + add("CBV p1, SRV p2, UAV p3 and constants p0, dispatch", 52, {cbv(1), srv(2), uav(3), constant(0, 0, 4), kDispatch}); + add("constants p0 then CBV p1, draw", 28, {constant(0, 0, 1), cbv(1), kDraw}); + add("CBV p1 then constants p0, draw", 28, {cbv(1), constant(0, 0, 1), kDraw}); + add("constant p5 then CBV p1, draw", 28, {constant(5, 0, 1), cbv(1), kDraw}); + add("constant p5 then constant p0, draw", 24, {constant(5, 0, 1), constant(0, 0, 1), kDraw}); + add("SRV p2 then CBV p1, draw", 32, {srv(2), cbv(1), kDraw}); + add("CBV p1 then SRV p2, draw", 32, {cbv(1), srv(2), kDraw}); + add("constants p0 [2, 4) then [0, 2), draw", 32, {constant(0, 2, 2), constant(0, 0, 2), kDraw}); + add("CBV p1, SRV p2, UAV p3, draw", 40, {cbv(1), srv(2), uav(3), kDraw}); + add("CBV p1, SRV p2, UAV p3 and constants p0", 56, {cbv(1), srv(2), uav(3), constant(0, 0, 4), kDraw}); + add("constant p0 [0, 1) and CBV p1, dispatch", 24, {constant(0, 0, 1), cbv(1), kDispatch}); + // Vertex and index buffer views. A root signature with no root argument is a case of its own, so each of these + // also sets a root constant (4 bytes, first) unless its name says "only". + add("VB slot 0 only", 32, {vb(0), kDraw}); + add("IB only, draw indexed", 36, {kIndexBuffer, kDrawIndexed}); + add("VB slot 0, draw", 36, {constant(5, 0, 1), vb(0), kDraw}); + add("VB slot 31, draw", 36, {constant(5, 0, 1), vb(31), kDraw}); + add("VB slot 32", 36, {constant(5, 0, 1), vb(32), kDraw}); + add("VB slot 0xffffffff", 36, {constant(5, 0, 1), vb(0xffffffffu), kDraw}); + add("VB slots 0 and 1, draw", 52, {constant(5, 0, 1), vb(0), vb(1), kDraw}); + add("VB slot 0 twice", 52, {constant(5, 0, 1), vb(0), vb(0), kDraw}); + add("VB slot 1 then slot 0", 52, {constant(5, 0, 1), vb(1), vb(0), kDraw}); + add("VB slot 0 before the root constant", 36, {vb(0), constant(5, 0, 1), kDraw}); + add("VB slot 0, dispatch", 32, {constant(5, 0, 1), vb(0), kDispatch}); + add("IB, draw indexed", 40, {constant(5, 0, 1), kIndexBuffer, kDrawIndexed}); + add("IB, draw (not indexed)", 36, {constant(5, 0, 1), kIndexBuffer, kDraw}); + add("IB twice, draw indexed", 56, {constant(5, 0, 1), kIndexBuffer, kIndexBuffer, kDrawIndexed}); + add("IB, dispatch", 32, {constant(5, 0, 1), kIndexBuffer, kDispatch}); + add("VB slot 0, IB and constant p0, draw indexed", 56, {vb(0), kIndexBuffer, constant(0, 0, 1), kDrawIndexed}); // Strides against the root arguments' size. + add("constant p0 [0, 4), draw, stride 31", 31, {constant(0, 0, 4), kDraw}); + add("constant p0 [0, 4), draw, stride 28 (too small)", 28, {constant(0, 0, 4), kDraw}); + add("CBV p1, draw, stride 20 (too small)", 20, {cbv(1), kDraw}); + add("CBV p1, draw, stride 256", 256, {cbv(1), kDraw}); + // The same root arguments without the root signature, and a valid one with it but a root argument after the draw. + add("constant p0 without a root signature", 20, {constant(0, 0, 1), kDraw}, false); + add("CBV p1 without a root signature", 24, {cbv(1), kDraw}, false); + add("VB slot 0, dispatch, without a root signature", 28, {vb(0), kDispatch}, false); + add("VB slot 32, draw, without a root signature", 32, {vb(32), kDraw}, false); + add("VB slot 0 twice, draw, without a root signature", 48, {vb(0), vb(0), kDraw}, false); + add("VB slots 0 and 1, draw, without a root signature", 48, {vb(0), vb(1), kDraw}, false); + add("IB, draw (not indexed), without a root signature", 32, {kIndexBuffer, kDraw}, false); + add("IB, draw indexed, without a root signature", 36, {kIndexBuffer, kDrawIndexed}, false); + add("IB twice, draw indexed, without a root signature", 52, {kIndexBuffer, kIndexBuffer, kDrawIndexed}, false); + add("draw, then constant p0", 20, {kDraw, constant(0, 0, 1)}); + return s; +} + +HRESULT create(ID3D12Device* device, const Step& s, ID3D12RootSignature* root, void** out) { + const D3D12_COMMAND_SIGNATURE_DESC desc{s.stride, UINT(s.args.size()), s.args.data(), 0}; + return device->CreateCommandSignature(&desc, s.with_root_signature ? root : nullptr, + __uuidof(ID3D12CommandSignature), out); +} + +HRESULT guarded(ID3D12Device* device, const Step* s, ID3D12RootSignature* root, void** out) { + __try { + return create(device, *s, root, out); + } __except (EXCEPTION_EXECUTE_HANDLER) { + return HRESULT(kFaulted); + } +} + +struct Record { + std::vector names; + std::vector values; + void add(std::string name, std::uint64_t value) { + names.push_back(std::move(name)); + values.push_back(value); + } +}; + +// The root signature from `blob` on `device`, then every step. A refused call's output is recorded: left alone, +// cleared to null, or (it shouldn't be) an object. +bool run(ID3D12Device* device, const std::vector& blob, Record& out) { + Ref root; + const HRESULT made = device->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p)); + out.add("CreateRootSignature", std::uint64_t(std::uint32_t(made))); + if (FAILED(made)) return false; + for (const Step& s : steps()) { + void* signature = kSentinel; + const HRESULT hr = guarded(device, &s, root.p, &signature); + out.add(s.name, std::uint64_t(std::uint32_t(hr))); + const bool written = signature != kSentinel; + if (SUCCEEDED(hr) && signature && written) static_cast(signature)->Release(); + out.add(s.name + " (output)", hr == HRESULT(kFaulted) ? kFaulted + : !written ? 0 + : !signature ? 1 + : 2); + } + return true; +} + +} // namespace + +int main() { + vktest::Device d; + const auto device_status = d.initialize(); + if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips + if (device_status != vktest::DeviceStatus::ready) return 1; + const bool verbose = GetEnvironmentVariableA("LEONIDA_D3D49_VERBOSE", nullptr, 0) > 0; + + const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + const auto create_host = d3d12 ? reinterpret_cast( + reinterpret_cast(GetProcAddress(d3d12, "D3D12CreateDevice"))) + : nullptr; + Ref host; + if (!create_host || FAILED(create_host(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) { + std::printf("command signatures with a root signature: no host D3D12 runtime, skipped\n"); + return 77; + } + const std::vector blob = serialize_root_signature(d3d12); + EXPECT(!blob.empty()); + Record theirs; + EXPECT(run(host.p, blob, theirs)); + + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = d.instance_proc; + info.instance = d.instance; + info.physical = d.physical; + info.device = d.device; + info.queue = d.queue; + info.queue_family = d.family; + Ref device; + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p)))); + Record ours; + EXPECT(run(device.p, blob, ours)); + EXPECT(theirs.names == ours.names); + + int mismatches = 0, faulted = 0; + for (std::size_t i = 0; i < ours.values.size(); ++i) { + if (theirs.values[i] == kFaulted) { + std::printf("command signature %s: the host faults, not compared (ours 0x%llx)\n", ours.names[i].c_str(), + static_cast(ours.values[i])); + ++faulted; + continue; + } + const bool same = ours.values[i] == theirs.values[i]; + if (verbose || !same) + std::printf("command signature %s: ours 0x%llx host 0x%llx%s\n", ours.names[i].c_str(), + static_cast(ours.values[i]), + static_cast(theirs.values[i]), same ? "" : " MISMATCH"); + mismatches += !same; + } + std::printf("command signatures with a root signature: %zu values compared with the host (%d more not compared), " + "%d mismatches\n", ours.values.size() - faulted, faulted, mismatches); + EXPECT(mismatches == 0); + std::printf("d3d12_command_signature_root_test: OK\n"); + return 0; +} From 0277b68c852a6164352acf40e9d06f810d83c34b Mon Sep 17 00:00:00 2001 From: mrp1no <137657338+mrp1no@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:53:23 +0100 Subject: [PATCH 2/2] d3d12: incrementing constants and mesh-dispatch signatures answer as the host does The command-signature comparison now also covers INCREMENTING_CONSTANT and DISPATCH_MESH: 26 more values, 14 of which differed (RTX 3060, driver 610.88). - An incrementing constant follows the root-constant rules: a root-constants parameter, an offset inside it, and no overlap with that parameter's other constants. The layer accepted any. - The host refuses an index buffer view with a mesh dispatch, so an index buffer view now comes only with an indexed draw. A vertex buffer view with a mesh dispatch is taken. - The host takes mesh-dispatch signatures, which the layer still answers with E_NOTIMPL until indirect mesh dispatch lands (#3777). The test leaves those four out of the comparison and says why. 177 values compared, 0 mismatches. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 10 ++++-- runtime/shims/d3d12_vulkan_device.cpp | 23 ++++++++----- .../d3d12_command_signature_root_test.cpp | 33 +++++++++++++++++++ 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index c651722746..a24c8b9e07 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3194,7 +3194,7 @@ Not implemented yet: `VK_NV_device_generated_commands` on NVIDIA. Start with the simple signatures; the trace will show which signatures the game creates. - `CreateCommandSignature` refuses with `E_INVALIDARG` what the host runtime refuses, with or without a root - signature, and a refused call leaves the output as it was. `d3d12_command_signature_root_test` compares 159 + signature, and a refused call leaves the output as it was. `d3d12_command_signature_root_test` compares 177 values (RTX 3060, driver 610.88) beyond `compare_invalid_arguments`' signatures without one. The rules it measured: - A root signature is passed only with at least one root argument (constants, CBV, SRV or UAV); with none it @@ -3203,8 +3203,12 @@ Not implemented yet: descriptor for a view), and constants fit their parameter, at least one value. - Root arguments come in increasing parameter order. Constants into one parameter come in increasing ranges that don't overlap. - - A vertex buffer view uses a slot below 32, each slot once, and not with a dispatch. An index buffer view comes - once and not with a plain draw or a dispatch. Both rules hold without a root signature too. + - An incrementing constant is one root constant: a root-constants parameter, an offset inside it, and no overlap + with the other constants of that parameter. + - A vertex buffer view uses a slot below 32, each slot once, and not with a compute dispatch (a mesh dispatch takes + one). An index buffer view comes once and only with an indexed draw. Both rules hold without a root signature too. + - The host takes mesh-dispatch signatures; the layer still answers `E_NOTIMPL` for them, so the test doesn't compare + those (indirect mesh dispatch is #3777). - Predication (`SetPredication`) maps to `VK_EXT_conditional_rendering` (D3D-18). Done: `GraphicsCommandList:: SetPredication` records a `Predication` command (`runtime/src/command_list.h`; offset 8-byte aligned, op `EQUAL_ZERO` or `NOT_EQUAL_ZERO`, a null buffer turns it off). The replay (`runtime/src/vulkan_replay.h`) begins diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 42780342b4..8bc1891bee 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5222,8 +5222,9 @@ class Device final // type (root constants for CONSTANT, the same root CBV/SRV/UAV for a view), constants that fit their // parameter (at least one value), and root arguments in increasing parameter order, constants into one // parameter in increasing ranges that don't overlap. Vertex buffer views need a slot below 32, each slot - // at most once, and no dispatch; an index buffer view comes at most once and not with a plain draw or a - // dispatch. The vertex and index buffer rules hold without a root signature too. + // at most once, and no compute dispatch; an index buffer view comes at most once and only with an indexed + // draw. The vertex and index buffer rules hold without a root signature too. An incrementing constant is one + // root constant. const RootArgumentLayout* layout = root_signature ? root_signature_layout(root_signature) : nullptr; bool any_root_argument = false; std::uint64_t root_position = 0; // where the last root argument ended: parameter << 33 | end of its range @@ -5268,9 +5269,14 @@ class Device final if (index >= layout->parameters.size()) return E_INVALIDARG; const RootArgumentLayout::Parameter& parameter = layout->parameters[index]; std::uint64_t first = 0, end = 1; // a view takes its whole parameter - if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT) { - first = arg.Constant.DestOffsetIn32BitValues; - end = first + arg.Constant.Num32BitValuesToSet; + if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT || + arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_INCREMENTING_CONSTANT) { + // An incrementing constant is one root constant the runtime counts up, with the same rules. + first = arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT + ? arg.Constant.DestOffsetIn32BitValues + : arg.IncrementingConstant.DestOffsetIn32BitValues; + end = first + (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT ? arg.Constant.Num32BitValuesToSet + : 1); if (parameter.type != rt::rootsig::ParamType::Constants || end == first || end > parameter.constant_count) return E_INVALIDARG; @@ -5292,9 +5298,10 @@ class Device final } if (root_signature && !any_root_argument) return E_INVALIDARG; const D3D12_INDIRECT_ARGUMENT_TYPE last = desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type; - // Measured against DISPATCH and DRAW; with DISPATCH_MESH (E_NOTIMPL below) the host's answer is not. - if ((vertex_slots || index_buffers) && last == D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH) return E_INVALIDARG; - if (index_buffers && last == D3D12_INDIRECT_ARGUMENT_TYPE_DRAW) return E_INVALIDARG; + // Measured against DRAW, DISPATCH and DISPATCH_MESH: the host takes a vertex buffer view with a mesh dispatch + // (E_NOTIMPL below here), an index buffer view only with an indexed draw. + if (vertex_slots && last == D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH) return E_INVALIDARG; + if (index_buffers && last != D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED) return E_INVALIDARG; if (desc->ByteStride % 4 || desc->ByteStride < size) return E_INVALIDARG; if (!out) return E_UNEXPECTED; // the host's answer for a valid description and no output *out = nullptr; diff --git a/runtime/test/d3d12_command_signature_root_test.cpp b/runtime/test/d3d12_command_signature_root_test.cpp index cf776fbcaf..787908b700 100644 --- a/runtime/test/d3d12_command_signature_root_test.cpp +++ b/runtime/test/d3d12_command_signature_root_test.cpp @@ -101,6 +101,12 @@ const D3D12_INDIRECT_ARGUMENT_DESC kDraw = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW const D3D12_INDIRECT_ARGUMENT_DESC kDrawIndexed = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED); // 20 bytes const D3D12_INDIRECT_ARGUMENT_DESC kDispatch = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH); // 12 bytes const D3D12_INDIRECT_ARGUMENT_DESC kIndexBuffer = arg(D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW); // 16 bytes +const D3D12_INDIRECT_ARGUMENT_DESC kDispatchMesh = arg(D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH); // 12 bytes +D3D12_INDIRECT_ARGUMENT_DESC incrementing(UINT index, UINT offset) { + D3D12_INDIRECT_ARGUMENT_DESC a = arg(D3D12_INDIRECT_ARGUMENT_TYPE_INCREMENTING_CONSTANT); + a.IncrementingConstant = {index, offset}; + return a; +} struct Step { std::string name; @@ -199,6 +205,20 @@ std::vector steps() { add("IB, draw indexed, without a root signature", 36, {kIndexBuffer, kDrawIndexed}, false); add("IB twice, draw indexed, without a root signature", 52, {kIndexBuffer, kIndexBuffer, kDrawIndexed}, false); add("draw, then constant p0", 20, {kDraw, constant(0, 0, 1)}); + // Incrementing constants (a root constant the runtime counts up per command) and mesh dispatches. + add("incrementing constant p5, draw", 20, {incrementing(5, 0), kDraw}); + add("incrementing constant p0 offset 3, draw", 20, {incrementing(0, 3), kDraw}); + add("incrementing constant p0 offset 4: past its 4 values", 20, {incrementing(0, 4), kDraw}); + add("incrementing constant p1: a root CBV", 20, {incrementing(1, 0), kDraw}); + add("incrementing constant p5 without a root signature", 20, {incrementing(5, 0), kDraw}, false); + add("incrementing constant p5, dispatch", 16, {incrementing(5, 0), kDispatch}); + add("constant p0 [0, 1) and incrementing constant p0 [0, 1)", 24, {constant(0, 0, 1), incrementing(0, 0), kDraw}); + add("dispatch mesh only", 12, {kDispatchMesh}); + add("constant p0 [0, 1), dispatch mesh", 16, {constant(0, 0, 1), kDispatchMesh}); + add("CBV p1, dispatch mesh", 20, {cbv(1), kDispatchMesh}); + add("VB slot 0 and constant p5, dispatch mesh", 32, {constant(5, 0, 1), vb(0), kDispatchMesh}); + add("IB and constant p5, dispatch mesh", 32, {constant(5, 0, 1), kIndexBuffer, kDispatchMesh}); + add("dispatch mesh only, without a root signature", 12, {kDispatchMesh}, false); return s; } @@ -284,6 +304,19 @@ int main() { int mismatches = 0, faulted = 0; for (std::size_t i = 0; i < ours.values.size(); ++i) { + // A mesh dispatch signature the host takes is E_NOTIMPL in the layer until indirect mesh dispatch lands + // (#3777): that HRESULT and its output row are not compared. Refused mesh signatures still are. + const std::size_t hr_row = ours.names[i].find("(output)") == std::string::npos ? i : i - 1; + const bool mesh_pending = ours.names[hr_row].find("dispatch mesh") != std::string::npos && + ours.values[hr_row] == std::uint32_t(E_NOTIMPL) && + SUCCEEDED(HRESULT(std::uint32_t(theirs.values[hr_row]))); if (mesh_pending) { + if (verbose) + std::printf("command signature %s: ours 0x%llx host 0x%llx (indirect mesh dispatch is #3777; not " + "compared)\n", ours.names[i].c_str(), static_cast(ours.values[i]), + static_cast(theirs.values[i])); + ++faulted; + continue; + } if (theirs.values[i] == kFaulted) { std::printf("command signature %s: the host faults, not compared (ours 0x%llx)\n", ours.names[i].c_str(), static_cast(ours.values[i]));