D3D12.X: handle empty BVH in Bvh::bounds() and NaN rays in intersect_node() #1322

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

@⁠b3IKK on GitHub · 2026-11-24 14:45 UTC · original
b3IKK:gpu/bvh-empty-bounds-and-nan → main · +28 −1 · 1 files · 1 commits · open

Bvh::bounds() indexed node_at(self.root) unconditionally, panicking out-of-bounds on the empty BVH that bvh::build(&[], 0) or bvh::build_top(&[]) returns (its root is INVALID, 0xFFFF_FFFF).

In intersect_node(), Rust's f32::min / f32::max follow IEEE minNum / maxNum and ignore NaN operands, so a ray with NaN in its origin or inverse direction kept a = -inf, b = +inf and reported a hit on every valid child box.

This returns the empty AABB ([+inf; 3], [-inf; 3]) when self.root == INVALID and treats a NaN slab interval as a miss.

What is in it

  • crates/xcl/src/gpu/bvh.rs: the root == INVALID guard in Bvh::bounds() and the NaN rejection in intersect_node(); unit tests empty_bvh_bounds_does_not_panic and nan_ray_misses_all_boxes.

Since the last push: rebased onto main 06c9f55. The only conflict was in the test module, where main had added triangle_node_types_0_to_3_select_expected_vertices at the same place; both tests are kept, and the branch's own test gained the #[test] attribute that the shared line had carried.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 1267 passed, 0 failed, 46 ignored (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, no heap windows, and cannot disable the driver cache (Windows Developer Mode is required))
  • cargo test -p xcl --lib -- gpu::bvh: 10 passed, 0 failed
  • both tests with the guard and the NaN check taken out: 0 passed, 2 failed — empty_bvh_bounds_does_not_panic panics at bvh.rs:264 (index out of bounds) and nan_ray_misses_all_boxes fails its assertion

Regression run

main (426a5a7, clean tree, no local changes):

xcl-smoke v1
commit: 426a5a745db4d5e4b037449ede0b70cb342f5192
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15158 MB

PR (71c978c, clean tree, no local changes):

xcl-smoke v1
commit: 71c978c935ff0ba0358b0f5e56b7942f59f3e5bd
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1640 (PresentX 1641)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15409 MB

(The main block is 426a5a7, an ancestor of the current main 06c9f55, from the same machine and data folder.)

Not tested

  • a title that hands XCL an empty BVH or a NaN ray in its own structures (the tests build both directly)
  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Claude Fable 5.2: rebased onto 06c9f55, kept both tests at the conflict, re-verified that each test fails without its fix, ran the suite and the scripts/smoke.ps1 pair.
**@⁠b3IKK** on GitHub · 2026-11-24 14:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322) `b3IKK:gpu/bvh-empty-bounds-and-nan` → `main` · +28 −1 · 1 files · 1 commits · open `Bvh::bounds()` indexed `node_at(self.root)` unconditionally, panicking out-of-bounds on the empty BVH that `bvh::build(&[], 0)` or `bvh::build_top(&[])` returns (its `root` is `INVALID`, `0xFFFF_FFFF`). In `intersect_node()`, Rust's `f32::min` / `f32::max` follow IEEE `minNum` / `maxNum` and ignore `NaN` operands, so a ray with `NaN` in its origin or inverse direction kept `a = -inf`, `b = +inf` and reported a hit on every valid child box. This returns the empty AABB `([+inf; 3], [-inf; 3])` when `self.root == INVALID` and treats a `NaN` slab interval as a miss. **What is in it** - `crates/xcl/src/gpu/bvh.rs`: the `root == INVALID` guard in `Bvh::bounds()` and the `NaN` rejection in `intersect_node()`; unit tests `empty_bvh_bounds_does_not_panic` and `nan_ray_misses_all_boxes`. **Since the last push**: rebased onto `main` `06c9f55`. The only conflict was in the test module, where `main` had added `triangle_node_types_0_to_3_select_expected_vertices` at the same place; both tests are kept, and the branch's own test gained the `#[test]` attribute that the shared line had carried. **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): **1267 passed, 0 failed, 46 ignored** (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, `no heap windows`, and `cannot disable the driver cache (Windows Developer Mode is required)`) - `cargo test -p xcl --lib -- gpu::bvh`: 10 passed, 0 failed - both tests with the guard and the `NaN` check taken out: 0 passed, 2 failed — `empty_bvh_bounds_does_not_panic` panics at `bvh.rs:264` (index out of bounds) and `nan_ray_misses_all_boxes` fails its assertion **Regression run** main (`426a5a7`, clean tree, no local changes): ``` xcl-smoke v1 commit: 426a5a745db4d5e4b037449ede0b70cb342f5192 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1642 (PresentX 1642) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15158 MB ``` PR (`71c978c`, clean tree, no local changes): ``` xcl-smoke v1 commit: 71c978c935ff0ba0358b0f5e56b7942f59f3e5bd gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1640 (PresentX 1641) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15409 MB ``` (The main block is `426a5a7`, an ancestor of the current `main` `06c9f55`, from the same machine and data folder.) **Not tested** - a title that hands XCL an empty BVH or a NaN ray in its own structures (the tests build both directly) - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Claude Fable 5.2: rebased onto `06c9f55`, kept both tests at the conflict, re-verified that each test fails without its fix, ran the suite and the `scripts/smoke.ps1` pair.
Bvh::bounds() indexed node_at(self.root) unconditionally, panicking
out-of-bounds on empty BVHs returned by bvh::build(&[], 0) or
bvh::build_top(&[]) whose root is INVALID (0xFFFF_FFFF).

