startup: record the calls into the D3D12 stand-in and replay them offline (draft for discussion) #3206

Open
tedin7 wants to merge 15 commits from upstream-pr/3206 into main
tedin7 commented 2026-11-27 21:43:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T21:43:02Z.


Part of #870

What this unblocks: reproducing a title's D3D12 work offline, on WARP or any GPU, with the real stand-in and translator, so a runtime or translator fix can be checked against the same frames without the title.

Draft for discussion. This is large (about 6.7k lines, most of it tests and the player). It will be split into reviewable PRs before anyone is asked to review it (plan at the end). It is open now so the design can be discussed and so the refusal census in #3198 has its input documented.

What it records

X2W_REPLAY_RECORD=all, <start s>,<seconds>, f<first present>,<presents> or d<draws>,<seconds> wraps every slot of every object kind the stand-in hands out (src/startup_stubs/d3d12_record.inl) and writes X2W_REPLAY_RECORD_FILE (default replay.x2wr). Unset, nothing is wrapped and nothing changes. Per call it keeps:

  • the slot, the object, the register and stack argument words, the return value, and a global sequence number taken before the call;
  • the memory behind pointer arguments, from the per-slot layouts in src/startup_stubs/d3d12_replay_slots.h (generic calls copy a bounded amount), stored once per content as blobs;
  • the wrapper an output pointer received, so the player can map recorded objects to its own;
  • for title memory that GPU work reads (placed resources, descriptor memory, optionally root-view ranges): page images and later changed pages at Present / submit, under a byte budget (src/startup_stubs/replay_pages.h);
  • the shader cache files the run used and the process's X2W_ settings.

Each title thread appends to its own buffer (no lock shared between title threads per call); full buffers go to the file under one lock. Size caps stop the recording cleanly with its end chunk.

File format

Chunks <4-byte tag><u32 header bytes><u64 data bytes>, header, data (src/replay/replay_file.h reads it mapped, in place):
XREC (version, timer frequency), XENV (settings), CALL, ARGM (argument memory: seq, arg, address, bytes, blob hash), BLOB (hash, bytes; once per content), OUTP (output wrapper after the call), HPLC (heap behind a slot 133 placement), DARR / DHPM (descriptor-copy arrays, caller-backed descriptor heap), PAGE / MAP (title memory pages and where they apply), SHDR (shader cache files), PRES (present), XEND (totals, dropped counts). The reader validates every chunk against its call before playing.

How a replay runs

x2w_replay <recording.x2wr> <out dir> [--adapter warp|hardware] [--from-present K] [--max-seconds S] [--prefix-live on] [--pace on]

src/replay/x2w_replay.cpp loads the stand-in (delay-loaded, so the recorded settings are in the environment first), reserves the recorded title ranges at their addresses, seeds the shader cache, then makes every recorded call again in sequence order on the stand-in's objects: recorded wrappers, descriptor handles, mapped and GPU addresses are rewritten to the replay's, argument memory is rebuilt from the blobs, pages are written before the first call that follows them, and fence waits wait for the recorded value. No title code runs; the stand-in, the translator and any fix run for real. It writes stderr.txt (the stand-in log), present_<nnnn>.png per present (X2W_PRESENT_PICTURES, src/startup_stubs/d3d12_present_pictures.inl), replay_summary.json, and one x2w-replay score line; exit codes name the failure (bad file, a title range that cannot be reserved, no device, time limit, a slot result that differs, no adapter).

What it found

