cache shared shaders per batch in known::names, make, and recipes #647

Open
RileyW wants to merge 1 commit from RileyW/Darner:pr/known-shader-cache into Main
First-time contributor

Note: This exact Darner revision has not yet been run end-to-end in the game on Darner main. The same caching optimization has been used in pipeline precompilation on our Windows 10 (i7-8700, RTX 3080 20 GiB) fork, and this Darner port passes all 16 pipeline::known::tests (including 2 new regression tests) and all workspace gates.

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: In crates/darner-gpu/src/pipeline/known.rs, names(), make(), and recipes() invoke parse() with a raw disk reader closure that seeks known-shaders.bin, allocates a fresh Vec, reads the full shader container bytes, hashes them with blake3, and locks the global SHADERS map on every single shader reference in every pipeline entry.
  • Real-world gameplay/runtime impact: During darner warm, darner preload, and early-pipeline startup loading, Darner processes thousands to ~84,000 pipelines that heavily reuse the same vertex, pixel, and compute shaders across state permutations. Re-reading, re-hashing, and relocking the global SHADERS mutex for every shared shader wastes disk I/O, heap allocations, and cross-thread CPU time during startup and shader precompilation.
  • What this does: Introduces a lightweight per-batch/per-worker ShaderLoader holding a HashMap keyed by (stage_id, hash) to Arc<Shader> and wires it into names(), make(), and recipes(). Shared shaders in a batch are read from disk and looked up once, returning Arc::clone on subsequent hits with zero repeated seeks, allocations, blake3 hashes, or global mutex contention. Also validates stage_id before reading from disk. Adds unit tests shader_loader_caches_shared_shaders_per_stage_and_hash and recipes_reuses_cached_shader_arc_across_pipelines.