In intersect_node(), Rust's f32::min / f32::max ignore NaN operands, so
a ray with NaN in origin or inverse direction kept a = -inf, b = +inf
and reported a hit on every valid child AABB.

Return the empty AABB ([+inf; 3], [-inf; 3]) when self.root == INVALID,
and treat any NaN slab interval as a miss in intersect_node().

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:50:44 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 14:53 UTC · original · edited 2026-11-24 14:55 UTC

Full-suite run, since this PR has no test report yet. Tested exact head 0e91ac0 (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: empty_bvh_bounds_does_not_panic, nan_ray_misses_all_boxes.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 14:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7135112483) · edited 2026-11-24 14:55 UTC Full-suite run, since this PR has no test report yet. Tested exact head `0e91ac0` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **753 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `empty_bvh_bounds_does_not_panic`, `nan_ray_misses_all_boxes`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **753 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 15:24 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: 709-710 passed, 0 failed, 21 ignored.

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 15:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7135965799) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: 709-710 passed, 0 failed, 21 ignored. Thanks!
Author
Owner

@⁠Melow-code on GitHub · 2026-11-24 23:02 UTC · original

The fix is correct. We wrote the same empty-bounds guard in our fork (fxceless/victory-gta6 issue #16): Bvh::bounds() on root == INVALID panics because node_at indexes out of range.

Two notes:

  1. The NaN slab fix is sound. Rust f32::min/f32::max propagate NaN on one side only (they return the other operand), so without this guard a NaN ray hits every child. The is_nan() check is the simplest correct fix.

  2. The empty AABB sentinel ([+inf; 3], [-inf; 3]) matches the D3D12 spec for D3D12_RAYTRACING_AABB with no geometry. Any intersection test against it misses, which is correct.

Tests look good. Both fail before the fix and pass after, confirmed on three different GPUs.

**@⁠Melow-code** on GitHub · 2026-11-24 23:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7146939767) The fix is correct. We wrote the same empty-bounds guard in our fork (fxceless/victory-gta6 issue #16): `Bvh::bounds()` on `root == INVALID` panics because `node_at` indexes out of range. Two notes: 1. The NaN slab fix is sound. Rust `f32::min`/`f32::max` propagate NaN on one side only (they return the other operand), so without this guard a NaN ray hits every child. The `is_nan()` check is the simplest correct fix. 2. The empty AABB sentinel `([+inf; 3], [-inf; 3])` matches the D3D12 spec for `D3D12_RAYTRACING_AABB` with no geometry. Any intersection test against it misses, which is correct. Tests look good. Both fail before the fix and pass after, confirmed on three different GPUs.
Author
Owner

@⁠throny on GitHub · 2026-11-24 23:07 UTC · original

Two contributors checked independently and found that #1633 solves the same problem in a different way: #1607, group D69. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

**@⁠throny** on GitHub · 2026-11-24 23:07 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7147034933) <!-- xcl-1607-hint:D69 --> Two contributors checked independently and found that #1633 solves the same problem in a different way: #1607, group D69. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint from the community triage in #1607, not a maintainer decision. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
Author
Owner

@⁠b3IKK on GitHub · 2026-11-25 15:43 UTC · original

Rebased onto main 06c9f55; the only conflict was a test main had added at the same place, and both are kept. Head 71c978c: 1267 passed, 0 failed, 46 ignored. Both tests fail with the guard and the NaN check taken out (an index panic at bvh.rs:264 and the assertion), and the smoke pair in the description is 426a5a7 / this head. AI use: OpenCode, Claude Fable 5.2.

**@⁠b3IKK** on GitHub · 2026-11-25 15:43 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7167274951) Rebased onto `main` `06c9f55`; the only conflict was a test `main` had added at the same place, and both are kept. Head `71c978c`: 1267 passed, 0 failed, 46 ignored. Both tests fail with the guard and the NaN check taken out (an index panic at `bvh.rs:264` and the assertion), and the smoke pair in the description is `426a5a7` / this head. AI use: OpenCode, Claude Fable 5.2.
Author
Owner

@⁠thehollowedone on GitHub · 2026-11-25 15:55 UTC · original

