Reclaim expired GPU detile buffers under VRAM pressure #2240

Open
LucaruluDev wants to merge 1 commit from upstream-pr/2240 into main
LucaruluDev commented 2026-11-29 10:55:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by LucaruluDev on 2026-11-29T10:55:45Z.


Refs #1620

What it changes

The GPU detile pool can hold up to 256 MiB of idle buffers when local VRAM is at its DXGI budget and host memory is under no pressure. This update keeps sh3nw4ld's full-pool host-memory trim from #2633 and limits the new VRAM path to returned buffers that have been idle for at least two seconds.

After the other pools have had a chance to release memory, tyr0nsur's DXGI recheck (from 7daea79aa12d7d9f39a211ab76abe0b75328a8bc, an earlier revision of this PR) runs outside the pool lock if an expired buffer is eligible. A valid reading below budget cancels the trim. An unavailable or zero initial budget skips reclamation. If the recheck fails or reports a zero budget, the valid initial reading still permits trimming expired buffers. Active buffers stay with their staging objects, and recently returned buffers remain available for exact-size reuse. VICTORY_GPU_DETILE is off by default.

Upstream rewrote main after the previous revision of this branch. The branch is now a single commit, 683d8d70342a4253b916a0c5d497911d69152031, directly on main at 5fabb46eaf6ed8771321787f8871051abde69b9b, so it no longer carries the pre-rewrite commits. The diff against main is unchanged: host.rs and texture.rs, +170/−4. tyr0nsur keeps co-authorship of the commit.

How it was tested

  • GPU and profile: Windows 11, RTX 5070, Rust 1.98.1. Offscreen D3D12 and unit tests. No game profile, resolution, DLSS, frame generation or ray tracing settings were used.
  • Before: An offscreen D3D12 harness on the pre-rewrite base e1251c3199465565147711887351d80359162bb0 retained all 256 MiB in both the mixed-age and fully idle scenarios under a synthetic local VRAM budget.
  • After: In the same harness, the reclamation code in this commit reclaimed 224 MiB in the mixed-age scenario and kept both recent 16 MiB buffers, with zero recreations. It reclaimed the full 256 MiB in the fully idle scenario. When sibling pools lowered DXGI usage below the synthetic budget, the recheck kept the detile spares. Active GPU copy checks reported zero mismatches. That measurement predates the pool-limit test below, which is the only later addition and is test-only. These were synthetic pressure scenarios using native D3D12 resources and DXGI readings. Gameplay and FPS were not tested.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A.
  • cargo test passed for the crates it touches: focused victory-gpu tests passed. The full crate and workspace suites were not run.

Tested locally on 683d8d70342a4253b916a0c5d497911d69152031 (tree b9af163bc7fb85cdc94afcf5ce5729a3637639aa) against main at 5fabb46eaf6ed8771321787f8871051abde69b9b:

Command Result
cargo check --locked -j 4 -p victory-gpu --all-targets Passed.
cargo test --locked -j 4 -p victory-gpu --lib texture:: -- --test-threads=2 24 passed, 0 failed, 4 benchmark tests ignored.
cargo test --locked -j 4 -p victory-gpu --lib texture::spare_linear_tests -- --nocapture --test-threads=1 2 passed. The D3D12 subprocess verified 16 retained buffers, 256 MiB accounting and rejection of the extra buffer.
cargo clippy --workspace --all-targets --locked -j 4 -- -D warnings Exit 101 with the same five findings as main (below), none in files this PR touches.

The release (cargo build --release --locked -p victory) and diagnostics (cargo build --locked --profile diag -p victory --features diagnostics) builds passed on a local integration with the same tree hash, b9af163b.

Clippy fails on main with the same five findings introduced by #4634: two too_many_arguments in host/font.rs, one manual_is_multiple_of in host/menu.rs, and two collapsible_if in host/panel.rs. This PR touches none of those files. #4679 and #4707 both address all five. Current main check · #4679 · #4707

