GPU: keep what shaders may write by page, in reused page buffers #497

Closed
github-bot wants to merge 2 commits from gh-pr/497/head into gh-pr/497/base
Owner

@⁠wbkepler on GitHub · 2026-11-24 04:08 UTC · original
wbkepler:gpu/snapshot-pages → main · +326 −80 · 2 files · 2 commits · closed without merging

Stacked on #464. This branch contains #464's commit; only the second commit is new. It is independent of #490.

Problem

Before a submission runs, Shadow::writing keeps the bytes of each range its shaders may write, so copy_back can tell what the GPU changed. Until now that was one buffer per host copy, grown to the bounding range of every range declared in the submission. The world declares overlapping views of its 174 MB heap dozens of times per submission. Each growth:

  • took a buffer from a pool of 8 largest buffers, or a fresh one (page faults on first touch) when none was free and big enough, and
  • copied the whole old snapshot into it.

In the world, temporary fault counters around writing showed 98K–450K page faults per frame (#464 runs).

Change

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

  • Snapshot by title page. It is kept per title page in 64 KiB buffers from a pool of up to 4,096 buffers (256 MiB). Each page's part grows as ranges in it are declared. As before, newly covered bytes, and gaps between ranges, are taken from the copy as it is at that moment. The semantics match the old bounding-range snapshot, at page granularity.
  • Early return for repeated ranges. The merged spans taken so far are kept, so a range declared again (or inside one already taken) returns at once without walking its pages.
  • Copy-back by page. copy_back compares page by page, as it did per 64 KiB piece, and checks that the page's snapshot covers the piece. Its 64 KiB scratch buffer comes from the same pool instead of a fresh vec![0; 64 KiB] per call.
  • Pages back to the pool. All page buffers return to the pool when the copy's last pending range is copied back, as before.

Measured

Diagnostics build, RTX 5090, same save, start screen to world, copystat world windows. The snapshot faults are a temporary counter around writing.

per world frame #464 + this
page faults in writing (temporary counter) 98K–337K 26K–61K
preparing 210–350 ms 172–404 ms

Example world lines:

  • #464: 2.6 fps, 377.7 ms/frame (210.1 preparing: copies 103.4 ms ...), snapshot faults 142,493
  • with this: 2.3 fps, 444.4 ms/frame (172.5 preparing: copies 105.6 ms ...), 1544 draws/frame, snapshot faults 26,271

The fault counter clearly drops. Preparing time varies with the scene too much to attribute a precise saving.

Most of the remaining faults come from refreshes, copy-back reads of title memory, and texture refreshes, not from snapshots. That is a separate follow-up.

Tests

New tests, run with a host device and skipped without one:

  • shadow::tests::copy_back_takes_only_what_the_gpu_wrote: two overlapping ranges over three unaligned title pages, with a refresh between the declarations. Only the bytes the GPU changed go back. A title write made meanwhile stays, and the copy takes it. The page buffers return to the pool.
  • shadow::tests::declaring_a_range_again_keeps_its_snapshot: a range declared again after a GPU write keeps its first snapshot, so the write is still found and copied back.

Gate:

  • cargo fmt --check (shadow.rs): passes.
  • cargo clippy --workspace --all-targets and cargo clippy -p victory --features diagnostics --all-targets: no new warnings.
  • cargo test --workspace: passes.
  • cargo xtask policy: passes.

🤖 Generated with Claude Code

**@⁠wbkepler** on GitHub · 2026-11-24 04:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/497) `wbkepler:gpu/snapshot-pages` → `main` · +326 −80 · 2 files · 2 commits · closed without merging **Stacked on #464.** This branch contains #464's commit; only the second commit is new. It is independent of #490. ## Problem Before a submission runs, `Shadow::writing` keeps the bytes of each range its shaders may write, so `copy_back` can tell what the GPU changed. Until now that was one buffer per host copy, grown to the bounding range of every range declared in the submission. The world declares overlapping views of its 174 MB heap dozens of times per submission. Each growth: - took a buffer from a pool of 8 largest buffers, or a fresh one (page faults on first touch) when none was free and big enough, and - copied the whole old snapshot into it. In the world, temporary fault counters around `writing` showed **98K–450K page faults per frame** (#464 runs). ## Change `crates/victory-gpu/src/shadow.rs` only: - **Snapshot by title page.** It is kept per title page in 64 KiB buffers from a pool of up to 4,096 buffers (256 MiB). Each page's part grows as ranges in it are declared. As before, newly covered bytes, and gaps between ranges, are taken from the copy as it is at that moment. The semantics match the old bounding-range snapshot, at page granularity. - **Early return for repeated ranges.** The merged spans taken so far are kept, so a range declared again (or inside one already taken) returns at once without walking its pages. - **Copy-back by page.** `copy_back` compares page by page, as it did per 64 KiB piece, and checks that the page's snapshot covers the piece. Its 64 KiB scratch buffer comes from the same pool instead of a fresh `vec![0; 64 KiB]` per call. - **Pages back to the pool.** All page buffers return to the pool when the copy's last pending range is copied back, as before. ## Measured Diagnostics build, RTX 5090, same save, start screen to world, `copystat` world windows. The snapshot faults are a temporary counter around `writing`. | per world frame | #464 | + this | |---|---|---| | page faults in `writing` (temporary counter) | 98K–337K | 26K–61K | | preparing | 210–350 ms | 172–404 ms | Example world lines: - #464: `2.6 fps, 377.7 ms/frame (210.1 preparing: copies 103.4 ms ...)`, snapshot faults 142,493 - with this: `2.3 fps, 444.4 ms/frame (172.5 preparing: copies 105.6 ms ...), 1544 draws/frame`, snapshot faults 26,271 The fault counter clearly drops. Preparing time varies with the scene too much to attribute a precise saving. Most of the remaining faults come from refreshes, copy-back reads of title memory, and texture refreshes, not from snapshots. That is a separate follow-up. ## Tests New tests, run with a host device and skipped without one: - `shadow::tests::copy_back_takes_only_what_the_gpu_wrote`: two overlapping ranges over three unaligned title pages, with a refresh between the declarations. Only the bytes the GPU changed go back. A title write made meanwhile stays, and the copy takes it. The page buffers return to the pool. - `shadow::tests::declaring_a_range_again_keeps_its_snapshot`: a range declared again after a GPU write keeps its first snapshot, so the write is still found and copied back. Gate: - `cargo fmt --check` (shadow.rs): passes. - `cargo clippy --workspace --all-targets` and `cargo clippy -p victory --features diagnostics --all-targets`: no new warnings. - `cargo test --workspace`: passes. - `cargo xtask policy`: passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A copy of view memory (physical-page views, which cannot be
write-watched) guards each 64 KiB block it took read-only. A block that
is not mapped (a placeholder between views of a GPU heap) cannot be made
read-only, so it never became clean: every asking reported it written
again, and the copy read it through the per-page fallback, whose
failing kernel reads cost hundreds of ms per world frame.

