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

Manually merged
github merged 2 commits from github-bot/gh-341 into gh-pr/341/base 2026-12-02 22:37:31 +00:00 AGit
Owner

@⁠Androtim on GitHub · 2026-12-02 14:05 UTC · original
Androtim:feat/hw-rt-phase1-shader-bvh-opt → main · +989 −220 · 3 files · 2 commits · merged 2026-12-02 22:33 UTC

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.)
**@⁠Androtim** on GitHub · 2026-12-02 14:05 UTC · [original](https://github.com/Androtim/MiniVI/pull/341) `Androtim:feat/hw-rt-phase1-shader-bvh-opt` → `main` · +989 −220 · 3 files · 2 commits · merged 2026-12-02 22:33 UTC 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`.)*
Phase 1 shader optimizations (pure victory-shader, zero RT-off runtime impact):
- Move `bvh_memory` inside both the lane guard and `kind < 6` check so
  inactive lanes and kind 6/7 misses never touch `SLOT_MEMORY` or enter
  the non-uniform descriptor waterfall.
- Fetch bytes 0..63 (16 dwords, align=16) once before branching on box vs
  triangle and select triangle vertices directly in registers from
  `first_all[0..15]`, eliminating dependent `id -> vertex` waterfall loads.
- Evaluate AABBs sequentially (`bvh_test_box`) and test boxes 0..1 before
  loading the second 64 bytes of 32-bit box nodes to cut live VGPRs/PHIs.
- Wrap unguarded single `global_load_*` on `MEMORY` in `begin_guarded`,
  coalesce consecutive same-base/same-guard `global_load_*` runs within
  256 bytes via `try_global_load_cluster` (with single-page check and
  cross-page fallback), use wave-uniform heap handles for `s_load_dword*`
  (`memory_smem`), and replace `URem` with power-of-2 bitmask + select for
  `SLOT_FAULTS` hashing.

Adapted from GPL-3.0 upstream `ReTerryDavis/ReVIctory` commit `925ac0a`
preserving MiniVI's `memory_near`, `VICTORY_MEMORY_NEAR`, and kind 6/7
`0xFFFFFFFF` semantics.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 14:07:22 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 14:33 UTC · original

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** on GitHub · 2026-12-02 14:33 UTC · [original](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7348752715) 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).
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 15:04 UTC · original

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** on GitHub · 2026-12-02 15:04 UTC · [original](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7349654095) 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.
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 15:07 UTC · original · edited 2026-12-02 17:05 UTC

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** on GitHub · 2026-12-02 15:07 UTC · [original](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7349753002) · edited 2026-12-02 17:05 UTC <!-- 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`)._
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 16:16 UTC · original

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** on GitHub · 2026-12-02 16:16 UTC · [original](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7351698477) 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.
github-bot force-pushed github-bot/gh-341 from cdf16541b5 to 3bf8fad12f 2026-12-02 16:33:13 +00:00 Compare
github-bot force-pushed github-bot/gh-341 from 3bf8fad12f to 23901a5bda 2026-12-02 22:05:48 +00:00 Compare
github-bot changed target branch from main to gh-pr/341/base 2026-12-02 22:37:28 +00:00
github manually merged commit 23901a5bda into gh-pr/341/base 2026-12-02 22:37:31 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 22:33 UTC · original

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.

**@⁠Androtim** on GitHub · 2026-12-02 22:33 UTC · [original](https://github.com/Androtim/MiniVI/pull/341#issuecomment-7360844398) 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`.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Androtim--MiniVI!341
No description provided.