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

Open
github-bot wants to merge 5 commits from github-bot/gh-2974 into main AGit
Owner

@⁠0xKaji on GitHub · 2026-11-26 01:50 UTC · original
0xKaji:perf/shader-pipeline-memory → main · +315 −56 · 6 files · 5 commits · open

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).
**@⁠0xKaji** on GitHub · 2026-11-26 01:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2974) `0xKaji:perf/shader-pipeline-memory` → `main` · +315 −56 · 6 files · 5 commits · open ## 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:03 +00:00
Author
Owner

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

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

**@⁠Marvisak** on GitHub · 2026-11-26 05:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2974#issuecomment-7183548626) <!-- 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)_
Author
Owner

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

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
**@⁠fuzeofficlal** on GitHub · 2026-11-26 02:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2974#pullrequestreview-6067615372) · **reviewed** 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
Author
Owner

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

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.

**@⁠kotekompotek** on GitHub · 2026-11-26 02:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/2974#pullrequestreview-6067786233) · **reviewed** 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.
Author
Owner

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

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).
**@⁠0xKaji** on GitHub · 2026-11-26 12:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2974#issuecomment-7191924858) 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).
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/2974/head:github-bot/gh-2974'
git switch 'github-bot/gh-2974'

Merge

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