From df802f319c3298d33b652abdca2b712db5c28f92 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 07:08:06 +0100 Subject: [PATCH 1/6] startup: opt-in copy of a graphics root SRV only for a draw that reads it Slot 44 (SetGraphicsRootShaderResourceView) at a title address without an upload mapping copies a 64 KiB window of title memory into the upload ring and binds it on the desktop list. Only a real-pipeline draw (list_draw) and an indirect graphics draw on the title's root signature read that binding; translated draws set their own root signature and copy the SRV from pass.root_address themselves. X2W_D3D12_ROOT_SRV_AT_DRAW=1 keeps the address pending at slot 44 (pass.srv_pending) and makes the copy in bind_pending_root_srvs at those two draws. Pending bits are cleared wherever srv_bound is cleared. The copy and bind are shared (bind_root_srv_copy). A report line counts deferred copies and those made at a draw. Unset, slot 44 behaves as before. Co-Authored-By: Claude Opus 5.5 --- src/startup_stubs/d3d12_draw.inl | 1 + src/startup_stubs/d3d12_xs_device.cpp | 74 +++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index d4ed161c58..906d583428 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2767,6 +2767,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI // The translated root invalidated the desktop arguments of the tracked guest root. if (pass.graphics_root) list->SetGraphicsRootSignature(pass.graphics_root); pass.srv_bound = 0; + pass.srv_pending = 0; drawn = true; } else if (indexed) { u64 offset = 0; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index ef26389b96..2f59af121e 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2821,6 +2821,9 @@ struct Pass { bool targets_bound = false; bool topology_ok = false; unsigned srv_bound = 0; // bit per root parameter with a desktop address bound + // X2W_D3D12_ROOT_SRV_AT_DRAW=1: slot 44 root SRVs whose upload copy waits for a draw that reads them. + unsigned srv_pending = 0; + u64 srv_pending_address[32]{}; const std::vector* root = nullptr; // Heap-indexing flags of the title root signature last set by slot 34. While nonzero, that root signature is // deferred: it is not set on the desktop list, root SRVs are not forwarded, and real draws go through decide_real_root. @@ -3329,6 +3332,16 @@ bool split_indirect_dispatch_enabled() } // The guest method has independent resource and byte-offset arguments. +// X2W_D3D12_ROOT_SRV_AT_DRAW=1 (opt-in): slot 44 copies a root SRV without an upload mapping only for a draw that reads +// the desktop binding (bind_pending_root_srvs); the bytes are then read at that draw instead of at slot 44. +bool root_srv_at_draw() +{ + static const bool on = GetEnvironmentVariableW(L"X2W_D3D12_ROOT_SRV_AT_DRAW", nullptr, 0) != 0; + return on; +} +LONG root_srv_copies_deferred, root_srv_copies_at_draw; +void bind_pending_root_srvs(Wrapper* self, UINT skip); + void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count, void* argument, u64 offset, void* count_buffer, u64 count_offset) { @@ -3598,6 +3611,8 @@ void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count, if (graphics) { list->SetPipelineState(static_cast(pass.pipeline->own().real)); list->SetGraphicsRootSignature(pass.graphics_root); + if (pass.srv_pending) + bind_pending_root_srvs(self, pass.indirect_graphics.cached_mesh ? pass.indirect_graphics.cached_srv_root : UINT_MAX); if (pass.indirect_graphics.cached_mesh) { const UINT srv = pass.indirect_graphics.cached_srv_root; if (srv != UINT_MAX) @@ -3687,6 +3702,7 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) } pass.root = params; pass.srv_bound = 0; + pass.srv_pending = 0; const bool real = w && w->own().kind == root_signature_kind && w->own().real; pass.root_heap_flags = real ? root_heap_flags(w) : 0; pass.deferred_root = pass.root_heap_flags ? static_cast(w->own().real) : nullptr; @@ -4128,12 +4144,50 @@ void __stdcall list_compute_upload_srv(Wrapper* self, UINT parameter, UINT64 add } } +// A graphics root SRV at a title address without an upload mapping: a 64 KiB window of title memory is copied into the +// upload ring and bound on the recording's desktop list. +void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pass, UINT parameter, u64 address) +{ + const std::size_t bytes = readable_window(address, 0x10000); + const D3D12_GPU_VIRTUAL_ADDRESS desktop = bytes ? upload_from_title(self, address, bytes) : 0; + static LONG logged; + if (first_calls(logged, 4)) { + char head[3 * 32 + 1]; + hex_text(reinterpret_cast(address), 32, head, sizeof(head)); + log("x2w-render root SRV %u at title 0x%llx: %zu bytes -> desktop 0x%llx [%s]", parameter, address, bytes, desktop, head); + } + if (!desktop) return; + list->SetGraphicsRootShaderResourceView(parameter, desktop); + pass.srv_bound |= 1u << parameter; +} + +// X2W_D3D12_ROOT_SRV_AT_DRAW=1: the bind_root_srv_copy of slot 44 waits for a draw that reads the desktop root SRV, +// a real-pipeline draw (list_draw) or an indirect graphics draw on the title's root signature. Translated draws set +// their own root signature and copy the SRV from pass.root_address themselves, so they never read it. +void bind_pending_root_srvs(Wrapper* self, UINT skip) +{ + auto* list = recording(self); + Pass& pass = pass_of(self); + unsigned pending = pass.srv_pending; + pass.srv_pending = 0; + if (!list) return; + while (pending) { + unsigned long parameter = 0; + _BitScanForward(¶meter, pending); + pending &= pending - 1; + if (parameter == skip) continue; + InterlockedIncrement(&root_srv_copies_at_draw); + bind_root_srv_copy(self, list, pass, parameter, pass.srv_pending_address[parameter]); + } +} + // List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address). void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) { auto* list = recording(self); Pass& pass = pass_of(self); if (parameter < 32) pass.srv_bound &= ~(1u << parameter); + if (parameter < 32) pass.srv_pending &= ~(1u << parameter); if (parameter < 16) pass.root_address[parameter] = address; // Under a deferred heap-indexed root the desktop list holds another root signature; only the title address is kept. if (!list || !pass.root || pass.root_heap_flags || parameter >= pass.root->size() || parameter >= 32 || @@ -4147,17 +4201,13 @@ void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) pass.srv_bound |= 1u << parameter; return; } - const std::size_t bytes = readable_window(address, 0x10000); - const D3D12_GPU_VIRTUAL_ADDRESS desktop = bytes ? upload_from_title(self, address, bytes) : 0; - static LONG logged; - if (first_calls(logged, 4)) { - char head[3 * 32 + 1]; - hex_text(reinterpret_cast(address), 32, head, sizeof(head)); - log("x2w-render root SRV %u at title 0x%llx: %zu bytes -> desktop 0x%llx [%s]", parameter, address, bytes, desktop, head); + if (root_srv_at_draw()) { + pass.srv_pending |= 1u << parameter; + pass.srv_pending_address[parameter] = address; + InterlockedIncrement(&root_srv_copies_deferred); + return; } - if (!desktop) return; - list->SetGraphicsRootShaderResourceView(parameter, desktop); - pass.srv_bound |= 1u << parameter; + bind_root_srv_copy(self, list, pass, parameter, address); } // List slot 42 (desktop 38 + 4), SetGraphicsRootConstantBufferView(parameter, title GPU address). // A translated shader reads a root CBV through the same pointer source as a root SRV (root_address). Nothing @@ -4236,6 +4286,7 @@ void __stdcall list_draw(Wrapper* self, UINT vertices, UINT instances, UINT firs // list, with an end-to-end fixture, makes the deferred bind here. list->SetGraphicsRootSignature(pass.deferred_root); } + if (list && pass.pipeline_real && pass.srv_pending) bind_pending_root_srvs(self, UINT_MAX); // The translated vertex shaders read their fetch table through root parameter 0. if (list && pass.pipeline_real && pass.targets_bound && pass.topology_ok && pass.topology != 0x11 && (pass.srv_bound & 1)) { const auto red_samples = red_producer_draw_before(self, list, "native-draw", 0); @@ -7069,6 +7120,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa heap_root_refusals[static_cast(x2w_startup::RealRootDecision::both_heaps_missing)], heap_root_refusals[static_cast(x2w_startup::RealRootDecision::state_not_restorable)], indirect_first_lookups, compute_requests_published, compute_requests_present, compute_requests_failed, compute_blob_pointer_words); + if (render_mode() && frame % 300 == 0 && root_srv_at_draw()) + log("x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): deferred %ld, made at a draw %ld", + root_srv_copies_deferred, root_srv_copies_at_draw); if (render_mode() && frame % 300 == 0) { log("x2w-render compute dispatches: translated %ld, refused %ld, no translation %ld, not modelled %ld", compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled); From b91f73521bb8dce1e3dffd18f2cef1743ed20356 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 07:24:54 +0100 Subject: [PATCH 2/6] tests: a translated draw reads a slot 44 root SRV at the draw, with and without the copy startup_root_srv_draw_{warp,hardware}: slot 44 sees red at a title address without an upload mapping, the title writes green, then a translated draw reads it through its root pointer. Both modes draw green; with X2W_D3D12_ROOT_SRV_AT_DRAW unset slot 44 also logs its copy, with it set nothing is copied. Mutant: ignoring the switch fails 'X2W_D3D12_ROOT_SRV_AT_DRAW=1: 1 slot 44 copy lines, expected none'. Co-Authored-By: Claude Opus 5.5 --- tests/cmake/startup_root_srv_at_draw.cmake | 12 +++++ tests/startup_root_srv_draw.cpp | 30 +++++++++++ tests/startup_root_srv_draw_check.py | 59 ++++++++++++++++++++++ tests/startup_stream_draw.cpp | 3 ++ tests/startup_stream_setup.cpp | 8 +-- 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 tests/cmake/startup_root_srv_at_draw.cmake create mode 100644 tests/startup_root_srv_draw.cpp create mode 100644 tests/startup_root_srv_draw_check.py diff --git a/tests/cmake/startup_root_srv_at_draw.cmake b/tests/cmake/startup_root_srv_at_draw.cmake new file mode 100644 index 0000000000..a35d6bf01c --- /dev/null +++ b/tests/cmake/startup_root_srv_at_draw.cmake @@ -0,0 +1,12 @@ +# Graphics root SRV copies made only for a draw that reads the desktop binding (opt-in X2W_D3D12_ROOT_SRV_AT_DRAW=1). +# A translated draw reads a slot 44 root SRV whose title bytes change before the draw; it runs with the switch unset +# (slot 44 copies) and set (no copy). +target_sources(startup_stream_consumer PRIVATE tests/startup_root_srv_draw.cpp) +if(EXISTS "${INTAKE_PYTHON}") + foreach(backend IN ITEMS warp hardware) + add_test(NAME startup_root_srv_draw_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_root_srv_draw_check.py" + "$" ${backend}) + set_tests_properties(startup_root_srv_draw_${backend} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/startup_root_srv_draw.cpp b/tests/startup_root_srv_draw.cpp new file mode 100644 index 0000000000..2fc1200fc9 --- /dev/null +++ b/tests/startup_root_srv_draw.cpp @@ -0,0 +1,30 @@ +#include + +#include +#include + +#include "startup_stream_setup.h" + +// A translated draw that reads a graphics root SRV set through list slot 44, at a title address without an upload +// mapping (X2W_TEST_ROOT_SRV_DRAW=write-after). Slot 44 sees the buffer holding red; the title then writes green and +// draws. The translated draw reads the SRV from its title address at the draw, so it draws green whether or not slot 44 +// also copied the bytes for the desktop binding (X2W_D3D12_ROOT_SRV_AT_DRAW). The stream root's parameter 0 is a root +// SRV in this mode (startup_stream_setup.cpp). +int draw_root_srv(void* list) +{ + char mode[16]{}; + GetEnvironmentVariableA("X2W_TEST_ROOT_SRV_DRAW", mode, sizeof(mode)); + if (std::strcmp(mode, "write-after")) return 2; + const StreamWord root = reinterpret_cast(stream_root_signature()); + if (!root) return 40; + // Title memory outside any resource: slot 44 finds no upload mapping. + auto* colour = static_cast(VirtualAlloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + if (!colour) return 41; + const float red[4] = {1, 0, 0, 1}, green[4] = {0, 1, 0, 1}; + std::memcpy(colour, red, sizeof(red)); + invoke(list, 34, root); // SetGraphicsRootSignature + invoke(list, 44, 0, reinterpret_cast(colour)); // SetGraphicsRootShaderResourceView(0, address) + std::memcpy(colour, green, sizeof(green)); + invoke(list, 12, 3, 1); // DrawInstanced(3, 1) + return 0; // The buffer stays allocated until the process ends. +} diff --git a/tests/startup_root_srv_draw_check.py b/tests/startup_root_srv_draw_check.py new file mode 100644 index 0000000000..66ae585982 --- /dev/null +++ b/tests/startup_root_srv_draw_check.py @@ -0,0 +1,59 @@ +"""A graphics root SRV (list slot 44) at a title address without an upload mapping, read by a translated draw. + +tests/startup_root_srv_draw.cpp (X2W_TEST_ROOT_SRV_DRAW=write-after): slot 44 sees the buffer holding red, the title +writes green, then draws. Run with X2W_D3D12_ROOT_SRV_AT_DRAW unset and set: + - both: the translated draw reads the SRV from its title address at the draw and draws green; + - unset: slot 44 also copies a window for the desktop binding (logged "x2w-render root SRV 0 at title"); + - set: no draw reads that binding, so no copy is made and nothing is logged. +The left half of the 8x4 target is drawn, the right half keeps the clear colour. +""" +import os +from pathlib import Path +import sys +import tempfile + +from startup_address_srv_check import draw_texture + +HLSL = ( + "ByteAddressBuffer colour : register(t0);\n" + "float4 main(float4 p : SV_Position) : SV_Target {\n" + " return asfloat(colour.Load4(0));\n" + "}\n" +) +CLEAR = bytes((0, 0, 255, 255)) +GREEN = bytes((0, 255, 0, 255)) +SWITCH = "X2W_D3D12_ROOT_SRV_AT_DRAW" +COPY_LINE = "x2w-render root SRV 0 at title" + + +def run(directory, consumer, backend, at_draw): + folder = directory / f"write-after-{int(at_draw)}" + folder.mkdir() + log = folder / "trap.log" + os.environ.pop(SWITCH, None) # an inherited setting must not decide the unset case + modes = {"X2W_TEST_ROOT_SRV_DRAW": "write-after", "X2W_TRAP_LOG": str(log)} + if at_draw: + modes[SWITCH] = "1" + pixels = draw_texture(folder, consumer, backend, HLSL, modes, binding="srv 0 buf root 0") + copies = [line for line in log.read_text(errors="replace").splitlines() if COPY_LINE in line] if log.exists() else [] + return pixels, copies + + +def check(directory, consumer, backend): + expected = (GREEN * 4 + CLEAR * 4) * 4 + for at_draw in (False, True): + pixels, copies = run(directory, consumer, backend, at_draw) + if pixels is None: + return 77 + label = f"{SWITCH}={int(at_draw)}" + if pixels != expected: + raise AssertionError(f"{label}: actual {pixels.hex()} expected {expected.hex()}") + if bool(copies) == at_draw: + raise AssertionError(f"{label}: {len(copies)} slot 44 copy lines, expected {'none' if at_draw else 'one'}") + print(f"{backend} {label}: 32 pixels exact (green, read at the draw); slot 44 copy lines {len(copies)}") + 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 9c7d7844bb..8848ae1572 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -487,6 +487,9 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* } else if (bc_chain) { extern int draw_bc_chain(void*, void*); if (const int result = draw_bc_chain(guest, list)) return result; + } else if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_SRV_DRAW", nullptr, 0)) { + extern int draw_root_srv(void*); + if (const int result = draw_root_srv(list)) return result; } else if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_CBV", nullptr, 0)) { extern int root_heap_control(ID3D12Device*); extern int draw_root_cbv(void*); diff --git a/tests/startup_stream_setup.cpp b/tests/startup_stream_setup.cpp index 2e05936d37..c92d9b49ff 100644 --- a/tests/startup_stream_setup.cpp +++ b/tests/startup_stream_setup.cpp @@ -160,11 +160,13 @@ int prepare_stream_pipeline(const char* vs_path, const char* ps_path, const char } // 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. + // X2W_TEST_ROOT_SRV_DRAW (startup_root_srv_draw.cpp) declares it as a shader resource view as well. D3D12_ROOT_PARAMETER root_cbv{}; char cbv_mode[16]{}; - if (!textured && GetEnvironmentVariableA("X2W_TEST_ROOT_CBV", cbv_mode, sizeof(cbv_mode))) { - root_cbv.ParameterType = std::strcmp(cbv_mode, "wrong_type") == 0 ? D3D12_ROOT_PARAMETER_TYPE_SRV - : D3D12_ROOT_PARAMETER_TYPE_CBV; + const bool root_srv_draw = GetEnvironmentVariableW(L"X2W_TEST_ROOT_SRV_DRAW", nullptr, 0) != 0; + if (!textured && (root_srv_draw || GetEnvironmentVariableA("X2W_TEST_ROOT_CBV", cbv_mode, sizeof(cbv_mode)))) { + root_cbv.ParameterType = root_srv_draw || std::strcmp(cbv_mode, "wrong_type") == 0 ? D3D12_ROOT_PARAMETER_TYPE_SRV + : D3D12_ROOT_PARAMETER_TYPE_CBV; root_cbv.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; root_desc.NumParameters = 1; root_desc.pParameters = &root_cbv; From 152ff0749a22e89e1921f7a8cb28c911adebeda8 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 08:22:50 +0100 Subject: [PATCH 3/6] startup: place the root SRV helpers after slot 44 and its report line before the render totals; document the switch Moves bind_root_srv_copy and bind_pending_root_srvs below list_root_srv and the report line above the render totals, away from the lines open PRs #2491 and #2919 also change. No behaviour change. Adds the switch row, a root SRV paragraph in docs/startup-runtime.md and docs/experiments/startup-root-srv-at-draw.md. Co-Authored-By: Claude Opus 5.5 --- docs/experiments/startup-root-srv-at-draw.md | 75 ++++++++++++++++++++ docs/startup-runtime.md | 10 ++- src/startup_stubs/d3d12_xs_device.cpp | 64 +++++++++-------- 3 files changed, 117 insertions(+), 32 deletions(-) create mode 100644 docs/experiments/startup-root-srv-at-draw.md diff --git a/docs/experiments/startup-root-srv-at-draw.md b/docs/experiments/startup-root-srv-at-draw.md new file mode 100644 index 0000000000..9d2d95b47b --- /dev/null +++ b/docs/experiments/startup-root-srv-at-draw.md @@ -0,0 +1,75 @@ +# Graphics root SRV: copy only for a draw that reads the desktop binding (opt-in) + +Refs [#2948](https://github.com/R0Y00/Xbox2Windows/issues/2948), +[#2949](https://github.com/R0Y00/Xbox2Windows/pull/2949). + +List slot 44 (`SetGraphicsRootShaderResourceView`) at a title address without an upload mapping copies a 64 KiB +window of title memory into the shared upload ring and binds it on the desktop list (`list_root_srv`). Two kinds of +draw read that desktop binding: a real-pipeline draw (`list_draw`, gated on `srv_bound` bit 0) and an indirect +graphics draw on the title's root signature. Translated draws set their own root signature and copy the SRV from its +title address (`pass.root_address`, `source == 2` in `draw_translated`) at the draw. In the current code a real stream +pipeline is built only over a root signature without bindings (`build_stream_pipeline`, `root_without_bindings`), so +no shader reads the slot 44 copy. + +## What the switch changes + +- `X2W_D3D12_ROOT_SRV_AT_DRAW=1`: slot 44 keeps the address pending (`pass.srv_pending`) instead of copying. + `bind_pending_root_srvs` makes the copy and the desktop binding at the two draws above, through the same + `bind_root_srv_copy` as before; the bytes are then read at that draw instead of at slot 44. +- Pending bits are cleared wherever `srv_bound` is cleared: a new slot 44 call for the parameter, slot 34, and the + translated indirect draw that restores the title's root signature. +- With the switch set, the render totals are followed every 300 frames by + `x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): deferred N, made at a draw M`. +- Unset, slot 44 copies at every call, as before. + +## Fixture + +```powershell +ctest --preset release --no-tests=error -j 1 -R '^startup_root_srv_draw_(warp|hardware)$' -V +``` + +`startup_root_srv_draw_*` (`X2W_TEST_ROOT_SRV_DRAW=write-after`, `tests/startup_root_srv_draw.cpp`): the stream +root's parameter 0 is a root SRV. Slot 44 binds a `VirtualAlloc` buffer holding red, the title writes green, then a +translated draw reads the SRV through its root pointer. Both modes draw green over the left half. With the switch +unset slot 44 also logs its copy (`x2w-render root SRV 0 at title`); with it set nothing is copied. Mutant: ignoring +the switch fails `X2W_D3D12_ROOT_SRV_AT_DRAW=1: 1 slot 44 copy lines, expected none`. + +Not covered by a pixel check: a real-pipeline draw that reads the binding, because none can be built (see above). +The clearing of pending bits follows `srv_bound` by construction. + +## Startup run of the main executable + +Local diagnostic runs, Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA driver 32.0.16.1047, i7-4790K, 16 GB, release +build of this change on `main` `98e4a2ee`, warm shader cache (revision 29), scripted input, 250 s. One build; only +the switches differ. Aggregates only; logs stay local. + +Each switch alone, against no switch: + +| | none | `ROOT_SRV_AT_DRAW` | `RING_RELEASE_AT_SUBMIT` | +|---|---|---|---| +| null bindings with cause `buffer` | 164,727 | 88 | 0 | +| `upload ring reservation refused` logged | yes | yes | no | +| visible | single black frames in the post-load animation | none seen | not watched | + +With `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1` and `X2W_D3D12_TABLE_ALIAS=1` set in every run, two pairs, the second in +reverse order: + +| | switch unset | switch set | +|---|---|---| +| mean fps 150–240 s, pair 1 / pair 2 | 6.3 / 7.1 | 14.8 / 14.0 | +| frames presented, pair 1 / pair 2 | 3,663 / 3,541 | 4,689 / 4,804 | +| executes, pair 1 / pair 2 | 33,408 / 36,029 | 72,177 / 70,916 | +| slot 44 time 150–240 s (threads; µs per call) | 1.14–1.19; 43–49 | 0.28–0.29; 5 | +| copies deferred / made at a draw | n/a | about 7 million / 0 | +| null bindings `token` per 1,000 translated draws | 33.8 / 32.3 | 36.7 / 26.9 | +| null bindings `buffer`, device removal | 0, none | 0, none | +| process commit peak | 19.1 / 19.0 GiB | 19.8 / 19.4 GiB | + +- The slot 44 copies were most of the upload ring traffic that #2948 describes: the switch alone removes all but + 88 of the buffer null bindings, but some reservations are still refused, so it complements #2949 rather than + replacing it. +- Without the table alias the frame rate barely changes (5.9 → 6.1 fps): the draw path is then bound by `frame_copy` + under `draw_lock`. +- Every run reads `deferred N, made at a draw 0`: no draw in these runs read the desktop binding. + +Not established: titles that draw with real pipelines over a root SRV (none can be built today), and other adapters. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 6694004a31..d69f340162 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -687,6 +687,13 @@ one on the desktop list, as before; a flagged one is deferred (`root signature . the desktop list`, first three). Root SRVs (slot 44) are not forwarded under a deferred root. Translated draws use their own root signature (no flags) and are unaffected. +A root SRV (slot 44) at a title address without an upload mapping copies a 64 KiB window of title memory into the +upload ring for the desktop binding. Only a real-pipeline draw (slot 12) and an indirect graphics draw on the +title's root signature read that binding; translated draws set their own root signature and copy the SRV from its +title address at the draw. With the opt-in `X2W_D3D12_ROOT_SRV_AT_DRAW=1`, slot 44 keeps the address pending and the +copy is made only by those two draws, which then read the bytes at the draw instead of at slot 44 +(`startup_root_srv_draw_*`; see [the experiment note](experiments/startup-root-srv-at-draw.md)). + A real draw under a deferred root is decided by `src/startup_stubs/heap_indexed_root.h`: it may bind only when the list holds a mirror of every heap the root indexes, covering the indices the draw reads, and every root constant, descriptor table and root descriptor can be set again. A one-entry sampler heap covers only index 0 and never stands @@ -1502,6 +1509,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr | `X2W_SHADER_CACHE=` | translation cache: the runtime writes source requests `req_.{a,b,root}`, when needed RTV variants `req_.rtv`, and for device slot 67 compute pipelines `req_.{desc,sa,sb,data,root,c67}` (see [Compute pipeline requests](#compute-pipeline-requests)). A console pipeline that an `ExecuteIndirect` draw (list slot 61) refuses writes the same request as a direct draw, once per pipeline, and draws nothing (`startup_indirect_requests_warp`). Only direct draws poll for its manifest, so such a pipeline stays in the `missing` count until a direct draw uses it; the `x2w-render totals` line counts `pipelines first looked up by ExecuteIndirect N`. This is a per-process lookup count. Check for the ready marker `req_.a` to confirm a complete request is available. Run `tools/startup/gfx2hlsl.py --pending ` while the process is running, or start the run with `run.ps1 -Translate` (see [Translating shaders during a run](#translating-shaders-during-a-run)). Active draws poll for an absent manifest at most once per second per pipeline and use the published translation on a subsequent draw. The translator compiles `.vs.cso` and `.ps.cso` with `fxc.exe`: `X2W_FXC`, else the Windows SDK 10.0.26100.0 copy, else the newest `Windows Kits\10\bin\\x64`, else `PATH`. When none exists it writes `.fail` naming `X2W_FXC` without publishing a new manifest. `X2W_FXC=` (empty) publishes the manifest alone. Rerun `--pending` with compilation enabled to fill in missing bytecode for an existing manifest. | | `X2W_D3D12_LINEAR_BUFFERS=1` or `=copy` | opt-in backing for linear buffers placed at a title address (device slot 133, initial state `COMMON`): shared where the adapter, extent and memory allow it (private allocations larger than 256 MiB are skipped only with `X2W_D3D12_CAP_EXISTING_HEAP=1`), otherwise copied from title memory at each submission, otherwise the default backing is kept and the refusal recorded; `copy` never shares. Unset or `0`, slot 133 keeps its default backing. See [Linear buffer backing (device slot 133, opt-in)](#linear-buffer-backing-device-slot-133-opt-in) | | `X2W_D3D12_CAP_EXISTING_HEAP=1` | opt-in 256 MiB (`unplaced_existing_heap_allowed`) allocation-size cap on `OpenExistingHeapFromAddress` in device slot 133 (`startup_slot133_heap_cap_*`). Unset (default), private allocations larger than 256 MiB keep the formerly aliased path: a capped copied placement is refused by `CopyBufferRegion` (list slot 15) as `title_backed_source`, losing a late guest write made after recording and before submit. Set, allocations larger than 256 MiB skip `OpenExistingHeapFromAddress` so multi-GiB startup blocks are not pinned in `WorkingSetSize` and `NonLocal` GPU memory | +| `X2W_D3D12_ROOT_SRV_AT_DRAW=1` | opt-in, render mode: a graphics root SRV (slot 44) without an upload mapping is copied into the upload ring only for a draw that reads the desktop binding (a real-pipeline draw, or an indirect graphics draw on the title's root signature), at that draw (default: a 64 KiB copy at every slot 44 call). Translated draws copy the SRV themselves and are unaffected (`startup_root_srv_draw_*`). See [the experiment note](experiments/startup-root-srv-at-draw.md) | | `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1` | opt-in, render mode: a closed command list releases its upload ring chunks once its submit is fenced instead of holding them until Reset, and the ring grows in 16 MiB chunks up to 512 MiB (default: doubling chunks up to 256 MiB, holds until Reset). The larger ring can add up to about 1 GiB of process commit at peak (startup runs in the experiment note). A later submit of the same list before its Reset fences its unchanged chunks again; if one was reused in between, that list is refused before the execute and logged (`startup_address_console_gate_ring_keep_*`, `upload_reservation`). See [the experiment note](experiments/startup-upload-ring-release-at-submit.md) | | `X2W_D3D12_QUERY_SLOTS=1` | diagnostic, in render mode: binds command-list slots 28 to 30 to the hypothesised occlusion query methods (see [Occlusion queries](#occlusion-queries)). Unset, the three slots stay no-ops | | `X2W_D3D12_BUDGET=1` | adds [video memory budget and allocation counts](#d3d12-resource-budget) to the D3D12 heartbeat | @@ -3473,7 +3481,7 @@ Buffers in `UNORDERED_ACCESS` or acceleration-structure state, and textures, kee A shared or copied placement logs `x2w-d3d12 Device::slot133(CreatePlacedResourceX?) linear buffer ... existing-heaps=
share=
->
shared|copied at submit`. A kept default logs the usual slot 133 line with `(kept default backing: linear refusal
, use=... type=... existing-heaps=
share=
)` appended. `x2w_d3d12_linear_buffer_info` reports path 3 with `refusal_hr`, and `stale`, `refresh_failures` and `last_refresh_hr` for a copied buffer. A refused submission logs `x2w-d3d12 linear buffers: submission on queue refused: its refresh would have to wait for queue (), so it has no safe completion order; none of its lists ran` (the first eight). `x2w_d3d12_linear_buffer_totals` counts `kept_default`, `refresh_failures` and `unsafe_refresh_refusals`. Fixtures read the same facts through `x2w_d3d12_linear_buffer_info` and `x2w_d3d12_linear_buffer_totals` (`src/startup_stubs/linear_buffer_report.h`). Fixture-only switches: `X2W_TEST_EXISTING_HEAPS_UNSUPPORTED=1` makes the feature query report no support, as an adapter with `EXISTING_HEAPS = 0` does. `X2W_TEST_LINEAR_REFRESH_FAIL=` makes staging requests 2, 2 + n, 2 + 2n and so on fail with `E_OUTOFMEMORY`. `X2W_TEST_LINEAR_STAGING_LIMIT=` shrinks the staging pool from 256 buffers to `n`. `x2w_d3d12_linear_buffer_internal_queue` hands fixtures the internal queue, so they can hold refreshes in flight behind a fence of their own. `x2w_d3d12_linear_buffer_test_pause` parks the next refresh at one point (before staging, after staging, or after its copy executed and before it is published), so a fixture can order two threads' refreshes or release a buffer while one is parked. Point 3 instead parks the next title `Wait` (queue slot 17) after the desktop `Wait` and before it is recorded, while it holds that queue's order lock. `X2W_TEST_LINEAR_BUFFER_MUTANT=` removes one guarantee for a negative control. Results and commands: [Production slice: slot 133 linear buffers](experiments.md#production-slice-slot-133-linear-buffers-1914). -Not covered: table SRVs of copied buffers in translated draws still take `frame_copy` at record, and root SRVs (`list_root_srv`) still take `upload_from_title` at bind. Neither reads the slot 133 backing. Shared buffers and their heaps live for the process, like every wrapper, so a private range under one cannot be released. +Not covered: table SRVs of copied buffers in translated draws still take `frame_copy` at record, and root SRVs (`list_root_srv`) still take `upload_from_title` at bind (with `X2W_D3D12_ROOT_SRV_AT_DRAW=1`, at a draw that reads the binding). Neither reads the slot 133 backing. Shared buffers and their heaps live for the process, like every wrapper, so a private range under one cannot be released. ## Buffer memory SRVs (device slot 70) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 2f59af121e..0f959f2c5e 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3340,6 +3340,7 @@ bool root_srv_at_draw() return on; } LONG root_srv_copies_deferred, root_srv_copies_at_draw; +void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pass, UINT parameter, u64 address); void bind_pending_root_srvs(Wrapper* self, UINT skip); void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count, void* argument, u64 offset, @@ -4144,6 +4145,35 @@ void __stdcall list_compute_upload_srv(Wrapper* self, UINT parameter, UINT64 add } } +// List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address). +void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) +{ + auto* list = recording(self); + Pass& pass = pass_of(self); + if (parameter < 32) pass.srv_bound &= ~(1u << parameter); + if (parameter < 32) pass.srv_pending &= ~(1u << parameter); + if (parameter < 16) pass.root_address[parameter] = address; + // Under a deferred heap-indexed root the desktop list holds another root signature; only the title address is kept. + if (!list || !pass.root || pass.root_heap_flags || parameter >= pass.root->size() || parameter >= 32 || + (*pass.root)[parameter].type != D3D12_ROOT_PARAMETER_TYPE_SRV) + return; + if (pass.indirect_graphics.cached_mesh && pass.indirect_graphics.cached_srv_root == parameter) + return; // The slot 61 draw binds the validated lease rather than an upload snapshot. + if (const auto gpu = list_upload_gpu(self, address, 1)) { + if (gpu % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT) return; + list->SetGraphicsRootShaderResourceView(parameter, gpu); + pass.srv_bound |= 1u << parameter; + return; + } + if (root_srv_at_draw()) { + pass.srv_pending |= 1u << parameter; + pass.srv_pending_address[parameter] = address; + InterlockedIncrement(&root_srv_copies_deferred); + return; + } + bind_root_srv_copy(self, list, pass, parameter, address); +} + // A graphics root SRV at a title address without an upload mapping: a 64 KiB window of title memory is copied into the // upload ring and bound on the recording's desktop list. void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pass, UINT parameter, u64 address) @@ -4181,34 +4211,6 @@ void bind_pending_root_srvs(Wrapper* self, UINT skip) } } -// List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address). -void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) -{ - auto* list = recording(self); - Pass& pass = pass_of(self); - if (parameter < 32) pass.srv_bound &= ~(1u << parameter); - if (parameter < 32) pass.srv_pending &= ~(1u << parameter); - if (parameter < 16) pass.root_address[parameter] = address; - // Under a deferred heap-indexed root the desktop list holds another root signature; only the title address is kept. - if (!list || !pass.root || pass.root_heap_flags || parameter >= pass.root->size() || parameter >= 32 || - (*pass.root)[parameter].type != D3D12_ROOT_PARAMETER_TYPE_SRV) - return; - if (pass.indirect_graphics.cached_mesh && pass.indirect_graphics.cached_srv_root == parameter) - return; // The slot 61 draw binds the validated lease rather than an upload snapshot. - if (const auto gpu = list_upload_gpu(self, address, 1)) { - if (gpu % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT) return; - list->SetGraphicsRootShaderResourceView(parameter, gpu); - pass.srv_bound |= 1u << parameter; - return; - } - if (root_srv_at_draw()) { - pass.srv_pending |= 1u << parameter; - pass.srv_pending_address[parameter] = address; - InterlockedIncrement(&root_srv_copies_deferred); - return; - } - bind_root_srv_copy(self, list, pass, parameter, address); -} // List slot 42 (desktop 38 + 4), SetGraphicsRootConstantBufferView(parameter, title GPU address). // A translated shader reads a root CBV through the same pointer source as a root SRV (root_address). Nothing // is forwarded to the desktop list and srv_bound is left alone: bit 0 means a vertex-fetch SRV is bound. @@ -7106,6 +7108,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa for (int c = 0; c < x2w_local::null_cause_count; ++c) null_causes[c] = null_by_cause[c]; ReleaseSRWLockExclusive(&draw_lock); } + if (render_mode() && frame % 300 == 0 && root_srv_at_draw()) + log("x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): deferred %ld, made at a draw %ld", + root_srv_copies_deferred, root_srv_copies_at_draw); if (render_mode() && frame % 300 == 0) log("x2w-render totals: resets %ld (failed %ld), closes %ld, clears %ld, target binds %ld (unresolved %ld), executes %ld, draws %ld (skipped %ld), " "translated draws %ld (not drawn %ld), pipelines translated %ld / missing %ld, null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld), " @@ -7120,9 +7125,6 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa heap_root_refusals[static_cast(x2w_startup::RealRootDecision::both_heaps_missing)], heap_root_refusals[static_cast(x2w_startup::RealRootDecision::state_not_restorable)], indirect_first_lookups, compute_requests_published, compute_requests_present, compute_requests_failed, compute_blob_pointer_words); - if (render_mode() && frame % 300 == 0 && root_srv_at_draw()) - log("x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): deferred %ld, made at a draw %ld", - root_srv_copies_deferred, root_srv_copies_at_draw); if (render_mode() && frame % 300 == 0) { log("x2w-render compute dispatches: translated %ld, refused %ld, no translation %ld, not modelled %ld", compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled); From bedfcafcea7438d8e5bb47081ff3f2ee5c2441c6 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 08:40:01 +0100 Subject: [PATCH 4/6] tests: assert the slot 44 copy counts instead of the copy log line x2w_d3d12_root_srv_copies exports the deferred, made-at-a-draw and copied counts; startup_root_srv_draw prints them after the draw is recorded and the check expects (0, 0, 1) unset and (1, 0, 0) with X2W_D3D12_ROOT_SRV_AT_DRAW=1. Mutant: ignoring the switch fails with (0, 0, 1), expected (1, 0, 0). Co-Authored-By: Claude Opus 5.5 --- docs/experiments/startup-root-srv-at-draw.md | 10 +++-- src/startup_stubs/d3d12_xs_device.cpp | 12 ++++- tests/startup_root_srv_draw.cpp | 12 ++++- tests/startup_root_srv_draw_check.py | 47 +++++++++++++------- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/docs/experiments/startup-root-srv-at-draw.md b/docs/experiments/startup-root-srv-at-draw.md index 9d2d95b47b..794e2f0cc7 100644 --- a/docs/experiments/startup-root-srv-at-draw.md +++ b/docs/experiments/startup-root-srv-at-draw.md @@ -30,12 +30,14 @@ ctest --preset release --no-tests=error -j 1 -R '^startup_root_srv_draw_(warp|ha `startup_root_srv_draw_*` (`X2W_TEST_ROOT_SRV_DRAW=write-after`, `tests/startup_root_srv_draw.cpp`): the stream root's parameter 0 is a root SRV. Slot 44 binds a `VirtualAlloc` buffer holding red, the title writes green, then a -translated draw reads the SRV through its root pointer. Both modes draw green over the left half. With the switch -unset slot 44 also logs its copy (`x2w-render root SRV 0 at title`); with it set nothing is copied. Mutant: ignoring -the switch fails `X2W_D3D12_ROOT_SRV_AT_DRAW=1: 1 slot 44 copy lines, expected none`. +translated draw reads the SRV through its root pointer. Both modes draw green over the left half. After the draw is +recorded the fixture reads the stand-in's counts (`x2w_d3d12_root_srv_copies`): unset, `deferred 0, made at a draw 0, +copied 1`; set, `deferred 1, made at a draw 0, copied 0`. Mutant: ignoring the switch fails +`X2W_D3D12_ROOT_SRV_AT_DRAW=1: copies (deferred, made at a draw, copied) (0, 0, 1), expected (1, 0, 0)`. Not covered by a pixel check: a real-pipeline draw that reads the binding, because none can be built (see above). -The clearing of pending bits follows `srv_bound` by construction. +The clearing of pending bits follows `srv_bound` by construction, and `list_close` resets the whole pass, so a +pending address never reaches the next recording. ## Startup run of the main executable diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 0f959f2c5e..00ba9a84dc 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3339,7 +3339,7 @@ bool root_srv_at_draw() static const bool on = GetEnvironmentVariableW(L"X2W_D3D12_ROOT_SRV_AT_DRAW", nullptr, 0) != 0; return on; } -LONG root_srv_copies_deferred, root_srv_copies_at_draw; +LONG root_srv_copies_deferred, root_srv_copies_at_draw, root_srv_copies_made; void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pass, UINT parameter, u64 address); void bind_pending_root_srvs(Wrapper* self, UINT skip); @@ -4187,10 +4187,20 @@ void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pa log("x2w-render root SRV %u at title 0x%llx: %zu bytes -> desktop 0x%llx [%s]", parameter, address, bytes, desktop, head); } if (!desktop) return; + InterlockedIncrement(&root_srv_copies_made); list->SetGraphicsRootShaderResourceView(parameter, desktop); pass.srv_bound |= 1u << parameter; } +// Fixture export (startup_root_srv_draw): slot 44 root SRV copies since the process started. `deferred` and `at_draw` +// count only with X2W_D3D12_ROOT_SRV_AT_DRAW=1; `copied` counts every copy into the upload ring, either mode. +extern "C" __declspec(dllexport) void x2w_d3d12_root_srv_copies(LONG* deferred, LONG* at_draw, LONG* copied) +{ + if (deferred) *deferred = root_srv_copies_deferred; + if (at_draw) *at_draw = root_srv_copies_at_draw; + if (copied) *copied = root_srv_copies_made; +} + // X2W_D3D12_ROOT_SRV_AT_DRAW=1: the bind_root_srv_copy of slot 44 waits for a draw that reads the desktop root SRV, // a real-pipeline draw (list_draw) or an indirect graphics draw on the title's root signature. Translated draws set // their own root signature and copy the SRV from pass.root_address themselves, so they never read it. diff --git a/tests/startup_root_srv_draw.cpp b/tests/startup_root_srv_draw.cpp index 2fc1200fc9..7b443594c3 100644 --- a/tests/startup_root_srv_draw.cpp +++ b/tests/startup_root_srv_draw.cpp @@ -1,15 +1,19 @@ #include #include +#include #include #include "startup_stream_setup.h" +extern "C" __declspec(dllimport) void x2w_d3d12_root_srv_copies(LONG* deferred, LONG* at_draw, LONG* copied); + // A translated draw that reads a graphics root SRV set through list slot 44, at a title address without an upload // mapping (X2W_TEST_ROOT_SRV_DRAW=write-after). Slot 44 sees the buffer holding red; the title then writes green and // draws. The translated draw reads the SRV from its title address at the draw, so it draws green whether or not slot 44 -// also copied the bytes for the desktop binding (X2W_D3D12_ROOT_SRV_AT_DRAW). The stream root's parameter 0 is a root -// SRV in this mode (startup_stream_setup.cpp). +// also copied the bytes for the desktop binding (X2W_D3D12_ROOT_SRV_AT_DRAW). After the draw is recorded it prints the +// stand-in's copy counts (x2w_d3d12_root_srv_copies). The stream root's parameter 0 is a root SRV in this mode +// (startup_stream_setup.cpp). int draw_root_srv(void* list) { char mode[16]{}; @@ -26,5 +30,9 @@ int draw_root_srv(void* list) invoke(list, 44, 0, reinterpret_cast(colour)); // SetGraphicsRootShaderResourceView(0, address) std::memcpy(colour, green, sizeof(green)); invoke(list, 12, 3, 1); // DrawInstanced(3, 1) + // The stand-in's slot 44 copy counts after the draw was recorded, for the check script. + LONG deferred = -1, at_draw = -1, copied = -1; + x2w_d3d12_root_srv_copies(&deferred, &at_draw, &copied); + std::printf("root SRV copies: deferred %ld, made at a draw %ld, copied %ld\n", deferred, at_draw, copied); return 0; // The buffer stays allocated until the process ends. } diff --git a/tests/startup_root_srv_draw_check.py b/tests/startup_root_srv_draw_check.py index 66ae585982..a9308a47ac 100644 --- a/tests/startup_root_srv_draw_check.py +++ b/tests/startup_root_srv_draw_check.py @@ -1,18 +1,20 @@ """A graphics root SRV (list slot 44) at a title address without an upload mapping, read by a translated draw. tests/startup_root_srv_draw.cpp (X2W_TEST_ROOT_SRV_DRAW=write-after): slot 44 sees the buffer holding red, the title -writes green, then draws. Run with X2W_D3D12_ROOT_SRV_AT_DRAW unset and set: +writes green, then draws, and the stand-in's copy counts are printed. Run with X2W_D3D12_ROOT_SRV_AT_DRAW unset and set: - both: the translated draw reads the SRV from its title address at the draw and draws green; - - unset: slot 44 also copies a window for the desktop binding (logged "x2w-render root SRV 0 at title"); - - set: no draw reads that binding, so no copy is made and nothing is logged. + - unset: slot 44 also copies a window into the upload ring for the desktop binding (copied 1); + - set: no draw reads that binding, so the copy is deferred and never made (deferred 1, made at a draw 0, copied 0). The left half of the 8x4 target is drawn, the right half keeps the clear colour. """ import os from pathlib import Path +import re +import subprocess import sys import tempfile -from startup_address_srv_check import draw_texture +from startup_address_srv_check import prepare_draw_cache HLSL = ( "ByteAddressBuffer colour : register(t0);\n" @@ -23,34 +25,45 @@ CLEAR = bytes((0, 0, 255, 255)) GREEN = bytes((0, 255, 0, 255)) SWITCH = "X2W_D3D12_ROOT_SRV_AT_DRAW" -COPY_LINE = "x2w-render root SRV 0 at title" +COUNTS = re.compile(r"root SRV copies: deferred (-?\d+), made at a draw (-?\d+), copied (-?\d+)") def run(directory, consumer, backend, at_draw): folder = directory / f"write-after-{int(at_draw)}" folder.mkdir() - log = folder / "trap.log" os.environ.pop(SWITCH, None) # an inherited setting must not decide the unset case - modes = {"X2W_TEST_ROOT_SRV_DRAW": "write-after", "X2W_TRAP_LOG": str(log)} + modes = {"X2W_TEST_ROOT_SRV_DRAW": "write-after", "X2W_TRAP_LOG": str(folder / "trap.log")} if at_draw: modes[SWITCH] = "1" - pixels = draw_texture(folder, consumer, backend, HLSL, modes, binding="srv 0 buf root 0") - copies = [line for line in log.read_text(errors="replace").splitlines() if COPY_LINE in line] if log.exists() else [] - return pixels, copies + env, args, output = prepare_draw_cache(folder, consumer, HLSL, modes, "srv 0 buf root 0") + result = subprocess.run([*args, backend, output], env=env, capture_output=True, text=True) + print(result.stdout, end="") + if result.returncode == 77: + return None, None + if result.returncode: + raise RuntimeError(f"consumer failed ({result.returncode})\n{result.stderr[-5000:]}") + found = COUNTS.search(result.stdout) + if not found: + raise AssertionError(f"{SWITCH}={int(at_draw)}: no copy counts printed") + return output.read_bytes(), tuple(int(value) for value in found.groups()) def check(directory, consumer, backend): - expected = (GREEN * 4 + CLEAR * 4) * 4 + expected_pixels = (GREEN * 4 + CLEAR * 4) * 4 + # (deferred, made at a draw, copied) + expected_counts = {False: (0, 0, 1), True: (1, 0, 0)} for at_draw in (False, True): - pixels, copies = run(directory, consumer, backend, at_draw) + pixels, counts = run(directory, consumer, backend, at_draw) if pixels is None: return 77 label = f"{SWITCH}={int(at_draw)}" - if pixels != expected: - raise AssertionError(f"{label}: actual {pixels.hex()} expected {expected.hex()}") - if bool(copies) == at_draw: - raise AssertionError(f"{label}: {len(copies)} slot 44 copy lines, expected {'none' if at_draw else 'one'}") - print(f"{backend} {label}: 32 pixels exact (green, read at the draw); slot 44 copy lines {len(copies)}") + if pixels != expected_pixels: + raise AssertionError(f"{label}: actual {pixels.hex()} expected {expected_pixels.hex()}") + if counts != expected_counts[at_draw]: + raise AssertionError(f"{label}: copies (deferred, made at a draw, copied) {counts}, " + f"expected {expected_counts[at_draw]}") + print(f"{backend} {label}: 32 pixels exact (green, read at the draw); " + f"deferred {counts[0]}, made at a draw {counts[1]}, copied {counts[2]}") return 0 From 7fb8bd88d42b366efc140ea8332034f70cda79a3 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:44:32 +0100 Subject: [PATCH 5/6] startup: skip the slot 44 root SRV copy instead of deferring it Review of #3028: no draw can read the desktop root SRV binding. list_draw forwards only with pipeline_real, which holds only for compute pipelines (every accepted desktop graphics stream gets an indirect graphics root), and a slot 61 graphics draw binds a pipeline over the list's root (empty, or the cached mesh root whose only SRV comes from its lease). The pending state, bind_pending_root_srvs and both call sites are removed: with X2W_D3D12_ROOT_SRV_AT_DRAW=1 slot 44 counts and skips the copy and the parameter stays unbound. The fixture export reports (skipped, copied). Co-Authored-By: Claude Opus 5.5 --- docs/experiments/startup-root-srv-at-draw.md | 53 +++++++++------- docs/startup-runtime.md | 14 ++-- src/startup_stubs/d3d12_draw.inl | 1 - src/startup_stubs/d3d12_xs_device.cpp | 67 +++++--------------- tests/cmake/startup_root_srv_at_draw.cmake | 2 +- tests/startup_root_srv_draw.cpp | 14 ++-- tests/startup_root_srv_draw_check.py | 12 ++-- 7 files changed, 67 insertions(+), 96 deletions(-) diff --git a/docs/experiments/startup-root-srv-at-draw.md b/docs/experiments/startup-root-srv-at-draw.md index 794e2f0cc7..5b8f743064 100644 --- a/docs/experiments/startup-root-srv-at-draw.md +++ b/docs/experiments/startup-root-srv-at-draw.md @@ -1,25 +1,30 @@ -# Graphics root SRV: copy only for a draw that reads the desktop binding (opt-in) +# Graphics root SRV: no desktop copy that no draw reads (opt-in) Refs [#2948](https://github.com/R0Y00/Xbox2Windows/issues/2948), [#2949](https://github.com/R0Y00/Xbox2Windows/pull/2949). List slot 44 (`SetGraphicsRootShaderResourceView`) at a title address without an upload mapping copies a 64 KiB -window of title memory into the shared upload ring and binds it on the desktop list (`list_root_srv`). Two kinds of -draw read that desktop binding: a real-pipeline draw (`list_draw`, gated on `srv_bound` bit 0) and an indirect -graphics draw on the title's root signature. Translated draws set their own root signature and copy the SRV from its -title address (`pass.root_address`, `source == 2` in `draw_translated`) at the draw. In the current code a real stream -pipeline is built only over a root signature without bindings (`build_stream_pipeline`, `root_without_bindings`), so -no shader reads the slot 44 copy. +window of title memory into the shared upload ring and binds it on the desktop list (`list_root_srv`). In the current +code no draw reads that desktop binding: + +- Translated draws set their own root signature and read the SRV from its title address (`pass.root_address`, + `source == 2` in `draw_translated`) at the draw. +- `list_draw` forwards a draw only with `pass.pipeline_real`, which `list_pipeline` sets for a desktop pipeline + without an indirect graphics root. `build_stream_pipeline` accepts a graphics stream only with VS/PS or MS/PS + ("desktop indirect graphics stream needs ...") and gives every accepted one that root, so `pipeline_real` holds only + for compute pipelines. A vertex-only desktop stream is refused by the same rule. +- An indirect graphics draw (list slot 61) requires `pass.indirect_graphics.root == pass.graphics_root`: an empty + root signature (`root_without_bindings`), or for a cached mesh pipeline the title's root, whose only root SRV + (`cached_srv_root`) the draw binds from its validated lease. ## What the switch changes -- `X2W_D3D12_ROOT_SRV_AT_DRAW=1`: slot 44 keeps the address pending (`pass.srv_pending`) instead of copying. - `bind_pending_root_srvs` makes the copy and the desktop binding at the two draws above, through the same - `bind_root_srv_copy` as before; the bytes are then read at that draw instead of at slot 44. -- Pending bits are cleared wherever `srv_bound` is cleared: a new slot 44 call for the parameter, slot 34, and the - translated indirect draw that restores the title's root signature. +- `X2W_D3D12_ROOT_SRV_AT_DRAW=1`: slot 44 skips that copy. The title address is kept for translated draws as before, + and the parameter stays unbound on the desktop list (`srv_bound` clear), so a forwarded `list_draw` that needs bit 0 + is skipped rather than drawn with an older binding. A desktop graphics pipeline that reads a root SRV, if one is + added, would need the copy at its draw. - With the switch set, the render totals are followed every 300 frames by - `x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): deferred N, made at a draw M`. + `x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): skipped N, copied M`. - Unset, slot 44 copies at every call, as before. ## Fixture @@ -31,19 +36,19 @@ ctest --preset release --no-tests=error -j 1 -R '^startup_root_srv_draw_(warp|ha `startup_root_srv_draw_*` (`X2W_TEST_ROOT_SRV_DRAW=write-after`, `tests/startup_root_srv_draw.cpp`): the stream root's parameter 0 is a root SRV. Slot 44 binds a `VirtualAlloc` buffer holding red, the title writes green, then a translated draw reads the SRV through its root pointer. Both modes draw green over the left half. After the draw is -recorded the fixture reads the stand-in's counts (`x2w_d3d12_root_srv_copies`): unset, `deferred 0, made at a draw 0, -copied 1`; set, `deferred 1, made at a draw 0, copied 0`. Mutant: ignoring the switch fails -`X2W_D3D12_ROOT_SRV_AT_DRAW=1: copies (deferred, made at a draw, copied) (0, 0, 1), expected (1, 0, 0)`. +recorded the fixture reads the stand-in's counts (`x2w_d3d12_root_srv_copies`): unset, `skipped 0, copied 1`; set, +`skipped 1, copied 0`. Mutant: ignoring the switch fails +`X2W_D3D12_ROOT_SRV_AT_DRAW=1: copies (skipped, copied) (0, 1), expected (1, 0)`. -Not covered by a pixel check: a real-pipeline draw that reads the binding, because none can be built (see above). -The clearing of pending bits follows `srv_bound` by construction, and `list_close` resets the whole pass, so a -pending address never reaches the next recording. +Not covered: a desktop draw that reads the binding, because none can be built (see above). ## Startup run of the main executable Local diagnostic runs, Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA driver 32.0.16.1047, i7-4790K, 16 GB, release -build of this change on `main` `98e4a2ee`, warm shader cache (revision 29), scripted input, 250 s. One build; only -the switches differ. Aggregates only; logs stay local. +build on `main` `98e4a2ee`, warm shader cache (revision 29), scripted input, 250 s. One build; only the switches +differ. Aggregates only; logs stay local. The tables were measured with the first revision of this change +(`bedfcafc`), which kept the address pending for a desktop draw instead of skipping the copy; no run made a copy at +a draw, so slot 44 did the same work as now. Each switch alone, against no switch: @@ -62,7 +67,7 @@ reverse order: | frames presented, pair 1 / pair 2 | 3,663 / 3,541 | 4,689 / 4,804 | | executes, pair 1 / pair 2 | 33,408 / 36,029 | 72,177 / 70,916 | | slot 44 time 150–240 s (threads; µs per call) | 1.14–1.19; 43–49 | 0.28–0.29; 5 | -| copies deferred / made at a draw | n/a | about 7 million / 0 | +| copies not made at slot 44 / made at a draw | n/a | about 7 million / 0 | | null bindings `token` per 1,000 translated draws | 33.8 / 32.3 | 36.7 / 26.9 | | null bindings `buffer`, device removal | 0, none | 0, none | | process commit peak | 19.1 / 19.0 GiB | 19.8 / 19.4 GiB | @@ -72,6 +77,6 @@ reverse order: replacing it. - Without the table alias the frame rate barely changes (5.9 → 6.1 fps): the draw path is then bound by `frame_copy` under `draw_lock`. -- Every run reads `deferred N, made at a draw 0`: no draw in these runs read the desktop binding. +- Every run read `deferred N, made at a draw 0`: no draw in these runs read the desktop binding. -Not established: titles that draw with real pipelines over a root SRV (none can be built today), and other adapters. +Not established: desktop graphics pipelines that read a root SRV (none can be built today), and other adapters. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index d69f340162..540cbd3085 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -688,11 +688,11 @@ the desktop list`, first three). Root SRVs (slot 44) are not forwarded under a d their own root signature (no flags) and are unaffected. A root SRV (slot 44) at a title address without an upload mapping copies a 64 KiB window of title memory into the -upload ring for the desktop binding. Only a real-pipeline draw (slot 12) and an indirect graphics draw on the -title's root signature read that binding; translated draws set their own root signature and copy the SRV from its -title address at the draw. With the opt-in `X2W_D3D12_ROOT_SRV_AT_DRAW=1`, slot 44 keeps the address pending and the -copy is made only by those two draws, which then read the bytes at the draw instead of at slot 44 -(`startup_root_srv_draw_*`; see [the experiment note](experiments/startup-root-srv-at-draw.md)). +upload ring for the desktop binding. No draw reads that binding today: translated draws set their own root signature +and copy the SRV from its title address at the draw, a forwarded slot 12 draw needs a desktop pipeline without an +indirect graphics root (only compute pipelines), and an indirect graphics draw binds a pipeline over the list's root. +With the opt-in `X2W_D3D12_ROOT_SRV_AT_DRAW=1`, slot 44 skips the copy and leaves the parameter unbound on the desktop +list (`startup_root_srv_draw_*`; see [the experiment note](experiments/startup-root-srv-at-draw.md)). A real draw under a deferred root is decided by `src/startup_stubs/heap_indexed_root.h`: it may bind only when the list holds a mirror of every heap the root indexes, covering the indices the draw reads, and every root constant, @@ -1509,7 +1509,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr | `X2W_SHADER_CACHE=` | translation cache: the runtime writes source requests `req_.{a,b,root}`, when needed RTV variants `req_.rtv`, and for device slot 67 compute pipelines `req_.{desc,sa,sb,data,root,c67}` (see [Compute pipeline requests](#compute-pipeline-requests)). A console pipeline that an `ExecuteIndirect` draw (list slot 61) refuses writes the same request as a direct draw, once per pipeline, and draws nothing (`startup_indirect_requests_warp`). Only direct draws poll for its manifest, so such a pipeline stays in the `missing` count until a direct draw uses it; the `x2w-render totals` line counts `pipelines first looked up by ExecuteIndirect N`. This is a per-process lookup count. Check for the ready marker `req_.a` to confirm a complete request is available. Run `tools/startup/gfx2hlsl.py --pending ` while the process is running, or start the run with `run.ps1 -Translate` (see [Translating shaders during a run](#translating-shaders-during-a-run)). Active draws poll for an absent manifest at most once per second per pipeline and use the published translation on a subsequent draw. The translator compiles `.vs.cso` and `.ps.cso` with `fxc.exe`: `X2W_FXC`, else the Windows SDK 10.0.26100.0 copy, else the newest `Windows Kits\10\bin\\x64`, else `PATH`. When none exists it writes `.fail` naming `X2W_FXC` without publishing a new manifest. `X2W_FXC=` (empty) publishes the manifest alone. Rerun `--pending` with compilation enabled to fill in missing bytecode for an existing manifest. | | `X2W_D3D12_LINEAR_BUFFERS=1` or `=copy` | opt-in backing for linear buffers placed at a title address (device slot 133, initial state `COMMON`): shared where the adapter, extent and memory allow it (private allocations larger than 256 MiB are skipped only with `X2W_D3D12_CAP_EXISTING_HEAP=1`), otherwise copied from title memory at each submission, otherwise the default backing is kept and the refusal recorded; `copy` never shares. Unset or `0`, slot 133 keeps its default backing. See [Linear buffer backing (device slot 133, opt-in)](#linear-buffer-backing-device-slot-133-opt-in) | | `X2W_D3D12_CAP_EXISTING_HEAP=1` | opt-in 256 MiB (`unplaced_existing_heap_allowed`) allocation-size cap on `OpenExistingHeapFromAddress` in device slot 133 (`startup_slot133_heap_cap_*`). Unset (default), private allocations larger than 256 MiB keep the formerly aliased path: a capped copied placement is refused by `CopyBufferRegion` (list slot 15) as `title_backed_source`, losing a late guest write made after recording and before submit. Set, allocations larger than 256 MiB skip `OpenExistingHeapFromAddress` so multi-GiB startup blocks are not pinned in `WorkingSetSize` and `NonLocal` GPU memory | -| `X2W_D3D12_ROOT_SRV_AT_DRAW=1` | opt-in, render mode: a graphics root SRV (slot 44) without an upload mapping is copied into the upload ring only for a draw that reads the desktop binding (a real-pipeline draw, or an indirect graphics draw on the title's root signature), at that draw (default: a 64 KiB copy at every slot 44 call). Translated draws copy the SRV themselves and are unaffected (`startup_root_srv_draw_*`). See [the experiment note](experiments/startup-root-srv-at-draw.md) | +| `X2W_D3D12_ROOT_SRV_AT_DRAW=1` | opt-in, render mode: a graphics root SRV (slot 44) without an upload mapping is not copied into the upload ring, since no draw reads that desktop binding (default: a 64 KiB copy at every slot 44 call). Translated draws copy the SRV themselves and are unaffected (`startup_root_srv_draw_*`). See [the experiment note](experiments/startup-root-srv-at-draw.md) | | `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1` | opt-in, render mode: a closed command list releases its upload ring chunks once its submit is fenced instead of holding them until Reset, and the ring grows in 16 MiB chunks up to 512 MiB (default: doubling chunks up to 256 MiB, holds until Reset). The larger ring can add up to about 1 GiB of process commit at peak (startup runs in the experiment note). A later submit of the same list before its Reset fences its unchanged chunks again; if one was reused in between, that list is refused before the execute and logged (`startup_address_console_gate_ring_keep_*`, `upload_reservation`). See [the experiment note](experiments/startup-upload-ring-release-at-submit.md) | | `X2W_D3D12_QUERY_SLOTS=1` | diagnostic, in render mode: binds command-list slots 28 to 30 to the hypothesised occlusion query methods (see [Occlusion queries](#occlusion-queries)). Unset, the three slots stay no-ops | | `X2W_D3D12_BUDGET=1` | adds [video memory budget and allocation counts](#d3d12-resource-budget) to the D3D12 heartbeat | @@ -3481,7 +3481,7 @@ Buffers in `UNORDERED_ACCESS` or acceleration-structure state, and textures, kee A shared or copied placement logs `x2w-d3d12 Device::slot133(CreatePlacedResourceX?) linear buffer ... existing-heaps=
share=
->
shared|copied at submit`. A kept default logs the usual slot 133 line with `(kept default backing: linear refusal
, use=... type=... existing-heaps=
share=
)` appended. `x2w_d3d12_linear_buffer_info` reports path 3 with `refusal_hr`, and `stale`, `refresh_failures` and `last_refresh_hr` for a copied buffer. A refused submission logs `x2w-d3d12 linear buffers: submission on queue refused: its refresh would have to wait for queue (), so it has no safe completion order; none of its lists ran` (the first eight). `x2w_d3d12_linear_buffer_totals` counts `kept_default`, `refresh_failures` and `unsafe_refresh_refusals`. Fixtures read the same facts through `x2w_d3d12_linear_buffer_info` and `x2w_d3d12_linear_buffer_totals` (`src/startup_stubs/linear_buffer_report.h`). Fixture-only switches: `X2W_TEST_EXISTING_HEAPS_UNSUPPORTED=1` makes the feature query report no support, as an adapter with `EXISTING_HEAPS = 0` does. `X2W_TEST_LINEAR_REFRESH_FAIL=` makes staging requests 2, 2 + n, 2 + 2n and so on fail with `E_OUTOFMEMORY`. `X2W_TEST_LINEAR_STAGING_LIMIT=` shrinks the staging pool from 256 buffers to `n`. `x2w_d3d12_linear_buffer_internal_queue` hands fixtures the internal queue, so they can hold refreshes in flight behind a fence of their own. `x2w_d3d12_linear_buffer_test_pause` parks the next refresh at one point (before staging, after staging, or after its copy executed and before it is published), so a fixture can order two threads' refreshes or release a buffer while one is parked. Point 3 instead parks the next title `Wait` (queue slot 17) after the desktop `Wait` and before it is recorded, while it holds that queue's order lock. `X2W_TEST_LINEAR_BUFFER_MUTANT=` removes one guarantee for a negative control. Results and commands: [Production slice: slot 133 linear buffers](experiments.md#production-slice-slot-133-linear-buffers-1914). -Not covered: table SRVs of copied buffers in translated draws still take `frame_copy` at record, and root SRVs (`list_root_srv`) still take `upload_from_title` at bind (with `X2W_D3D12_ROOT_SRV_AT_DRAW=1`, at a draw that reads the binding). Neither reads the slot 133 backing. Shared buffers and their heaps live for the process, like every wrapper, so a private range under one cannot be released. +Not covered: table SRVs of copied buffers in translated draws still take `frame_copy` at record, and root SRVs (`list_root_srv`) still take `upload_from_title` at bind (not with `X2W_D3D12_ROOT_SRV_AT_DRAW=1`). Neither reads the slot 133 backing. Shared buffers and their heaps live for the process, like every wrapper, so a private range under one cannot be released. ## Buffer memory SRVs (device slot 70) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 906d583428..d4ed161c58 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2767,7 +2767,6 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI // The translated root invalidated the desktop arguments of the tracked guest root. if (pass.graphics_root) list->SetGraphicsRootSignature(pass.graphics_root); pass.srv_bound = 0; - pass.srv_pending = 0; drawn = true; } else if (indexed) { u64 offset = 0; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 00ba9a84dc..56bfc01f43 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2821,9 +2821,6 @@ struct Pass { bool targets_bound = false; bool topology_ok = false; unsigned srv_bound = 0; // bit per root parameter with a desktop address bound - // X2W_D3D12_ROOT_SRV_AT_DRAW=1: slot 44 root SRVs whose upload copy waits for a draw that reads them. - unsigned srv_pending = 0; - u64 srv_pending_address[32]{}; const std::vector* root = nullptr; // Heap-indexing flags of the title root signature last set by slot 34. While nonzero, that root signature is // deferred: it is not set on the desktop list, root SRVs are not forwarded, and real draws go through decide_real_root. @@ -3332,16 +3329,14 @@ bool split_indirect_dispatch_enabled() } // The guest method has independent resource and byte-offset arguments. -// X2W_D3D12_ROOT_SRV_AT_DRAW=1 (opt-in): slot 44 copies a root SRV without an upload mapping only for a draw that reads -// the desktop binding (bind_pending_root_srvs); the bytes are then read at that draw instead of at slot 44. +// X2W_D3D12_ROOT_SRV_AT_DRAW=1 (opt-in): slot 44 makes no desktop copy of a root SRV without an upload mapping; the +// translated draws read it from its title address at the draw (list_root_srv). bool root_srv_at_draw() { static const bool on = GetEnvironmentVariableW(L"X2W_D3D12_ROOT_SRV_AT_DRAW", nullptr, 0) != 0; return on; } -LONG root_srv_copies_deferred, root_srv_copies_at_draw, root_srv_copies_made; -void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pass, UINT parameter, u64 address); -void bind_pending_root_srvs(Wrapper* self, UINT skip); +LONG root_srv_copies_skipped, root_srv_copies_made; void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count, void* argument, u64 offset, void* count_buffer, u64 count_offset) @@ -3612,8 +3607,6 @@ void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count, if (graphics) { list->SetPipelineState(static_cast(pass.pipeline->own().real)); list->SetGraphicsRootSignature(pass.graphics_root); - if (pass.srv_pending) - bind_pending_root_srvs(self, pass.indirect_graphics.cached_mesh ? pass.indirect_graphics.cached_srv_root : UINT_MAX); if (pass.indirect_graphics.cached_mesh) { const UINT srv = pass.indirect_graphics.cached_srv_root; if (srv != UINT_MAX) @@ -3703,7 +3696,6 @@ void __stdcall list_graphics_root(Wrapper* self, void* root) } pass.root = params; pass.srv_bound = 0; - pass.srv_pending = 0; const bool real = w && w->own().kind == root_signature_kind && w->own().real; pass.root_heap_flags = real ? root_heap_flags(w) : 0; pass.deferred_root = pass.root_heap_flags ? static_cast(w->own().real) : nullptr; @@ -4151,7 +4143,6 @@ void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) auto* list = recording(self); Pass& pass = pass_of(self); if (parameter < 32) pass.srv_bound &= ~(1u << parameter); - if (parameter < 32) pass.srv_pending &= ~(1u << parameter); if (parameter < 16) pass.root_address[parameter] = address; // Under a deferred heap-indexed root the desktop list holds another root signature; only the title address is kept. if (!list || !pass.root || pass.root_heap_flags || parameter >= pass.root->size() || parameter >= 32 || @@ -4165,19 +4156,17 @@ void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) pass.srv_bound |= 1u << parameter; return; } + // Without an upload mapping a 64 KiB window of title memory is copied into the upload ring for the desktop binding. + // No draw reads that binding: translated draws set their own root signature and read the SRV from root_address at + // the draw; every desktop graphics pipeline is an indirect one (build_stream_pipeline sets its root), so list_draw + // forwards only with a compute pipeline; and an indirect graphics draw binds a pipeline over the list's root, an + // empty one or, for a cached mesh pipeline, the title's root, whose only SRV it binds from its lease. With + // X2W_D3D12_ROOT_SRV_AT_DRAW=1 the copy is skipped and the parameter stays unbound (srv_bound clear). A desktop + // graphics pipeline that reads a root SRV would need the copy at its draw. if (root_srv_at_draw()) { - pass.srv_pending |= 1u << parameter; - pass.srv_pending_address[parameter] = address; - InterlockedIncrement(&root_srv_copies_deferred); + InterlockedIncrement(&root_srv_copies_skipped); return; } - bind_root_srv_copy(self, list, pass, parameter, address); -} - -// A graphics root SRV at a title address without an upload mapping: a 64 KiB window of title memory is copied into the -// upload ring and bound on the recording's desktop list. -void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pass, UINT parameter, u64 address) -{ const std::size_t bytes = readable_window(address, 0x10000); const D3D12_GPU_VIRTUAL_ADDRESS desktop = bytes ? upload_from_title(self, address, bytes) : 0; static LONG logged; @@ -4192,35 +4181,14 @@ void bind_root_srv_copy(Wrapper* self, ID3D12GraphicsCommandList* list, Pass& pa pass.srv_bound |= 1u << parameter; } -// Fixture export (startup_root_srv_draw): slot 44 root SRV copies since the process started. `deferred` and `at_draw` -// count only with X2W_D3D12_ROOT_SRV_AT_DRAW=1; `copied` counts every copy into the upload ring, either mode. -extern "C" __declspec(dllexport) void x2w_d3d12_root_srv_copies(LONG* deferred, LONG* at_draw, LONG* copied) +// Fixture export (startup_root_srv_draw): slot 44 root SRVs without an upload mapping since the process started. +// `skipped` counts only with X2W_D3D12_ROOT_SRV_AT_DRAW=1; `copied` counts every copy into the upload ring. +extern "C" __declspec(dllexport) void x2w_d3d12_root_srv_copies(LONG* skipped, LONG* copied) { - if (deferred) *deferred = root_srv_copies_deferred; - if (at_draw) *at_draw = root_srv_copies_at_draw; + if (skipped) *skipped = root_srv_copies_skipped; if (copied) *copied = root_srv_copies_made; } -// X2W_D3D12_ROOT_SRV_AT_DRAW=1: the bind_root_srv_copy of slot 44 waits for a draw that reads the desktop root SRV, -// a real-pipeline draw (list_draw) or an indirect graphics draw on the title's root signature. Translated draws set -// their own root signature and copy the SRV from pass.root_address themselves, so they never read it. -void bind_pending_root_srvs(Wrapper* self, UINT skip) -{ - auto* list = recording(self); - Pass& pass = pass_of(self); - unsigned pending = pass.srv_pending; - pass.srv_pending = 0; - if (!list) return; - while (pending) { - unsigned long parameter = 0; - _BitScanForward(¶meter, pending); - pending &= pending - 1; - if (parameter == skip) continue; - InterlockedIncrement(&root_srv_copies_at_draw); - bind_root_srv_copy(self, list, pass, parameter, pass.srv_pending_address[parameter]); - } -} - // List slot 42 (desktop 38 + 4), SetGraphicsRootConstantBufferView(parameter, title GPU address). // A translated shader reads a root CBV through the same pointer source as a root SRV (root_address). Nothing // is forwarded to the desktop list and srv_bound is left alone: bit 0 means a vertex-fetch SRV is bound. @@ -4298,7 +4266,6 @@ void __stdcall list_draw(Wrapper* self, UINT vertices, UINT instances, UINT firs // list, with an end-to-end fixture, makes the deferred bind here. list->SetGraphicsRootSignature(pass.deferred_root); } - if (list && pass.pipeline_real && pass.srv_pending) bind_pending_root_srvs(self, UINT_MAX); // The translated vertex shaders read their fetch table through root parameter 0. if (list && pass.pipeline_real && pass.targets_bound && pass.topology_ok && pass.topology != 0x11 && (pass.srv_bound & 1)) { const auto red_samples = red_producer_draw_before(self, list, "native-draw", 0); @@ -7119,8 +7086,8 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa ReleaseSRWLockExclusive(&draw_lock); } if (render_mode() && frame % 300 == 0 && root_srv_at_draw()) - log("x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): deferred %ld, made at a draw %ld", - root_srv_copies_deferred, root_srv_copies_at_draw); + log("x2w-render root SRV copies (X2W_D3D12_ROOT_SRV_AT_DRAW): skipped %ld, copied %ld", root_srv_copies_skipped, + root_srv_copies_made); if (render_mode() && frame % 300 == 0) log("x2w-render totals: resets %ld (failed %ld), closes %ld, clears %ld, target binds %ld (unresolved %ld), executes %ld, draws %ld (skipped %ld), " "translated draws %ld (not drawn %ld), pipelines translated %ld / missing %ld, null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld), " diff --git a/tests/cmake/startup_root_srv_at_draw.cmake b/tests/cmake/startup_root_srv_at_draw.cmake index a35d6bf01c..9bf9d83f15 100644 --- a/tests/cmake/startup_root_srv_at_draw.cmake +++ b/tests/cmake/startup_root_srv_at_draw.cmake @@ -1,4 +1,4 @@ -# Graphics root SRV copies made only for a draw that reads the desktop binding (opt-in X2W_D3D12_ROOT_SRV_AT_DRAW=1). +# Graphics root SRV: no desktop copy that no draw reads (opt-in X2W_D3D12_ROOT_SRV_AT_DRAW=1). # A translated draw reads a slot 44 root SRV whose title bytes change before the draw; it runs with the switch unset # (slot 44 copies) and set (no copy). target_sources(startup_stream_consumer PRIVATE tests/startup_root_srv_draw.cpp) diff --git a/tests/startup_root_srv_draw.cpp b/tests/startup_root_srv_draw.cpp index 7b443594c3..e3d11e08e1 100644 --- a/tests/startup_root_srv_draw.cpp +++ b/tests/startup_root_srv_draw.cpp @@ -6,14 +6,14 @@ #include "startup_stream_setup.h" -extern "C" __declspec(dllimport) void x2w_d3d12_root_srv_copies(LONG* deferred, LONG* at_draw, LONG* copied); +extern "C" __declspec(dllimport) void x2w_d3d12_root_srv_copies(LONG* skipped, LONG* copied); // A translated draw that reads a graphics root SRV set through list slot 44, at a title address without an upload // mapping (X2W_TEST_ROOT_SRV_DRAW=write-after). Slot 44 sees the buffer holding red; the title then writes green and // draws. The translated draw reads the SRV from its title address at the draw, so it draws green whether or not slot 44 -// also copied the bytes for the desktop binding (X2W_D3D12_ROOT_SRV_AT_DRAW). After the draw is recorded it prints the -// stand-in's copy counts (x2w_d3d12_root_srv_copies). The stream root's parameter 0 is a root SRV in this mode -// (startup_stream_setup.cpp). +// also copied the bytes for the desktop binding (skipped with X2W_D3D12_ROOT_SRV_AT_DRAW=1). After the draw is recorded +// it prints the stand-in's copy counts (x2w_d3d12_root_srv_copies). The stream root's parameter 0 is a root SRV in this +// mode (startup_stream_setup.cpp). int draw_root_srv(void* list) { char mode[16]{}; @@ -31,8 +31,8 @@ int draw_root_srv(void* list) std::memcpy(colour, green, sizeof(green)); invoke(list, 12, 3, 1); // DrawInstanced(3, 1) // The stand-in's slot 44 copy counts after the draw was recorded, for the check script. - LONG deferred = -1, at_draw = -1, copied = -1; - x2w_d3d12_root_srv_copies(&deferred, &at_draw, &copied); - std::printf("root SRV copies: deferred %ld, made at a draw %ld, copied %ld\n", deferred, at_draw, copied); + LONG skipped = -1, copied = -1; + x2w_d3d12_root_srv_copies(&skipped, &copied); + std::printf("root SRV copies: skipped %ld, copied %ld\n", skipped, copied); return 0; // The buffer stays allocated until the process ends. } diff --git a/tests/startup_root_srv_draw_check.py b/tests/startup_root_srv_draw_check.py index a9308a47ac..e01f8fbde6 100644 --- a/tests/startup_root_srv_draw_check.py +++ b/tests/startup_root_srv_draw_check.py @@ -4,7 +4,7 @@ writes green, then draws, and the stand-in's copy counts are printed. Run with X2W_D3D12_ROOT_SRV_AT_DRAW unset and set: - both: the translated draw reads the SRV from its title address at the draw and draws green; - unset: slot 44 also copies a window into the upload ring for the desktop binding (copied 1); - - set: no draw reads that binding, so the copy is deferred and never made (deferred 1, made at a draw 0, copied 0). + - set: no draw reads that binding, so slot 44 skips the copy (skipped 1, copied 0). The left half of the 8x4 target is drawn, the right half keeps the clear colour. """ import os @@ -25,7 +25,7 @@ CLEAR = bytes((0, 0, 255, 255)) GREEN = bytes((0, 255, 0, 255)) SWITCH = "X2W_D3D12_ROOT_SRV_AT_DRAW" -COUNTS = re.compile(r"root SRV copies: deferred (-?\d+), made at a draw (-?\d+), copied (-?\d+)") +COUNTS = re.compile(r"root SRV copies: skipped (-?\d+), copied (-?\d+)") def run(directory, consumer, backend, at_draw): @@ -50,8 +50,8 @@ def run(directory, consumer, backend, at_draw): def check(directory, consumer, backend): expected_pixels = (GREEN * 4 + CLEAR * 4) * 4 - # (deferred, made at a draw, copied) - expected_counts = {False: (0, 0, 1), True: (1, 0, 0)} + # (skipped, copied) + expected_counts = {False: (0, 1), True: (1, 0)} for at_draw in (False, True): pixels, counts = run(directory, consumer, backend, at_draw) if pixels is None: @@ -60,10 +60,10 @@ def check(directory, consumer, backend): if pixels != expected_pixels: raise AssertionError(f"{label}: actual {pixels.hex()} expected {expected_pixels.hex()}") if counts != expected_counts[at_draw]: - raise AssertionError(f"{label}: copies (deferred, made at a draw, copied) {counts}, " + raise AssertionError(f"{label}: copies (skipped, copied) {counts}, " f"expected {expected_counts[at_draw]}") print(f"{backend} {label}: 32 pixels exact (green, read at the draw); " - f"deferred {counts[0]}, made at a draw {counts[1]}, copied {counts[2]}") + f"skipped {counts[0]}, copied {counts[1]}") return 0 From d4d76b881d8ab72782f61a67479de518f2c55f33 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 10:27:18 +0100 Subject: [PATCH 6/6] docs: retest of the slot 44 root SRV skip on this revision Two startup pairs (second reversed) on 41b3c771 with ring and alias set: 6.3 / 6.5 fps unset against 13.9 / 13.4 set, no buffer nulls or ring refusals, the same commit peak. Co-Authored-By: Claude Opus 5.5 --- docs/experiments/startup-root-srv-at-draw.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/experiments/startup-root-srv-at-draw.md b/docs/experiments/startup-root-srv-at-draw.md index 5b8f743064..51f7f6c552 100644 --- a/docs/experiments/startup-root-srv-at-draw.md +++ b/docs/experiments/startup-root-srv-at-draw.md @@ -46,7 +46,7 @@ Not covered: a desktop draw that reads the binding, because none can be built (s Local diagnostic runs, Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA driver 32.0.16.1047, i7-4790K, 16 GB, release build on `main` `98e4a2ee`, warm shader cache (revision 29), scripted input, 250 s. One build; only the switches -differ. Aggregates only; logs stay local. The tables were measured with the first revision of this change +differ. Aggregates only; logs stay local. The first two tables were measured with the first revision of this change (`bedfcafc`), which kept the address pending for a desktop draw instead of skipping the copy; no run made a copy at a draw, so slot 44 did the same work as now. @@ -79,4 +79,22 @@ reverse order: under `draw_lock`. - Every run read `deferred N, made at a draw 0`: no draw in these runs read the desktop binding. +Retest of this revision (merged with `main` `3b6c38a8`), same machine, cache and input, ring and alias set in every +run, two pairs, the second in reverse order: + +| | switch unset | switch set | +|---|---|---| +| mean fps 150–240 s, pair 1 / pair 2 | 6.3 / 6.5 | 13.9 / 13.4 | +| frames presented, pair 1 / pair 2 | 3,839 / 3,706 | 4,798 / 4,960 | +| executes, pair 1 / pair 2 | 24,065 / 33,220 | 69,730 / 54,789 | +| slot 44 time 150–240 s (threads; µs per call) | 1.17 / 1.11; 52 / 48 | 0.29 / 0.29; 5 / 6 | +| copies skipped / copied (render totals) | n/a | 6.9 / 4.9 million; 0 | +| null bindings `token` per 1,000 translated draws | 23.4 / 29.8 | 24.2 / 33.0 | +| null bindings `wrapper`, pair 1 / pair 2 | 0 / 40 | 107 / 101 | +| null bindings `buffer`, ring refusals, device removal | 0, none, none | 0, none, none | +| process commit peak | 18.7 / 18.5 GiB | 18.7 / 18.7 GiB | + +- `wrapper` null bindings also appear in an unset run (40). Per 1,000 translated draws they are 0 / 0.26 unset and + 0.37 / 0.40 set: few, but not explained here. + Not established: desktop graphics pipelines that read a root SRV (none can be built today), and other adapters.