From 13d95b76185d34b982033828fb8eaa2e50e8813f Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:47:59 +0100 Subject: [PATCH 1/2] d3d12: test bundle ExecuteIndirect(DISPATCH) and fix the stale bundle note (D3D-BUNDLE-EXECUTE-INDIRECT) ExecuteIndirect in a bundle is already deferred on main (single DRAW, DRAW_INDEXED, DISPATCH or DISPATCH_MESH, no count buffer); only DRAW and DRAW_INDEXED had a GPU test and docs/d3d12_vulkan.md still listed it as "Not deferred yet". d3d12_indirect_dispatch_count_test --bundle (ctest d3d12_bundle_indirect_dispatch) runs a fixed-count DISPATCH recorded in a bundle twice from a direct list with a different parent root UAV each time, then a direct Dispatch, on the layer, WARP and the default adapter, and checks that a count buffer or a root-constant signature fails the bundle's Close (E_INVALIDARG on both host adapters, as on the layer). Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 4 +- runtime/CMakeLists.txt | 2 + .../d3d12_indirect_dispatch_count_test.cpp | 221 ++++++++++++++++++ 3 files changed, 226 insertions(+), 1 deletion(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3084bf06b8..bab5d31e4a 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -164,7 +164,7 @@ The design is therefore an **IR per command list**: here, while the host hands out a video list (`E_NOINTERFACE` for `ID3D12GraphicsCommandList`), so they are not compared. - `Close` freezes the IR. `ExecuteBundle` is accepted only on a direct list, with an already-closed bundle as its callee; the bundle's recorded commands and calls are replayed at the call site, so the source can be reused or reset without changing the parent. Command-list creation and reset require an allocator of the same type. A bundle cannot be submitted directly to a command queue. -- Bundle restrictions are checked while recording for the IR operations currently implemented here (barriers, clears, copies, resolves, `SetPredication`, `DiscardResource`, the atomic copies, `RSSetViewports`, `RSSetScissorRects`, `OMSetRenderTargets` and `ClearState`; `WriteBufferImmediate` is allowed in a bundle); those calls are not legal inside a D3D12 bundle and make `Close` return `E_INVALIDARG`. `d3d12_bundle_forbidden_test` measured the last four on the PC runtime without the debug layer: the bundle's `Close` fails (`E_FAIL`, where this layer gives `E_INVALIDARG`) on the default adapter and on WARP, and executing a bundle whose `Close` failed crashes the runtime, so the layer refuses it in `ExecuteBundle` instead. A bundle has no render targets, viewport or root signature of its own, so its state calls (pipeline state, root signature and arguments, vertex and index buffers, topology, blend factor, stencil references) and its draws and dispatches are kept as they were called, and `ExecuteBundle` plays them on the parent through the same members the application would have called. A draw then snapshots the parent's state plus what the bundle set, and what the bundle set stays on the parent afterwards. Commands that need no state (markers, immediate writes) are copied between them in order. `d3d12_bundle_test` measured the rule on the PC runtime, where the default adapter (an RTX 4060 Ti) and WARP drew the same picture in every case it ran. A bundle's draws see the parent's render targets, viewport and scissor, root signature and arguments (also when the bundle sets the same root signature first, so those arguments stay), and vertex and index buffers. A bundle starts with the pipeline state it was created or reset with, not the parent's. After `ExecuteBundle` the parent has that pipeline state and whatever else the bundle set: topology, root arguments, vertex and index buffers, and a different root signature with the bundle's arguments in place of the parent's. Topology: the default adapter takes the parent's when the bundle sets none. That is more lenient than Microsoft's stated rule (topology is undefined when a bundle begins), and WARP follows the rule by removing the device when a bundle draws or hands back without one, so a bundle should set it; the layer follows the default adapter. A bundle that sets a root argument before any root signature crashes the NVIDIA driver (the layer applies it to the parent's signature). The deferred calls are checked when they play, against the parent's root signature, so a bad one makes the parent's `Close` fail, not the bundle's. A per-list state setter must go through the deferred path (`defer()` in `d3d12_com.cpp`, replaying through a private member that does not trace) to work in a bundle. Not deferred yet: `ExecuteIndirect` in a bundle, and bundle `Dispatch` has no GPU test. +- Bundle restrictions are checked while recording for the IR operations currently implemented here (barriers, clears, copies, resolves, `SetPredication`, `DiscardResource`, the atomic copies, `RSSetViewports`, `RSSetScissorRects`, `OMSetRenderTargets` and `ClearState`; `WriteBufferImmediate` is allowed in a bundle); those calls are not legal inside a D3D12 bundle and make `Close` return `E_INVALIDARG`. `d3d12_bundle_forbidden_test` measured the last four on the PC runtime without the debug layer: the bundle's `Close` fails (`E_FAIL`, where this layer gives `E_INVALIDARG`) on the default adapter and on WARP, and executing a bundle whose `Close` failed crashes the runtime, so the layer refuses it in `ExecuteBundle` instead. A bundle has no render targets, viewport or root signature of its own, so its state calls (pipeline state, root signature and arguments, vertex and index buffers, topology, blend factor, stencil references) and its draws and dispatches are kept as they were called, and `ExecuteBundle` plays them on the parent through the same members the application would have called. A draw then snapshots the parent's state plus what the bundle set, and what the bundle set stays on the parent afterwards. Commands that need no state (markers, immediate writes) are copied between them in order. `d3d12_bundle_test` measured the rule on the PC runtime, where the default adapter (an RTX 4060 Ti) and WARP drew the same picture in every case it ran. A bundle's draws see the parent's render targets, viewport and scissor, root signature and arguments (also when the bundle sets the same root signature first, so those arguments stay), and vertex and index buffers. A bundle starts with the pipeline state it was created or reset with, not the parent's. After `ExecuteBundle` the parent has that pipeline state and whatever else the bundle set: topology, root arguments, vertex and index buffers, and a different root signature with the bundle's arguments in place of the parent's. Topology: the default adapter takes the parent's when the bundle sets none. That is more lenient than Microsoft's stated rule (topology is undefined when a bundle begins), and WARP follows the rule by removing the device when a bundle draws or hands back without one, so a bundle should set it; the layer follows the default adapter. A bundle that sets a root argument before any root signature crashes the NVIDIA driver (the layer applies it to the parent's signature). The deferred calls are checked when they play, against the parent's root signature, so a bad one makes the parent's `Close` fail, not the bundle's. A per-list state setter must go through the deferred path (`defer()` in `d3d12_com.cpp`, replaying through a private member that does not trace) to work in a bundle. `ExecuteIndirect` in a bundle is deferred the same way (see `CreateCommandSignature` and `ExecuteIndirect` below): a single DRAW, DRAW_INDEXED, DISPATCH or DISPATCH_MESH operation with no count buffer plays at each `ExecuteBundle` with the parent's state at that call, and a count buffer or a signature with root or IA arguments makes the bundle's `Close` fail. `d3d12_bundle_indirect_dispatch` measured the DISPATCH case on the PC runtime (an RTX 4060, driver 610.88, and WARP, Windows 11 10.0.26300.9974): a bundle created with a compute pipeline and no root signature, executed twice with a different root UAV bound on the parent each time, wrote each run's counters through the parent's root UAV of that call, also when the bundle set the parent's root signature first, and a direct `Dispatch` after it still used the parent's root signature and arguments. Both host adapters return `E_INVALIDARG` from the bundle's `Close` for a count buffer and for a root-constant signature, the code this layer gives. Bundle `Dispatch` itself has no GPU test. - What `Close` returns after one recorded call is compared with the PC runtime over WARP (no debug layer) by `d3d12_close_parity_test`, 116 cases on fresh lists, nothing executed. 85 agree (three of them enhanced texture barriers, two into `LAYOUT_UNDEFINED`: section 4.3); the other 31 are listed in the @@ -523,6 +523,8 @@ What is implemented (all of it testable without the game): direct parent's state at that call. Count buffers and compound signatures remain invalid in bundles. `d3d12_bundle_indirect_warp` and `d3d12_bundle_indirect_layer` compare DRAW and DRAW_INDEXED against direct pixels with two different parent bindings; the layer fixture also checks marker order and captured views. + `d3d12_bundle_indirect_dispatch` checks DISPATCH the same way against the counters WARP and the default + adapter write (bundle section above). Compound graphics signatures, non-unit instance divisors and unsupported capabilities stay guarded. A GPU-counted draw currently requires `maxDrawIndirectCount == UINT32_MAX`: Vulkan also limits the raw counter value, not just the caller's maximum. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 9c433257d3..388dd43e49 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3171,6 +3171,8 @@ if(TARGET Vulkan::Headers) set_tests_properties(d3d12_indirect_group_budget PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_INDIRECT_GROUP_BUDGET=2") set_tests_properties(d3d12_indirect_dispatch_count PROPERTIES SKIP_RETURN_CODE 77) + add_test(NAME d3d12_bundle_indirect_dispatch COMMAND d3d12_indirect_dispatch_count_test --bundle) + set_tests_properties(d3d12_bundle_indirect_dispatch PROPERTIES SKIP_RETURN_CODE 77) # A single ordinary compute PSO and direct dispatch for cold/warm production journal comparisons; # only the authored two-counter shader is shared with the indirect test. diff --git a/runtime/test/d3d12_indirect_dispatch_count_test.cpp b/runtime/test/d3d12_indirect_dispatch_count_test.cpp index f53502b5d7..7e85b35219 100644 --- a/runtime/test/d3d12_indirect_dispatch_count_test.cpp +++ b/runtime/test/d3d12_indirect_dispatch_count_test.cpp @@ -164,6 +164,173 @@ std::array expected(UINT count, bool update, UINT second_count = 3) { return result; } +// --bundle: a fixed-count DISPATCH ExecuteIndirect recorded in a bundle and executed twice by a direct list. The +// bundle starts with the compute pipeline it was created with and has no root signature or root UAV of its own; the +// parent binds the root UAV at slot 0 for the first ExecuteBundle and at slot 1 for the second, then dispatches one +// group directly at slot 2. Each slot holds the shader's two counters (8 bytes, 64 bytes apart), so a slot shows +// which root arguments the indirect records ran with: three records of 1, 2 and 3 groups give 6/10 in slots 0 and +// 1, and the direct Dispatch(1, 1, 1) gives 1/1 in slot 2 (the bundle's pipeline stays on the parent, the parent's +// root signature and arguments stay as they were). `same_root`: the bundle sets the parent's root signature before the call, +// which keeps the parent's root arguments (docs/d3d12_vulkan.md, bundles). +constexpr UINT kSlot = 64; // bytes between the slots' counters +using BundleResult = std::array; + +bool run_bundle(ID3D12Device* dev, const std::vector& shader, bool same_root, BundleResult& result, + bool shim) { + Ref root; + D3D12_ROOT_PARAMETER param{}; + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; + param.Descriptor = {0, 0}; + param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; + D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; + root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; + root_desc.Desc_1_0 = {1, ¶m, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + const auto serialized = serialize_root_signature(root_desc); + if (serialized.empty() || + FAILED(dev->CreateRootSignature(0, serialized.data(), serialized.size(), IID_PPV_ARGS(&root.p)))) + return false; + D3D12_COMPUTE_PIPELINE_STATE_DESC desc{}; + desc.pRootSignature = root.p; + desc.CS = {shader.data(), shader.size()}; + Ref pso; + if (FAILED(dev->CreateComputePipelineState(&desc, IID_PPV_ARGS(&pso.p)))) return false; + const D3D12_INDIRECT_ARGUMENT_DESC argument{D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH}; + const D3D12_COMMAND_SIGNATURE_DESC signature_desc{kStride, 1, &argument, 0}; + Ref signature; + if (FAILED(dev->CreateCommandSignature(&signature_desc, nullptr, IID_PPV_ARGS(&signature.p)))) return false; + const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD}; + const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; + const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK}; + Ref source, args, output, readback; + const auto create = [&](Ref& dest, D3D12_HEAP_PROPERTIES heap, D3D12_RESOURCE_STATES state, + D3D12_RESOURCE_FLAGS flags) { + const auto buffer = buffer_desc(512, flags); + return SUCCEEDED(dev->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &buffer, state, nullptr, + IID_PPV_ARGS(&dest.p))); + }; + if (!create(source, upload_heap, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_FLAG_NONE) || + !create(args, default_heap, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_NONE) || + !create(output, default_heap, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) || + !create(readback, readback_heap, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_NONE)) + return false; + std::array input{}; // bytes 256..511 stay zero: the output's initial value + for (UINT i = 0; i < kMaximum; ++i) { + const UINT base = (kArgsOffset + i * kStride) / 4; + input[base] = i + 1; + input[base + 1] = input[base + 2] = 1; + input[base + 3] = 0xa5a5a5a5; // padding: must be ignored + input[base + 4] = 0x5a5a5a5a; + } + void* mapped = nullptr; + if (FAILED(source->Map(0, nullptr, &mapped)) || !mapped) return false; + std::memcpy(mapped, input.data(), sizeof(input)); + source->Unmap(0, nullptr); + + const D3D12_COMMAND_QUEUE_DESC queue_desc{D3D12_COMMAND_LIST_TYPE_DIRECT}; + Ref queue; + Ref allocator, bundle_allocator; + Ref list, bundle; + Ref fence; + if (FAILED(dev->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))) || + FAILED(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))) || + FAILED(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, IID_PPV_ARGS(&bundle_allocator.p))) || + FAILED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, bundle_allocator.p, pso.p, + IID_PPV_ARGS(&bundle.p))) || + FAILED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p))) || + FAILED(dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))) + return false; + if (same_root) bundle->SetComputeRootSignature(root.p); + bundle->ExecuteIndirect(signature.p, kMaximum, args.p, kArgsOffset, nullptr, 0); + if (FAILED(bundle->Close())) return false; + + list->CopyBufferRegion(args.p, kArgsOffset, source.p, kArgsOffset, kMaximum * kStride); + list->CopyBufferRegion(output.p, 0, source.p, 256, 3 * kSlot); + const D3D12_RESOURCE_BARRIER ready[] = { + transition(args.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT), + transition(output.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)}; + list->ResourceBarrier(2, ready); + list->SetComputeRootSignature(root.p); + const D3D12_GPU_VIRTUAL_ADDRESS va = output->GetGPUVirtualAddress(); + list->SetComputeRootUnorderedAccessView(0, va); + list->ExecuteBundle(bundle.p); + list->SetComputeRootUnorderedAccessView(0, va + kSlot); + list->ExecuteBundle(bundle.p); + list->SetComputeRootUnorderedAccessView(0, va + 2 * kSlot); + list->Dispatch(1, 1, 1); + const auto to_copy = transition(output.p, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_copy); + list->CopyBufferRegion(readback.p, 0, output.p, 0, 3 * kSlot); + if (FAILED(list->Close())) return false; + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + if (shim && rt::d3d12::last_execute_result(queue.p) != leonida::ReplayResult::success) return false; + if (FAILED(queue->Signal(fence.p, 1))) return false; + const HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); + if (!event) return false; + const bool completed = SUCCEEDED(fence->SetEventOnCompletion(1, event)) && + WaitForSingleObject(event, 20000) == WAIT_OBJECT_0; + CloseHandle(event); + if (!completed || FAILED(dev->GetDeviceRemovedReason()) || FAILED(readback->Map(0, nullptr, &mapped)) || !mapped) + return false; + const auto* words = static_cast(mapped); + for (UINT slot = 0; slot < 3; ++slot) { + result[2 * slot] = words[slot * kSlot / 4]; + result[2 * slot + 1] = words[slot * kSlot / 4 + 1]; + } + readback->Unmap(0, nullptr); + return true; +} + +// What a bundle's Close returns for the ExecuteIndirect calls a bundle may not make: a count buffer, and a signature +// that writes a root constant before its DISPATCH. Only Close is called: executing a bundle whose Close failed +// crashes the PC runtime (d3d12_bundle_forbidden_test). +bool bundle_refusals(ID3D12Device* dev, HRESULT& with_count, HRESULT& with_constant) { + D3D12_ROOT_PARAMETER params[2] = {}; + params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; + params[0].Descriptor = {0, 0}; + params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + params[1].Constants = {0, 0, 1}; + D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; + root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; + root_desc.Desc_1_0 = {2, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + const auto serialized = serialize_root_signature(root_desc); + Ref root; + if (serialized.empty() || + FAILED(dev->CreateRootSignature(0, serialized.data(), serialized.size(), IID_PPV_ARGS(&root.p)))) + return false; + const D3D12_INDIRECT_ARGUMENT_DESC dispatch{D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH}; + D3D12_INDIRECT_ARGUMENT_DESC compound[2] = {}; + compound[0].Type = D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT; + compound[0].Constant = {1, 0, 1}; + compound[1].Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH; + const D3D12_COMMAND_SIGNATURE_DESC simple_desc{kStride, 1, &dispatch, 0}; + const D3D12_COMMAND_SIGNATURE_DESC compound_desc{kStride, 2, compound, 0}; + Ref simple, constant; + if (FAILED(dev->CreateCommandSignature(&simple_desc, nullptr, IID_PPV_ARGS(&simple.p))) || + FAILED(dev->CreateCommandSignature(&compound_desc, root.p, IID_PPV_ARGS(&constant.p)))) + return false; + const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; + const auto buffer = buffer_desc(256); + Ref args; + if (FAILED(dev->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &buffer, + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, nullptr, IID_PPV_ARGS(&args.p)))) + return false; + const auto close = [&](ID3D12CommandSignature* sig, ID3D12Resource* count, HRESULT& hr) { + Ref allocator; + Ref bundle; + if (FAILED(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, IID_PPV_ARGS(&allocator.p))) || + FAILED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, allocator.p, nullptr, + IID_PPV_ARGS(&bundle.p)))) + return false; + if (sig == constant.p) bundle->SetComputeRootSignature(root.p); + bundle->ExecuteIndirect(sig, 1, args.p, 0, count, count ? 128 : 0); + hr = bundle->Close(); + return true; + }; + return close(simple.p, args.p, with_count) && close(constant.p, nullptr, with_constant); +} + // Build and revision of a loaded system DLL ("22621.5415"; Windows shows it as 10.0.22621.5415), or "unknown". std::string module_version(const wchar_t* name) { const HMODULE module = GetModuleHandleW(name); @@ -208,6 +375,8 @@ int make_host(Ref& host, std::vector& native_code) { int main(int argc, char** argv) { const bool clamp = argc == 2 && !std::strcmp(argv[1], "--clamp"); + const bool bundle = argc == 2 && !std::strcmp(argv[1], "--bundle"); + if (argc != 1 && !clamp && !bundle) return 2; // Wine has no WARP (its host is vkd3d on the GPU); the layer is still checked against expected(). const bool compare_host = !hostwine::host_is_wine(); Ref host; @@ -215,6 +384,33 @@ int main(int argc, char** argv) { if (!clamp && compare_host) { if (const int status = make_host(host, native_code)) return status; } + // --bundle runs on WARP and, outside Wine, on the default adapter too: both are held to the same values. + const BundleResult bundle_want{6, 10, 6, 10, 1, 1}; + unsigned bundle_hosts = 0; + if (bundle && compare_host) { + Ref adapter_default; + ID3D12Device* devices[] = {host.p, nullptr}; + const char* names[] = {"WARP", "the default adapter"}; + if (SUCCEEDED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&adapter_default.p)))) + devices[1] = adapter_default.p; + for (int i = 0; i < 2; ++i) { + if (!devices[i]) continue; + HRESULT with_count = S_OK, with_constant = S_OK; + if (!bundle_refusals(devices[i], with_count, with_constant)) return 1; + std::printf("%s: bundle ExecuteIndirect Close with a count buffer 0x%08lx, with a root constant 0x%08lx\n", + names[i], static_cast(with_count), static_cast(with_constant)); + if (SUCCEEDED(with_count) || SUCCEEDED(with_constant)) return 1; + for (const bool same_root : {false, true}) { + BundleResult got{}; + if (!run_bundle(devices[i], native_code, same_root, got, false) || got != bundle_want) { + std::fprintf(stderr, "%s same_root=%d: %u/%u %u/%u %u/%u, expected 6/10 6/10 1/1\n", names[i], + int(same_root), got[0], got[1], got[2], got[3], got[4], got[5]); + return 1; + } + } + ++bundle_hosts; + } + } vktest::Device vk; vk.request_buffer_device_address = true; const auto status = vk.initialize(); @@ -247,6 +443,31 @@ int main(int argc, char** argv) { Ref layer; if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&layer.p)))) return 1; + if (bundle) { + HRESULT with_count = S_OK, with_constant = S_OK; + if (!bundle_refusals(layer.p, with_count, with_constant) || with_count != E_INVALIDARG || + with_constant != E_INVALIDARG) { + std::fprintf(stderr, "layer: bundle ExecuteIndirect Close with a count buffer 0x%08lx, with a root " + "constant 0x%08lx, expected E_INVALIDARG for both\n", + static_cast(with_count), static_cast(with_constant)); + return 1; + } + for (const bool same_root : {false, true}) { + BundleResult got{}; + if (!run_bundle(layer.p, container, same_root, got, true) || got != bundle_want) { + std::fprintf(stderr, "layer same_root=%d: %u/%u %u/%u %u/%u, expected 6/10 6/10 1/1\n", + int(same_root), got[0], got[1], got[2], got[3], got[4], got[5]); + return 1; + } + } + if (vk.validation_errors()) { + std::fprintf(stderr, "validation errors: %u\n", vk.validation_errors()); + return 1; + } + std::printf("bundle ExecuteIndirect(DISPATCH): layer and %u host adapter(s) gave 6/10 6/10 1/1 in both " + "cases; count buffer and root constant refused\n", bundle_hosts); + return 0; + } if (clamp) { for (bool fixed : {false, true}) { for (UINT count : {0u, 1u, 7u}) { From b5447692f28361a4a0a0ad6c774dc656d9232418 Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Sat, 28 Nov 2026 20:42:41 +0100 Subject: [PATCH 2/2] test: catch a lost parent root UAV right after a bundle's ExecuteIndirect (review on #2575) After the second ExecuteBundle, a UAV barrier and a direct Dispatch run before any rebind, so slot 1 holds 7/11 only if the parent's root UAV survived the bundle. Layer, WARP and the default adapter (RTX 4060, 610.88) give 6/10 7/11 1/1 in both same_root cases, as measured by the reviewer on an RTX 5070. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 2 +- .../d3d12_indirect_dispatch_count_test.cpp | 24 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index bab5d31e4a..34eab704a3 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -164,7 +164,7 @@ The design is therefore an **IR per command list**: here, while the host hands out a video list (`E_NOINTERFACE` for `ID3D12GraphicsCommandList`), so they are not compared. - `Close` freezes the IR. `ExecuteBundle` is accepted only on a direct list, with an already-closed bundle as its callee; the bundle's recorded commands and calls are replayed at the call site, so the source can be reused or reset without changing the parent. Command-list creation and reset require an allocator of the same type. A bundle cannot be submitted directly to a command queue. -- Bundle restrictions are checked while recording for the IR operations currently implemented here (barriers, clears, copies, resolves, `SetPredication`, `DiscardResource`, the atomic copies, `RSSetViewports`, `RSSetScissorRects`, `OMSetRenderTargets` and `ClearState`; `WriteBufferImmediate` is allowed in a bundle); those calls are not legal inside a D3D12 bundle and make `Close` return `E_INVALIDARG`. `d3d12_bundle_forbidden_test` measured the last four on the PC runtime without the debug layer: the bundle's `Close` fails (`E_FAIL`, where this layer gives `E_INVALIDARG`) on the default adapter and on WARP, and executing a bundle whose `Close` failed crashes the runtime, so the layer refuses it in `ExecuteBundle` instead. A bundle has no render targets, viewport or root signature of its own, so its state calls (pipeline state, root signature and arguments, vertex and index buffers, topology, blend factor, stencil references) and its draws and dispatches are kept as they were called, and `ExecuteBundle` plays them on the parent through the same members the application would have called. A draw then snapshots the parent's state plus what the bundle set, and what the bundle set stays on the parent afterwards. Commands that need no state (markers, immediate writes) are copied between them in order. `d3d12_bundle_test` measured the rule on the PC runtime, where the default adapter (an RTX 4060 Ti) and WARP drew the same picture in every case it ran. A bundle's draws see the parent's render targets, viewport and scissor, root signature and arguments (also when the bundle sets the same root signature first, so those arguments stay), and vertex and index buffers. A bundle starts with the pipeline state it was created or reset with, not the parent's. After `ExecuteBundle` the parent has that pipeline state and whatever else the bundle set: topology, root arguments, vertex and index buffers, and a different root signature with the bundle's arguments in place of the parent's. Topology: the default adapter takes the parent's when the bundle sets none. That is more lenient than Microsoft's stated rule (topology is undefined when a bundle begins), and WARP follows the rule by removing the device when a bundle draws or hands back without one, so a bundle should set it; the layer follows the default adapter. A bundle that sets a root argument before any root signature crashes the NVIDIA driver (the layer applies it to the parent's signature). The deferred calls are checked when they play, against the parent's root signature, so a bad one makes the parent's `Close` fail, not the bundle's. A per-list state setter must go through the deferred path (`defer()` in `d3d12_com.cpp`, replaying through a private member that does not trace) to work in a bundle. `ExecuteIndirect` in a bundle is deferred the same way (see `CreateCommandSignature` and `ExecuteIndirect` below): a single DRAW, DRAW_INDEXED, DISPATCH or DISPATCH_MESH operation with no count buffer plays at each `ExecuteBundle` with the parent's state at that call, and a count buffer or a signature with root or IA arguments makes the bundle's `Close` fail. `d3d12_bundle_indirect_dispatch` measured the DISPATCH case on the PC runtime (an RTX 4060, driver 610.88, and WARP, Windows 11 10.0.26300.9974): a bundle created with a compute pipeline and no root signature, executed twice with a different root UAV bound on the parent each time, wrote each run's counters through the parent's root UAV of that call, also when the bundle set the parent's root signature first, and a direct `Dispatch` after it still used the parent's root signature and arguments. Both host adapters return `E_INVALIDARG` from the bundle's `Close` for a count buffer and for a root-constant signature, the code this layer gives. Bundle `Dispatch` itself has no GPU test. +- Bundle restrictions are checked while recording for the IR operations currently implemented here (barriers, clears, copies, resolves, `SetPredication`, `DiscardResource`, the atomic copies, `RSSetViewports`, `RSSetScissorRects`, `OMSetRenderTargets` and `ClearState`; `WriteBufferImmediate` is allowed in a bundle); those calls are not legal inside a D3D12 bundle and make `Close` return `E_INVALIDARG`. `d3d12_bundle_forbidden_test` measured the last four on the PC runtime without the debug layer: the bundle's `Close` fails (`E_FAIL`, where this layer gives `E_INVALIDARG`) on the default adapter and on WARP, and executing a bundle whose `Close` failed crashes the runtime, so the layer refuses it in `ExecuteBundle` instead. A bundle has no render targets, viewport or root signature of its own, so its state calls (pipeline state, root signature and arguments, vertex and index buffers, topology, blend factor, stencil references) and its draws and dispatches are kept as they were called, and `ExecuteBundle` plays them on the parent through the same members the application would have called. A draw then snapshots the parent's state plus what the bundle set, and what the bundle set stays on the parent afterwards. Commands that need no state (markers, immediate writes) are copied between them in order. `d3d12_bundle_test` measured the rule on the PC runtime, where the default adapter (an RTX 4060 Ti) and WARP drew the same picture in every case it ran. A bundle's draws see the parent's render targets, viewport and scissor, root signature and arguments (also when the bundle sets the same root signature first, so those arguments stay), and vertex and index buffers. A bundle starts with the pipeline state it was created or reset with, not the parent's. After `ExecuteBundle` the parent has that pipeline state and whatever else the bundle set: topology, root arguments, vertex and index buffers, and a different root signature with the bundle's arguments in place of the parent's. Topology: the default adapter takes the parent's when the bundle sets none. That is more lenient than Microsoft's stated rule (topology is undefined when a bundle begins), and WARP follows the rule by removing the device when a bundle draws or hands back without one, so a bundle should set it; the layer follows the default adapter. A bundle that sets a root argument before any root signature crashes the NVIDIA driver (the layer applies it to the parent's signature). The deferred calls are checked when they play, against the parent's root signature, so a bad one makes the parent's `Close` fail, not the bundle's. A per-list state setter must go through the deferred path (`defer()` in `d3d12_com.cpp`, replaying through a private member that does not trace) to work in a bundle. `ExecuteIndirect` in a bundle is deferred the same way (see `CreateCommandSignature` and `ExecuteIndirect` below): a single DRAW, DRAW_INDEXED, DISPATCH or DISPATCH_MESH operation with no count buffer plays at each `ExecuteBundle` with the parent's state at that call, and a count buffer or a signature with root or IA arguments makes the bundle's `Close` fail. `d3d12_bundle_indirect_dispatch` measured the DISPATCH case on the PC runtime (an RTX 4060, driver 610.88, and WARP, Windows 11 10.0.26300.9974): a bundle created with a compute pipeline and no root signature, executed twice with a different root UAV bound on the parent each time, wrote each run's counters through the parent's root UAV of that call, also when the bundle set the parent's root signature first, and a direct `Dispatch` right after the second `ExecuteBundle` (behind a UAV barrier, with no rebind) still wrote through the parent's root UAV, so the parent keeps its root signature and arguments. Both host adapters return `E_INVALIDARG` from the bundle's `Close` for a count buffer and for a root-constant signature, the code this layer gives. Bundle `Dispatch` itself has no GPU test. - What `Close` returns after one recorded call is compared with the PC runtime over WARP (no debug layer) by `d3d12_close_parity_test`, 116 cases on fresh lists, nothing executed. 85 agree (three of them enhanced texture barriers, two into `LAYOUT_UNDEFINED`: section 4.3); the other 31 are listed in the diff --git a/runtime/test/d3d12_indirect_dispatch_count_test.cpp b/runtime/test/d3d12_indirect_dispatch_count_test.cpp index 7e85b35219..0489a86636 100644 --- a/runtime/test/d3d12_indirect_dispatch_count_test.cpp +++ b/runtime/test/d3d12_indirect_dispatch_count_test.cpp @@ -168,10 +168,12 @@ std::array expected(UINT count, bool update, UINT second_count = 3) { // bundle starts with the compute pipeline it was created with and has no root signature or root UAV of its own; the // parent binds the root UAV at slot 0 for the first ExecuteBundle and at slot 1 for the second, then dispatches one // group directly at slot 2. Each slot holds the shader's two counters (8 bytes, 64 bytes apart), so a slot shows -// which root arguments the indirect records ran with: three records of 1, 2 and 3 groups give 6/10 in slots 0 and -// 1, and the direct Dispatch(1, 1, 1) gives 1/1 in slot 2 (the bundle's pipeline stays on the parent, the parent's -// root signature and arguments stay as they were). `same_root`: the bundle sets the parent's root signature before the call, -// which keeps the parent's root arguments (docs/d3d12_vulkan.md, bundles). +// which root arguments the indirect records ran with: three records of 1, 2 and 3 groups give 6/10 in slot 0. After +// the second ExecuteBundle a UAV barrier and a direct Dispatch(1, 1, 1) run before any rebind, so slot 1 holds 7/11 +// only if the parent's root UAV survived the bundle; the last direct Dispatch(1, 1, 1) gives 1/1 in slot 2 (the +// bundle's pipeline stays on the parent, the parent's root signature and arguments stay as they were). +// `same_root`: the bundle sets the parent's root signature before the call, which keeps the parent's root arguments +// (docs/d3d12_vulkan.md, bundles). constexpr UINT kSlot = 64; // bytes between the slots' counters using BundleResult = std::array; @@ -256,6 +258,12 @@ bool run_bundle(ID3D12Device* dev, const std::vector& shader, bool list->ExecuteBundle(bundle.p); list->SetComputeRootUnorderedAccessView(0, va + kSlot); list->ExecuteBundle(bundle.p); + // No rebind before this direct dispatch: it must still find the parent's root UAV (slot 1) after the bundle. + D3D12_RESOURCE_BARRIER uav{}; + uav.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; + uav.UAV.pResource = output.p; + list->ResourceBarrier(1, &uav); + list->Dispatch(1, 1, 1); list->SetComputeRootUnorderedAccessView(0, va + 2 * kSlot); list->Dispatch(1, 1, 1); const auto to_copy = transition(output.p, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); @@ -385,7 +393,7 @@ int main(int argc, char** argv) { if (const int status = make_host(host, native_code)) return status; } // --bundle runs on WARP and, outside Wine, on the default adapter too: both are held to the same values. - const BundleResult bundle_want{6, 10, 6, 10, 1, 1}; + const BundleResult bundle_want{6, 10, 7, 11, 1, 1}; unsigned bundle_hosts = 0; if (bundle && compare_host) { Ref adapter_default; @@ -403,7 +411,7 @@ int main(int argc, char** argv) { for (const bool same_root : {false, true}) { BundleResult got{}; if (!run_bundle(devices[i], native_code, same_root, got, false) || got != bundle_want) { - std::fprintf(stderr, "%s same_root=%d: %u/%u %u/%u %u/%u, expected 6/10 6/10 1/1\n", names[i], + std::fprintf(stderr, "%s same_root=%d: %u/%u %u/%u %u/%u, expected 6/10 7/11 1/1\n", names[i], int(same_root), got[0], got[1], got[2], got[3], got[4], got[5]); return 1; } @@ -455,7 +463,7 @@ int main(int argc, char** argv) { for (const bool same_root : {false, true}) { BundleResult got{}; if (!run_bundle(layer.p, container, same_root, got, true) || got != bundle_want) { - std::fprintf(stderr, "layer same_root=%d: %u/%u %u/%u %u/%u, expected 6/10 6/10 1/1\n", + std::fprintf(stderr, "layer same_root=%d: %u/%u %u/%u %u/%u, expected 6/10 7/11 1/1\n", int(same_root), got[0], got[1], got[2], got[3], got[4], got[5]); return 1; } @@ -464,7 +472,7 @@ int main(int argc, char** argv) { std::fprintf(stderr, "validation errors: %u\n", vk.validation_errors()); return 1; } - std::printf("bundle ExecuteIndirect(DISPATCH): layer and %u host adapter(s) gave 6/10 6/10 1/1 in both " + std::printf("bundle ExecuteIndirect(DISPATCH): layer and %u host adapter(s) gave 6/10 7/11 1/1 in both " "cases; count buffer and root constant refused\n", bundle_hosts); return 0; }