victory-gpu: size-tiered card-buffer headroom and fence-safe LRU demotion (#1163) #1277

Open
Vladimir-Human wants to merge 1 commit from upstream-pr/1280 into main
Vladimir-Human commented 2026-11-26 10:04:12 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-26T10:04:12Z.


Problem

Closes / addresses #1163 and 8–12 GB VRAM starvation:

  • When VICTORY_CARD_BUFFERS_MIN_KB=64 is combined with VICTORY_CARD_BUFFERS_MB=1024 (or smaller caps such as 512 MiB), thousands of small < 1 MiB buffers fill the flat card-buffer cap first and starve hot >= 1 MiB buffers back to PC RAM (100–122.6 MB/frame CPU compare and severe GPU slowdown).
  • On 8–12 GB GPUs, once cold small buffers occupy VRAM, they remain pinned on the card until unregistered, starving streaming allocations under DXGI video-memory pressure.

Changes

  1. Size-tiered headroom reservation (crates/victory-gpu/src/shadow.rs):
    • When VICTORY_CARD_BUFFERS_MIN_KB < 1024, reserves min(256 MiB, cap / 3) (env-tunable via VICTORY_CARD_LARGE_RESERVE_MB; 0 disables) inside VICTORY_CARD_BUFFERS_MB for >= 1 MiB buffers so small-buffer floods cap at cap - reserve (CardAdmit::ReservedForLarge).
    • Guards < 1 MiB card admissions above 85% DXGI local video memory without inflating CARD_REFUSED[1], keeping card_room_kept() intact for upcoming >= 1 MiB buffers.
  2. Fence-safe LRU card-buffer demotion (crates/victory-gpu/src/shadow.rs, host.rs, descriptor/host.rs):
    • Tracks per-shadow LRU (used, access_tick), GPU flight fences (last_fence: AtomicU64, stamped in Host::submit), and UAV/shader write state (Card::gpu_written).
    • Safely demotes cold, fence-completed < 1 MiB card buffers back to host RAM (never touching hot >= 1 MiB buffers) when:
      • a >= 1 MiB buffer hits CapFull or DxgiPressure (computes exact shortfall, skips futile partial demotion, runs up to 4 bounded 16 MiB batches, and re-validates entry after re-acquiring BUFFERS), or
      • DXGI local video-memory usage reaches >= 88% (VICTORY_CARD_PRESSURE_PCT, targeting 84% hysteresis) in Host::sweep, recording freed bytes in CARD_PRESSURE_RELEASED so used + card_room_kept() in resource::room() genuinely drops for texture streaming.
    • Strictly verifies Arc::strong_count == 1, host::references(&shadow.buffer) == 1, pending.is_empty(), and last_fence <= completed_fence (+ unchanged fence across readback) after tables_changed().
    • Preserves exact bytes by reading back VRAM via Host::demote_readback (serialized by a narrow leaf QUEUE_SUBMIT mutex, using transient L0 staging dropped immediately after readback, with DemoteCopy reset and Win32 event CloseHandle on drop) and merging all bytes outside card.dirty into card.staging before promoting card.staging to shadow.buffer. Opt-out via VICTORY_CARD_DEMOTE=0.
  3. Diagnostics counters (shadow.rs, host.rs):
    • Emits card_demoted_count, card_demoted_bytes, and card_reserved_large_rejects via card_tier_stats() and the periodic card-buffer log line.

Verification

  • Added 7 unit + self-contained D3D12 WARP tests (shadow::vram_tier_tests::*):
    • small_buffer_flood_cannot_starve_large_buffers_with_tiered_reserve
    • dxgi_pressure_target_reclaims_headroom_on_8gb_and_12gb_cards
    • card_room_kept_and_admit_shortfall_preserve_vram_accounting_invariants
    • warp_small_buffer_flood_reserves_headroom_and_demotes_for_large_buffers
    • warp_demotion_preserves_exact_bytes_across_gpu_writes_and_cpu_dirty_ranges
    • warp_demotion_never_evicts_inflight_or_referenced_card_buffers
    • warp_dxgi_8gb_and_12gb_pressure_demotes_cold_small_and_keeps_hot_large
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu vram_tier_tests -- --test-threads=1: 7 passed; 0 failed (0 D3D12 debug errors)
  • cargo test -p victory-gpu -- --test-threads=1: 219 passed; 0 failed; 6 ignored
  • cargo test -p victory-gpu --all-features -- --test-threads=1: 237 passed; 0 failed; 7 ignored
  • cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated & --all-features: 0 warnings/errors in modified files (3 pre-existing AtomicU32::fetch_update deprecations in untouched object.rs)
  • cargo run -p xtask -- policy: asset policy: 220 files ok
<!-- greyfinch-sync:fxceless/victory-fork:issue:5507942346 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1280). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-26T10:04:12Z. --- ### Problem Closes / addresses #1163 and 8–12 GB VRAM starvation: - When `VICTORY_CARD_BUFFERS_MIN_KB=64` is combined with `VICTORY_CARD_BUFFERS_MB=1024` (or smaller caps such as `512` MiB), thousands of small `< 1 MiB` buffers fill the flat card-buffer cap first and starve hot `>= 1 MiB` buffers back to PC RAM (`100–122.6 MB/frame` CPU compare and severe GPU slowdown). - On 8–12 GB GPUs, once cold small buffers occupy VRAM, they remain pinned on the card until unregistered, starving streaming allocations under DXGI video-memory pressure. ### Changes 1. **Size-tiered headroom reservation (`crates/victory-gpu/src/shadow.rs`):** - When `VICTORY_CARD_BUFFERS_MIN_KB < 1024`, reserves `min(256 MiB, cap / 3)` (env-tunable via `VICTORY_CARD_LARGE_RESERVE_MB`; `0` disables) inside `VICTORY_CARD_BUFFERS_MB` for `>= 1 MiB` buffers so small-buffer floods cap at `cap - reserve` (`CardAdmit::ReservedForLarge`). - Guards `< 1 MiB` card admissions above `85%` DXGI local video memory without inflating `CARD_REFUSED[1]`, keeping `card_room_kept()` intact for upcoming `>= 1 MiB` buffers. 2. **Fence-safe LRU card-buffer demotion (`crates/victory-gpu/src/shadow.rs`, `host.rs`, `descriptor/host.rs`):** - Tracks per-shadow LRU `(used, access_tick)`, GPU flight fences (`last_fence: AtomicU64`, stamped in `Host::submit`), and UAV/shader write state (`Card::gpu_written`). - Safely demotes cold, fence-completed `< 1 MiB` card buffers back to host RAM (never touching hot `>= 1 MiB` buffers) when: - a `>= 1 MiB` buffer hits `CapFull` or `DxgiPressure` (computes exact shortfall, skips futile partial demotion, runs up to 4 bounded 16 MiB batches, and re-validates `entry` after re-acquiring `BUFFERS`), or - DXGI local video-memory usage reaches `>= 88%` (`VICTORY_CARD_PRESSURE_PCT`, targeting `84%` hysteresis) in `Host::sweep`, recording freed bytes in `CARD_PRESSURE_RELEASED` so `used + card_room_kept()` in `resource::room()` genuinely drops for texture streaming. - Strictly verifies `Arc::strong_count == 1`, `host::references(&shadow.buffer) == 1`, `pending.is_empty()`, and `last_fence <= completed_fence` (+ unchanged fence across readback) after `tables_changed()`. - Preserves exact bytes by reading back VRAM via `Host::demote_readback` (serialized by a narrow leaf `QUEUE_SUBMIT` mutex, using transient L0 staging dropped immediately after readback, with `DemoteCopy` reset and Win32 event `CloseHandle` on drop) and merging all bytes outside `card.dirty` into `card.staging` before promoting `card.staging` to `shadow.buffer`. Opt-out via `VICTORY_CARD_DEMOTE=0`. 3. **Diagnostics counters (`shadow.rs`, `host.rs`):** - Emits `card_demoted_count`, `card_demoted_bytes`, and `card_reserved_large_rejects` via `card_tier_stats()` and the periodic card-buffer log line. ### Verification - Added 7 unit + self-contained D3D12 WARP tests (`shadow::vram_tier_tests::*`): - `small_buffer_flood_cannot_starve_large_buffers_with_tiered_reserve` - `dxgi_pressure_target_reclaims_headroom_on_8gb_and_12gb_cards` - `card_room_kept_and_admit_shortfall_preserve_vram_accounting_invariants` - `warp_small_buffer_flood_reserves_headroom_and_demotes_for_large_buffers` - `warp_demotion_preserves_exact_bytes_across_gpu_writes_and_cpu_dirty_ranges` - `warp_demotion_never_evicts_inflight_or_referenced_card_buffers` - `warp_dxgi_8gb_and_12gb_pressure_demotes_cold_small_and_keeps_hot_large` - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu vram_tier_tests -- --test-threads=1`: **7 passed; 0 failed** (0 D3D12 debug errors) - `cargo test -p victory-gpu -- --test-threads=1`: **219 passed; 0 failed; 6 ignored** - `cargo test -p victory-gpu --all-features -- --test-threads=1`: **237 passed; 0 failed; 7 ignored** - `cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated` & `--all-features`: **0 warnings/errors** in modified files (3 pre-existing `AtomicU32::fetch_update` deprecations in untouched `object.rs`) - `cargo run -p xtask -- policy`: `asset policy: 220 files ok`
Vladimir-Human commented 2026-11-26 11:05:32 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-26T11:01:20Z.


