GPU: cache parsed shader resource metadata across runs (shader-meta.bin) #2612

Open
Bebrik229 wants to merge 2 commits from upstream-pr/2614 into main
Bebrik229 commented 2026-11-25 16:52:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Bebrik229 on 2026-11-25T16:52:54Z.


Summary

Persists each translated shader container's parsed UsedResource table alongside the pipeline cache (shader-meta.bin) so warm runs avoid repeating Container::parse and zlib XBCH decompression (native_programs) on every shader cache hit.

  • New module crates/victory-gpu/src/shader_meta.rs:
    • Loads shader-meta.bin (VSM1 v1 binary store keyed by the container's 32-byte BLAKE3 hash) in library::use_pipeline_cache.
    • get_or_compute checks a RwLock<MetaStore> under read lock first, drops the lock before computing on a miss, and marks the store dirty on insertion.
    • save short-circuits lock-free/read-only when clean, serializes outside the store lock on dirty state, and writes atomically via shader-meta.bin.tmp-<pid>-<seq> + rename from library::save.
    • Losslessly encodes/decodes all ResourceType (Srv, Sampler, Cbv, Uav, Other(u8)) and RootType (Constants, Descriptor, Table, ImmediateSampler, ResourceHeap, SamplerHeap, Other(u32)) variants, rejecting truncated/corrupt/trailing data.
  • No cache invalidation: shader_cache.rs and crates/victory-shader are untouched, so all existing cached shader translations remain valid hits on the next launch (shader-meta.bin populates automatically on the first run and hits from the second run onward).
  • crates/victory-gpu/src/pipeline.rs: stores full_hash: [u8; 32] on Shader (avoiding double hashing in shader()) and resolves used in with_resources via shader_meta::get_or_compute.

Verification

  • cargo test -p victory-gpu --lib shader_meta -- --test-threads=1 — 2 passed (encode_decode_roundtrip_and_corruption, store_persists_to_disk_and_skips_recompute)
  • cargo test -p victory-gpu --lib pipeline -- --test-threads=1 — 16 passed, 0 failed
  • cargo clippy -p victory-gpu --lib --tests -- -D warnings — clean (0 warnings)
  • cargo xtask policy — asset policy: 185 files ok
<!-- greyfinch-sync:Marvisak/VIctory:issue:6609372986 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2614). Originally posted by [Bebrik229](https://github.com/Bebrik229) on 2026-11-25T16:52:54Z. --- ## Summary Persists each translated shader container's parsed `UsedResource` table alongside the pipeline cache (`shader-meta.bin`) so warm runs avoid repeating `Container::parse` and zlib `XBCH` decompression (`native_programs`) on every shader cache hit. - **New module `crates/victory-gpu/src/shader_meta.rs`**: - Loads `shader-meta.bin` (`VSM1` v1 binary store keyed by the container's 32-byte BLAKE3 hash) in `library::use_pipeline_cache`. - `get_or_compute` checks a `RwLock<MetaStore>` under read lock first, drops the lock before computing on a miss, and marks the store dirty on insertion. - `save` short-circuits lock-free/read-only when clean, serializes outside the store lock on dirty state, and writes atomically via `shader-meta.bin.tmp-<pid>-<seq>` + rename from `library::save`. - Losslessly encodes/decodes all `ResourceType` (`Srv`, `Sampler`, `Cbv`, `Uav`, `Other(u8)`) and `RootType` (`Constants`, `Descriptor`, `Table`, `ImmediateSampler`, `ResourceHeap`, `SamplerHeap`, `Other(u32)`) variants, rejecting truncated/corrupt/trailing data. - **No cache invalidation**: `shader_cache.rs` and `crates/victory-shader` are untouched, so all existing cached shader translations remain valid hits on the next launch (`shader-meta.bin` populates automatically on the first run and hits from the second run onward). - **`crates/victory-gpu/src/pipeline.rs`**: stores `full_hash: [u8; 32]` on `Shader` (avoiding double hashing in `shader()`) and resolves `used` in `with_resources` via `shader_meta::get_or_compute`. ## Verification - `cargo test -p victory-gpu --lib shader_meta -- --test-threads=1` — 2 passed (`encode_decode_roundtrip_and_corruption`, `store_persists_to_disk_and_skips_recompute`) - `cargo test -p victory-gpu --lib pipeline -- --test-threads=1` — 16 passed, 0 failed - `cargo clippy -p victory-gpu --lib --tests -- -D warnings` — clean (0 warnings) - `cargo xtask policy` — `asset policy: 185 files ok`
DrDarkDK commented 2026-11-25 17:04:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T16:54:15Z.

Review: CHANGES_REQUESTED


TMP_SEQ gives each save a unique temporary file. The snapshots can still be published out of order:

  1. A worker snapshots {A}, clears dirty, and pauses before rename.
  2. Entry B arrives. The shutdown save snapshots {A, B} and publishes it.
  3. The worker renames its older {A} snapshot over it. The store is clean, so B has no pending retry.

Even without B, shutdown can see dirty == false while the worker is still writing, skip the save, and terminate the process before the rename.

Could save_in take a save mutex before checking dirty and hold it through rename? The map lock can still be released before disk I/O. A test that pauses the worker before rename would cover the ordering.

Static review only.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6061571091 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2614#pullrequestreview-6061571091). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T16:54:15Z. **Review:** CHANGES_REQUESTED --- `TMP_SEQ` gives each save a unique temporary file. The snapshots can still be published out of order: 1. A worker snapshots `{A}`, clears `dirty`, and pauses before rename. 2. Entry `B` arrives. The shutdown save snapshots `{A, B}` and publishes it. 3. The worker renames its older `{A}` snapshot over it. The store is clean, so `B` has no pending retry. Even without `B`, shutdown can see `dirty == false` while the worker is still writing, skip the save, and terminate the process before the rename. Could `save_in` take a save mutex before checking `dirty` and hold it through rename? The map lock can still be released before disk I/O. A test that pauses the worker before rename would cover the ordering. Static review only. *AI-assisted with Codex.*
kotekompotek commented 2026-11-25 18:02:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:51:39Z.


Reviewed (first half): carrying the full BLAKE3 alongside the 16-char display hash and passing it into translate() makes the metadata cache key collision-free. The SHADERS map key is now the full hash — consistent. CI green.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170101320 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2614#issuecomment-7170101320). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T17:51:39Z. --- Reviewed (first half): carrying the full BLAKE3 alongside the 16-char display hash and passing it into `translate()` makes the metadata cache key collision-free. The SHADERS map key is now the full hash — consistent. CI green.
Marvisak commented 2026-11-25 23:06:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:16:55Z.


Automated triage for the maintainers.

Merges cleanly. The idea (skip Container::parse / native_programs on warm runs) is plausible, but as written the store can go stale:

  • shader-meta.bin is keyed only by the container's BLAKE3 (shader_meta::get_or_compute). The shader cache next to it keys every entry on the build fingerprint (shader_cache::key: victory_shader::SOURCE_HASH plus its own encoding), so a translator change invalidates it. Here, a fix to native_programs or to how UsedResource is derived would keep serving the old resource lists from disk, silently, with no way to notice except deleting the file. Please key the store (or the file header) on the same fingerprint, e.g. victory_shader::SOURCE_HASH, and drop the file when it differs.
  • The description gives no measurement of what this saves on a warm run (time spent in with_resources before and after). A 488-line persistent cache wants a number showing it is worth having.

With the fingerprint keying and a measured gain this could be reconsidered.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176165326 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2614#issuecomment-7176165326). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:16:55Z. --- <!-- victory-triage v1 head=8c42f1476eb848455ac364b651b001c34ef931ea verdict=needs-work at=2026-11-25T22:17:30Z --> Automated triage for the maintainers. Merges cleanly. The idea (skip `Container::parse` / `native_programs` on warm runs) is plausible, but as written the store can go stale: - `shader-meta.bin` is keyed only by the container's BLAKE3 (`shader_meta::get_or_compute`). The shader cache next to it keys every entry on the build fingerprint (`shader_cache::key`: `victory_shader::SOURCE_HASH` plus its own encoding), so a translator change invalidates it. Here, a fix to `native_programs` or to how `UsedResource` is derived would keep serving the old resource lists from disk, silently, with no way to notice except deleting the file. Please key the store (or the file header) on the same fingerprint, e.g. `victory_shader::SOURCE_HASH`, and drop the file when it differs. - The description gives no measurement of what this saves on a warm run (time spent in `with_resources` before and after). A 488-line persistent cache wants a number showing it is worth having. With the fingerprint keying and a measured gain this could be reconsidered. --- _Generated by [Claude Code](https://claude.ai/code)_
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/2614:upstream-pr/2614'
git switch 'upstream-pr/2614'

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