Skip to content

GPU: keep only the bytes shaders may write, each once per submission - #1267

Open
TG0000 wants to merge 3 commits into
Marvisak:mainfrom
TG0000:perf/declared-snapshots
Open

TG0000 wants to merge 3 commits into
Marvisak:mainfrom
TG0000:perf/declared-snapshots

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Why

Before a submission's shaders run, the copy of a buffer is kept so that copy-back can tell what the GPU wrote. The snapshot covers everything from the first declared range to the last. World frames declare ranges that are far apart, and overlapping ones, in a 174 MB heap dozens of times, so most of what is copied is never written, and each new range copies the whole union again.

What

  • Each declared byte is kept once per submission, and nothing between the ranges.
  • All snapshots of a submission share one block of memory that is reused.
  • A copy that is dropped while it still has snapshots gives them up.

Measured

Time of a looped world frame's submissions (preparation, GPU, copy-back), all ways in one launch (RX 6800, Windows; 8 frames, each repeated with every way; a frame has 36 submissions). This is not frame time, and nothing here is an fps claim. One launch, one area of the world, in a build of main (ffe310e) with measuring tools and other local changes, the same in every row; about 1,400 of 5,000 draws a frame are still dropped there.

submissions, ms per frame (about 285 of it GPU in every row)
copies as on main 1606
this change with four other copy changes of mine (not all open) 551
the same four without this change +267 (244 to 351) against the row above

The last row is the median of the paired differences, the range is over the 8 frames. A control (the same switches in another order) differed by 9 ms.

Other ways to make the same snapshots cheaper, as my own ports into that tool, 2 frames only, so a hint and not a result: with this change 307 ms, with #440's in its place 464 ms, with #991's 469 ms. These are ports of other people's pull requests; their authors' code may do better.

A model (only the memory copies of each way, with 64 distinct ranges declared 4 times each in a 174 MB block, not the code itself): main 785.5 MB, 68 ms; #1186 at c1dd5b1 8,363.5 MB, 658 ms (it lost the early return for a range the snapshot already covers); this change 16.0 MB, 2.6 ms.

Tests

  • Unit tests (shadow::snapshot_tests): each declared byte once; only changed declared bytes come back; a dropped copy leaves no snapshot; two copies in one submission do not share theirs. The last two fail without the Drop.
  • cargo test --workspace --locked, clippy with -D warnings with and without --all-features on Windows, clippy on macOS: pass at 4cefaac (346 tests). frame::tests::waits_end_on_ticks_and_count_frames measures time: it failed in two runs while a game ran beside it, passes 10 of 10 alone on main and here, and the run counted above is clean.
  • One recorded frame (28 submissions, 24 of them with shader writes) replayed under main's way and this one: title memory and the copies are byte for byte equal in every run, with the writes as recorded and with random ones added (shader writes, undeclared host writes, title writes before copy-back). Seven sets (no change, each of five, all five), 504 runs, 34.7 GB compared, with synthetic overlapping declared ranges too: 0 differences; 0 snapshots left over. Three deliberate faults put into this change are all found with the overlapping ranges (two of three without). The set covers only one frame, and title writes come before, not during, the copy-back.

Not handled on purpose

A list reset while its submission is in flight keeps its snapshots until the copy is dropped. execute returns only after copy-back, inside the title's ExecuteCommandLists, so such a reset is already an error and ends in fail(); dropping parts from reset() could take those of another submission that has the same range.

Overlapping copy-backs can replay an earlier CPU edit; that is so on main and unchanged here (#1186 is about it).

Not verified

  • NVIDIA and Intel GPUs; Linux and Proton.
  • Frame time and fps in the game.
  • Title writes during copy-back (only before it), and other frames than the recorded one.

Conflicts

shadow.rs conflicts with #440, #991, #1124 and #1186; the other open pull requests that touch this code (#943, #1010, #1024, #1215) merge cleanly.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

The snapshot that tells what the GPU wrote covered a buffer copy from the
first declared range to the last, in buffers the submission grew, copied
and freed. Each declared byte is now kept once, in one block of memory
that is reused: in a looped world frame this saves about 270 ms of
submission work. A copy dropped with a range not copied back gives its
part up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@justlovett0

Copy link
Copy Markdown

Verified on Win10 21H2 / RTX 3080 (driver 621.24) stacked with #986 and #1260: the 174 MB heap-union re-copy was a large part of the ~400 ms/frame copy cost; with this the world runs ~20 fps with no device removal. Combined result on #986. AI use: Qwen Code (model name not disclosed); user-confirmed.

@ChruutvoLuzi

ChruutvoLuzi commented Nov 24, 2026 •

Copy link
Copy Markdown

Merged at 4cefaac onto r4tedx integration/testers update 9 (94e0ea5), which conflicts in shadow.rs, and measured on an RTX 4090 (driver 621.01, Windows 11 26200).

The conflict. The tester tree has #261's a2243ad ("copy-back puts back only what shaders wrote, in two more cases"), written for the single grown snapshot. It does two things:

  • It takes again the gap bytes a later range's refresh brings in. With this PR the snapshot holds no gaps, so I left that part out.
  • After a piece is copied back, it writes the title's bytes into the snapshot too, so an overlapping range copied back later doesn't put the copy's older bytes over a title write made in between. With this PR's parts, kept returns a read-only slice, so I added a small store that writes the title's bytes into the parts covering the piece, right after the loop that compares them.

Without store, that tree's test overlapping_ranges_keep_the_titles_newer_bytes fails; with it, it passes. main has that gap too, without this PR: the fix is #261 (a2243ad), which isn't merged. If #261 lands first, this PR will need the same store. That tree's two tests also need this PR's ONE lock now that the snapshots are shared (pub(super) here). With both, all six shadow tests and the 110 victory-gpu tests pass.

In the title: diagnostics build, DRED off, same settings and save for both runs. Frames are binned by draws per frame because the runs weren't in the same scene at the same time:

draws per frame update 9 update 9 + this PR
2,500 to 3,500 1,183 ms (8 frames) 842 ms (53 frames)
4,500 to 5,500 1,327 ms (69 frames) 1,133 ms (2 frames)

Most of the gain is copy-back, about 190 ms down to about 125 ms per frame. No device removal in either run.

AI-assisted (Claude Code).

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks both for merging and measuring this, and @ChruutvoLuzi for tracing the store gap: noted, if #261 lands first I will rebase and add the same write into the parts covering the piece (with overlapping_ranges_keep_the_titles_newer_bytes as the check), and the two tests under the shared lock.

@BitWuehler

Copy link
Copy Markdown

Linux + Proton + NVIDIA RTX 5080 runtime data point (filling in the "NVIDIA / Linux and Proton / in-game frame time" items in Not verified).

Note on attribution: I stacked #1267 (4cefaac) together with #1260 (incl. #986, replacing #206) and a local MIN_DYNAMIC = 1 << 18 (#747) bump on top of #994 + #177 + #454, so the frame-time/draw deltas reflect that combined step rather than #1267 in isolation.

Setup

  • Hardware / OS: Ryzen 7 9800X3D, 60 GB RAM, NVIDIA GeForce RTX 5080 16 GB (driver 620.73.04), CachyOS (Linux 7.3.6-1-cachyos).
  • Runtime: GE-Proton11-14 via umu-run (VKD3D_DISABLE_EXTENSIONS=VK_NV_raw_access_chains).
  • Title / Build: retail 1.0.2.1, --console series-x, diag build (--features diagnostics), VICTORY_D3D12_DRED=1, VICTORY_DEBUG_PS=1, VICTORY_SHOW_TARGET=2560x1440-f28-3, VICTORY_NO_WATCHDOG=1.

A/B comparison on the same machine & setup (draws >= 100 world intervals)

Metric #994 + #177 + #454 + #206 (2^16 dyn, n = 68) #994 + #177 + #454 + #1260 + #1267 (2^18 dyn, n = 30)
Median frame time / fps 2,867.0 ms / 0.30 fps (max 2.50) 2,016.6 ms / 0.50 fps (max 5.00)
Draws / frame (median / max) 1,076 / 4,639 2,076 / 5,662
copy-back (median ms / MB) 228.8 ms / 15.2 MB 220.8 ms / 24.6 MB
copies prep (median ms / MB) 1,565.3 ms / 112.6 MB 1,235.5 ms / 301.9 MB
GPU faults / device removals / Xids 0 / 0 / 0 0 / 0 / 0

Observations

  • Copy-back scales well: even with +93% more draws/frame and +62% more copy-back MB/frame (15.2 -> 24.6 MB), median copy-back stayed flat (228.8 -> 220.8 ms) and median frame time dropped by ~30% with noticeably richer debug geometry on screen and no visual/snapshot regressions.
  • Remaining Linux/Proton bottleneck: copies (input upload/refreshes under Wine) still dominates (~1.24 s median, peaking at 3,842.8 ms with 6,656 refreshes / 4,805.7 MB, e.g. the 174 MB 0x1a3f0000+0xae00000 range refreshed 10.5x/frame), which matches audio still stuttering/cutting out later in the run.
  • End of run: active world logging ran ~4.5 min (to frame=1443), then hit a silent title hang (draws=26, black_frames=17; no 0x887A0006, no crashes.log entry because VICTORY_NO_WATCHDOG=1).

AI-assisted log analysis and summary; built and run locally on my own retail copy, no game files or raw logs attached.

@marquezinii

Copy link
Copy Markdown
Contributor

Measured with the benchmark in #1743, which replays a world frame's refreshes, snapshots and copy-back on a made-up 174 MB heap (36 submissions, 24 overlapping declared ranges each). This PR was merged alone onto ffe310e. Host: RTX 5070, release build, 6 runs of each build.

median frame snapshots copy-back
main 2472–4282 ms 1544–2514 ms 830–1535 ms
this PR 798–1155 ms 221–291 ms 490–758 ms
  • It was the only one of the 15 open copy PRs I ran whose frame came out clearly below main's range.
  • It passes the benchmark's checks: the GPU's bytes reach title memory, and the title's writes during the run are kept.
  • With it, copy-back is the largest part left. GPU: reuse unchanged page checks across shadow copy-back ranges #124 goes after that part but conflicts with this PR.

Main's copy-back of each 64 KiB piece once per submission, writes that stay
in the copy and the copy counters are kept; the snapshot segments become the
parts of this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Merged main in (fast-forward): its compared-once set, kept writes and counters stay, its snapshot segments become this PR's parts.
Main now snapshots declared ranges only, so the numbers in the description (ffe310e) are too large for it; what is left is copying a segment again when a range grows it.
Benchmark of #1743 on both (Ryzen 9 5900X, RX 6800, release, 3 launches each): median frame 770-784 ms on main, 512-583 ms here; snapshots 384-524 ms against 194-264 ms; copy-back about the same; its checks pass.
Thanks @marquezinii for the benchmark and the measurement. The frame time in the game was not measured again.
victory-gpu 82 and victory-shader 209 tests pass on Windows (RX 6800), clippy clean. AI-assisted (Claude Code).

1277 perf/view-memory-cache : DROP (reported only, not closed)

No local ref. Main's new change tracking asks about the memory once per host copy (watch::track), not at each check: the cost this PR removes is gone, and the function it changes (guard) no longer exists.
Left over in main: xmem::unmap still reports the remapping before it unmaps. A take in between can guard, and mark as taken, a page that is going away. map() reports again and opens it, so I see no wrong result from it (by reading, not tested). Not worth a PR without a failing test.
Draft comment (only if the user wants it closed; the review has no open question):
Main now asks whether memory is a view once per host copy (c75fdba), so what this PR saves is no longer spent. Thanks @adamdotpy for the careful review.
I found no case on the new code where the unmapping order gives a wrong result (by reading only). AI-assisted (Claude Code).

1010 perf/user-mode-title-copies : PUSH

local ref: prready/1010 (caa937c), GitHub head 79c9b93
git push fork prready/1010:perf/user-mode-title-copies
Resolved: main's per-step diagnostics timer and this PR's run of fills timed as one, both kept (the timer takes its own clock, in diagnostics builds only); module list.
Checks: Mac lint 0. Windows tests gpu 87 (title_copy and fills tests among them), shader 209, 0 failed. Benchmark not run again.
Draft comment:
Merged main in (fast-forward); both the per-step timer of main and the run of fills timed as one are kept.
victory-gpu 87 and victory-shader 209 tests pass on Windows (RX 6800), clippy clean with and without all features. The microbenchmark was not run again and I still claim no frame-time gain.
Thanks @adnanoos for the run in the world. AI-assisted (Claude Code).

139 perf/shader-address-sums : PUSH

local ref: prready/139 (90cf2f6), GitHub head 697c4bc
git push fork prready/139:perf/shader-address-sums
Resolved: took main's tracer whole; the cache sits in a RefCell so its &self stays, and is emptied after the spilled parameters change the origins. One test expected a refusal that main turned into title memory: expectation changed, and the same test file passes on main without the cache.
Checks: Mac lint 0, Mac tests formats 23, shader 118 + 3. Windows tests gpu 78, shader 209 + 3, 0 failed. Benchmark and census not run again.
Draft comment:
Merged main in (fast-forward). Main's tracer is unchanged; the sums are cached behind a RefCell and dropped when the spilled parameters change the origins.
One test follows main's rule that an address from no root parameter is title memory; the same file passes on main without the cache.
victory-shader 209 + 3 tests pass on Windows (RX 6800). The benchmark was not run again. AI-assisted (Claude Code).

999 perf/shader-lane-condition-search : MERGED, CENSUS BEFORE THE PUSH

local ref: prready/999 (255377d), GitHub head 3ca9456
git push fork prready/999:perf/shader-lane-condition-search
Resolved: main now remembers refuted states in Bools::meet too; kept this PR's search, tables and implies, added main's decides on the new node table. The pass that finds all missing phis was REWRITTEN: it looks at main's live edges only and skips edges main calls never taken or dead.
Checks: Mac lint 0, Mac tests formats 23, shader 120. Windows tests gpu 78, shader 211, 0 failed.
Not done: the census against main. "None goes from translating to refused", the byte-for-byte check and all timings in the body are from ffe310e and the old phi pass. Do not repeat them for this head.
Draft comment (if pushed without the census):
Merged main in (fast-forward). Main's search now remembers failed states as well, so item 1 gains less than the table says.
The pass that finds every missing phi was rewritten: it follows main's live edges and skips the edges main calls never taken or dead.
victory-shader 211 tests pass on Windows (RX 6800), clippy clean. The corpus comparison and the timings in the description are from ffe310e and were not run again on this head.
AI-assisted (Claude Code).

No push needed (mergeable, no failing check, no open question)

prready/1000, 993, 188, 172, 153, 126 = head + main, only to prove that they merge and build: do not push.
All six: clean merge, Mac lint 0, Mac tests pass (formats 23 to 25, shader 118 to 121). Their Windows-only tests (1000 and 153 gpu, 188 loader) were not run on the merge.
#1000: the two nits promised for its next change (a message when the test is skipped, one line on the order) are still open.
#124 is closed on GitHub: nothing done.

Worktree: C:\Users\theo_\victory-prs (detached at origin/main).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This pull request does not merge cleanly into main. It was previously marked merge candidate; main has moved since and the two now conflict. Conflicting files: crates/victory-gpu/src/shadow.rs.

It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 25, 2026
… per submission

Snapshots of shader-written ranges shared one reused block per
submission (SNAPSHOTS) instead of a 2 GiB POOL of per-copy segments:
each declared byte is kept once, nothing between ranges. Ported the
update_busy hunk (title writes under busy ranges) to the new Part
representation; conflicts none. From TG0000/perf/declared-snapshots.
The bytes a refresh puts into a busy range go into the parts kept for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 25, 2026
… queue\n\nUpstream cd94d35+940d32f: translated shaders mark the 64 KiB pages they\nwrite through unordered buffer views (tables, heap, root UAVs) in a\nper-copy bitmap; copy-backs compare/write only marked pages instead of\nsnapshotting+comparing declared GBs. Our live logs showed exactly that\nbottleneck (2.5-3.8 GB snapshots + 2.3-3.9 GB compares per frame).\n\nPort notes (no accuracy change, our faster paths kept):\n- Marvisak#1267 Part-based snapshots kept over upstream segments; writing()\ngains the marked fast path (arm, no snapshot) with our parallel fill on\nthe unmarked path; Drop merges snapshot-forget and mark give-back.\n- copy_back_marks runs before our parallel run_back loop (marks first,\nloop disarms — upstream order); back-loop destructuring unchanged.\n- copy_back gains upstream disarm; marked entries find no snapshot\nparts and skip, as designed.\n- Tests: kept buffer_states test (new() now returns Arc); ours +\nupstream marked tests updated to 3-arg writing(); fuzz/valu helpers\ngain view_marks fields per upstream pattern.\n- Cache safety verified: shader disk cache folders key on translator\nsources, pipeline names hash root-sig+code, prebuilt tag covers\nSOURCE_HASH — old no-marks entries cannot be served. Marvisak#1824 opcode-key\nfix intact in the rewritten op cache.\n- First run after this rebuilds shaders+pipelines (new keys); measure\nsteady state on the run after.\n\nVerified: gpu 161 + full workspace suite green, clippy --all-targets\n--all-features -D warnings clean, policy 186 ok.
@TG0000

TG0000 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Merged main 940d32f in (fast-forward). Two things of main are kept with it: ranges whose shaders mark their writes take no snapshot, and what a refresh puts into a busy range now goes into the parts kept for it (that code merged without a conflict but used the old segments).

Because marked ranges take no snapshot any more, what is left to gain is smaller than in the description. Neither the benchmark nor the frame time was measured again on this head.

Windows (RX 6800 and WARP, one thread): victory-gpu 147 and victory-shader 247 passed; clippy -D warnings clean for the Windows target, with and without all features. AI-assisted (Claude Code).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants