Skip to content

GPU: hash shaders once in pipeline::shader() and cache SFI0 atomics64 on Translated - #2211

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/pipeline-startup-fast
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/pipeline-startup-fast

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown

During startup pipeline creation (~60,000+ pipelines in the world), pipeline registration, key hashing, and library tracking perform avoidable allocations, double hashing, and lock contention:

  1. pipeline::shader() computes BLAKE3 twice and allocates Arc<Shader> while holding the SHADERS mutex.
  2. GraphicsState::describe() builds intermediate String / Vec<String> allocations on every make::key() and library::name() call.
  3. library::USED acquires a global mutex on every early-pipeline lookup and stores full 32-byte String keys in both USED and CREATED.
  4. dxil_atomics64 re-parses the DXIL container on every host pipeline creation instead of caching the result on Translated.

Claimed on #1633 (https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155570873).

The change

  • crates/victory-gpu/src/pipeline.rs:
    • Compute BLAKE3 once in shader() and allocate Arc<Shader> outside the SHADERS mutex on misses (re-checking under the lock before inserting).
    • Compute dxil_atomics64 once inside with_resources() and cache atomics64: bool on Translated.
    • Add unit tests shader_deduplicates_and_matches_blake3_prefix and dxil_atomics64_reads_sfi0_flags.
  • crates/victory-gpu/src/pipeline/host.rs & crates/victory-gpu/src/pipeline/make.rs:
    • Add zero-allocation streaming GraphicsState::write_description() + Blake3Writer for make::key() and library::name_with(), preserving byte-for-byte identical keys and library names.
    • Add unit test write_description_matches_legacy_describe_and_blake3.
  • crates/victory-gpu/src/pipeline/library.rs:
    • Add name_with() streaming the state description directly into BLAKE3 with identical length-prefixed layout.
    • Move early-pipelines.txt keys into a lock-free OnceLock<KeySet> (EARLY_BEFORE) and store canonical 32-hex keys as compact u128 values (with exact string fallback for non-canonical lines) in both EARLY_BEFORE and CREATED.
    • Add unit tests parse_key_u128_accepts_only_canonical_32_hex, created_deduplicates_hex_and_non_hex_keys, and early_before_lock_free_lookup_handles_hex_and_non_hex.

Not a duplicate of

  • #2192 (#1509) replaces make.rs's FIFO queue with a shader/PSO dependency DAG; it does not remove shader() double-hashing, stream GraphicsState descriptions, move early-pipelines.txt to lock-free OnceLock<KeySet>, or cache atomics64.
  • #2179 decouples library::pipeline() loads from save() via a second library handle, and #2180 skips rewriting host-pipelines.bin when only early-pipelines.txt changed; both are orthogonal to key hashing and EARLY_BEFORE/CREATED storage.

Tested

  • Fork windows-latest MSVC + Windows WARP SM 6.6 CI run 45570823602 (Clippy clean, Clippy, diagnostics build clean, Tests: 731 passed; 0 failed; 10 ignored, victory-gpu: 153 passed; 0 failed; 2 ignored + 1 passed, xtask: 6 passed; 0 failed):
    • pipeline::tests::shader_deduplicates_and_matches_blake3_prefix ... ok
    • pipeline::tests::dxil_atomics64_reads_sfi0_flags ... ok
    • pipeline::host::tests::write_description_matches_legacy_describe_and_blake3 ... ok
    • pipeline::library::tests::parse_key_u128_accepts_only_canonical_32_hex ... ok
    • pipeline::library::tests::created_deduplicates_hex_and_non_hex_keys ... ok
    • pipeline::library::tests::early_before_lock_free_lookup_handles_hex_and_non_hex ... ok

Not run: the title; any hardware GPU. The title was not used in these tests.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

@catminou4 catminou4 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.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

60k-pipeline startup allocation storm eliminated, double-buffered... — startup perf; pairs with #2192's async scheduling and #2211's own scope. Big win if it holds.

@DenRakEiw DenRakEiw 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 locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib pipeline:: — 21 passed, 0 failed on RTX 5090 (including shader_deduplicates_and_matches_blake3_prefix)
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean elimination of the startup pipeline allocation storm, double BLAKE3 hashing, and USED lock contention. Looks great.

AI-assisted (Gemini 4 Neon).

@Xvania7 Xvania7 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 locally merged onto main (940d32f, Windows 10 x64, NVIDIA GeForce RTX 5060, Rust MSVC):

  • cargo test -p victory-gpu --lib -- --test-threads=1 pipeline:: — 21 passed, 0 failed
  • cargo clippy -p victory-gpu --all-targets -- -D warnings — 0 warnings
  • cargo xtask policy — ok (182 files ok)

