Skip to content

Copy-back by memory class: opt-in switches to leave GPU writes the title does not read on the card - #3838

Merged
trustno1-coder merged 3 commits into
trustno1-coder:mainfrom
x86:pr/unified-memory-classes
Nov 30, 2026
Merged

trustno1-coder merged 3 commits into
trustno1-coder:mainfrom
x86:pr/unified-memory-classes

Conversation

@x86

@x86 x86 commented Nov 30, 2026

Copy link
Copy Markdown

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:

  1. Keeps that protection per range (shadow::title_memory_protection, fed by xmem's note_gpu_protection, forgotten in title_memory_freed), and adds VICTORY_UNIFIED_BACK=<classes> to copy shader writes back only for the classes named.
  2. Lets the write-combined rule take small copies: 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; 2 also copies outside the card's memory), VICTORY_WC_NO_BACK_PC=1 (copies in the PC's memory).
  3. Keeps copying back what another host copy holds (VICTORY_UNIFIED_ALIAS, on under UNIFIED_BACK, for copies under VICTORY_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=1 adds 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 in docs/unified-memory-classes.md.

The set measured:

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

How it was tested

  • GPU and profile: RTX 5090, Ryzen 9 9950X3D, Windows 10 22H2 with the Agility runtime; Series S, 4K output, DLSS Performance, frame generation off (so every frame counted is rendered), ray tracing at the launcher default. The opening (cutscene, then the indoor scene and credits), 330 s runs alternating switched and unswitched, median frame time over the frame-rate lines with ≥1000 draws.
  • Before: 31.3 ms in the opening (~32 fps), 23.7 ms in the later scenes (~42 fps) (median of 4 runs)
  • After: 28.9 ms in the opening (~35 fps, −8 %), 22.4 ms later (~45 fps, −5 %) (2 runs; the final branch build: 28.7 ms). Picture checked against unswitched runs at full resolution across the whole opening, including the car close-ups of both faces.
  • For shader changes, the wave sizes tested: n/a (no shader changes)
  • cargo test passed 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 on 41adb551 without 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-ro and noncoherent are 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

VIctory contributor and others added 3 commits November 30, 2026 21:16
…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>
@github-actions github-actions Bot added memory RAM or VRAM use, out of memory, leaks NVIDIA pr-performance Pull request: makes it faster or use less memory labels Nov 30, 2026
@github-actions github-actions Bot changed the title [pr-performance][memory][NVIDIA] Copy-back by memory class: opt-in switches to leave GPU writes the title does not read on the card Copy-back by memory class: opt-in switches to leave GPU writes the title does not read on the card Nov 30, 2026
@Kassadd

Kassadd commented Nov 30, 2026 •

Copy link
Copy Markdown

Measured under Wine, and it is the largest single win of the three optimisations I
tried together.

Environment: RTX 4080 SUPER 16 GB, i9-14900K, 62 GB, CachyOS, NVIDIA 620.73.04,
Proton Experimental …-20261128 (vkd3d-proton 71cae0) via umu-run with its own
prefix, Series X 1.0.2.1, --res 1440 with VICTORY_NATIVE_1440P=0 (drawing and
showing at 4K; 1080to1440 gives a doubled character). Built main at 41adb551
(the v2026.11.29.05 tree) with #2585, #3756 and this PR, which applied with no
conflict.

The scene matters when comparing with your figures. Yours are Windows, RTX 5090,
Series S, 4K with DLSS Performance, 31.3 ms in the opening and 23.7 ms in the later
scenes. Mine is the open world in play — --res 1440 with
VICTORY_NATIVE_1440P=0, i.e. the full 4K scene with no upscaler, Series X, under
Wine — so my absolute numbers are not comparable and are not offered as such. What is
comparable is the split: how much of the frame goes to copying back before and after.

Before and after, same save, same place in the open world, single samples per
figure:

before with #3838 + #3756 + #3762
frame 71.0 ms 51.4 ms
copying back 14.8 ms 5.3 ms
copies 66.4 MB 44.0 MB
preparing 35.5 ms 25.9 ms
GPU busy 13.3 ms (19 %) 17.4 ms (34 %)

I cannot attribute all of that to this PR. The three went in together, so what I
can say is narrower: with VICTORY_UNIFIED_BACK=1 among them, copying back fell by
two thirds and the GPU's share of the frame doubled — the card was idle waiting for
work that, per this PR's argument, the title never reads. That is exactly the
behaviour the doc describes, and it is the change that moved the figure most.

The thing a player notices is not the average but the variance, and that is where
the open world shows it: standing still the frame is about 20 fps, and before these
PRs turning the camera dropped it to 8 — nearly all of it in the copy-back and the
refresh sifting the turn makes. With the three it no longer collapses. On this machine
that is worth more than the mean, because in the world the camera is always moving.

Two notes from the run:

  • The switches were not in the binary's string table under the names the doc uses
    for VICTORY_MAP_ONCE and VICTORY_COPY_BACK, while VICTORY_UNIFIED_BACK was
    accepted. Either the doc's names and the code's had drifted apart at the commit I
    built, or I misread it — worth a check, since someone following the doc would set a
    switch that does nothing.
  • refreshes is still where the remaining time goes: 8,604 of 8,986 a frame are
    known to copy nothing, i.e. the layer asks whether title memory changed and almost
    always it has not. watch: extend WRITTEN_EPOCHS for lock-free journal reads and whole-page notes #3756 and Fuse refresh sifting when write watch is kept #3762 make that cheaper to process, but nothing yet
    asks less often. If this PR's split is the framework for memory classes, that seems
    the next place to point it.

Logs and the exact invocation on request.

@ColorC

ColorC commented Nov 30, 2026

Copy link
Copy Markdown

Disclosure: This cross-validation benchmark and comment were executed and posted by an AI agent on real hardware (RTX 5070 Ti 16 GB Blackwell + Ryzen 7 9800X3D, Win11 26200, PCIe 5.0 x16, ReBAR ON). No game screenshots are included; telemetry only.

Isolated Single-Variable A/B Cross-Validation on RTX 5070 Ti (VICTORY_HW_RT=1, 1440p DLSS Quality + 3x MFG)

Tested cleanly on top of our bench-super-v19-clean baseline using a camera-locked 3-scene plan (Noon ~9–10.4k draws, Night ~5.5–6.2k draws, Misty Dawn ~11.7–12.5k draws; same save restored before each run; 0 crashes):

Switches tested:
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

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 ms to -9.7 ms, up to -27%) and huge true per-frame 1% Low (P99) gains (+2.4 to +4.9 FPS) on RTX 5070 Ti + 9800X3D. Strong LGTM from this hardware configuration!

