Skip to content

startup: refresh a frame copy when the title rewrites its memory - #1658

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-p07-framecopy
Nov 24, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-p07-framecopy

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Change

frame_copy in d3d12_draw.inl keeps one upload-ring copy per frame for each (address, bytes) that a translated draw reads from title memory (constant buffers, vertex and index ranges, root pointers). It never checked the contents. A title that rewrites the same address between two draws of one frame, such as a dynamic constant buffer, gave the second draw the first draw's data.

Each entry now also keeps the bytes it was made from. A lookup is a hit only when the address and size match and the title's memory still equals those bytes (memcmp). Otherwise the draw gets a new ring slice, filled from a fresh snapshot, and the entry is replaced. The slice the first draw already uses is not touched. The first four refreshes log frame copy of ... refreshed.

Choices:

  • The comparison is against a kept copy, not against the ring: the ring is an upload-heap buffer mapped for writing, so reading it back is slow.
  • No hash: a memcmp cannot collide. The old lookup key address * prime ^ bytes could also name two different ranges; the stored address and size now settle that case.
  • The snapshot is taken first and the ring is filled from it, so the ring slice and the kept bytes agree even if the title writes the memory on another thread.
  • Readability: all callers (DRAW:1024 buffers and constants, DRAW:1074 indices) probe readable_window for the whole range immediately before calling, as they did for the old memcpy. The compare reads the same bytes under the same probe. draw_lock is held, so the map needs no lock of its own.
  • Indices and every other caller go through the same function and benefit without a second path.

Cost: one extra copy of each range per frame and a memcmp per repeated lookup, both of the size of the data the draw already reads.

Only frame_copy and its map changed in the draw file; no hunk in d3d12_xs_device.cpp, none near pipeline_for or the draw call sequence. Overlap with #1441, #1469, #1567, #1641 and #1654: none in source; #1654 also adds a test to the same CMakeLists.txt and docs/startup-runtime.md in different places.

Refs #870.

Verification

Run on Linux only; the draw fixture needs Windows:

  • git diff --check: clean.
  • python tests/docs_links_check.py: self-test passed, 16 Markdown files and 349 links.
  • python tests/bidi_controls_check.py: self-test passed, 1277 tracked files, no bidirectional controls.
  • python -m py_compile tests/startup_frame_copy_check.py: ok.
  • x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only on tests/startup_frame_copy.cpp and tests/startup_stream_draw.cpp: no diagnostics.
  • The same on d3d12_xs_device.cpp (which includes the draw file): only the two MSVC-only __try/__except errors in local_log.h. That one compile peaked near 1 GB of memory.
  • The new lookup logic compiled outside the project against a stub ring: reuse of unchanged bytes (one slice), refresh on changed bytes (new slice, old slice intact, one log line), reuse of the refreshed bytes, a different size as a different entry, a new frame recopies. All passed. That harness is not part of this PR.

Windows Server 2025 (WARP), python3 x2w_pipe.py win2025 1658 --force --targets startup_stream_consumer,startup_stub_xg_xs,startup_stub_consumer,d3d12_xs_shim,d3d12x_graphics_list_check --regex '^(startup_frame_copy_warp|startup_stream_draw_warp|startup_stubs|d3d12x_graphics_list_warp|startup_cb_blend_warp|stream_root_srv_warp|startup_address_srv_warp|startup_address_buffer_srv_warp)$' at 1a6e8cc3:

  • Debug (ctest --preset dev): 8/8 passed, including startup_frame_copy_warp (1.78 s).
  • Release /O2 (ctest --preset release): 8/8 passed, including startup_frame_copy_warp (1.68 s).

startup_frame_copy_hardware is not run: the node only has WARP.

Evidence and follow-up

tests/startup_frame_copy_check.py reuses draw_texture from startup_address_srv_check.py: the translated pixel stage is replaced by one that returns asfloat(constants.Load4(0)) from a ByteAddressBuffer bound with srv 0 buf table 0 0. tests/startup_frame_copy.cpp (selected by X2W_TEST_FRAME_COPY) creates a title-memory descriptor heap with one CBV over a 256-byte array, binds it, draws the left half of the 8x4 target, then draws the right half through a second scissor:

mode between the draws expected target refresh log lines
changed the colour is rewritten in place, red to blue left half red, right half blue 1
same nothing all red 0

On the base commit the changed case should give red on both halves; that was not run.

Assumption: both draws fall in the same frame, which the fixture guarantees by recording one list and presenting nothing in between.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted; commands listed were executed locally.

frame_copy keeps the bytes each entry was made from and makes a new ring slice when the title's memory no longer matches. Adds startup_frame_copy_{warp,hardware} with a rewritten and an untouched constant buffer.
@tedin7
tedin7 marked this pull request as ready for review November 24, 2026 16:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T16:43:32.773595Z 1a6e8cc Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

e01e5bd keeps snapshot caching optional when an allocation fails. The already-reserved upload slice can still be filled directly. Windows Release frame-copy and address-SRV checks passed 4/4 on RTX and WARP. In a local variant that forced the refresh snapshot allocation to fail, both frame-copy readbacks still passed.

@R0Y00
R0Y00 merged commit da4bf08 into R0Y00:main Nov 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants