GPU: pool and recycle shadow::exact() buffers with GPU quarantine (#346) #451

Open
github-bot wants to merge 1 commit from github-bot/gh-451 into a-publier AGit
Owner

@⁠BE29ZZ on GitHub · 2026-11-27 02:27 UTC · original
BE29ZZ:feat/issue-346-exact-buffer-pool → a-publier · +642 −50 · 2 files · 1 commits · open

Addresses #346 (item 1: \shadow::exact()\ lock-free allocation + quarantined recycled buffer pool), independently confirmed in #368.

Problem

In \crates/victory-gpu/src/shadow.rs, \shadow::exact()\ held a single global \Mutex\ across lookups, ran an \O(N)\ \map.retain(...)\ sweep on every miss once \map.len() >= 256, and called \host()?.shared_buffer(...)\ (\CreateCommittedResource) while holding the mutex. Under heavy ring-buffer turnover (#346 reported ~716 misses/frame), list threads serialized on D3D12 buffer creation and constantly created/destroyed committed buffers.

What changed

  • Lock-stratified cache (\ExactStore\ in \crates/victory-gpu/src/shadow.rs):
    • Read hits take only \RwLock::read()\ and update \last_frame\ via \AtomicU64, eliminating read-hit contention across parallel \ ictory-list-*\ threads.
    • Throttled, single-winner (CAS-guarded) idle pruning evicts entries with
      ow_frame >= last_frame + 2, \Arc::strong_count == 1, and \host::references == 1.
    • \map\ and \pool\ locks are never nested (deadlock-free), and both \shared_buffer()\ allocation and overflow \host::retire()\ run outside all locks.
  • Power-of-two GPU-quarantined buffer pool (\ExactPool):
    • Buffers are allocated in power-of-two buckets (\EXACT_MIN_ALLOC = 64 KiB\ up to \64 MiB), matching D3D12's 64 KiB committed-resource granularity so small off-element views share one bucket with no extra RAM cost.
    • Evicted buffers stay in quarantine until both \EXACT_QUARANTINE_FRAMES\ (2 more frames, 4 frames total past last use) and every GPU submission started or queued at eviction (\host::submissions()\ + \queue::queued()) have completed.
    • Recycled buffers zero all previously touched bytes (\dirty_len, rounded to 256 B) before reuse so unmapped holes or trailing reads never observe stale bytes, and update their diagnostic object name.
    • Race-loser allocations (never exposed to any GPU command) return directly to the ready pool.
    • Overflow evictions use \crate::host::retire(shadow)\ (respecting \ury_lag()) instead of dropping immediately.
    • Budget defaults to 256 MiB when
      esidency::limited_host_ram()\ is set, else 320 MiB; configurable via \VICTORY_EXACT_POOL_MB=\ (\ disables pooling while keeping shared read locks and out-of-lock allocation).
    • \shadow::let_go_idle()\ graduates retired entries and drains ready pooled buffers under host memory pressure (
      esidency::pressured()\ or \RamBand::High).
    • \exact_line(frames)\ now reports asked, new, recycled, kept, quarantined, and pooled MiB.
  • Updated existing test (\crates/victory-gpu/src/descriptor/host.rs):
    • Updated \ iews_off_their_elements_get_a_copy_of_their_bytes\ to check \shadow::exact_bucket(len)\ instead of the pre-pool \len.next_multiple_of(256)\ width.

Relation to #443

#443 also moves \exact()\ allocation out of its lock (and covers \loose()\ / \xmem::change()), without buffer pooling. This PR focuses strictly on \shadow::exact()\ and combines shared read locks + out-of-lock allocation with the power-of-two GPU-quarantined recycling pool described in #346.

Verification (Windows 11 x64, NVIDIA GeForce RTX 3060, Rust 1.98.1)

  • \cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: pass (0 warnings)
  • \cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: pass (0 warnings)
  • \cargo test -p victory-gpu --lib --locked -- --test-threads=1: 402 passed, 0 failed (34 ignored), including 6 new tests in \shadow::exact_tests:
    • Bucket rounding and RAM/override pool-limit logic
    • Quarantine waiting for both frame lag and GPU submission completion + cap enforcement
    • Pressure drain freeing ready buffers without disturbing quarantine
    • End-to-end RTX 3060 GPU recycle test verifying same D3D12 \ID3D12Resource\ reuse and full stale-byte zeroing
    • 8-thread concurrent miss race verifying canonical \Arc\ identity and clean loser pooling
  • \cargo test -p victory-gpu --lib --all-features --locked -- exact_tests: 6 passed, 0 failed
  • \cargo test -p xtask --locked: 9 passed, 0 failed

Credit to @⁠ToolxMaster (#346) for profiling, designing, and measuring the quarantined pool, and to @⁠Miquella135 (#368) for independently validating it.
Not tested in the title itself (no game files on this machine); ready for in-game validation.

AI-assisted; no game data.

**@⁠BE29ZZ** on GitHub · 2026-11-27 02:27 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451) `BE29ZZ:feat/issue-346-exact-buffer-pool` → `a-publier` · +642 −50 · 2 files · 1 commits · open Addresses #346 (item 1: \shadow::exact()\ lock-free allocation + quarantined recycled buffer pool), independently confirmed in #368. ### Problem In \crates/victory-gpu/src/shadow.rs\, \shadow::exact()\ held a single global \Mutex\ across lookups, ran an \O(N)\ \map.retain(...)\ sweep on every miss once \map.len() >= 256\, and called \host()?.shared_buffer(...)\ (\CreateCommittedResource\) while holding the mutex. Under heavy ring-buffer turnover (#346 reported ~716 misses/frame), list threads serialized on D3D12 buffer creation and constantly created/destroyed committed buffers. ### What changed - **Lock-stratified cache (\ExactStore\ in \crates/victory-gpu/src/shadow.rs\):** - Read hits take only \RwLock::read()\ and update \last_frame\ via \AtomicU64\, eliminating read-hit contention across parallel \ ictory-list-*\ threads. - Throttled, single-winner (CAS-guarded) idle pruning evicts entries with \ ow_frame >= last_frame + 2\, \Arc::strong_count == 1\, and \host::references == 1\. - \map\ and \pool\ locks are never nested (deadlock-free), and both \shared_buffer()\ allocation and overflow \host::retire()\ run outside all locks. - **Power-of-two GPU-quarantined buffer pool (\ExactPool\):** - Buffers are allocated in power-of-two buckets (\EXACT_MIN_ALLOC = 64 KiB\ up to \64 MiB\), matching D3D12's 64 KiB committed-resource granularity so small off-element views share one bucket with no extra RAM cost. - Evicted buffers stay in quarantine until **both** \EXACT_QUARANTINE_FRAMES\ (2 more frames, 4 frames total past last use) **and** every GPU submission started or queued at eviction (\host::submissions()\ + \queue::queued()\) have completed. - Recycled buffers zero all previously touched bytes (\dirty_len\, rounded to 256 B) before reuse so unmapped holes or trailing reads never observe stale bytes, and update their diagnostic object name. - Race-loser allocations (never exposed to any GPU command) return directly to the ready pool. - Overflow evictions use \crate::host::retire(shadow)\ (respecting \ury_lag()\) instead of dropping immediately. - Budget defaults to **256 MiB** when \ esidency::limited_host_ram()\ is set, else **320 MiB**; configurable via \VICTORY_EXACT_POOL_MB=<MiB>\ (\\ disables pooling while keeping shared read locks and out-of-lock allocation). - \shadow::let_go_idle()\ graduates retired entries and drains ready pooled buffers under host memory pressure (\ esidency::pressured()\ or \RamBand::High\). - \exact_line(frames)\ now reports asked, new, recycled, kept, quarantined, and pooled MiB. - **Updated existing test (\crates/victory-gpu/src/descriptor/host.rs\):** - Updated \ iews_off_their_elements_get_a_copy_of_their_bytes\ to check \shadow::exact_bucket(len)\ instead of the pre-pool \len.next_multiple_of(256)\ width. ### Relation to #443 #443 also moves \exact()\ allocation out of its lock (and covers \loose()\ / \xmem::change()\), without buffer pooling. This PR focuses strictly on \shadow::exact()\ and combines shared read locks + out-of-lock allocation with the power-of-two GPU-quarantined recycling pool described in #346. ### Verification (Windows 11 x64, NVIDIA GeForce RTX 3060, Rust 1.98.1) - \cargo clippy -p victory-gpu --all-targets --locked -- -D warnings\: **pass** (0 warnings) - \cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings\: **pass** (0 warnings) - \cargo test -p victory-gpu --lib --locked -- --test-threads=1\: **402 passed, 0 failed** (34 ignored), including 6 new tests in \shadow::exact_tests\: - Bucket rounding and RAM/override pool-limit logic - Quarantine waiting for **both** frame lag and GPU submission completion + cap enforcement - Pressure drain freeing ready buffers without disturbing quarantine - End-to-end RTX 3060 GPU recycle test verifying same D3D12 \ID3D12Resource\ reuse and full stale-byte zeroing - 8-thread concurrent miss race verifying canonical \Arc<Shadow>\ identity and clean loser pooling - \cargo test -p victory-gpu --lib --all-features --locked -- exact_tests\: **6 passed, 0 failed** - \cargo test -p xtask --locked\: **9 passed, 0 failed** Credit to @⁠ToolxMaster (#346) for profiling, designing, and measuring the quarantined pool, and to @⁠Miquella135 (#368) for independently validating it. Not tested in the title itself (no game files on this machine); ready for in-game validation. AI-assisted; no game data.
Replace the single global mutex and per-miss O(N) sweep/allocation in shadow::exact() with a lock-stratified cache and a power-of-two recycling pool:

- Concurrent cache hits take only an RwLock read guard (no serialization across list threads).
- Idle pruning is throttled and single-winner (CAS-guarded), never scanning on every miss.
- Host D3D12 buffer creation (shared_buffer) and overflow retirement (host::retire) run outside all locks; map and pool locks are never nested.
- Evicted buffers wait in quarantine for both EXACT_QUARANTINE_FRAMES (2 frames past 2 idle frames = 4 frames total) and completion of all GPU submissions started or queued at eviction before re-entering the ready pool.
- Recycled buffers are rounded to power-of-two buckets (64 KiB min, 64 MiB max), zeroed across previously touched bytes before reuse, and drained under host memory pressure in shadow::let_go_idle().
- Budget defaults to 256 MiB under limited host RAM else 320 MiB (VICTORY_EXACT_POOL_MB=<MiB>, 0 disables pooling).

Design and measurements from #346 (@ToolxMaster) and #368 (@Miquella135).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 02:29:59 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-27 03:55 UTC · original

Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At d03efab: cargo test --release -p victory-gpu --lib 402 passed, 0 failed, 34 ignored.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-27 03:55 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7209382371) Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At `d03efab`: `cargo test --release -p victory-gpu --lib` 402 passed, 0 failed, 34 ignored. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-27 06:50 UTC · original

Tested on RX 7900 XT (RDNA3), driver 32.0.32029.2010, Windows 11 26300, Rust 1.98.1. Head d03efab vs base a-publier 4eb6cfd. Release and debug builds.

  • cargo test -p victory-gpu --lib, release, 3 runs each, base and head interleaved: base 396/0/34 and head 402/0/34 on every run.
  • Debug, --test-threads=1: base 396/0/34, head 402/0/34.
  • shadow:: filter, 10 runs each side: base 35/0 and head 41/0 on all 10 runs.
  • views_off_their_elements_get_a_copy_of_their_bytes passed 10/10 on both base and head.
  • The two device tests, each run alone 10 times: exact_recycles_quarantined_gpu_buffer_and_zeroes_stale_bytes 10/10, exact_concurrent_misses_return_the_same_shadow_and_pool_losers 10/10. shadow::exact_tests with 1 thread: 10/10.
  • The device was present in the test processes. These tests skip silently without one.
  • With VICTORY_EXACT_POOL_MB=0, the views test passed 3/3. The shadow::exact_tests set their own pool limit, so this variable reaches exact() only through that test.

Reading notes:

  • VICTORY_EXACT_POOL_MB=0 does not restore base behaviour for an A/B.
    • exact_bucket rounding still applies, so a view just over 64 KiB commits 128 KiB, and 33 MiB commits 64 MiB.
    • The 2-frame idle eviction rule still applies.
    • Evicted buffers still go through host::retire instead of being dropped.
  • Base, once the map held 256 entries, dropped on every miss the unheld entries not asked for in the last 512 askings. Head, once it holds 256, evicts unheld entries idle for 2 frames, at most once per max(256, half the map) misses. The map size now follows misses per frame (at least 64 KiB each), and this memory is not counted in the 256/320 MiB pool budget. Not measured.
  • The quarantine watermark comes from ExactClock::now() at the start of exact(), before the write lock and the reference checks. host::retire samples it when called. A submission begun in that window that reaches the buffer only through a descriptor is covered only by the 2-frame wait.
  • OWN_MADE (the "copies made" count in the frame line) now counts new allocations only, not buffers taken from the pool. The shared-buffer bytes in the memory line now count bucket sizes.

Not tested: in the title; memory use or map size under load; timing.

**@⁠adrianmjim** on GitHub · 2026-11-27 06:50 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7211893783) Tested on RX 7900 XT (RDNA3), driver 32.0.32029.2010, Windows 11 26300, Rust 1.98.1. Head d03efab vs base a-publier 4eb6cfd. Release and debug builds. - `cargo test -p victory-gpu --lib`, release, 3 runs each, base and head interleaved: base 396/0/34 and head 402/0/34 on every run. - Debug, `--test-threads=1`: base 396/0/34, head 402/0/34. - `shadow::` filter, 10 runs each side: base 35/0 and head 41/0 on all 10 runs. - `views_off_their_elements_get_a_copy_of_their_bytes` passed 10/10 on both base and head. - The two device tests, each run alone 10 times: `exact_recycles_quarantined_gpu_buffer_and_zeroes_stale_bytes` 10/10, `exact_concurrent_misses_return_the_same_shadow_and_pool_losers` 10/10. `shadow::exact_tests` with 1 thread: 10/10. - The device was present in the test processes. These tests skip silently without one. - With `VICTORY_EXACT_POOL_MB=0`, the views test passed 3/3. The `shadow::exact_tests` set their own pool limit, so this variable reaches `exact()` only through that test. Reading notes: - `VICTORY_EXACT_POOL_MB=0` does not restore base behaviour for an A/B. - `exact_bucket` rounding still applies, so a view just over 64 KiB commits 128 KiB, and 33 MiB commits 64 MiB. - The 2-frame idle eviction rule still applies. - Evicted buffers still go through `host::retire` instead of being dropped. - Base, once the map held 256 entries, dropped on every miss the unheld entries not asked for in the last 512 askings. Head, once it holds 256, evicts unheld entries idle for 2 frames, at most once per max(256, half the map) misses. The map size now follows misses per frame (at least 64 KiB each), and this memory is not counted in the 256/320 MiB pool budget. Not measured. - The quarantine watermark comes from `ExactClock::now()` at the start of `exact()`, before the write lock and the reference checks. `host::retire` samples it when called. A submission begun in that window that reaches the buffer only through a descriptor is covered only by the 2-frame wait. - `OWN_MADE` (the "copies made" count in the frame line) now counts new allocations only, not buffers taken from the pool. The shared-buffer bytes in the memory line now count bucket sizes. Not tested: in the title; memory use or map size under load; timing.
Author
Owner