@Marsietiss

Copy link
Copy Markdown

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.

Frame Copying back Picture
off 39.1 ms 10.0 ms (8.4 MB) fine
on (measured set) 34.8 ms 3.9 ms (3.5 MB) sparkling / broken surfaces within seconds; fine again when switched off

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.

@NebulaeTUT

Copy link
Copy Markdown

Test on RTX 3060 Laptop (6 GB): faster here, and by more than on the 5090

I ran the set on a laptop with a 6 GB card. wc is the class that dominates
in this game, and the saving here is larger than on your 5090, so I am
posting the numbers in case they are useful.

Setup

GPU NVIDIA GeForce RTX 3060 Laptop GPU, 5,994 MB own memory
CPU 12th Gen Intel Core i5-12500H (12 P+E cores, 16 threads)
RAM 39.8 GB
OS / driver Windows 11 26300, driver 621.40.0, Agility runtime D3D12Core.dll from System32
Profile Series S, DLSS Performance (scene 0.5), frame generation off
Switches VICTORY_THIN=0 VICTORY_JOIN_PARTS=1 on both runs; the set under test added on top
Cache shaders + warm --used before each session; neither log has (N being made)
Runs 3–5 min in the world on the same mixed indoor/outdoor route

The set:

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

Result (median over frame-rate lines with draws >= 1000)

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

  1. 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-ro and noncoherent stayed 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 stale noncoherent read to
    show.

  2. 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, with wc at 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

@Marsietiss

Copy link
Copy Markdown

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.

@Marsietiss

Copy link
Copy Markdown

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

Lists in flight Variant Frame Copy-back Largest indirect mesh dispatch group
256 off 33.7-36.0 ms 10.1-10.6 ms 294
256 set on, whole-page sends (your behaviour) 27.7-28.4 ms 3.7-3.8 ms 293-294
256 set on + our changed-words-only guard 30.5 ms 3.4 ms 14,786
32 off 40.1 ms 10.5 ms 294
32 set on, whole-page sends 33.6 ms 3.1 ms 294
32 set on + our guard 39.1 ms 3.1 ms 14,803
  • Picture with whole-page sends: clean to the eye over several minutes of walking around that area, at 256 lists in flight.
  • With our guard the fault appears at 32 and at 256 alike, so it is not a lists-in-flight effect: a per-frame reset of a count to the same value was never sent, and the card kept counting on top of the old value. Your comment about why the page is sent whole describes exactly this.
  • About -6 ms a frame here (-17 to -20%), nearly all of it copy-back.

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.

@Marsietiss

Copy link
Copy Markdown
3838-control-our-guard 3838-whole-page-sends

@trustno1-coder
trustno1-coder merged commit 6b70b9e into trustno1-coder:main Nov 30, 2026
1 check passed
SJTheGreat06 pushed a commit to SJTheGreat06/VIctory-NVIDIA that referenced this pull request Nov 30, 2026
…ts, lazy compare, trustno1-coder#3838 copy-back classes)


Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
trustno1-coder added a commit that referenced this pull request Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

memory RAM or VRAM use, out of memory, leaks NVIDIA pr-performance Pull request: makes it faster or use less memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants