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
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.
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. |
|
Automated triage for the maintainers. Needs work. It now merges cleanly into This adds opt-in ( What blocks it:
Generated by Claude Code |
84601fe to
9659ab9
Compare
|
Setup: RTX 3080 Ti (driver 621.24), Windows 11, resizable BAR, a local stack of open PRs merged with main 67e8ae1. First run of this with a title here. One scene, means of 20 reports, one run each (ms):
No device removal in those runs. Port notes for a tree with #2280 and #2409: the new copy-back paths ( Written with Claude Code; tested locally. |
|
Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP verification and comparison review of Verification (
|
…ies, written copies, residency and the memory budget) into demo With Marvisak#2051, queued refreshes of a copy in video memory copy at once (`Shadow::copy` writes the video memory, not the mirror). With Marvisak#2226, textures in a title heap's host heap take that heap's residency priority (D3D12 gives placed resources none of their own), and Marvisak#2222's test makes its textures with no spot.
9659ab9 to
8ab1672
Compare
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.
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 refresh over a page whose bytes from before the title's first write are saved writes the mirror too, so that those bytes take it (`Tracked::saves`, `Tracked::refreshed`). 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.
The mirror of a copy in video memory (VICTORY_GPU_UPLOAD_WRITES=1) took the GPU's writes only through pulls: of declared ranges, marked pages and the title memory pages that go back. Other writes stayed in video memory alone: past a root view's declared 1 MiB, through the heap's views of copies without marks, and on pages of programs using title memory whose results were held back. A later snapshot over such bytes read the mirror's older ones, so that submission's copy-back took the earlier GPU bytes for its own and put them into title memory, over a byte the title wrote during the submission. Held-back pages are now pulled with the others (the copies keep the GPU's bytes either way). After a submission, the pages shaders may have written past a root view's range (`Shadow::may_write`) or through the heap's unordered views of copies without marks (`bindless::written_unseen`) are stale in the mirror, so the CPU reads video memory's bytes there first. Tests: writes_past_declared_ranges_leave_the_mirror_stale (without the stale marks, the later copy-back puts the GPU's earlier byte over the title's) and held_back_marked_pages_come_back_to_the_mirror.
8ab1672 to
eff9f91
Compare
|
Thanks. Rebased with #2120 onto 63bcca4; head is now Stacked on #2120. Understood. Once #2120 is settled I'll rebase so only this PR's commits remain.
The lock stays held over the copy so that a concurrent reader can't see a cleared bit and read the page before the copy lands. Without it, a refresh during the copy would still re-mark its page afterwards (it sets the bit after writing video memory).
The fix:
The Full review pass of Checks on
|
With Marvisak#2051: queued refreshes of copies in video memory copy at once; the snapshot sync knows whether a range went through Shadow::copy. A Marvisak#2130 test calls copy_back_marked with its range argument.
… in video memory A copy in video memory, the default where the host has GPU upload heaps (Marvisak#2870), copies at once instead of queuing (Marvisak#2130): the test accepts either.
Writes past a root view's declared range mark the mirror stale (may_write, written_unseen), and withheld marked pages are dropped from the copies. The GPU memory page stays: the PRs stacked on Marvisak#2130 carry it.
…pdate applied)
… memory `VICTORY_STALE_CHECK=N` (diagnostics feature): once per presented frame, at the start of the first submission (under the submission lock, before any refresh), one host copy in N, round robin, has every page its tracker calls unchanged compared with title memory. A page that differs holds a write the tracking missed: the title's through a path that neither faults nor reports, or the shaders' through a view nobody declared or marked. Passed by: pages the title writes during the compare (asked again after a difference), pages with nothing mapped, untracked parts, and pages whose withheld shader writes stay in the copies until the frame's end. `1` checks every copy, `fix` also copies the pages that differ, any other value means one in 64. A line with the frame statistics gives copies, pages compared, pages and bytes that differed, the total, and the first 10 addresses. The idea, the variable and `fix` are ka1qux's (`VICTORY_STALE_CHECK`, 30a71b7c, GPL-3.0-or-later), rewritten for main's copies: theirs checked at each update, which main skips for clean copies. Co-Authored-By: ka1qux <80919110+ka1qux@users.noreply.github.com> (cherry picked from commit bba1998, fork mem/stale-check, upstream Marvisak#2629) Taken as a cherry-pick, not a merge: bba1998 sits on upstream main f3578e3, 24 commits past this line's 781e846, and a merge would bring those in. The commit needs none of them: it builds and its tests pass on 781e846. One conflict, shadow.rs: stale_pages() is placed beside Marvisak#2130's copy_queued; Marvisak#2130's test helper of the same name (a mirrored copy's stale pages) is renamed mirror_stale_pages. WARP: shadow::tests stale_check_settings and the_stale_check_finds_writes_the_tracking_missed pass; shadow:: 23 passed. Every hook is behind the diagnostics feature and off unless VICTORY_STALE_CHECK is set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Independent AMD RDNA 4 verification + live title measurement of Setup: Windows 11 ( Checks & microbenchmark:
Live title run (
Cross-vendor takeaway (AMD RDNA 4 vs. NVIDIA):
Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity. |
|
Thanks for the RDNA 4 runs, on this PR and on #2120. Both stay opt-in here: |
|
Replaced by #3102, which combines this with the rest of the host-memory placement work in one PR, with measurements on a discrete and an integrated GPU. Closing here. |
The copy-back of marked pages compared and wrote about 2,000 pages of 64 KiB a frame on the submitting thread, 55 to 80 us a page. Pages are now grouped by page index, so two buffers that cover the same page stay on one thread in their old order, and the groups are taken by up to six scoped threads, the submitting one among them. Fewer than sixteen pages stay on that thread. One run each at the same scene (medians, on a stack including Marvisak#2120, Marvisak#2130 and Marvisak#2280): marked pages 166 to 111 ms, copy-back 184 to 124 ms, frame 777 to 690 ms; same dropped draws and untranslated programs, no device removal. Ported to main from Machad3x/VIctory-nvidia-3080ti@7229f41 (Marvisak#2826), minus that fork's doc page. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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, snapshot updates under a busy range, and the zeros a refresh writes over unmapped pages.copy_back_markspulls their marked pages.Shadow::written_unseen): after a submission, the pages shaders may have written past a root view's declared 1 MiB (Shadow::may_write) and through the heap's unordered views of copies without marks (bindless::written_unseen). The CPU's next read there brings video memory's bytes first. Pages of programs using title memory whose results are held back are pulled with the others.Copies of loose title memory and of single views (
loose,exact) stay in system memory.Docs: no separate page. The earlier
docs/gpu-memory.mdis gone (and its link fromdocs/architecture.md); a short paragraph in theshadow.rsmodule docs says where copies live with each switch. Its measurements are below.Benchmarks
RTX 5070 Ti (driver 621.24), Ryzen 9 7900X, Windows 11, release build.
Where a copy can live, 256 MiB at a time:
L0, the default for copies)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 program using title memory writes in a list that missed memory, so its writes stay out of title memory. Title memory keeps its byte, and the copy and its mirror have the GPU's until the copy's next refresh takes title memory's bytes again.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.writes_past_declared_ranges_leave_the_mirror_stale: the GPU writes past a declared range (as through a root view): those bytes stay out of title memory and the mirror is stale there. A later list declaring them, with the title writing one of those bytes during its submission, keeps the title's byte and leaves the earlier GPU bytes out. Without the stale marks, that copy-back put the GPU's earlier byte over the title's.held_back_marked_pages_come_back_to_the_mirror: a page marked by a program using title memory while results are held back stays out of title memory, and the mirror has the GPU's bytes.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).
Rebased onto 13f91ca
memory::drop_withheld). So the GPU's bytes stay in the copy and its mirror only until the next refresh. The withheld test now declares its write from a program using title memory and checks the mirror right after the submission.mapped_buffer(size, upload)is nowshared_mapped_buffer.Rebased onto f3578e3
Tracked::refreshed, which lays it over the bytes saved of armed pages (marked copy-back compares with them). It reads those bytes from the copy'sdata, which for a mirrored copy is the mirror, and a refresh writes video memory only. So a refresh over a page that has saved bytes (newTracked::saves) writes the mirror too, and then hands the bytes over; every other refresh still writes video memory only. New testpartial_refresh_of_a_mirrored_copy_updates_saved_bytesruns upstream's partial-refresh test with mirrored copies; without the mirror write it fails.update_busyandupdate_freeintoupdate_tracked. Snapshot updates under a busy range still write the mirror where a refresh wrote video memory only (the "update_busydoes not write the mirror" row of the mutation table is now that code). Pages with nothing mapped are zeroed in the mirror directly and pushed, so their snapshot update copies nothing more.cargo test -p victory-gpu --libwith no switches and with both switches: the same two failures as on upstreammainhere.cargo clippy -p victory-gpu --all-targets, with and without--features diagnostics: clean.Triage: rebased onto 63bcca4, docs dropped, unpulled writes
docs/gpu-memory.mdis removed (see Docs above).eff9f91b: mirrors are stale wherever shaders may write with nothing pulled (the bullet above, and the two new tests). Before it, a later snapshot over such bytes read the mirror's older ones, so that submission's copy-back took the earlier GPU bytes for its own and could put them over a byte the title wrote during the submission.bring_backwaits for the GPU (Host::copy_now) with the copies' stale-page locks held. Every caller runs insideHost::executeunder thesubmittedlock: before the lists are submitted, or after the wait for them. So the host's only queue holds nothing but the copy list, and no host list waits on a fence (title queue waits are held on the CPU,crate::hold, beforeHost::executeruns).host::present_plane_tests::compositor_failure_presents_primary_in_the_same_frameexits the process with--features diagnosticson this machine, onmaintoo, so the diagnostics run skips it.cargo clippy -p victory-gpu --all-targets, with and without--features diagnostics: clean.cargo xtask policy: ok.