@⁠pr0vId3nce on GitHub · 2026-11-27 07:23 UTC · original

A data point for this approach from NVIDIA. An RTX 4080 has 32-lane waves and no raw descriptor twins, so every off-element view goes through shadow::exact(). On avatarmanz main, pooling the own-bytes copies and making them outside the lock took Series X from 276 to 142 ms a frame, with the picture unchanged. The patch and numbers are here: https://github.com/avatarmanz/VIctory-7900XTX/issues/221#issuecomment-7200700579

Two pieces we added on top of plain pooling:

  • a byte cap on the pool (128 MiB / 4,096 buffers), so it can't grow without bound;
  • zeroing a reused buffer's tail past the new copy's length, so a view that reads past its own bytes sees zeros, not the previous owner's data.

🤖 Generated with Claude Code

**@⁠pr0vId3nce** on GitHub · 2026-11-27 07:23 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7212431065) A data point for this approach from NVIDIA. An RTX 4080 has 32-lane waves and no raw descriptor twins, so every off-element view goes through `shadow::exact()`. On avatarmanz main, pooling the own-bytes copies and making them outside the lock took Series X from 276 to 142 ms a frame, with the picture unchanged. The patch and numbers are here: https://github.com/avatarmanz/VIctory-7900XTX/issues/221#issuecomment-7200700579 Two pieces we added on top of plain pooling: - a byte cap on the pool (128 MiB / 4,096 buffers), so it can't grow without bound; - zeroing a reused buffer's tail past the new copy's length, so a view that reads past its own bytes sees zeros, not the previous owner's data. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠Robotniq on GitHub · 2026-11-27 10:14 UTC · original

