Skip to content

GPU: preserve CPU stores across overlapping snapshot writebacks - #3412

Open
Jineggegg wants to merge 2 commits into
Marvisak:mainfrom
Jineggegg:fix/shadow-snapshot-race
Open

Jineggegg wants to merge 2 commits into
Marvisak:mainfrom
Jineggegg:fix/shadow-snapshot-race

Conversation

@Jineggegg

Copy link
Copy Markdown

Reproduction

Two declarations from one submission can cover the same bytes. In the first regression test the ranges cover offsets 2–5 and 3–7. The shader output changes bytes 2 and 5. A CPU thread writes C1 at byte 4 after the first writeback reads title memory. The original code writes its earlier value, 00, over C1. The test also writes C2 before processing the second declaration and verifies that C2 survives.

The second regression test declares an aligned 64 KiB page followed by its four bytes at offsets 100–103. The full-page writeback publishes A1 A2 A3 A4. The CPU then writes C2 at offset 101. Processing the smaller declaration used to publish A2 again, overwriting the newer CPU store.

Change

The snapshot path compares shader output with the bytes saved before the submission. It writes contiguous runs of changed bytes through the existing checked write API. A CPU store in a gap between two shader changes therefore survives. After restoring write tracking on a completed full page, it reads title memory back into the shadow. It also updates the comparison baseline for a reconciled partial piece so that a later declaration can revisit it safely.

The per-submission set now records a full page after a successful full-page comparison against readable title memory. A later partial declaration now looks up the same aligned page in that set and skips it. Partial pieces do not mark the whole page complete. A failed read spanning a decommitted 4 KiB block leaves the 64 KiB page eligible for retry. A third regression test verifies that a later four-byte declaration beyond the hole still publishes its shader output.

Windows test evidence

The three regression tests in shadow::tests use real D3D12 devices and synthetic shadow contents, so the CPU store lands at the same point on every run:

  • overlapping_snapshots_keep_late_cpu_stores
  • partial_snapshot_after_a_full_page_preserves_a_new_cpu_store
  • readable_partial_snapshot_retries_after_a_full_page_read_fails

