fix(gpu): keep slot_evict inside ring history and recycle card buffer heaps #362

Merged
Androtim merged 1 commit from upstream-pr/362 into main 2026-12-02 17:33:10 +00:00
Androtim commented 2026-12-02 16:48:11 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T16:48:11Z.


Fixes the #282 P0 #1 (slot_evict idle-cutoff ring mismatch) and P0 #2 (CardBumpPool pinned heap growth) VRAM leaks (#276).

Changes

  1. Ring-bounded slot_evict (crates/victory-gpu/src/resource.rs)
    • Added slot_evict_idle_ms(budget, used, high) clamped to 5_000..=60_000 ms inside RING's 120 s history instead of passing vram_keep_below's KEPT_MS = 600_000, which caused ring::frame_at to return None and skip slot eviction until 10 minutes elapsed.
    • Tuned default hysteresis to (92%, 88%, 256 MiB) (VICTORY_SLOT_EVICT_PERCENT / LOW_PERCENT / MB, with VICTORY_SLOT_EVICT=0 kill-switch) and lifted the tight-pressure threshold / uncapped sweep trigger to 96% so steady-state VRAM stays stable without mid-scene eviction thrashing.
  2. Recycling CardPool (crates/victory-gpu/src/ahead.rs)
    • Replaced the monotonic CardBumpPool bump allocator with bitset-backed CardBlock (CardPool) suballocation gated on both fence completion (card_fence >= last_fence) and full rest (alloc_mask == 0), plus a CardReclaim COM drop hook matching SharedBlock.
    • Shared slot_range_mask and find_free_slots bitset helpers across SharedBlock and CardBlock.
    • Added post-CreateHeap budget re-check in alloc_pooled_card (Drop frees excess heap), extended trim_buffer_pools to release idle empty card heaps, and added live_card_heaps= telemetry to ahead pool logging.

Verification Gates

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy -> asset policy: 325 files ok
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu slot_evict --locked -> 3 passed; 0 failed
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu ahead::tests --locked -- --test-threads=1 -> 12 passed; 0 failed
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings -> pass
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings -> pass
  • objdump -p on plain --release build -> standard Windows system DLLs only

Paired Plain --release A/B Benchmarks (vs main @ 3efe907, Series X parity, DLSS Quality)

  • 4K 90s matched-scene (ab-main-3efe907-4k-90s vs ab2-pr-vram-4k-90s):
    • Peak VRAM: 6525 MB -> 6215 MB (-310 MB); room_false: 100.0% -> 87.5%; pool_not_made: 889 -> 651
    • Host texture copies: 26506 (636 MB card) -> 21661 (530 MB card)
    • card_heaps: 116 -> 62 (live_card_heaps=48) while serving 7426 vs 6450 card buffers
    • FPS: 6.3 fps -> 6.35 fps (max_draws 12832 -> 13462); preparing: 23.4 ms -> 21.35 ms; copyback: 15.0 ms -> 13.65 ms; mapped() longest: 20.5 ms -> 15.0 ms
  • 4K 120s driving stress (ab-main-3efe907-4k-drive120 vs ab2-pr-vram-4k-drive120):
    • Peak VRAM: 6891 MB (over=118 MB) -> 6338 MB (over=0 MB, -553 MB)
    • card_heaps: 167 -> 58 (live_card_heaps=45) while serving 10296 vs 8194 card buffers
    • mapped() longest: 9.6 ms
  • 1440p 90s matched-scene (ab2-main-3efe907-1440p-90s vs ab2-pr-vram-1440p-90s):
    • Peak VRAM: 6124 MB -> 6090 MB; final_waiting: 4190 -> 3287; card_heaps: 94 -> 74 (live_card_heaps=71)
    • FPS: 13.75 fps -> 13.5 fps (min FPS 1.8 -> 5.2 fps); mapped() longest: 46.3 ms -> 10.0 ms

