GPU: key the shader cache by the translator's sources, and keep early pipeline starts off the title's processors #147

Open
github-bot wants to merge 2 commits from github-bot/gh-147 into main AGit
Owner

@⁠iurysf on GitHub · 2026-11-24 21:51 UTC · original
iurysf:shader-cache-key → main · +404 −45 · 6 files · 2 commits · open

What's wrong

Two things make the first seconds after the game opens stutter whenever the pipeline library is large (it is after any run that reached the world) and the shader cache does not hold the current build's translations:

  1. The shader cache key is the BLAKE3 of the executable. Any rebuild for another reason (a change in victory-gpu, victory-hle or the loader, a diag build beside a release one) starts with an empty cache. Every new executable translates its shaders again.
  2. The pipeline library lists thousands of pipelines to "start early" (2,389 here). Each needs its shader's translation and the driver's compile. They are queued on every pipeline worker (half the logical processors, 2 to 8) at once, and the translations took the processors from the title's own threads while it loaded: windows of 183 ms and 113 ms per frame at 15-35 s, 416 "shader translated" lines and ~1,400 pipelines "being made" in the first 40 s.

What the fix does

  • victory-shader/build.rs and SOURCE_HASH: the BLAKE3 of victory-shader's sources (taken from Marvisak/VIctory#254 by wbkepler, with credit in the commit). shader_cache::build() keys entries by that hash and a CACHE_SCHEMA_VERSION of this file's encoding, instead of the executable. It keeps the settings that change a translation (loop limit, group_sync, the VICTORY_WAVE_* and VICTORY_F16_NEAREST switches, shader_opt), now in a small key() that is easy to extend. VICTORY_SHADER_CACHE_KEY=exe keys by the executable again.
  • shader_cache::is_cached: whether an entry exists, without reading or translating.
  • pipeline/make.rs: early work is limited to ceil(workers / 4) workers (VICTORY_EARLY_WORKERS=N; 0 for no limit), and an early pipeline is made only if all its shaders are already translated or in the shader cache; the others stay for their first use. Requested pipelines (a draw wants them) go first and are not limited. A finished early pipeline wakes the others under the queue's lock (no lost wake-up).

How it behaves after

