Repository navigation
GPU: copies of written title buffers in video memory with a mirror (opt-in) - #2130
0xkn0wh3r3 wants to merge 5 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
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.
e751777 to
84601fe
Compare
…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.
|
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
Per command (
After the switches, the GPU time left is spread over many small mesh-shader The cause matches what the PR targets. A microbenchmark on this host shows how much slower GPU access is when the buffer is a
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. |
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(andVICTORY_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:Shadow::copy). They mark the pages they touched as stale in the mirror (a bit per page).freshen→bring_back→Host::copy_now).shadow::pull): the marked pages before their copy-back, the declared ranges with snapshots, and the title memory pages inmemory's copy-back.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.Shadow::push): copy-back taking the title's bytes, and snapshot updates under a busy range.copy_back_markspulls their marked pages.Copies of loose title memory and of single views (
loose,exact) stay in system memory.Docs:
docs/gpu-memory.mdexplains 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 fromdocs/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_memoryand #2120'sreading_copies_in_video_and_system_memory):Bringing pages back from video memory:
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::executedoes. Both builds ran interleaved, three rounds; the table gives the median of the three medians, in 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=1with 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 throughHost::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:freshendoes nothingupdate_busydoes not write the mirrorbring_backclears no bitsHost::executepulls every declared rangepulldoes nothingpushdoes nothingThe
memorycopy-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 --libon the RTX 5070 Ti:VICTORY_GPU_UPLOAD=1--features diagnosticsThe 2 failures are the same in every configuration:
descriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views. Both fail the same way onmainon 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).