From 49eea1e71d6287ed3d5a2efaf72c1b02fed9ce63 Mon Sep 17 00:00:00 2001 From: SuperCraft3M Date: Tue, 24 Nov 2026 20:02:10 +0100 Subject: [PATCH] startup: invalidate cached constants when the graphics root changes --- docs/experiments.md | 8 +++++++- docs/startup-runtime.md | 10 ++++++---- src/startup_stubs/d3d12_xs_device.cpp | 9 +++++---- tests/startup_root_reset.cpp | 12 +++++++----- tests/startup_root_reset_check.py | 18 +++++++++--------- tests/startup_stream_setup.cpp | 12 ++++++++++-- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 605a9b7eed..c5611bcd9e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11009,7 +11009,13 @@ ctest --preset release --no-tests=error -R '^cpu_aot_vex_parity$' -V ## Startup root signature changes -`tests/startup_root_reset.cpp` and `tests/startup_root_reset_check.py` bind a 1D texture through a descriptor table (list slot 36) and check that setting a different root signature (slot 34) drops the table, that the same one keeps it, and that six out-of-range `SetGraphicsRoot32BitConstants` calls log four lines and leave the table alone. Details are in [startup-runtime.md](startup-runtime.md#root-signature-changes-and-dropped-root-constants). Limits: synthetic command-stream fixture; the `*_hardware` target skips when no hardware D3D12 adapter is present. +`tests/startup_root_reset.cpp` and `tests/startup_root_reset_check.py` bind a 1D texture through a descriptor table +(list slot 36) and a root constant through slot 40. The pixel shader puts the constant in the blue channel. The +fixture checks that a different root signature (slot 34) clears both inputs, the same signature preserves them, +and six out-of-range constant writes log four lines and preserve both bindings. Before clearing cached constants, +the switched case reads back blue 173 on a pixel where the fixture expects zero. Details are in +[startup-runtime.md](startup-runtime.md#root-signature-changes-and-dropped-root-constants). The synthetic hardware +case returns 77 without a physical D3D12 adapter. ```powershell cmake --build --preset dev --target startup_stream_consumer diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 582fb63ca0..2b10ff5cbe 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -159,13 +159,15 @@ data. The comparison uses the kept bytes because the ring is write-combined memo ## Root signature changes and dropped root constants -Command-list slot 34 (`SetGraphicsRootSignature`) leaves no descriptor table bound when the signature differs from the -current one, as it already clears the root addresses; setting the same signature again keeps the tables. Slot 40 +Command-list slot 34 (`SetGraphicsRootSignature`) clears the translated draw's cached descriptor tables, root +addresses and root constants when the signature changes. Setting the same signature again preserves these arguments. +An unset constant reads as zero in the translated draw. Slot 40 (`SetGraphicsRoot32BitConstants`) keeps 16 parameters of 8 words. A call with a parameter of 16 or more or an offset of 8 or more is dropped, and one whose count passes the eighth word is truncated; each logs one line (`root constants dropped` or `root constants truncated`, with the parameter, offset and count) for the first four such -calls in the process. `startup_root_reset_{warp,hardware}` draws a 1D texture through a bound table with no change, the same -signature, a switched signature and six out-of-range calls. +calls in the process. `startup_root_reset_{warp,hardware}` draws a 1D texture through a bound table and writes a +root constant into the blue channel. It checks the original bindings, the same signature, a switched signature and +six out-of-range calls. The switched case clears both inputs to the shader. ## Null bindings in translated draws diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 8c96001c34..121756aa1f 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1778,12 +1778,13 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) { Wrapper* w = as_wrapper(root); Pass& pass = pass_of(self); - // A different root signature leaves no descriptor table bound; the same one keeps them. - if (pass.root != (w ? root_info(w) : nullptr)) - for (auto& table : pass.view_tables) table = {}; const auto* params = w ? root_info(w) : nullptr; - if (pass.root != params) + // A different root signature invalidates the title's cached root arguments. + if (pass.root != params) { + for (auto& table : pass.view_tables) table = {}; std::fill_n(pass.root_address, 16, 0); + std::memset(pass.constants, 0, sizeof(pass.constants)); + } pass.root = params; pass.srv_bound = 0; const bool real = w && w->own().kind == root_signature_kind && w->own().real; diff --git a/tests/startup_root_reset.cpp b/tests/startup_root_reset.cpp index eb1edf9b08..09b6366709 100644 --- a/tests/startup_root_reset.cpp +++ b/tests/startup_root_reset.cpp @@ -9,12 +9,12 @@ namespace { template StreamWord word_of(T* value) { return reinterpret_cast(value); } } -// A descriptor table is bound with SetGraphicsRootDescriptorTable (list slot 36) under the stream root signature, then +// A descriptor table and a root constant are set under the stream root signature, then // X2W_TEST_ROOT_RESET names what the title does before the draw: -// bound nothing: the table stays -// same_root SetGraphicsRootSignature with the same signature: the table stays -// switch_root a different signature and back: the table is gone -// out_of_range six SetGraphicsRoot32BitConstants calls that reach no stored word, in four ways; the table stays +// bound nothing: both bindings stay +// same_root SetGraphicsRootSignature with the same signature: both bindings stay +// switch_root a different signature and back: both bindings are invalidated +// out_of_range six out-of-range SetGraphicsRoot32BitConstants calls; both bindings stay int draw_root_reset(void* guest, void* list) { char mode[16]{}; @@ -47,6 +47,8 @@ int draw_root_reset(void* guest, void* list) invoke(list, 34, root); // SetGraphicsRootSignature invoke(list, 32, 1, word_of(&heap)); invoke(list, 36, 0, invoke(heap, 10)); + const std::uint32_t blue = 173; + invoke(list, 40, 1, 1, word_of(&blue), 0); if (!std::strcmp(mode, "same_root")) { invoke(list, 34, root); } else if (!std::strcmp(mode, "switch_root")) { diff --git a/tests/startup_root_reset_check.py b/tests/startup_root_reset_check.py index b4748a5e1e..6dc56475e1 100644 --- a/tests/startup_root_reset_check.py +++ b/tests/startup_root_reset_check.py @@ -1,5 +1,4 @@ -"""A different root signature drops the bound descriptor tables, the same one keeps them, and root constants that -reach no stored word are logged (list slots 34, 36 and 40).""" +"""Check root table and constant lifetime through translated rendering (list slots 34, 36 and 40).""" from pathlib import Path import sys import tempfile @@ -8,14 +7,15 @@ HLSL = ( "Texture1D tex : register(t0);\n" + "cbuffer Entry : register(b0) { uint4 entry; };\n" "float4 main(float4 p : SV_Position) : SV_Target {\n" " float4 value = tex.Load(int2(uint(p.x) % 4, 0));\n" - " return float4(value.r, value.a, 0, 1);\n" + " return float4(value.r, value.a, float(entry.w) / 255.0, 1);\n" "}\n" ) CLEAR = bytes((0, 0, 255, 255)) -TEXELS = bytes(b for x in range(4) for b in (10 + 20 * x, 200 - 30 * x, 0, 255)) -NO_TABLE = bytes((0, 0, 0, 255)) * 4 +TEXELS = bytes(b for x in range(4) for b in (10 + 20 * x, 200 - 30 * x, 173, 255)) +NO_BINDINGS = bytes((0, 0, 0, 255)) * 4 def run(directory, consumer, backend, mode): @@ -23,7 +23,7 @@ def run(directory, consumer, backend, mode): folder.mkdir() log = folder / "trap.log" pixels = draw_texture(folder, consumer, backend, HLSL, {"X2W_TEST_ROOT_RESET": mode, "X2W_TRAP_LOG": str(log)}, - "tex1d") + binding="srv 0 tex1d table 0 0\nudsrc 3 rootconst 1 0") lines = [line for line in log.read_text(errors="replace").splitlines() if "root constants" in line] if log.exists() else [] return pixels, lines @@ -32,9 +32,9 @@ def check(directory, consumer, backend): # The left half of the 8x4 target is drawn, the right half keeps the clear colour. cases = ( ("bound", TEXELS, 0), - ("same_root", TEXELS, 0), # the same root signature again keeps the table - ("switch_root", NO_TABLE, 0), # another root signature drops it: the shader reads zeros - ("out_of_range", TEXELS, 4), # six calls reach no stored word; the first four are logged + ("same_root", TEXELS, 0), + ("switch_root", NO_BINDINGS, 0), + ("out_of_range", TEXELS, 4), ) for mode, drawn, logged in cases: pixels, lines = run(directory, consumer, backend, mode) diff --git a/tests/startup_stream_setup.cpp b/tests/startup_stream_setup.cpp index 636d8efddf..e176e4acd2 100644 --- a/tests/startup_stream_setup.cpp +++ b/tests/startup_stream_setup.cpp @@ -106,13 +106,21 @@ int prepare_stream_pipeline(const char* vs_path, const char* ps_path, const char D3D12_ROOT_SIGNATURE_DESC root_desc{}; const D3D12_DESCRIPTOR_RANGE srv{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0, 0, 0}; - D3D12_ROOT_PARAMETER table{}; + D3D12_ROOT_PARAMETER textured_parameters[2]{}; if (textured) { + D3D12_ROOT_PARAMETER& table = textured_parameters[0]; table.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; table.DescriptorTable = {1, &srv}; table.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; root_desc.NumParameters = 1; - root_desc.pParameters = &table; + root_desc.pParameters = textured_parameters; + if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_RESET", nullptr, 0)) { + D3D12_ROOT_PARAMETER& constant = textured_parameters[1]; + constant.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + constant.Constants = {0, 0, 1}; + constant.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; + root_desc.NumParameters = 2; + } } // X2W_TEST_ROOT_CBV= declares root parameter 0 as a constant buffer view (startup_root_cbv.cpp); // "wrong_type" declares a shader resource view there instead.