--features diagnostics Visual Verification (diag-pr-vram-1440p-90s, 90s 1440p)

  • 6/6 gates PASS: world_windows=20 (max_draws=12750), crashes=0, used_max=5971 MB (over=0 MB, room_false=0.0%), final_waiting=2123, mapped_rate=1.1 ms/s (longest 9.2 ms), pngs=24 (lit=22/24, luma=100.09, near_black=0.06%, edge_energy=7.76, magenta=0.0%).
  • Visually inspected frame-2086.png, frame-2094.png, and frame-2100.png: sharp architectural/foliage textures, natural lighting, clean water reflections, and crisp HUD overlays.
<!-- greyfinch-sync:Androtim/MiniVI:issue:5610925397 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/362). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T16:48:11Z. --- Fixes the `#282` P0 #1 (`slot_evict` idle-cutoff ring mismatch) and P0 #2 (`CardBumpPool` pinned heap growth) VRAM leaks (`#276`). ### Changes 1. **Ring-bounded `slot_evict` (`crates/victory-gpu/src/resource.rs`)** - Added `slot_evict_idle_ms(budget, used, high)` clamped to `5_000..=60_000 ms` inside `RING`'s 120 s history instead of passing `vram_keep_below`'s `KEPT_MS = 600_000`, which caused `ring::frame_at` to return `None` and skip slot eviction until 10 minutes elapsed. - Tuned default hysteresis to `(92%, 88%, 256 MiB)` (`VICTORY_SLOT_EVICT_PERCENT` / `LOW_PERCENT` / `MB`, with `VICTORY_SLOT_EVICT=0` kill-switch) and lifted the tight-pressure threshold / uncapped sweep trigger to `96%` so steady-state VRAM stays stable without mid-scene eviction thrashing. 2. **Recycling `CardPool` (`crates/victory-gpu/src/ahead.rs`)** - Replaced the monotonic `CardBumpPool` bump allocator with bitset-backed `CardBlock` (`CardPool`) suballocation gated on both fence completion (`card_fence >= last_fence`) and full rest (`alloc_mask == 0`), plus a `CardReclaim` COM drop hook matching `SharedBlock`. - Shared `slot_range_mask` and `find_free_slots` bitset helpers across `SharedBlock` and `CardBlock`. - Added post-`CreateHeap` budget re-check in `alloc_pooled_card` (`Drop` frees excess heap), extended `trim_buffer_pools` to release idle empty card heaps, and added `live_card_heaps=` telemetry to `ahead` pool logging. ### Verification Gates - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy` -> `asset policy: 325 files ok` - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu slot_evict --locked` -> `3 passed; 0 failed` - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu ahead::tests --locked -- --test-threads=1` -> `12 passed; 0 failed` - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings` -> pass - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings` -> pass - `objdump -p` on plain `--release` build -> standard Windows system DLLs only ### Paired Plain `--release` A/B Benchmarks (vs `main` @ `3efe907`, `Series X` parity, DLSS Quality) - **4K 90s matched-scene (`ab-main-3efe907-4k-90s` vs `ab2-pr-vram-4k-90s`):** - Peak VRAM: `6525 MB` -> **`6215 MB` (`-310 MB`)**; `room_false`: `100.0%` -> **`87.5%`**; `pool_not_made`: `889` -> `651` - Host texture copies: `26506` (`636 MB` card) -> **`21661` (`530 MB` card)** - `card_heaps`: `116` -> **`62` (`live_card_heaps=48`)** while serving `7426` vs `6450` card buffers - FPS: `6.3 fps` -> **`6.35 fps`** (`max_draws` `12832` -> `13462`); `preparing`: `23.4 ms` -> **`21.35 ms`**; `copyback`: `15.0 ms` -> **`13.65 ms`**; `mapped()` longest: `20.5 ms` -> **`15.0 ms`** - **4K 120s driving stress (`ab-main-3efe907-4k-drive120` vs `ab2-pr-vram-4k-drive120`):** - Peak VRAM: `6891 MB` (`over=118 MB`) -> **`6338 MB` (`over=0 MB`, `-553 MB`)** - `card_heaps`: `167` -> **`58` (`live_card_heaps=45`)** while serving `10296` vs `8194` card buffers - `mapped()` longest: `9.6 ms` - **1440p 90s matched-scene (`ab2-main-3efe907-1440p-90s` vs `ab2-pr-vram-1440p-90s`):** - Peak VRAM: `6124 MB` -> **`6090 MB`**; `final_waiting`: `4190` -> **`3287`**; `card_heaps`: `94` -> **`74` (`live_card_heaps=71`)** - FPS: `13.75 fps` -> `13.5 fps` (min FPS **`1.8` -> `5.2 fps`**); `mapped()` longest: `46.3 ms` -> **`10.0 ms`** ### `--features diagnostics` Visual Verification (`diag-pr-vram-1440p-90s`, 90s 1440p) - **6/6 gates PASS:** `world_windows=20` (`max_draws=12750`), `crashes=0`, `used_max=5971 MB` (`over=0 MB`, `room_false=0.0%`), `final_waiting=2123`, `mapped_rate=1.1 ms/s` (longest `9.2 ms`), `pngs=24` (`lit=22/24`, `luma=100.09`, `near_black=0.06%`, `edge_energy=7.76`, `magenta=0.0%`). - Visually inspected `frame-2086.png`, `frame-2094.png`, and `frame-2100.png`: sharp architectural/foliage textures, natural lighting, clean water reflections, and crisp HUD overlays.
Androtim commented 2026-12-02 17:04:54 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T17:04:54Z.