Known coverage gap: trim_idle_spare_linear has no unit test of its own. the_spare_detile_pool_trims_whole (from #2633) covers the full trim under host pressure, and staging_drop_rejects_spares_past_the_pool_limit covers the 256 MiB cap. Neither exercises the three behaviours specific to this PR: rechecking DXGI only when a trim would happen, expiring only cold spares while at budget, and keeping warm spares under sustained pressure. Those were checked only in the offscreen harness above.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #2633 and #3503 cover host-pressure trimming, #2338 changes buffer leasing, #4138 moves releases to a background reaper, #3858 and #4305 change sweep scheduling, and #2038 covers the texture pools. None implements the same age-based trim of detile spares under VRAM-only pressure.
  • Merge-order conflict with #2338 and #4138, in both directions. This PR turns SpareLinear into SpareLinear { buffer, returned_ms }. #2338 builds the tuple form (SpareLinear(buffer) in give_linear) and #4138 reads it (.map(|s| s.0) in the reaper). Whichever of the three merges second will not compile until it adapts to the other's form. If this PR merges first, #2338 and #4138 need a small adaptation to the struct. If either merges first, this PR will be updated to match.

AI-assisted implementation and validation using OpenAI Codex and Claude Code. The reclamation policy was independently reviewed with Claude Code.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5550024500 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2240). Originally posted by [LucaruluDev](https://github.com/LucaruluDev) on 2026-11-29T10:55:45Z. --- Refs #1620 ## What it changes The GPU detile pool can hold up to 256 MiB of idle buffers when local VRAM is at its DXGI budget and host memory is under no pressure. This update keeps sh3nw4ld's full-pool host-memory trim from #2633 and limits the new VRAM path to returned buffers that have been idle for at least two seconds. After the other pools have had a chance to release memory, tyr0nsur's DXGI recheck (from `7daea79aa12d7d9f39a211ab76abe0b75328a8bc`, an earlier revision of this PR) runs outside the pool lock if an expired buffer is eligible. A valid reading below budget cancels the trim. An unavailable or zero initial budget skips reclamation. If the recheck fails or reports a zero budget, the valid initial reading still permits trimming expired buffers. Active buffers stay with their staging objects, and recently returned buffers remain available for exact-size reuse. `VICTORY_GPU_DETILE` is off by default. Upstream rewrote `main` after the previous revision of this branch. The branch is now a single commit, `683d8d70342a4253b916a0c5d497911d69152031`, directly on `main` at `5fabb46eaf6ed8771321787f8871051abde69b9b`, so it no longer carries the pre-rewrite commits. The diff against `main` is unchanged: `host.rs` and `texture.rs`, +170/−4. tyr0nsur keeps co-authorship of the commit. ## How it was tested - GPU and profile: Windows 11, RTX 5070, Rust 1.98.1. Offscreen D3D12 and unit tests. No game profile, resolution, DLSS, frame generation or ray tracing settings were used. - Before: An offscreen D3D12 harness on the pre-rewrite base `e1251c3199465565147711887351d80359162bb0` retained all 256 MiB in both the mixed-age and fully idle scenarios under a synthetic local VRAM budget. - After: In the same harness, the reclamation code in this commit reclaimed 224 MiB in the mixed-age scenario and kept both recent 16 MiB buffers, with zero recreations. It reclaimed the full 256 MiB in the fully idle scenario. When sibling pools lowered DXGI usage below the synthetic budget, the recheck kept the detile spares. Active GPU copy checks reported zero mismatches. That measurement predates the pool-limit test below, which is the only later addition and is test-only. These were synthetic pressure scenarios using native D3D12 resources and DXGI readings. Gameplay and FPS were not tested. - For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A. - `cargo test` passed for the crates it touches: focused `victory-gpu` tests passed. The full crate and workspace suites were not run. Tested locally on `683d8d70342a4253b916a0c5d497911d69152031` (tree `b9af163bc7fb85cdc94afcf5ce5729a3637639aa`) against `main` at `5fabb46eaf6ed8771321787f8871051abde69b9b`: | Command | Result | | --- | --- | | `cargo check --locked -j 4 -p victory-gpu --all-targets` | Passed. | | `cargo test --locked -j 4 -p victory-gpu --lib texture:: -- --test-threads=2` | 24 passed, 0 failed, 4 benchmark tests ignored. | | `cargo test --locked -j 4 -p victory-gpu --lib texture::spare_linear_tests -- --nocapture --test-threads=1` | 2 passed. The D3D12 subprocess verified 16 retained buffers, 256 MiB accounting and rejection of the extra buffer. | | `cargo clippy --workspace --all-targets --locked -j 4 -- -D warnings` | Exit 101 with the same five findings as `main` (below), none in files this PR touches. | The release (`cargo build --release --locked -p victory`) and diagnostics (`cargo build --locked --profile diag -p victory --features diagnostics`) builds passed on a local integration with the same tree hash, `b9af163b`. Clippy fails on `main` with the same five findings introduced by #4634: two `too_many_arguments` in `host/font.rs`, one `manual_is_multiple_of` in `host/menu.rs`, and two `collapsible_if` in `host/panel.rs`. This PR touches none of those files. #4679 and #4707 both address all five. [Current main check](https://github.com/trustno1-coder/VIctory-NVIDIA/actions/runs/46563306780/job/142355972539) · [#4679](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4679) · [#4707](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4707) Known coverage gap: `trim_idle_spare_linear` has no unit test of its own. `the_spare_detile_pool_trims_whole` (from #2633) covers the full trim under host pressure, and `staging_drop_rejects_spares_past_the_pool_limit` covers the 256 MiB cap. Neither exercises the three behaviours specific to this PR: rechecking DXGI only when a trim would happen, expiring only cold spares while at budget, and keeping warm spares under sustained pressure. Those were checked only in the offscreen harness above. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #2633 and #3503 cover host-pressure trimming, #2338 changes buffer leasing, #4138 moves releases to a background reaper, #3858 and #4305 change sweep scheduling, and #2038 covers the texture pools. None implements the same age-based trim of detile spares under VRAM-only pressure. - Merge-order conflict with #2338 and #4138, in both directions. This PR turns `SpareLinear` into `SpareLinear { buffer, returned_ms }`. #2338 builds the tuple form (`SpareLinear(buffer)` in `give_linear`) and #4138 reads it (`.map(|s| s.0)` in the reaper). Whichever of the three merges second will not compile until it adapts to the other's form. If this PR merges first, #2338 and #4138 need a small adaptation to the struct. If either merges first, this PR will be updated to match. AI-assisted implementation and validation using OpenAI Codex and Claude Code. The reclamation policy was independently reviewed with Claude Code.
tyr0nsur commented 2026-11-30 07:13:12 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by tyr0nsur on 2026-11-30T07:13:12Z.


I reproduced a same-tick ordering case on 24426e6. The sweep samples DXGI, calls pool::room(video), then passes the same sample to linear_spare_room. If the first reclaim already takes local usage under budget, the second can still discard reusable detile buffers.

On a Windows RTX 4060, I reproduced this in three fresh processes with real resident D3D12 allocations and a simulated trigger budget. Dropping two texture heaps took DXGI local usage from 188.45 to 60.45 MiB, below the 124.45 MiB trigger. With the old sample the detile pool still dropped both 16 MiB spares. With a fresh sample it retained both, and DXGI stayed at 60.45 MiB.

Cherry-pickable fix. It rechecks DXGI outside the pool lock only when the original sample would trim a cold spare. Empty and warm-only pools get no additional query. The three focused tests and the staged asset-policy check passed. Claude Opus 5.5 reviewed the call order.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7271488197 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2240#issuecomment-7271488197). Originally posted by [tyr0nsur](https://github.com/tyr0nsur) on 2026-11-30T07:13:12Z. --- I reproduced a same-tick ordering case on `24426e6`. The sweep samples DXGI, calls `pool::room(video)`, then passes the same sample to `linear_spare_room`. If the first reclaim already takes local usage under budget, the second can still discard reusable detile buffers. On a Windows RTX 4060, I reproduced this in three fresh processes with real resident D3D12 allocations and a simulated trigger budget. Dropping two texture heaps took DXGI local usage from 188.45 to 60.45 MiB, below the 124.45 MiB trigger. With the old sample the detile pool still dropped both 16 MiB spares. With a fresh sample it retained both, and DXGI stayed at 60.45 MiB. [Cherry-pickable fix](https://github.com/tyr0nsur/VIctory-NVIDIA/commit/7daea79aa12d7d9f39a211ab76abe0b75328a8bc). It rechecks DXGI outside the pool lock only when the original sample would trim a cold spare. Empty and warm-only pools get no additional query. The three focused tests and the staged asset-policy check passed. Claude Opus 5.5 reviewed the call order.
LucaruluDev commented 2026-11-30 08:38:38 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by LucaruluDev on 2026-11-30T08:38:38Z.


Thanks, @tyr0nsur, for reproducing the same-tick reclaim and providing 7daea79. I kept your exact commit and authorship in the local candidate.

I reproduced the ordering issue independently on an RTX 5070 in five paired offscreen D3D12 runs. Releasing two resident texture heaps reduced local usage from 205.66 to 77.66 MiB against a simulated 141.66 MiB limit. The published code still discarded both 16 MiB spares. Your commit retained both in all five runs and still retired them when the fresh sample showed pressure. Empty and warm pools made no extra query. Each run also verified an active 16 MiB GPU copy byte for byte.

The independent policy tests covered failed or invalid queries and a concurrent checkout and return with the mutex released during the refresh. The conditional local and non-local DXGI query pair measured 0.7–1.1 µs in this fixture. Release and diagnostics builds both passed on a local merge with 41adb551. The pressure threshold and buffer age were simulated.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7273161952 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2240#issuecomment-7273161952). Originally posted by [LucaruluDev](https://github.com/LucaruluDev) on 2026-11-30T08:38:38Z. --- Thanks, @tyr0nsur, for reproducing the same-tick reclaim and providing `7daea79`. I kept your exact commit and authorship in the local candidate. I reproduced the ordering issue independently on an RTX 5070 in five paired offscreen D3D12 runs. Releasing two resident texture heaps reduced local usage from 205.66 to 77.66 MiB against a simulated 141.66 MiB limit. The published code still discarded both 16 MiB spares. Your commit retained both in all five runs and still retired them when the fresh sample showed pressure. Empty and warm pools made no extra query. Each run also verified an active 16 MiB GPU copy byte for byte. The independent policy tests covered failed or invalid queries and a concurrent checkout and return with the mutex released during the refresh. The conditional local and non-local DXGI query pair measured 0.7–1.1 µs in this fixture. Release and diagnostics builds both passed on a local merge with `41adb551`. The pressure threshold and buffer age were simulated.
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/2240:upstream-pr/2240'
git switch 'upstream-pr/2240'

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