perf(shader): coalesce BVH node fetches and global_load clusters (#339) #341

Merged
Androtim merged 2 commits from upstream-pr/341 into main 2026-12-02 22:33:05 +00:00
Androtim commented 2026-12-02 14:05:11 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T14:05:11Z.


Part of #339 (Phase 1 — Software BVH & Shader Coalescing).

Summary

Pure victory-shader optimizations adapted cleanly from GPL-3.0 upstream ReTerryDavis/ReVIctory commit 925ac0a while preserving MiniVI's memory_near, VICTORY_MEMORY_NEAR, and kind 6/7 -1 semantics (zero RT-off runtime impact):

  • Coalesced 64-byte BVH fetch & register-extracted triangles (emit/intersect.rs):
    • Moves bvh_memory inside both begin_guarded(guard) and kind < 6 so inactive lanes and kind 6/7 misses never touch SLOT_MEMORY / SLOT_FAULTS or enter the non-uniform descriptor waterfall.
    • Fetches the shared 64-byte cache line (16 dwords, align = 16) once before diverging on box vs triangle (is_box).
    • Extracts triangle vertices (0..=4) in registers from first_all[0..15] using first_all[15], removing 4 dependent rawBufferLoad + waterfall sequences per triangle test.
    • Evaluates AABBs one at a time via bvh_test_box, consuming boxes 0..=1 before loading the second 64 bytes (bvh_second_words) to cut VGPR and PHI pressure.
  • Guarded/coalesced global_load_* clusters & memory lookup optimizations (emit/lower.rs):
    • Adds try_global_load_cluster to coalesce 2+ consecutive same-base, same-guard global_load_* instructions within 256 bytes into a single directory lookup and contiguous dwordx4 rawBufferLoads (raw_load_words) with an exact 64 KiB page-boundary fallback.
    • Guards single unshared global_load_* (!memory_shares()) with begin_guarded(guard).
    • Uses wave-uniform heap handles (nonuniform = false) for scalar s_load_dword* (memory_smem).
    • Replaces URem with a bitmask + select for power-of-2 SLOT_FAULTS hashing (MEMORY_FAULTS = 4096).
  • Tests (emit/lower_tests.rs):
    • Added global_load_cluster_coalesces_contiguous_loads_and_handles_page_cross (inspects coalesced 3-dword mask=7 rawBufferLoad, validates via dxil.dll, and verifies cross-page + same-page + inactive-lane results on hardware & WARP).
    • Added smem_memory_load_uses_uniform_handle_and_validates (verifies nonuniform_handles == 0 and dxil.dll validation).

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy --staged passed.
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets -- -D warnings and --all-features passed.
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-shader --lib -- --test-threads=1: all bvh::tests (16/16), emit::intersect_tests (11/11), and emit::lower::tests (24/24, 1 ignored) passed (dxil.dll + hardware RTX 4060 Ti + WARP). (Note: 3 unrelated pre-existing VS/GS/PS PSV0 tests fail locally only against Windows SDK 10.0.22000.0 dxil.dll; CI runs windows-latest.)
<!-- greyfinch-sync:Androtim/MiniVI:issue:5608049584 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/341). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T14:05:11Z. --- Part of #339 (Phase 1 — Software BVH & Shader Coalescing). ### Summary Pure `victory-shader` optimizations adapted cleanly from GPL-3.0 upstream `ReTerryDavis/ReVIctory` commit `925ac0a` while preserving MiniVI's `memory_near`, `VICTORY_MEMORY_NEAR`, and kind 6/7 `-1` semantics (zero RT-off runtime impact): - **Coalesced 64-byte BVH fetch & register-extracted triangles (`emit/intersect.rs`):** - Moves `bvh_memory` inside both `begin_guarded(guard)` and `kind < 6` so inactive lanes and kind 6/7 misses never touch `SLOT_MEMORY` / `SLOT_FAULTS` or enter the non-uniform descriptor waterfall. - Fetches the shared 64-byte cache line (`16` dwords, `align = 16`) once before diverging on box vs triangle (`is_box`). - Extracts triangle vertices (`0..=4`) in registers from `first_all[0..15]` using `first_all[15]`, removing 4 dependent `rawBufferLoad` + waterfall sequences per triangle test. - Evaluates AABBs one at a time via `bvh_test_box`, consuming boxes `0..=1` before loading the second 64 bytes (`bvh_second_words`) to cut VGPR and PHI pressure. - **Guarded/coalesced `global_load_*` clusters & memory lookup optimizations (`emit/lower.rs`):** - Adds `try_global_load_cluster` to coalesce 2+ consecutive same-base, same-guard `global_load_*` instructions within 256 bytes into a single directory lookup and contiguous `dwordx4` `rawBufferLoad`s (`raw_load_words`) with an exact 64 KiB page-boundary fallback. - Guards single unshared `global_load_*` (`!memory_shares()`) with `begin_guarded(guard)`. - Uses wave-uniform heap handles (`nonuniform = false`) for scalar `s_load_dword*` (`memory_smem`). - Replaces `URem` with a bitmask + `select` for power-of-2 `SLOT_FAULTS` hashing (`MEMORY_FAULTS = 4096`). - **Tests (`emit/lower_tests.rs`):** - Added `global_load_cluster_coalesces_contiguous_loads_and_handles_page_cross` (inspects coalesced 3-dword `mask=7` `rawBufferLoad`, validates via `dxil.dll`, and verifies cross-page + same-page + inactive-lane results on hardware & WARP). - Added `smem_memory_load_uses_uniform_handle_and_validates` (verifies `nonuniform_handles == 0` and `dxil.dll` validation). ### Verification - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy --staged` passed. - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets -- -D warnings` and `--all-features` passed. - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-shader --lib -- --test-threads=1`: all `bvh::tests` (16/16), `emit::intersect_tests` (11/11), and `emit::lower::tests` (24/24, 1 ignored) passed (`dxil.dll` + hardware RTX 4060 Ti + WARP). *(Note: 3 unrelated pre-existing VS/GS/PS PSV0 tests fail locally only against Windows SDK `10.0.22000.0` `dxil.dll`; CI runs `windows-latest`.)*
Androtim commented 2026-12-02 14:33:36 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T14:33:36Z.