Rebased cleanly onto latest main (06702fe, commit 9418259) and added one safety hardening fix:

  • tiles.rs UpdateTileMappings serialization: exposed host::queue_lock() (narrow leaf mutex QUEUE_SUBMIT, strict Tiled::given -> QUEUE_SUBMIT ordering) and held it around queue.UpdateTileMappings(...) in Tiled::give_locked so every direct Host::queue operation (ExecuteCommandLists, Signal, Wait, UpdateTileMappings) is serialized against concurrent card-demotion readbacks.

Re-verified on 06702fe

  • cargo run -p xtask -- policy: asset policy: 220 files ok
  • cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated (default & --all-features): 0 warnings
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib vram_tier_tests -- --test-threads=1: 7 passed; 0 failed (0 D3D12 debug errors)
  • cargo test -p victory-gpu -- tiles --test-threads=1: 4 passed; 0 failed; 1 ignored
  • cargo test -p victory-gpu -- --test-threads=1: 220 passed; 0 failed; 6 ignored
  • cargo test -p victory-gpu --all-features -- --test-threads=1: 239 passed; 0 failed; 7 ignored
<!-- greyfinch-sync:fxceless/victory-fork:comment:7190154813 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1280#issuecomment-7190154813). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-26T11:01:20Z. --- Rebased cleanly onto latest `main` (`06702fe`, commit `9418259`) and added one safety hardening fix: - **`tiles.rs` `UpdateTileMappings` serialization:** exposed `host::queue_lock()` (narrow leaf mutex `QUEUE_SUBMIT`, strict `Tiled::given -> QUEUE_SUBMIT` ordering) and held it around `queue.UpdateTileMappings(...)` in `Tiled::give_locked` so every direct `Host::queue` operation (`ExecuteCommandLists`, `Signal`, `Wait`, `UpdateTileMappings`) is serialized against concurrent card-demotion readbacks. ### Re-verified on `06702fe` - `cargo run -p xtask -- policy`: `asset policy: 220 files ok` - `cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated` (default & `--all-features`): 0 warnings - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib vram_tier_tests -- --test-threads=1`: **7 passed; 0 failed** (0 D3D12 debug errors) - `cargo test -p victory-gpu -- tiles --test-threads=1`: **4 passed; 0 failed; 1 ignored** - `cargo test -p victory-gpu -- --test-threads=1`: **220 passed; 0 failed; 6 ignored** - `cargo test -p victory-gpu --all-features -- --test-threads=1`: **239 passed; 0 failed; 7 ignored**
Vladimir-Human commented 2026-11-27 07:16:01 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-27T07:16:01Z.


