Shadows: batch GPU copy-back writes safely #1124

Open
github-bot wants to merge 2 commits from github-bot/gh-1124 into main AGit
Owner

@⁠divyeshganta on GitHub · 2026-11-24 15:58 UTC · original
divyeshganta:fix/shadow-batched-copyback-boundary → main · +117 −26 · 1 files · 2 commits · open

Related to #918.

This carries cybx's copy-back batching change (commit 4c0d57d5ceec0b836de6849318ae58a404107f91) and fixes the short-range fallback regression reported by DrDarkDK in #918. A changed run shorter than 64 KiB can still cross a 64 KiB boundary; the fallback now splits the run and preserves readable pieces instead of skipping the whole range.

Credit: batching implementation by cybx; boundary-case correction based on DrDarkDK's reproducer and fix report.

The issue report measured copy-back decreasing from 555 ms to 133 ms per frame and total frame time from about 1,033 ms to 750 ms on its test setup. DrDarkDK reported the fallback fix passing synthetic Windows checks on WARP and an RTX 4070 Ti. I have not run tests or performance measurements locally.

**@⁠divyeshganta** on GitHub · 2026-11-24 15:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1124) `divyeshganta:fix/shadow-batched-copyback-boundary` → `main` · +117 −26 · 1 files · 2 commits · open Related to #918. This carries cybx's copy-back batching change (commit `4c0d57d5ceec0b836de6849318ae58a404107f91`) and fixes the short-range fallback regression reported by DrDarkDK in #918. A changed run shorter than 64 KiB can still cross a 64 KiB boundary; the fallback now splits the run and preserves readable pieces instead of skipping the whole range. Credit: batching implementation by cybx; boundary-case correction based on DrDarkDK's reproducer and fix report. The issue report measured copy-back decreasing from 555 ms to 133 ms per frame and total frame time from about 1,033 ms to 750 ms on its test setup. DrDarkDK reported the fallback fix passing synthetic Windows checks on WARP and an RTX 4070 Ti. I have not run tests or performance measurements locally.
Copy-back read and wrote title memory one 64 KiB piece at a time. In the
world of a retail game that is ~650 WriteProcessMemory calls a frame, about
300 ms of an ~850 ms frame, though the bytes compared took ~30 ms: a call
costs hundreds of microseconds whatever its size.

Consecutive pieces the GPU changed are now read with one call, and the
consecutive ones the title lacked a GPU byte in are written with one call.
The merge rule per piece is unchanged (GPU bytes only where the copy moved
and the title does not have them; title bytes kept elsewhere), and a run
that cannot be read whole falls back to piece by piece, so unmapped pieces
are still skipped. Measured: copy-back ~350 ms -> ~130 ms a frame.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:34 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 16:04 UTC · original

Reviewed on Windows 11 / Rust 1.98.1, b3d8f95 on ffe310e.

  • cargo test -p victory-gpu: 51 passed, 1 ignored
  • cargo test -p victory-gpu --features diagnostics: 54 passed, 1 ignored
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean
  • cargo build --release -p victory: builds

The merge is byte-for-byte the old per-piece loop, which is the point of a change like this. I went through it against the code it replaces: the run test host(a, b) != &before[a - from..b - from] is exactly the old host != old per piece; merge's first chunking reproduces the piece cut including a run that begins mid-piece (first = PIECE - (start + from) % PIECE), and then steps by PIECE from there; synced comes out with the same pages because runs contain no gaps — coalescing happens only when run.1 == at, so a piece the GPU did not write splits the run instead of being passed over; write_mapped's source pointer title[a..].as_ptr() with length b - a matches the old title.as_ptr() with n; and the fallback keeps the old behaviour exactly — a piece that cannot be read is skipped, and put_back's recursion is bounded at depth two (first == to - from returns 0), so an unmapped page cannot recurse forever. The differs closure's borrows are all dead before put_back takes the mutable slice over the same region, so there is no overlap there.

One caveat, and it is the only thing I would change: the window a run opens. copy_back's own doc comment says the title's threads write these buffers while the GPU runs, and the merge reads, then merges, then writes back with WriteProcessMemory. At 64 KiB that window was a piece; a run can now be the whole range, so a title byte written between our read_into and our write_mapped is overwritten with the stale title byte we read. It is the same race at a different width, so this is not a regression — but the PR title says "safely" and the commit says title writes are preserved, and a reader will take that as absolute. Either a sentence in put_back naming the window, or a cap on a run (say 16 pieces, still 16 calls saved per 16 pieces) would make the claim true as written. I would take the sentence.

