GPU: reclaim idle texture staging, command list scratch, and shadow arena buffers #2972

Open
github-bot wants to merge 6 commits from github-bot/gh-2972 into main AGit
Owner

@⁠0xKaji on GitHub · 2026-11-26 01:50 UTC · original
0xKaji:perf/reclaim-gpu-buffers → main · +188 −30 · 3 files · 6 commits · open

Summary

  • Texture staging chunks (texture.rs): trim Chunks::all after an idle window of 64 submissions down to the recent peak (with a 2-chunk / 64 MiB warm floor) instead of retaining up to 16 chunks (512 MiB) permanently.
  • Shadow view arena (shadow.rs): key Slice::chunk by stable chunk ID and release empty chunks past 1 standard spare (and any empty oversized chunk) in Arena::give_back.
  • Command list scratch & collections (host.rs): trim idle HostList::scratch chunks after 64 resets (keeping 1 spare) and shrink oversized cleared states/twins/notes/targets collections (capacity > 256 → shrink_to(64)).
  • System-memory eviction (resource.rs): trigger evict_unused when GPU shared system memory (system_budget) exceeds BUDGET_HIGH, not only when local VRAM does.

Testing

  • cargo xwin check --tests --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --locked passes cleanly.
  • New unit tests (staging_chunks_trim_after_idle_window, scratch_chunks_trim_after_idle_window, eviction_over_triggers_on_system_memory_pressure, arena_runs_are_aligned_to_their_elements_and_join_again) pass under Wine (RTX 3060).
  • Tested live in GTA VI (Series S mode) with no regressions.
**@⁠0xKaji** on GitHub · 2026-11-26 01:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972) `0xKaji:perf/reclaim-gpu-buffers` → `main` · +188 −30 · 3 files · 6 commits · open ## Summary - **Texture staging chunks (`texture.rs`):** trim `Chunks::all` after an idle window of 64 submissions down to the recent peak (with a 2-chunk / 64 MiB warm floor) instead of retaining up to 16 chunks (512 MiB) permanently. - **Shadow view arena (`shadow.rs`):** key `Slice::chunk` by stable chunk ID and release empty chunks past 1 standard spare (and any empty oversized chunk) in `Arena::give_back`. - **Command list scratch & collections (`host.rs`):** trim idle `HostList::scratch` chunks after 64 resets (keeping 1 spare) and shrink oversized cleared `states`/`twins`/`notes`/`targets` collections (`capacity > 256` → `shrink_to(64)`). - **System-memory eviction (`resource.rs`):** trigger `evict_unused` when GPU shared system memory (`system_budget`) exceeds `BUDGET_HIGH`, not only when local VRAM does. ## Testing - `cargo xwin check --tests --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --locked` passes cleanly. - New unit tests (`staging_chunks_trim_after_idle_window`, `scratch_chunks_trim_after_idle_window`, `eviction_over_triggers_on_system_memory_pressure`, `arena_runs_are_aligned_to_their_elements_and_join_again`) pass under Wine (RTX 3060). - Tested live in GTA VI (Series S mode) with no regressions.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:00 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-26 03:25 UTC · original

Wine 11.0 / vkd3d-proton 3.2.0, physical RTX 4060 Laptop GPU (8 GiB), 16 GiB system RAM: I verified exact head 7e769e07e274 with four focused Windows-target tests: the two idle chunk trim fixtures 2/2, shared-system-memory eviction policy 1/1, and shadow arena coalescing/release 1/1 with a required real NVIDIA host. All passed. The diagnostic logs are local (verify-pr2972-updated-wine-{chunks-trim,memory-eviction,rtx4060-arena}.log); the temporary required-host assertion was restored.