Automated triage for the maintainers (not an approval).

Verdict: needs-gpu-test · CI (check): none

Summary

This PR addresses two VRAM growth issues in victory-gpu:

  1. Ring-bounded slot_evict (crates/victory-gpu/src/resource.rs): Introduces slot_evict_idle_ms(budget, used, high) (clamped to 5_000..=60_000 ms, linearly scaling down between min(high, 95)% and 96%) so frame_at(&ring, now_ms.saturating_sub(evict_idle_ms)) stays within RING's 120-second history instead of returning None when callers pass KEPT_MS = 600_000. Also updates default parse_slot_evict_settings hysteresis from (85%, 75%, 256 MiB) to (92%, 88%, 256 MiB) and raises the uncapped sweep trigger in slot_evict from 92% to 96%.
  2. Recycling CardPool (crates/victory-gpu/src/ahead.rs): Extracts shared slot_range_mask / find_free_slots bitset helpers and replaces the monotonic CardBumpPool with CardBlock / CardPool + a CardReclaim COM drop hook (SetPrivateDataInterface(&CARD_HEAP_KEY, ...)). Freed slot ranges are gated on both GPU submission completion (finished_sub >= freed_sub) and REST_MS before reuse, concurrent CreateHeap calls re-check pool.take under lock (dropping the extra heap outside the lock), trim_buffer_pools releases idle empty card heaps, and live_card_heaps is logged in note_pool_progress.

Review Findings

  • Correctness & Safety:
    • slot_evict_idle_ms uses u128 for percentage math, handles budget == 0, and clamps start to 95 * 100 (< end = 96 * 100), avoiding overflow or divide-by-zero even when VICTORY_SLOT_EVICT_PERCENT=97.
    • CardReclaim COM refcounting and attach_card_reclaim properly avoid double-retire or leaks on SetPrivateDataInterface failure (forget(object) + drop(Box::from_raw(made)), followed by CARD_POOL.lock().cancel(...)).
    • No ID3D12Resource or ID3D12Heap is dropped while holding CARD_POOL (including in alloc_pooled_card and trim_buffer_pools), avoiding recursive card_reclaim_release deadlocks.
  • Note — Recycled card buffer zero-initialization: Unlike SharedBlock (which tracks ever_used and zeroes recycled ranges via write_bytes), CardBlock recycles GPU heap ranges without clearing them. While alloc_pooled_card's doc comment was updated to drop "zero-initialized", the module doc comment at the top of crates/victory-gpu/src/ahead.rs still states buffers from ahead match Host::uav_buffer as "new, zeroed". Please confirm during local GPU testing (and across all alloc_pooled_card / uav_buffer callers) that no caller reads unwritten bytes from a recycled card buffer without initializing them first.
  • Overlap with open PR #355: Open PR #355 (fix(gpu/ahead): replace bump-only CardBumpPool with fence-gated recycling) modifies the same CardBumpPool region in crates/victory-gpu/src/ahead.rs. Maintainers should coordinate which PR lands and close/supersede the other.
  • CI: No CI status is reported yet (none); ensure workspace tests and clippy pass on CI alongside local GPU validation.