How it was tested

  • Tested revision: 97089d4 (PR branch on c8258f9) and 96096db (merged into riley/all)
  • Commands and results:
    • cargo clippy --workspace --all-targets --locked -- -D warnings: passed (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passed (0 warnings)
    • cargo xtask policy and cargo xtask policy --staged: passed (576 files ok)
    • cargo test -p darner-gpu --lib --locked pipeline::known::tests::: passed (16 passed, 0 failed)
  • Changes a default: no (pure internal read/lookup cache with identical output and Arc<Shader> instances)
  • A/B result: Verified via unit tests proving shared shaders are read once instead of once per pipeline and return identical Arc::ptr_eq instances across recipes(); no gameplay frame-rate claim (startup and warm/preload throughput improvement).
  • Gates run on that revision: all three workspace gates in AGENTS.md on 97089d4 and 96096db
  • Gameplay, when relevant: Windows 10 22H2, i7-8700, RTX 3080 20 GiB (used in warm on fork; pure CPU/I/O cache)
  • Shader wave sizes tested, when relevant: n/a (pipeline book reader)
  • Hardware dependence: general (all systems; biggest win during darner warm naming and precompilation)
  • Coverage gaps: Not yet timed on a full 84k-pipeline darner warm pass on Darner main.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found
  • Replaces or overlaps: none found
Note: This exact Darner revision has not yet been run end-to-end in the game on Darner main. The same caching optimization has been used in pipeline precompilation on our Windows 10 (i7-8700, RTX 3080 20 GiB) fork, and this Darner port passes all 16 `pipeline::known::tests` (including 2 new regression tests) and all workspace gates. ## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: In `crates/darner-gpu/src/pipeline/known.rs`, `names()`, `make()`, and `recipes()` invoke `parse()` with a raw disk reader closure that seeks `known-shaders.bin`, allocates a fresh `Vec`, reads the full shader container bytes, hashes them with `blake3`, and locks the global `SHADERS` map on every single shader reference in every pipeline entry. - Real-world gameplay/runtime impact: During `darner warm`, `darner preload`, and early-pipeline startup loading, Darner processes thousands to ~84,000 pipelines that heavily reuse the same vertex, pixel, and compute shaders across state permutations. Re-reading, re-hashing, and relocking the global `SHADERS` mutex for every shared shader wastes disk I/O, heap allocations, and cross-thread CPU time during startup and shader precompilation. - What this does: Introduces a lightweight per-batch/per-worker `ShaderLoader` holding a `HashMap` keyed by `(stage_id, hash)` to `Arc<Shader>` and wires it into `names()`, `make()`, and `recipes()`. Shared shaders in a batch are read from disk and looked up once, returning `Arc::clone` on subsequent hits with zero repeated seeks, allocations, `blake3` hashes, or global mutex contention. Also validates `stage_id` before reading from disk. Adds unit tests `shader_loader_caches_shared_shaders_per_stage_and_hash` and `recipes_reuses_cached_shader_arc_across_pipelines`. ## How it was tested - Tested revision: `97089d4` (PR branch on `c8258f9`) and `96096db` (merged into `riley/all`) - Commands and results: - `cargo clippy --workspace --all-targets --locked -- -D warnings`: passed (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passed (0 warnings) - `cargo xtask policy` and `cargo xtask policy --staged`: passed (576 files ok) - `cargo test -p darner-gpu --lib --locked pipeline::known::tests::`: passed (16 passed, 0 failed) - Changes a default: no (pure internal read/lookup cache with identical output and `Arc<Shader>` instances) - A/B result: Verified via unit tests proving shared shaders are read once instead of once per pipeline and return identical `Arc::ptr_eq` instances across `recipes()`; no gameplay frame-rate claim (startup and `warm`/`preload` throughput improvement). - Gates run on that revision: all three workspace gates in AGENTS.md on `97089d4` and `96096db` - Gameplay, when relevant: Windows 10 22H2, i7-8700, RTX 3080 20 GiB (used in `warm` on fork; pure CPU/I/O cache) - Shader wave sizes tested, when relevant: n/a (pipeline book reader) - Hardware dependence: general (all systems; biggest win during `darner warm` naming and precompilation) - Coverage gaps: Not yet timed on a full 84k-pipeline `darner warm` pass on Darner main. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found - Replaces or overlaps: none found
TrustNo1 changed title from [pr-performance][warm] cache shared shaders per batch in known::names, make, and recipes to cache shared shaders per batch in known::names, make, and recipes 2026-12-06 03:25:34 +00:00
Owner

Test needed — commit 97089d40; what to check:

  • The problem on main: In crates/darner-gpu/src/pipeline/known.rs, names(), make(), and recipes() invoke parse() with a raw disk reader closure that seeks known-shaders.bin, allocates a fresh Vec, reads the full shader container bytes, hashes them with blake3, and locks the global SHADERS map on every single shader reference in every pipeline entry.
  • Real-world gameplay/runtime impact: During darner warm, darner preload, and early-pipeline startup loading, Darner processes thousands to ~84,000 pipelines that heavily reuse the same vertex, pixel, and compute shaders across state permutations. Re-reading, re-hashing, and relocking the global SHADERS mutex for every shared shader wastes disk I/O, heap allocations, and cross-thread CPU time during startup and shader precompilation.
  • What this does: Introduces a lightweight per-batch/per-worker ShaderLoader holding a HashMap keyed by (stage_id, hash) to Arc<Shader> and wires it into names(), make(), and recipes(). Shared shaders in a batch are read from disk and looked up once, returning Arc::clone on subsequent hits with zero repeated seeks, allocations, blake3 hashes, or global mutex content

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `97089d40`; what to check: - The problem on main: In `crates/darner-gpu/src/pipeline/known.rs`, `names()`, `make()`, and `recipes()` invoke `parse()` with a raw disk reader closure that seeks `known-shaders.bin`, allocates a fresh `Vec`, reads the full shader container bytes, hashes them with `blake3`, and locks the global `SHADERS` map on every single shader reference in every pipeline entry. - Real-world gameplay/runtime impact: During `darner warm`, `darner preload`, and early-pipeline startup loading, Darner processes thousands to ~84,000 pipelines that heavily reuse the same vertex, pixel, and compute shaders across state permutations. Re-reading, re-hashing, and relocking the global `SHADERS` mutex for every shared shader wastes disk I/O, heap allocations, and cross-thread CPU time during startup and shader precompilation. - What this does: Introduces a lightweight per-batch/per-worker `ShaderLoader` holding a `HashMap` keyed by `(stage_id, hash)` to `Arc<Shader>` and wires it into `names()`, `make()`, and `recipes()`. Shared shaders in a batch are read from disk and looked up once, returning `Arc::clone` on subsequent hits with zero repeated seeks, allocations, `blake3` hashes, or global mutex content Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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 'pr/known-shader-cache:RileyW-pr/known-shader-cache'
git switch 'RileyW-pr/known-shader-cache'

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 'RileyW-pr/known-shader-cache'
git switch 'RileyW-pr/known-shader-cache'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'RileyW-pr/known-shader-cache'
git switch 'RileyW-pr/known-shader-cache'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'RileyW-pr/known-shader-cache'
git switch 'Main'
git merge --squash 'RileyW-pr/known-shader-cache'
git switch 'Main'
git merge --ff-only 'RileyW-pr/known-shader-cache'
git switch 'Main'
git merge 'RileyW-pr/known-shader-cache'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
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
TrustNo1/Darner!647
No description provided.