Synthetic tests only; no game data.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. The PR is now small. pipeline::shader() in crates/victory-gpu/src/pipeline.rs hashes the container once and passes the hash to Shader::with_hash. On a miss it builds the Arc<Shader> outside the SHADERS lock and uses or_insert when it takes the lock again, so the first entry inserted is kept. with_resources computes dxil_atomics64 once and stores it in Translated::atomics64. HostPipeline creation in pipeline/host.rs reads that flag and no longer parses the DXIL container for each pipeline. The flag test (0x1040_0000 in SFI0) is unchanged. Translated is only built in with_resources, so the new field cannot be left unset. The gain is small and not measured, but the change carries no risk.

On your reply: thanks for splitting it. Dropping the DebugStrContent escaper and the KeySet rewrite resolves the earlier concern about library and early-list keys.

CI: check passing.


Generated by Claude Code

Bebrik229 pushed a commit to Bebrik229/VIctory that referenced this pull request Nov 25, 2026
… on Translated

Trimmed per triage feedback (removed the GraphicsState escaper and
library/early-list key rewrite so only the zero-risk savings remain):
- Reuse the single blake3::hash(bytes) in pipeline::shader() instead of
  re-hashing inside Shader::new, and allocate Arc<Shader> outside SHADERS.
- Compute dxil_atomics64(&host.dxil) once inside with_resources() and
  cache atomics64: bool on Translated instead of re-parsing the DXIL
  container on every HostPipeline creation.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
@RVZO6
RVZO6 force-pushed the rvzo/pipeline-startup-fast branch from 4ccd2e3 to ab5ac96 Compare November 25, 2026 17:43
@RVZO6 RVZO6 changed the title GPU: eliminate 60k-pipeline startup allocation storm, double BLAKE3 hash, and USED lock contention GPU: hash shaders once in pipeline::shader() and cache SFI0 atomics64 on Translated Nov 25, 2026
@RVZO6

RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Rebased cleanly onto 13f91ca and split per triage feedback:

  • Dropped the GraphicsState DebugStrContent escaper and KeySet / EARLY_BEFORE rewrite (~740 lines removed).
  • Kept only the two zero-risk wins (2 files changed, +73/-19):
    1. Reuse the single blake3::hash(bytes) in pipeline::shader() instead of re-hashing inside Shader::new, and allocate Arc<Shader> outside the SHADERS lock.
    2. Compute dxil_atomics64(&host.dxil) once inside with_resources() and cache atomics64: bool on Translated instead of re-parsing the DXIL container on every HostPipeline creation.
  • Verified on windows-latest MSVC + WARP SM 6.6 CI (both clippy -D warnings profiles, xtask 6/6, victory-gpu + victory-shader all green): https://github.com/RVZO6/VIctory/actions/runs/45666985023

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

Tested merged against current main (63bcca43) on Windows 11. Although git merge-tree reports zero text conflicts and cargo check -p victory-gpu --lib compiles, merging into current main breaks test compilation (error[E0063]) under both default and --features diagnostics:

  • Adding pub(crate) atomics64: bool to pipeline::Translated breaks 3 newer Translated { ... } initializers added on main since 13f91caee:
    1. crates/victory-gpu/src/draw.rs:3408 (default & diagnostics)
    2. crates/victory-gpu/src/pipeline/depth_bounds_test.rs:58 (default & diagnostics)
    3. crates/victory-gpu/src/pipeline/placed_uav_tests.rs:82 (--features diagnostics)
  • Needs a rebase onto main updating those three test initializers (note: #2974 also introduces atomics64 on Translated with those three call sites updated).

@smaxxey

smaxxey commented Nov 26, 2026

Copy link
Copy Markdown

Heads-up before this lands: merged onto current main (63bcca4), the library still builds but the tests no longer compile. Git merges the branch without conflicts. The triage's point that Translated is only built in with_resources held when it was written, but two test initializers reached main since then and don't set the new field:

error[E0063]: missing field `atomics64` in initializer of `pipeline::Translated`
    --> crates\victory-gpu\src\draw.rs:3408:13
error[E0063]: missing field `atomics64` in initializer of `pipeline::Translated`
  --> crates\victory-gpu\src\pipeline\depth_bounds_test.rs:58:43

They come from e976344 (#2155) and 842b0cb (#158). On the merge, cargo check -p victory-gpu --lib passes and cargo check -p victory-gpu --lib --tests fails, so CI would turn red after merging. At this branch's own head (ab5ac96) both pass. Setting atomics64 in those two initializers after a rebase should be all it needs.

Found by merging locally (Windows 11, Rust 1.98.1, no game files). Tested with Claude Code's help.

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…shader() and cache SFI0 atomics64 on Translated
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
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.

7 participants