Verdict

needs-gpu-test — Logic and safety look solid with unit tests covering both changes, but this PR modifies D3D12 placed-resource heap recycling and VRAM eviction thresholds and warrants a local real-GPU smoke test (including D3D12 debug layer validation for placed-resource lifetime/aliasing) before merging.


Generated with Antigravity CLI (agy).

<!-- greyfinch-sync:Androtim/MiniVI:comment:7352872951 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/362#issuecomment-7352872951). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T17:04:54Z. --- <!-- minivi-triage v1 head=9d487e3ad5323c7d571464910b77be289a598a15 verdict=needs-gpu-test ci=none at=2026-12-02T17:05:26Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-gpu-test` · **CI (`check`):** `none` ### Summary This PR addresses two VRAM growth issues in `victory-gpu`: 1. **Ring-bounded `slot_evict` (`crates/victory-gpu/src/resource.rs`)**: Introduces `slot_evict_idle_ms(budget, used, high)` (clamped to `5_000..=60_000 ms`, linearly scaling down between `min(high, 95)%` and `96%`) so `frame_at(&ring, now_ms.saturating_sub(evict_idle_ms))` stays within `RING`'s 120-second history instead of returning `None` when callers pass `KEPT_MS = 600_000`. Also updates default `parse_slot_evict_settings` hysteresis from `(85%, 75%, 256 MiB)` to `(92%, 88%, 256 MiB)` and raises the uncapped sweep trigger in `slot_evict` from `92%` to `96%`. 2. **Recycling `CardPool` (`crates/victory-gpu/src/ahead.rs`)**: Extracts shared `slot_range_mask` / `find_free_slots` bitset helpers and replaces the monotonic `CardBumpPool` with `CardBlock` / `CardPool` + a `CardReclaim` COM drop hook (`SetPrivateDataInterface(&CARD_HEAP_KEY, ...)`). Freed slot ranges are gated on both GPU submission completion (`finished_sub >= freed_sub`) and `REST_MS` before reuse, concurrent `CreateHeap` calls re-check `pool.take` under lock (dropping the extra heap outside the lock), `trim_buffer_pools` releases idle empty card heaps, and `live_card_heaps` is logged in `note_pool_progress`. ### Review Findings - **Correctness & Safety**: - `slot_evict_idle_ms` uses `u128` for percentage math, handles `budget == 0`, and clamps `start` to `95 * 100` (`< end = 96 * 100`), avoiding overflow or divide-by-zero even when `VICTORY_SLOT_EVICT_PERCENT=97`. - `CardReclaim` COM refcounting and `attach_card_reclaim` properly avoid double-retire or leaks on `SetPrivateDataInterface` failure (`forget(object)` + `drop(Box::from_raw(made))`, followed by `CARD_POOL.lock().cancel(...)`). - No `ID3D12Resource` or `ID3D12Heap` is dropped while holding `CARD_POOL` (including in `alloc_pooled_card` and `trim_buffer_pools`), avoiding recursive `card_reclaim_release` deadlocks. - **Note — Recycled card buffer zero-initialization**: Unlike `SharedBlock` (which tracks `ever_used` and zeroes recycled ranges via `write_bytes`), `CardBlock` recycles GPU heap ranges without clearing them. While `alloc_pooled_card`'s doc comment was updated to drop `"zero-initialized"`, the module doc comment at the top of `crates/victory-gpu/src/ahead.rs` still states buffers from `ahead` match `Host::uav_buffer` as *"new, zeroed"*. Please confirm during local GPU testing (and across all `alloc_pooled_card` / `uav_buffer` callers) that no caller reads unwritten bytes from a recycled card buffer without initializing them first. - **Overlap with open PR #355**: Open PR #355 (`fix(gpu/ahead): replace bump-only CardBumpPool with fence-gated recycling`) modifies the same `CardBumpPool` region in `crates/victory-gpu/src/ahead.rs`. Maintainers should coordinate which PR lands and close/supersede the other. - **CI**: No CI status is reported yet (`none`); ensure workspace tests and clippy pass on CI alongside local GPU validation. ### Verdict `needs-gpu-test` — Logic and safety look solid with unit tests covering both changes, but this PR modifies D3D12 placed-resource heap recycling and VRAM eviction thresholds and warrants a local real-GPU smoke test (including D3D12 debug layer validation for placed-resource lifetime/aliasing) before merging. --- _Generated with Antigravity CLI (`agy`)._
Androtim commented 2026-12-02 17:06:09 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T17:06:09Z.


