shader/bvh: reject partial-axis NaN and inverted boxes via ordered min <= max #464

Open
github-bot wants to merge 1 commit from github-bot/gh-464 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 01:16 UTC · original
IlyassRS3:fix/bvh-box-ordered-bounds → main · +69 −8 · 3 files · 1 commits · open

Summary

Tightens the BVH box validity check in both the CPU reference (crates/victory-shader/src/bvh.rs::boxes) and the lowered DXIL slab test (crates/victory-shader/src/emit/intersect.rs::Lower::bvh_boxes) to require ordered per-axis bounds (b[0] <= b[3] && b[1] <= b[4] && b[2] <= b[5], emitted via pred::OLE).

Bug

Previously, box validity only checked !b[0].is_nan(), which allowed two degenerate box classes to register as false hits:

  1. Partial-axis NaN (b[1..6] == NaN with finite b[0]): low.min(high) / low.max(high) (and DXIL FMin / FMax) follow IEEE minNum / maxNum semantics and silently drop a single NaN operand, keeping enter and leave finite so the ray hits the disabled box.
  2. Inverted / empty boxes (min > max on any axis): e.g. standard (+inf, -inf) empty BVH sentinels or BOX16 (+65504, -65504) sentinels. Taking low.min(high) and low.max(high) silently swaps the inverted endpoints back into a solid box (turning (+inf, -inf) into an all-space box every ray hits).

Using three ordered comparisons (pred::OLE in DXIL, <= in Rust) rejects both partial-axis NaN in any of the six coordinates and inverted/empty boxes on any axis, while preserving zero-thickness (min == max) and unbounded (-inf <= +inf) boxes.

Scope & non-overlap

  • Checked open PRs #213, #230, and #414: all three check children[n] != NONE, whereas this PR addresses box bound validity (b[a] <= b[3 + a]). They do not duplicate each other.
  • Also adds a degenerate BOX32/BOX16 node to crates/victory-shader/src/emit/intersect_tests.rs::scene() so the Windows GPU/WARP parity tests cover these cases directly.

Verification

  • AI-assisted: Yes (written and verified with AI assistance).
  • Environment: macOS arm64 (Apple M5), CPU-only, Rust 1.98.1, no Windows GPU / no game files.
  • cargo test --locked -p victory-shader -p xtask: 172 passed, 0 failed (victory-shader, including new bvh::tests::partial_axis_nan_and_inverted_boxes_miss_in_box32_and_box16) + 4 passed, 0 failed (xtask, including repository_passes_policy).
  • cargo clippy --locked -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: 0 warnings (includes emit/intersect_tests.rs).
  • cargo clippy --locked -p victory-shader --all-targets --all-features --target x86_64-pc-windows-msvc -- -D warnings: 0 warnings.
  • Mutation check on unpatched upstream/main (a803c87): running partial_axis_nan_and_inverted_boxes_miss_in_box32_and_box16 against unpatched bvh.rs fails immediately (box32 coord 3 returns hit [8, NONE, NONE, NONE] instead of [NONE; 4]).
