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

Open
github-bot wants to merge 1 commit from github-bot/gh-786 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 07:35 UTC · original
AscilCH:fix/gpu-shadow-relay-and-card-merge-write-changed-runs → main · +285 −67 · 1 files · 1 commits · open

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

**@⁠AscilCH** on GitHub · 2026-11-25 07:35 UTC · [original](https://github.com/fxceless/victory-fork/pull/786) `AscilCH:fix/gpu-shadow-relay-and-card-merge-write-changed-runs` → `main` · +285 −67 · 1 files · 1 commits · open ## 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.)*
`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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:08 +00:00
Author
Owner

@⁠alexodee on GitHub · 2026-11-25 11:09 UTC · original

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.

**@⁠alexodee** on GitHub · 2026-11-25 11:09 UTC · [original](https://github.com/fxceless/victory-fork/pull/786#issuecomment-7160662205) 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 has changes conflicting with the target branch.
  • crates/victory-gpu/src/shadow.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/786/head:github-bot/gh-786'
git switch 'github-bot/gh-786'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-786'
git switch 'github-bot/gh-786'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-786'
git switch 'github-bot/gh-786'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-786'
git switch 'main'
git merge --squash 'github-bot/gh-786'
git switch 'main'
git merge --ff-only 'github-bot/gh-786'
git switch 'main'
git merge 'github-bot/gh-786'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.