Replaying recordings on WARP with the debug layer showed invalid calls that hardware drivers had tolerated; each one is its own small draft: console-format clear values (#3194), viewports, scissors and DSV formats (#3195), the presenter's allocator reset (#3196), debug-layer floods (#3197), sampler fields (#3199), UAV/RTV/DSV view fields (#3200). Shader resource view validation (MipLevels 0 from a UAV descriptor read as an SRV, depth-stencil plane slices) follows. Run on a recording with X2W_SHADER_CACHE set, tools/startup/refusal_census.py (#3198) ranks the first shader refusals by the draws and dispatches they block.

Not in this draft (follow-ups)

  • Replaying on a device whose descriptor increments differ from the recording GPU's (title handles mapped by descriptor index).
  • Submission dumps for work without Present, replays of compute work that writes title memory.
  • Float arguments passed by value, one replay thread per recorded thread.

Split plan

  1. File format reader, page store and slot layouts with their pure tests (startup_replay_pages, startup_replay_prefix_live, startup_replay_call_formats).
  2. The recorder in the stand-in, with startup_replay_record_*.
  3. The player and present pictures, with startup_replay_*, startup_replay_determinism_*, startup_replay_submit_pages_*, startup_replay_title_ranges_warp.
  4. The follow-ups above.

Tests in this draft

  • startup_replay_pages + 3 WILL_FAIL mutants, startup_replay_prefix_live, startup_replay_invoke, startup_replay_call_formats, startup_replay_pages_play (pure or synthetic recordings).
  • startup_replay_record_{warp,hardware}: a stream draw fixture recorded draws the same pixels and the file is well formed.
  • startup_replay_{warp,hardware} and the slot_layouts, null_views, paced variants: a recorded scene replays to the same pictures pixel for pixel; a recording with one clear colour changed replays to a different last picture only; mutated frame token / no-op return are detected.
  • startup_replay_determinism_{warp,hardware}, startup_replay_submit_pages_{warp,hardware}, startup_replay_title_ranges_warp.

Verification:

  • x86_64-w64-mingw32-g++ -std=c++20 -fsyntax-only -fms-extensions -Isrc -Isrc/startup_stubs src/startup_stubs/d3d12_xs_device.cpp -> the same 17 errors as on main (MSVC __try/__except only), no new ones
  • x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -fms-extensions -Isrc src/replay/x2w_replay.cpp -> OK; same for the five new test programs
  • python3 -m py_compile tests/startup_replay_*.py -> OK
  • cmake -S . -B /tmp/cfg -G Ninja -DDXC_EXECUTABLE=/bin/true -> configure OK
  • Win2025 (Build 26100, MSVC, D3D12 WARP) at 35a14b3e, x2w_pipe.py win2025 3206 --targets x2w_replay,startup_replay_quad,startup_replay_pages,startup_replay_prefix_live_check,startup_replay_submit_pages,startup_replay_invoke,startup_stream_consumer,d3d12_xs_shim --regex '^startup_replay_' --force:
    • dev: ctest --preset dev -R '^startup_replay_' -> 23/23 passed
    • release (/O2): same -> 23/23 passed

Thanks to @Makto94 for the testing that found this.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5533451962 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3206). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-27T21:43:02Z. --- Part of #870 What this unblocks: reproducing a title's D3D12 work offline, on WARP or any GPU, with the real stand-in and translator, so a runtime or translator fix can be checked against the same frames without the title. **Draft for discussion.** This is large (about 6.7k lines, most of it tests and the player). It will be split into reviewable PRs before anyone is asked to review it (plan at the end). It is open now so the design can be discussed and so the refusal census in #3198 has its input documented. ## What it records `X2W_REPLAY_RECORD=all`, `<start s>,<seconds>`, `f<first present>,<presents>` or `d<draws>,<seconds>` wraps every slot of every object kind the stand-in hands out (`src/startup_stubs/d3d12_record.inl`) and writes `X2W_REPLAY_RECORD_FILE` (default `replay.x2wr`). Unset, nothing is wrapped and nothing changes. Per call it keeps: - the slot, the object, the register and stack argument words, the return value, and a global sequence number taken before the call; - the memory behind pointer arguments, from the per-slot layouts in `src/startup_stubs/d3d12_replay_slots.h` (generic calls copy a bounded amount), stored once per content as blobs; - the wrapper an output pointer received, so the player can map recorded objects to its own; - for title memory that GPU work reads (placed resources, descriptor memory, optionally root-view ranges): page images and later changed pages at Present / submit, under a byte budget (`src/startup_stubs/replay_pages.h`); - the shader cache files the run used and the process's `X2W_` settings. Each title thread appends to its own buffer (no lock shared between title threads per call); full buffers go to the file under one lock. Size caps stop the recording cleanly with its end chunk. ## File format Chunks `<4-byte tag><u32 header bytes><u64 data bytes>`, header, data (`src/replay/replay_file.h` reads it mapped, in place): `XREC` (version, timer frequency), `XENV` (settings), `CALL`, `ARGM` (argument memory: seq, arg, address, bytes, blob hash), `BLOB` (hash, bytes; once per content), `OUTP` (output wrapper after the call), `HPLC` (heap behind a slot 133 placement), `DARR` / `DHPM` (descriptor-copy arrays, caller-backed descriptor heap), `PAGE` / `MAP` (title memory pages and where they apply), `SHDR` (shader cache files), `PRES` (present), `XEND` (totals, dropped counts). The reader validates every chunk against its call before playing. ## How a replay runs ``` x2w_replay <recording.x2wr> <out dir> [--adapter warp|hardware] [--from-present K] [--max-seconds S] [--prefix-live on] [--pace on] ``` `src/replay/x2w_replay.cpp` loads the stand-in (delay-loaded, so the recorded settings are in the environment first), reserves the recorded title ranges at their addresses, seeds the shader cache, then makes every recorded call again in sequence order on the stand-in's objects: recorded wrappers, descriptor handles, mapped and GPU addresses are rewritten to the replay's, argument memory is rebuilt from the blobs, pages are written before the first call that follows them, and fence waits wait for the recorded value. No title code runs; the stand-in, the translator and any fix run for real. It writes `stderr.txt` (the stand-in log), `present_<nnnn>.png` per present (`X2W_PRESENT_PICTURES`, `src/startup_stubs/d3d12_present_pictures.inl`), `replay_summary.json`, and one `x2w-replay score` line; exit codes name the failure (bad file, a title range that cannot be reserved, no device, time limit, a slot result that differs, no adapter). ## What it found Replaying recordings on WARP with the debug layer showed invalid calls that hardware drivers had tolerated; each one is its own small draft: console-format clear values (#3194), viewports, scissors and DSV formats (#3195), the presenter's allocator reset (#3196), debug-layer floods (#3197), sampler fields (#3199), UAV/RTV/DSV view fields (#3200). Shader resource view validation (MipLevels 0 from a UAV descriptor read as an SRV, depth-stencil plane slices) follows. Run on a recording with `X2W_SHADER_CACHE` set, `tools/startup/refusal_census.py` (#3198) ranks the first shader refusals by the draws and dispatches they block. ## Not in this draft (follow-ups) - Replaying on a device whose descriptor increments differ from the recording GPU's (title handles mapped by descriptor index). - Submission dumps for work without Present, replays of compute work that writes title memory. - Float arguments passed by value, one replay thread per recorded thread. ## Split plan 1. File format reader, page store and slot layouts with their pure tests (`startup_replay_pages`, `startup_replay_prefix_live`, `startup_replay_call_formats`). 2. The recorder in the stand-in, with `startup_replay_record_*`. 3. The player and present pictures, with `startup_replay_*`, `startup_replay_determinism_*`, `startup_replay_submit_pages_*`, `startup_replay_title_ranges_warp`. 4. The follow-ups above. ## Tests in this draft - `startup_replay_pages` + 3 WILL_FAIL mutants, `startup_replay_prefix_live`, `startup_replay_invoke`, `startup_replay_call_formats`, `startup_replay_pages_play` (pure or synthetic recordings). - `startup_replay_record_{warp,hardware}`: a stream draw fixture recorded draws the same pixels and the file is well formed. - `startup_replay_{warp,hardware}` and the `slot_layouts`, `null_views`, `paced` variants: a recorded scene replays to the same pictures pixel for pixel; a recording with one clear colour changed replays to a different last picture only; mutated frame token / no-op return are detected. - `startup_replay_determinism_{warp,hardware}`, `startup_replay_submit_pages_{warp,hardware}`, `startup_replay_title_ranges_warp`. Verification: - `x86_64-w64-mingw32-g++ -std=c++20 -fsyntax-only -fms-extensions -Isrc -Isrc/startup_stubs src/startup_stubs/d3d12_xs_device.cpp` -> the same 17 errors as on main (MSVC `__try`/`__except` only), no new ones - `x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -fms-extensions -Isrc src/replay/x2w_replay.cpp` -> OK; same for the five new test programs - `python3 -m py_compile tests/startup_replay_*.py` -> OK - `cmake -S . -B /tmp/cfg -G Ninja -DDXC_EXECUTABLE=/bin/true` -> configure OK - Win2025 (Build 26100, MSVC, D3D12 WARP) at `35a14b3e`, `x2w_pipe.py win2025 3206 --targets x2w_replay,startup_replay_quad,startup_replay_pages,startup_replay_prefix_live_check,startup_replay_submit_pages,startup_replay_invoke,startup_stream_consumer,d3d12_xs_shim --regex '^startup_replay_' --force`: - dev: `ctest --preset dev -R '^startup_replay_'` -> 23/23 passed - release (`/O2`): same -> 23/23 passed Thanks to @Makto94 for the testing that found this. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-28 13:01:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-28T13:01:07Z.


