Skip to content

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

Open
0xKaji wants to merge 5 commits into
Marvisak:mainfrom
0xKaji:perf/shader-pipeline-memory
Open

0xKaji wants to merge 5 commits into
Marvisak:mainfrom
0xKaji:perf/shader-pipeline-memory

Conversation

@0xKaji

@0xKaji 0xKaji commented Nov 26, 2026

Copy link
Copy Markdown

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 GPU: hash shaders once in pipeline::shader() and cache SFI0 atomics64 on Translated #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 fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

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 GPU: hash shaders once in pipeline::shader() and cache SFI0 atomics64 on Translated #2211 on atomics64: one should rebase on the other.

Verdict: needs work. CI: passing.


Generated by Claude Code

@0xKaji

0xKaji commented Nov 26, 2026

Copy link
Copy Markdown
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants