Shadows: put a run of GPU-written pieces back with one kernel call (fix on a fork) #918

Open
opened 2026-11-26 04:14:09 +00:00 by github-bot · 3 comments
Owner

@⁠cybx on GitHub · 2026-11-24 14:09 UTC · original

Problem

After a submission runs, copy_back compares the host copy of every GPU-written range with the snapshot taken before it, piece by piece (64 KiB), and puts each differing piece back into title memory with its own ReadProcessMemory/WriteProcessMemory call. In a busy world frame of the retail title this was thousands of kernel calls a frame: copy-back took ~350–555 ms of a ~830–1030 ms frame on a Ryzen 7 9800X3D / RTX 5070 Ti.

Change

crates/victory-gpu/src/shadow.rs:

  • merge(title, host, old, first) (pure) does the byte merge that was done per piece: the GPU's bytes (where the host copy differs from the snapshot) go into a local copy of title memory where it lacks them, title memory goes into the host copy, and it returns the joined runs of pieces where title memory lacked a GPU byte.
  • put_back writes each of those runs back with one kernel call. If part of a run is not mapped, it falls back to piece by piece for that run.
  • Pieces where title memory already has the GPU's bytes are not written, so title writes made while the GPU ran are kept, as before. What is compared, when, and the order against refreshes (the submitted lock) are unchanged.

Verification

  • New test put_back_writes_joined_runs_of_pieces_the_gpu_changed: synthetic buffers of three pieces plus a short first one. The GPU wrote a byte in each; the title already has one of them and wrote its own bytes in two pieces while the GPU ran. It checks the joined runs returned and that title memory and the host copy both end with the GPU's bytes and the title's.
  • cargo clippy -p victory-gpu --all-targets [--all-features] -- -D warnings clean, cargo xtask policy ok, cargo test -p victory-gpu: all pass except frame::tests::waits_end_on_ticks_and_count_frames, which also fails on main here under load (timing).
  • Measured on the retail title, same scene, same machine, A/B of this commit on a local branch (main plus several open PRs) against the same branch without it: copy-back 555 → 133 ms a frame, frame time ~1033 → ~750 ms. (Diagnostic counters were local; nothing of them is in this change.)

Branch: https://github.com/cybx/VIctory/tree/shadow-batched-copy-back (one commit on ffe310e). Compare: https://github.com/Marvisak/VIctory/compare/main...cybx:VIctory:shadow-batched-copy-back

No game files were used in the tests or the change. Written with the help of an AI assistant (Claude); I reviewed and tested it.

