GPU: copies of written title buffers in video memory with a mirror (opt-in) #2130
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2130
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2130"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@0xkn0wh3r3 on GitHub · 2026-11-25 05:45 UTC · original
0xkn0wh3r3:gpu/upload-heap-written-copies→main· +1607 −35 · 7 files · 5 commits · closed without mergingStacked 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
13f91ca0a5be3cwithholds only the writes of programs using title memory when their list misses, and upstream drops withheld ranges from the host copies after the submission (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.2a4938ezeroes pages with nothing mapped during a refresh. Those zeros are also pushed to the copy in video memory, so it does not keep stale bytes the mirror no longer has.mapped_buffer(size, upload)is nowshared_mapped_buffer.Rebased onto
f3578e3106377dmakes a refresh hand what it copied toTracked::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.5319366mergedupdate_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 writes63bcca4. The branch now has #2120's two commits and three of its own; the docs-only commit is gone.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.github-bot referenced this pull request2026-11-26 05:07:28 +00:00
github-bot referenced this pull request2026-11-26 05:17:22 +00:00
@wbkepler on GitHub · 2026-11-25 12:07 UTC · original
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
mainat940d32fplus open PRs needed to reach the world on a 32-lane host. #2130 merged on top, with one conflict inshadow.rs(the integration's copy-back goes through a sharedput_back, sofreshencomes before it andpushaftertake_others). 300 s capped runs, same input script,VICTORY_D3D12_DRED=1.VICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1Per command (
VICTORY_GPU_TIMING=1, every timed command summed over ~170-190 s in the world):After the switches, the GPU time left is spread over many small mesh-shader
ExecuteIndirectdraws 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/L0copy (Host::shared_buffer). Test: 65,536 threads, each making 64 chained dword loads from a 16 MiB buffer.WRITE_BACKL0: 5.5 msDEFAULT, GPU upload, upload orWRITE_COMBINEL0: 0.05 ms (the buffer stays in L2)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.
@Marvisak on GitHub · 2026-11-25 18:18 UTC · original · edited 2026-11-25 19:21 UTC
Automated triage for the maintainers.
Needs work. It now merges cleanly into
main. CI: not run on this head.This adds opt-in (
VICTORY_GPU_UPLOAD_WRITES=1) GPU-upload-heap copies for title buffers that shaders write, with a system-memory mirror:pull,freshen,bring_backandsnapshots_aheadinshadow.rs,Host::copy_nowinhost.rs, and a pull inmemory::bind's copy-back. The world measurement in this thread (GPU time per frame from about 320 ms to about 100 ms on an RTX 5090) makes a good case for the direction.What blocks it:
docs/gpu-memory.md(256 lines, with charts and benchmark tables) goes against the short-docs rule. Please cut it to a few lines and put the measurements in the PR description.bring_backholds each copy's stale-page lock whilecopy_nowwaits on the title's main queue withWaitForSingleObject(INFINITE), and it is called from copy-back paths. Please explain why nothing already queued can wait on work that this thread or those locks block.pullshortcut leaves shader writes outside declared ranges and marked pages stale in the mirror. Please argue why no CPU reader can see those bytes.shadow.rsrefresh and copy-back logic still needs a full review pass after the rebase.Generated by Claude Code
@Machad3x on GitHub · 2026-11-25 19:41 UTC · original
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 (
put_back,take_and_read) have topushorfreshentoo, and a refresh of a range with saved bytes goes through the mirror soTracked::refreshedkeeps them current.host.rsneeds theINFINITEimport.cargo test -p victory-gpu --libpasses with both switches off and on (259 tests).Written with Claude Code; tested locally.
@fuzeofficlal on GitHub · 2026-11-25 19:50 UTC · original
Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP verification and comparison review of
9659ab92(on13f91ca):Verification (
rustc 1.98.1MSVC)cargo xtask policy: 185 files ok;cargo clippy --locked -p victory-gpu --all-targets(default &--features diagnostics): 0 warnings.VICTORY_D3D12_WARP=1,--features diagnostics --test-threads=1): 207 passed, 0 failed, 3 ignored (both unset andVICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1).shadow::tests17 passed, 0 failed, 2 ignored;draw::tests11 passed, 0 failed;memory::tests4 passed, 1 failed (only the pre-existing #1744 1M view-heap cap failure inlists_share_a_table_and_copies_keep_their_views).Architecture & Comparison Notes (vs #2120 / #2705)
git merge-treeconflicts with #2705 inshadow.rs), requires Resizable BAR (GPUUploadHeapSupported), holds per-copyv.stalelocks across synchronousHost::copy_now(WaitForSingleObject), and adds ~1.7k lines (includingdocs/gpu-memory.md) with unmarked-snapshot CPU overhead (708 ms -> 950 msin its own table).DEFAULTVRAM + batched same-queueCopyBufferRegionup/down outside page-bitmap locks, works without ReBAR) appears substantially simpler and more orthogonal than the fullGPU_UPLOADmirror state machine.@0xkn0wh3r3 on GitHub · 2026-11-26 00:27 UTC · original
Thanks. Rebased with #2120 onto
63bcca4; head is noweff9f91b. The branch has #2120's two commits (4941d712,b7e4a6f6) and three of its own:836b6b97,78e77d61,eff9f91b. Line numbers below are ateff9f91b.Stacked on #2120. Understood. Once #2120 is settled I'll rebase so only this PR's commits remain.
docs/gpu-memory.md. Removed, along with its link indocs/architecture.md, and the docs-only commit is dropped. What's left is a short paragraph in theshadow.rsmodule docs (lines 19-25): where copies live with each switch, and that the mirror's stale pages come back before the CPU reads them. The measurements are in the PR description now, including the bandwidth table that was only in the doc.bring_backwaiting on the queue with the stale-page locks held. Nothing already queued can wait on this thread or on those locks:copy_nowruns on the thread insideHost::execute, which holdssubmittedfrom preparation to copy-back (host.rs:1159). These aresnapshots_ahead(host.rs:1257) andShadow::writingfromrun_deferred(host.rs:2395), both beforeExecuteCommandLists. The others run after the submission'sWaitForSingleObject(self.event, INFINITE)(host.rs:1301):copy_back_marks(:1414),pull(:1423),copy_back/keep_writes, and thememory::bindcopy-back, adefer_afterclosure (memory.rs:644,:689). The diagnosticspeekruns indefer_host/defer_aftersteps, so it is insideexecutetoo. Only tests call it from anywhere else.host.rs:210). OnlyHost::execute(:1294) andcopy_now(:3833) submit to it, and both wait for their own work before returning. Whilecopy_nowruns, the thread holdssubmitted, so no submission of the title's lists can be queued. The previous one has already been waited for, and in the copy-back paths so has the current one.copy_nowtakesCOPIER(:3821) around its submit and wait, so at most one copy list is in flight. The queue therefore holds nothing but that list ofCopyBufferRegions.ID3D12CommandQueue::Wait. Title queue waits are held on the CPU (queue.rs:404-411,crate::hold): a segment reachesHost::execute(queue.rs:210) only once its waits are met. So the fencecopy_nowwaits for is signalled once the copies finish, whatever the CPU holds. If the device is removed, the fence completes as well;Host::execute's own wait already relies on that.staleover the wait only delays other takers. Those are the same thread (one submission at a time) or tests. The order is the same everywhere.bring_backtakesstalelocks in copy-address order, thenCOPIER, and while holding them takes nothing else.before→stale(copy_back_or_notatshadow.rs:519/531,update_trackedat:734) andahead→stale(copyat:819) are the only nestings, and nothing takesbeforeoraheadwhile holdingstale.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).
pullshortcut and CPU readers. You were right that one could see them, so this is fixed ineff9f91b. Every CPU read of a mirror goes throughfreshen, apullor a fresh write, except one: a later snapshot (Shadow::writing,shadow.rs:435) freshens only stale pages. GPU writes that nothing pulled were therefore read as the mirror's older bytes. That submission's copy-back then took the earlier GPU bytes as its own and merged them into title memory, over a byte the title wrote during the submission. Upstream's system-memory copy never does this, because its snapshot already holds those bytes. The writes nothing pulled were:bindless's shortcut);memory::bind, and held marked pages incopy_back_marks.The fix:
memory.rs:683-689, andcopy_back_marks). The copies keep the GPU's bytes either way.Shadow::written_unseen,shadow.rs:417). For root views that isShadow::may_write(shadow.rs:405), called with the part past the declared range (draw.rs:221,:886,:949). For the heap's views it isbindless::written_unseen(host.rs:1440, only withVICTORY_GPU_UPLOAD_WRITES). The next CPU read there brings video memory's bytes first, so a mirrored copy now behaves like a system-memory one: those bytes stay out of title memory, and a later snapshot holds them.writes_past_declared_ranges_leave_the_mirror_staleandheld_back_marked_pages_come_back_to_the_mirror. With the stale marks removed, the first fails at the title's byte (the GPU's earlier0x77overwrote the title's0x55). Without the held pages in the pull, the second fails.The
pulldoc comment no longer calls this a shortcut. It says where the mirror is marked stale instead.Full review pass of
shadow.rs. Noted. Over #2120, this PR'sshadow.rsdiff is about 910 lines before the fix and 1,040 with it, about 90 of those being the two new tests.Checks on
eff9f91b(RTX 5070 Ti, driver 621.24):cargo test -p victory-gpu --libpasses 241 with no switches, withVICTORY_GPU_UPLOAD=1, and with both switches. With both switches and--features diagnosticsit passes 255. Each configuration has the same 2 failures asmainhere (#1835).host::present_plane_tests::compositor_failure_presents_primary_in_the_same_frameexits the process with--features diagnosticsonmaintoo on this machine, so that run skips it.cargo clippy -p victory-gpu --all-targets, with and without--features diagnostics: clean.cargo xtask policy: ok.@catminou4 on GitHub · 2026-11-25 05:49 UTC · original · reviewed
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.
@MeMako on GitHub · 2026-11-26 04:37 UTC · original
Independent AMD RDNA 4 verification + live title measurement of
#2130(eff9f91, including#2120):Setup: Windows 11 (
10.0.26200), Ryzen 9 7950X, AMD Radeon RX 9070 XT (RDNA 4, driver32.0.32029.2010, Resizable BAR enabled, 16 GB VRAM), local integration branch onorigin/main63bcca4with#2051+#2472+#2918+#1010(in the local merge,copy_queueddelegatesself.vram.is_some()directly toself.copy, andcopy_back_markedrunsfreshenbefore#2918's diff andpushafter updating the mirror).Checks & microbenchmark:
cargo clippy -p victory-gpu -p victory --all-targets --locked -- -D warnings(default &--all-features): clean.cargo test -p victory-gpu --locked: 264 passed, 0 failed (including all#2130mirrored-copy tests andpartial_refresh_of_a_mirrored_copy_updates_saved_bytes).written_copies_in_video_and_system_memory(256 MiB, release):3.3 GB/seach way)160.1 GB/seach way — 49.2x)Live title run (
VICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1, 8 min):3,813copied title buffers (1,103.2 MBlogical;1,331.0 MBlive in GPU upload heaps including rounding/in-flight refs) sat comfortably inside the 2 GiB cap (22.8 MBread-only +1,080.5 MBwritable).>=4,000draws/frame, plus steady tail of last 20 heavy samples):copies)414.1tail)165.4tail)155.1tail)84.8tail)#2120only469.8tail)194.1tail)164.9tail)108.5tail)#2120 + #2130495.0tail)219.7tail)150.0tail)121.8tail)Cross-vendor takeaway (AMD RDNA 4 vs. NVIDIA):
#2130is rock-solid on RX 9070 XT and accelerates the synthetic 256 MiB streaming read/write test by 49.2x.155 msGPU time with system-memory copies (unlike NVIDIA's320–577 msL2-bypass cliff), so moving1.1 GBof writable buffers intoGPU_UPLOADVRAM only shaved ~4–8 ms off GPU time (156.7 → 152.8 msheavy;155.1 → 150.0 mstail).preparing+38–54 ms,copy_back+29–37 ms— matching the CPU delta reported on RTX 3080 Ti; our local breakdown sawsync_heapsrise from ~40 → 69 ms,deferredrise from ~73–84 → 99–100 mspartly due to serializingcopy_queuedfor mirrored copies, andmarksrise from ~29–30 → 33–35 µs/page).#2130a massive net win on NVIDIA GPUs, but net-negative on AMD RDNA 4 right now — good evidence for keepingVICTORY_GPU_UPLOAD_WRITESopt-in (or vendor-gated) rather than enabling it unconditionally on AMD.Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.
@0xkn0wh3r3 on GitHub · 2026-11-26 05:03 UTC · original
Thanks for the RDNA 4 runs, on this PR and on #2120. Both stay opt-in here:
VICTORY_GPU_UPLOADandVICTORY_GPU_UPLOAD_WRITESare off unless set. The per-phase breakdown is useful: the GPU time saved on the RX 9070 XT (~4–8 ms) is smaller than the addedpreparing/copy-back time, unlike the NVIDIA cards, where system-memory reads were the bottleneck. #2870 is the PR that would turn these on by default where the host has GPU upload heaps, so I'll take this into account there: the written-buffer half shouldn't be the default on hosts where it costs more than it saves.@0xkn0wh3r3 on GitHub · 2026-11-26 08:05 UTC · original
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.
Pull request closed