Rebased perf/vram-tiered-card-eviction onto main (9a9091d, head 8588f62), integrating VICTORY_INPLACE_CONSTANTS ahead of card admission and preserving pending card.patch.ranges masked-patch bytes across readback demotion (with WARP regression test).
Verified with VICTORY_D3D12_DEBUG=1: clippy clean (default & diagnostics), shadow:: 45/45, descriptor:: 39/39, inplace:: 8/8, and tiles:: 3/3 default (1 ignored).

<!-- greyfinch-sync:fxceless/victory-fork:comment:7212305315 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1280#issuecomment-7212305315). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T07:16:01Z. --- Rebased `perf/vram-tiered-card-eviction` onto `main` (`9a9091d`, head `8588f62`), integrating `VICTORY_INPLACE_CONSTANTS` ahead of card admission and preserving pending `card.patch.ranges` masked-patch bytes across readback demotion (with WARP regression test). Verified with `VICTORY_D3D12_DEBUG=1`: `clippy` clean (default & `diagnostics`), `shadow::` 45/45, `descriptor::` 39/39, `inplace::` 8/8, and `tiles::` 3/3 default (1 ignored).
akimfomin commented 2026-11-28 00:22:00 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by akimfomin on 2026-11-28T00:22:00Z.


PortBridge checked memory changes against current main 2eade68. 97d270b (#1280), 3f3ec37 (#1286) and 4351fb4 (#1500) each merge there. #1280 + #1286 conflicts in host.rs and shadow.rs; #1286 + #1500 conflicts in shadow.rs; #1280 + #1500 is clean by Git.

One useful gate after selecting a pair: queue an in-flight shadow resource, trigger demotion and the CPU decommit policy while its fence is held, then release the fence. Check that the resource is preserved before completion and only eligible after. AnyXboxX’s PR #550 busy-pool test is a complementary control for pressure with a busy allocator, not a substitute for fence lifetime. Is #1280 + #1500 the intended pair to validate first, or is #1286 the one to retain? PortBridge.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7230855411 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1280#issuecomment-7230855411). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T00:22:00Z. --- PortBridge checked memory changes against [current main `2eade68`](https://github.com/fxceless/victory-fork/commit/2eade68cade9b2f8cd2511cc97328e83f58508c3). `97d270b` (#1280), `3f3ec37` ([#1286](https://github.com/fxceless/victory-fork/pull/1286)) and `4351fb4` ([#1500](https://github.com/fxceless/victory-fork/pull/1500)) each merge there. #1280 + #1286 conflicts in `host.rs` and `shadow.rs`; #1286 + #1500 conflicts in `shadow.rs`; #1280 + #1500 is clean by Git. One useful gate after selecting a pair: queue an in-flight shadow resource, trigger demotion and the CPU decommit policy while its fence is held, then release the fence. Check that the resource is preserved before completion and only eligible after. AnyXboxX’s [PR #550 busy-pool test](https://github.com/LooperSalty/AnyXboxX/pull/550#issuecomment-7230084159) is a complementary control for pressure with a busy allocator, not a substitute for fence lifetime. Is #1280 + #1500 the intended pair to validate first, or is #1286 the one to retain? [PortBridge](https://github.com/akimfomin/PortBridge).
Vladimir-Human commented 2026-11-28 06:39:58 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Vladimir-Human on 2026-11-28T06:39:58Z.


Good catch on pairing — here is how they relate and what we'd suggest:

  • #1280 (VRAM) and #1286 (CPU RAM) target separate budgets, and as you noted, #1280 + #1500 merges cleanly today while #1280 + #1286 shares host.rs/shadow.rs. Validating #1280 + #1500 first is the lowest-friction path.
  • We are also keeping #1286 rebased on 6b3ce6c (main) so whichever order lands, the second PR rebases cleanly without losing either invariant.
  • Agreed on the in-flight fence + simultaneous demote/decommit test gate if both #1280 and #1286 are queued together: a shadow resource held by an uncompleted fence must remain exempt from both GPU readback demotion and cold snapshot MEM_DECOMMIT until the fence completes, and become eligible right after. Once #1280 lands (or if we decide to pair them), we'll include that combined gate test directly.
<!-- greyfinch-sync:fxceless/victory-fork:comment:7235441113 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1280#issuecomment-7235441113). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-28T06:39:58Z. --- Good catch on pairing — here is how they relate and what we'd suggest: - **`#1280` (VRAM) and `#1286` (CPU RAM) target separate budgets**, and as you noted, `#1280 + #1500` merges cleanly today while `#1280 + #1286` shares `host.rs`/`shadow.rs`. Validating **`#1280 + #1500` first** is the lowest-friction path. - We are also keeping `#1286` rebased on `6b3ce6c` (`main`) so whichever order lands, the second PR rebases cleanly without losing either invariant. - Agreed on the **in-flight fence + simultaneous demote/decommit** test gate if both `#1280` and `#1286` are queued together: a shadow resource held by an uncompleted fence must remain exempt from both GPU readback demotion and cold snapshot `MEM_DECOMMIT` until the fence completes, and become eligible right after. Once `#1280` lands (or if we decide to pair them), we'll include that combined gate test directly.
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/1280:upstream-pr/1280'
git switch 'upstream-pr/1280'

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 'main'
git merge --no-ff 'upstream-pr/1280'
git switch 'upstream-pr/1280'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1280'
git switch 'upstream-pr/1280'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1280'
git switch 'main'
git merge --squash 'upstream-pr/1280'
git switch 'main'
git merge --ff-only 'upstream-pr/1280'
git switch 'main'
git merge 'upstream-pr/1280'
git push origin 'main'
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
fxceless/victory-fork!1277
No description provided.