Skip to content

GPU: the views' written-pages bitmap lives in video memory - #2465

Draft
wbkepler wants to merge 2 commits into
Marvisak:mainfrom
wbkepler:gpu/view-marks-in-vram
Draft

wbkepler wants to merge 2 commits into
Marvisak:mainfrom
wbkepler:gpu/view-marks-in-vram

Conversation

@wbkepler

Copy link
Copy Markdown
Contributor

What

Translated shaders mark each 64 KiB page they write through views ("Write marks", crate::shadow::copy_back_marks) with an atomic OR into one bitmap. That bitmap was a shared buffer in CPU-cached system memory. On an NVIDIA host, atomics there cost about 600 times what they cost in video memory: 1M InterlockedOr to one dword take 18 ms there and 0.03 ms in video memory. The lanes of a pass writing one region all OR the same few dwords.

The bitmap now lives in video memory:

  • shadow::bitmap() makes a DEFAULT-heap UAV buffer (Host::uav_buffer), a mapped read-back buffer (Host::readback_buffer) and an upload buffer of zeros. It also records one list, once, and closes it: a UAV barrier, a copy of the bitmap into the read-back buffer, COPY_SOURCE -> COPY_DEST, and a copy of the zeros over the bitmap.
  • A list that binds the bitmap says so (HostList::marks_views, from draw.rs). Host::execute then runs that closed list after the submission's lists, and copy_back_marks reads the marks from the read-back mapping. The CPU no longer clears them.
  • Lists record nothing more, so there is no extra barrier inside them. The cost is two 512 KiB copies per submission that marks. Two runs of one list in a submission both leave their marks.
  • Only the bitmap's last bit (where marks of offsets past it land) is ignored now. Before, the whole last dword was ignored.

Nothing changes in the translator, so shader cache entries stay valid.

Based on #2175 (gpu/title-memory-bitmap-in-vram, which did the same for the title memory page bitmaps in memory.rs). This branch includes its commit, for Host::uav_buffer / Host::readback_buffer and the memory::tests shader helper (now pub(crate)). Review the second commit only; it rebases trivially once #2175 lands.

Verification (Windows 11, NVIDIA RTX 5090)

Live runs on an integration build (this change on top of several open PRs needed to reach the world on a 32-lane host). Diag build, same settings and same 300 s script, averaged over 150–285 s in the world (about 4,900 draws per frame):

fps ms/frame GPU ms copy-back ms preparing ms
before 2.55 395 85.7 111.8 176
this PR 2.38 423 85.2 115.2 201

GPU and copy-back time are unchanged within noise in this scene. The frame-time difference is in "preparing", the CPU side, which this change does not touch and which varies about as much between runs of identical builds (another build of the same series measured 2.13 fps / 208 ms there). The marks' atomics are evidently not a large share of the GPU time here. With them in video memory they stay cheap where passes mark heavily, as the microbenchmark shows. Stable: no device removal, and world captures look like before.

A first version gave each list its own bitmap (cleared where the list first used it, read back at its end, as #2175 does). It cost about 5 ms more GPU time per frame (an extra barrier inside each list plus two 512 KiB copies per list), so this version uses one bitmap and one fixed list per submission.

  • cargo clippy -p victory-gpu --all-targets -- -D warnings, cargo clippy -p victory --features diagnostics --all-targets -- -D warnings: clean
  • cargo test -p victory-gpu: 143 passed. New shadow::tests::view_marks_are_read_back_and_cleared_each_submission: two lists mark from a shader, and both marks are read back; the next submission's read-back holds only its own mark. marked_pages_go_back_by_the_bytes_the_gpu_changed now takes the marks as read back. Two tests fail identically on main on this host because they assume 2^21 view slots and NVIDIA caps heaps at 1,000,000: memory::tests::lists_share_a_table_and_copies_keep_their_views and descriptor::host::tests::dynamic_slots_are_held_until_released.
  • cargo xtask policy: ok

🤖 Generated with Claude Code

wbkepler and others added 2 commits November 25, 2026 14:34
Translated shaders that use title memory mark every page they write
(and every page they miss) with an atomic OR into their list's page
bitmap. That bitmap was a shared buffer in system memory, so each of
those atomics went over the bus, and the lanes of a pass writing one
output region all hit the same few dwords one after another. On an
NVIDIA host the title's acceleration-structure build passes (two
straight-line compute programs with two root UAVs, dispatched a few
thousand times in one list) then ran for seconds and the device was
removed with DEVICE_HUNG.

The bitmap is now a video-memory buffer: the list clears it (a copy from
a shared zero buffer) where it first uses title memory and copies it
into a read-back buffer when it is closed (HostList::read_back_at_close),
so every run of the list, also a repeated one, starts clear and the CPU
reads the marks after the run as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Translated shaders mark each page they write through views ("Write
marks") with an atomic OR into one bitmap. That bitmap was a shared
buffer in CPU-cached system memory, where on an NVIDIA host atomics cost
about 600 times what they cost in video memory (1M ORs to one dword:
18 ms against 0.03 ms), and the lanes of a pass writing one region all
OR the same few dwords.

The bitmap is now a video-memory buffer. A list recorded once and closed
copies it into a mapped read-back buffer and clears it (a copy from
zeros in upload memory); a submission runs that list after its own lists
when one of them binds the bitmap (HostList::marks_views), and the
copy-back reads the marks from the read-back buffer. Lists record
nothing more, and two runs of one list in a submission both leave their
marks. Only the last bit of the bitmap (where marks past it land) is
ignored now, not its whole last dword.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kotekompotek

Copy link
Copy Markdown

Reviewed — this is a real device-removal fix, not just perf: >2 s marking lists trip TDR, and the 600× atomic-write penalty on a shared-heap buffer explains why. Both bitmaps follow the same sound pattern:

  • clear moved GPU-side at list start (zeros copy inside the list recording, so a re-recorded list starts clean — previously the CPU take cleared, which only ran if the read-back ran)
  • read back at close via UAV→COPY_SOURCE barrier + copy into a mapped readback buffer; CPU side reads it as const — no mutation, so the mem::take clearing is correctly replaced by the GPU zeros copy
  • marks_views tracking means marks_tail is appended only when a marking list ran, so copy_back_marks always reads a populated read-back
  • last-bit masking moved from "clear word" to word & !(1 << 31) — equivalent on a read-only slice

The video-memory view_marks_are_read_back_and_cleared_each_submission test exercises the full path on a real device (runs the marking pipeline).

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

Written-pages bitmap moved to video memory (+438). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.

@kais7654

Copy link
Copy Markdown

Verified on the branch (Windows 11, RTX 5070 Laptop GPU, driver 616.87,
hardware + WARP, rustc 1.98.1, --locked): merge into current main
(67e8ae1) is clean, clippy -D warnings clean with and without
--all-features, and the runs are green on the machines' known anomalies:
victory-gpu lib 143 passed / 2 failed (the two heap-cap tests #1943's
fix owns), victory-shader lib 244 passed / 3 failed (the gathers + 2
stride tests of the NVIDIA HEAP_VIEWS family, #2317). No new failure
against a same-base control.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This branch now conflicts with main in crates/victory-gpu/src/host.rs, memory.rs and shadow.rs. It will be reviewed once it is rebased and out of draft.


Generated by Claude Code

github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
Two 256.5 KiB copies, the bitmap into read-back memory and zeros over
it, as the video-memory bitmaps of Marvisak#2175 and Marvisak#2465 record at a list's
end or after a submission: 12.2 µs on an RTX 5070 Ti (median of 20).
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
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.

5 participants