Skip to content

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

Closed
wbkepler wants to merge 2 commits into
Marvisak:mainfrom
wbkepler:gpu/snapshot-pages
Closed

wbkepler wants to merge 2 commits into
Marvisak:mainfrom
wbkepler:gpu/snapshot-pages

Conversation

@wbkepler

Copy link
Copy Markdown

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:

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

@LooperSalty

Copy link
Copy Markdown

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

wbkepler and others added 2 commits November 24, 2026 21:48
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@wbkepler

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants