Repository navigation
Copy-back by memory class: opt-in switches to leave GPU writes the title does not read on the card - #3838
Conversation
…er writes back by class
The title declares how each processor reaches each range of its memory: the protection it passes to
XMemVirtualAlloc and VirtualProtect (GDK graphics memory overview). GPU read-only pages cannot be
written by shaders; write-combined ("garlic") pages are written by the CPU and read by it only
uncached; coherent ("onion") pages are made for the CPU to read GPU results; cached non-coherent pages
are read by the CPU only after the title's own GPU L2 write-back; and some pages carry no GPU bits.
The protection of every range is now kept (shadow::title_memory_protection, fed by xmem's
note_gpu_protection, forgotten in title_memory_freed), and VICTORY_UNIFIED_BACK=<classes> (names:
unknown, gpu-ro, wc, coherent, noncoherent, cpu-only) copies shader writes back to title memory only
for the classes named. Write-combined copies the existing size rule leaves on the card
(VICTORY_WC_NO_BACK, _MIN_KB) stay there when wc is kept. Unset, nothing changes.
VICTORY_UNIFIED_STATS=1 adds a line to the frame-rate log: shader writes per class, declared, copied
back and left on the host side.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ill copied back Of the copy-back that is left with the existing write-combined rule (a 1 MiB floor, whole copies, copies in the card's memory only), most is compare passes over small write-combined copies: on an RTX 5090 in the opening of the game about 650 small copies a frame, each a pass of its own. Three switches, all off unless set, let the rule take them: - VICTORY_WC_NO_BACK_MIN_BYTES: the floor in bytes (takes precedence over _MIN_KB). What the title reads back from write-combined memory (the exposure) lies in copies of 64 to 256 bytes: a floor of 64 bytes overexposes the opening, 256 or 1024 keeps it right. - VICTORY_WC_PIECES=1: the rule takes the write-combined pieces of a copy that also covers other memory (the title's main graphics pool is one buffer over shader code, write-combined and non-coherent regions); =2 also takes copies that are not in the card's memory. - VICTORY_WC_NO_BACK_PC=1: the rule also takes copies in the PC's memory. Small write-combined buffers also hold data a later pass reads through another host copy of the same bytes (a window of 1 MiB or more), which is refreshed from title memory: left on the card, that data never reaches it. With the floor alone, the faces' skinning data went stale and vertices of the characters' faces were pulled to one point beside the head. So a piece the class rule would leave on the card is copied back when another host copy holds any of it (VICTORY_UNIFIED_ALIAS, on unless 0), for copies under VICTORY_UNIFIED_ALIAS_MAX bytes (1 MiB; larger copies under windows have been left on the card by the write-combined rule without a fault). Measured on an RTX 5090, Windows 10, Series S profile, 4K output with DLSS Performance, frame generation off, in the opening (cutscene and the scenes after it, 330 s runs alternating with unswitched ones): with VICTORY_UNIFIED_BACK=wc,coherent,cpu-only,unknown VICTORY_WC_PIECES=2 VICTORY_WC_NO_BACK_PC=1 VICTORY_WC_NO_BACK_MIN_BYTES=1024, about 8 % less frame time early (31.3 -> 28.9 ms median) and 5 % later (23.7 -> 22.4 ms), the picture checked at full resolution against unswitched runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Measured under Wine, and it is the largest single win of the three optimisations I Environment: RTX 4080 SUPER 16 GB, i9-14900K, 62 GB, CachyOS, NVIDIA 620.73.04, The scene matters when comparing with your figures. Yours are Windows, RTX 5090, Before and after, same save, same place in the open world, single samples per
I cannot attribute all of that to this PR. The three went in together, so what I The thing a player notices is not the average but the variance, and that is where Two notes from the run:
Logs and the exact invocation on request. |
Isolated Single-Variable A/B Cross-Validation on RTX 5070 Ti (
|
| Locked Scene (HWRT 1440p) | Variant | Avg FPS (draws) | GPU ms (cardmeter) |
CPU preparing ms |
True Med ms | P95 ms | 1% Low (P99) | Worst ms | >100 ms |
|---|---|---|---|---|---|---|---|---|---|
| Noon (13:00) | Baseline (PR #3838 OFF) | 22.85 (9,132) | 30.0 | 29.8 | 42.0 | 47.9 | 55.8 ms (17.9 fps) | 97.5 | 0 |
| Noon (13:00) | #3838 ON | 24.30 (+1.45) (10,338) | 29.2 | 24.4 (-5.4 ms) | 40.5 | 46.6 | 48.8 ms (20.5 fps, +2.6) | 64.4 | 0 |
| Night (23:00) | Baseline (PR #3838 OFF) | 24.42 (~5.8k) | 23.7 | 30.8 | 39.0 | 48.3 | 55.6 ms (18.0 fps) | 107.5 | 2 |
| Night (23:00) | #3838 ON | 24.62 (+0.20) (~5.8k) | 26.7 | 22.7 (-8.1 ms) | 42.7 | 43.3 | 43.7 ms (22.9 fps, +4.9) | - | 0 |
| Misty Dawn (07:00) | Baseline (PR #3838 OFF) | 18.15 (11,943) | 37.7 | 36.3 | 54.5 | 57.8 | 60.8 ms (16.4 fps) | 76.1 | 0 |
| Misty Dawn (07:00) | #3838 ON | 19.20 (+1.05) (~12k) | 36.5 | 26.6 (-9.7 ms) | 52.2 | 53.0 | 53.2 ms (18.8 fps, +2.4) | - | 0 |
Visual & Stability Check
- Locally inspected 3-scene frame captures (
Noon,Night,Dawn) side-by-side against baseline: character skinning, face/cap shaders, volumetric fog, wet puddles, foliage, and night streetlamp lighting showed zero regressions or corruption, and 0 crashes across consecutive runs. - Massive CPU preparing reduction (
-5.4 msto-9.7 ms, up to-27%) and huge true per-frame 1% Low (P99) gains (+2.4to+4.9 FPS) on RTX 5070 Ti + 9800X3D. Strong LGTM from this hardware configuration!
|
Tested a hand port of this on the Mangrove line (RTX 5080 + Ryzen 9 9950X3D, 256 lists in flight, frame generation off, walk-sampled route over three spots, live switch flipped in one launch). It is our own port of the measured set, so it may differ from your code in details.
So the speed is real here too (about -4 ms), but the picture breaks on our tree. Our reading: the set skips copy-back for memory the title reads back on the CPU after the GPU wrote it. Earlier we saw the title read inside a large write-combined pool, and its pieces stay on the card under the byte floor. As far as we can tell from the console's memory rules, only GPU-read-only memory can skip copy-back safely; write-combined and non-coherent memory are still CPU-readable after GPU writes. A variant that skips only GPU-read-only memory was picture-safe here but saved about 0 MB. Possibly useful: a per-block "title read this after a GPU write" record (we feed ours from a read probe) to exclude those blocks. With it the saving would shrink, but the picture should hold. Happy to test a revision on the 5080. |
Test on RTX 3060 Laptop (6 GB): faster here, and by more than on the 5090I ran the set on a laptop with a 6 GB card. Setup
The set: Result (median over frame-rate lines with
|
baseline (main, 41adb55) |
with the set | ||
|---|---|---|---|
| World samples | 91 | 89 | |
| draws/frame (median) | 3,623 | 3,603 | comparable |
| Frame | 94.9 ms | 73.9 ms | −21.0 ms (−22 %) |
| preparing | 48.1 ms | 47.4 ms | −0.7 ms |
| GPU | 6.1 ms | 2.8 ms | −3.3 ms |
| copy-back | 23.9 ms | 13.9 ms | −10.0 ms (−42 %) |
| draws dropped (median) | 117 | 110 | |
| fps | ~10.5 | ~13.5 | +3 |
The copy-back is what the PR targets and it drops by 42 %, from 23.9 to
13.9 ms. Preparing stays flat, as you found on the 5090. A second run of
the set gave the same copy-back (13.5 and 13.9 ms), so it is not one lucky
run.
wc really is the class that matters
VICTORY_UNIFIED_STATS=1 on this route:
shader writes per frame by memory class, MB:
declared [unknown 0.0, gpu-ro 24.9, wc 3171.6, coherent 0.0,
noncoherent 92.3, cpu-only 18.8]
wc is 3.17 GB a frame, so taking wc out of the copy-back is what does
the work. Your 1024-byte floor looks right: I did not try to lower it.
Two things worth reporting
-
The picture held up on the route I ran (indoor rooms, a street with
NPCs, a vehicle section): no stale faces, no seams, nothing visibly
different from the baseline run.gpu-roandnoncoherentstayed out
of the copy-back the whole time and I saw no fault. That is one route,
not a proof: I did not pass through a scene with reflections or a water
surface, which is where I would expect a stalenoncoherentread to
show. -
One hard system freeze. With the set on, the machine locked up: no
BSOD, the crash log stayed empty and the play log simply stops mid-frame;
I had to power-cycle the PC. It did not happen again on the second run
with the same set, so I cannot pin it on this PR. Just before the
freeze the card was reported about 95 % busy, 134.7 ms of card time a
frame, withwcat 3.17 GB a frame of declared writes. On a 6 GB card
that could be the card or the driver running out of room rather than the
copy-back logic. I mention it because you asked for reports on cards
other than the 5090, and I would not call this quiet until someone has
run it longer than I have.
Representative frame lines
Baseline (main), world:
11.5 fps, 87.1 ms/frame (60.4 preparing: copies 26.0 ms 91.4 MB, textures 5.9 ms 20.5 MB; 8.7 on the GPU; 26.9 copying back 1.1 MB), 5,044 draws/frame
14.4 fps, 69.5 ms/frame (51.6 preparing: copies 22.0 ms 68.2 MB, textures 6.3 ms 26.0 MB; 2.4 on the GPU; 20.1 copying back 1.0 MB), 4,071 draws/frame
With the set, same route:
10.8 fps, 92.4 ms/frame (48.6 preparing: copies 23.4 ms 69.4 MB, textures 5.8 ms 18.9 MB; 1.3 on the GPU; 13.4 copying back 0.5 MB), 3,067 draws/frame
15.2 fps, 65.9 ms/frame (41.8 preparing: copies 17.8 ms 59.1 MB, textures 5.3 ms 20.7 MB; 2.2 on the GPU; 13.9 copying back 0.4 MB), 3,211 draws/frame
|
Correction to my comment above: please do not count our broken picture against this PR yet. A closer look at our hand port points at a guard we added ourselves, which is not in your code: for copies with writes left we sent only the dwords the title changed, where your PR sends the written page whole (your comment explains why). With our guard a per-frame reset to the same value is never sent, and in our logs the largest indirect mesh dispatch group count climbs from about 300 to 20-30 thousand while the set is on, then falls back within seconds of switching it off. That matches the sparkle, and it is our bug, not yours. We are rebuilding with your whole-page behaviour and will post the result (picture and numbers at 32 and at 256 lists in flight) here. |
|
Retest done, and the result is the opposite of my first report: with your whole-page behaviour the set is clean here and a clear win. Our broken picture was our own added guard. RTX 5080 + Ryzen 9 9950X3D, Series S profile, 1080p output with DLSS quality, frame generation off, one launch, live switches, same spot (close NPCs). This is our hand port of the measured set (wc, coherent, cpu-only, unknown; pieces 2; floor 1024).
Caveats: single spot, standing or walking, other processes were compiling during the run, so the ms values are indicative; a matched-draw route run follows. We still read the console's memory rules as allowing the title to read write-combined and non-coherent memory after GPU writes, so we will keep it opt-in on our line until more scenes are checked, but we have not found a scene that shows a problem. Stills of the broken control and the clean run are available; I will attach them when I can upload images. Thanks for this one. |
…ts, lazy compare, trustno1-coder#3838 copy-back classes) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…without COMPILE_MEMORY=0 (4017)


Fixes # (none: no issue for this; see Related below)
What it changes
Every title buffer has a host copy; what shaders write into it is hashed, compared and merged back into title memory after the submission, for the title's CPU and for other copies of the same bytes. On an RTX 5090 in the opening, that bridge costs about 11 ms of a 31 ms frame (measured by switching copy-back and refreshes off for timing only, picture broken), and after the existing 1 MiB write-combined rule most of the copy-back that is left is compare passes over small write-combined copies (about 650 a frame), not bytes.
The title declares how each processor reaches each range of its memory, in the protection it passes to
XMemVirtualAlloc/VirtualProtect(GDK graphics memory overview: GPU read-only, write-combined "garlic", coherent "onion", cached non-coherent, no GPU bits). This PR:shadow::title_memory_protection, fed byxmem'snote_gpu_protection, forgotten intitle_memory_freed), and addsVICTORY_UNIFIED_BACK=<classes>to copy shader writes back only for the classes named.VICTORY_WC_NO_BACK_MIN_BYTES(the floor in bytes),VICTORY_WC_PIECES=1|2(write-combined pieces of copies that also cover other memory, such as the title's main pool;2also copies outside the card's memory),VICTORY_WC_NO_BACK_PC=1(copies in the PC's memory).VICTORY_UNIFIED_ALIAS, on underUNIFIED_BACK, for copies underVICTORY_UNIFIED_ALIAS_MAX= 1 MiB). Small write-combined buffers hold data a later pass reads through another copy (a window of 1 MiB or more) that is refreshed from title memory. With the floor alone, the faces' skinning data went stale and vertices of Jason's and Lucia's faces were pulled to one point beside the head.Everything is off by default; with no switch set the layer behaves as before (the only always-on part is recording each range's protection at allocation/protect time).
VICTORY_UNIFIED_STATS=1adds one frame-rate log line with shader writes per class. Details, the reasons for the 1024-byte floor (the exposure the title reads back lies in 64–256 byte copies; a 64-byte floor overexposes the opening) and the limits are indocs/unified-memory-classes.md.The set measured:
How it was tested
cargo testpassed for the crates it touches: yes, where the tests can run on this PC.victory-hle: all pass (423 + 2 + 1).victory-gpu: the new test and the modules touched pass (shadow::68 passed,watch::65 passed, 0 failed). The tests that need a D3D12 device (e.g.ambient::tests::the_passes_build,shadow::exact_tests::*,shadow::land_tests::*) abort the test binary on this Windows 10 machine (test binaries don't load the Agility runtime); they abort identically on41adb551without this PR.cargo clippy -p victory-gpu -p victory-hle --all-targets: no findings in the lines this PR adds.Limits: one scene, one card, one profile; frame generation was not engaged in the measured runs.
gpu-roandnoncoherentare left out of the copy-back in the measured set with no visible fault, but a scene where the title reads GPU results from non-coherent memory would show one. Other scenes and AMD cards are untested, which is why it is opt-in.Checked for overlap
shadow.rs/host.rs), VICTORY_WC_BAR=runs: write only the dwords the title changed into the mapped copy (fixes the #553 flicker) #1927 and gpu: join copy_beside before placed_copy/publish in submit split and note batch writes on shader_writes #2433 (touch the write-combined code inshadow.rs; may need a rebase against each other)