From 27125e431a60a9090664988ddb2b5529dff345cd Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 19:01:44 +0100 Subject: [PATCH 1/3] startup: drop bound descriptor tables on a root signature change and log dropped root constants --- CMakeLists.txt | 5 ++ docs/experiments.md | 9 ++++ docs/startup-runtime.md | 10 ++++ src/startup_stubs/d3d12_draw.inl | 4 ++ src/startup_stubs/d3d12_xs_device.cpp | 4 ++ tests/startup_root_reset.cpp | 70 +++++++++++++++++++++++++++ tests/startup_root_reset_check.py | 58 ++++++++++++++++++++++ tests/startup_stream_draw.cpp | 3 ++ 8 files changed, 163 insertions(+) create mode 100644 tests/startup_root_reset.cpp create mode 100644 tests/startup_root_reset_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index f53e8843ea..e03f5bdd46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -572,6 +572,7 @@ add_executable(startup_stream_consumer tests/startup_stream_consumer.cpp tests/s target_compile_features(startup_stream_consumer PRIVATE cxx_std_17) target_link_libraries(startup_stream_consumer PRIVATE d3d12_xs_shim startup_stub_xg_xs d3d12 dxgi) target_sources(startup_stream_consumer PRIVATE tests/startup_frame_copy.cpp) +target_sources(startup_stream_consumer PRIVATE tests/startup_root_reset.cpp) add_executable(startup_stream_depth tests/startup_stream_depth.cpp tests/startup_stream_setup.cpp) target_compile_features(startup_stream_depth PRIVATE cxx_std_17) @@ -1795,6 +1796,10 @@ if(EXISTS "${INTAKE_PYTHON}") "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_1d_srv_check.py" "$" ${backend}) set_tests_properties(startup_1d_srv_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + add_test(NAME startup_root_reset_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_root_reset_check.py" + "$" ${backend}) + set_tests_properties(startup_root_reset_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) add_test(NAME startup_root_cbv_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_root_cbv_check.py" "$" ${backend}) diff --git a/docs/experiments.md b/docs/experiments.md index 1114f5beb0..b8055dbc5e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -10855,6 +10855,15 @@ The [MASM fixture](../tests/cpu/vex_parity.asm) and [differential check](../test 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: written on Linux only; the test needs Windows with D3D12 and has not been run. + +```powershell +cmake --build --preset dev --target startup_stream_consumer +ctest --preset dev --no-tests=error -R "^startup_root_reset_(warp|hardware)$" -V +``` + ## Startup root constant buffer views Command-list slot 42 records the address of a root parameter declared as a CBV, so a translated shader reading through its root pointer sees it. The fixture (`tests/startup_root_cbv.cpp`, driven by `tests/startup_root_cbv_check.py`) is synthetic only: it uses generated command streams and no title data, and the slot number is derived from the desktop order and the console shift, not seen in a title log. It checks a bound call, an unbound call, a null overwrite that clears the slot, a wrong-type call that is ignored with `root CBV N ignored`, a redundant root bind that preserves the address, and a signature switch that clears it. Details are in [startup-runtime.md](startup-runtime.md#root-constant-buffer-views). diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 7d736f480c..9aec1faa2e 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -171,6 +171,16 @@ by reading a render log. The device prints the counts by cause value, so checks without a device that each cause lands in the slot printed under its label, and that the header's names follow the same order. The control swaps the buffer and token slots and must fail. +## 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 +(`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. + ## Root constant buffer views Command-list slot 42 (desktop `SetGraphicsRootConstantBufferView` plus four) records the title address for a root diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 8f72aafbaa..44ac099be9 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1191,6 +1191,10 @@ void __stdcall list_table(Wrapper* self, UINT parameter, u64 gpu_handle) // List slot 40 (desktop 36 + 4), SetGraphicsRoot32BitConstants(parameter, count, data, offset). void __stdcall list_root_constants(Wrapper* self, UINT parameter, UINT count, const std::uint32_t* data, UINT offset) { + static LONG dropped; + if (count && (parameter >= 16 || offset >= 8 || count > 8 - offset) && first_calls(dropped, 4)) + log("x2w-render root constants %s: parameter %u, offset %u, count %u (16 parameters of 8 words are kept)", + parameter >= 16 || offset >= 8 ? "dropped" : "truncated", parameter, offset, count); if (parameter >= 16 || offset >= 8 || !count || !x2w_local::readable(data, std::size_t{count} * 4)) return; for (UINT i = 0; i < count && offset + i < 8; ++i) pass_of(self).constants[parameter][offset + i] = data[i]; } diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index c91c2b97f7..5ae3337cdf 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1732,11 +1732,15 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) { Wrapper* w = as_wrapper(root); Pass& pass = pass_of(self); + const auto* previous = pass.root; const auto* params = w ? root_info(w) : nullptr; if (pass.root != params) std::fill_n(pass.root_address, 16, 0); pass.root = params; pass.srv_bound = 0; + // A different root signature leaves no descriptor table bound; the same one keeps them. + if (previous != params) + for (auto& table : pass.view_tables) table = {}; if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real) list->SetGraphicsRootSignature(static_cast(w->own().real)); } diff --git a/tests/startup_root_reset.cpp b/tests/startup_root_reset.cpp new file mode 100644 index 0000000000..2aad6f35ae --- /dev/null +++ b/tests/startup_root_reset.cpp @@ -0,0 +1,70 @@ +#include + +#include +#include + +#include "startup_stream_setup.h" + +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 +// 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 +int draw_root_reset(void* guest, void* list) +{ + char mode[16]{}; + GetEnvironmentVariableA("X2W_TEST_ROOT_RESET", mode, sizeof(mode)); + const StreamWord root = word_of(stream_root_signature()); + if (!root) return 40; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE1D, 0, 4, 1, 1, 1, + DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE}; + static unsigned char texels[16]; + for (unsigned x = 0; x < 4; ++x) { + texels[4 * x] = static_cast(10 + 20 * x); + texels[4 * x + 3] = static_cast(200 - 30 * x); + } + void* texture = nullptr; + if (FAILED(static_cast(invoke(guest, 133, word_of(texels), word_of(&desc), D3D12_RESOURCE_STATE_COMMON, 0, + word_of(&__uuidof(ID3D12Resource)), word_of(&texture))))) return 41; + struct Extended { + D3D12_DESCRIPTOR_HEAP_DESC desc; + StreamWord address, bytes; + } heap_desc{{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE}, 0, 0}; + void* heap = nullptr; + if (FAILED(static_cast(invoke(guest, 14, word_of(&heap_desc), word_of(&__uuidof(ID3D12DescriptorHeap)), + word_of(&heap))))) return 42; + D3D12_SHADER_RESOURCE_VIEW_DESC view{}; + view.Format = desc.Format; + view.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE1D; + view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + view.Texture1D.MipLevels = 1; + invoke(guest, 18, word_of(texture), word_of(&view), invoke(heap, 9)); + invoke(list, 34, root); // SetGraphicsRootSignature + invoke(list, 32, 1, word_of(&heap)); + invoke(list, 36, 0, invoke(heap, 10)); + if (!std::strcmp(mode, "same_root")) { + invoke(list, 34, root); + } else if (!std::strcmp(mode, "switch_root")) { + const StreamWord alternate = word_of(stream_alternate_root_signature()); + if (!alternate) return 40; + invoke(list, 34, alternate); + invoke(list, 34, root); + } else if (!std::strcmp(mode, "out_of_range")) { + static std::uint32_t words[10]; + invoke(list, 40, 16, 1, word_of(words), 0); // parameter 16 + invoke(list, 40, 0xffffffffu, 1, word_of(words), 0); // parameter -1 + invoke(list, 40, 0, 1, word_of(words), 8); // offset 8 + invoke(list, 40, 0, 4, word_of(words), 6); // truncated to two words + invoke(list, 40, 0, 9, word_of(words), 0); // truncated to eight words + invoke(list, 40, 99, 2, word_of(words), 0); // parameter 99 + } else if (std::strcmp(mode, "bound")) { + return 2; + } + invoke(list, 12, 3, 1); + return 0; +} diff --git a/tests/startup_root_reset_check.py b/tests/startup_root_reset_check.py new file mode 100644 index 0000000000..b4748a5e1e --- /dev/null +++ b/tests/startup_root_reset_check.py @@ -0,0 +1,58 @@ +"""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).""" +from pathlib import Path +import sys +import tempfile + +from startup_address_srv_check import draw_texture + +HLSL = ( + "Texture1D tex : register(t0);\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" + "}\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 + + +def run(directory, consumer, backend, mode): + folder = directory / 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") + lines = [line for line in log.read_text(errors="replace").splitlines() if "root constants" in line] if log.exists() else [] + return pixels, lines + + +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 + ) + for mode, drawn, logged in cases: + pixels, lines = run(directory, consumer, backend, mode) + if pixels is None: + return 77 + expected = (drawn + CLEAR * 4) * 4 + if pixels != expected: + raise AssertionError(f"{mode}: actual {pixels.hex()} expected {expected.hex()}") + if len(lines) != logged: + raise AssertionError(f"{mode}: {len(lines)} root constants log lines, expected {logged}:\n" + "\n".join(lines)) + if mode == "out_of_range": + kinds = [("dropped" if "dropped" in line else "truncated" if "truncated" in line else "?") for line in lines] + if kinds != ["dropped", "dropped", "dropped", "truncated"]: + raise AssertionError(f"{mode}: log lines {kinds}:\n" + "\n".join(lines)) + print(f"{backend} {mode}: 32 pixels exact, {logged} log lines") + return 0 + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory() as temporary: + sys.exit(check(Path(temporary), *sys.argv[1:])) diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 7fc1ff8faa..8f5e6ba1d2 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -282,6 +282,9 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* } else if (GetEnvironmentVariableW(L"X2W_TEST_ADDRESS_BUFFER_SRV", nullptr, 0)) { extern int draw_address_buffer_srvs(ID3D12Device*, void*, void*); if (const int result = draw_address_buffer_srvs(desktop.Get(), guest, list)) return result; + } else if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_RESET", nullptr, 0)) { + extern int draw_root_reset(void*, void*); + if (const int result = draw_root_reset(guest, list)) return result; } else if (GetEnvironmentVariableW(L"X2W_TEST_FRAME_COPY", nullptr, 0)) { extern int draw_frame_copies(ID3D12Device*, void*, void*, UINT, UINT); if (const int result = draw_frame_copies(desktop.Get(), guest, list, width, height)) return result; From 7e9af999c2316c26a197f28d2a79bde5a3c9b668 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 19:02:07 +0100 Subject: [PATCH 2/3] startup: keep the root signature reset and its notes clear of the open root signature changes --- docs/startup-runtime.md | 20 ++++++++++---------- src/startup_stubs/d3d12_xs_device.cpp | 7 +++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 9aec1faa2e..0afa66caf4 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -151,6 +151,16 @@ one address between two draws of a frame, such as a dynamic constant buffer, gav data. The comparison uses the kept bytes because the ring is write-combined memory that is slow to read. `startup_frame_copy_{warp,hardware}` rewrites a constant buffer between two draws and checks both halves. +## 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 +(`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. + ## Null bindings in translated draws The `x2w-render totals` line reports `null bindings N`, then the same count split by cause as @@ -171,16 +181,6 @@ by reading a render log. The device prints the counts by cause value, so checks without a device that each cause lands in the slot printed under its label, and that the header's names follow the same order. The control swaps the buffer and token slots and must fail. -## 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 -(`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. - ## Root constant buffer views Command-list slot 42 (desktop `SetGraphicsRootConstantBufferView` plus four) records the title address for a root diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 5ae3337cdf..54083924bf 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1732,15 +1732,14 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) { Wrapper* w = as_wrapper(root); Pass& pass = pass_of(self); - const auto* previous = pass.root; + // 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) std::fill_n(pass.root_address, 16, 0); pass.root = params; pass.srv_bound = 0; - // A different root signature leaves no descriptor table bound; the same one keeps them. - if (previous != params) - for (auto& table : pass.view_tables) table = {}; if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real) list->SetGraphicsRootSignature(static_cast(w->own().real)); } From 50df4c2d04472c2c5d412626e833d8c62849ba7a Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 19:14:15 +0100 Subject: [PATCH 3/3] tests: create an alternate root signature directly in startup_root_reset --- docs/experiments.md | 2 +- tests/startup_root_reset.cpp | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index b8055dbc5e..089bbecca1 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -10857,7 +10857,7 @@ 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: written on Linux only; the test needs Windows with D3D12 and has not been run. +`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. ```powershell cmake --build --preset dev --target startup_stream_consumer diff --git a/tests/startup_root_reset.cpp b/tests/startup_root_reset.cpp index 2aad6f35ae..eb1edf9b08 100644 --- a/tests/startup_root_reset.cpp +++ b/tests/startup_root_reset.cpp @@ -50,9 +50,15 @@ int draw_root_reset(void* guest, void* list) if (!std::strcmp(mode, "same_root")) { invoke(list, 34, root); } else if (!std::strcmp(mode, "switch_root")) { - const StreamWord alternate = word_of(stream_alternate_root_signature()); - if (!alternate) return 40; - invoke(list, 34, alternate); + const D3D12_ROOT_SIGNATURE_DESC empty{}; + Microsoft::WRL::ComPtr blob; + void* alternate = nullptr; + if (FAILED(D3D12SerializeRootSignature(&empty, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr)) || + FAILED(static_cast(invoke(guest, 16, 0, word_of(blob->GetBufferPointer()), blob->GetBufferSize(), + word_of(&__uuidof(ID3D12RootSignature)), word_of(&alternate)))) || + !alternate) + return 40; + invoke(list, 34, word_of(alternate)); invoke(list, 34, root); } else if (!std::strcmp(mode, "out_of_range")) { static std::uint32_t words[10];