A rebuild that does not touch victory-shader (or shader_cache.rs's schema) keeps its cache. With an empty cache and a large library, the early starts no longer put the translator on every worker.

Measured on an RTX 4070 (Ryzen 7 5800X3D, 32 GB), 40 s each, same bad condition for both: a 295 MiB pipeline library copied from an earlier run and an empty shader cache, VICTORY_RELAY=2, VICTORY_PIPELINE_WAIT_MS=24, snapshot after the prologue, no capture:

0-40 s before after
"shader translated" lines 416 (132 in 15-35 s) 21 (6 in 15-35 s)
pipelines "being made" 980 at 9 s, ~1,400 from 16 s 0 in every window
ms/frame, 5 s windows 30.5, 183, 32.7, 50.7, 112.8, 47.8 64.2 (10 s), 34.3, 31.3, 30.3, 42.7, 32.3
flips (frame waits) 619 808
interval p90 / p99 94.0 / 463.6 ms 34.7 / 174.1 ms
intervals > 200 ms 24 7
intervals > 100 ms 59 46
longest interval 1,512 ms at 11.3 s 1,608 ms at 11.2 s

Builds: "before" is 4e17478 + #63 + the watchdog hooks and worker stacks of #28; "after" is that plus #71 and this PR (#71 changes the relay's copy-back queue, which the start-up screens do not use; the two builds were not otherwise identical). One run each.

The sustained stutter is gone. Isolated holes of 1.1-1.6 s remain, with and without this change: one at 9-11 s (the title's boot-to-first-screen transition, with the GPU idle and nothing of ours in the log) and one more elsewhere between 25 and 36 s. They are not caused by the cache or the early starts; this PR does not claim to fix them.

Tests (cargo test -p victory-gpu -- pipeline::make shader_cache, 12 pass; clippy clean for victory-gpu, victory-shader, victory-pipeline): the key follows the translator, the loop limit and a wave switch and nothing else; SOURCE_HASH equals the digest of victory-shader/src as it is now; an entry is found only for the key that stored it; the early limit (8 workers: 2, 4: 1, 5: 2, 1: 1; the setting wins, 0 is no limit); an early pipeline waits for a free early worker and a requested one does not; an early pipeline with no translation ready is skipped and still queues on its first use.

Overlap and order

Looked for in the upstream open PRs and the fork's: Marvisak/VIctory#254 (the same stable key plus in-flight deduplication and an AOT translate --write-cache: this PR takes only the key) and #1730 (translate --cache DIR, an AOT too). Neither is included: pre-translating the pack offline is the next step and complements this one (with a key that does not follow the executable, one offline run serves every build with the same translator). Fork PRs touching the same lines:

  • #35 (key by effective host wave width) edits cached()/host_shader; the entry is built in cache_input() here, so the merge is one function. An offline pre-translation must run with the same wave width as the host.
  • #98 and #134 add an input to build() (portable heap, view heap limit): they become one more argument of key().
  • #49 raises the pipeline workers to 12: the early limit scales with it (3 of 12).
  • #69 adds a build.rs to victory-shader (Agility SDK test exports): the two scripts have to be merged.
  • #1 (prewarm of pending requests, merged) is unrelated: it makes the wait configurable.

Not covered: the translations are still made at first use when the cache has none; an offline pre-translation (the AOT) and the deduplication of concurrent translations of one shader are left for a follow-up.

AI-assisted.

**@⁠iurysf** on GitHub · 2026-11-24 21:51 UTC · [original](https://github.com/fxceless/victory-fork/pull/147) `iurysf:shader-cache-key` → `main` · +404 −45 · 6 files · 2 commits · open ## What's wrong Two things make the first seconds after the game opens stutter whenever the pipeline library is large (it is after any run that reached the world) and the shader cache does not hold the current build's translations: 1. The shader cache key is the BLAKE3 of the **executable**. Any rebuild for another reason (a change in `victory-gpu`, `victory-hle` or the loader, a `diag` build beside a `release` one) starts with an empty cache. Every new executable translates its shaders again. 2. The pipeline library lists thousands of pipelines to "start early" (2,389 here). Each needs its shader's translation and the driver's compile. They are queued on every pipeline worker (half the logical processors, 2 to 8) at once, and the translations took the processors from the title's own threads while it loaded: windows of 183 ms and 113 ms per frame at 15-35 s, 416 "shader translated" lines and ~1,400 pipelines "being made" in the first 40 s. ## What the fix does - `victory-shader/build.rs` and `SOURCE_HASH`: the BLAKE3 of `victory-shader`'s sources (taken from Marvisak/VIctory#254 by wbkepler, with credit in the commit). `shader_cache::build()` keys entries by that hash and a `CACHE_SCHEMA_VERSION` of this file's encoding, instead of the executable. It keeps the settings that change a translation (loop limit, `group_sync`, the `VICTORY_WAVE_*` and `VICTORY_F16_NEAREST` switches, `shader_opt`), now in a small `key()` that is easy to extend. `VICTORY_SHADER_CACHE_KEY=exe` keys by the executable again. - `shader_cache::is_cached`: whether an entry exists, without reading or translating. - `pipeline/make.rs`: early work is limited to `ceil(workers / 4)` workers (`VICTORY_EARLY_WORKERS=N`; `0` for no limit), and an early pipeline is made only if all its shaders are already translated or in the shader cache; the others stay for their first use. Requested pipelines (a draw wants them) go first and are not limited. A finished early pipeline wakes the others under the queue's lock (no lost wake-up). ## How it behaves after A rebuild that does not touch `victory-shader` (or `shader_cache.rs`'s schema) keeps its cache. With an empty cache and a large library, the early starts no longer put the translator on every worker. Measured on an RTX 4070 (Ryzen 7 5800X3D, 32 GB), 40 s each, same bad condition for both: a 295 MiB pipeline library copied from an earlier run and an **empty** shader cache, `VICTORY_RELAY=2`, `VICTORY_PIPELINE_WAIT_MS=24`, snapshot after the prologue, no capture: | 0-40 s | before | after | |---|---|---| | "shader translated" lines | 416 (132 in 15-35 s) | 21 (6 in 15-35 s) | | pipelines "being made" | 980 at 9 s, ~1,400 from 16 s | 0 in every window | | ms/frame, 5 s windows | 30.5, **183**, 32.7, 50.7, **112.8**, 47.8 | 64.2 (10 s), 34.3, 31.3, 30.3, 42.7, 32.3 | | flips (frame waits) | 619 | 808 | | interval p90 / p99 | 94.0 / 463.6 ms | 34.7 / 174.1 ms | | intervals > 200 ms | 24 | 7 | | intervals > 100 ms | 59 | 46 | | longest interval | 1,512 ms at 11.3 s | 1,608 ms at 11.2 s | Builds: "before" is `4e17478` + #63 + the watchdog hooks and worker stacks of #28; "after" is that plus #71 and this PR (#71 changes the relay's copy-back queue, which the start-up screens do not use; the two builds were not otherwise identical). One run each. The sustained stutter is gone. Isolated holes of 1.1-1.6 s remain, with and without this change: one at 9-11 s (the title's boot-to-first-screen transition, with the GPU idle and nothing of ours in the log) and one more elsewhere between 25 and 36 s. They are not caused by the cache or the early starts; this PR does not claim to fix them. Tests (`cargo test -p victory-gpu -- pipeline::make shader_cache`, 12 pass; clippy clean for `victory-gpu`, `victory-shader`, `victory-pipeline`): the key follows the translator, the loop limit and a wave switch and nothing else; `SOURCE_HASH` equals the digest of `victory-shader/src` as it is now; an entry is found only for the key that stored it; the early limit (8 workers: 2, 4: 1, 5: 2, 1: 1; the setting wins, 0 is no limit); an early pipeline waits for a free early worker and a requested one does not; an early pipeline with no translation ready is skipped and still queues on its first use. ## Overlap and order Looked for in the upstream open PRs and the fork's: Marvisak/VIctory#254 (the same stable key plus in-flight deduplication and an AOT `translate --write-cache`: this PR takes only the key) and #1730 (`translate --cache DIR`, an AOT too). Neither is included: pre-translating the pack offline is the next step and complements this one (with a key that does not follow the executable, one offline run serves every build with the same translator). Fork PRs touching the same lines: - #35 (key by effective host wave width) edits `cached()`/`host_shader`; the entry is built in `cache_input()` here, so the merge is one function. An offline pre-translation must run with the same wave width as the host. - #98 and #134 add an input to `build()` (portable heap, view heap limit): they become one more argument of `key()`. - #49 raises the pipeline workers to 12: the early limit scales with it (3 of 12). - #69 adds a `build.rs` to `victory-shader` (Agility SDK test exports): the two scripts have to be merged. - #1 (prewarm of pending requests, merged) is unrelated: it makes the wait configurable. Not covered: the translations are still made at first use when the cache has none; an offline pre-translation (the AOT) and the deduplication of concurrent translations of one shader are left for a follow-up. AI-assisted.
A rebuild for another reason (a change in the gpu, hle or loader crates, a diag build next to a release one) emptied the whole shader cache, and the first run of every new executable translated each shader again on the title's own processors while the pipeline library's thousands of early starts asked for them (a 253 ms frame at 20-30 s). The key is now victory-shader's source hash (build.rs, from upstream PR Marvisak/VIctory#254 by wbkepler) and a schema version of this file's encoding, with the settings that change a translation as before. VICTORY_SHADER_CACHE_KEY=exe keys by the executable again. Adds is_cached, a lookup of an entry without reading or translating.

Co-authored-by: wbkepler <[email protected]>
Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
The pipeline library lists thousands of pipelines to start early. Each needs its shader's translation and the driver's compile; on every worker at once they took the processors from the title's own threads while it loaded. Early work is limited to ceil(workers/4) workers (VICTORY_EARLY_WORKERS, 0 for no limit) and an early pipeline whose shaders are neither translated nor in the shader cache is left for its first use. Requested pipelines go first and are not limited.

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:36:57 +00:00
Author
Owner

@⁠Revens2 on GitHub · 2026-11-25 05:13 UTC · original

Merging this PR on top of current main (c96b4f2) conflicts in Cargo.lock and crates/victory-gpu/src/shader_cache.rs: main now also puts the view heap limit (probe_view_limit / view_heap_limit) into the cache key. The rebase has to keep that, or a translation cached without the clamp is served again on GPUs whose limit differs. I resolved it in a local integration branch by adding those bytes to the translator part of the key; it builds. Part of a combo with seven other PRs, it ran in the game 3 times: one run reached 900 s (median 4.95 fps), the other two stopped early (one device removed, one title breakpoint), which cannot be attributed to this PR (see #115). Not verified alone.

**@⁠Revens2** on GitHub · 2026-11-25 05:13 UTC · [original](https://github.com/fxceless/victory-fork/pull/147#issuecomment-7153563322) Merging this PR on top of current main (c96b4f2) conflicts in `Cargo.lock` and `crates/victory-gpu/src/shader_cache.rs`: main now also puts the view heap limit (`probe_view_limit` / `view_heap_limit`) into the cache key. The rebase has to keep that, or a translation cached without the clamp is served again on GPUs whose limit differs. I resolved it in a local integration branch by adding those bytes to the translator part of the key; it builds. Part of a combo with seven other PRs, it ran in the game 3 times: one run reached 900 s (median 4.95 fps), the other two stopped early (one device removed, one title breakpoint), which cannot be attributed to this PR (see #115). Not verified alone.
This pull request has changes conflicting with the target branch.
  • Cargo.lock
  • crates/victory-gpu/src/shader_cache.rs
  • crates/victory-shader/build.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/147/head:github-bot/gh-147'
git switch 'github-bot/gh-147'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-147'
git switch 'github-bot/gh-147'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-147'
git switch 'github-bot/gh-147'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-147'
git switch 'main'
git merge --squash 'github-bot/gh-147'
git switch 'main'
git merge --ff-only 'github-bot/gh-147'
git switch 'main'
git merge 'github-bot/gh-147'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.