A separately guarded title load on that same head reached picker/loading art, not a controllable world. In the extended scheduled 130 s run, the external guard stopped at t=86.25 s when global MemAvailable fell from 13,297 to 1,979 MiB, below the 2,048 MiB safety floor; scope used 7,450 MiB, anon 2,787 MiB, GPU peaked at 3,113 MiB, exit 137 from the guard. The earlier 95 s run started with different available RAM and timed out while above its floor, so these trials do not establish improvement or regression versus base. No title crash or world-frame failure is attributed to this PR.

This PR is useful bounded reclamation, but it is not an application-wide host RAM cap: the texture eviction runs once per presented frame and idle trimming after 64 submissions may not fire during a stalled load. A distinct opt-in cooperative loading/admission budget with explicit no-progress handling is requested in #3012. If adding allocation-time pressure handling here, please preserve pinned/live data and never wait while holding host queue locks. No safety guard was disabled and no gameplay/FPS claim is made.

**@⁠pickles976** on GitHub · 2026-11-26 03:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972#issuecomment-7181713103) Wine 11.0 / vkd3d-proton 3.2.0, physical **RTX 4060 Laptop GPU (8 GiB)**, 16 GiB system RAM: I verified exact head `7e769e07e274` with four focused Windows-target tests: the two idle chunk trim fixtures 2/2, shared-system-memory eviction policy 1/1, and shadow arena coalescing/release 1/1 with a required real NVIDIA host. All passed. The diagnostic logs are local (`verify-pr2972-updated-wine-{chunks-trim,memory-eviction,rtx4060-arena}.log`); the temporary required-host assertion was restored. A separately **guarded title load** on that same head reached picker/loading art, not a controllable world. In the extended scheduled 130 s run, the external guard stopped at t=86.25 s when global `MemAvailable` fell from 13,297 to **1,979 MiB**, below the 2,048 MiB safety floor; scope used 7,450 MiB, anon 2,787 MiB, GPU peaked at 3,113 MiB, exit 137 from the guard. The earlier 95 s run started with different available RAM and timed out while above its floor, so these trials do **not** establish improvement or regression versus base. No title crash or world-frame failure is attributed to this PR. This PR is useful bounded reclamation, but it is not an application-wide host RAM cap: the texture eviction runs once per presented frame and idle trimming after 64 submissions may not fire during a stalled load. A distinct opt-in cooperative loading/admission budget with explicit no-progress handling is requested in https://git.gtahub.st/github/Marvisak--VIctory/issues/3012. If adding allocation-time pressure handling here, please preserve pinned/live data and never wait while holding host queue locks. No safety guard was disabled and no gameplay/FPS claim is made.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:48 UTC · original

Automated triage for the maintainers.

Needs work. The idle trimming looks sound. texture::recycle/trim_target and HostList::reset/trim_scratch run when nothing is executing. Arena chunks dropped in shadow::Arena::give_back stay alive as long as any slice holds its buffer clone, and slices now use stable chunk ids, so removing a chunk no longer shifts indices. The part that needs changing is resource::evict_unused/eviction_over:

  • host::video_memory()'s second entry is DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL. On a discrete GPU, the host textures evict gives up live in local video memory, so evicting them hardly lowers non-local usage. Non-local usage is mostly upload heaps and shadow copies. Once it is over BUDGET_HIGH, every presented frame would ask choose for the same over bytes and evict textures again, which can turn into make/evict churn without relieving the pressure. Please either limit this to adapters where it helps (UMA, where local is the system memory anyway) or show measurements (non-local usage and textures evicted per frame) from a run where it fires.
  • The test eviction_over_triggers_on_system_memory_pressure only checks the arithmetic. It can't show that eviction reduces the memory being measured.
  • Smaller: give_back now walks every chunk with retain on each return, and these come by the hundred per frame. Cheap for a few chunks, but checking only the chunk just given back would do.

The tester's run above didn't establish a memory improvement or a regression. If the eviction-trigger change were split out, the chunk and collection trimming could be judged on its own. Before/after RSS at the same point would help it too.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 05:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972#issuecomment-7183816483) <!-- victory-triage v1 head=7e769e07e274dff7985b57e5b84042dfcda6feef verdict=needs-work at=2026-11-26T05:47:50Z --> Automated triage for the maintainers. **Needs work.** The idle trimming looks sound. `texture::recycle`/`trim_target` and `HostList::reset`/`trim_scratch` run when nothing is executing. Arena chunks dropped in `shadow::Arena::give_back` stay alive as long as any slice holds its `buffer` clone, and slices now use stable chunk ids, so removing a chunk no longer shifts indices. The part that needs changing is `resource::evict_unused`/`eviction_over`: - `host::video_memory()`'s second entry is `DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL`. On a discrete GPU, the host textures `evict` gives up live in local video memory, so evicting them hardly lowers non-local usage. Non-local usage is mostly upload heaps and shadow copies. Once it is over `BUDGET_HIGH`, every presented frame would ask `choose` for the same `over` bytes and evict textures again, which can turn into make/evict churn without relieving the pressure. Please either limit this to adapters where it helps (UMA, where local is the system memory anyway) or show measurements (non-local usage and textures evicted per frame) from a run where it fires. - The test `eviction_over_triggers_on_system_memory_pressure` only checks the arithmetic. It can't show that eviction reduces the memory being measured. - Smaller: `give_back` now walks every chunk with `retain` on each return, and these come by the hundred per frame. Cheap for a few chunks, but checking only the chunk just given back would do. The tester's run above didn't establish a memory improvement or a regression. If the eviction-trigger change were split out, the chunk and collection trimming could be judged on its own. Before/after RSS at the same point would help it too. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot changed title from GPU: reclaim idle staging, scratch, and arena buffers and evict on system memory pressure to GPU: reclaim idle texture staging, command list scratch, and shadow arena buffers 2026-11-26 12:25:09 +00:00
github-bot force-pushed github-bot/gh-2972 from 7e769e07e2 to 5c0ad8e109 2026-11-26 12:25:10 +00:00 Compare
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 02:01 UTC · original · reviewed

Reviewed and verified against main (63bcca43) on Windows 11 (RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1) — clean and ready to merge:

  • Lifetime & concurrency safety: monotonic ArenaChunk::id prevents slice index invalidation when Arena::give_back retains active chunks; cloned COM ID3D12Resource handles held by Shadow and retired HostHeap::kept entries keep GPU-referenced buffers alive until completion; texture::recycle runs under Host::execute post-wait and HostList::reset runs only on idle lists.
  • Non-local memory pressure: reuses DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL from host::video_memory() to evict idle host textures LRU when shared GPU memory exceeds the 90% budget threshold.
  • 7e769e07 resolves the two clippy::manual-clamp warnings in host.rs:988 and texture.rs:208.

Local verification:

  • victory-gpu (host::tests::, resource::tests::, shadow::, texture::, watch::, draw::tests:: with VICTORY_D3D12_DEBUG=1, tested at 6451ee8c plus verified 2-line clamp commit 7e769e07): 98/98 passed on both RTX 5090 and WARP (excluding the pre-existing NVIDIA 1M view-heap baseline test), including the 3 new reclamation tests and expanded arena_runs_are_aligned_to_their_elements_and_join_again
  • cargo xtask policy: 191 files ok
