Skip to content

GPU: copies of written title buffers in video memory with a mirror (opt-in) - #2130

Draft
0xkn0wh3r3 wants to merge 5 commits into
Marvisak:mainfrom
0xkn0wh3r3:gpu/upload-heap-written-copies
Draft

0xkn0wh3r3 wants to merge 5 commits into
Marvisak:mainfrom
0xkn0wh3r3:gpu/upload-heap-written-copies

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #2120 (copies of read-only buffers in GPU upload heaps). The first two commits here are #2120's; the rest are this change. Copies of written buffers come from the same gpu_upload_buffer, so they are charged to the upload budget and given back the same way (#2120's second commit).

What

#2120 puts the copies of buffers that shaders only read in GPU upload heaps: video memory the CPU writes through the PCI BAR. Copies that shaders write stayed in system memory, because the CPU reads them back. Copy-back compares what shaders wrote, and snapshots read the copy before a submission. Reading a GPU upload heap from the CPU runs at about 50 MB/s.

With VICTORY_GPU_UPLOAD_WRITES=1 (and VICTORY_GPU_UPLOAD=1), a written buffer's copy goes in a GPU upload heap, and shaders use it there. A mirror in system memory is what the CPU reads. The copy in video memory is always current; the mirror may lag behind it by 64 KiB pages:

  • Refreshes write video memory only (Shadow::copy). They mark the pages they touched as stale in the mirror (a bit per page).
  • Stale pages come back before the CPU reads the mirror. The GPU copies them, one list and one wait for any number of pages (freshen → bring_back → Host::copy_now).
  • Pages shaders wrote come back the same way (shadow::pull): the marked pages before their copy-back, the declared ranges with snapshots, and the title memory pages in memory's copy-back.
  • A submission's snapshot ranges are prepared up front (snapshots_ahead). Their stale pages come back in one copy. Until the copy-backs, refreshes there write the mirror too, so no snapshot waits for the GPU.
  • Every other CPU write to the mirror goes on into video memory (Shadow::push): copy-back taking the title's bytes, and snapshot updates under a busy range.
  • Marked ranges are not pulled whole. Nothing reads the mirror for them, and copy_back_marks pulls their marked pages.
  • Shortcut: shaders' writes outside every declared range and marked page reach the copy only, as before. The mirror gets them once a later range brings them.

Copies of loose title memory and of single views (loose, exact) stay in system memory.

Docs: docs/gpu-memory.md explains how copies of title memory bridge the console's unified memory and a PC's two pools: where copies live, the switches, the lazy mirror, and the measurements, with Mermaid diagrams. It is linked from docs/architecture.md.

Benchmarks

RTX 5070 Ti (driver 621.24), Ryzen 9 7900X, Windows 11, release build.