Architect Triage (#339 Phase 1 — CI 46898570093 GREEN, clean-room & GPL-3.0 attribution verified):

  • Clean victory-shader diff (emit/intersect.rs 64-byte coalesced BVH fetch + register triangle extraction; emit/lower.rs try_global_load_cluster, uniform memory_smem handles, bitmask SLOT_FAULTS hashing; strong unit + hardware/WARP tests).
  • Because the emit/lower.rs changes (global_load_* clustering/guarding, memory_smem, SLOT_FAULTS hash) also run on RT-off compute/graphics shaders, this needs a paired plain---release (no --features diagnostics) RT-off A/B vs main (baf967fc+) confirming zero FPS/memory regression and 0 crashes/DRED, plus --features diagnostics visual PNGs before merge (per Directive #21).
<!-- greyfinch-sync:Androtim/MiniVI:comment:7348752715 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7348752715). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T14:33:36Z. --- Architect Triage (`#339` Phase 1 — CI `46898570093` **GREEN**, clean-room & GPL-3.0 attribution verified): - Clean `victory-shader` diff (`emit/intersect.rs` 64-byte coalesced BVH fetch + register triangle extraction; `emit/lower.rs` `try_global_load_cluster`, uniform `memory_smem` handles, bitmask `SLOT_FAULTS` hashing; strong unit + hardware/WARP tests). - Because the `emit/lower.rs` changes (`global_load_*` clustering/guarding, `memory_smem`, `SLOT_FAULTS` hash) also run on **RT-off** compute/graphics shaders, this needs a paired plain-`--release` (no `--features diagnostics`) **RT-off** A/B vs `main` (`baf967fc`+) confirming zero FPS/memory regression and `0` crashes/DRED, plus `--features diagnostics` visual PNGs before merge (per Directive #21).
Androtim commented 2026-12-02 15:04:03 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T15:04:03Z.


Holding merge (triage: needs work) per deep static audit routing (#282 comment 7349410307, P0/P1 findings #13, #14, #16, #17 routed to #339). Please resolve the flagged findings, re-verify CI, and run a paired plain---release RT-off A/B + --features diagnostics PNG check before merge.

<!-- greyfinch-sync:Androtim/MiniVI:comment:7349654095 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7349654095). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T15:04:03Z. --- Holding merge (`triage: needs work`) per deep static audit routing (`#282` comment `7349410307`, P0/P1 findings #13, #14, #16, #17 routed to `#339`). Please resolve the flagged findings, re-verify CI, and run a paired plain-`--release` RT-off A/B + `--features diagnostics` PNG check before merge.
Androtim commented 2026-12-02 15:07:08 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T15:07:08Z.


Automated triage for the maintainers (not an approval).

Verdict: needs-rebase · CI (check): passing

This PR no longer merges cleanly into main.

To change the verdict: Rebase onto current main and resolve conflicts; CI and triage will run again automatically.


Generated with Antigravity CLI (agy).

<!-- greyfinch-sync:Androtim/MiniVI:comment:7349753002 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7349753002). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T15:07:08Z. --- <!-- minivi-triage v1 head=3bf8fad12f430e07c546deed74457633bd4b3144 verdict=needs-rebase ci=passing at=2026-12-02T17:05:43Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-rebase` · **CI (`check`):** `passing` This PR no longer merges cleanly into `main`. **To change the verdict:** Rebase onto current `main` and resolve conflicts; CI and triage will run again automatically. --- _Generated with Antigravity CLI (`agy`)._
Androtim commented 2026-12-02 16:16:19 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T16:16:19Z.


Rebased onto \main\ (\3efe907, resolving the \lower_tests.rs\ conflict) and resolved #282\ finding #16: added \uniform_scalars\ analysis in \crates/victory-shader/src/emit/lower.rs\ (using \ll_lane_blocks_native, shared \post_dominators/\ranch_region, RPO forward PHI convergence, and excluding \waterfall.reads, \waterfall.loads, \ _readfirstlane_b32, divergent \Atom::Uniform\ branches, and loop-carried/divergent PHIs) so \memory_smem\ only uses \NonUniformIndex = false\ when all incoming scalar address words and \soffset\ are proven wave-uniform across active lanes, plus added \smem_memory_load_divergent_sgpr_keeps_nonuniform_handle\ covering both \ _readfirstlane_b32\ and divergent-PHI branches. Verified \emit::lower::tests\ (\27 passed; 0 failed; 1 ignored), \emit::intersect_tests\ (\11 passed; 0 failed), workspace \clippy --all-targets\ (normal & --all-features), and \xtask policy --staged. Ready for CI and maintainer GPU A/B check.

<!-- greyfinch-sync:Androtim/MiniVI:comment:7351698477 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7351698477). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T16:16:19Z. --- Rebased onto \main\ (\3efe907\, resolving the \lower_tests.rs\ conflict) and resolved \#282\ finding #16: added \uniform_scalars\ analysis in \crates/victory-shader/src/emit/lower.rs\ (using \ll_lane_blocks_native\, shared \post_dominators\/\ranch_region\, RPO forward PHI convergence, and excluding \waterfall.reads\, \waterfall.loads\, \ _readfirstlane_b32\, divergent \Atom::Uniform\ branches, and loop-carried/divergent PHIs) so \memory_smem\ only uses \NonUniformIndex = false\ when all incoming scalar address words and \soffset\ are proven wave-uniform across active lanes, plus added \smem_memory_load_divergent_sgpr_keeps_nonuniform_handle\ covering both \ _readfirstlane_b32\ and divergent-PHI branches. Verified \emit::lower::tests\ (\27 passed; 0 failed; 1 ignored\), \emit::intersect_tests\ (\11 passed; 0 failed\), workspace \clippy --all-targets\ (normal & \--all-features\), and \xtask policy --staged\. Ready for CI and maintainer GPU A/B check.
Androtim commented 2026-12-02 22:33:02 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T22:33:02Z.


Rebased cleanly onto main (23901a5b), Deep Static Audit Pass #2 & Pass #4 verified clean, CI GREEN. Merging #339 Phase 1 coalesced BVH node fetches & global_load clusters to main.

<!-- greyfinch-sync:Androtim/MiniVI:comment:7360844398 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7360844398). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T22:33:02Z. --- Rebased cleanly onto `main` (`23901a5b`), Deep Static Audit Pass #2 & Pass #4 verified clean, CI **GREEN**. Merging `#339` Phase 1 coalesced BVH node fetches & `global_load` clusters to `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
Androtim/MiniVI!341
No description provided.