Tested in the title on an 8 GB NVIDIA card: RTX 3070 Ti (8 GB, 32-lane waves, no raw twins, driver 32.0.16.2140), i7-13700F, 32 GB RAM, Windows 11 26200. Diagnostics builds on a-publier 4eb6cfd, --console series-s, the scripts\play.ps1 settings (not -Safe), VICTORY_PORT_CONSTANTS/DESCRIPTORS/CBV/GUARD/JOURNAL=1, VICTORY_D3D12_DRED=faults, VICTORY_UPSCALER=6, ray tracing off. Same save, four to seven minutes in the prologue's first area per run, warm pipeline library.

What was compared: #443 alone against this PR with #443's loose and xmem hunks on top (its exact hunks left out, since the two rewrite the same function). So the difference between the two columns is this PR's exact against #443's.

Frame rate by draws a frame (median of the frame lines in each band):

draws a frame #443 this PR (+ #443's loose/xmem)
2,000-3,500 10.7 fps 14.8 fps
3,500-5,000 9.8 fps 13.3 fps
5,000-7,000 8.8 fps (6 lines) 9.7 fps (5 lines)
all world lines after the first minute 9.5 fps 13.7 fps

List calls (VICTORY_THIN, median over the frame lines at 20,000 calls or more): 14.2 µs a call with #443, 4.9 µs with this PR.

