GPU: add opt-in LRU eviction for unreferenced shadow buffer copies (VICTORY_SHADOW_BUDGET_MB) #3457

Open
Luislariosss wants to merge 3 commits from upstream-pr/3457 into main
Luislariosss commented 2026-11-27 03:26:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Luislariosss on 2026-11-27T03:26:35Z.


Summary

Adds opt-in LRU eviction (VICTORY_SHADOW_BUDGET_MB, off by default) for large unreferenced Shadow buffer copies in shadow::BUFFERS:

  • Stamps each Shadow with a monotonic last_used sequence on creation and on every shadow::find() hit.
  • Once per presented frame (in Host::execute, right beside resource::evict_unused(), after the previous submission has finished), shadow::evict_unused() evicts least-recently-used buffer copies when total live shadow bytes exceed VICTORY_SHADOW_BUDGET_MB.
  • Strictly evicts only copies with Arc::strong_count == 1 and empty pending/before writeback state — guaranteeing no active command list, descriptor heap slot, or pending GPU writeback is ever disturbed.
  • Calls changed() so memory.rs page tables refresh on next use, and drops all evicted Arc<Shadow>s outside BUFFERS.lock().

Validation

  • cargo test -p victory-gpu --lib shadow::: all shadow tests passed (including new shadow_budget_evicts_lru_unreferenced_copies_and_keeps_held_ones).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics): clean.
  • cargo xtask policy: 191 files ok.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5520652352 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3457). Originally posted by [Luislariosss](https://github.com/Luislariosss) on 2026-11-27T03:26:35Z. --- ### Summary Adds opt-in LRU eviction (`VICTORY_SHADOW_BUDGET_MB`, off by default) for large unreferenced `Shadow` buffer copies in `shadow::BUFFERS`: - Stamps each `Shadow` with a monotonic `last_used` sequence on creation and on every `shadow::find()` hit. - Once per presented frame (in `Host::execute`, right beside `resource::evict_unused()`, after the previous submission has finished), `shadow::evict_unused()` evicts least-recently-used buffer copies when total live shadow bytes exceed `VICTORY_SHADOW_BUDGET_MB`. - Strictly evicts **only** copies with `Arc::strong_count == 1` and empty `pending`/`before` writeback state — guaranteeing no active command list, descriptor heap slot, or pending GPU writeback is ever disturbed. - Calls `changed()` so `memory.rs` page tables refresh on next use, and drops all evicted `Arc<Shadow>`s **outside** `BUFFERS.lock()`. ### Validation - `cargo test -p victory-gpu --lib shadow::`: all shadow tests passed (including new `shadow_budget_evicts_lru_unreferenced_copies_and_keeps_held_ones`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`): clean. - `cargo xtask policy`: 191 files ok.
Luislariosss commented 2026-11-27 03:37:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Luislariosss on 2026-11-27T03:37:52Z.


Live verification of the 11-PR stack with this PR merged (60 s release runs, Windows 11, AMD RX 9060 XT 16 GB, 32 GB RAM, same cache and boot; every run reached frame 1024 with 0 errors):

Config Peak private commit Peak WS
10-PR stack (without this PR) 10,589.8 MiB 5,745.8 MiB
11-PR stack, feature off (default) 10,588.4 MiB 5,744.1 MiB
11-PR stack, VICTORY_SHADOW_BUDGET_MB=256 11,135.7 MiB 5,923.5 MiB
11-PR stack, VICTORY_SHADOW_BUDGET_MB=1024 11,208.4 MiB 6,787.4 MiB
  • Default (feature off) is regression-free vs the 10-PR stack: the full stack keeps the measured ~4.37 GiB saving vs main.
  • With the eviction active, ~0.5 GiB shows up on this 32 GB box that has no commit pressure; the knob targets low-commit machines where staying under budget beats the churn. Draw counts at frame 1024 vary run-to-run (3,317-6,656), so ~0.5 GiB is near this benchmark's noise floor.
  • shadow:: tests: 12/12 pass post-merge on the integration branch (the new eviction test plus the pre-existing loose-window suite).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7209145156 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7209145156). Originally posted by [Luislariosss](https://github.com/Luislariosss) on 2026-11-27T03:37:52Z. --- Live verification of the 11-PR stack with this PR merged (60 s release runs, Windows 11, AMD RX 9060 XT 16 GB, 32 GB RAM, same cache and boot; every run reached frame 1024 with 0 errors): | Config | Peak private commit | Peak WS | |---|---:|---:| | 10-PR stack (without this PR) | 10,589.8 MiB | 5,745.8 MiB | | 11-PR stack, feature off (default) | **10,588.4 MiB** | 5,744.1 MiB | | 11-PR stack, `VICTORY_SHADOW_BUDGET_MB=256` | 11,135.7 MiB | 5,923.5 MiB | | 11-PR stack, `VICTORY_SHADOW_BUDGET_MB=1024` | 11,208.4 MiB | 6,787.4 MiB | - Default (feature off) is regression-free vs the 10-PR stack: the full stack keeps the measured ~4.37 GiB saving vs `main`. - With the eviction active, ~0.5 GiB shows up on this 32 GB box that has no commit pressure; the knob targets low-commit machines where staying under budget beats the churn. Draw counts at frame 1024 vary run-to-run (3,317-6,656), so ~0.5 GiB is near this benchmark's noise floor. - `shadow::` tests: 12/12 pass post-merge on the integration branch (the new eviction test plus the pre-existing loose-window suite).
oni-swr commented 2026-11-27 04:10:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-27T04:10:36Z.


WARP test report for #3457 (AI-assisted (Claude), claimed on #1633 comment 7209455899; I did not repeat the RX 9060 XT live numbers above)

  • Environment: Windows 11 Pro 10.0.26200, WARP adapter (d3d10warp.dll 10.0.26100.9818, VICTORY_WARP=1 VICTORY_D3D12_WARP=1), rustc 1.98.1 / cargo 1.98.1, Windows SDK 10.0.26100 dxil.dll via VICTORY_DXIL, cargo test -j 4 -p <crate> --lib --locked -- --test-threads=1. No GPU hardware used. No game files involved.
  • Base: origin/main 63bcca43c6a00a758510398fa43096ce7e4f7df0; head 6b442f527447b9b4e1f320be020a51f08a294340. The head already contains this main, so the merge is a fast-forward and the tested tree equals the head.
  • victory-gpu lib: main 231 passed / 0 failed / 1 ignored -> head 232 passed / 0 failed / 1 ignored. The extra test is shadow::tests::shadow_budget_evicts_lru_unreferenced_copies_and_keeps_held_ones. I confirmed it really ran on the WARP host (it does not take its "no host GPU" early return): it evicts the unreferenced copy and keeps the one a caller still holds. No regressions.
  • Default off: with VICTORY_SHADOW_BUDGET_MB unset (or 0/garbage) evict_unused() returns before touching any map, so the only default-path cost is one AtomicU64 stamp per find().
  • Review notes (quick read, not a proof): eviction only takes copies with Arc::strong_count == 1, empty pending and empty before, so a copy held by an in-flight command list, descriptor or snapshot is skipped, and the drop happens after the BUFFERS lock is released. That looks right, provided every in-flight user keeps an Arc<Shadow> rather than only a raw address; the author should confirm this. cfg!(test) forces the budget to None, so the once-per-frame throttle in evict_unused() and the env parsing path are not exercised by tests. The new test calls evict_unused_with_budget(0), which evicts every unreferenced shadow in the process-global map, so it could disturb other shadow tests if run in parallel (I ran --test-threads=1).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7209582789 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7209582789). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-27T04:10:36Z. --- **WARP test report for #3457** (AI-assisted (Claude), claimed on #1633 comment 7209455899; I did not repeat the RX 9060 XT live numbers above) - Environment: Windows 11 Pro 10.0.26200, WARP adapter (d3d10warp.dll 10.0.26100.9818, `VICTORY_WARP=1 VICTORY_D3D12_WARP=1`), rustc 1.98.1 / cargo 1.98.1, Windows SDK 10.0.26100 `dxil.dll` via `VICTORY_DXIL`, `cargo test -j 4 -p <crate> --lib --locked -- --test-threads=1`. No GPU hardware used. No game files involved. - Base: origin/main `63bcca43c6a00a758510398fa43096ce7e4f7df0`; head `6b442f527447b9b4e1f320be020a51f08a294340`. The head already contains this main, so the merge is a fast-forward and the tested tree equals the head. - `victory-gpu` lib: main **231 passed / 0 failed / 1 ignored** -> head **232 passed / 0 failed / 1 ignored**. The extra test is `shadow::tests::shadow_budget_evicts_lru_unreferenced_copies_and_keeps_held_ones`. I confirmed it really ran on the WARP host (it does not take its "no host GPU" early return): it evicts the unreferenced copy and keeps the one a caller still holds. No regressions. - Default off: with `VICTORY_SHADOW_BUDGET_MB` unset (or `0`/garbage) `evict_unused()` returns before touching any map, so the only default-path cost is one `AtomicU64` stamp per `find()`. - Review notes (quick read, not a proof): eviction only takes copies with `Arc::strong_count == 1`, empty `pending` and empty `before`, so a copy held by an in-flight command list, descriptor or snapshot is skipped, and the drop happens after the `BUFFERS` lock is released. That looks right, provided every in-flight user keeps an `Arc<Shadow>` rather than only a raw address; the author should confirm this. `cfg!(test)` forces the budget to `None`, so the once-per-frame throttle in `evict_unused()` and the env parsing path are not exercised by tests. The new test calls `evict_unused_with_budget(0)`, which evicts every unreferenced shadow in the process-global map, so it could disturb other shadow tests if run in parallel (I ran `--test-threads=1`).
