From 33d31412cef36b25c0a4ca1d43d1f853e34b487c Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:36:17 +0100 Subject: [PATCH 1/3] startup: opt-in release of upload ring chunks at a fenced submit X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 lets a closed command list stop holding its upload ring chunks once its submit is fenced, instead of holding them until Reset. The queue fence alone then keeps the chunks from reuse until the GPU has passed it. In this mode the ring grows in 16 MiB chunks up to 512 MiB. A later submit of the same list before its Reset fences its chunks again when their generation is unchanged; a chunk recycled in between is counted and logged. Unset, the ring behaves as before (doubling chunks up to 256 MiB, holds until Reset). - upload_reservation.h: ReleasedUploadChunks, release_upload_chunks_at_submit, resubmitted_upload_chunks; chunk arrays sized for 512 MiB. - d3d12_xs_device.cpp: switch, release in note_submits, resubmit check, cleanup at Reset and Release, ring sizing. - Tests: upload_reservation check_release_at_submit; new startup_address_console_gate_ring_keep{,_release}_{warp,hardware}; fixtures that describe the default ring clear the switch for their process. - Docs: switch row and ring paragraph in docs/startup-runtime.md, experiment note docs/experiments/startup-upload-ring-release-at-submit.md. Refs #2837, #2849. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 3 +- .../startup-upload-ring-release-at-submit.md | 72 +++++++++++++++++++ docs/startup-runtime.md | 12 ++++ src/startup_stubs/d3d12_xs_device.cpp | 49 +++++++++++-- src/startup_stubs/upload_reservation.h | 39 +++++++++- tests/startup_address_console_check.py | 41 ++++++++++- tests/startup_slot133_heap_cap_check.cpp | 3 + tests/startup_stream_draw.cpp | 39 ++++++++++ tests/upload_reservation_check.cpp | 46 ++++++++++++ 9 files changed, 296 insertions(+), 8 deletions(-) create mode 100644 docs/experiments/startup-upload-ring-release-at-submit.md diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d9886fdf..5bdc40f83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4833,7 +4833,8 @@ if(EXISTS "${INTAKE_PYTHON}") # queue has run them. The ring control fills them from the shared upload ring, which must refuse the uploads # past its 256 MiB cap while the queue is held. gate-ring-reuse: once those submits have completed, the ring # must accept a new upload again. - foreach(gate IN ITEMS gate gate-ring gate-ring-reuse) + # gate-ring-keep(-release): a closed list that is never Reset, without and with X2W_D3D12_RING_RELEASE_AT_SUBMIT. + foreach(gate IN ITEMS gate gate-ring gate-ring-reuse gate-ring-keep gate-ring-keep-release) string(REPLACE "-" "_" gate_name "${gate}") add_test(NAME startup_address_console_${gate_name}_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_console_check.py" diff --git a/docs/experiments/startup-upload-ring-release-at-submit.md b/docs/experiments/startup-upload-ring-release-at-submit.md new file mode 100644 index 000000000..83d7f5a4d --- /dev/null +++ b/docs/experiments/startup-upload-ring-release-at-submit.md @@ -0,0 +1,72 @@ +# Upload ring: release a closed list's chunks at its fenced submit (opt-in) + +Refs [#2837](https://github.com/R0Y00/Xbox2Windows/issues/2837), +[#2849](https://github.com/R0Y00/Xbox2Windows/pull/2849). + +Since #2823 a reservation in the shared upload ring keeps its bytes until its chunk is recycled, and #2849 +recycles a chunk only when no command-list recording holds it and every queue that used it has passed its +fence. A closed list keeps its hold until Reset, Release or a failed Close, because D3D12 lets it be submitted +again. `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1` moves that release to the list's fenced submit and sizes the ring +in 16 MiB chunks up to 512 MiB. + +## What the switch changes + +- After a submit whose queue fence was signalled, each submitted list stops holding its chunks + (`release_upload_chunks_at_submit`). The queue fence still keeps those chunks from reuse until the GPU has + passed it. Without a signalled fence nothing changes: the chunk stays busy, as without the switch. +- The list records the generation of each chunk it released. If the same list is submitted again before its + Reset, chunks with an unchanged generation are fenced again by that submit + (`resubmitted_upload_chunks`). A chunk recycled in between is counted and logged once per occurrence + (first eight): that submit reads replaced bytes. The check runs after the lists were executed, under the same + locks, so it reports the case; it does not refuse the execute. +- The ring grows in 16 MiB chunks up to 512 MiB, so one list that is still recording or waiting to be submitted + pins 16 MiB instead of a chunk of up to 128 MiB. +- Unset, the ring behaves exactly as before: doubling chunks up to 256 MiB, holds until Reset. + +## Fixtures + +```powershell +cmake --build --preset release --target startup_stream_consumer upload_reservation_check d3d12_xs_shim +ctest --preset release --no-tests=error -j 1 -R '^(upload_reservation|startup_address_console_gate_ring_keep(_release)?_(warp|hardware))$' -V +``` + +- `upload_reservation`: `check_release_at_submit` releases two chunks after a fenced submit, keeps them from + reuse until the fence passes, reuses one, then finds the other intact and the reused one reported on a + resubmit, and fences the intact one again. Mutants: keeping the holds fails "release: holds dropped"; + ignoring generations fails "a resubmit finds chunk 1 intact and chunk 0 reused". +- `startup_address_console_gate_ring_keep_*` (`X2W_TEST_CONSOLE_KEEP`): a second list A uploads 32 by-address + payloads of 16 MiB each from the ring (512 MiB), is submitted, completes and is never Reset. The fixture's own + list then draws payload 32 over the right half of the target, the target is read back, and A is submitted + again. + - Without the switch, A keeps every chunk: the ring refuses past 256 MiB and the right half reads the null + view. + - `..._keep_release_*` sets the switch: the right half holds payload 32 from a chunk A released, and A's second + submit is reported as reading a reused chunk. + - Mutants: disabling the release leaves the right half on the null view; silencing the report fails + "list A submitted again after B reused its chunk was not reported". +- Fixtures that describe the default ring clear the switch for their own process, so they keep testing the + default mode when it is set in the environment: the #2849 `startup_address_console_gate_ring{,_reuse}_*` + (a refusal at the 256 MiB cap) and `startup_slot133_heap_cap_capped_*` (doubling growth to 48 MiB). + +## 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, warm shader cache, `run.ps1 -Render` without `-Translate`, scripted input, 250 s. +One build; only the switch differs. Aggregates only; logs stay local. + +| | switch unset | switch set | +|---|---|---| +| null bindings with cause `buffer` | 33,632 | 0 | +| `upload ring reservation refused` logged | yes | no | +| resubmits reported after a reuse | n/a | 0 | +| process commit peak | 16.7 GiB | 18.1 GiB | +| visible | single full black frames in the post-load animation | none seen | + +- Without the switch, a census at each refusal showed every chunk held by closed lists not yet Reset, while + every queue had passed the chunk's fence. Most refused requests were 64 KiB: the window + `upload_from_title` copies for a graphics root SRV without an existing upload mapping. +- The title never submitted a closed list again without a Reset: 0 of more than 500,000 submits over five runs. + The switch relies on that; a title that does resubmit gets the logged report described above. +- The larger ring costs about 1 GiB of process commit at peak in this run. + +Not established: other titles' resubmit behaviour, and whether 512 MiB is enough on other workloads. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 2563133bb..6f5edacff 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1472,6 +1472,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_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). A later submit of the same list before its Reset fences its unchanged chunks again and logs any chunk reused in between (`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 | | `X2W_D3D12_RELEASE_RETIRED=1` | opt-in, effective with `X2W_D3D12_RENDER=1` only (the list handlers that record holds exist only there): frees the desktop object of a released committed 2D texture of one narrow class once no recorded list, submission or release-time fence refers to it ([details](#d3d12-resource-budget)). Unset, nothing changes | @@ -3368,6 +3369,17 @@ signal or a removed device keeps the chunk busy. When no chunk qualifies, the reservation is refused; live storage is never reset. Recycling bumps the chunk's generation, and a cached frame copy from an older generation is not reused. +With the opt-in `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1`, a closed list stops +holding its chunks as soon as its submit is fenced: the queue fence alone then +keeps the chunks from reuse until the GPU has passed it. The ring grows in +16 MiB chunks up to 512 MiB in this mode. If the same list is submitted again +before its Reset, the submit fences its chunks again when their generation is +unchanged. A chunk that was recycled in between is counted and logged (`closed +list ... submitted again after its released chunks ... were reused`); that +submit reads replaced bytes, so the mode suits titles that Reset a list before +submitting it again. `startup_address_console_gate_ring_keep_*` keeps one list +closed and never Reset while a second list needs ring space, without and with +the switch. See [the experiment note](experiments/startup-upload-ring-release-at-submit.md). `upload_reservation` exercises the shared cursor and returned-pointer contract without D3D12; on non-Windows hosts run `c++ -std=c++17 -Isrc tests/upload_reservation_check.cpp -o out/upload_reservation_check && out/upload_reservation_check`. diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 56d453557..16a89a371 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1459,6 +1459,9 @@ std::unordered_map title_allocators; std::unordered_map list_recordings; // list -> allocator holding its recording std::unordered_map> queue_submits; std::vector upload_chunks; // allocator_lock also protects recording holds and queue fence values +// X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 (allocator_lock): chunks each closed list released at a fenced submit, until its Reset. +std::unordered_map released_upload_chunks; +LONG upload_resubmits_after_reuse; // closed lists submitted again after a released chunk was reused LONG allocator_swaps, allocator_creates; @@ -1466,6 +1469,7 @@ void release_upload_recording(Wrapper* list) { AcquireSRWLockExclusive(&allocator_lock); discard_upload_chunks(list->own().upload_chunks, upload_chunks.data()); + released_upload_chunks.erase(list); ReleaseSRWLockExclusive(&allocator_lock); } @@ -1657,6 +1661,15 @@ bool submit_fence_fails() return on; } +// X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 (opt-in): a closed list stops holding its upload ring chunks once its submit is +// fenced, so the queue fence alone guards them; the ring then grows in 16 MiB chunks up to 512 MiB. A later submit +// of the same list before its Reset fences its intact chunks again; one whose chunk was reused meanwhile is logged. +bool ring_release_at_submit() +{ + static const bool on = GetEnvironmentVariableW(L"X2W_D3D12_RING_RELEASE_AT_SUBMIT", nullptr, 0) != 0; + return on; +} + // draw_lock and allocator_lock held, after the lists were executed on queue. Each list's allocator holds a fence value // the queue must pass before the allocator can be reset. If that fence cannot be created or // signalled, the work is on the GPU all the same: the allocators are marked untracked instead. @@ -1673,10 +1686,34 @@ void note_submits(ID3D12CommandQueue* queue, const std::vector& lists) } } UploadChunkMask chunks = 0; - for (auto* list : lists) chunks |= list->own().upload_chunks; + for (auto* list : lists) { + chunks |= list->own().upload_chunks; + const auto released = released_upload_chunks.find(list); + if (released == released_upload_chunks.end()) continue; + UploadChunkMask reused = 0; + chunks |= resubmitted_upload_chunks(released->second, upload_chunks.data(), reused); + if (reused) { + InterlockedIncrement(&upload_resubmits_after_reuse); + static LONG logged; + if (first_calls(logged, 8)) + log("x2w-draw upload ring: closed list %p submitted again after its released chunks 0x%x were reused; " + "it reads replaced upload bytes", static_cast(list), static_cast(reused)); + } + } const bool signaled = fence && SUCCEEDED(queue->Signal(fence, value + 1)); if (signaled) ++value; submit.submitted(chunks, signaled); + if (signaled && ring_release_at_submit()) { + for (Wrapper* list : lists) { + if (!list->own().upload_chunks) continue; + try { + release_upload_chunks_at_submit(list->own().upload_chunks, upload_chunks.data(), + released_upload_chunks[list]); + } catch (const std::bad_alloc&) { + // Keep the holds: the list then pins its chunks until its Reset, as without the switch. + } + } + } if (signaled) { for (Wrapper* list : lists) { const auto found = list_recordings.find(list); @@ -1811,6 +1848,7 @@ HRESULT __stdcall list_reset(Wrapper* self, void* allocator) on = title.current; ++on->lists; discard_upload_chunks(state.upload_chunks, upload_chunks.data()); + released_upload_chunks.erase(self); state.list_uploads = nullptr; state.list_scratch = nullptr; state.list_ready = false; @@ -2435,7 +2473,8 @@ ID3D12PipelineState* build_stream_pipeline(const D3D12_PIPELINE_STATE_STREAM_DES // Transient guest data is copied into mapped upload chunks. Chunks grow from // 16 MiB up to 256 MiB in total and stay alive for the process. A full chunk can be reused once -// no recording can submit its bytes and every queue that used it has finished. +// no recording can submit its bytes and every queue that used it has finished. With +// X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 every chunk is 16 MiB, up to 512 MiB, and a fenced submit releases its lists. UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes) { @@ -2460,9 +2499,11 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes) if (reservation.mapped) return finish(active); } - const u64 remaining = total_bytes < max_upload_ring_bytes ? max_upload_ring_bytes - total_bytes : 0; + const bool release_at_submit = ring_release_at_submit(); + const u64 ring_bytes = release_at_submit ? max_released_upload_ring_bytes : max_upload_ring_bytes; + const u64 remaining = total_bytes < ring_bytes ? ring_bytes - total_bytes : 0; if (rounded <= remaining) { - u64 chunk_bytes = chunks.empty() ? initial_upload_chunk_bytes : chunks.back().capacity * 2; + u64 chunk_bytes = chunks.empty() || release_at_submit ? initial_upload_chunk_bytes : chunks.back().capacity * 2; if (chunk_bytes < rounded) chunk_bytes = rounded; if (chunk_bytes > remaining) chunk_bytes = remaining; auto* device = the_device ? device_of(the_device) : nullptr; diff --git a/src/startup_stubs/upload_reservation.h b/src/startup_stubs/upload_reservation.h index 57bf321a5..eec33a9d3 100644 --- a/src/startup_stubs/upload_reservation.h +++ b/src/startup_stubs/upload_reservation.h @@ -7,8 +7,10 @@ struct ID3D12Resource; inline constexpr std::uint64_t initial_upload_chunk_bytes = 16ull << 20; inline constexpr std::uint64_t max_upload_ring_bytes = 256ull << 20; +// X2W_D3D12_RING_RELEASE_AT_SUBMIT=1: chunks of the initial size, up to this total. +inline constexpr std::uint64_t max_released_upload_ring_bytes = 512ull << 20; // Each allocation except possibly the final remainder is at least the initial size. -inline constexpr unsigned max_upload_chunks = max_upload_ring_bytes / initial_upload_chunk_bytes; +inline constexpr unsigned max_upload_chunks = max_released_upload_ring_bytes / initial_upload_chunk_bytes; using UploadChunkMask = std::uint32_t; static_assert(max_upload_chunks <= 32); @@ -60,6 +62,41 @@ inline void discard_upload_chunks(UploadChunkMask& recording, UploadRingChunk* c if (recording & 1) --chunks[i].recordings; } +// X2W_D3D12_RING_RELEASE_AT_SUBMIT=1: the chunks a closed list stopped holding when its submit was fenced, and +// each chunk's generation at that moment. The queue fence keeps those chunks from reuse until the GPU passes it. +struct ReleasedUploadChunks { + UploadChunkMask chunks = 0; + std::array generation{}; +}; + +// Caller holds the lock, after the queue recorded a signalled fence for the recording's chunks. +inline void release_upload_chunks_at_submit(UploadChunkMask& recording, UploadRingChunk* chunks, + ReleasedUploadChunks& released) +{ + for (unsigned i = 0; recording; ++i, recording >>= 1) { + if (!(recording & 1)) continue; + released.chunks |= UploadChunkMask{1} << i; + released.generation[i] = chunks[i].generation; + --chunks[i].recordings; + } +} + +// Caller holds the lock. A closed list submitted again before its Reset still reads the bytes it reserved: returns +// the released chunks that were not reused since, which the new submit must fence again, and sets `recycled` to +// those that were. +inline UploadChunkMask resubmitted_upload_chunks(const ReleasedUploadChunks& released, const UploadRingChunk* chunks, + UploadChunkMask& recycled) +{ + UploadChunkMask intact = 0; + recycled = 0; + for (unsigned i = 0; i < max_upload_chunks; ++i) { + const auto bit = UploadChunkMask{1} << i; + if (!(released.chunks & bit)) continue; + (chunks[i].generation == released.generation[i] ? intact : recycled) |= bit; + } + return intact; +} + struct UploadReservation { unsigned char* mapped = nullptr; std::uint64_t offset = 0; diff --git a/tests/startup_address_console_check.py b/tests/startup_address_console_check.py index 41ba62e26..062b11c09 100644 --- a/tests/startup_address_console_check.py +++ b/tests/startup_address_console_check.py @@ -9,6 +9,13 @@ refuses the uploads past its cap, the refused draws read the null view and the left half keeps payload 0 gate-ring-reuse after completion, a new ring upload draws payload 18 over the right half + gate-ring-keep list A uploads 32 payloads (512 MiB) from the ring, is submitted, completes + and is never Reset; list B then draws payload 32 over the right half. Without + X2W_D3D12_RING_RELEASE_AT_SUBMIT, A keeps its chunks: the ring refuses past 256 MiB + and the right half reads the null view + gate-ring-keep-release the same with X2W_D3D12_RING_RELEASE_AT_SUBMIT=1: A releases its + chunks at its fenced submit, so B draws payload 32; A submitted again afterwards + is reported as reading reused chunks The backing bytes are built here, independently of src/startup_stubs/gfx10_layout.h, from the address equations of AMD addrlib's GFX10 pattern tables, written out below. Reference: addrlib as Mesa carries it @@ -211,6 +218,7 @@ def check(directory, consumer, backend, case, extra=None, held=None, late=False) GATE_PAYLOADS = 18 +KEEP_PAYLOADS = 32 # tests/startup_stream_draw.cpp, X2W_TEST_CONSOLE_KEEP def gate_colour(payload): @@ -240,6 +248,16 @@ def check_gate(directory, consumer, backend, mode): modes = {"X2W_TEST_CONSOLE_GATE": "1", "X2W_TRAP_LOG": str(trap_log)} ring = mode != "gate" reuse = mode == "gate-ring-reuse" + keep = mode in ("gate-ring-keep", "gate-ring-keep-release") + release = mode == "gate-ring-keep-release" + if keep: + modes["X2W_TEST_CONSOLE_KEEP"] = "1" + else: + os.environ.pop("X2W_TEST_CONSOLE_KEEP", None) + if release: + modes["X2W_D3D12_RING_RELEASE_AT_SUBMIT"] = "1" + else: + os.environ.pop("X2W_D3D12_RING_RELEASE_AT_SUBMIT", None) if reuse: modes["X2W_TEST_ADDRESS_UPLOAD_RING_REUSE"] = "1" else: @@ -258,6 +276,8 @@ def check_gate(directory, consumer, backend, mode): last = bytes(4) if ring else gate_colour(GATE_PAYLOADS - 1) if reuse: last = gate_colour(GATE_PAYLOADS) + if release: + last = gate_colour(KEEP_PAYLOADS) wrong = {"left": 0, "right": 0} seen = set() for y in range(TARGET_H): @@ -270,6 +290,21 @@ def check_gate(directory, consumer, backend, mode): log = trap_log.read_text(errors="replace") released = f"x2w-draw by-address uploads: {GATE_PAYLOADS} submits completed and released" half = TARGET_W * TARGET_H // 2 + if keep: + if wrong["left"] or wrong["right"]: + ring_lines = "\n".join(line for line in log.splitlines() if "upload ring" in line) + raise AssertionError(f"{mode}: {wrong} wrong pixels, found {sorted(seen)[:4]}; expected {first.hex()} left, " + f"{last.hex()} right\n{ring_lines}") + refused = "x2w-draw upload ring reservation refused: no unused capacity" in log + reported = "submitted again after its released chunks" in log + if release and not reported: + raise AssertionError(f"{mode}: list A submitted again after B reused its chunk was not reported") + if not release and (not refused or reported): + raise AssertionError(f"{mode}: expected the 256 MiB refusal and no release report (refused={refused}, reported={reported})") + print(f"{backend} {mode}: {half} left pixels hold payload 0 and {half} right pixels hold {last.hex()}; " + + ("A's chunks were released at its fenced submit and its later resubmit was reported" if release + else "the never-Reset list kept its chunks and the ring refused past its cap")) + return 0 if ring: if wrong["left"] or wrong["right"]: raise AssertionError(f"{mode}: {wrong} wrong pixels, found {sorted(seen)[:4]}; expected {first.hex()} left, {last.hex()} right") @@ -290,9 +325,11 @@ def check_gate(directory, consumer, backend, mode): return 0 +GATES = ("gate", "gate-ring", "gate-ring-reuse", "gate-ring-keep", "gate-ring-keep-release") + if __name__ == "__main__": - if len(sys.argv) not in (3, 4) or (len(sys.argv) == 4 and sys.argv[3] not in ("gate", "gate-ring", "gate-ring-reuse")): - usage = "usage: startup_address_console_check.py [gate | gate-ring | gate-ring-reuse]\n" + if len(sys.argv) not in (3, 4) or (len(sys.argv) == 4 and sys.argv[3] not in GATES): + usage = "usage: startup_address_console_check.py [gate | gate-ring | gate-ring-reuse | gate-ring-keep | gate-ring-keep-release]\n" sys.exit(usage + "\n\n".join(__doc__.split("\n\n")[:2])) if len(sys.argv) == 4: with tempfile.TemporaryDirectory() as temporary: diff --git a/tests/startup_slot133_heap_cap_check.cpp b/tests/startup_slot133_heap_cap_check.cpp index dce82648f..a45f05e87 100644 --- a/tests/startup_slot133_heap_cap_check.cpp +++ b/tests/startup_slot133_heap_cap_check.cpp @@ -764,6 +764,9 @@ int run_check(IUnknown* guest, ID3D12Device* device, ID3D12InfoQueue* debug, boo int main(int argc, char** argv) { + // The growth checks describe the default ring (doubling chunks up to 256 MiB). The stand-in reads + // X2W_D3D12_RING_RELEASE_AT_SUBMIT on first use in this process, so clear it before any call. + SetEnvironmentVariableW(L"X2W_D3D12_RING_RELEASE_AT_SUBMIT", nullptr); if (argc < 2 || argc > 3 || (std::strcmp(argv[1], "warp") && std::strcmp(argv[1], "hardware")) || (argc == 3 && std::strcmp(argv[2], "cap"))) { std::puts("usage: startup_slot133_heap_cap_check [cap]"); diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 26b37f639..f78a7309e 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -584,6 +584,45 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* invoke(list, 25, arg(pso)); return true; }; + // X2W_TEST_CONSOLE_KEEP (with the shared ring): a second list A records 32 payloads (32 x 16 MiB = 512 MiB), + // is submitted and waited for, and is never Reset. The fixture's own list then draws payload 32 over the right + // half. The target is read back; then A is submitted again, still closed and not Reset. + if (GetEnvironmentVariableW(L"X2W_TEST_CONSOLE_KEEP", nullptr, 0)) { + constexpr unsigned kept = 32; + void *allocator_a = nullptr, *list_a = nullptr; + const unsigned list_a_desc[] = {D3D12_COMMAND_LIST_TYPE_DIRECT, 2, 1, 0}; + if (FAILED(static_cast(invoke(guest, 9, D3D12_COMMAND_LIST_TYPE_DIRECT, + arg(&__uuidof(ID3D12CommandAllocator)), arg(&allocator_a)))) || + FAILED(static_cast(invoke(guest, 73, arg(list_a_desc), arg(allocator_a), 0, + arg(&__uuidof(ID3D12GraphicsCommandList)), arg(&list_a)))) || + FAILED(static_cast(invoke(allocator_a, 8))) || + FAILED(static_cast(invoke(list_a, 139, arg(allocator_a))))) return 23; // created closed + const D3D12_VIEWPORT whole{0, 0, static_cast(width), static_cast(height), 0, 1}; + invoke(list_a, 50, 1, arg(&rtv)); + invoke(list_a, 21, 1, arg(&whole)); + invoke(list_a, 20, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + invoke(list_a, 25, arg(pso)); + for (unsigned payload = 0; payload < kept; ++payload) + if (const int result = draw_gated_console_payload(guest, list_a, payload, payload == 0)) return result; + const auto submit_and_wait = [&](void* recorded, UINT64 value) { + invoke(queue, 10, 1, arg(&recorded)); + if (FAILED(static_cast(invoke(queue, 14, arg(fence), value)))) return false; + const auto deadline = GetTickCount64() + 60000; + while (invoke(fence, 8) < value && GetTickCount64() < deadline) Sleep(1); + return invoke(fence, 8) == value; + }; + invoke(list, 9); // the fixture's own recording so far (its clear) runs first + if (!submit_and_wait(list, 1)) return 25; + invoke(list_a, 9); + if (!submit_and_wait(list_a, 2)) return 25; + if (!next_recording()) return 35; + if (const int result = draw_gated_console_payload(guest, list, kept, false)) return result; + invoke(list, 9); + if (!submit_and_wait(list, 3)) return 25; + const int read = read_stream_target(desktop.Get(), frame, pixels); + if (!submit_and_wait(list_a, 4)) return 25; // A again: closed, never Reset + return read; + } // Retrying the same bytes after abandoning a recording still needs an upload. The shared-ring // control skips it so every upload behind the gate comes from the ring, whose 256 MiB cap is // then exhausted: the later reservations are refused and those draws bind the null view. diff --git a/tests/upload_reservation_check.cpp b/tests/upload_reservation_check.cpp index 9b84a3b4d..075e38f68 100644 --- a/tests/upload_reservation_check.cpp +++ b/tests/upload_reservation_check.cpp @@ -64,6 +64,51 @@ void check_ring_reuse() check(!reuse_upload_chunk(chunk, 0, bytes, queues).mapped, "untracked submit prevents reuse"); } +// X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 bookkeeping: a closed list stops holding its chunks once its submit is fenced, +// the fence alone then guards the chunks, and a later submit of the same list before its Reset fences the intact +// chunks again and reports the reused ones. +void check_release_at_submit() +{ + constexpr std::uint64_t bytes = 4 * kAlign; + unsigned char storage[2][bytes]; + std::array chunks{}; + UploadChunkMask recording = 0; + for (unsigned i = 0; i < chunks.size(); ++i) { + chunks[i] = {nullptr, storage[i], bytes}; + check(reserve_upload_chunk(chunks[i], bytes).mapped != nullptr, "release: fill chunk"); + chunks[i].hold(recording, i); + } + + TestFence fence; + std::map> queues{{0, {&fence, 1}}}; + queues[0].submitted(recording, true); + check(!reuse_upload_chunk(chunks[0], 0, bytes, queues).mapped, "release: a closed list holds until released"); + + ReleasedUploadChunks released; + release_upload_chunks_at_submit(recording, chunks.data(), released); + check(recording == 0 && chunks[0].recordings == 0 && chunks[1].recordings == 0, "release: holds dropped"); + check(released.chunks == 3 && released.generation[0] == 0 && released.generation[1] == 0, + "release: chunks and generations recorded"); + check(!reuse_upload_chunk(chunks[0], 0, bytes, queues).mapped, "release: the fence still guards the chunk"); + + fence.value = 1; + const auto reused = reuse_upload_chunk(chunks[0], 0, bytes, queues); + check(reused.mapped == storage[0] && reused.generation == 1, "release: chunk reused after the fence"); + + UploadChunkMask recycled = UINT32_MAX; + const auto intact = resubmitted_upload_chunks(released, chunks.data(), recycled); + check(intact == 2 && recycled == 1, "release: a resubmit finds chunk 1 intact and chunk 0 reused"); + + ++queues[0].value; + queues[0].submitted(intact, true); + check(!reuse_upload_chunk(chunks[1], 1, bytes, queues).mapped, "release: the resubmit fences the intact chunk"); + fence.value = 2; + check(reuse_upload_chunk(chunks[1], 1, bytes, queues).mapped != nullptr, + "release: the intact chunk is reused after the resubmit's fence"); + check(resubmitted_upload_chunks(released, chunks.data(), recycled) == 0 && recycled == 3, + "release: both chunks reported once reused"); +} + } // namespace int main() @@ -111,6 +156,7 @@ int main() "existing reservations survive exhaustion"); check_ring_reuse(); + check_release_at_submit(); std::puts("upload_reservation_check: all checks passed"); return 0; } From 3fcd36fc6876f4c67e4dd3e33f1ad1417b9f354f Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 06:20:25 +0100 Subject: [PATCH 2/3] startup: refuse a stale resubmit of a released list before the execute With X2W_D3D12_RING_RELEASE_AT_SUBMIT=1, a closed list submitted again after one of its released upload chunks was reused would read replaced bytes. The check ran in note_submits, after ExecuteCommandLists, so it could only log. queue_execute now checks each closed list before the execute, with draw_lock and allocator_lock held (released_chunks_recycled). Such a list is left out of that execute, counted and logged, and stays refused until its Reset; the other lists of the call still run. The locks stay held through note_submits, which therefore only re-fences intact chunks. gate-ring-keep-release now reads the target back after A's second submit and asserts the right half still holds payload 32 (A's last draw is payload 31). With the check disabled, A runs and its payload 0 draw shows the replacement bytes (payload 32's colour) on the left half. Docs: the experiment note and docs/startup-runtime.md describe the refusal; the switch row states the ~1 GiB commit cost. Refs #2948. Co-Authored-By: Claude Opus 5.5 --- .../startup-upload-ring-release-at-submit.md | 27 +++++++++------- docs/startup-runtime.md | 11 ++++--- src/startup_stubs/d3d12_xs_device.cpp | 32 +++++++++++++------ tests/startup_address_console_check.py | 14 ++++---- tests/startup_stream_draw.cpp | 9 +++--- 5 files changed, 57 insertions(+), 36 deletions(-) diff --git a/docs/experiments/startup-upload-ring-release-at-submit.md b/docs/experiments/startup-upload-ring-release-at-submit.md index 83d7f5a4d..842474f01 100644 --- a/docs/experiments/startup-upload-ring-release-at-submit.md +++ b/docs/experiments/startup-upload-ring-release-at-submit.md @@ -16,9 +16,11 @@ in 16 MiB chunks up to 512 MiB. passed it. Without a signalled fence nothing changes: the chunk stays busy, as without the switch. - The list records the generation of each chunk it released. If the same list is submitted again before its Reset, chunks with an unchanged generation are fenced again by that submit - (`resubmitted_upload_chunks`). A chunk recycled in between is counted and logged once per occurrence - (first eight): that submit reads replaced bytes. The check runs after the lists were executed, under the same - locks, so it reports the case; it does not refuse the execute. + (`resubmitted_upload_chunks`). If a chunk was recycled in between, the list would read replaced bytes: the + queue leaves it out of `ExecuteCommandLists` (`released_chunks_recycled`, checked with `draw_lock` and + `allocator_lock` held, the same locks the submit bookkeeping holds), counts it and logs the first eight + (`... were reused; refused, it is not executed`). The other lists of that call still run, as a list that is not + closed is left out today. It stays refused until its Reset, which records it afresh. - The ring grows in 16 MiB chunks up to 512 MiB, so one list that is still recording or waiting to be submitted pins 16 MiB instead of a chunk of up to 128 MiB. - Unset, the ring behaves exactly as before: doubling chunks up to 256 MiB, holds until Reset. @@ -31,19 +33,20 @@ ctest --preset release --no-tests=error -j 1 -R '^(upload_reservation|startup_ad ``` - `upload_reservation`: `check_release_at_submit` releases two chunks after a fenced submit, keeps them from - reuse until the fence passes, reuses one, then finds the other intact and the reused one reported on a + reuse until the fence passes, reuses one, then finds the other intact and the reused one recycled on a resubmit, and fences the intact one again. Mutants: keeping the holds fails "release: holds dropped"; ignoring generations fails "a resubmit finds chunk 1 intact and chunk 0 reused". - `startup_address_console_gate_ring_keep_*` (`X2W_TEST_CONSOLE_KEEP`): a second list A uploads 32 by-address payloads of 16 MiB each from the ring (512 MiB), is submitted, completes and is never Reset. The fixture's own - list then draws payload 32 over the right half of the target, the target is read back, and A is submitted - again. + list then draws payload 32 over the right half of the target, A is submitted again, and the target is read back + after that submit. - Without the switch, A keeps every chunk: the ring refuses past 256 MiB and the right half reads the null view. - - `..._keep_release_*` sets the switch: the right half holds payload 32 from a chunk A released, and A's second - submit is reported as reading a reused chunk. - - Mutants: disabling the release leaves the right half on the null view; silencing the report fails - "list A submitted again after B reused its chunk was not reported". + - `..._keep_release_*` sets the switch: the right half holds payload 32 from a chunk A released. A's second + submit would read a reused chunk, so it is refused and not executed: the right half still holds payload 32, + not A's payload 31. + - Mutants: disabling the release leaves the right half on the null view; executing the stale resubmit (the + check before the execute removed) puts A's draws back over the right half and fails on wrong pixels. - Fixtures that describe the default ring clear the switch for their own process, so they keep testing the default mode when it is set in the environment: the #2849 `startup_address_console_gate_ring{,_reuse}_*` (a refusal at the 256 MiB cap) and `startup_slot133_heap_cap_capped_*` (doubling growth to 48 MiB). @@ -58,7 +61,7 @@ One build; only the switch differs. Aggregates only; logs stay local. |---|---|---| | null bindings with cause `buffer` | 33,632 | 0 | | `upload ring reservation refused` logged | yes | no | -| resubmits reported after a reuse | n/a | 0 | +| resubmits after a reuse (now refused) | n/a | 0 | | process commit peak | 16.7 GiB | 18.1 GiB | | visible | single full black frames in the post-load animation | none seen | @@ -66,7 +69,7 @@ One build; only the switch differs. Aggregates only; logs stay local. every queue had passed the chunk's fence. Most refused requests were 64 KiB: the window `upload_from_title` copies for a graphics root SRV without an existing upload mapping. - The title never submitted a closed list again without a Reset: 0 of more than 500,000 submits over five runs. - The switch relies on that; a title that does resubmit gets the logged report described above. + A title that does resubmit after a reuse gets the refusal described above: that list does not run. - The larger ring costs about 1 GiB of process commit at peak in this run. Not established: other titles' resubmit behaviour, and whether 512 MiB is enough on other workloads. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 34859c2c3..6289a9a28 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1495,7 +1495,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_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). A later submit of the same list before its Reset fences its unchanged chunks again and logs any chunk reused in between (`startup_address_console_gate_ring_keep_*`, `upload_reservation`). See [the experiment note](experiments/startup-upload-ring-release-at-submit.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 cost about 1 GiB of process commit at peak in the startup run of 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 | | `X2W_D3D12_RELEASE_RETIRED=1` | opt-in, effective with `X2W_D3D12_RENDER=1` only (the list handlers that record holds exist only there): frees the desktop object of a released committed 2D texture of one narrow class once no recorded list, submission or release-time fence refers to it ([details](#d3d12-resource-budget)). Unset, nothing changes | @@ -3398,10 +3398,11 @@ holding its chunks as soon as its submit is fenced: the queue fence alone then keeps the chunks from reuse until the GPU has passed it. The ring grows in 16 MiB chunks up to 512 MiB in this mode. If the same list is submitted again before its Reset, the submit fences its chunks again when their generation is -unchanged. A chunk that was recycled in between is counted and logged (`closed -list ... submitted again after its released chunks ... were reused`); that -submit reads replaced bytes, so the mode suits titles that Reset a list before -submitting it again. `startup_address_console_gate_ring_keep_*` keeps one list +unchanged. If a chunk was recycled in between, the list would read replaced +bytes: it is left out of `ExecuteCommandLists` before the execute, counted and +logged (`closed list ... submitted again after its released chunks ... were +reused; refused, it is not executed`) until its Reset. The mode therefore suits +titles that Reset a list before submitting it again. `startup_address_console_gate_ring_keep_*` keeps one list closed and never Reset while a second list needs ring space, without and with the switch. See [the experiment note](experiments/startup-upload-ring-release-at-submit.md). `upload_reservation` exercises the shared cursor and returned-pointer contract diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 4a04a18fc..1eb15d0dd 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1462,7 +1462,7 @@ std::unordered_map> queue_submits; std::vector upload_chunks; // allocator_lock also protects recording holds and queue fence values // X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 (allocator_lock): chunks each closed list released at a fenced submit, until its Reset. std::unordered_map released_upload_chunks; -LONG upload_resubmits_after_reuse; // closed lists submitted again after a released chunk was reused +LONG upload_resubmits_refused; // closed lists submitted again after a released chunk was reused; not executed LONG allocator_swaps, allocator_creates; @@ -1664,13 +1664,32 @@ bool submit_fence_fails() // X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 (opt-in): a closed list stops holding its upload ring chunks once its submit is // fenced, so the queue fence alone guards them; the ring then grows in 16 MiB chunks up to 512 MiB. A later submit -// of the same list before its Reset fences its intact chunks again; one whose chunk was reused meanwhile is logged. +// of the same list before its Reset fences its chunks again. One whose chunk was reused meanwhile is refused before +// ExecuteCommandLists (released_chunks_recycled), so it never reads replaced bytes. bool ring_release_at_submit() { static const bool on = GetEnvironmentVariableW(L"X2W_D3D12_RING_RELEASE_AT_SUBMIT", nullptr, 0) != 0; return on; } +// draw_lock and allocator_lock held, before the queue executes `list`. True when the closed list released its chunks +// at an earlier fenced submit and one of them was reused since: the submit leaves it out, and it stays refused until +// its Reset. Counted and logged. +bool released_chunks_recycled(Wrapper* list) +{ + const auto released = released_upload_chunks.find(list); + if (released == released_upload_chunks.end()) return false; + UploadChunkMask recycled = 0; + resubmitted_upload_chunks(released->second, upload_chunks.data(), recycled); + if (!recycled) return false; + InterlockedIncrement(&upload_resubmits_refused); + static LONG logged; + if (first_calls(logged, 8)) + log("x2w-draw upload ring: closed list %p submitted again after its released chunks 0x%x were reused; " + "refused, it is not executed", static_cast(list), static_cast(recycled)); + return true; +} + // draw_lock and allocator_lock held, after the lists were executed on queue. Each list's allocator holds a fence value // the queue must pass before the allocator can be reset. If that fence cannot be created or // signalled, the work is on the GPU all the same: the allocators are marked untracked instead. @@ -1691,15 +1710,9 @@ void note_submits(ID3D12CommandQueue* queue, const std::vector& lists) chunks |= list->own().upload_chunks; const auto released = released_upload_chunks.find(list); if (released == released_upload_chunks.end()) continue; + // A list with a reused chunk was refused before the execute, under the same locks: all of them are intact. UploadChunkMask reused = 0; chunks |= resubmitted_upload_chunks(released->second, upload_chunks.data(), reused); - if (reused) { - InterlockedIncrement(&upload_resubmits_after_reuse); - static LONG logged; - if (first_calls(logged, 8)) - log("x2w-draw upload ring: closed list %p submitted again after its released chunks 0x%x were reused; " - "it reads replaced upload bytes", static_cast(list), static_cast(reused)); - } } const bool signaled = fence && SUCCEEDED(queue->Signal(fence, value + 1)); if (signaled) ++value; @@ -4268,6 +4281,7 @@ void __stdcall queue_execute(Wrapper* self, UINT count, void* const* lists) for (UINT i = 0; i < count; ++i) { Wrapper* w = as_wrapper(lists[i]); if (w && w->own().kind == list_kind && w->own().list_ready) { + if (released_chunks_recycled(w)) continue; // its upload bytes were replaced: it is not executed ready.push_back(static_cast(w->own().real)); submitted.push_back(w); } diff --git a/tests/startup_address_console_check.py b/tests/startup_address_console_check.py index 062b11c09..be74340af 100644 --- a/tests/startup_address_console_check.py +++ b/tests/startup_address_console_check.py @@ -15,7 +15,8 @@ and the right half reads the null view gate-ring-keep-release the same with X2W_D3D12_RING_RELEASE_AT_SUBMIT=1: A releases its chunks at its fenced submit, so B draws payload 32; A submitted again afterwards - is reported as reading reused chunks + would read reused chunks, so it is refused before the execute: the target, read + back after that submit, still holds payload 32 on the right (not A's payload 31) The backing bytes are built here, independently of src/startup_stubs/gfx10_layout.h, from the address equations of AMD addrlib's GFX10 pattern tables, written out below. Reference: addrlib as Mesa carries it @@ -296,13 +297,14 @@ def check_gate(directory, consumer, backend, mode): raise AssertionError(f"{mode}: {wrong} wrong pixels, found {sorted(seen)[:4]}; expected {first.hex()} left, " f"{last.hex()} right\n{ring_lines}") refused = "x2w-draw upload ring reservation refused: no unused capacity" in log - reported = "submitted again after its released chunks" in log + reported = "were reused; refused, it is not executed" in log if release and not reported: - raise AssertionError(f"{mode}: list A submitted again after B reused its chunk was not reported") + raise AssertionError(f"{mode}: list A submitted again after B reused its chunk was not refused") if not release and (not refused or reported): - raise AssertionError(f"{mode}: expected the 256 MiB refusal and no release report (refused={refused}, reported={reported})") - print(f"{backend} {mode}: {half} left pixels hold payload 0 and {half} right pixels hold {last.hex()}; " - + ("A's chunks were released at its fenced submit and its later resubmit was reported" if release + raise AssertionError(f"{mode}: expected the 256 MiB refusal and no resubmit refusal (refused={refused}, reported={reported})") + print(f"{backend} {mode}: {half} left pixels hold payload 0 and {half} right pixels hold {last.hex()} after A's " + "resubmit; " + + ("A's chunks were released at its fenced submit and its stale resubmit was refused, not executed" if release else "the never-Reset list kept its chunks and the ring refused past its cap")) return 0 if ring: diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index afbb5b6b2..9c7d7844b 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -589,7 +589,7 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* }; // X2W_TEST_CONSOLE_KEEP (with the shared ring): a second list A records 32 payloads (32 x 16 MiB = 512 MiB), // is submitted and waited for, and is never Reset. The fixture's own list then draws payload 32 over the right - // half. The target is read back; then A is submitted again, still closed and not Reset. + // half. A is then submitted again, still closed and not Reset, and the target is read back. if (GetEnvironmentVariableW(L"X2W_TEST_CONSOLE_KEEP", nullptr, 0)) { constexpr unsigned kept = 32; void *allocator_a = nullptr, *list_a = nullptr; @@ -622,9 +622,10 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* if (const int result = draw_gated_console_payload(guest, list, kept, false)) return result; invoke(list, 9); if (!submit_and_wait(list, 3)) return 25; - const int read = read_stream_target(desktop.Get(), frame, pixels); - if (!submit_and_wait(list_a, 4)) return 25; // A again: closed, never Reset - return read; + // A again: closed, never Reset. The target is read back after it, so a stale A that ran (its last + // payload, 31, over the right half) shows in the pixels. + if (!submit_and_wait(list_a, 4)) return 25; + return read_stream_target(desktop.Get(), frame, pixels); } // Retrying the same bytes after abandoning a recording still needs an upload. The shared-ring // control skips it so every upload behind the gate comes from the ring, whose 256 MiB cap is From 8c1837003cb0a85080e203afbe5eb64ae725bac5 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 06:37:38 +0100 Subject: [PATCH 3/3] docs: repeat the upload ring startup pair after the stale-resubmit refusal Same settings on a later main: buffer null bindings 50,665 -> 0, no reservation refusals, 0 stale resubmits refused, commit peak 17.9 -> 18.0 GiB, no black frames seen. The switch row now says the ring can add up to about 1 GiB of commit. Co-Authored-By: Claude Opus 5.5 --- docs/experiments/startup-upload-ring-release-at-submit.md | 5 ++++- docs/startup-runtime.md | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/experiments/startup-upload-ring-release-at-submit.md b/docs/experiments/startup-upload-ring-release-at-submit.md index 842474f01..b24ea0e27 100644 --- a/docs/experiments/startup-upload-ring-release-at-submit.md +++ b/docs/experiments/startup-upload-ring-release-at-submit.md @@ -70,6 +70,9 @@ One build; only the switch differs. Aggregates only; logs stay local. `upload_from_title` copies for a graphics root SRV without an existing upload mapping. - The title never submitted a closed list again without a Reset: 0 of more than 500,000 submits over five runs. A title that does resubmit after a reuse gets the refusal described above: that list does not run. -- The larger ring costs about 1 GiB of process commit at peak in this run. +- The larger ring cost about 1 GiB of process commit at peak in this run. +- Repeated after the stale-resubmit refusal, on a later `main`, same settings: buffer null bindings 50,665 → + 0, reservation refusals logged → none, stale resubmits refused 0 → 0, commit peak 17.9 → 18.0 GiB, black + frames → none seen. Not established: other titles' resubmit behaviour, and whether 512 MiB is enough on other workloads. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 6289a9a28..2568270ce 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1495,7 +1495,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_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 cost about 1 GiB of process commit at peak in the startup run of 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_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 | | `X2W_D3D12_RELEASE_RETIRED=1` | opt-in, effective with `X2W_D3D12_RENDER=1` only (the list handlers that record holds exist only there): frees the desktop object of a released committed 2D texture of one narrow class once no recorded list, submission or release-time fence refers to it ([details](#d3d12-resource-budget)). Unset, nothing changes |