Note such blocks absent (only after VirtualProtect fails and the block is
not committed). An absent block is not reported again until
title_memory_remapped, which the runtime calls after mapping or
unmapping views, clears it. The copy keeps the zeros it took.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Before a submission runs, the bytes of each range its shaders write are
kept to tell afterwards what the GPU changed. They were one buffer per
copy, grown to the bounding range of every range the submission
declared: each growth took a buffer from a pool of eight (a fresh one,
page faults and all, when none was free and big enough) and copied the
whole old snapshot into it.

Keep them per title page instead, in 64 KiB buffers from a pool of up to
4,096, each page's part grown as ranges in it are declared (newly covered
bytes taken from the copy as it is then, as before). A range inside one
already taken returns at once. Copy-back compares page by page as it did
by 64 KiB piece, with its scratch page from the same pool.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:06:10 +00:00
github-bot closed this pull request 2026-11-26 04:06:10 +00:00
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-24 16:28 UTC · original

From a tester working with Claude (AI-assisted); measured on my own machine (i7-13700KF, RTX 4070 Super). No game data involved.

A follow-up on top of this PR (and #440, plus our #1215): the page snapshots and the copy-back comparison run on a persistent pool of victory-copy-N threads, so a world submission that snapshots hundreds of MB no longer does it on one thread. The pool is created once (creating threads per call crashed in the title's thread-attach callbacks).

Branch, one commit on top of #440 + #497 + #1215: https://github.com/LooperSalty/VIctory/tree/perf/copy-pool-stacked (commit 8cce780). cargo test -p victory-gpu -p victory-shader is green on it.

Measured, world stage, DRED off: snapshot about 50 ms, copy back about 73 ms per frame; with the rest of our perf set the world runs at 2.3-2.5 fps (it was 0.2).

**@⁠LooperSalty** on GitHub · 2026-11-24 16:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/497#issuecomment-7137734780) From a tester working with Claude (AI-assisted); measured on my own machine (i7-13700KF, RTX 4070 Super). No game data involved. A follow-up on top of this PR (and #440, plus our #1215): the page snapshots and the copy-back comparison run on a persistent pool of `victory-copy-N` threads, so a world submission that snapshots hundreds of MB no longer does it on one thread. The pool is created once (creating threads per call crashed in the title's thread-attach callbacks). Branch, one commit on top of #440 + #497 + #1215: https://github.com/LooperSalty/VIctory/tree/perf/copy-pool-stacked (commit 8cce780). `cargo test -p victory-gpu -p victory-shader` is green on it. Measured, world stage, DRED off: snapshot about 50 ms, copy back about 73 ms per frame; with the rest of our perf set the world runs at 2.3-2.5 fps (it was 0.2).
Author
Owner

@⁠wbkepler on GitHub · 2026-11-24 20:51 UTC · original

Rebased onto current main (ffe310e) as 92e1d4d, restacked on the rebased #464 (9dd1507); no content conflicts after restacking. Gate clean.

Thanks @⁠LooperSalty for the persistent copy-thread-pool follow-up branch built on top of this PR. Once this one lands it can be proposed as its own PR (nothing imported here).

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

**@⁠wbkepler** on GitHub · 2026-11-24 20:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/497#issuecomment-7144157125) Rebased onto current main (`ffe310e`) as `92e1d4d`, restacked on the rebased #464 (`9dd1507`); no content conflicts after restacking. Gate clean. Thanks @⁠LooperSalty for the persistent copy-thread-pool follow-up branch built on top of this PR. Once this one lands it can be proposed as its own PR (nothing imported here). _AI-assisted (Claude Code); checked on Windows 11 / RTX 5090._

Pull request closed

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!497
No description provided.