The frame line with this PR: about 1,770 asked for a frame, 0.0 new + 261 recycled copies a frame, 4,100-4,750 kept, up to 1,560 quarantined, 135-175 MiB pooled. With #443 it read about 500 copies made a frame and 5,200 kept. The card went from about 70% busy to about 91% by the card's clock line, so it is the limit again here.

No device loss and nothing in crashes.log in three runs with the PR (about 15 minutes in the world). Private memory about 24 GB in both.

On the picture: the person playing thought there were slightly more black patches with the PR, then fewer in a later, slower run of the same build. I could not settle it: VICTORY_CAPTURE_WORLD runs of the two builds do not line up (the save opens in a cutscene and the slower build is at an earlier shot at the same frame count), and the share of near-black pixels over about 100 captured world frames each was no higher with the PR. So no regression shown, and none ruled out.

AI-assisted (Claude Code): the runs, the reading of the logs and this text.

**@⁠Robotniq** on GitHub · 2026-11-27 10:14 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7215644971) Tested in the title on an 8 GB NVIDIA card: RTX 3070 Ti (8 GB, 32-lane waves, no raw twins, driver 32.0.16.2140), i7-13700F, 32 GB RAM, Windows 11 26200. Diagnostics builds on `a-publier` `4eb6cfd`, `--console series-s`, the `scripts\play.ps1` settings (not `-Safe`), `VICTORY_PORT_CONSTANTS/DESCRIPTORS/CBV/GUARD/JOURNAL=1`, `VICTORY_D3D12_DRED=faults`, `VICTORY_UPSCALER=6`, ray tracing off. Same save, four to seven minutes in the prologue's first area per run, warm pipeline library. What was compared: #443 alone against this PR with #443's `loose` and `xmem` hunks on top (its `exact` hunks left out, since the two rewrite the same function). So the difference between the two columns is this PR's `exact` against #443's. Frame rate by draws a frame (median of the frame lines in each band): | draws a frame | #443 | this PR (+ #443's `loose`/`xmem`) | |---|---:|---:| | 2,000-3,500 | 10.7 fps | 14.8 fps | | 3,500-5,000 | 9.8 fps | 13.3 fps | | 5,000-7,000 | 8.8 fps (6 lines) | 9.7 fps (5 lines) | | all world lines after the first minute | 9.5 fps | 13.7 fps | List calls (`VICTORY_THIN`, median over the frame lines at 20,000 calls or more): 14.2 µs a call with #443, 4.9 µs with this PR. The frame line with this PR: about 1,770 asked for a frame, 0.0 new + 261 recycled copies a frame, 4,100-4,750 kept, up to 1,560 quarantined, 135-175 MiB pooled. With #443 it read about 500 copies made a frame and 5,200 kept. The card went from about 70% busy to about 91% by the card's clock line, so it is the limit again here. No device loss and nothing in `crashes.log` in three runs with the PR (about 15 minutes in the world). Private memory about 24 GB in both. On the picture: the person playing thought there were slightly more black patches with the PR, then fewer in a later, slower run of the same build. I could not settle it: `VICTORY_CAPTURE_WORLD` runs of the two builds do not line up (the save opens in a cutscene and the slower build is at an earlier shot at the same frame count), and the share of near-black pixels over about 100 captured world frames each was no higher with the PR. So no regression shown, and none ruled out. AI-assisted (Claude Code): the runs, the reading of the logs and this text.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-451'
git switch 'github-bot/gh-451'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-451'
git switch 'github-bot/gh-451'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-451'
git switch 'a-publier'
git merge --squash 'github-bot/gh-451'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-451'
git switch 'a-publier'
git merge 'github-bot/gh-451'
git push origin 'a-publier'
This discussion has been locked. Commenting is limited to contributors.
No description provided.