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

Open
RVZO6 wants to merge 1 commit from upstream-pr/2211 into main
RVZO6 commented 2026-11-25 08:00:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T08:00:10Z.


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)

<!-- greyfinch-sync:Marvisak/VIctory:issue:6599930763 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-25T08:00:10Z. --- 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`](https://github.com/RVZO6/VIctory/actions/runs/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 commented 2026-11-25 08:06:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T08:03:11Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6053837447 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#pullrequestreview-6053837447). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T08:03:11Z. **Review:** COMMENTED --- 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 commented 2026-11-25 08:06:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T08:03:28Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6053840479 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#pullrequestreview-6053840479). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T08:03:28Z. **Review:** APPROVED --- 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 commented 2026-11-25 09:05:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Xvania7 on 2026-11-25T08:52:55Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6054436563 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#pullrequestreview-6054436563). Originally posted by [Xvania7](https://github.com/Xvania7) on 2026-11-25T08:52:55Z. **Review:** APPROVED --- 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 2026-11-25 14:07:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T13:29:50Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163861180 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#issuecomment-7163861180). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T13:29:50Z. --- <!-- victory-triage v1 head=ab5ac96c3e35d4605bd563c2a813f100d0b3b441 verdict=merge-candidate at=2026-11-25T18:43:21Z --> 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](https://claude.ai/code)_
upstream-sync changed title from [PR #2211] GPU: eliminate 60k-pipeline startup allocation storm, double BLAKE3 hash, and USED lock contention to [PR #2211] GPU: hash shaders once in pipeline::shader() and cache SFI0 atomics64 on Translated 2026-11-25 18:01:52 +00:00
RVZO6 commented 2026-11-25 18:02:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T17:43:28Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:comment:7169908967 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#issuecomment-7169908967). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-25T17:43:28Z. --- 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 commented 2026-11-26 03:03:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T02:20:32Z.

Review: COMMENTED


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).
<!-- greyfinch-sync:Marvisak/VIctory:review:6067703513 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#pullrequestreview-6067703513). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T02:20:32Z. **Review:** COMMENTED --- 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 commented 2026-11-26 11:04:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by smaxxey on 2026-11-26T10:53:54Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7189985040 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2211#issuecomment-7189985040). Originally posted by [smaxxey](https://github.com/smaxxey) on 2026-11-26T10:53:54Z. --- 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.
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/2211:upstream-pr/2211'
git switch 'upstream-pr/2211'

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