GPU: reduce shader and pipeline RAM usage (Weak cache, deflate containers, 32 MiB DXIL cache) #2972

Open
0xKaji wants to merge 5 commits from upstream-pr/2974 into main
0xKaji commented 2026-11-26 01:50:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-26T01:50:52Z.


Summary

  • Weak SHADERS cache & pruning (pipeline.rs): store Weak<Shader> in the global shader map with periodic pruning instead of retaining every shader forever.
  • Release Pending::graphics & failed containers (pipeline.rs, make.rs): drop GraphicsState once HostPipeline::new finishes and clear Shader::container if translation fails.
  • Compress Shader::container in RAM (pipeline.rs): deflate containers >= 1 KiB with miniz_oxide (level 1) while kept in memory.
  • Bounded 32 MiB DXIL cache (pipeline.rs, pipeline/host.rs): keep translated DXIL bytecode in a 32 MiB FIFO cache instead of retaining every translated DXIL buffer in Translated, reloading on miss from shader_cache via the compressed container. (Note: caching atomics64 on Translated and hashing once in Shader::new also overlaps slightly with #2211.)

Testing

  • cargo xwin check --tests --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --locked passes cleanly.
  • New unit tests (unreferenced_shaders_are_released_from_cache, dxil_cache_evicts_oldest_past_cap, large_shader_containers_are_stored_compressed, graphics_state_is_released_after_make) pass under Wine (RTX 3060).
  • Tested live in GTA VI (Series S mode): reduced Rust heap by up to 104 MB during world loading with 0 FPS regression (30.0 fps in menus).
<!-- greyfinch-sync:Marvisak/VIctory:issue:5502497289 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2974). Originally posted by [0xKaji](https://github.com/0xKaji) on 2026-11-26T01:50:52Z. --- ## Summary - **Weak `SHADERS` cache & pruning (`pipeline.rs`):** store `Weak<Shader>` in the global shader map with periodic pruning instead of retaining every shader forever. - **Release `Pending::graphics` & failed containers (`pipeline.rs`, `make.rs`):** drop `GraphicsState` once `HostPipeline::new` finishes and clear `Shader::container` if translation fails. - **Compress `Shader::container` in RAM (`pipeline.rs`):** deflate containers >= 1 KiB with `miniz_oxide` (level 1) while kept in memory. - **Bounded 32 MiB DXIL cache (`pipeline.rs`, `pipeline/host.rs`):** keep translated DXIL bytecode in a 32 MiB FIFO cache instead of retaining every translated DXIL buffer in `Translated`, reloading on miss from `shader_cache` via the compressed container. *(Note: caching `atomics64` on `Translated` and hashing once in `Shader::new` also overlaps slightly with #2211.)* ## Testing - `cargo xwin check --tests --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --locked` passes cleanly. - New unit tests (`unreferenced_shaders_are_released_from_cache`, `dxil_cache_evicts_oldest_past_cap`, `large_shader_containers_are_stored_compressed`, `graphics_state_is_released_after_make`) pass under Wine (RTX 3060). - Tested live in GTA VI (Series S mode): reduced Rust heap by up to **104 MB** during world loading with **0 FPS regression** (30.0 fps in menus).
fuzeofficlal commented 2026-11-26 02:09:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T02:09:04Z.

Review: COMMENTED


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

  • Reuses existing miniz_oxide with --locked (zero new dependencies).
  • Lock ordering is clean (SHADERS, Shader.container, and DXIL_CACHE never nest); HostPipeline::new retains Vec<Option<Arc<[u8]>>> across PSO creation and rectangle_gs so bytecode pointers stay valid; Shader::dxil() falls back through t.host.dxil -> DXIL_CACHE -> shader_cache::host_shader(&container) cleanly; Pending.graphics is safely dropped after Pending::make() completes.
  • (Minor non-blocking optimization note: shader() computes blake3::hash(bytes) once before Shader::new() computes it again, and deflate compression runs while holding the SHADERS mutex — compressing/hashing before locking would cut contention during bulk shader loads.)

Local verification:

  • victory-gpu (VICTORY_D3D12_DEBUG=1): RTX 5090 and WARP both passed default pipeline:: 27/27, shader_cache:: 4/4, draw:: 20/20, and --features diagnostics pipeline:: 30/30 (including all 4 new memory-reclamation unit tests)
  • cargo clippy --all-targets -- -D warnings (default & --features diagnostics) + cargo xtask policy (191 files ok): 0 warnings
<!-- greyfinch-sync:Marvisak/VIctory:review:6067615372 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2974#pullrequestreview-6067615372). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T02:09:04Z. **Review:** COMMENTED --- Reviewed and verified against `main` (`63bcca43`) on Windows 11 (RTX 5090 + WARP, `VICTORY_D3D12_DEBUG=1`) — clean design, thread-safe, and ready to merge: - Reuses existing `miniz_oxide` with `--locked` (zero new dependencies). - Lock ordering is clean (`SHADERS`, `Shader.container`, and `DXIL_CACHE` never nest); `HostPipeline::new` retains `Vec<Option<Arc<[u8]>>>` across PSO creation and `rectangle_gs` so bytecode pointers stay valid; `Shader::dxil()` falls back through `t.host.dxil` -> `DXIL_CACHE` -> `shader_cache::host_shader(&container)` cleanly; `Pending.graphics` is safely dropped after `Pending::make()` completes. - *(Minor non-blocking optimization note: `shader()` computes `blake3::hash(bytes)` once before `Shader::new()` computes it again, and deflate compression runs while holding the `SHADERS` mutex — compressing/hashing before locking would cut contention during bulk shader loads.)* **Local verification**: - `victory-gpu` (`VICTORY_D3D12_DEBUG=1`): **RTX 5090** and **WARP** both passed default `pipeline::` **27/27**, `shader_cache::` **4/4**, `draw::` **20/20**, and `--features diagnostics` `pipeline::` **30/30** (including all 4 new memory-reclamation unit tests) - `cargo clippy --all-targets -- -D warnings` (default & `--features diagnostics`) + `cargo xtask policy` (`191 files ok`): 0 warnings
kotekompotek commented 2026-11-26 02:32:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T02:32:47Z.

Review: COMMENTED


Verified all three mechanisms: (1) Weak shader cache prunes dead entries amortized at max(2*len,1024), and dedup still works among live pipelines holding Arcs. (2) container compression stores only when deflate actually wins, translation failure frees it entirely via Empty. (3) host.dxil moves to a bounded FIFO; dxil() correctly chains empty-slot -> RAM cache -> disk shader cache -> re-insert, and the Arc lives in dxils during CreateGraphicsPipelineState so bytecode pointers stay valid. atomics64 hoist to per-translation is right too.

<!-- greyfinch-sync:Marvisak/VIctory:review:6067786233 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2974#pullrequestreview-6067786233). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T02:32:47Z. **Review:** COMMENTED --- Verified all three mechanisms: (1) Weak shader cache prunes dead entries amortized at max(2*len,1024), and dedup still works among live pipelines holding Arcs. (2) container compression stores only when deflate actually wins, translation failure frees it entirely via Empty. (3) host.dxil moves to a bounded FIFO; dxil() correctly chains empty-slot -> RAM cache -> disk shader cache -> re-insert, and the Arc lives in dxils during CreateGraphicsPipelineState so bytecode pointers stay valid. atomics64 hoist to per-translation is right too.
Marvisak commented 2026-11-26 06:03:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T05:30:33Z.


Automated triage for the maintainers.

This reduces RAM use in crates/victory-gpu/src/pipeline.rs in three ways: SHADERS holds Weak<Shader> with pruning, title containers of 1 KiB and up are deflated in memory (miniz_oxide, already a dependency), and translated DXIL moves out of Translated into a 32 MiB FIFO DXIL_CACHE. HostPipeline::new fetches the DXIL through the new Shader::dxil. The 64-bit-atomics check is hoisted into Translated::atomics64, which overlaps #2211. CI: passing.

What it needs:

  • The cost of an eviction. On a miss, Shader::dxil calls shader_cache::host_shader on the inflated container. When there is no disk cache (CACHE unset), that is a full retranslation inside pipeline creation. With a disk cache it is a file read plus an inflate. The same happens for any DXIL over 32 MiB, which DxilCache::insert never keeps. Please show how often misses happen in a play session and what they cost, or keep the DXIL resident when there is no disk cache.
  • Evidence for the saving. "Up to 104 MB" of Rust heap during world loading is the only figure given. A before/after with the same route and the stall times around pipeline creation would show whether the trade is worth it.
  • Translation failures now set Shader::container to Container::Empty. That is fine for translated() (its OnceLock caches the failure), but say so in a comment, since container_bytes() then returns nothing for that shader.
  • Overlap with #2211 on atomics64: one should rebase on the other.

Verdict: needs work. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7183548626 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2974#issuecomment-7183548626). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T05:30:33Z. --- <!-- victory-triage v1 head=db83e4955e893efe5dfe3e8d5e8c75cb2d6ef1a0 verdict=needs-work at=2026-11-26T05:30:22Z --> Automated triage for the maintainers. This reduces RAM use in `crates/victory-gpu/src/pipeline.rs` in three ways: `SHADERS` holds `Weak<Shader>` with pruning, title containers of 1 KiB and up are deflated in memory (`miniz_oxide`, already a dependency), and translated DXIL moves out of `Translated` into a 32 MiB FIFO `DXIL_CACHE`. `HostPipeline::new` fetches the DXIL through the new `Shader::dxil`. The 64-bit-atomics check is hoisted into `Translated::atomics64`, which overlaps #2211. CI: passing. What it needs: - **The cost of an eviction.** On a miss, `Shader::dxil` calls `shader_cache::host_shader` on the inflated container. When there is no disk cache (`CACHE` unset), that is a full retranslation inside pipeline creation. With a disk cache it is a file read plus an inflate. The same happens for any DXIL over 32 MiB, which `DxilCache::insert` never keeps. Please show how often misses happen in a play session and what they cost, or keep the DXIL resident when there is no disk cache. - **Evidence for the saving.** "Up to 104 MB" of Rust heap during world loading is the only figure given. A before/after with the same route and the stall times around pipeline creation would show whether the trade is worth it. - Translation failures now set `Shader::container` to `Container::Empty`. That is fine for `translated()` (its `OnceLock` caches the failure), but say so in a comment, since `container_bytes()` then returns nothing for that shader. - Overlap with #2211 on `atomics64`: one should rebase on the other. **Verdict: needs work.** CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
0xKaji commented 2026-11-26 13:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-26T12:22:45Z.


Addressed all triage and review notes:

  1. Keep DXIL resident when no disk cache is active or DXIL > 32 MiB: Shader::translated now only moves t.host.dxil into DXIL_CACHE when shader_cache::counts() > (0, 0) and dxil.len() <= DXIL_CACHE_BYTES. Without a disk cache (or for oversized DXIL), t.host.dxil stays resident so Shader::dxil() never triggers a re-translation.
  2. Documented Container::Empty on failure: added comment noting self.translated's OnceLock caches None permanently, so container_bytes() is never needed again for failed shaders.
  3. Hashed once and compressed outside SHADERS mutex: pipeline::shader now computes blake3::hash once and runs deflate compression before acquiring the SHADERS lock (per @fuzeofficlal's note).
  4. Before/after measurements (GTA VI Series S, 90 s A/B on main vs combined branch):
    • Menus: Rust heap 602 -> 580 MB (-22 MB), 30.0 fps (33.3 ms/frame) unchanged.
    • World load start (11 MB placed textures): Rust heap 753 -> 649 MB (-104 MB), process commit 2,249 -> 2,166 MB (-83 MB), working set 6,622 -> 6,466 MB (-156 MB).
    • World entry (~4 GB system memory): Rust heap 835 -> 814 MB (-21 MB).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7191924858 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2974#issuecomment-7191924858). Originally posted by [0xKaji](https://github.com/0xKaji) on 2026-11-26T12:22:45Z. --- Addressed all triage and review notes: 1. **Keep DXIL resident when no disk cache is active or DXIL > 32 MiB:** `Shader::translated` now only moves `t.host.dxil` into `DXIL_CACHE` when `shader_cache::counts() > (0, 0)` and `dxil.len() <= DXIL_CACHE_BYTES`. Without a disk cache (or for oversized DXIL), `t.host.dxil` stays resident so `Shader::dxil()` never triggers a re-translation. 2. **Documented `Container::Empty` on failure:** added comment noting `self.translated`'s `OnceLock` caches `None` permanently, so `container_bytes()` is never needed again for failed shaders. 3. **Hashed once and compressed outside `SHADERS` mutex:** `pipeline::shader` now computes `blake3::hash` once and runs deflate compression before acquiring the `SHADERS` lock (per @fuzeofficlal's note). 4. **Before/after measurements (GTA VI Series S, 90 s A/B on `main` vs combined branch):** - Menus: Rust heap **602 -> 580 MB** (-22 MB), **30.0 fps (33.3 ms/frame)** unchanged. - World load start (11 MB placed textures): Rust heap **753 -> 649 MB** (-104 MB), process commit **2,249 -> 2,166 MB** (-83 MB), working set **6,622 -> 6,466 MB** (-156 MB). - World entry (~4 GB system memory): Rust heap **835 -> 814 MB** (-21 MB).
shortselow-byte commented 2026-11-27 01:20:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by shortselow-byte on 2026-11-27T01:20:06Z.


In-game miss rate and cost of DXIL_CACHE, measured on head e1630fb4ee (GTA VI, Series S profile, RX 6900 XT / RDNA 2, Ryzen 5 5600X, 16 GB RAM, disk shader cache warm with 25,756 shaders).

To answer the triage's "how often misses happen in a play session and what they cost", I added a local measure-only counter to Shader::dxil() (not proposed for the PR): hits, misses, and the time a miss spends in container_bytes() + shader_cache::host_shader.

DXIL cache over one 330 s session (menus → world load → ~3 min in the world):

elapsed hits misses total miss time longest miss
50 s (menus) 2,046 2 1.1 ms 0.7 ms
86 s 2,910 4 2.9 ms 0.9 ms
172 s (world) 2,998 5 4.7 ms 1.8 ms
203 s onward 3,002 5 4.7 ms 1.8 ms

So with a disk cache, 5 misses in 3,007 lookups (0.17 %), 4.7 ms in total for the whole session. No miss after 172 s, and the host log's "being made" count stays at 0 from 148 s to the end (330 s) in the world. With a warm disk cache the eviction cost is negligible on this route. I did not test the no-cache case, which your latest push now keeps resident anyway.

Rust heap, main 63bcca4 vs this PR, same binary profile (diag), same cache, one run each:

  • Menus (both runs idle on the same screen for 5 min, steady state): 896 → 727 MB (−169 MB); process commit 11,224 → 11,042 MB.
  • World load, nearest samples to the same elapsed time (108 s on main, 112 s on the PR): 1,865 → 1,671 MB (−194 MB).

That is one run per side, so treat the memory figures as indicative only. Frame times are not comparable: on this 16 GB machine both world runs hit the system commit limit (0x8007000E), main aborted at 179 s, the PR run started logging allocation failures at ~324 s and was stopped by my script at 330 s. That is a property of the machine, not of the PR, so I am not counting it either way.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7207204836 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2974#issuecomment-7207204836). Originally posted by [shortselow-byte](https://github.com/shortselow-byte) on 2026-11-27T01:20:06Z. --- In-game miss rate and cost of `DXIL_CACHE`, measured on head `e1630fb4ee` (GTA VI, Series S profile, RX 6900 XT / RDNA 2, Ryzen 5 5600X, 16 GB RAM, disk shader cache warm with 25,756 shaders). To answer the triage's "how often misses happen in a play session and what they cost", I added a local measure-only counter to `Shader::dxil()` (not proposed for the PR): hits, misses, and the time a miss spends in `container_bytes()` + `shader_cache::host_shader`. **DXIL cache over one 330 s session (menus → world load → ~3 min in the world):** | elapsed | hits | misses | total miss time | longest miss | |---|---|---|---|---| | 50 s (menus) | 2,046 | 2 | 1.1 ms | 0.7 ms | | 86 s | 2,910 | 4 | 2.9 ms | 0.9 ms | | 172 s (world) | 2,998 | 5 | 4.7 ms | 1.8 ms | | 203 s onward | 3,002 | 5 | 4.7 ms | 1.8 ms | So with a disk cache, 5 misses in 3,007 lookups (0.17 %), 4.7 ms in total for the whole session. No miss after 172 s, and the host log's "being made" count stays at 0 from 148 s to the end (330 s) in the world. With a warm disk cache the eviction cost is negligible on this route. I did not test the no-cache case, which your latest push now keeps resident anyway. **Rust heap, `main` `63bcca4` vs this PR, same binary profile (`diag`), same cache, one run each:** - Menus (both runs idle on the same screen for 5 min, steady state): **896 → 727 MB** (−169 MB); process commit 11,224 → 11,042 MB. - World load, nearest samples to the same elapsed time (108 s on `main`, 112 s on the PR): **1,865 → 1,671 MB** (−194 MB). That is one run per side, so treat the memory figures as indicative only. Frame times are not comparable: on this 16 GB machine both world runs hit the system commit limit (`0x8007000E`), `main` aborted at 179 s, the PR run started logging allocation failures at ~324 s and was stopped by my script at 330 s. That is a property of the machine, not of the PR, so I am not counting it either way.
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/2974:upstream-pr/2974'
git switch 'upstream-pr/2974'

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