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
Contributor

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

@Marvisak Marvisak closed this Nov 24, 2026
chippy01 added a commit to chippy01/VIctory that referenced this pull request Nov 25, 2026
…sole wave

What was wrong: two results in halves were wrong in the upper host wave
(console lanes 32-63). Measured before this change on an RTX 3070 Ti
(driver 32.0.16.2124) and the Radeon iGPU of a Ryzen 9 7900X (RDNA2),
32 of 64 lanes wrong on both, while the Radeon's own wave64 form gave the
console's results:

- v_readfirstlane_b32 read the host wave's first lane, which in the upper
  half is console lane 32 or later: a first-lane read of the thread id gave
  32 in lanes 32-63, the console 0 (reported on Marvisak#2633 by justrealyme).
- A DPP row mask was applied by the host lane's row, 0 or 1: with
  row_mask:0xc, lanes 32-63 (the console's rows 2 and 3) kept their old
  value instead of taking the DPP result.

What changed:

- A first-lane read in halves is kept for a value proven the same in every
  lane of the console wave (halves::lane_uniform: values made from scalars,
  constants and such values, under conditions that are themselves alike;
  LDS reads, and buffer loads through a scalar descriptor, at alike
  addresses; the thread id shifted past the console wave) and in groups of
  32 threads or fewer, which one host wave holds whole. Any other is
  refused with its reason; one nothing uses costs nothing. The analysis is
  justrealyme's (#1), ported from fxceless/victory-fork
  (Marvisak#497 there). Added here: buffer loads (a typed buffer's element the
  compiler loads with a vector load, then reads the first lane of), the
  dwords a waterfall loop reads (each lane's own, so not alike), and the
  small-group exception.
- DPP row masks take the row from the console lane.
- The GPU tests share one dispatch helper.

Evidence, translate --list --wave32 over shaders.rpf, the executable,
XboxBvhBuilder.dll and the FidelityFX DLL: compute programs emitted
2,372 -> 2,369. The 3 no longer emitted read the first lane of a value the
analysis cannot show is alike (scattering_transmittance_system's cloud
multiple-scattering cache: a value joined from a branch on a per-lane
compare; two ptx_prerender light-atlas programs: a select on a mask made
from EXEC, in a loop), so in halves they could compute per-half values. 14
programs refused before give this reason first now; no other outcome
changes. The analysis as #1 has it refuses 12: the
small-group exception keeps 5 of them, buffer loads 4. No program of the title uses a DPP row mask
other than 0xF in halves, so that fix changes none of its translations.

Tests (emit::halves_tests):

- dpp_row_masks_name_the_console_rows_on_every_gpu (new): row_mask:0xc;
  0 of 64 lanes differ on both GPUs (32 before).
- first_lanes_kept_in_halves_match_the_console_on_every_gpu (new): the
  thread id's first lane in a group of 32 and l >> 6 in a group of 128;
  0 lanes differ on both GPUs.
- reads_across_32_lane_halves_are_refused_with_their_reason: the thread
  id's first lane is refused in a group of 64 and kept in a group of 32;
  an unused one costs nothing; a buffer dword loaded at an offset alike in
  every lane is kept, at the thread id's offset refused.
- the_test_programs_are_what_their_comments_say: every hand-encoded
  program checked against the disassembler.

Checks: cargo clippy --workspace --all-targets --locked -- -D warnings,
and with --all-features: clean. cargo xtask policy: ok. cargo test -p
victory-shader --lib -- --test-threads=1: 273 passed, 3 failed, the same 3
as on main on this NVIDIA machine (two stride_tests,
gathers_at_a_level_match_the_hardware);
heap_direct_texture_reads_mark_their_heap_index skipped as before. lower.rs
is 495,122 bytes.

Co-Authored-By: justrealyme <82029927+justrealyme@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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