diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index d988a45f14..bb6f313aa6 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3788,7 +3788,12 @@ Not implemented yet: - Signatures that change root constants, vertex/index buffers or descriptors per argument cannot be expressed by 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. + signatures the game creates. Until then `CreateCommandSignature` refuses a valid signature with more than one + argument with `E_NOTIMPL`, logged once per first argument type (D3D-INDIRECT-STATE-REFUSE): the signature keeps + only its kind and stride and `ExecuteIndirect` reads the draw's arguments at the start of each record, so + accepting one would draw with the state argument's bytes as counts. Invalid descriptions are still + `E_INVALIDARG`, as on the host. The title's five startup signatures each have one argument (section 4.9). + Checked by `d3d12_execute_indirect_validation`. - 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 5dd6b42e47..d89a91cdbe 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -11734,6 +11734,17 @@ class Device final } 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 + // A command signature keeps only its kind and stride, and ExecuteIndirect reads the draw or dispatch + // arguments at the start of each record. Arguments before them (root constants, root views, vertex and index + // buffer views) change state per command, which the replay does not do yet: accepted, their bytes would be + // read as the draw's arguments. So such a signature is refused, logged once per first argument type. The + // title's startup signatures each have one argument. + if (desc->NumArgumentDescs > 1) { + log_once("CreateCommandSignature refused (not implemented): arguments before the draw or dispatch, " + "first argument type", + int(desc->pArgumentDescs[0].Type)); + return E_NOTIMPL; + } leonida::IndirectKind kind; switch (desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type) { case D3D12_INDIRECT_ARGUMENT_TYPE_DRAW: kind = leonida::IndirectKind::draw; break; diff --git a/runtime/test/d3d12_execute_indirect_validation_test.cpp b/runtime/test/d3d12_execute_indirect_validation_test.cpp index 39a4961f82..d6b11ee9d5 100644 --- a/runtime/test/d3d12_execute_indirect_validation_test.cpp +++ b/runtime/test/d3d12_execute_indirect_validation_test.cpp @@ -333,6 +333,77 @@ bool run_positive_gpu_dispatch(Env& env) { return true; } +// Layer-only. Valid D3D12 signatures that put state-changing arguments before the draw or dispatch: the layer keeps +// only a signature's kind and stride and reads the draw's arguments at the start of each record, so it refuses them +// (E_NOTIMPL, no object) rather than drawing with those bytes as counts. The root signature is the empty one; its +// parameter indexes are not checked by the layer, which refuses before it would need them. +bool refuses_state_arguments(Env& env) { + CHECK(env.empty_root.p); + struct Shape { + const char* name; + UINT stride; + std::vector types; + }; + const Shape shapes[] = { + {"vertex buffer view, draw", 32, + {D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW}}, + {"index buffer view, draw indexed", 36, + {D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED}}, + {"constant, draw", 20, {D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW}}, + {"constant, dispatch", 16, {D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT, D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH}}, + {"CBV, draw", 24, + {D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW}}, + {"SRV, draw indexed", 28, + {D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED}}, + {"UAV, dispatch", 20, + {D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW, D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH}}, + {"vertex buffer view, constant, draw", 36, + {D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW, D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT, + D3D12_INDIRECT_ARGUMENT_TYPE_DRAW}}, + }; + for (const Shape& shape : shapes) { + std::vector args; + for (const D3D12_INDIRECT_ARGUMENT_TYPE type : shape.types) { + D3D12_INDIRECT_ARGUMENT_DESC arg{}; + arg.Type = type; + if (type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT) arg.Constant.Num32BitValuesToSet = 1; + args.push_back(arg); + } + const D3D12_COMMAND_SIGNATURE_DESC desc{shape.stride, UINT(args.size()), args.data(), 0}; + Ref sig; + const HRESULT hr = env.device->CreateCommandSignature(&desc, env.empty_root.p, IID_PPV_ARGS(&sig.p)); + if (hr != E_NOTIMPL || sig.p) { + std::fprintf(stderr, "FAIL state-argument signature (%s): hr=0x%08lx, object %s\n", shape.name, + static_cast(hr), sig.p ? "created" : "null"); + return false; + } + } + // An invalid description is still E_INVALIDARG, checked before the refusal: a root argument with no root + // signature, and a stride that cannot hold the record. + { + D3D12_INDIRECT_ARGUMENT_DESC args[2]{}; + args[0].Type = D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT; + args[0].Constant.Num32BitValuesToSet = 1; + args[1].Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW; + D3D12_COMMAND_SIGNATURE_DESC desc{20, 2, args, 0}; + Ref sig; + CHECK(env.device->CreateCommandSignature(&desc, nullptr, IID_PPV_ARGS(&sig.p)) == E_INVALIDARG && !sig.p); + desc.ByteStride = 16; + CHECK(env.device->CreateCommandSignature(&desc, env.empty_root.p, IID_PPV_ARGS(&sig.p)) == E_INVALIDARG && + !sig.p); + } + // A one-argument signature with a root signature is still created. + { + D3D12_INDIRECT_ARGUMENT_DESC arg{}; + arg.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW; + const D3D12_COMMAND_SIGNATURE_DESC desc{16, 1, &arg, 0}; + Ref sig; + CHECK(SUCCEEDED(env.device->CreateCommandSignature(&desc, env.empty_root.p, IID_PPV_ARGS(&sig.p))) && sig.p); + } + std::printf("state-argument signatures: %zu shapes refused with E_NOTIMPL\n", std::size(shapes)); + return true; +} + } // namespace int main() { @@ -399,6 +470,9 @@ int main() { null_hr)); REQUIRE(FAILED(null_hr)); + // Layer-only: signatures with state-changing arguments before the draw or dispatch are refused. + REQUIRE(refuses_state_arguments(env)); + // Positive valid GPU ExecuteIndirect dispatch on Vulkan. REQUIRE(run_positive_gpu_dispatch(env)); }