Architect Review (plain---release A/B + --features diagnostics visual PNGs verified):

  • Huge VRAM win verified: 4K 120s driving stress peak VRAM 6,891 MB (118 MB over) -> 6,338 MB (0 MB over, -553 MB), card_heaps 167 -> 58 (live_card_heaps=45); 4K 90s -310 MB VRAM, preparing 23.4 -> 21.35 ms, copyback 15.0 -> 13.65 ms; 1440p final_waiting 4,190 -> 3,287, min FPS 1.8 -> 5.2, mapped() longest 46.3 -> 10.0 ms, 0 crashes.
  • Visual PNGs directly inspected (diag-pr-vram-1440p-90s frame-2086.png, frame-2094.png): clean textures, foliage, reflections, lighting, and HUD (6/6 gates PASS).
  • Much cleaner than #355 (no CardZeroer / host::settle() stall, includes both finished_sub >= freed_sub and 500 ms REST_MS rest window).
  • One minor safety note & blocker: checks=[] (no GitHub Actions CI run recorded on 9d487e3a yet — please push/rebase onto main to trigger CI). While touching the branch, consider adding + crate::queue::queued() to started_sub in card_reclaim_release (reading queued() before submissions().0, matching #335 / #282 P1 #21) so thin-mode queued submissions are always covered even if a queue stalls > 500 ms. Once CI is green, this is ready to merge immediately (and supersedes #355)!
<!-- greyfinch-sync:Androtim/MiniVI:comment:7352899805 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/362#issuecomment-7352899805). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T17:06:09Z. --- Architect Review (plain-`--release` A/B + `--features diagnostics` visual PNGs verified): - **Huge VRAM win verified:** 4K 120s driving stress peak VRAM `6,891 MB` (`118 MB` over) -> **`6,338 MB` (`0 MB` over, `-553 MB`)**, `card_heaps 167 -> 58` (`live_card_heaps=45`); 4K 90s `-310 MB` VRAM, `preparing 23.4 -> 21.35 ms`, `copyback 15.0 -> 13.65 ms`; 1440p `final_waiting 4,190 -> 3,287`, `min FPS 1.8 -> 5.2`, `mapped()` longest `46.3 -> 10.0 ms`, `0` crashes. - **Visual PNGs directly inspected (`diag-pr-vram-1440p-90s` `frame-2086.png`, `frame-2094.png`):** clean textures, foliage, reflections, lighting, and HUD (6/6 gates PASS). - Much cleaner than `#355` (no `CardZeroer` / `host::settle()` stall, includes both `finished_sub >= freed_sub` and 500 ms `REST_MS` rest window). - **One minor safety note & blocker:** `checks=[]` (no GitHub Actions CI run recorded on `9d487e3a` yet — please push/rebase onto `main` to trigger CI). While touching the branch, consider adding `+ crate::queue::queued()` to `started_sub` in `card_reclaim_release` (reading `queued()` before `submissions().0`, matching `#335` / `#282` P1 #21) so thin-mode queued submissions are always covered even if a queue stalls > 500 ms. Once CI is green, this is ready to merge immediately (and supersedes `#355`)!
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
Androtim/MiniVI!362
No description provided.