**@⁠cybx** on GitHub · 2026-11-24 14:09 UTC · [original](https://github.com/Marvisak/VIctory/issues/918) ## Problem After a submission runs, `copy_back` compares the host copy of every GPU-written range with the snapshot taken before it, piece by piece (64 KiB), and puts each differing piece back into title memory with its own `ReadProcessMemory`/`WriteProcessMemory` call. In a busy world frame of the retail title this was thousands of kernel calls a frame: copy-back took ~350–555 ms of a ~830–1030 ms frame on a Ryzen 7 9800X3D / RTX 5070 Ti. ## Change `crates/victory-gpu/src/shadow.rs`: - `merge(title, host, old, first)` (pure) does the byte merge that was done per piece: the GPU's bytes (where the host copy differs from the snapshot) go into a local copy of title memory where it lacks them, title memory goes into the host copy, and it returns the joined runs of pieces where title memory lacked a GPU byte. - `put_back` writes each of those runs back with one kernel call. If part of a run is not mapped, it falls back to piece by piece for that run. - Pieces where title memory already has the GPU's bytes are not written, so title writes made while the GPU ran are kept, as before. What is compared, when, and the order against refreshes (the `submitted` lock) are unchanged. ## Verification - New test `put_back_writes_joined_runs_of_pieces_the_gpu_changed`: synthetic buffers of three pieces plus a short first one. The GPU wrote a byte in each; the title already has one of them and wrote its own bytes in two pieces while the GPU ran. It checks the joined runs returned and that title memory and the host copy both end with the GPU's bytes and the title's. - `cargo clippy -p victory-gpu --all-targets [--all-features] -- -D warnings` clean, `cargo xtask policy` ok, `cargo test -p victory-gpu`: all pass except `frame::tests::waits_end_on_ticks_and_count_frames`, which also fails on `main` here under load (timing). - Measured on the retail title, same scene, same machine, A/B of this commit on a local branch (main plus several open PRs) against the same branch without it: copy-back 555 → 133 ms a frame, frame time ~1033 → ~750 ms. (Diagnostic counters were local; nothing of them is in this change.) Branch: https://github.com/cybx/VIctory/tree/shadow-batched-copy-back (one commit on `ffe310e`). Compare: https://github.com/Marvisak/VIctory/compare/main...cybx:VIctory:shadow-batched-copy-back No game files were used in the tests or the change. Written with the help of an AI assistant (Claude); I reviewed and tested it.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:09 +00:00
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-24 14:15 UTC · original

I found a fallback regression on 4c0d57d with a synthetic native Windows copy-back test. The range is 8 bytes: the last four bytes of one 64 KiB piece and the first four of the next. I marked one side PAGE_NOACCESS and repeated with the other side protected.

On ffe310e, both cases wrote the four readable bytes back. On 4c0d57d, both wrote zero bytes and the readable word stayed at its original value. The control with both sides readable wrote all eight bytes on both revisions.

In put_back, to - from <= PIECE can be true even when the range spans two pieces. Computing the first piece’s length before the read and returning early only when first == to - from restores the piece-by-piece fallback. With that change, all four shadow tests passed on WARP and RTX 4070 Ti. Diagnostics Clippy passed with warnings denied.

AI-assisted. Synthetic buffers only, with no gameplay or performance measurements.

**@⁠DrDarkDK** on GitHub · 2026-11-24 14:15 UTC · [original](https://github.com/Marvisak/VIctory/issues/918#issuecomment-7134073733) I found a fallback regression on `4c0d57d` with a synthetic native Windows copy-back test. The range is 8 bytes: the last four bytes of one 64 KiB piece and the first four of the next. I marked one side `PAGE_NOACCESS` and repeated with the other side protected. On `ffe310e`, both cases wrote the four readable bytes back. On `4c0d57d`, both wrote zero bytes and the readable word stayed at its original value. The control with both sides readable wrote all eight bytes on both revisions. In `put_back`, `to - from <= PIECE` can be true even when the range spans two pieces. Computing the first piece’s length before the read and returning early only when `first == to - from` restores the piece-by-piece fallback. With that change, all four shadow tests passed on WARP and RTX 4070 Ti. Diagnostics Clippy passed with warnings denied. AI-assisted. Synthetic buffers only, with no gameplay or performance measurements.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-24 19:45 UTC · original

@⁠cybx your fix for this issue is now in a pull request, #1610, with seven other fixes from contributors' forks: the branch is merged as it is, with your commit(s) and your name unchanged, onto main ffe310e. Clippy (both runs) and cargo test --workspace pass on the result. If you would rather open your own pull request for this one, say so and I will take it out of #1610. AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-24 19:45 UTC · [original](https://github.com/Marvisak/VIctory/issues/918#issuecomment-7142548179) @⁠cybx your fix for this issue is now in a pull request, #1610, with seven other fixes from contributors' forks: the branch is merged as it is, with your commit(s) and your name unchanged, onto `main` `ffe310e`. Clippy (both runs) and `cargo test --workspace` pass on the result. If you would rather open your own pull request for this one, say so and I will take it out of #1610. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:54 UTC · original

Automated triage for the maintainers.

main changed the copy-back after this branch was written: since cd94d35 and 940d32f, translated shaders mark the 64 KiB title pages they write through views, and after a submission only marked pages are compared and written back, one page at a time (copy_back_marked in crates/victory-gpu/src/shadow.rs). The branch (put_back, on ffe310e) would need rebasing onto that, and the cross-piece fallback regression found above (to - from <= PIECE for a range spanning two pieces) needs the fix described. The bundle that carried it, #1610, was closed without being merged; pull requests are open to everyone, so a rebased pull request of your own is the way forward. A measurement of copy-back time on current main (see #400, where world frame time is tracked) would show what is left to gain.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:54 UTC · [original](https://github.com/Marvisak/VIctory/issues/918#issuecomment-7155275951) <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-24T19:45:32Z at=2026-11-25T06:54:04Z --> Automated triage for the maintainers. `main` changed the copy-back after this branch was written: since cd94d35 and 940d32f, translated shaders mark the 64 KiB title pages they write through views, and after a submission only marked pages are compared and written back, one page at a time (`copy_back_marked` in `crates/victory-gpu/src/shadow.rs`). The branch (`put_back`, on `ffe310e`) would need rebasing onto that, and the cross-piece fallback regression found above (`to - from <= PIECE` for a range spanning two pieces) needs the fix described. The bundle that carried it, #1610, was closed without being merged; pull requests are open to everyone, so a rebased pull request of your own is the way forward. A measurement of copy-back time on current `main` (see #400, where world frame time is tracked) would show what is left to gain. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#918
No description provided.