shader(rayquery): lift wave64 bvh64 and deep-cascade/merged bvh32 loops to RayQuery #4170

Open
steven-seagull27 wants to merge 1 commit from upstream-pr/4170 into main
steven-seagull27 commented 2026-11-30 16:58:20 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by steven-seagull27 on 2026-11-30T16:58:20Z.


Summary

Extends hardware DXR 1.1 RayQuery shader lifting (VICTORY_HW_RT=1) across wave64 image_bvh64_intersect_ray shaders and wave-shared image_bvh_intersect_ray (bvh32) shaders with deep branch cascades, mask-PHI joins, inactive-lane preserved registers, and cross-query post-loop merge blocks:

  1. Wave64 image_bvh64_intersect_ray unfolded widening (emit/mod.rs, emit/lower.rs):

    • When hw_rt() && hw_rt_lift() is active on a shader containing image_bvh* instructions, skip the early folded (before wave-wide) attempt in emit_strict so unfolded wave-wide forms (without_skips, SafeSingleWave widen) can lift bvh64 loops to RayQuery rather than interleaving two console lanes per host lane onto the forced-miss software fallback (folded (last resort) remains as fallback if no unfolded form translates).
    • In lower_with_particles and lower_with, clone and reuse the analyzed rayquery::Plan across PHI retries and propagate needs_every_lane / LANE_MASK errors directly without re-lowering under Plan::default(), preventing unlifted ds_wrxchg_rtn_b32 traversal-stack accesses inside divergent loops from polluting NEEDS prior to SafeSingleWave widening.
  2. Memoized pairwise ROBDD disjointness under deep cascades (structure.rs):

    • Add Bools::atom_bool, Bools::disjoint, Bools::lits_disjoint_seen, and Bools::consistent_seen (caching node-pair results in Meets::pairs).
    • Proves pairwise literal contradictions such as (cascade & P) ∧ (cascade & !P) == FALSE in linear ROBDD node-pair steps independent of outer branch-cascade depth, avoiding MAX_MEET budget exhaustion in top-down Bools::meet.
  3. Mask-PHI edge refinement & LiveOutKind::PreserveInit (emit/rayquery.rs):

    • Propagate Structured::phi_masks incoming edge equivalences during backward liveness analysis (compute_live_outside) and increase the per-value ObsState::Disj subsumption cap so infeasible cross-edge paths through mask PHIs are pruned accurately.
    • Add LiveOutKind::PreserveInit (find_inactive_preserved_phi) for wave-shared bvh32 loops carrying an active_mask, preserving pre-loop VGPRs that are never overwritten on !active_mask lanes inside the loop and only read along !active_mask paths after loop exit.
  4. Cross-query merge PHI boundaries for MergedQuery (emit/rayquery.rs):

    • Stop single-query expand_ptr_vals and match_instance_addr expansion at cross-query merge PHIs (arg_from_other_query / addr_arg_from_other_query) so shared post-merge attribute loads are not prematurely claimed as AttrSource::Single.
    • Extend find_merged_attr_loads to group both hit_inst and hit_tri merge PHIs and include surviving queries' pre-branch tlas_lo_vals.