Smaller.

  • host::write_mapped splits at its own literal 64 << 10; PIECE is the same 64 KiB now named in shadow.rs. Same alignment in two places is fine until one moves.
  • Two cheap cases the new test does not cover: first == PIECE (a run that starts on a piece boundary, where the first chunk is a whole piece) and a range where nothing changed (merge returning no runs). Both are a few lines in the existing test and they are the two places the at == 0 { first } versus at + PIECE split could go wrong.
  • I cannot verify the cost claim — "tens to hundreds of microseconds" and "~650 pieces a frame" need the title running, which I do not have. The reasoning is sound and the shape is obviously better; I am only flagging that it is unmeasured here, not doubted.
**@⁠adamdotpy** on GitHub · 2026-11-24 16:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1124#issuecomment-7137068330) Reviewed on Windows 11 / Rust 1.98.1, `b3d8f95` on `ffe310e`. - `cargo test -p victory-gpu`: 51 passed, 1 ignored - `cargo test -p victory-gpu --features diagnostics`: 54 passed, 1 ignored - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean - `cargo build --release -p victory`: builds **The merge is byte-for-byte the old per-piece loop, which is the point of a change like this.** I went through it against the code it replaces: the run test `host(a, b) != &before[a - from..b - from]` is exactly the old `host != old` per piece; `merge`'s `first` chunking reproduces the piece cut including a run that begins mid-piece (`first = PIECE - (start + from) % PIECE`), and then steps by `PIECE` from there; `synced` comes out with the same pages because runs contain no gaps — coalescing happens only when `run.1 == at`, so a piece the GPU did not write splits the run instead of being passed over; `write_mapped`'s source pointer `title[a..].as_ptr()` with length `b - a` matches the old `title.as_ptr()` with `n`; and the fallback keeps the old behaviour exactly — a piece that cannot be read is skipped, and `put_back`'s recursion is bounded at depth two (`first == to - from` returns 0), so an unmapped page cannot recurse forever. The `differs` closure's borrows are all dead before `put_back` takes the mutable slice over the same region, so there is no overlap there. **One caveat, and it is the only thing I would change: the window a run opens.** `copy_back`'s own doc comment says the title's threads write these buffers while the GPU runs, and the merge reads, then merges, then writes back with `WriteProcessMemory`. At 64 KiB that window was a piece; a run can now be the whole range, so a title byte written between our `read_into` and our `write_mapped` is overwritten with the stale title byte we read. It is the same race at a different width, so this is not a regression — but the PR title says "safely" and the commit says title writes are preserved, and a reader will take that as absolute. Either a sentence in `put_back` naming the window, or a cap on a run (say 16 pieces, still 16 calls saved per 16 pieces) would make the claim true as written. I would take the sentence. **Smaller.** - `host::write_mapped` splits at its own literal `64 << 10`; `PIECE` is the same 64 KiB now named in `shadow.rs`. Same alignment in two places is fine until one moves. - Two cheap cases the new test does not cover: `first == PIECE` (a run that starts on a piece boundary, where the first chunk is a whole piece) and a range where nothing changed (`merge` returning no runs). Both are a few lines in the existing test and they are the two places the `at == 0 { first }` versus `at + PIECE` split could go wrong. - I cannot verify the cost claim — "tens to hundreds of microseconds" and "~650 pieces a frame" need the title running, which I do not have. The reasoning is sound and the shape is obviously better; I am only flagging that it is unmeasured here, not doubted.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 18:01 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b3d8f95: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1124#issuecomment-7140050450) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b3d8f95: `cargo test -p victory-gpu` passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:35 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/shadow.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1124#issuecomment-7148677727) <!-- victory-triage v1 head=b3d8f95cab449c95634fc5869c1ade3e2488daef verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-gpu/src/shadow.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
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/1124/head:github-bot/gh-1124'
git switch 'github-bot/gh-1124'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1124'
git switch 'github-bot/gh-1124'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1124'
git switch 'github-bot/gh-1124'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1124'
git switch 'main'
git merge --squash 'github-bot/gh-1124'
git switch 'main'
git merge --ff-only 'github-bot/gh-1124'
git switch 'main'
git merge 'github-bot/gh-1124'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1124
No description provided.