Update (still Draft), head 9bdc41be5. Merged current main, then added:

  • Queue calls shaped like ExecuteCommandLists (count 1-64 plus a readable array of list wrappers) now have their list array recorded on any slot, not only slot 10. New test startup_replay_execute_slot35_{warp,hardware}.
  • Opt-in X2W_REPLAY_INFER_EXECUTE=1 in the player: a submit recorded without its array submits the next closed lists in Close order; a recorded array always wins. New test startup_replay_infer_execute_{warp,hardware}.
  • Command lists closed before the recorder window have their calls carried in memory (cap X2W_REPLAY_CARRY_MAX_MIB, default 64) and written to the file when the window first submits the list; a list the window Resets is dropped. Tests startup_replay_static_list_*.
  • A carried list is forgotten at its last Release in every phase, so a reused address does not inherit old calls. Test startup_replay_static_release_* (reads the carried bytes before and after the Release).

Not included: the GPU-VA binding of plain title memory, the window-start GPU snapshot and the timed-window shift. They build on replay-draw-memory and GPU-state pieces this PR does not contain, so they belong with those pieces. The pass-statistics option is also left out: it is a separate diagnostic outside this PR's scope.

Verification (aws-1, dev + release):

  • x2w_pipe.py win2025 3206 --host aws-1 --wait 7200 --regex '^startup_replay_' --force: 31/31 passed on both legs.
  • Mutant 1: recorder captures the array on slot 10 only (patched run): 9 of 31 fail on each leg, including execute_slot35, infer_execute, static_list, static_release and slot_layouts.
  • Mutant 2: carry cap default 0 (patched run): static_list and static_release fail on both backends and both legs.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7239629796 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3206#issuecomment-7239629796). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-28T13:01:07Z. --- Update (still Draft), head 9bdc41be5. Merged current main, then added: - Queue calls shaped like ExecuteCommandLists (count 1-64 plus a readable array of list wrappers) now have their list array recorded on any slot, not only slot 10. New test `startup_replay_execute_slot35_{warp,hardware}`. - Opt-in `X2W_REPLAY_INFER_EXECUTE=1` in the player: a submit recorded without its array submits the next closed lists in Close order; a recorded array always wins. New test `startup_replay_infer_execute_{warp,hardware}`. - Command lists closed before the recorder window have their calls carried in memory (cap `X2W_REPLAY_CARRY_MAX_MIB`, default 64) and written to the file when the window first submits the list; a list the window Resets is dropped. Tests `startup_replay_static_list_*`. - A carried list is forgotten at its last Release in every phase, so a reused address does not inherit old calls. Test `startup_replay_static_release_*` (reads the carried bytes before and after the Release). Not included: the GPU-VA binding of plain title memory, the window-start GPU snapshot and the timed-window shift. They build on replay-draw-memory and GPU-state pieces this PR does not contain, so they belong with those pieces. The pass-statistics option is also left out: it is a separate diagnostic outside this PR's scope. Verification (aws-1, dev + release): - `x2w_pipe.py win2025 3206 --host aws-1 --wait 7200 --regex '^startup_replay_' --force`: 31/31 passed on both legs. - Mutant 1: recorder captures the array on slot 10 only (patched run): 9 of 31 fail on each leg, including execute_slot35, infer_execute, static_list, static_release and slot_layouts. - Mutant 2: carry cap default 0 (patched run): static_list and static_release fail on both backends and both legs. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-28 22:08:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-28T22:08:03Z.


