perf(gpu): pool and recycle shadow::exact() buffers with GPU quarantine #181

Open
github-bot wants to merge 1 commit from github-bot/gh-181 into main AGit
Owner

@⁠BE29ZZ on GitHub · 2026-11-27 19:22 UTC · original
BE29ZZ:perf/exact-buffer-pool → main · +673 −50 · 2 files · 1 commits · open

### Problem
On NVIDIA RTX cards (32-lane waves and no raw descriptor twins due to the 1M shader-visible view heap cap), every off-element structured/raw buffer view falls through to \shadow::exact()\ in \crates/victory-gpu/src/shadow.rs. Currently \shadow::exact():

  1. Serializes all parallel \ ictory-list-*\ threads behind a single global \Mutex\ on every lookup (hits and misses).
  2. Runs an \O(N)\ \map.retain(...)\ sweep across all cached entries on every miss once \map.len() >= 256.
  3. Calls \host()?.shared_buffer(...)\ (\CreateCommittedResource) while holding the mutex, allocating and destroying hundreds of committed D3D12 buffers per frame (~500–716/frame in world gameplay).

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 lock contention across parallel 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 recycling pool (\ExactPool):
    • Pooled 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 views share a single bucket with no extra physical RAM cost.
    • GPU submission retirement watermarks (\host::submissions().0 + queue::queued()) are sampled after evicting entries from \map\ and releasing its write lock (matching \host::retire), so submissions started before eviction cannot race past quarantine.
    • Evicted buffers stay in quarantine until both 2 additional frames (4 frames total past last use) and every GPU submission started or queued at eviction 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 refresh their diagnostic D3D12 object name.
    • Race-loser allocations (never exposed to any GPU command) return directly to the ready pool.
    • Overflow evictions go through \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=. Setting \VICTORY_EXACT_POOL_MB=0\ disables both pooling and 64 KiB bucket rounding (allocating \len.next_multiple_of(256)\ as before) for clean A/B comparisons.
    • \shadow::let_go_idle()\ graduates retired entries and drains ready pooled buffers under host memory pressure (
      esidency::pressured()).
    • \exact_line(frames)\ reports asked, new, recycled, kept, quarantined, and pooled MiB.
  • Test update (\crates/victory-gpu/src/descriptor/host.rs):
    • Updated \ iews_off_their_elements_get_a_copy_of_their_bytes\ to check \shadow::exact_alloc_size(len)\ so it passes both with default pooling and with \VICTORY_EXACT_POOL_MB=0.

Prior community in-game & multi-GPU validation (from the identical Belobug implementation before repo takedown)

  • NVIDIA RTX 3070 Ti 8 GB (in-game, Series S, warm cache, 3 runs / ~15 min in world, tested by @⁠Robotniq):
    • World FPS (median after first minute): 9.5 -> 13.7 FPS (+44%) (and 10.7 -> 14.8 FPS in 2,000–3,500 draw scenes), even when compared against an out-of-lock-only \exact()\ without pooling.
    • \VICTORY_THIN\ list call cost: 14.2 us -> 4.9 us/call.
    • Frame-line exact buffer turnover: ~500 new/frame -> 0.0 new + 261 recycled/frame (135–175 MiB pooled); GPU busy ~70% -> ~91%; 0 crashes / 0 device loss.
  • AMD RX 6950 XT (RDNA 2, @⁠MamaLeVrai) & RX 7900 XT (RDNA 3, @⁠adrianmjim): full \ ictory-gpu\ test suite passed (10/10 on both new GPU tests); this PR also incorporates @⁠adrianmjim's review suggestions (post-eviction watermark sampling and \VICTORY_EXACT_POOL_MB=0\ keeping 256-byte allocation size).
  • Credit to @⁠ToolxMaster and @⁠Miquella135 for the original RTX 3080 / 3080 Ti quarantined-pool design and measurements.