**@⁠fuzeofficlal** on GitHub · 2026-11-26 02:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972#pullrequestreview-6067561646) · **reviewed** Reviewed and verified against `main` (`63bcca43`) on Windows 11 (RTX 5090 + WARP, `VICTORY_D3D12_DEBUG=1`) — clean and ready to merge: - **Lifetime & concurrency safety**: monotonic `ArenaChunk::id` prevents slice index invalidation when `Arena::give_back` retains active chunks; cloned COM `ID3D12Resource` handles held by `Shadow` and retired `HostHeap::kept` entries keep GPU-referenced buffers alive until completion; `texture::recycle` runs under `Host::execute` post-wait and `HostList::reset` runs only on idle lists. - **Non-local memory pressure**: reuses `DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL` from `host::video_memory()` to evict idle host textures LRU when shared GPU memory exceeds the 90% budget threshold. - `7e769e07` resolves the two `clippy::manual-clamp` warnings in `host.rs:988` and `texture.rs:208`. **Local verification**: - `victory-gpu` (`host::tests::`, `resource::tests::`, `shadow::`, `texture::`, `watch::`, `draw::tests::` with `VICTORY_D3D12_DEBUG=1`, tested at `6451ee8c` plus verified 2-line `clamp` commit `7e769e07`): **98/98 passed** on both **RTX 5090** and **WARP** (excluding the pre-existing NVIDIA 1M view-heap baseline test), including the 3 new reclamation tests and expanded `arena_runs_are_aligned_to_their_elements_and_join_again` - `cargo xtask policy`: 191 files ok
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 02:29 UTC · original · reviewed

Verified: the peak-window trimming is right on both allocators — excess above the cap still trims immediately, and the window-end keep is clamped to [spare, cap] of the observed peak. The Slice chunk->id change is a latent correctness fix too: give_back after retain's index shift would have freed into the wrong chunk. Arena retain drops oversized empties outright and keeps one spare standard chunk. eviction_over's max() of local/system excess correctly extends pressure detection to shared memory.

**@⁠kotekompotek** on GitHub · 2026-11-26 02:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972#pullrequestreview-6067766826) · **reviewed** Verified: the peak-window trimming is right on both allocators — excess above the cap still trims immediately, and the window-end keep is clamped to [spare, cap] of the observed peak. The Slice chunk->id change is a latent correctness fix too: give_back after retain's index shift would have freed into the wrong chunk. Arena retain drops oversized empties outright and keeps one spare standard chunk. eviction_over's max() of local/system excess correctly extends pressure detection to shared memory.
Author
Owner

@⁠0xKaji on GitHub · 2026-11-26 12:21 UTC · original

Addressed all triage points:

  1. Removed the resource::evict_unused / eviction_over change from this PR as suggested, so it is strictly focused on bounded idle buffer/collection reclamation (zero risk of discrete-GPU texture make/evict churn).
  2. Optimized shadow::Arena::give_back: now only checks for release when the returned chunk itself becomes empty (free.get(&0) == Some(&size)), avoiding walking/retaining all chunks on every return.
  3. Before/after measurements (GTA VI Series S, 90 s A/B on main vs combined branch): menu FPS remained 30.0 fps (33.3 ms/frame); system memory dropped 1,427 -> 1,407 MB in menus and 6,622 -> 6,466 MB working set at world load start.
**@⁠0xKaji** on GitHub · 2026-11-26 12:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972#issuecomment-7191896447) Addressed all triage points: 1. **Removed the `resource::evict_unused` / `eviction_over` change** from this PR as suggested, so it is strictly focused on bounded idle buffer/collection reclamation (zero risk of discrete-GPU texture make/evict churn). 2. **Optimized `shadow::Arena::give_back`:** now only checks for release when the returned chunk itself becomes empty (`free.get(&0) == Some(&size)`), avoiding walking/retaining all chunks on every return. 3. **Before/after measurements (GTA VI Series S, 90 s A/B on `main` vs combined branch):** menu FPS remained **30.0 fps (33.3 ms/frame)**; `system memory` dropped **1,427 -> 1,407 MB** in menus and **6,622 -> 6,466 MB** working set at world load start.
Author
Owner

@⁠Desamodeth on GitHub · 2026-11-26 20:39 UTC · original · reviewed

The states shrink in HostList::reset reclaims memory after a burst, but also shrinks a map that the next recording immediately refills. On 5c0ad8e1 the steady-list threshold showed up between 224 and 225 distinct textures.

