Skip to content

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

Open
Androtim wants to merge 1 commit into
mainfrom
feat/hw-rt-phase1-shader-bvh-opt
Open

Androtim wants to merge 1 commit into
mainfrom
feat/hw-rt-phase1-shader-bvh-opt

Conversation

@Androtim

@Androtim Androtim commented Dec 2, 2026

Copy link
Copy Markdown
Owner

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

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