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

Open
github-bot wants to merge 2 commits from github-bot/gh-2465 into main AGit
Owner

@⁠wbkepler on GitHub · 2026-11-25 14:04 UTC · original
wbkepler:gpu/view-marks-in-vram → main · +438 −48 · 4 files · 2 commits · draft

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** on GitHub · 2026-11-25 14:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/2465) `wbkepler:gpu/view-marks-in-vram` → `main` · +438 −48 · 4 files · 2 commits · draft ## 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](https://claude.com/claude-code)
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 <[email protected]>
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:06:42 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 14:24 UTC · original

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

**@⁠kotekompotek** on GitHub · 2026-11-25 14:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2465#issuecomment-7165218466) 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).
Author
Owner

@⁠kais7654 on GitHub · 2026-11-25 17:12 UTC · original

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.

**@⁠kais7654** on GitHub · 2026-11-25 17:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2465#issuecomment-7169169520) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:20 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 18:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2465#issuecomment-7170768100) <!-- victory-triage v1 head=482899f10a37e068da2434823ada17068782f015 verdict=needs-rebase at=2026-11-25T18:31:00Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 14:57 UTC · original · reviewed

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.

**@⁠catminou4** on GitHub · 2026-11-25 14:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2465#pullrequestreview-6059995984) · **reviewed** 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.
Author
Owner

@⁠latentdev on GitHub · 2026-11-26 14:46 UTC · original

In the title on an RTX 4090: the bitmap's place was ~90 % of the GPU's time (on a tree with more marks)

A data point where this change matters a lot, plus notes on porting it to current main.

Setup

  • RTX 4090, driver 621.24, Windows 11 26300.
  • Retail 1.0.2.1, Series X profile, diag build, warm caches.
  • Scene: the opening cutscene into the first gameplay.
  • Tree: main 63bcca4 + #1630, #1832, #2332, #2178, #2121, #439, #2214, with the structure builder held back.
  • That tree marks more than main does: #2121's views read through paged heaps set page-read marks in the same bitmap.
  • Medians of the frame-rate lines over the world:
views' bitmap ms/frame GPU ms samples
system memory (as on main) 5,537 4,627 141
video memory (this PR's approach) 745 528 ¹ 35

¹ measured with VICTORY_GPU_TIMING=1, which adds about a quarter here.

With the bitmap in system memory, VICTORY_GPU_TIMING named:

  • deferred_tiled:CS_ResetTilesMask (a7eeed0646be1ab7) at about 1 s per dispatch;
  • full-screen passes at about 100 ms each.

With it in video memory, most commands drop to a few milliseconds. One program still runs long in some frames (100 ms to 1 s): depth_pyramid:CS_InitDepthPyramidWithCpuOcclusion3x3 (42683eebec081520), which I'm still looking at. Adding #3102's copies in video memory took the GPU to about 140 ms per frame (numbers on #3102).

Porting it to main 63bcca4. Since this PR was written, main has made two changes to the bitmap:

  • it split it in two (bitmap(memory); marks of shaders using title memory go back only while not withheld);
  • it added read marks (VIEW_READS, take_reads, forget_read, reads_since).

What worked here, keeping all of the CPU-side code:

  • Each Bitmap is a DEFAULT-heap UAV buffer (Host::uav_buffer), plus a read-back buffer, an upload buffer of zeros, and one closed tail list (UAV barrier, copy to read-back, COPY_SOURCE -> COPY_DEST, copy of zeros).
  • Host::execute appends both tails to every submission's lists.
  • Bitmap::data now points at the read-back mapping. take_reads, take_page_reads, copy_back_marks and forget_read then work unchanged on cached memory.
  • One change in meaning: read marks used to stay set in the bitmap between reads_since calls. The GPU copy is now cleared after each submission, so take_reads keeps their union on the CPU for reads_since.

I ran it in about fifteen runs of up to 9 minutes. The removals in them were of two kinds, and neither points at this change:

  • the #71 hang family, after vfx_proj_decal_hit_detection programs bind;
  • the PC running out of memory: a monitor alongside showed commit at its limit and under 1 GB of RAM free at the removal.

I can post the patch if it helps the rebase.

**@⁠latentdev** on GitHub · 2026-11-26 14:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/2465#issuecomment-7195311142) ### In the title on an RTX 4090: the bitmap's place was ~90 % of the GPU's time (on a tree with more marks) A data point where this change matters a lot, plus notes on porting it to current `main`. **Setup** - RTX 4090, driver 621.24, Windows 11 26300. - Retail 1.0.2.1, Series X profile, diag build, warm caches. - Scene: the opening cutscene into the first gameplay. - Tree: `main` 63bcca4 + #1630, #1832, #2332, #2178, #2121, #439, #2214, with the structure builder held back. - That tree marks more than `main` does: #2121's views read through paged heaps set page-read marks in the same bitmap. - Medians of the frame-rate lines over the world: | views' bitmap | ms/frame | GPU ms | samples | |---|---:|---:|---:| | system memory (as on `main`) | 5,537 | 4,627 | 141 | | video memory (this PR's approach) | 745 | 528 ¹ | 35 | ¹ measured with `VICTORY_GPU_TIMING=1`, which adds about a quarter here. With the bitmap in system memory, `VICTORY_GPU_TIMING` named: - `deferred_tiled:CS_ResetTilesMask` (`a7eeed0646be1ab7`) at about 1 s per dispatch; - full-screen passes at about 100 ms each. With it in video memory, most commands drop to a few milliseconds. One program still runs long in some frames (100 ms to 1 s): `depth_pyramid:CS_InitDepthPyramidWithCpuOcclusion3x3` (`42683eebec081520`), which I'm still looking at. Adding #3102's copies in video memory took the GPU to about 140 ms per frame (numbers on #3102). **Porting it to `main` 63bcca4.** Since this PR was written, `main` has made two changes to the bitmap: - it split it in two (`bitmap(memory)`; marks of shaders using title memory go back only while not withheld); - it added read marks (`VIEW_READS`, `take_reads`, `forget_read`, `reads_since`). What worked here, keeping all of the CPU-side code: - Each `Bitmap` is a `DEFAULT`-heap UAV buffer (`Host::uav_buffer`), plus a read-back buffer, an upload buffer of zeros, and one closed tail list (UAV barrier, copy to read-back, `COPY_SOURCE -> COPY_DEST`, copy of zeros). - `Host::execute` appends both tails to every submission's lists. - `Bitmap::data` now points at the read-back mapping. `take_reads`, `take_page_reads`, `copy_back_marks` and `forget_read` then work unchanged on cached memory. - One change in meaning: read marks used to stay set in the bitmap between `reads_since` calls. The GPU copy is now cleared after each submission, so `take_reads` keeps their union on the CPU for `reads_since`. I ran it in about fifteen runs of up to 9 minutes. The removals in them were of two kinds, and neither points at this change: - the #71 hang family, after `vfx_proj_decal_hit_detection` programs bind; - the PC running out of memory: a monitor alongside showed commit at its limit and under 1 GB of RAM free at the removal. I can post the patch if it helps the rebase.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
  • crates/victory-gpu/src/memory.rs
  • crates/victory-gpu/src/shadow.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2465/head:github-bot/gh-2465'
git switch 'github-bot/gh-2465'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2465'
git switch 'github-bot/gh-2465'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2465'
git switch 'github-bot/gh-2465'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2465'
git switch 'main'
git merge --squash 'github-bot/gh-2465'
git switch 'main'
git merge --ff-only 'github-bot/gh-2465'
git switch 'main'
git merge 'github-bot/gh-2465'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2465
No description provided.