Luislariosss commented 2026-11-27 04:22:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Luislariosss on 2026-11-27T04:22:31Z.


Follow-up to the WARP report: an independent audit found one blocking gap this PR's tests did not cover. Bytes shaders write through a heap's unordered access views never go back to title memory (bindless refresh's shortcut): once the slots viewing a copy were all rebound, its refcount could drop to one, the budget pass could evict it, and find() would rebuild it from stale title memory, silently losing the GPU's bytes. Fixed in a070759:

  • Each heap counts its UAV ranges per copy (Shadow::uav_ranges: added with the first range, removed with the last or when the heap goes); the eviction pass skips copies with any, and re-checks before the take.
  • The budget now counts only what it can free, and when copies it cannot evict alone are past the budget it warns once and skips the pass (evicting every evictable copy each frame only churned).
  • A VICTORY_SHADOW_BUDGET_MB that does not parse warns once instead of silently turning off.
  • The two budget tests are serialized behind a shared mutex (the report's parallel-eviction concern), and a new regression test covers the UAV case end to end.

Verified: 12/12 shadow::, 233/233 victory-gpu --lib, clippy -D warnings clean; 60 s and 300 s live runs with the knob at 256 MiB show 0 errors (the 300 s run reaches the 3D world).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7209746590 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7209746590). Originally posted by [Luislariosss](https://github.com/Luislariosss) on 2026-11-27T04:22:31Z. --- Follow-up to the WARP report: an independent audit found one blocking gap this PR's tests did not cover. Bytes shaders write through a heap's unordered access views never go back to title memory (bindless `refresh`'s shortcut): once the slots viewing a copy were all rebound, its refcount could drop to one, the budget pass could evict it, and `find()` would rebuild it from stale title memory, silently losing the GPU's bytes. Fixed in a070759: - Each heap counts its UAV ranges per copy (`Shadow::uav_ranges`: added with the first range, removed with the last or when the heap goes); the eviction pass skips copies with any, and re-checks before the take. - The budget now counts only what it can free, and when copies it cannot evict alone are past the budget it warns once and skips the pass (evicting every evictable copy each frame only churned). - A `VICTORY_SHADOW_BUDGET_MB` that does not parse warns once instead of silently turning off. - The two budget tests are serialized behind a shared mutex (the report's parallel-eviction concern), and a new regression test covers the UAV case end to end. Verified: 12/12 `shadow::`, 233/233 `victory-gpu --lib`, clippy `-D warnings` clean; 60 s and 300 s live runs with the knob at 256 MiB show 0 errors (the 300 s run reaches the 3D world).
Luislariosss commented 2026-11-28 03:53:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Luislariosss on 2026-11-28T03:53:02Z.


Follow-up safety fixes in 3d4460a closing two remaining correctness gaps found during self-audit:

  1. Live heap slots with own == false: Kept previously stored only ID3D12Resource when own == false, so Arc::strong_count == 1 could evict a copy while a descriptor slot still referenced its buffer. Added a lightweight slots: Arc<()> token on Shadow stored in Kept for buffer_srv/buffer_uav; eviction now requires no live slot tokens (Arc::strong_count(&s.slots) == 1).
  2. Permanent heap-UAV protection: remove_uav_range previously made a copy evictable again once UAV ranges were rebound, even though GPU UAV writes never copy back to title memory. Replaced the reversible counter with a permanent heap_uav: AtomicBool (note_heap_uav()), so any copy written through a heap UAV is never evicted.

Verified with cargo xtask policy (191 files ok), cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics), cargo test -p victory-gpu --lib shadow:: (13/13) and descriptor:: (22/22).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7233476039 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7233476039). Originally posted by [Luislariosss](https://github.com/Luislariosss) on 2026-11-28T03:53:02Z. --- Follow-up safety fixes in `3d4460a` closing two remaining correctness gaps found during self-audit: 1. **Live heap slots with `own == false`**: `Kept` previously stored only `ID3D12Resource` when `own == false`, so `Arc::strong_count == 1` could evict a copy while a descriptor slot still referenced its buffer. Added a lightweight `slots: Arc<()>` token on `Shadow` stored in `Kept` for `buffer_srv`/`buffer_uav`; eviction now requires no live slot tokens (`Arc::strong_count(&s.slots) == 1`). 2. **Permanent heap-UAV protection**: `remove_uav_range` previously made a copy evictable again once UAV ranges were rebound, even though GPU UAV writes never copy back to title memory. Replaced the reversible counter with a permanent `heap_uav: AtomicBool` (`note_heap_uav()`), so any copy written through a heap UAV is never evicted. Verified with `cargo xtask policy` (191 files ok), `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`), `cargo test -p victory-gpu --lib shadow::` (13/13) and `descriptor::` (22/22).
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/3457:upstream-pr/3457'
git switch 'upstream-pr/3457'

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/3457'
git switch 'upstream-pr/3457'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3457'
git switch 'upstream-pr/3457'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3457'
git switch 'main'
git merge --squash 'upstream-pr/3457'
git switch 'main'
git merge --ff-only 'upstream-pr/3457'
git switch 'main'
git merge 'upstream-pr/3457'
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
Marvisak/VIctory!3457
No description provided.