Same Windows x64 process, Ryzen 7 9800X3D, Rust 1.98.1 MSVC release. One synthetic list, 192 record/reset cycles, one state insertion per texture and a second transition for every fourth texture. Median µs per complete cycle; allocation counts cover all 192 cycles, including the initial fill.

Distinct textures per recording main 63bcca43 PR local 64-reset variant allocations (main / PR / variant)
224 6.13 6.14 6.14 7 / 7 / 7
225 6.18 8.32 6.16 8 / 582 / 8
512 14.03 18.85 14.09 9 / 774 / 9
512 every eighth recording, otherwise 16 2.18 2.75 2.18 9 / 102 / 9

The memory win is real: after one 4,096-texture recording followed by 16-texture recordings, main kept 204,816 bytes of map storage. The PR kept 3,216 bytes immediately. The local variant reached the same 3,216 bytes on reset 128. It tracks the maximum map length over each 64-reset window; after clearing, it calls shrink_to(peak.max(64)) only at the window boundary when capacity is over 256 and the peak used at most half of it. The first window still included the spike. A list that stops resetting will retain the larger map under that policy.

I'd consider using recent occupancy for this map if busy lists commonly pass the threshold. Reversing the timing order reproduced the 225-texture result (6.20 / 8.36 / 6.19 µs). This isolates the source track and state-map reset bodies with synthetic Arc-backed host textures; first/last state and release of the list's references were checked. Allocator-requested map bytes were counted in a separate build. No GPU, title run or process RSS measurement. Coordination. AI-assisted with Codex.

**@⁠Desamodeth** on GitHub · 2026-11-26 20:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/2972#pullrequestreview-6080272574) · **reviewed** The `states` shrink in `HostList::reset` reclaims memory after a burst, but also shrinks a map that the next recording immediately refills. On `5c0ad8e1` the steady-list threshold showed up between **224 and 225 distinct textures**. Same Windows x64 process, Ryzen 7 9800X3D, Rust 1.98.1 MSVC release. One synthetic list, 192 record/reset cycles, one state insertion per texture and a second transition for every fourth texture. Median µs per complete cycle; allocation counts cover all 192 cycles, including the initial fill. | Distinct textures per recording | main `63bcca43` | PR | local 64-reset variant | allocations (main / PR / variant) | |---|---:|---:|---:|---:| | 224 | 6.13 | 6.14 | 6.14 | 7 / 7 / 7 | | 225 | 6.18 | 8.32 | 6.16 | 8 / 582 / 8 | | 512 | 14.03 | 18.85 | 14.09 | 9 / 774 / 9 | | 512 every eighth recording, otherwise 16 | 2.18 | 2.75 | 2.18 | 9 / 102 / 9 | The memory win is real: after one 4,096-texture recording followed by 16-texture recordings, main kept **204,816 bytes** of map storage. The PR kept **3,216 bytes** immediately. The local variant reached the same 3,216 bytes on reset 128. It tracks the maximum map length over each 64-reset window; after clearing, it calls `shrink_to(peak.max(64))` only at the window boundary when capacity is over 256 and the peak used at most half of it. The first window still included the spike. A list that stops resetting will retain the larger map under that policy. I'd consider using recent occupancy for this map if busy lists commonly pass the threshold. Reversing the timing order reproduced the 225-texture result (6.20 / 8.36 / 6.19 µs). This isolates the source `track` and state-map reset bodies with synthetic Arc-backed host textures; first/last state and release of the list's references were checked. Allocator-requested map bytes were counted in a separate build. No GPU, title run or process RSS measurement. [Coordination](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633). AI-assisted with Codex.
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/2972/head:github-bot/gh-2972'
git switch 'github-bot/gh-2972'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2972'
git switch 'github-bot/gh-2972'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2972'
git switch 'github-bot/gh-2972'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2972'
git switch 'main'
git merge --squash 'github-bot/gh-2972'
git switch 'main'
git merge --ff-only 'github-bot/gh-2972'
git switch 'main'
git merge 'github-bot/gh-2972'
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!2972
No description provided.