GPU side, one copy, 256 MiB (written_copies_in_video_and_system_memory and #2120's reading_copies_in_video_and_system_memory):

copy in system memory (today) copy in video memory
shader reads 256 MiB (read-only buffer, #2120) 11.64 ms (23.1 GB/s) 0.37 ms (722 GB/s) 31×
shader reads and writes 256 MiB (written buffer, this PR) 212.23 ms (1.3 GB/s each way) 1.25 ms (214.7 GB/s each way) 170×

Bringing pages back from video memory:

time
1 page (the fixed cost: a list, a fence wait) 0.048 ms
64 scattered 64 KiB pages (4 MiB) 0.232 ms

CPU side per frame: #1743's world-frame benchmark (a 174 MB heap, 36 submissions, 24 declared ranges each; not part of this PR). Simulated GPU writes go to video memory, and the benchmark does the snapshot preparation and pulls that Host::execute does. Both builds ran interleaved, three rounds; the table gives the median of the three medians, in ms:

path switches off both switches, mirror always written (first commit) both switches, lazy mirror (now)
marked pages 80.7 114.6 69.0
declared-range snapshots 708 910 950
  • Marked path. With the lazy mirror, the switches cost less CPU time than copies in system memory. A refresh writes each changed page once, through the BAR, and only the marked pages come back.
  • Snapshot path. Every declared range is brought back before its compare (several GB per frame here), and the lazy mirror does not change that. It needs marks, or a compare on the GPU.
  • Correction. This description's earlier snapshot figure (770 ms) came from a benchmark that skipped the declared-range pull. With the pull, the first commit measures 910 ms.

Shader accesses to these copies run at video memory speed. How much a title gains depends on how much of a world frame's GPU time (180–320 ms in #400) goes to these copies. That needs a run of the title: compare VICTORY_GPU_TIMING=1 with and without the switches.

Testing

Each test below runs in a child process with the switches on:

  • written_buffers_copies_are_in_video_memory_with_a_mirror: the copy is in a GPU upload heap, and the GPU sees what refreshes wrote. The mirror is behind after a refresh and catches up when read.
  • gpu_writes_to_mirrored_copies_reach_title_memory: a GPU write into the copy through Host::execute, while the title writes the same page. Title memory ends up with both bytes. Run for a declared range and for a marked page.
  • withheld_writes_stay_in_the_copy_and_its_mirror: a list whose writes stay out of title memory. Title memory keeps its byte, and the mirror has the GPU's.
  • mirrors_catch_up_on_the_pages_read: the stale pages through a refresh, a read of one page, a pull, and a snapshot range ahead and done.
  • snapshots_of_mirrored_copies_come_back_at_once: four declared ranges over stale pages take two GPU copies in all (the snapshots', then the GPU's writes), and title memory is right.
  • marked_ranges_of_mirrored_copies_bring_back_their_marked_pages_only: a marked range takes one GPU copy (its marked page), and the other pages stay stale.
  • copy_tests_pass_with_mirrored_copies: three copy tests written for system memory copies, run again with mirrored copies: snapshots, refreshes under a busy range, and marked copy-back.

Mutation checks, each run against shadow:: tests:

change fails
freshen does nothing the placement and catch-up tests
a refresh marks nothing stale placement, catch-up, snapshots, marked range
no snapshot ranges ahead catch-up, snapshots
update_busy does not write the mirror busy-range test (mirrored)
bring_back clears no bits catch-up, snapshots, marked range
copy-back freshens its whole range marked range
Host::execute pulls every declared range marked range
pull does nothing GPU-writes, withheld, catch-up, snapshots
push does nothing GPU-writes, snapshots, the mirrored copy tests

The memory copy-back pull is not covered here. Its test, memory::tests::lists_share_a_table_and_copies_keep_their_views, fails on NVIDIA before reaching it (#1835).

cargo test -p victory-gpu --lib on the RTX 5070 Ti:

switches passed failed
none 150 2
VICTORY_GPU_UPLOAD=1 150 2
both 150 2
both, --features diagnostics 153 2

The 2 failures are the same in every configuration: descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views. Both fail the same way on main on NVIDIA (#1835).

cargo clippy -p victory-gpu --all-targets, with and without --features diagnostics: clean.

None of this has been run with a title (no game data here).

Host copies of title buffers are in system memory (`Host::shared_buffer`:
a custom heap, write-back, memory pool L0) so that the CPU can refresh
them and read back what shaders write. Every shader read of a copy then
crosses PCIe. On an RTX 5070 Ti a shader reads 256 MiB from such a copy
at 22.9 GB/s, and from video memory at 713 GB/s: the console's memory is
one pool, and the GPU reads it at video memory speed.

A buffer the title makes without unordered access is one shaders only
read: nothing reads its copy back, and the CPU only writes it (refreshes).
Its copy can be in video memory the CPU writes through the PCI BAR: a GPU
upload heap (`D3D12_HEAP_TYPE_GPU_UPLOAD`, `D3D12_OPTIONS16`, on hosts
with resizable BAR). With `VICTORY_GPU_UPLOAD=1`:

- `shadow::register_read_only` registers such buffers (`resource.rs`, from
  the title's `ALLOW_UNORDERED_ACCESS` flag), and `shadow::find` makes
  their copies with `Host::gpu_upload_buffer`;
- copies past 2 GiB of video memory, and every copy on hosts without GPU
  upload heaps, stay in system memory, as before;
- shaders that still write such a copy (through a view of the same memory
  under another resource) get it right, slowly: the copy-back reads video
  memory at ~50 MB/s; a warning names the first one.

Measured on an RTX 5070 Ti (driver 621.24, Ryzen 9 7900X), 256 MiB,
through `shadow::find` (`reading_copies_in_video_and_system_memory`):

| copy | shader reads | refresh writes (first, every page) |
|---|---|---|
| writable buffer's (system memory) | 11.74 ms, 22.9 GB/s | 42.1 ms |
| read-only buffer's (GPU upload heap) | 0.38 ms, 713 GB/s | 32.8 ms |

Off by default: how much of a world frame's GPU time this is depends on
which buffers the title makes without unordered access, which needs a
run of the title to measure.

`read_only_buffers_get_their_copies_in_video_memory` (in a child process
with the switch on) checks that a read-only buffer's copy is in a GPU
upload heap, that the GPU reads what a refresh wrote there, and that a
writable buffer's copy stays in system memory. The victory-gpu tests
give the same results with the switch on and off.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Written title buffers copied in video memory — complements #2120 (RO buffers in upload heaps); together they cover the copy-placement matrix.

The 2 GiB allowance for host copies in GPU upload heaps counted every
copy made over the whole run and never took any back. After enough read-
only buffers came and went (64 of 32 MiB on an RTX 3050 Laptop, with
only one alive at a time), every later copy went to system memory for
the rest of the run.

A copy's bytes now leave the count when D3D12 destroys its buffer, through
the buffer's destruction notifier. That is when the last reference goes
(the copy's, and the clones lists and descriptor tables hold), not when
the copy drops. A host without the notifier keeps copies in system memory
instead of charging them for good.

Test: upload_budget_is_given_back_when_copies_go. It makes 20 copies of
128 MiB one after another (2.5 GiB, past the budget). Each one is in a GPU
upload heap, stays charged while a clone of its buffer is held after the
copy dropped, and is given back once the clone goes. With the callback
doing nothing, it fails in the first round.
…pt-in)

Copies shaders write stayed in system memory, because the CPU reads them
back (copy-back compares what shaders wrote; snapshots read them before a
submission) and reading a GPU upload heap from the CPU runs at ~50 MB/s.
Shaders reading and writing system memory are slower still than reading
it: on an RTX 5070 Ti a shader reading and writing 256 MiB of a copy
takes 212 ms in system memory and 1.25 ms in video memory.

With `VICTORY_GPU_UPLOAD_WRITES=1` (and `VICTORY_GPU_UPLOAD=1`) such a
copy is in a GPU upload heap, which shaders use, mirrored in system
memory (`Shadow::data`), where the CPU reads and writes as before:

- every CPU write into the copy (refreshes, copy-back taking the title's
  bytes) goes on into video memory (`Shadow::push`);
- before the CPU reads what shaders may have written, the GPU copies it
  into the mirror (`shadow::pull`, `Host::copy_now`: one list and one
  wait per call): the marked pages in `copy_back_marks`, the declared
  ranges before `copy_back`/`keep_writes` in `Host::execute`, the title
  memory pages in `memory`'s copy-back;
- shaders' writes outside every declared range and marked page reach
  the copy only, as they did; the mirror has them once a later range
  brings them (a shortcut, noted at `pull`).

Copies of loose title memory and of single views (`loose`, `exact`) stay
in system memory.

Tests (each in a child process with the switches on):
`written_buffers_copies_are_in_video_memory_with_a_mirror` (placement;
the GPU sees refreshes), `gpu_writes_to_mirrored_copies_reach_title_memory`
(a GPU write into the copy in a declared range, then in a marked page,
with the title writing the same page meanwhile, through `Host::execute`:
title memory gets both), `withheld_writes_stay_in_the_copy_and_its_mirror`.
Without `pull` the second and third fail; without `push` the first two
do; without the marked pages' pull the marked case does. The victory-gpu
tests give the same results with the switches off, on for read-only
copies, and on for both. Benchmark: `written_copies_in_video_and_system_memory`.
A copy in video memory (VICTORY_GPU_UPLOAD_WRITES=1) took every refresh
twice: into its system-memory mirror, then on into video memory. Now a
refresh writes video memory only and marks the 64 KiB pages it touched as
stale in the mirror. Before the CPU reads the mirror, its stale pages come
back from video memory in one GPU copy (`freshen`, `bring_back`).

A submission's snapshot ranges are brought up to date up front, in one
copy (`snapshots_ahead`), and refreshes there write the mirror too until
the copy-backs, so no snapshot waits for the GPU. The declared-range pull
now covers only those ranges: nothing reads the mirror for a marked range,
whose pages `copy_back_marks` pulls. Copy-back reads the mirror only where
it compares.

Synthetic world frame, marked path, median ms: copies in system memory
80.7, mirror always written 114.6, now 69.0.

docs/gpu-memory.md explains how copies bridge the console's unified memory
and the PC's two pools, with the measurements.
@0xkn0wh3r3
0xkn0wh3r3 force-pushed the gpu/upload-heap-written-copies branch from e751777 to 84601fe Compare November 25, 2026 06:15
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
…mmit

Marvisak#2130 was rewritten on top of Marvisak#2120's update, so layering it on the old
merges kept two versions of it. This tree is upstream main (940d32f) with
all 32 open PRs of 0xkn0wh3r3 at their current commits, the upstream
fixes Marvisak#1887, Marvisak#1717, Marvisak#1832, Marvisak#1630, Marvisak#1763 and Marvisak#1613, and the demo-only
conformance list; the old history stays as this merge's first parent.
@wbkepler

Copy link
Copy Markdown

Measured this PR (with #2120 under it) on a title, since the description asks for one.

Host: Windows 11, NVIDIA RTX 5090 (32-lane waves), Ryzen 7 9800X3D, diag build. The base is an integration of main at 940d32f plus open PRs needed to reach the world on a 32-lane host. #2130 merged on top, with one conflict in shadow.rs (the integration's copy-back goes through a shared put_back, so freshen comes before it and push after take_others). 300 s capped runs, same input script, VICTORY_D3D12_DRED=1.

steady state in the world copies in system memory VICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1
GPU time per frame 316-332 ms 100-103 ms
frame time 633-650 ms (1.5-1.6 fps) 467-533 ms (1.9-2.1 fps)
draws per frame ~4,920 ~4,960
device removal none none

Per command (VICTORY_GPU_TIMING=1, every timed command summed over ~170-190 s in the world):

compute work before after
ray-query indirect dispatch (1 group of records) 47 ms/s, up to 114 ms each under 1 ms/s
acceleration-structure build, 64-lane groups, 1-30 groups 36 ms/s, up to 437 ms each 2.9 ms/s, up to 71 ms each
BVH node rehydration, 1-11 groups 25 ms/s, up to 185 ms each 1 ms/s
cloth-style solver, 43-289 groups 19 ms/s, up to 400 ms each under 1 ms/s
skinning, 788x1x1 18 ms/s (~2.9 ms each) under 1 ms/s
second ray-query indirect dispatch 13 ms/s under 1 ms/s

After the switches, the GPU time left is spread over many small mesh-shader ExecuteIndirect draws and texture uploads. No single program stands out. The frame is now bound by CPU work: about 225 ms preparing and about 140 ms copying back per frame.

The cause matches what the PR targets. A microbenchmark on this host shows how much slower GPU access is when the buffer is a CUSTOM / WRITE_BACK / L0 copy (Host::shared_buffer). Test: 65,536 threads, each making 64 chained dword loads from a 16 MiB buffer.

  • WRITE_BACK L0: 5.5 ms
  • DEFAULT, GPU upload, upload or WRITE_COMBINE L0: 0.05 ms (the buffer stays in L2)
  • Streaming dword loads: 0.35 ms vs 0.02 ms.
  • 1M same-dword InterlockedOr: 18 ms vs 0.03 ms.

On NVIDIA, the GPU never caches CPU-cached system memory in L2, so every access crosses the bus. The translated programs themselves were not the problem: their page-table lookups and marks cost little once the data is in video memory.

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