diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index e23985ecb3..49d22dd5a2 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. Fixed-count `ExecuteIndirect` with a single `DISPATCH` argument is deferred too. It captures the compute pipeline and root arguments each time the parent executes the bundle. Count-buffer and state-changing signatures remain unsupported in bundles. Checked against WARP by `d3d12_indirect_dispatch_count_test`; direct bundle `Dispatch` 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/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 883cce32a1..22669ffd48 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -144,15 +144,18 @@ class CommandSignature final // {9a4e1c2b-55d8-4f7a-b012-3c8e7a1f6d90} static constexpr GUID kTag{0x9a4e1c2b, 0x55d8, 0x4f7a, {0xb0, 0x12, 0x3c, 0x8e, 0x7a, 0x1f, 0x6d, 0x90}}; - CommandSignature(leonida::IndirectKind kind, UINT byte_stride) : kind_(kind), stride_(byte_stride) {} + CommandSignature(leonida::IndirectKind kind, UINT byte_stride, UINT argument_count) + : kind_(kind), stride_(byte_stride), argument_count_(argument_count) {} leonida::IndirectKind kind() const { return kind_; } UINT stride() const { return stride_; } + UINT argument_count() const { return argument_count_; } static CommandSignature* from(IUnknown* object) { return Child::from(object); } private: leonida::IndirectKind kind_; UINT stride_; + UINT argument_count_; }; class CommandAllocator final @@ -954,10 +957,12 @@ class GraphicsCommandList final void STDMETHODCALLTYPE ExecuteIndirect(ID3D12CommandSignature* signature, UINT max_command_count, ID3D12Resource* argument_buffer, UINT64 argument_buffer_offset, ID3D12Resource* count_buffer, UINT64 count_buffer_offset) override { - if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); - leonida::IndirectKind kind = leonida::IndirectKind::draw; - UINT stride = 0; - if (!command_signature_info(signature, kind, stride) || !argument_buffer) + const auto* sig = CommandSignature::from(signature); + if (!sig || !argument_buffer) return check(false); + const auto kind = sig->kind(); + const UINT stride = sig->stride(); + if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE && + (kind != leonida::IndirectKind::dispatch || sig->argument_count() != 1 || count_buffer)) return check(false); const auto arg_desc = argument_buffer->GetDesc(); if (arg_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || argument_buffer_offset % 4 != 0) @@ -1008,12 +1013,8 @@ class GraphicsCommandList final cmd.argument_offset = argument_buffer_offset; cmd.count_buffer = count_buffer ? resource_id(count_buffer) : 0; cmd.count_offset = count_buffer ? count_buffer_offset : 0; - if (kind == leonida::IndirectKind::dispatch && max_command_count) { // the pipeline and root arguments a Dispatch would record - if (!compute_pipeline_ || !compute_.layout) return check(false); - cmd.pipeline = compute_pipeline_; - cmd.user_data = compute_user_data(); - } - check(list_.execute_indirect(cmd)); + if (defer([cmd](GraphicsCommandList& parent) { parent.record_indirect(cmd); }, true)) return; + record_indirect(cmd); } void STDMETHODCALLTYPE BuildRaytracingAccelerationStructure( @@ -1582,6 +1583,15 @@ class GraphicsCommandList final held_.push_back(object); } + void record_indirect(leonida::ExecuteIndirect cmd) { + if (cmd.kind == leonida::IndirectKind::dispatch && cmd.max_count) { + if (!compute_pipeline_ || !compute_.layout) return check(false); + cmd.pipeline = compute_pipeline_; + cmd.user_data = compute_user_data(); + } + check(list_.execute_indirect(cmd)); + } + void release_held() { for (IUnknown* object : held_) object->Release(); held_.clear(); @@ -2625,23 +2635,15 @@ bool queue_uses_calibrated_timestamps(ID3D12CommandQueue* queue) { return q && q->uses_calibrated_timestamps(); } -HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, REFIID riid, void** out, +HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, UINT argument_count, REFIID riid, void** out, IUnknown* device) { if (!out) return E_POINTER; *out = nullptr; - auto* sig = new CommandSignature(kind, byte_stride); + auto* sig = new CommandSignature(kind, byte_stride, argument_count); sig->attach_device(device); return hand_out(sig, riid, out); } -bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride) { - const CommandSignature* s = CommandSignature::from(signature); - if (!s) return false; - kind = s->kind(); - byte_stride = s->stride(); - return true; -} - leonida::ReplayResult last_execute_result(ID3D12CommandQueue* queue) { if (ID3D12CommandQueue* real = d3d12_xs::queue_proxy_real_queue(queue)) queue = real; // the Xbox-ABI proxy CommandQueue* q = CommandQueue::from(queue); diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index c55229a82c..78e8fcdae4 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -60,9 +60,8 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator void** out, IUnknown* device = nullptr, ID3D12PipelineState* initial_state = nullptr); HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, void** out, IUnknown* device = nullptr); -HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, REFIID riid, void** out, +HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, UINT argument_count, REFIID riid, void** out, IUnknown* device = nullptr); -bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride); // ID3D12Device1::SetEventOnMultipleFenceCompletion on fences from create_fence: sets `event` once every fence // (D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL) or any fence (_ANY) has reached its value; a null event blocks until then. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 4a892c21af..37db01f320 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -11715,7 +11715,7 @@ class Device final break; default: return E_NOTIMPL; } - return create_command_signature(kind, desc->ByteStride, riid, out, identity()); + return create_command_signature(kind, desc->ByteStride, desc->NumArgumentDescs, riid, out, identity()); } void STDMETHODCALLTYPE GetRaytracingAccelerationStructurePrebuildInfo( diff --git a/runtime/test/d3d12_indirect_dispatch_count_test.cpp b/runtime/test/d3d12_indirect_dispatch_count_test.cpp index 9a97032e8f..9211b6ed38 100644 --- a/runtime/test/d3d12_indirect_dispatch_count_test.cpp +++ b/runtime/test/d3d12_indirect_dispatch_count_test.cpp @@ -36,7 +36,7 @@ void main(uint3 group : SV_GroupID) { )"; bool run(ID3D12Device* dev, const std::vector& shader, UINT count, bool gpu_update, - std::array& result, bool shim) { + std::array& result, bool shim, bool bundle_mode = false) { Ref root; D3D12_ROOT_PARAMETER param{}; param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; @@ -90,29 +90,42 @@ bool run(ID3D12Device* dev, const std::vector& shader, UINT count, std::memcpy(mapped, input.data(), sizeof(input)); source->Unmap(0, nullptr); - const D3D12_COMMAND_QUEUE_DESC queue_desc{D3D12_COMMAND_LIST_TYPE_COMPUTE}; + const auto list_type = bundle_mode ? D3D12_COMMAND_LIST_TYPE_DIRECT : D3D12_COMMAND_LIST_TYPE_COMPUTE; + const D3D12_COMMAND_QUEUE_DESC queue_desc{list_type}; Ref queue; - Ref allocator; - Ref list; + 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_COMPUTE, IID_PPV_ARGS(&allocator.p))) || - FAILED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, allocator.p, - pso.p, IID_PPV_ARGS(&list.p))) || + FAILED(dev->CreateCommandAllocator(list_type, IID_PPV_ARGS(&allocator.p))) || + FAILED(dev->CreateCommandList(0, list_type, allocator.p, bundle_mode ? nullptr : pso.p, IID_PPV_ARGS(&list.p))) || FAILED(dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))) return false; list->CopyBufferRegion(args.p, kArgsOffset, source.p, kArgsOffset, kMaximum * kStride); list->CopyBufferRegion(counts.p, kCountOffset, source.p, 160, 4); - list->CopyBufferRegion(output.p, 0, source.p, 0, 8); // initialize the UAV to zero + const UINT output_bytes = bundle_mode ? 16 : 8; + list->CopyBufferRegion(output.p, 0, source.p, 0, output_bytes); const D3D12_RESOURCE_BARRIER ready[] = { transition(args.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT), transition(counts.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(3, ready); list->SetComputeRootSignature(root.p); - list->SetPipelineState(pso.p); list->SetComputeRootUnorderedAccessView(0, output->GetGPUVirtualAddress()); - list->ExecuteIndirect(signature.p, kMaximum, args.p, kArgsOffset, counts.p, kCountOffset); + if (bundle_mode) { + if (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)))) + return false; + bundle->ExecuteIndirect(signature.p, kMaximum, args.p, kArgsOffset, nullptr, 0); + if (FAILED(bundle->Close())) return false; + list->ExecuteBundle(bundle.p); + list->SetComputeRootUnorderedAccessView(0, output->GetGPUVirtualAddress() + 8); + list->ExecuteBundle(bundle.p); + } else { + list->SetPipelineState(pso.p); + list->ExecuteIndirect(signature.p, kMaximum, args.p, kArgsOffset, counts.p, kCountOffset); + } if (gpu_update) { auto to_copy = transition(counts.p, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, D3D12_RESOURCE_STATE_COPY_DEST); @@ -127,7 +140,7 @@ bool run(ID3D12Device* dev, const std::vector& shader, UINT count, 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, 8); + list->CopyBufferRegion(readback.p, 0, output.p, 0, output_bytes); if (FAILED(list->Close())) return false; ID3D12CommandList* lists[] = {list.p}; queue->ExecuteCommandLists(1, lists); @@ -140,8 +153,10 @@ bool run(ID3D12Device* dev, const std::vector& shader, UINT count, CloseHandle(event); if (!completed || FAILED(readback->Map(0, nullptr, &mapped)) || !mapped) return false; std::memcpy(result.data(), mapped, sizeof(result)); + std::array second{}; + if (bundle_mode) std::memcpy(second.data(), static_cast(mapped) + 8, sizeof(second)); readback->Unmap(0, nullptr); - return true; + return !bundle_mode || second == result; } // The second dispatch of a gpu_update run reads the count written by the copy before it (3). @@ -272,6 +287,17 @@ int main() { if (stale_host) std::printf("%u GPU-count-overwrite case(s) checked against the expected result only (stale WARP count)\n", stale_host); + std::array bundle_host{}, bundle_layer{}; + const auto bundle_expected = expected(kMaximum, false); + if (!run(host.p, native_code, 0, false, bundle_host, false, true) || + !run(layer.p, container, 0, false, bundle_layer, true, true) || + bundle_host != bundle_expected || bundle_layer != bundle_expected) { + std::fprintf(stderr, "indirect compute bundle: host %u/%u, layer %u/%u, expected %u/%u\n", + bundle_host[0], bundle_host[1], bundle_layer[0], bundle_layer[1], + bundle_expected[0], bundle_expected[1]); + return 1; + } + std::printf("fixed-count ExecuteIndirect bundle: both parent UAV bindings matched WARP\n"); if (vk.validation_errors()) { std::fprintf(stderr, "validation errors: %u\n", vk.validation_errors()); return 1;