**@⁠IlyassRS3** on GitHub · 2026-11-25 01:16 UTC · [original](https://github.com/fxceless/victory-fork/pull/464) `IlyassRS3:fix/bvh-box-ordered-bounds` → `main` · +69 −8 · 3 files · 1 commits · open ### Summary Tightens the BVH box validity check in both the CPU reference (`crates/victory-shader/src/bvh.rs::boxes`) and the lowered DXIL slab test (`crates/victory-shader/src/emit/intersect.rs::Lower::bvh_boxes`) to require ordered per-axis bounds (`b[0] <= b[3] && b[1] <= b[4] && b[2] <= b[5]`, emitted via `pred::OLE`). ### Bug Previously, box validity only checked `!b[0].is_nan()`, which allowed two degenerate box classes to register as false hits: 1. **Partial-axis `NaN` (`b[1..6] == NaN` with finite `b[0]`)**: `low.min(high)` / `low.max(high)` (and DXIL `FMin` / `FMax`) follow IEEE `minNum` / `maxNum` semantics and silently drop a single `NaN` operand, keeping `enter` and `leave` finite so the ray hits the disabled box. 2. **Inverted / empty boxes (`min > max` on any axis)**: e.g. standard `(+inf, -inf)` empty BVH sentinels or BOX16 `(+65504, -65504)` sentinels. Taking `low.min(high)` and `low.max(high)` silently swaps the inverted endpoints back into a solid box (turning `(+inf, -inf)` into an all-space box every ray hits). Using three ordered comparisons (`pred::OLE` in DXIL, `<=` in Rust) rejects both partial-axis `NaN` in any of the six coordinates and inverted/empty boxes on any axis, while preserving zero-thickness (`min == max`) and unbounded (`-inf <= +inf`) boxes. ### Scope & non-overlap - Checked open PRs `#213`, `#230`, and `#414`: all three check `children[n] != NONE`, whereas this PR addresses box bound validity (`b[a] <= b[3 + a]`). They do not duplicate each other. - Also adds a degenerate BOX32/BOX16 node to `crates/victory-shader/src/emit/intersect_tests.rs::scene()` so the Windows GPU/WARP parity tests cover these cases directly. ### Verification - **AI-assisted**: Yes (written and verified with AI assistance). - **Environment**: macOS arm64 (Apple M5), CPU-only, Rust `1.98.1`, no Windows GPU / no game files. - `cargo test --locked -p victory-shader -p xtask`: **172 passed, 0 failed** (`victory-shader`, including new `bvh::tests::partial_axis_nan_and_inverted_boxes_miss_in_box32_and_box16`) + **4 passed, 0 failed** (`xtask`, including `repository_passes_policy`). - `cargo clippy --locked -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: **0 warnings** (includes `emit/intersect_tests.rs`). - `cargo clippy --locked -p victory-shader --all-targets --all-features --target x86_64-pc-windows-msvc -- -D warnings`: **0 warnings**. - **Mutation check on unpatched `upstream/main` (`a803c87`)**: running `partial_axis_nan_and_inverted_boxes_miss_in_box32_and_box16` against unpatched `bvh.rs` fails immediately (`box32 coord 3` returns hit `[8, NONE, NONE, NONE]` instead of `[NONE; 4]`).
Both the CPU reference (bvh::boxes) and the lowered DXIL slab test
(emit::intersect::Lower::bvh_boxes) previously only checked !b[0].is_nan().
Two classes of degenerate BVH boxes slipped through as false hits:

1. Partial-axis NaN (b[1..6] == NaN while b[0] is finite): IEEE minNum/maxNum
   (f32::min/max and DXIL FMin/FMax) drop a single NaN operand, so the
   slab interval stays finite and the box still hits.
2. Inverted/empty boxes (min > max on any axis, such as (+inf, -inf) empty
   sentinels or BOX16 (+65504, -65504) sentinels): low.min(high) and
   low.max(high) silently swap min and max into a solid box.

Replace the single b[0].is_nan() check with three ordered per-axis
comparisons (b[0] <= b[3] && b[1] <= b[4] && b[2] <= b[5], emitted as
pred::OLE in DXIL). Ordered <= rejects every NaN coordinate as well as
every inverted/empty box while preserving zero-thickness (min == max) and
unbounded (-inf <= +inf) boxes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:56 +00:00
Author
Owner

@⁠pr0vId3nce on GitHub · 2026-11-25 16:05 UTC · original

Hardware result: an RDNA2 GPU never enters the empty and inverted boxes this PR rejects, but it does hit boxes with a NaN bound, and boxes entered exactly at the extent

I ran the box test through the real instruction: image_bvh64_intersect_ray and image_bvh_intersect_ray, run natively from raw words on a Ryzen 9 7900X's iGPU (gfx1036, RDNA2 with the ray accelerators, driver 32.0.21042.62) through the AMD driver's own amd_comgr/HIP DLLs. I compared that against this PR's bvh.rs (head 02be6ab2a) over the same cases.

What it gets right. In 1,902 empty-slot cases, this PR's bvh.rs returns the GPU's words in every one. Those cases are six nodes, 16-bit and 32-bit, with +inf/−inf slots beside real children and under ffffffff pointers, each tested with 317 rays. main gets 604 of them. The GPU never enters such a slot, and with this PR neither does bvh.rs. Among the f16 probes, boxes inverted on one axis and the +inf/−inf patterns from #16 match too.

Where it still differs. It matches 389 of 552 f16 box probes. 160 of the other 163 fall into two cases. All rows below are 16-bit nodes, children 8, 10, 18, 20, sorted. Slots 1 to 3 are all NaN (missed by both). The ray runs along x, and the result is the four dwords.

  1. A NaN bound: the GPU hits, this PR misses (34 of the 552 rows). Slot 0, halves as (min x, min y, min z / max x, max y, max z), ray from (−5, 0, 0) along +x, extent 100:

    slot 0 GPU this PR
    7e00 bc00 bc00 / 3c00 3c00 3c00 (min x NaN) 00000008 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
    bc00 bc00 bc00 / 7e00 3c00 3c00 (max x NaN) 00000008 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
    bc00 7e00 bc00 / 3c00 7e00 3c00 (y NaN on both sides) 00000008 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff

    Rule: a NaN bound drops out of the max that forms the entry and the min that forms the exit. The box is missed only when the entry or the exit is still NaN, as with all six bounds NaN, which the GPU missed with all 6 rays.

  2. An entry exactly at the extent: the GPU hits, this PR misses (126 of the 552 rows: 63 halves, each as a min and as a max face). Ray from the origin, extent equal to the entry:

    slot 0 ray GPU this PR
    0001 bc00 bc00 / 7bff 3c00 3c00 +x, extent 33800000 (2^-24) 00000008 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
    fbff bc00 bc00 / 8001 3c00 3c00 −x, extent 33800000 00000008 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
    3c00 bc00 bc00 / 7bff 3c00 3c00 +x, extent 3f800000 (1.0) 00000008 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff

    Rule: a box is hit iff max(entry, 0) ≤ min(exit, extent). This PR keeps enter < ray.extent. With the extent one f32 ulp lower, the GPU misses all 126.

I have both fixes (NaN bounds dropped from the entry and exit, and the extent compare above), with a test per case holding these GPU words, for bvh.rs and the DXIL; happy to send them as a suggestion here or as a PR on top of this one.

Scope: desktop RDNA2, not the console. Synthetic nodes, no game data. I ran this PR's bvh.rs on the CPU, not its DXIL.

AI-assisted (Claude Code); the runs are from my machine.

**@⁠pr0vId3nce** on GitHub · 2026-11-25 16:05 UTC · [original](https://github.com/fxceless/victory-fork/pull/464#issuecomment-7167592157) **Hardware result: an RDNA2 GPU never enters the empty and inverted boxes this PR rejects, but it does hit boxes with a NaN bound, and boxes entered exactly at the extent** I ran the box test through the real instruction: `image_bvh64_intersect_ray` and `image_bvh_intersect_ray`, run natively from raw words on a Ryzen 9 7900X's iGPU (gfx1036, RDNA2 with the ray accelerators, driver 32.0.21042.62) through the AMD driver's own amd_comgr/HIP DLLs. I compared that against this PR's `bvh.rs` (head `02be6ab2a`) over the same cases. **What it gets right.** In 1,902 empty-slot cases, this PR's `bvh.rs` returns the GPU's words in every one. Those cases are six nodes, 16-bit and 32-bit, with +inf/−inf slots beside real children and under `ffffffff` pointers, each tested with 317 rays. `main` gets 604 of them. The GPU never enters such a slot, and with this PR neither does `bvh.rs`. Among the f16 probes, boxes inverted on one axis and the +inf/−inf patterns from #16 match too. **Where it still differs.** It matches 389 of 552 f16 box probes. 160 of the other 163 fall into two cases. All rows below are 16-bit nodes, children `8, 10, 18, 20`, sorted. Slots 1 to 3 are all NaN (missed by both). The ray runs along x, and the result is the four dwords. 1. **A NaN bound: the GPU hits, this PR misses** (34 of the 552 rows). Slot 0, halves as (min x, min y, min z / max x, max y, max z), ray from (−5, 0, 0) along +x, extent 100: | slot 0 | GPU | this PR | |---|---|---| | `7e00 bc00 bc00 / 3c00 3c00 3c00` (min x NaN) | `00000008 ffffffff ffffffff ffffffff` | `ffffffff ffffffff ffffffff ffffffff` | | `bc00 bc00 bc00 / 7e00 3c00 3c00` (max x NaN) | `00000008 ffffffff ffffffff ffffffff` | `ffffffff ffffffff ffffffff ffffffff` | | `bc00 7e00 bc00 / 3c00 7e00 3c00` (y NaN on both sides) | `00000008 ffffffff ffffffff ffffffff` | `ffffffff ffffffff ffffffff ffffffff` | Rule: a NaN bound drops out of the max that forms the entry and the min that forms the exit. The box is missed only when the entry or the exit is still NaN, as with all six bounds NaN, which the GPU missed with all 6 rays. 2. **An entry exactly at the extent: the GPU hits, this PR misses** (126 of the 552 rows: 63 halves, each as a min and as a max face). Ray from the origin, extent equal to the entry: | slot 0 | ray | GPU | this PR | |---|---|---|---| | `0001 bc00 bc00 / 7bff 3c00 3c00` | +x, extent `33800000` (2^-24) | `00000008 ffffffff ffffffff ffffffff` | `ffffffff ffffffff ffffffff ffffffff` | | `fbff bc00 bc00 / 8001 3c00 3c00` | −x, extent `33800000` | `00000008 ffffffff ffffffff ffffffff` | `ffffffff ffffffff ffffffff ffffffff` | | `3c00 bc00 bc00 / 7bff 3c00 3c00` | +x, extent `3f800000` (1.0) | `00000008 ffffffff ffffffff ffffffff` | `ffffffff ffffffff ffffffff ffffffff` | Rule: a box is hit iff max(entry, 0) ≤ min(exit, extent). This PR keeps `enter < ray.extent`. With the extent one f32 ulp lower, the GPU misses all 126. I have both fixes (NaN bounds dropped from the entry and exit, and the extent compare above), with a test per case holding these GPU words, for `bvh.rs` and the DXIL; happy to send them as a suggestion here or as a PR on top of this one. Scope: desktop RDNA2, not the console. Synthetic nodes, no game data. I ran this PR's `bvh.rs` on the CPU, not its DXIL. _AI-assisted (Claude Code); the runs are from my machine._
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/464/head:github-bot/gh-464'
git switch 'github-bot/gh-464'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-464'
git switch 'github-bot/gh-464'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-464'
git switch 'github-bot/gh-464'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-464'
git switch 'main'
git merge --squash 'github-bot/gh-464'
git switch 'main'
git merge --ff-only 'github-bot/gh-464'
git switch 'main'
git merge 'github-bot/gh-464'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.