Independent Windows integration check of 71c978c on 0829441. The build and all ten stock CPU BVH tests passed. The workspace reported 1,274 passed, 0 failed and 46 ignored, including 26 runtime skips. The stock GPU BVH comparison passed too.

In a separate synthetic box32 comparison related to #1559, CPU, HLSL and Direct DXIL agreed on the finite hit, finite miss and NaN origin on a parallel axis. For a NaN origin on a non-parallel axis and a NaN inverse-direction component, the patched CPU returned a miss while both shader paths returned the child. The GPU paths were unchanged in this check.

RTX 3080 Ti (621.35), Windows 11 build 26300, Rust 1.99.1 MSVC and DXC 1.8.2502.11. Synthetic checks only. AI-assisted. Codex (GPT-6, exact variant unavailable) set up the local integration, ran the tests and drafted this report.

**@⁠thehollowedone** on GitHub · 2026-11-25 15:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7167440862) Independent Windows integration check of [71c978c](https://github.com/xcl-dev/xcl/commit/71c978c935ff0ba0358b0f5e56b7942f59f3e5bd) on [0829441](https://github.com/xcl-dev/xcl/commit/082944135cd2b96288470a595861fa608eee08dc). The build and all ten stock CPU BVH tests passed. The workspace reported **1,274 passed, 0 failed and 46 ignored**, including 26 runtime skips. The stock GPU BVH comparison passed too. In a separate synthetic box32 comparison related to [#1559](https://git.gtahub.st/github/xcl-dev--xcl/issues/1559), CPU, HLSL and Direct DXIL agreed on the finite hit, finite miss and NaN origin on a parallel axis. For a NaN origin on a non-parallel axis and a NaN inverse-direction component, the patched CPU returned a miss while both shader paths returned the child. The GPU paths were unchanged in this check. RTX 3080 Ti (621.35), Windows 11 build 26300, Rust 1.99.1 MSVC and DXC 1.8.2502.11. Synthetic checks only. AI-assisted. Codex (GPT-6, exact variant unavailable) set up the local integration, ran the tests and drafted this report.
Author
Owner

@⁠b3IKK on GitHub · 2026-11-25 15:59 UTC · original

Thanks - that is a useful cross-check. The NaN miss here is deliberately CPU-only: intersect_node is the CPU emulation, and the shader paths are generated separately (crates/xcl/src/gpu/recomp/emit.rs).

#1559 is the open audit of box16/box32 NaN/inf handling in those shader paths, so aligning them with this CPU behaviour belongs there; this PR does not claim the two agree for a NaN ray. I have added that limitation to the description so it is not read as a complete CPU/GPU conformance fix.

AI use: OpenCode, Claude Fable 5.2 read the report, checked which issue covers the shader emission and wrote this.

**@⁠b3IKK** on GitHub · 2026-11-25 15:59 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7167495782) Thanks - that is a useful cross-check. The NaN miss here is deliberately CPU-only: ``intersect_node`` is the CPU emulation, and the shader paths are generated separately (``crates/xcl/src/gpu/recomp/emit.rs``). #1559 is the open audit of box16/box32 NaN/inf handling in those shader paths, so aligning them with this CPU behaviour belongs there; this PR does not claim the two agree for a NaN ray. I have added that limitation to the description so it is not read as a complete CPU/GPU conformance fix. AI use: OpenCode, Claude Fable 5.2 read the report, checked which issue covers the shader emission and wrote this.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 04:54 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: 1 failure under parallel scheduling; clean on --test-threads=1 rerun (1307 passed, 0 failed, 27 ignored, 76s). Intermittent single-test flake — tracked in #1978.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 04:54 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#issuecomment-7183012662) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: 1 failure under parallel scheduling; clean on `--test-threads=1` rerun (**1307 passed, 0 failed, 27 ignored**, 76s). Intermittent single-test flake — tracked in #1978. Thanks! — Devin
Author
Owner

@⁠Melow-code on GitHub · 2026-11-24 23:02 UTC · original · reviewed

The bounds() fix is correct and needed. The NaN guard only covers the CPU model — the HLSL bvh_intersect still ignores NaN rays, so CPU and GPU diverge on NaN input. Consider either extending the guard to the HLSL path or splitting this PR into the bounds fix (merge now) and the NaN fix (after the HLSL is updated).

**@⁠Melow-code** on GitHub · 2026-11-24 23:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/1322#pullrequestreview-6048793328) · **reviewed** The bounds() fix is correct and needed. The NaN guard only covers the CPU model — the HLSL bvh_intersect still ignores NaN rays, so CPU and GPU diverge on NaN input. Consider either extending the guard to the HLSL path or splitting this PR into the bounds fix (merge now) and the NaN fix (after the HLSL is updated).
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/1322/head:github-bot/gh-1322'
git switch 'github-bot/gh-1322'

Merge

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