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

Open
BE29ZZ wants to merge 1 commit from upstream-pr/451 into a-publier
BE29ZZ commented 2026-11-27 02:27:38 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by BE29ZZ on 2026-11-27T02:27:38Z.


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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:5520131660 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451). Originally posted by [BE29ZZ](https://github.com/BE29ZZ) on 2026-11-27T02:27:38Z. --- 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.
MamaLeVrai commented 2026-11-27 03:55:46 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by MamaLeVrai on 2026-11-27T03:55:46Z.


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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7209382371 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7209382371). Originally posted by [MamaLeVrai](https://github.com/MamaLeVrai) on 2026-11-27T03:55:46Z. --- 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.
adrianmjim commented 2026-11-27 06:50:20 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by adrianmjim on 2026-11-27T06:50:20Z.


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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7211893783 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7211893783). Originally posted by [adrianmjim](https://github.com/adrianmjim) on 2026-11-27T06:50:20Z. --- 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.
pr0vId3nce commented 2026-11-27 07:23:51 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by pr0vId3nce on 2026-11-27T07:23:51Z.


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

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7212431065 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7212431065). Originally posted by [pr0vId3nce](https://github.com/pr0vId3nce) on 2026-11-27T07:23:51Z. --- 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)
Robotniq commented 2026-11-27 10:14:29 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by Robotniq on 2026-11-27T10:14:29Z.


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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7215644971 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/451#issuecomment-7215644971). Originally posted by [Robotniq](https://github.com/Robotniq) on 2026-11-27T10:14:29Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/451:upstream-pr/451'
git switch 'upstream-pr/451'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'a-publier'
git merge --no-ff 'upstream-pr/451'
git switch 'upstream-pr/451'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'upstream-pr/451'
git switch 'upstream-pr/451'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'upstream-pr/451'
git switch 'a-publier'
git merge --squash 'upstream-pr/451'
git switch 'a-publier'
git merge --ff-only 'upstream-pr/451'
git switch 'a-publier'
git merge 'upstream-pr/451'
git push origin 'a-publier'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Belobug/VIctory-Belobug-AMD!451
No description provided.