Skip to content

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

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

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
Contributor

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, snapshot updates under a busy range, and the zeros a refresh writes over unmapped pages.
  • Marked ranges are not pulled whole. Nothing reads the mirror for them, and copy_back_marks pulls their marked pages.
  • Writes nothing pulls make the mirror stale there (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.md is gone (and its link from docs/architecture.md); a short paragraph in the shadow.rs module 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:

GPU reads CPU writes CPU reads
system memory (L0, the default for copies) 22.9 GB/s 28.7 GB/s 22.7 GB/s
GPU upload heap (video memory through the BAR) 716.8 GB/s 22.7 GB/s 0.05 GB/s
video memory, default heap 721.2 GB/s (no CPU access) (no CPU access)

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

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 241 2
VICTORY_GPU_UPLOAD=1 241 2
both 241 2
both, --features diagnostics 255 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).

Rebased onto 13f91ca

  • Withheld writes: upstream 0a5be3c withholds 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.
  • Upstream 2a4938e zeroes 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.
  • Still stacked on GPU: copies of read-only title buffers in GPU upload heaps (opt-in) #2120. Its function mapped_buffer(size, upload) is now shared_mapped_buffer.

Rebased onto f3578e3

  • Upstream 106377d makes a refresh hand what it copied to 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's data, 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 (new Tracked::saves) writes the mirror too, and then hands the bytes over; every other refresh still writes video memory only. New test partial_refresh_of_a_mirrored_copy_updates_saved_bytes runs upstream's partial-refresh test with mirrored copies; without the mirror write it fails.
  • Upstream 5319366 merged update_busy and update_free into update_tracked. Snapshot updates under a busy range still write the mirror where a refresh wrote video memory only (the "update_busy does 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.
  • Still stacked on GPU: copies of read-only title buffers in GPU upload heaps (opt-in) #2120.
  • cargo test -p victory-gpu --lib with no switches and with both switches: the same two failures as on upstream main here. cargo clippy -p victory-gpu --all-targets, with and without --features diagnostics: clean.

Triage: rebased onto 63bcca4, docs dropped, unpulled writes

  • Rebased with GPU: copies of read-only title buffers in GPU upload heaps (opt-in) #2120 onto 63bcca4. The branch now has GPU: copies of read-only title buffers in GPU upload heaps (opt-in) #2120's two commits and three of its own; the docs-only commit is gone.
  • docs/gpu-memory.md is removed (see Docs above).
  • New commit 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_back waits for the GPU (Host::copy_now) with the copies' stale-page locks held. Every caller runs inside Host::execute under the submitted lock: 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, before Host::execute runs).
  • Test counts in the table above are for this head. host::present_plane_tests::compositor_failure_presents_primary_in_the_same_frame exits the process with --features diagnostics on this machine, on main too, so the diagnostics run skips it. cargo clippy -p victory-gpu --all-targets, with and without --features diagnostics: clean. cargo xtask policy: ok.

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

@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
Contributor

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.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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_back and snapshots_ahead in shadow.rs, Host::copy_now in host.rs, and a pull in memory::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:


Generated by Claude Code

@Machad3x

Copy link
Copy Markdown

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

frame preparing GPU copy-back
neither switch 919 203 577 135
#2120 only 881 189 557 131
+ 03590b5 869 288 296 282
+ 0b9cd11 792 270 334 183

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 to push or freshen too, and a refresh of a range with saved bytes goes through the mirror so Tracked::refreshed keeps them current. host.rs needs the INFINITE import. cargo test -p victory-gpu --lib passes with both switches off and on (259 tests).

Written with Claude Code; tested locally.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP verification and comparison review of 9659ab92 (on 13f91ca):

Verification (rustc 1.98.1 MSVC)

  • cargo xtask policy: 185 files ok; cargo clippy --locked -p victory-gpu --all-targets (default & --features diagnostics): 0 warnings.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics --test-threads=1): 207 passed, 0 failed, 3 ignored (both unset and VICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1).
  • RTX 5090 (both flags enabled): shadow::tests 17 passed, 0 failed, 2 ignored; draw::tests 11 passed, 0 failed; memory::tests 4 passed, 1 failed (only the pre-existing Test report: RTX 5070 (Blackwell), Windows 11, 70564ae: 471 passed, 5 failed, 7 ignored (4 from the 1,000,000 view heap cap) #1744 1M view-heap cap failure in lists_share_a_table_and_copies_keep_their_views).

Architecture & Comparison Notes (vs #2120 / #2705)

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
…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.
@0xkn0wh3r3
0xkn0wh3r3 force-pushed the gpu/upload-heap-written-copies branch from 9659ab9 to 8ab1672 Compare November 25, 2026 22:22
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.
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks. Rebased with #2120 onto 63bcca4; head is now eff9f91b. The branch has #2120's two commits (4941d712, b7e4a6f6) and three of its own: 836b6b97, 78e77d61, eff9f91b. Line numbers below are at eff9f91b.

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 in docs/architecture.md, and the docs-only commit is dropped. What's left is a short paragraph in the shadow.rs module 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_back waiting on the queue with the stale-page locks held. Nothing already queued can wait on this thread or on those locks:

  • Who calls it. Every path to copy_now runs on the thread inside Host::execute, which holds submitted from preparation to copy-back (host.rs:1159). These are snapshots_ahead (host.rs:1257) and Shadow::writing from run_deferred (host.rs:2395), both before ExecuteCommandLists. The others run after the submission's WaitForSingleObject(self.event, INFINITE) (host.rs:1301): copy_back_marks (:1414), pull (:1423), copy_back/keep_writes, and the memory::bind copy-back, a defer_after closure (memory.rs:644, :689). The diagnostics peek runs in defer_host/defer_after steps, so it is inside execute too. Only tests call it from anywhere else.
  • What is on the queue. The host has one queue (host.rs:210). Only Host::execute (:1294) and copy_now (:3833) submit to it, and both wait for their own work before returning. While copy_now runs, the thread holds submitted, 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_now takes COPIER (:3821) around its submit and wait, so at most one copy list is in flight. The queue therefore holds nothing but that list of CopyBufferRegions.
  • What GPU work can wait on. No host list waits on anything the CPU does. Nothing in victory-gpu calls ID3D12CommandQueue::Wait. Title queue waits are held on the CPU (queue.rs:404-411, crate::hold): a segment reaches Host::execute (queue.rs:210) only once its waits are met. So the fence copy_now waits 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.
  • The locks. GPU work takes no CPU locks, so holding stale over 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_back takes stale locks in copy-address order, then COPIER, and while holding them takes nothing else. before → stale (copy_back_or_not at shadow.rs:519/531, update_tracked at :734) and ahead → stale (copy at :819) are the only nestings, and nothing takes before or ahead while holding stale.

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

pull shortcut and CPU readers. You were right that one could see them, so this is fixed in eff9f91b. Every CPU read of a mirror goes through freshen, a pull or 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:

  • past a root view's declared 1 MiB (host root UAVs, and heap-root views of copies without marks);
  • through the heap's unordered views of copies without marks (bindless's shortcut);
  • on pages of programs using title memory whose results are held back: memory-path pages in memory::bind, and held marked pages in copy_back_marks.

The fix:

  • Held-back pages are now pulled with the others (memory.rs:683-689, and copy_back_marks). The copies keep the GPU's bytes either way.
  • After a submission, the mirror's pages are marked stale wherever shaders may have written with nothing pulled after (Shadow::written_unseen, shadow.rs:417). For root views that is Shadow::may_write (shadow.rs:405), called with the part past the declared range (draw.rs:221, :886, :949). For the heap's views it is bindless::written_unseen (host.rs:1440, only with VICTORY_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.
  • New tests: writes_past_declared_ranges_leave_the_mirror_stale and held_back_marked_pages_come_back_to_the_mirror. With the stale marks removed, the first fails at the title's byte (the GPU's earlier 0x77 overwrote the title's 0x55). Without the held pages in the pull, the second fails.

The pull doc 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's shadow.rs diff 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):

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
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.
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
… 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.
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
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.
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 26, 2026
… 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>
@MeMako

MeMako commented Nov 26, 2026

Copy link
Copy Markdown

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, driver 32.0.32029.2010, Resizable BAR enabled, 16 GB VRAM), local integration branch on origin/main 63bcca4 with #2051 + #2472 + #2918 + #1010 (in the local merge, copy_queued delegates self.vram.is_some() directly to self.copy, and copy_back_marked runs freshen before #2918's diff and push after 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 #2130 mirrored-copy tests and partial_refresh_of_a_mirrored_copy_updates_saved_bytes).
  • written_copies_in_video_and_system_memory (256 MiB, release):
    • System memory: 82.59 ms (3.3 GB/s each way)
    • GPU upload heap: 1.68 ms (160.1 GB/s each way — 49.2x)
    • Mirror pull: 0.148 ms for 64 written 64 KiB pages (4 MiB); 0.060 ms for 1 page

Live title run (VICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1, 8 min):

  • Stability & VRAM footprint: 0 crashes, 0 device removals, 0 upload-budget fallbacks. In steady state, 3,813 copied title buffers (1,103.2 MB logical; 1,331.0 MB live in GPU upload heaps including rounding/in-flight refs) sat comfortably inside the 2 GiB cap (22.8 MB read-only + 1,080.5 MB writable).
  • World frame medians (>=4,000 draws/frame, plus steady tail of last 20 heavy samples):
Run Samples Frame (med) Preparing (copies) GPU (med / avg) Copy-back
Baseline (switches off) 66 (tail 20) 445.1 ms (414.1 tail) 197.3 ms (165.4 tail) 156.7 / 163.3 ms (155.1 tail) 89.2 ms (84.8 tail)
#2120 only 96 (tail 20) 471.4 ms (469.8 tail) 196.4 ms (194.1 tail) 166.0 / 177.3 ms (164.9 tail) 105.6 ms (108.5 tail)
#2120 + #2130 63 (tail 20) 512.0 ms (495.0 tail) 235.0 ms (219.7 tail) 152.8 / 159.5 ms (150.0 tail) 118.1 ms (121.8 tail)

Cross-vendor takeaway (AMD RDNA 4 vs. NVIDIA):

  • Mechanically, #2130 is rock-solid on RX 9070 XT and accelerates the synthetic 256 MiB streaming read/write test by 49.2x.
  • In a live world frame, however, RX 9070 XT is already at ~155 ms GPU time with system-memory copies (unlike NVIDIA's 320–577 ms L2-bypass cliff), so moving 1.1 GB of writable buffers into GPU_UPLOAD VRAM only shaved ~4–8 ms off GPU time (156.7 → 152.8 ms heavy; 155.1 → 150.0 ms tail).
  • Meanwhile, CPU prep and copy-back rose (preparing +38–54 ms, copy_back +29–37 ms — matching the CPU delta reported on RTX 3080 Ti; our local breakdown saw sync_heaps rise from ~40 → 69 ms, deferred rise from ~73–84 → 99–100 ms partly due to serializing copy_queued for mirrored copies, and marks rise from ~29–30 → 33–35 µs/page).
  • That makes #2130 a massive net win on NVIDIA GPUs, but net-negative on AMD RDNA 4 right now — good evidence for keeping VICTORY_GPU_UPLOAD_WRITES opt-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

Copy link
Copy Markdown
Contributor Author

Thanks for the RDNA 4 runs, on this PR and on #2120. Both stay opt-in here: VICTORY_GPU_UPLOAD and VICTORY_GPU_UPLOAD_WRITES are 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 added preparing/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

Copy link
Copy Markdown
Contributor Author

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.

@0xkn0wh3r3 0xkn0wh3r3 closed this Nov 26, 2026
BastienRodz pushed a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants