Shadows: batch GPU copy-back writes safely #1124

Open
divyeshganta wants to merge 2 commits from upstream-pr/1124 into main
divyeshganta commented 2026-11-24 15:58:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by divyeshganta on 2026-11-24T15:58:12Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6585930148 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1124). Originally posted by [divyeshganta](https://github.com/divyeshganta) on 2026-11-24T15:58:12Z. --- 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.
DenRakEiw commented 2026-11-24 16:04:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T16:01:31Z.

Review: APPROVED


Tested merged cleanly onto main (ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP (VICTORY_D3D12_WARP=1):

  • cargo test -p victory-gpu (RTX 5090): 51 passed, 0 failed, 1 ignored (including shadow::tests::put_back_writes_joined_runs_of_pieces_the_gpu_changed)
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu (WARP): 51 passed, 0 failed, 1 ignored
  • cargo clippy -p victory-gpu --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean batched GPU copy-back write handling, verified on Blackwell hardware and WARP. Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6042814296 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1124#pullrequestreview-6042814296). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T16:01:31Z. **Review:** APPROVED --- Tested merged cleanly onto `main` (`ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC) with **NVIDIA GeForce RTX 5090** (Blackwell) and **WARP** (`VICTORY_D3D12_WARP=1`): - `cargo test -p victory-gpu` (RTX 5090): **51 passed, 0 failed, 1 ignored** (including `shadow::tests::put_back_writes_joined_runs_of_pieces_the_gpu_changed`) - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu` (WARP): **51 passed, 0 failed, 1 ignored** - `cargo clippy -p victory-gpu --all-targets`: **0 warnings** - `cargo xtask policy`: ok Clean batched GPU copy-back write handling, verified on Blackwell hardware and WARP. Approving. AI-assisted (Gemini 4 Neon).
adamdotpy commented 2026-11-24 16:05:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T16:04:11Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7137068330 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1124#issuecomment-7137068330). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T16:04:11Z. --- 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.
Zipettte commented 2026-11-24 18:06:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T18:01:39Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140050450 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1124#issuecomment-7140050450). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T18:01:39Z. --- 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)._
Marvisak commented 2026-11-25 01:08:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:35:51Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148677727 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1124#issuecomment-7148677727). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:35:51Z. --- <!-- 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 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/1124:upstream-pr/1124'
git switch 'upstream-pr/1124'

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/1124'
git switch 'upstream-pr/1124'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1124'
git switch 'upstream-pr/1124'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1124'
git switch 'main'
git merge --squash 'upstream-pr/1124'
git switch 'main'
git merge --ff-only 'upstream-pr/1124'
git switch 'main'
git merge 'upstream-pr/1124'
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
Marvisak/VIctory!1124
No description provided.