Skip to content

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

Open
iurysf wants to merge 2 commits into
fxceless:mainfrom
iurysf:shader-cache-key
Open

iurysf wants to merge 2 commits into
fxceless:mainfrom
iurysf:shader-cache-key

Conversation

@iurysf

@iurysf iurysf commented Nov 24, 2026

Copy link
Copy Markdown

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 Pipeline & CLI: stable shader cache key, in-flight dedup, and AOT warm-up 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:

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 and others added 2 commits November 24, 2026 18:46
…xecutable

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 <211408787+wbkepler@users.noreply.github.com>
Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
…aders already translated

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant