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 from upstream-pr/786 into main
AscilCH commented 2026-11-25 07:35:31 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T07:35:31Z.


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.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6599577162 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/786). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T07:35:31Z. --- ## 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.)*
alexodee commented 2026-11-25 11:10:43 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by alexodee on 2026-11-25T11:09:09Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7160662205 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/786#issuecomment-7160662205). Originally posted by [alexodee](https://github.com/alexodee) on 2026-11-25T11:09:09Z. --- 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.
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/786:upstream-pr/786'
git switch 'upstream-pr/786'

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/786'
git switch 'upstream-pr/786'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/786'
git switch 'upstream-pr/786'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/786'
git switch 'main'
git merge --squash 'upstream-pr/786'
git switch 'main'
git merge --ff-only 'upstream-pr/786'
git switch 'main'
git merge 'upstream-pr/786'
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
fxceless/victory-fork!786
No description provided.