From 9990228299d7e34234513aa990ebd3610024e348 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 00:05:33 +0100 Subject: [PATCH 1/3] startup: record root views on command-list slots 41, 43, 45 and 46 Compute root CBV and SRV are copied to desktop memory and bound; both UAV slots are recorded only. A new root signature clears the stored views. --- CMakeLists.txt | 5 ++ src/startup_stubs/d3d12_xs_device.cpp | 87 ++++++++++++++++++++++----- src/startup_stubs/root_view_store.h | 54 +++++++++++++++++ tests/startup_root_view_store.cpp | 46 ++++++++++++++ 4 files changed, 176 insertions(+), 16 deletions(-) create mode 100644 src/startup_stubs/root_view_store.h create mode 100644 tests/startup_root_view_store.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 05927f896..dae9dc03f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1507,6 +1507,11 @@ add_test(NAME startup_translated_sampler_bindings COMMAND startup_translated_sam add_test(NAME startup_translated_sampler_bindings_shared_axis_control COMMAND startup_translated_sampler_bindings_check --mutate shared-axis) set_tests_properties(startup_translated_sampler_bindings_shared_axis_control PROPERTIES WILL_FAIL TRUE) +# Root descriptor views set through list slots 41, 43, 45 and 46 (src/startup_stubs/root_view_store.h), host-only. +add_executable(startup_root_view_store_check tests/startup_root_view_store.cpp) +target_include_directories(startup_root_view_store_check PRIVATE src) +target_compile_features(startup_root_view_store_check PRIVATE cxx_std_17) +add_test(NAME startup_root_view_store COMMAND startup_root_view_store_check) # Real-draw decision for title root signatures that index descriptor heaps directly # (src/startup_stubs/heap_indexed_root.h), without a device. The mutated runs are the negative controls. # Limits of the slot 70 refusal and "token" null census (src/startup_stubs/memory_srv_census.h), without a device. diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 2d4ab5d61..a0eca4181 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -36,6 +36,7 @@ #include "startup_stubs/placement_publish.h" #include "startup_stubs/placed_build.h" #include "startup_stubs/placed_buffer_range.h" +#include "startup_stubs/root_view_store.h" #include "startup_stubs/memory_srv_report.h" #include "startup_stubs/memory_srv_census.h" #include "startup_stubs/memory_srv_buffer.h" @@ -2149,6 +2150,8 @@ struct Pass { std::array desktop_compute_upload_srvs{}; const std::vector* compute_params = nullptr; std::unique_ptr compute_trace; + x2w_startup::RootViewStore compute_views; // slots 41 / 43 / 45 + x2w_startup::RootViewStore graphics_uav; // slot 46 IndirectGraphicsPipeline indirect_graphics; ID3D12RootSignature* graphics_root = nullptr; bool targets_bound = false; @@ -2329,6 +2332,7 @@ void __stdcall list_compute_root(Wrapper* self, void* root) pass.compute_trace = std::make_unique(); pass.compute_trace->root = w; } + pass.compute_views.clear(); if (pass.compute_root) { if (pass.desktop_compute_root.Get() != pass.compute_root) pass.desktop_compute_upload_srvs.fill(0); @@ -2692,6 +2696,7 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) std::memset(pass.constant_words_bound, 0, sizeof(pass.constant_words_bound)); } pass.root = params; + pass.graphics_uav.clear(); pass.srv_bound = 0; const bool real = w && w->own().kind == root_signature_kind && w->own().real; pass.root_heap_flags = real ? root_heap_flags(w) : 0; @@ -2945,21 +2950,6 @@ void __stdcall list_stencil_ref(Wrapper* self, UINT reference) list->OMSetStencilRef(reference); } } -// List slot 43 (desktop 39 + 4), set a compute root SRV backed by this recording's upload pages. -void __stdcall list_compute_upload_srv(Wrapper* self, UINT parameter, UINT64 address) -{ - auto* list = recording(self); - auto& pass = pass_of(self); - const auto* params = pass.compute_params; - if (!list || !params || parameter >= params->size() || parameter >= pass.desktop_compute_upload_srvs.size() || - (*params)[parameter].type != D3D12_ROOT_PARAMETER_TYPE_SRV) return; - const auto gpu = list_upload_gpu(self, address, 1); - if (gpu && !(gpu % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT)) { - pass.desktop_compute_upload_srvs[parameter] = gpu; - list->SetComputeRootShaderResourceView(parameter, gpu); - } -} - // List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address). void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) { @@ -3007,6 +2997,68 @@ void __stdcall list_root_cbv(Wrapper* self, UINT parameter, u64 address) } pass.root_address[parameter] = (address && readable_window(address, 1)) ? address : 0; } +// Declared type of a root parameter, or ~0u when the root signature has no such parameter. +unsigned declared_root_type(const std::vector* params, UINT parameter) +{ + return params && parameter < params->size() ? static_cast((*params)[parameter].type) : ~0u; +} +void log_root_view_refusal(const char* what, UINT parameter, x2w_startup::RootViewResult result) +{ + static LONG refused; + if (first_calls(refused, 6)) log("x2w-render %s %u ignored: %s", what, parameter, x2w_startup::root_view_reason(result)); +} +// List slots 41 / 43 (desktop 37 / 39 + 4), SetComputeRootConstantBufferView / SetComputeRootShaderResourceView. +// The bytes at the title address are copied to desktop memory and the copy is bound, as for slot 44. The +// view is dropped from the store when no copy can be made. +void compute_read_view(Wrapper* self, UINT parameter, u64 address, unsigned wanted, const char* what) +{ + auto* list = recording(self); + if (!list) return; + Pass& pass = pass_of(self); + const unsigned declared = declared_root_type(pass.compute_params, parameter); + const std::size_t bytes = address ? readable_window(address, 0x10000) : 0; + // An SRV address inside this recording's own upload pages already has a desktop address: bind it directly. + const UINT64 own_page = wanted == x2w_startup::root_type_srv ? list_upload_gpu(self, address, 1) : 0; + const bool direct = own_page && !(own_page % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT); + const auto result = pass.compute_views.set(pass.compute_root, declared, wanted, parameter, bytes || direct ? address : 0); + if (result != x2w_startup::RootViewResult::stored) return log_root_view_refusal(what, parameter, result); + if (direct) { + list->SetComputeRootShaderResourceView(parameter, own_page); + return; + } + if (!bytes) return; + const D3D12_GPU_VIRTUAL_ADDRESS desktop = upload_from_title(address, bytes); + if (!desktop) return; + if (wanted == x2w_startup::root_type_cbv) list->SetComputeRootConstantBufferView(parameter, desktop); + else list->SetComputeRootShaderResourceView(parameter, desktop); +} +void __stdcall list_compute_root_cbv(Wrapper* self, UINT parameter, u64 address) +{ + compute_read_view(self, parameter, address, x2w_startup::root_type_cbv, "compute root CBV"); +} +void __stdcall list_compute_root_srv(Wrapper* self, UINT parameter, u64 address) +{ + compute_read_view(self, parameter, address, x2w_startup::root_type_srv, "compute root SRV"); +} +// List slots 45 / 46 (desktop 41 / 42 + 4), SetComputeRootUnorderedAccessView / SetGraphicsRootUnorderedAccessView. +// Recorded only. A copy would lose the writes the shader makes, so nothing is forwarded until the address maps +// to a placed desktop buffer. +void __stdcall list_compute_root_uav(Wrapper* self, UINT parameter, u64 address) +{ + if (!recording(self)) return; + Pass& pass = pass_of(self); + const auto result = pass.compute_views.set(pass.compute_root, declared_root_type(pass.compute_params, parameter), + x2w_startup::root_type_uav, parameter, address); + if (result != x2w_startup::RootViewResult::stored) log_root_view_refusal("compute root UAV", parameter, result); +} +void __stdcall list_graphics_root_uav(Wrapper* self, UINT parameter, u64 address) +{ + if (!recording(self)) return; + Pass& pass = pass_of(self); + const auto result = pass.graphics_uav.set(pass.root != nullptr, declared_root_type(pass.root, parameter), + x2w_startup::root_type_uav, parameter, address); + if (result != x2w_startup::RootViewResult::stored) log_root_view_refusal("graphics root UAV", parameter, result); +} extern LONG translated_draws, untranslated_draws, frame_number; bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UINT start, INT base_vertex, UINT start_instance); // List slot 12, DrawInstanced. @@ -4645,9 +4697,12 @@ void build_tables() list_typed[32] = reinterpret_cast(&list_set_descriptor_heaps); list_typed[33] = reinterpret_cast(&list_compute_root); list_typed[34] = reinterpret_cast(&list_graphics_root); - list_typed[43] = reinterpret_cast(&list_compute_upload_srv); list_typed[44] = reinterpret_cast(&list_root_srv); list_typed[42] = reinterpret_cast(&list_root_cbv); + list_typed[41] = reinterpret_cast(&list_compute_root_cbv); + list_typed[43] = reinterpret_cast(&list_compute_root_srv); + list_typed[45] = reinterpret_cast(&list_compute_root_uav); + list_typed[46] = reinterpret_cast(&list_graphics_root_uav); list_typed[50] = reinterpret_cast(&list_targets); list_typed[13] = reinterpret_cast(&list_draw_indexed); list_typed[36] = reinterpret_cast(&list_table); diff --git a/src/startup_stubs/root_view_store.h b/src/startup_stubs/root_view_store.h new file mode 100644 index 000000000..b14f859a9 --- /dev/null +++ b/src/startup_stubs/root_view_store.h @@ -0,0 +1,54 @@ +#pragma once + +// Root descriptor views (CBV, SRV, UAV) a title sets on a command list, for the slots that carry them: +// compute CBV 41, compute SRV 43, compute UAV 45 and graphics UAV 46 (desktop 37, 39, 41, 42 plus four). +// Plain header with no Windows types so the host-only test can build it. +// +// A view is accepted only when a root signature of the matching kind is set, the parameter is below +// root_view_parameters and the root signature declares that parameter with the same view type. Anything +// else is refused and the store is left as it was. Setting a new root signature clears the store. + +#include + +namespace x2w_startup { + +constexpr unsigned root_view_parameters = 16; + +// D3D12_ROOT_PARAMETER_TYPE values. +constexpr unsigned root_type_cbv = 2; +constexpr unsigned root_type_srv = 3; +constexpr unsigned root_type_uav = 4; + +enum class RootViewResult { stored, no_root, parameter_out_of_range, wrong_type }; + +inline const char* root_view_reason(RootViewResult result) +{ + switch (result) { + case RootViewResult::stored: return "stored"; + case RootViewResult::no_root: return "no root signature set"; + case RootViewResult::parameter_out_of_range: return "parameter out of range"; + case RootViewResult::wrong_type: return "parameter declared as another type"; + } + return "unknown"; +} + +struct RootViewStore { + std::uint64_t address[root_view_parameters] = {}; // title address, 0 when never set or the address was unreadable + std::uint32_t bound = 0; // bit per parameter that holds a view + + void clear() { *this = RootViewStore{}; } + + // declared: the type the root signature gives parameter, or ~0u when it has no such parameter. + RootViewResult set(bool root_set, unsigned declared, unsigned wanted, unsigned parameter, std::uint64_t value) + { + if (!root_set) return RootViewResult::no_root; + if (parameter >= root_view_parameters || declared == ~0u) return RootViewResult::parameter_out_of_range; + if (declared != wanted) return RootViewResult::wrong_type; + address[parameter] = value; + if (value) bound |= 1u << parameter; + else bound &= ~(1u << parameter); + return RootViewResult::stored; + } +}; + +} // namespace x2w_startup diff --git a/tests/startup_root_view_store.cpp b/tests/startup_root_view_store.cpp new file mode 100644 index 000000000..f15435c4c --- /dev/null +++ b/tests/startup_root_view_store.cpp @@ -0,0 +1,46 @@ +// Host-only check of src/startup_stubs/root_view_store.h: which root view calls a list accepts and what the +// store holds afterwards. +#include "startup_stubs/root_view_store.h" + +#include + +using namespace x2w_startup; + +namespace { +int failures; +void expect(bool ok, const char* what) +{ + if (ok) return; + ++failures; + std::printf("FAIL %s\n", what); +} +} // namespace + +int main() +{ + RootViewStore store; + expect(store.set(false, root_type_cbv, root_type_cbv, 0, 0x1000) == RootViewResult::no_root, "no root refused"); + expect(store.bound == 0 && store.address[0] == 0, "no root leaves the store empty"); + + expect(store.set(true, root_type_cbv, root_type_cbv, 3, 0x2000) == RootViewResult::stored, "cbv stored"); + expect(store.address[3] == 0x2000 && store.bound == (1u << 3), "cbv address and bit"); + + expect(store.set(true, root_type_srv, root_type_uav, 3, 0x3000) == RootViewResult::wrong_type, "uav on srv parameter refused"); + expect(store.address[3] == 0x2000, "refused call keeps the old address"); + + expect(store.set(true, ~0u, root_type_srv, 5, 0x4000) == RootViewResult::parameter_out_of_range, "undeclared parameter refused"); + expect(store.set(true, root_type_srv, root_type_srv, root_view_parameters, 0x4000) == RootViewResult::parameter_out_of_range, + "parameter 16 refused"); + expect(store.set(true, root_type_srv, root_type_srv, 15, 0x5000) == RootViewResult::stored, "last parameter stored"); + expect(store.bound == ((1u << 3) | (1u << 15)), "two bits set"); + + expect(store.set(true, root_type_cbv, root_type_cbv, 3, 0) == RootViewResult::stored, "zero address stored"); + expect(store.bound == (1u << 15) && store.address[3] == 0, "zero address clears the bit"); + + store.clear(); + expect(store.bound == 0 && store.address[15] == 0, "clear empties the store"); + + if (failures) return 1; + std::printf("root view store ok\n"); + return 0; +} From 46f4751376351973eb731ba942d755a493e5aca4 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 01:50:07 +0100 Subject: [PATCH 2/3] startup: record compute root SRVs without a prefix copy; document root views --- docs/experiments.md | 14 ++++++++++++++ src/startup_stubs/d3d12_xs_device.cpp | 17 ++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 093385c93..cd37d2506 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -13947,3 +13947,17 @@ ctest --preset dev --no-tests=error -R '^(cpu_aot_switch_sal|cpu_aot_switch_shif The [MASM fixture](../tests/cpu/switch_sal.asm) emits explicit `/6` byte sequences (because ML64 assembles the `sal` mnemonic as `/4` `SHL`) across 24 translated functions covering all 12 `SAL` `Code` encodings (`SAL_RM{8,16,32,64}_{1,CL,IMM8}`), register and memory destinations, count-`1` all-16 `SETcc` checks, count->1 10-condition `SETcc` checks, `Jcc`, `CMOVcc`, narrow returns, and nonvolatile/stack-frame operands, plus 11 negative exports. [The differential check](../tests/cpu/switch_sal_check.py) verifies 28,024 `dev` and 28,024 `/O2` comparisons against native execution and the Python reference, four `/O2` mutated-C++ controls (`shift_dir`, `cf_mask`, `of_mask`, `cl_mask`), 11 MASM, 4 unit, and 7 PE-patched rejection controls, and records decoded mnemonics via `iced_x86.Mnemonic` to `FORM_COVERAGE_RECORD`. Not covered by this experiment: `SAL` outside leaf/switch-lifted functions, and title CPU code execution. + +## Root descriptor views on command-list slots 41, 43, 45 and 46 + +Build and run the host-only check with +`cmake --build --preset dev --target startup_root_view_store_check` and +`ctest --preset dev --no-tests=error -R '^startup_root_view_store$' -V`. +It covers the store only: a view is accepted when a root signature is set, the parameter is below 16 and the +root signature declares it with the same view type; otherwise it is refused and the store keeps its old state. + +Implementation boundary: +- Compute CBV (slot 41): the bytes at the title address (at most 64 KiB, the CBV limit) are copied to desktop memory and the copy is bound. +- Compute SRV (slot 43): the title address is recorded only. A root SRV has no 64 KiB bound and the buffer extent is not known, so nothing is copied or bound. +- Compute UAV (slot 45) and graphics UAV (slot 46): addresses are recorded only. A stored UAV address is not a working GPU write. +- Not yet measured on a live compute dispatch; that needs a placed buffer with a known offset (see #1441, #1974, #1975). diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index a0eca4181..929e6ddea 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3008,19 +3008,24 @@ void log_root_view_refusal(const char* what, UINT parameter, x2w_startup::RootVi if (first_calls(refused, 6)) log("x2w-render %s %u ignored: %s", what, parameter, x2w_startup::root_view_reason(result)); } // List slots 41 / 43 (desktop 37 / 39 + 4), SetComputeRootConstantBufferView / SetComputeRootShaderResourceView. -// The bytes at the title address are copied to desktop memory and the copy is bound, as for slot 44. The -// view is dropped from the store when no copy can be made. +// A CBV never exceeds 64 KiB, so its bytes at the title address are copied to desktop memory and the copy is +// bound, as for slot 44. A root SRV has no such bound (a shader may read any offset of the buffer behind it) and +// the buffer extent is not known here, so an SRV is only recorded: a prefix copy would let reads past it return +// unrelated ring memory. The view is dropped from the store when no copy can be made. void compute_read_view(Wrapper* self, UINT parameter, u64 address, unsigned wanted, const char* what) { auto* list = recording(self); if (!list) return; Pass& pass = pass_of(self); const unsigned declared = declared_root_type(pass.compute_params, parameter); - const std::size_t bytes = address ? readable_window(address, 0x10000) : 0; + const bool copies = wanted == x2w_startup::root_type_cbv; + const std::size_t bytes = (copies && address) ? readable_window(address, 0x10000) : 0; // An SRV address inside this recording's own upload pages already has a desktop address: bind it directly. + // Any other SRV address is only recorded. const UINT64 own_page = wanted == x2w_startup::root_type_srv ? list_upload_gpu(self, address, 1) : 0; const bool direct = own_page && !(own_page % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT); - const auto result = pass.compute_views.set(pass.compute_root, declared, wanted, parameter, bytes || direct ? address : 0); + const u64 kept = copies ? (bytes ? address : 0) : address; + const auto result = pass.compute_views.set(pass.compute_root, declared, wanted, parameter, kept); if (result != x2w_startup::RootViewResult::stored) return log_root_view_refusal(what, parameter, result); if (direct) { list->SetComputeRootShaderResourceView(parameter, own_page); @@ -3028,9 +3033,7 @@ void compute_read_view(Wrapper* self, UINT parameter, u64 address, unsigned want } if (!bytes) return; const D3D12_GPU_VIRTUAL_ADDRESS desktop = upload_from_title(address, bytes); - if (!desktop) return; - if (wanted == x2w_startup::root_type_cbv) list->SetComputeRootConstantBufferView(parameter, desktop); - else list->SetComputeRootShaderResourceView(parameter, desktop); + if (desktop) list->SetComputeRootConstantBufferView(parameter, desktop); } void __stdcall list_compute_root_cbv(Wrapper* self, UINT parameter, u64 address) { From 480c6fb76e73800b99bc6ea7ecefb1d854d1a3c0 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 11:47:36 +0100 Subject: [PATCH 3/3] startup: bind compute root UAV/SRV addresses in live placed buffers A root UAV or non-upload SRV whose address lies in a live aliased placed buffer (COMMON-tracked, aligned offset, UAV flag for writes) is forwarded at its desktop address plus the known offset. Other addresses stay recorded only. Adds RootViewStore::covers for the Dispatch root-completeness check. --- docs/experiments.md | 6 ++--- src/startup_stubs/d3d12_xs_device.cpp | 34 ++++++++++++++++++++++++--- src/startup_stubs/root_view_store.h | 4 ++++ tests/startup_root_view_store.cpp | 3 +++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index cd37d2506..5673acf17 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -13958,6 +13958,6 @@ root signature declares it with the same view type; otherwise it is refused and Implementation boundary: - Compute CBV (slot 41): the bytes at the title address (at most 64 KiB, the CBV limit) are copied to desktop memory and the copy is bound. -- Compute SRV (slot 43): the title address is recorded only. A root SRV has no 64 KiB bound and the buffer extent is not known, so nothing is copied or bound. -- Compute UAV (slot 45) and graphics UAV (slot 46): addresses are recorded only. A stored UAV address is not a working GPU write. -- Not yet measured on a live compute dispatch; that needs a placed buffer with a known offset (see #1441, #1974, #1975). +- Compute SRV (slot 43): an address in this recording's own upload pages or in a live aliased placed buffer (tracked from COMMON, 16-byte aligned offset) is bound at its desktop address; any other address is recorded only, because a root SRV has no 64 KiB bound and a prefix copy would let reads past it return unrelated memory. +- Compute UAV (slot 45): bound only when the address lies in a live aliased placed buffer that allows unordered access, so writes reach the title-visible memory at the known offset; otherwise recorded only. Graphics UAV (slot 46): recorded only. +- Not yet measured on a live compute dispatch. The acceptance check (fenced UAV readback at a nonzero offset, and a Dispatch refused while a declared root is unbound, using `RootViewStore::covers`) needs the direct Dispatch of #1974 and the root constants and views of #1975 (see #1441). diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 929e6ddea..fca9dcb93 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3007,6 +3007,25 @@ void log_root_view_refusal(const char* what, UINT parameter, x2w_startup::RootVi static LONG refused; if (first_calls(refused, 6)) log("x2w-render %s %u ignored: %s", what, parameter, x2w_startup::root_view_reason(result)); } +// Desktop GPU address of a title address inside a live aliased placed buffer that is tracked from COMMON and +// allows unordered access, or 0. Reads and writes through it reach the same memory the title sees, at the +// offset of the title address in the buffer; the extent is the rest of that buffer. +D3D12_GPU_VIRTUAL_ADDRESS placed_root_gpu(Wrapper* self, u64 address, bool writable) +{ + if (!address) return 0; + const auto placed = placed_buffer_range(self->own().list_device, address, 1); + if (placed.reject || !placed.owner || !placed.alias) return 0; + const auto* info = resource_info(placed.owner); + if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON) return 0; + const auto base = reinterpret_cast(placed.owner->own().title_memory); + if (!base || address < base) return 0; + const u64 offset = address - base; + if (offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT || !InterlockedCompareExchange(&placed.owner->own().refs, 0, 0)) return 0; + const D3D12_RESOURCE_DESC desc = placed.alias->GetDesc(); + if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || offset >= desc.Width) return 0; + if (writable && !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) return 0; + return placed.alias->GetGPUVirtualAddress() + offset; +} // List slots 41 / 43 (desktop 37 / 39 + 4), SetComputeRootConstantBufferView / SetComputeRootShaderResourceView. // A CBV never exceeds 64 KiB, so its bytes at the title address are copied to desktop memory and the copy is // bound, as for slot 44. A root SRV has no such bound (a shader may read any offset of the buffer behind it) and @@ -3028,9 +3047,15 @@ void compute_read_view(Wrapper* self, UINT parameter, u64 address, unsigned want const auto result = pass.compute_views.set(pass.compute_root, declared, wanted, parameter, kept); if (result != x2w_startup::RootViewResult::stored) return log_root_view_refusal(what, parameter, result); if (direct) { + if (parameter < pass.desktop_compute_upload_srvs.size()) pass.desktop_compute_upload_srvs[parameter] = own_page; list->SetComputeRootShaderResourceView(parameter, own_page); return; } + if (wanted == x2w_startup::root_type_srv) { + // A live placed buffer keeps its known offset and extent; any other address stays recorded only. + if (const auto gpu = placed_root_gpu(self, address, false)) list->SetComputeRootShaderResourceView(parameter, gpu); + return; + } if (!bytes) return; const D3D12_GPU_VIRTUAL_ADDRESS desktop = upload_from_title(address, bytes); if (desktop) list->SetComputeRootConstantBufferView(parameter, desktop); @@ -3044,15 +3069,18 @@ void __stdcall list_compute_root_srv(Wrapper* self, UINT parameter, u64 address) compute_read_view(self, parameter, address, x2w_startup::root_type_srv, "compute root SRV"); } // List slots 45 / 46 (desktop 41 / 42 + 4), SetComputeRootUnorderedAccessView / SetGraphicsRootUnorderedAccessView. -// Recorded only. A copy would lose the writes the shader makes, so nothing is forwarded until the address maps -// to a placed desktop buffer. +// Always recorded. Compute is forwarded when the address lies in a live aliased placed buffer (placed_root_gpu); +// graphics UAVs are recorded only. void __stdcall list_compute_root_uav(Wrapper* self, UINT parameter, u64 address) { if (!recording(self)) return; Pass& pass = pass_of(self); const auto result = pass.compute_views.set(pass.compute_root, declared_root_type(pass.compute_params, parameter), x2w_startup::root_type_uav, parameter, address); - if (result != x2w_startup::RootViewResult::stored) log_root_view_refusal("compute root UAV", parameter, result); + if (result != x2w_startup::RootViewResult::stored) return log_root_view_refusal("compute root UAV", parameter, result); + // Only a live placed buffer is forwarded; a copy would lose the shader's writes. + if (auto* list = recording(self)) + if (const auto gpu = placed_root_gpu(self, address, true)) list->SetComputeRootUnorderedAccessView(parameter, gpu); } void __stdcall list_graphics_root_uav(Wrapper* self, UINT parameter, u64 address) { diff --git a/src/startup_stubs/root_view_store.h b/src/startup_stubs/root_view_store.h index b14f859a9..b21ac42af 100644 --- a/src/startup_stubs/root_view_store.h +++ b/src/startup_stubs/root_view_store.h @@ -38,6 +38,10 @@ struct RootViewStore { void clear() { *this = RootViewStore{}; } + // True when every parameter in required (one bit each) holds a view. A Dispatch that needs these roots + // must be recorded only when this holds. + bool covers(std::uint32_t required) const { return (bound & required) == required; } + // declared: the type the root signature gives parameter, or ~0u when it has no such parameter. RootViewResult set(bool root_set, unsigned declared, unsigned wanted, unsigned parameter, std::uint64_t value) { diff --git a/tests/startup_root_view_store.cpp b/tests/startup_root_view_store.cpp index f15435c4c..1bba8b18e 100644 --- a/tests/startup_root_view_store.cpp +++ b/tests/startup_root_view_store.cpp @@ -37,6 +37,9 @@ int main() expect(store.set(true, root_type_cbv, root_type_cbv, 3, 0) == RootViewResult::stored, "zero address stored"); expect(store.bound == (1u << 15) && store.address[3] == 0, "zero address clears the bit"); + expect(store.covers(1u << 15) && store.covers(0), "bound parameter covered"); + expect(!store.covers((1u << 15) | (1u << 4)), "a missing root is not covered"); + store.clear(); expect(store.bound == 0 && store.address[15] == 0, "clear empties the store");