Verification

  • cargo test -p victory-shader --lib -- rayquery
    • wave64_bvh64_prefers_unfolded_wave_wide_when_hw_rt_is_on
    • bvh32_cascade_phi_masks_and_preserve_init_lifts_and_validates
    • bvh32_cross_query_merge_phi_lifts_both_queries_and_merged_attr_load
  • cargo test -p victory-shader --lib -- structure::
    • disjoint_and_consistent_seen_prune_contradictions_under_deep_cascades
    • a_search_that_remembers_answers_as_one_made_anew
  • cargo clippy -p victory-shader --lib --tests
  • cargo run -p xtask -- policy
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5569831887 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4170). Originally posted by [steven-seagull27](https://github.com/steven-seagull27) on 2026-11-30T16:58:20Z. --- ## Summary Extends hardware DXR 1.1 `RayQuery` shader lifting (`VICTORY_HW_RT=1`) across wave64 `image_bvh64_intersect_ray` shaders and wave-shared `image_bvh_intersect_ray` (`bvh32`) shaders with deep branch cascades, mask-PHI joins, inactive-lane preserved registers, and cross-query post-loop merge blocks: 1. **Wave64 `image_bvh64_intersect_ray` unfolded widening (`emit/mod.rs`, `emit/lower.rs`):** - When `hw_rt() && hw_rt_lift()` is active on a shader containing `image_bvh*` instructions, skip the early `folded (before wave-wide)` attempt in `emit_strict` so unfolded wave-wide forms (`without_skips`, `SafeSingleWave` `widen`) can lift `bvh64` loops to `RayQuery` rather than interleaving two console lanes per host lane onto the forced-miss software fallback (`folded (last resort)` remains as fallback if no unfolded form translates). - In `lower_with_particles` and `lower_with`, clone and reuse the analyzed `rayquery::Plan` across PHI retries and propagate `needs_every_lane` / `LANE_MASK` errors directly without re-lowering under `Plan::default()`, preventing unlifted `ds_wrxchg_rtn_b32` traversal-stack accesses inside divergent loops from polluting `NEEDS` prior to `SafeSingleWave` widening. 2. **Memoized pairwise ROBDD disjointness under deep cascades (`structure.rs`):** - Add `Bools::atom_bool`, `Bools::disjoint`, `Bools::lits_disjoint_seen`, and `Bools::consistent_seen` (caching node-pair results in `Meets::pairs`). - Proves pairwise literal contradictions such as `(cascade & P) ∧ (cascade & !P) == FALSE` in linear ROBDD node-pair steps independent of outer branch-cascade depth, avoiding `MAX_MEET` budget exhaustion in top-down `Bools::meet`. 3. **Mask-PHI edge refinement & `LiveOutKind::PreserveInit` (`emit/rayquery.rs`):** - Propagate `Structured::phi_masks` incoming edge equivalences during backward liveness analysis (`compute_live_outside`) and increase the per-value `ObsState::Disj` subsumption cap so infeasible cross-edge paths through mask PHIs are pruned accurately. - Add `LiveOutKind::PreserveInit` (`find_inactive_preserved_phi`) for wave-shared `bvh32` loops carrying an `active_mask`, preserving pre-loop VGPRs that are never overwritten on `!active_mask` lanes inside the loop and only read along `!active_mask` paths after loop exit. 4. **Cross-query merge PHI boundaries for `MergedQuery` (`emit/rayquery.rs`):** - Stop single-query `expand_ptr_vals` and `match_instance_addr` expansion at cross-query merge PHIs (`arg_from_other_query` / `addr_arg_from_other_query`) so shared post-merge attribute loads are not prematurely claimed as `AttrSource::Single`. - Extend `find_merged_attr_loads` to group both `hit_inst` and `hit_tri` merge PHIs and include surviving queries' pre-branch `tlas_lo_vals`. ## Verification - `cargo test -p victory-shader --lib -- rayquery` - `wave64_bvh64_prefers_unfolded_wave_wide_when_hw_rt_is_on` - `bvh32_cascade_phi_masks_and_preserve_init_lifts_and_validates` - `bvh32_cross_query_merge_phi_lifts_both_queries_and_merged_attr_load` - `cargo test -p victory-shader --lib -- structure::` - `disjoint_and_consistent_seen_prune_contradictions_under_deep_cascades` - `a_search_that_remembers_answers_as_one_made_anew` - `cargo clippy -p victory-shader --lib --tests` - `cargo run -p xtask -- policy`
VibeCodeGuy commented 2026-12-01 00:21:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by VibeCodeGuy on 2026-12-01T00:21:45Z.


Offline corpus check (every shader container in a v1.0.2.1 dump: 25,699 unique programs), main e1251c31 against main + this PR at 6923eb78, both with VICTORY_HW_RT=1.

  • Newly failing: 0. Newly passing: 0.
  • DXIL output changed: 20 programs (20 Cs); 20 of them pass the validator.
The 20 changed programs
  • b0f711280e298008 Cs rtdirectionalshadows:CS_RTDistantLODLightShadow
  • e8c962358d862e3f Cs rtdirectionalshadows:CS_RTDistantShadowTraceLeakPlug32_Tiled
  • e9c39e7efe3c508d Cs rtdirectionalshadows:CS_RTShadowTrace32HQ
  • 534b3645dd344ab2 Cs rtdirectionalshadows:CS_RTShadowTrace32HQ_TiledFarOnly
  • fb515ca95d694c18 Cs rtdirectionalshadows:CS_RTShadowTrace32HQ_Tiled
  • 43ce3255529b200a Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ
  • c2d3fdeffa839418 Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP
  • a41f0b553a07e79d Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP_TiledFarOnly
  • ffd9788ba9fec033 Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP_Tiled
  • 22f3f0f74693bff9 Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver4RPP_TiledFarOnly
  • 693d1d787d4a22eb Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver4RPP_Tiled
  • 221925bf9ed4e06e Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver8RPP_TiledFarOnly
  • 7ec122fc9eedf367 Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver8RPP_Tiled
  • 813783478ebb80e6 Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_TiledFarOnly
  • 1250dc0656b3c091 Cs rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_Tiled
  • 07c93bfb77fecebb Cs shadowmask:CS_TraceShadowRays_Full
  • 4e816e1ef600ddcf Cs shadowmask:CS_TraceShadowRays_Fullres
  • 41d9434155880e8a Cs sr_tracing:CS_RaytraceVBuffer_RayBinning_ShaderBinning_Mixed
  • 8e5d070d8d4793ea Cs sr_tracing:CS_RaytraceVBuffer_RayBinning_ShaderBinning_Shared_WithFar
  • 381ef16494cc782d Cs sr_tracing:CS_RaytraceVBuffer_ShaderBinning_WithFar

How: victory translate <dump> --validate --list built for each side (release, cross-built with cargo-xwin, run under Wine), VICTORY_HOST_WAVE=32, plus the four shader-cache-key settings play starts with (VICTORY_EARLY_DEPTH=1, VICTORY_NAN_SCRUB=1, VICTORY_BVH_MISS=1, VICTORY_SAMPLED_MARKS=1). Validator: dxil.dll from DXC v1.9.2609. "Changed" compares translate's emitted hash of each module and its bindings. There was no GPU on this side, so no host pipelines were made and nothing was run on a card. On main the same run has 72 failures: 71 mesh programs over the 32 KiB output limit (the known shortcut in mesh_prologue) and CS_TraceShadowRays_FullAdaptive (#2993).

For scale: on main, VICTORY_HW_RT=1 itself changes 63 compute programs against the default. The 20 above are all among those 63: the PR changes 20 of the 63 that VICTORY_HW_RT=1 touches. The PR's run with the switch off showed no change at all, so the default path is untouched.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7296245800 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4170#issuecomment-7296245800). Originally posted by [VibeCodeGuy](https://github.com/VibeCodeGuy) on 2026-12-01T00:21:45Z. --- **Offline corpus check** (every shader container in a v1.0.2.1 dump: 25,699 unique programs), `main` e1251c31 against `main` + this PR at 6923eb78, both with `VICTORY_HW_RT=1`. - Newly failing: **0**. Newly passing: **0**. - DXIL output changed: **20** programs (20 Cs); 20 of them pass the validator. <details><summary>The 20 changed programs</summary> - `b0f711280e298008` Cs `rtdirectionalshadows:CS_RTDistantLODLightShadow` - `e8c962358d862e3f` Cs `rtdirectionalshadows:CS_RTDistantShadowTraceLeakPlug32_Tiled` - `e9c39e7efe3c508d` Cs `rtdirectionalshadows:CS_RTShadowTrace32HQ` - `534b3645dd344ab2` Cs `rtdirectionalshadows:CS_RTShadowTrace32HQ_TiledFarOnly` - `fb515ca95d694c18` Cs `rtdirectionalshadows:CS_RTShadowTrace32HQ_Tiled` - `43ce3255529b200a` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ` - `c2d3fdeffa839418` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP` - `a41f0b553a07e79d` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP_TiledFarOnly` - `ffd9788ba9fec033` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP_Tiled` - `22f3f0f74693bff9` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver4RPP_TiledFarOnly` - `693d1d787d4a22eb` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver4RPP_Tiled` - `221925bf9ed4e06e` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver8RPP_TiledFarOnly` - `7ec122fc9eedf367` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver8RPP_Tiled` - `813783478ebb80e6` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_TiledFarOnly` - `1250dc0656b3c091` Cs `rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_Tiled` - `07c93bfb77fecebb` Cs `shadowmask:CS_TraceShadowRays_Full` - `4e816e1ef600ddcf` Cs `shadowmask:CS_TraceShadowRays_Fullres` - `41d9434155880e8a` Cs `sr_tracing:CS_RaytraceVBuffer_RayBinning_ShaderBinning_Mixed` - `8e5d070d8d4793ea` Cs `sr_tracing:CS_RaytraceVBuffer_RayBinning_ShaderBinning_Shared_WithFar` - `381ef16494cc782d` Cs `sr_tracing:CS_RaytraceVBuffer_ShaderBinning_WithFar` </details> How: `victory translate <dump> --validate --list` built for each side (release, cross-built with cargo-xwin, run under Wine), `VICTORY_HOST_WAVE=32`, plus the four shader-cache-key settings `play` starts with (`VICTORY_EARLY_DEPTH=1`, `VICTORY_NAN_SCRUB=1`, `VICTORY_BVH_MISS=1`, `VICTORY_SAMPLED_MARKS=1`). Validator: `dxil.dll` from DXC v1.9.2609. "Changed" compares translate's `emitted` hash of each module and its bindings. There was no GPU on this side, so no host pipelines were made and nothing was run on a card. On `main` the same run has 72 failures: 71 mesh programs over the 32 KiB output limit (the known shortcut in `mesh_prologue`) and `CS_TraceShadowRays_FullAdaptive` (#2993). For scale: on `main`, `VICTORY_HW_RT=1` itself changes 63 compute programs against the default. The 20 above are all among those 63: the PR changes 20 of the 63 that `VICTORY_HW_RT=1` touches. The PR's run with the switch off showed no change at all, so the default path is untouched.
paales commented 2026-12-01 01:41:06 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by paales on 2026-12-01T01:41:06Z.


Written by 🤖:

Tested together with #4337, #4578 and #4144 on an RTX 5070 Ti (https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4337#issuecomment-7297773692). With VICTORY_HW_RT=1 the first cutscene and a save with the boat scene lift every ray query: 13 programs, no software fallback, 4.4 lifted dispatches a frame. Card time 24.5 ms a frame against 99.4 ms with VICTORY_BVH_BUILDER=1. Without VICTORY_HW_RT the build runs as before (median frame 33.3 ms both, submitting thread 24.5 against 23.9 ms).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7297777211 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4170#issuecomment-7297777211). Originally posted by [paales](https://github.com/paales) on 2026-12-01T01:41:06Z. --- _Written by 🤖:_ Tested together with #4337, #4578 and #4144 on an RTX 5070 Ti (https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4337#issuecomment-7297773692). With `VICTORY_HW_RT=1` the first cutscene and a save with the boat scene lift every ray query: 13 programs, no software fallback, 4.4 lifted dispatches a frame. Card time 24.5 ms a frame against 99.4 ms with `VICTORY_BVH_BUILDER=1`. Without `VICTORY_HW_RT` the build runs as before (median frame 33.3 ms both, submitting thread 24.5 against 23.9 ms).
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/4170:upstream-pr/4170'
git switch 'upstream-pr/4170'

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/4170'
git switch 'upstream-pr/4170'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/4170'
git switch 'upstream-pr/4170'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/4170'
git switch 'main'
git merge --squash 'upstream-pr/4170'
git switch 'main'
git merge --ff-only 'upstream-pr/4170'
git switch 'main'
git merge 'upstream-pr/4170'
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
trustno1-coder/VIctory-NVIDIA!4170
No description provided.