Status update, still Draft: merged current main (merge commit, no force push, no conflicts); head is now 8494b98cd.

Verification (Windows Server 2025, D3D12 WARP, head 8494b98cd): x2w_pipe.py win2025 3206 --regex '^startup_replay' -> 31/31 passed.

This stays Draft until an independent review of the head. @KelboLive or @MasterDomino, a review of this head would be welcome; after that I will mark it Ready.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7246519544 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3206#issuecomment-7246519544). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-28T22:08:03Z. --- Status update, still Draft: merged current `main` (merge commit, no force push, no conflicts); head is now `8494b98cd`. Verification (Windows Server 2025, D3D12 WARP, head `8494b98cd`): `x2w_pipe.py win2025 3206 --regex '^startup_replay'` -> 31/31 passed. This stays Draft until an independent review of the head. @KelboLive or @MasterDomino, a review of this head would be welcome; after that I will mark it Ready. AI-assisted; commands listed were executed locally.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3206:upstream-pr/3206'
git switch 'upstream-pr/3206'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3206'
git switch 'upstream-pr/3206'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3206'
git switch 'upstream-pr/3206'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3206'
git switch 'main'
git merge --squash 'upstream-pr/3206'
git switch 'main'
git merge --ff-only 'upstream-pr/3206'
git switch 'main'
git merge 'upstream-pr/3206'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3206
No description provided.