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

Open
github-bot wants to merge 2 commits from github-bot/gh-3457 into main AGit
Owner

@⁠Luislariosss on GitHub · 2026-11-27 03:26 UTC · original
Luislariosss:feat/shadow-budget-evict → main · +296 −2 · 3 files · 2 commits · open

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** on GitHub · 2026-11-27 03:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3457) `Luislariosss:feat/shadow-budget-evict` → `main` · +296 −2 · 3 files · 2 commits · open ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:30:18 +00:00
Author
Owner

@⁠Luislariosss on GitHub · 2026-11-27 03:37 UTC · original

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).
**@⁠Luislariosss** on GitHub · 2026-11-27 03:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7209145156) 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).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 04:10 UTC · original

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).
**@⁠oni-swr** on GitHub · 2026-11-27 04:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7209582789) **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`).
Bytes shaders write through a heap's unordered access views stay in the
host copy and never go back to title memory (bindless, refresh's
shortcut). Once the slots viewing it were rewritten, its refcount could
drop to one, so VICTORY_SHADOW_BUDGET_MB evicted it and find made it
again from stale title memory: later draws read the title's bytes where
the shader's were, silently.

Each heap counts the ranges of a copy it views as unordered access
(Shadow::uav_ranges, taken with the first and given back with the last,
or when the heap goes); the eviction pass skips copies with any.

The pass also stops counting only what it can free: when copies it
cannot evict alone are past the budget it warns once and skips instead
of evicting every evictable copy each frame. A VICTORY_SHADOW_BUDGET_MB
value that does not parse warns once. New test: a marked copy survives
a zero-budget pass and is evicted once unmarked.
Author
Owner

@⁠Luislariosss on GitHub · 2026-11-27 04:22 UTC · original

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** on GitHub · 2026-11-27 04:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/3457#issuecomment-7209746590) 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).
This pull request can be merged automatically.
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/3457/head:github-bot/gh-3457'
git switch 'github-bot/gh-3457'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3457'
git switch 'github-bot/gh-3457'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3457'
git switch 'github-bot/gh-3457'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3457'
git switch 'main'
git merge --squash 'github-bot/gh-3457'
git switch 'main'
git merge --ff-only 'github-bot/gh-3457'
git switch 'main'
git merge 'github-bot/gh-3457'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3457
No description provided.