Local verification on \main\ (\cef07e) — Windows 11 x64, NVIDIA GeForce RTX 3060, Rust 1.98.1

  • Clippy (default and --all-features): 0 warnings/errors in modified code (pre-existing \main\ Clippy issues in untouched files tracked in #86).
  • \cargo test -p victory-gpu --lib --locked -- exact_tests: 7 passed, 0 failed (unit + real RTX 3060 D3D12 buffer recycle, post-eviction watermark, stale-byte zeroing, \pool_limit=0\ 256-byte width, and 8-thread concurrency tests).
  • \cargo test -p victory-gpu --lib --all-features --locked -- exact_tests: 7 passed, 0 failed.
  • \ iews_off_their_elements_get_a_copy_of_their_bytes: pass both with default pooling and with \VICTORY_EXACT_POOL_MB=0.
  • \cargo test -p victory-gpu --lib --locked -- --test-threads=1: 425 passed, 0 new failures (the 2 pre-existing \indirect::tests\ failures on \main\ pass with \VICTORY_INDIRECT_REST=1, #67 -> 427/427).
  • \cargo test -p xtask --locked: 6 passed, 0 failed (
    epository_passes_policy\ clean).

Not tested in the title itself on this machine (no game files here).

AI-assisted; no game data.

**@⁠BE29ZZ** on GitHub · 2026-11-27 19:22 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/181) `BE29ZZ:perf/exact-buffer-pool` → `main` · +673 −50 · 2 files · 1 commits · open ### Problem On NVIDIA RTX cards (32-lane waves and no raw descriptor twins due to the 1M shader-visible view heap cap), every off-element structured/raw buffer view falls through to \shadow::exact()\ in \crates/victory-gpu/src/shadow.rs\. Currently \shadow::exact()\: 1. Serializes all parallel \ ictory-list-*\ threads behind a single global \Mutex\ on every lookup (hits and misses). 2. Runs an \O(N)\ \map.retain(...)\ sweep across all cached entries on every miss once \map.len() >= 256\. 3. Calls \host()?.shared_buffer(...)\ (\CreateCommittedResource\) while holding the mutex, allocating and destroying hundreds of committed D3D12 buffers per frame (~500–716/frame in world gameplay). ### 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 lock contention across parallel 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 recycling pool (\ExactPool\):** - Pooled 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 views share a single bucket with no extra physical RAM cost. - GPU submission retirement watermarks (\host::submissions().0 + queue::queued()\) are sampled **after** evicting entries from \map\ and releasing its write lock (matching \host::retire\), so submissions started before eviction cannot race past quarantine. - Evicted buffers stay in quarantine until **both** 2 additional frames (4 frames total past last use) **and** every GPU submission started or queued at eviction 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 refresh their diagnostic D3D12 object name. - Race-loser allocations (never exposed to any GPU command) return directly to the ready pool. - Overflow evictions go through \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>\. Setting \VICTORY_EXACT_POOL_MB=0\ disables both pooling and 64 KiB bucket rounding (allocating \len.next_multiple_of(256)\ as before) for clean A/B comparisons. - \shadow::let_go_idle()\ graduates retired entries and drains ready pooled buffers under host memory pressure (\ esidency::pressured()\). - \exact_line(frames)\ reports asked, new, recycled, kept, quarantined, and pooled MiB. - **Test update (\crates/victory-gpu/src/descriptor/host.rs\):** - Updated \ iews_off_their_elements_get_a_copy_of_their_bytes\ to check \shadow::exact_alloc_size(len)\ so it passes both with default pooling and with \VICTORY_EXACT_POOL_MB=0\. ### Prior community in-game & multi-GPU validation (from the identical Belobug implementation before repo takedown) - **NVIDIA RTX 3070 Ti 8 GB (in-game, Series S, warm cache, 3 runs / ~15 min in world, tested by @⁠Robotniq):** - World FPS (median after first minute): **9.5 -> 13.7 FPS (+44%)** (and **10.7 -> 14.8 FPS** in 2,000–3,500 draw scenes), even when compared against an out-of-lock-only \exact()\ without pooling. - \VICTORY_THIN\ list call cost: **14.2 us -> 4.9 us/call**. - Frame-line exact buffer turnover: **~500 new/frame -> 0.0 new + 261 recycled/frame** (135–175 MiB pooled); GPU busy **~70% -> ~91%**; **0 crashes / 0 device loss**. - **AMD RX 6950 XT (RDNA 2, @⁠MamaLeVrai) & RX 7900 XT (RDNA 3, @⁠adrianmjim):** full \ ictory-gpu\ test suite passed (10/10 on both new GPU tests); this PR also incorporates @⁠adrianmjim's review suggestions (post-eviction watermark sampling and \VICTORY_EXACT_POOL_MB=0\ keeping 256-byte allocation size). - Credit to @⁠ToolxMaster and @⁠Miquella135 for the original RTX 3080 / 3080 Ti quarantined-pool design and measurements. ### Local verification on \main\ (\cef07e\) — Windows 11 x64, NVIDIA GeForce RTX 3060, Rust 1.98.1 - Clippy (default and \--all-features\): **0 warnings/errors** in modified code (pre-existing \main\ Clippy issues in untouched files tracked in #86). - \cargo test -p victory-gpu --lib --locked -- exact_tests\: **7 passed, 0 failed** (unit + real RTX 3060 D3D12 buffer recycle, post-eviction watermark, stale-byte zeroing, \pool_limit=0\ 256-byte width, and 8-thread concurrency tests). - \cargo test -p victory-gpu --lib --all-features --locked -- exact_tests\: **7 passed, 0 failed**. - \ iews_off_their_elements_get_a_copy_of_their_bytes\: **pass** both with default pooling and with \VICTORY_EXACT_POOL_MB=0\. - \cargo test -p victory-gpu --lib --locked -- --test-threads=1\: **425 passed, 0 new failures** (the 2 pre-existing \indirect::tests\ failures on \main\ pass with \VICTORY_INDIRECT_REST=1\, #67 -> **427/427**). - \cargo test -p xtask --locked\: **6 passed, 0 failed** (\ epository_passes_policy\ clean). Not tested in the title itself on this machine (no game files here). 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), sampling the GPU submission watermark (submissions + queued) AFTER evicting entries from the map so submissions begun before eviction are always covered.
- 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 2 extra frames (4 frames total past last use) and completion of all GPU submissions started or queued at eviction before re-entering the ready pool.
- Pooled buffers round to power-of-two buckets (64 KiB min, 64 MiB max), zero all previously touched bytes before reuse, and drain ready buffers 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=0 disables both pooling and 64 KiB bucket rounding (keeps 256-byte rounding).

Design/measurements credited to @ToolxMaster and @Miquella135; review improvements from @adrianmjim; in-game RTX 3070 Ti validation by @Robotniq.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 19:24:24 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/181/head:github-bot/gh-181'
git switch 'github-bot/gh-181'

Merge

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