cargo test -p victory-gpu --features diagnostics --lib --locked shadow::tests:: -- --test-threads=1, on the RTX 5090 and again with VICTORY_D3D12_WARP=1 (the GPU crate's WARP switch exists only in diagnostics builds).

Regression Before After Adapters
CPU store in a gap between two shader writes, followed by overlapping partial declarations Upstream 63bcca4 read back 00 where the CPU had written C1. The first writeback retained C1 and the second retained C2. RTX 5090 and Microsoft Basic Render Driver (WARP)
Full aligned page followed by four bytes within it The preceding snapshot fix (b1fb182) read back A1 A2 A3 A4 after the CPU replaced A2 with C2. Both devices read back A1 C2 A3 A4. RTX 5090 and Microsoft Basic Render Driver (WARP)

The decommitted-block test also passed on both devices. The 64 KiB read failed as expected and the four mapped bytes beyond the hole received A1 A2 A3 A4.

On Windows the upstream-based workspace recorded 868 passes, 6 failures and 10 ignored tests. Every failure also reproduced on the preceding snapshot commit, b1fb182. Two GPU assertions concern the native descriptor heap size and the view beyond the dynamic slots. One shader test (heap_direct_texture_reads_mark_their_heap_index) terminated with Windows status 0xc0000005 (#2571); the remaining shader tests were then run by name. Three other shader tests failed on hardware: the stride used for a structured-buffer read, dispatch when the buffer view is absent, and the gathered values at a specified level. The same outcomes were recorded on both revisions. The adapter-dependent regression tests follow the existing skip convention if no host can be created.

Comparison with prior work

The following candidates were each ported onto one local integration tree (not tested at their own heads) and run with the first regression test on an RTX 5090. The comparisons concern the snapshot path, even where a proposal mainly changes another path.

Candidate Observed result
#2009 (closed) Preserved C1 in the initial race. With a preceding B1 CPU store, the later overlapping declaration restored B1 over C2.
#2242 Combining nearby differences let the first writeback restore 00 over C1.
#2280 The first writeback restored 00 over C1.
#2409 The first writeback restored 00 over C1.
#2914 The first snapshot writeback restored 00 over C1.
#3069 The first snapshot writeback restored 00 over C1.
#3099 The first snapshot writeback restored 00 over C1.
#3118 The first snapshot writeback restored 00 over C1.
#2918 The first snapshot writeback restored 00 over C1.

Three open PRs that change the same snapshot code were tested directly at their current heads, on the RTX 5090 and on WARP. The three tests above were added unchanged, with the same read seam in copy_back_or_not (the title read goes through a closure, as in this PR):

Candidate overlapping_snapshots_keep_late_cpu_stores partial_snapshot_after_a_full_page_preserves_a_new_cpu_store
main 63bcca4 (same seam) fails: [A1, 00, 00, A2], the CPU's C1 lost fails: [A1, A2, A3, A4], the CPU's C2 lost
#3318 8b284dd fails the same way fails the same way
#3211 ebd01cd fails the same way fails the same way
#2437 60d81cf fails the same way fails the same way
this PR 31626ec passes passes

readable_partial_snapshot_retries_after_a_full_page_read_fails passes on all of them; it guards this PR's own change. #3318 and #3211 rework the same function for speed, and this PR will need rebasing on whichever lands first.

Scope

This fix covers declared ranges with saved snapshots. Upstream’s separate marked-page path, copy_back_marked, can still overwrite a concurrent CPU store when it writes a merged whole page. That issue remains open.

RTX 5090, driver 621.14, Windows 11 build 26300, in-box WARP 10.0.26100.9818, Rust 1.98.1.

Synthetic tests only, no game data. AI-assisted (Claude Code); the runs are from my machine.

🤖 Generated with Claude Code

Write precisely the shader-modified ranges. Rearm tracked pages before taking the live title bytes back into the shadow and advance the baseline for partial pieces that another declaration can visit. Exercise one real CPU store after the title read and another before a second overlapping copy-back.
The full page has published its shader writes and has been read back from the title. Replaying a smaller declaration can overwrite a CPU store made after that point. Check the aligned page in the per-submission compared set. Record completion after a successful full read so a readable subrange can retry after a hole.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Preserving late CPU stores across overlapping and partial snapshot writebacks (and retrying readable partial snapshots after a full-page read fails) is cleanly implemented and thoroughly tested.
  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 13 passed, 0 failed (overlapping_snapshots_keep_late_cpu_stores, partial_snapshot_after_a_full_page_preserves_a_new_cpu_store, readable_partial_snapshot_retries_after_a_full_page_read_fails).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@Jackster

Copy link
Copy Markdown

Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • cargo test -p victory-gpu --lib: 234 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231 passed; +3).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

The three added tests are readable_partial_snapshot_retries_after_a_full_page_read_fails, partial_snapshot_after_a_full_page_preserves_a_new_cpu_store and overlapping_snapshots_keep_late_cpu_stores; all pass, and the existing snapshot/withheld tests are unchanged. Synthetic only. AI-assisted.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid. Reviewed store_snapshot_changes and the copy_back_or_not_with tracking/re-arm sequence in crates/victory-gpu/src/shadow.rs, and verified on both RTX 5090 and WARP with VICTORY_D3D12_DEBUG=1:

  • cargo xtask policy and cargo clippy --tests -p victory-gpu (default and --features diagnostics, -D warnings) pass cleanly.
  • store_snapshot_changes emits writes only for contiguous gpu != old runs that still differ from title, avoiding gap overwrites of concurrent CPU stores.
  • Keying compared on the aligned 64 KiB page and inserting only after a successful full-page read correctly prevents smaller overlapping declarations from replaying completed pages while still allowing readable subranges to retry after a failed full-page read.
  • Re-arming watch tracking (t.taken(&whole)) before re-reading title memory into the shadow and refreshing the partial-piece before baseline preserves late CPU writes across partial-before-full and overlapping partial snapshots.
  • All shadow::tests plus an additional cross-page partial-before-full overlap probe pass on both RTX 5090 and WARP (13/13 passed, 0 D3D12 debug layer messages).

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.

4 participants