Skip to content

GPU/shadow: write only GPU-changed byte runs in relay_merge, card_merge, and card_merge_relay - #786

Open
AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/gpu-shadow-relay-and-card-merge-write-changed-runs
Open

AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/gpu-shadow-relay-and-card-merge-write-changed-runs

Conversation

@AscilCH

@AscilCH AscilCH commented Nov 25, 2026

Copy link
Copy Markdown

Summary

Fix the write-back race (upstream VIctory #2009) across all three default shadow-buffer merge paths in crates/victory-gpu/src/shadow.rs:

  • Shadow::copy_back_with (back_piece, VICTORY_RELAY=0) was previously updated to write back only the maximal contiguous byte runs modified by the GPU, leaving untouched bytes between the first and last changed byte alone so concurrent title CPU writes in the gap survive.
  • However, Shadow::relay_merge, Shadow::card_merge, and Shadow::card_merge_relay still wrote back the entire [first..=last] stretch (up to 64 KiB) in a single write_mapped call, reverting concurrent title CPU writes landing in untouched gap bytes (or inside skip ranges in card_merge_relay) between read_into and write_mapped.

Changes

  • Record per-byte change masks (merged ^ old_title) in merge_piece and merge_card_piece, extract maximal nonzero byte runs via collect_mask_runs (reused by back_piece as well) using per-thread scratch (with_merge_scratch), and write back only those runs via write_changed_runs.
  • Pass the actual written runs into merge_race so VICTORY_MERGE_RACE=1 diagnostics measure only bytes actually written back to title memory.
  • Add synthetic unit tests (card_and_relay_merges_emit_only_gpu_changed_runs and write_changed_runs_preserves_concurrent_cpu_writes_in_gap) verifying run extraction and proving that both deterministic and concurrent CPU writes in the untouched middle of a 64 KiB stretch are never clobbered.

Verification

  • cargo test -p victory-gpu --locked -- shadow::: 30 passed; 0 failed; 0 ignored.
  • cargo clippy -p victory-gpu --all-targets --all-features --locked: 0 warnings in crates/victory-gpu/src/shadow.rs (only pre-existing 9376fd6 warnings in draw.rs and indirect.rs).
  • cargo xtask policy: 171 files ok.

(AI-assisted; synthetic unit tests only, no game data.)

…ge, and card_merge_relay

`Shadow::copy_back_with` (`back_piece`) was fixed for upstream VIctory #2009
to write back only the maximal contiguous byte runs actually modified by the
GPU, leaving untouched bytes between the first and last changed byte alone so
concurrent title CPU writes in the gap survive.

However, all three default-path merges (`Shadow::relay_merge`,
`Shadow::card_merge`, and `Shadow::card_merge_relay`) still wrote back the
entire `[first..=last]` stretch (up to 64 KiB) in a single `write_mapped`
call. Any concurrent title CPU write landing between `read_into` and
`write_mapped` in the untouched gap (or inside `skip` ranges in
`card_merge_relay`) was silently overwritten with stale bytes from `read_into`.

Record per-byte change masks in `merge_piece` and `merge_card_piece`, extract
maximal nonzero byte runs via `collect_mask_runs` using reused per-thread
scratch, pass those exact runs to `merge_race` so `VICTORY_MERGE_RACE=1`
inspects only written bytes, and write only those runs via
`write_changed_runs`. Add unit tests covering run extraction for all merge
paths plus deterministic and concurrent CPU writes in the untouched gap of
a 64 KiB stretch.
oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
…PU-changed byte runs in relay_merge, card_merge, and card_merge_relay (by AscilCH)
oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
JustDownTV added a commit to JustDownTV/victory-gta6 that referenced this pull request Nov 25, 2026
…rite

Since upstream PR fxceless#786 every run of bytes the GPU changed is its own kernel write (write_mapped, NtWriteVirtualMemory), and shader output changes bytes here and there in every word. In the game the copy-back went from 42 ms to 630-690 ms a frame (frame 270 -> 1,100 ms while warming up). Runs fewer than 64 bytes apart are now written as one, the bytes between them as the title had them; runs further apart stay apart, so a title write in a real gap is still kept.
@alexodee

Copy link
Copy Markdown

A cost number that may matter here, from the one path that already writes per run. It is not a review of the correctness part, and I have not run this PR.

With VICTORY_RELAY=0, copy_back_with writes one write_mapped per run since 8dd30ab. On an RTX 3070 Ti Laptop 8 GB with 16 GB RAM (Windows 11, diagnostics build, Series X profile, a26859f plus a few open PRs), medians over gameplay report lines:

pieces that differ "putting them back" frame
one call per run 688 632 ms 1,109 ms
runs at most 4096 bytes apart in one call (local change) 680-685 37.3 ms 462-467 ms
older tree (2119b0e), whole stretch in one call 609 13.4 ms 421 ms

So on this machine per-run writes cost about 0.9 ms per differing piece in this path. write_changed_runs has no bound on the number of calls, and this PR uses it in relay_merge, card_merge and card_merge_relay, which are the default paths. I do not know whether their pieces are as scattered, since I did not run the relay. A run with the relay on, comparing the merge times before and after, would tell.

If the cost shows up there too, collect_mask_runs looks like the place for a bound, since all four paths share it here. #857 joins runs less than 64 bytes apart in copy_back, and upstream Marvisak/VIctory#2242 does it under 256 bytes. #690 and Marvisak/VIctory#1024 store from user mode, which leaves the gaps untouched. Joining reopens the race inside the joined gaps, so it is a trade.

Limits: one machine, one run per row, and the joined runs each differ from the per-run one by one more switch. More Windows rows in my comment on #857.

AI-assisted (Claude Code). No game data.

oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
Our merge of fxceless#783 used its first form (b5dee8d). Its later commits:

- Drop the one-write copy-back. It filled the gaps between GPU-changed runs
  with the title's bytes as read and wrote the whole stretch after the
  loop, over any title write that landed in between, undoing upstream
  #2009 and the intent of fxceless#786 (write only GPU-changed runs). The author
  dropped it after review: it did not move the copying-back time.
- Forget a released heap's kept pages, so a new heap at the same address
  does not skip its first sync.
- Walk only the records between a page's first and last changed byte, plus
  slots never mirrored. Adapted to our registry-held CopiedSlots (Codex
  #13): the never-mirrored check is CopiedSlots::needs. Upstream measured
  descriptor sync 18.9 -> 10.5 ms/frame (RTX 3080).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLuVRkAEvF29hMbrBJw8os
oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
…, bisect plan

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLuVRkAEvF29hMbrBJw8os
oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
…sion

It wrote a host copy back whole and overwrote the title's writes, undoing
fxceless#786. The PR's author removed it (b5dee8d -> d695015); this applies that
change to shadow.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLuVRkN9XKxpoYQhz4KNXw
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