diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..83f70890b5 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -144,7 +144,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. `DispatchMesh` is deferred the same way (`d3d12_mesh_test`). Not deferred yet: `ExecuteIndirect` in a bundle, and 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`, 113 cases on fresh lists, nothing executed. 82 agree; the other 31 are listed in the test as known differences. In 16 the layer fails a list WARP closes: draws and dispatches with no pipeline, diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e965..1003b98a11 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -485,12 +485,8 @@ class GraphicsCommandList final void STDMETHODCALLTYPE DispatchMesh(UINT x, UINT y, UINT z) override { d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh"); if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false); - if (!graphics_pipeline_ || !graphics_.layout) return check(false); - if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized - leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, - scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; - draw.mesh = true; - check(list_.draw(std::move(draw))); + if (defer([x, y, z](GraphicsCommandList& l) { l.record_dispatch_mesh(x, y, z); }, true)) return; + record_dispatch_mesh(x, y, z); } void STDMETHODCALLTYPE ResourceBarrier(UINT count, const D3D12_RESOURCE_BARRIER* barriers) override { @@ -1420,6 +1416,16 @@ class GraphicsCommandList final first, base_vertex, first_instance}; check(list_.draw(std::move(draw))); } + + void record_dispatch_mesh(UINT x, UINT y, UINT z) { + if (!graphics_pipeline_ || !graphics_.layout) return check(false); + note_direct_heap_indexing(graphics_.layout); + if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized + leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, + scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; + draw.mesh = true; + check(list_.draw(std::move(draw))); + } }; class Fence final diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 1b99b8d0b1..63316e9654 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -504,6 +504,112 @@ int main() { std::printf("pipelineStatisticsQuery not available: the D3D-291 statistics check is skipped\n"); } + // Bundle DispatchMesh: a bundle starts with its creation PSO (mesh_pso) rather than the parent's (vs_pso), + // inherits the parent's render targets, viewport, scissor and root arguments across multiple executions, and + // hands its PSO and root arguments back to the parent. + { + Ref bundle_alloc, state_bundle_alloc, bad_bundle_alloc; + Ref bundle, state_bundle, bad_bundle; + Ref bundle6, bad_bundle6; + EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, + IID_PPV_ARGS(&bundle_alloc.p)))); + EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, bundle_alloc.p, mesh_pso.p, + IID_PPV_ARGS(&bundle.p)))); + EXPECT(SUCCEEDED(bundle->QueryInterface(IID_PPV_ARGS(&bundle6.p)))); + bundle6->DispatchMesh(2, 2, 1); + EXPECT(bundle->Close() == S_OK); + + const float down_left[2] = {0, -1}; + EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, + IID_PPV_ARGS(&state_bundle_alloc.p)))); + EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, state_bundle_alloc.p, + mesh_pso.p, IID_PPV_ARGS(&state_bundle.p)))); + state_bundle->SetGraphicsRootSignature(root.p); + state_bundle->SetGraphicsRoot32BitConstants(0, 2, down_left, 0); + state_bundle->SetGraphicsRootConstantBufferView(1, upload_va + 512); + EXPECT(state_bundle->Close() == S_OK); + + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, vs_pso.p))); + if (!d.pipeline_statistics_query) { + D3D12_RESOURCE_BARRIER back_to_target = + transition(target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET); + list->ResourceBarrier(1, &back_to_target); + } + list->ClearRenderTargetView(rtv, blue, 0, nullptr); + list->SetGraphicsRootSignature(root.p); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); + + list->SetGraphicsRoot32BitConstants(0, 2, origin, 0); + list->SetGraphicsRootConstantBufferView(1, upload_va); + list->ExecuteBundle(bundle.p); + + list->SetGraphicsRoot32BitConstants(0, 2, down_right, 0); + list->SetGraphicsRootConstantBufferView(1, upload_va + 256); + list->ExecuteBundle(bundle.p); + + list->ExecuteBundle(state_bundle.p); + list6->DispatchMesh(2, 2, 1); + + list->ResourceBarrier(1, &to_copy); + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + EXPECT(list->Close() == S_OK); + queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); + const UINT64 bundle_fence = d.pipeline_statistics_query ? 4 : 3; + HANDLE bundle_event = CreateEventW(nullptr, FALSE, FALSE, nullptr); + EXPECT(bundle_event != nullptr); + EXPECT(SUCCEEDED(queue->Signal(fence.p, bundle_fence)) && + SUCCEEDED(fence->SetEventOnCompletion(bundle_fence, bundle_event)) && + WaitForSingleObject(bundle_event, 20000) == WAIT_OBJECT_0); + CloseHandle(bundle_event); + + std::array bundle_pixels{}; + void* bundle_mapped = nullptr; + EXPECT(SUCCEEDED(readback->Map(0, nullptr, &bundle_mapped))); + std::memcpy(bundle_pixels.data(), bundle_mapped, bundle_pixels.size()); + readback->Unmap(0, nullptr); + for (UINT row = 0; row < size; ++row) { + for (UINT col = 0; col < size; ++col) { + const bool top = row < size / 2, left = col < size / 2; + using Pixel = std::array; + const Pixel want = top && left ? Pixel{255, 0, 0, 255} + : !top && !left ? Pixel{0, 255, 0, 255} + : !top && left ? Pixel{255, 255, 0, 255} + : Pixel{0, 0, 255, 255}; + const std::uint8_t* got = bundle_pixels.data() + row * 256 + col * 4; + EXPECT(std::equal(want.begin(), want.end(), got)); + } + } + + // Negative bundle cases: oversized grid fails bundle Close, missing parent root fails parent Close, and + // DispatchMesh on an already-closed bundle invalidates it for ExecuteBundle. + EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, + IID_PPV_ARGS(&bad_bundle_alloc.p)))); + EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, bad_bundle_alloc.p, + mesh_pso.p, IID_PPV_ARGS(&bad_bundle.p)))); + EXPECT(SUCCEEDED(bad_bundle->QueryInterface(IID_PPV_ARGS(&bad_bundle6.p)))); + bad_bundle6->DispatchMesh(65536, 1, 1); + EXPECT(bad_bundle->Close() == E_INVALIDARG); + + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr))); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); + list->ExecuteBundle(bundle.p); + EXPECT(list->Close() == E_INVALIDARG); + + bundle6->DispatchMesh(1, 1, 1); // bundle is already closed: marks it invalid + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr))); + list->SetGraphicsRootSignature(root.p); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); + list->ExecuteBundle(bundle.p); + EXPECT(list->Close() == E_INVALIDARG); + } + for (auto* r : {&upload, &readback, &target}) r->reset(); for (auto* p : {&mesh_pso, &vs_pso, &refused}) p->reset(); root.reset(); @@ -539,8 +645,9 @@ int main() { return 1; } std::printf("D3D-29: mesh tier 1; mesh and VS PSOs from pipeline state streams (9 refused as invalid, 2 as not " - "implemented); DispatchMesh over X, Y and Z and a stream VS draw matched all %u pixels; wrong-kind " - "draws fail at execution, oversized grids at Close; statistics queries around mesh draws resolve\n", + "implemented); DispatchMesh over X, Y and Z, deferred bundle DispatchMesh and a stream VS draw matched " + "all %u pixels; wrong-kind draws fail at execution, oversized grids and invalid bundle replays at " + "Close; statistics queries around mesh draws resolve\n", size * size); return 0; }