d3d12/shader: the ray-box test misses a present child's inverted box (D3D12-BVH-INVERTED-MISS) #3036

Closed
leonida/mandy
upipibi wants to merge 0 commits from d3d12/bvh-inverted-miss into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

upipibi commented 2026-12-02 04:02:09 +00:00

D3D12-BVH-INVERTED-MISS. The software ray-box test now misses a present child whose box is inverted (minimum above maximum on any axis), as AMD's form of the hardware test does.

What was wrong

bvh::boxes and the box test emitted by emit/intersect.rs take each axis's near and far as min/max of the two slab distances. That is symmetric in the box's two corners, so a box with min > max on an axis is entered as the box between its corners. A child that isn't there already gets this right: its inverted infinite bounds are skipped by the children[n] != NONE check (after VIctory bc3cd4b0). A present child doesn't.

AMD's software stand-in for the hardware intersection, GPURT fast_intersect_bbox (IntersectCommon.hlsl L393-441, MIT; used where the intrinsic isn't), picks each axis's entry and exit by the sign of the ray's inverse direction. For an inverted box the entry then comes after the exit: an empty interval, so a miss in either direction. The exception is a box inverted by no more than that test's growth of a few ulps (L515-518).

This reads AMD's source; it doesn't measure the console's instruction. If you'd rather keep this off by default until someone checks it on RDNA hardware, it's a one-line change.

What it changes

  • bvh::inverted_miss(): LEONIDA_BVH_INVERTED_MISS, on unless 0. The translator's build script already keys every LEONIDA_* name it reads into the shader cache, so a run with =0 gets its own translations.
  • bvh::boxes → boxes_as(.., inverted_miss), which is pure, for tests. A box with b[a] > b[3 + a] on any axis is missed.
  • emit/intersect.rs: the same compare in the emitted code. It uses ordered OGT, so a NaN gives "not inverted" and the existing NaN rule decides, the same as the reference's >.
  • d3d12/docs/switches.md: the new row, as tools/d3d12_switches.py writes it. Only that row is added; the regenerated page also changes an unrelated, stale LEONIDA_PROMPTS_PAD line, which I left alone.

Only -RayTracing runs this path, and it is off by default, so default frame rates do not change.

How it was tested

RTX 3070 8 GB, driver 621.40, Windows 11 24H2. No game files: the GPU tests build their nodes by hand.

  • cargo test -p leonida-shader --lib --locked: 421 passed, 0 failed, 5 ignored.
  • bvh:: + emit::intersect_tests: 23 of 23 pass with the switch unset, and 23 of 23 with LEONIDA_BVH_INVERTED_MISS=0.
  • Red/green. With only the emit/intersect.rs change reverted (the reference still misses inverted boxes), the four *_node_test_*_matches_the_reference_on_the_card tests fail at intersect_tests.rs:593. They pass with the change. The four new nodes are what make the card test cover this: present children inverted on x, on every axis, on y alone and on z alone, in 32- and 16-bit form. In the 16-bit node the infinite minimum rounds to -0, which is still above its -inf maximum, so it is still inverted.
  • New unit test bvh::tests::an_inverted_box_is_missed covers boxes inverted on x and y, on y alone, on z alone, and on every axis with infinite bounds, plus a flat box. It runs both ray directions, sorted and not.
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md passes, as does python -I tests/test_d3d12_switches.py (20 OK).
  • cargo clippy -p leonida-shader --all-targets -- -D warnings: nothing in the changed files. The 6 errors left are in emit/valu_tests.rs and are on main already.

Also checked, earlier, on VIctory-NVIDIA's equal code: a port of GPURT's function compared with the reference over 1,000,000 random rays and boxes per case.

case without the change with it
ordinary boxes 0 differ 0
inverted on one axis 26,175 differ 0
inverted on all axes, or inf/-inf 513,020 differ 0
flat on an axis 0 0

One small difference is left: a box inverted by only 1-8 ulps. GPURT enters it through its 6-ulp box growth and rounding, and the explicit compare misses it. Properly ordered boxes and the title's inf/-inf slots aren't affected.

Not tested: the game with -RayTracing.

Credit

A port of HQasper's VICTORY_BVH_INVERTED_MISS (VIctory-NVIDIA 72230a3e1, an opt-in there), adapted to this tree's boxes and emitted test. The two card-test nodes and the GPURT comparison are mine.

AI-assisted: written with Claude Code (Claude Opus 5.5). A separate read-only reviewer agent (Claude Opus 5.5) audited the diff before this was opened.

🤖 Generated with Claude Code

**D3D12-BVH-INVERTED-MISS.** The software ray-box test now misses a present child whose box is inverted (minimum above maximum on any axis), as AMD's form of the hardware test does. ## What was wrong `bvh::boxes` and the box test emitted by `emit/intersect.rs` take each axis's near and far as `min`/`max` of the two slab distances. That is symmetric in the box's two corners, so a box with `min > max` on an axis is entered as the box between its corners. A child that isn't there already gets this right: its inverted infinite bounds are skipped by the `children[n] != NONE` check (after VIctory bc3cd4b0). A present child doesn't. AMD's software stand-in for the hardware intersection, GPURT `fast_intersect_bbox` ([`IntersectCommon.hlsl` L393-441](https://github.com/GPUOpen-Drivers/gpurt/blob/7b226d48b46b7e92fec3b9ecc5712e5bf2bf3dd9/src/shaders/IntersectCommon.hlsl#L393-L441), MIT; used where the intrinsic isn't), picks each axis's entry and exit by the sign of the ray's inverse direction. For an inverted box the entry then comes after the exit: an empty interval, so a miss in either direction. The exception is a box inverted by no more than that test's growth of a few ulps (L515-518). This reads AMD's source; it doesn't measure the console's instruction. If you'd rather keep this off by default until someone checks it on RDNA hardware, it's a one-line change. ## What it changes - `bvh::inverted_miss()`: `LEONIDA_BVH_INVERTED_MISS`, on unless `0`. The translator's build script already keys every `LEONIDA_*` name it reads into the shader cache, so a run with `=0` gets its own translations. - `bvh::boxes` → `boxes_as(.., inverted_miss)`, which is pure, for tests. A box with `b[a] > b[3 + a]` on any axis is missed. - `emit/intersect.rs`: the same compare in the emitted code. It uses ordered `OGT`, so a NaN gives "not inverted" and the existing NaN rule decides, the same as the reference's `>`. - `d3d12/docs/switches.md`: the new row, as `tools/d3d12_switches.py` writes it. Only that row is added; the regenerated page also changes an unrelated, stale `LEONIDA_PROMPTS_PAD` line, which I left alone. Only `-RayTracing` runs this path, and it is off by default, so default frame rates do not change. ## How it was tested RTX 3070 8 GB, driver 621.40, Windows 11 24H2. No game files: the GPU tests build their nodes by hand. - `cargo test -p leonida-shader --lib --locked`: 421 passed, 0 failed, 5 ignored. - `bvh::` + `emit::intersect_tests`: 23 of 23 pass with the switch unset, and 23 of 23 with `LEONIDA_BVH_INVERTED_MISS=0`. - **Red/green.** With only the `emit/intersect.rs` change reverted (the reference still misses inverted boxes), the four `*_node_test_*_matches_the_reference_on_the_card` tests fail at `intersect_tests.rs:593`. They pass with the change. The four new nodes are what make the card test cover this: present children inverted on x, on every axis, on y alone and on z alone, in 32- and 16-bit form. In the 16-bit node the infinite minimum rounds to -0, which is still above its -inf maximum, so it is still inverted. - New unit test `bvh::tests::an_inverted_box_is_missed` covers boxes inverted on x and y, on y alone, on z alone, and on every axis with infinite bounds, plus a flat box. It runs both ray directions, sorted and not. - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` passes, as does `python -I tests/test_d3d12_switches.py` (20 OK). - `cargo clippy -p leonida-shader --all-targets -- -D warnings`: nothing in the changed files. The 6 errors left are in `emit/valu_tests.rs` and are on `main` already. Also checked, earlier, on VIctory-NVIDIA's equal code: a port of GPURT's function compared with the reference over 1,000,000 random rays and boxes per case. | case | without the change | with it | |---|---|---| | ordinary boxes | 0 differ | 0 | | inverted on one axis | 26,175 differ | **0** | | inverted on all axes, or inf/-inf | 513,020 differ | **0** | | flat on an axis | 0 | 0 | One small difference is left: a box inverted by only 1-8 ulps. GPURT enters it through its 6-ulp box growth and rounding, and the explicit compare misses it. Properly ordered boxes and the title's inf/-inf slots aren't affected. Not tested: the game with `-RayTracing`. ## Credit A port of HQasper's `VICTORY_BVH_INVERTED_MISS` (VIctory-NVIDIA 72230a3e1, an opt-in there), adapted to this tree's `boxes` and emitted test. The two card-test nodes and the GPURT comparison are mine. AI-assisted: written with Claude Code (Claude Opus 5.5). A separate read-only reviewer agent (Claude Opus 5.5) audited the diff before this was opened. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kal0s-design commented 2026-12-02 06:05:15 +00:00

Independent run on Windows 11, MSVC toolchain 1.98.1. Head 1a8773e1f merged into current main 0ae5a2664: the merge is clean.

  • cargo test -p leonida-shader --lib: 421 passed, 0 failed, 5 ignored. I touched build.rs first so the translator's source hash was fresh.
  • bvh:: + emit::intersect_tests: 23 of 23 pass with the switch unset, and 23 of 23 with LEONIDA_BVH_INVERTED_MISS=0.
  • The four *_node_test_*_matches_the_reference_on_the_card tests pass. With --nocapture neither device printed "skipped", so they ran on both the hardware device and WARP.
  • Red/green reproduced. With only emit/intersect.rs put back to main, all four card tests fail. Each fails on the hardware pass with batch 2 thread 14: Some((5, 5, Ray { extent: 6.0, ... })). The line is intersect_tests.rs:601 here rather than 593, because of the merge with main. With the change they pass. That run stops at the hardware pass, so it doesn't show WARP's answer on the reverted emit.
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md: exit 0. python -I tests/test_d3d12_switches.py: OK.
  • cargo clippy -p leonida-shader --lib --tests: no warning in bvh.rs or emit/intersect.rs.

Not checked: AMD hardware, and the game with -RayTracing. The card here is the same vendor as yours, so this is a second machine, not a second vendor.

GPUs tested: one NVIDIA desktop card + WARP.

AI-assisted: yes (Claude Code).

Independent run on Windows 11, MSVC toolchain 1.98.1. Head `1a8773e1f` merged into current `main` `0ae5a2664`: the merge is clean. - `cargo test -p leonida-shader --lib`: 421 passed, 0 failed, 5 ignored. I touched `build.rs` first so the translator's source hash was fresh. - `bvh::` + `emit::intersect_tests`: 23 of 23 pass with the switch unset, and 23 of 23 with `LEONIDA_BVH_INVERTED_MISS=0`. - The four `*_node_test_*_matches_the_reference_on_the_card` tests pass. With `--nocapture` neither device printed "skipped", so they ran on both the hardware device and WARP. - **Red/green reproduced.** With only `emit/intersect.rs` put back to `main`, all four card tests fail. Each fails on the hardware pass with `batch 2 thread 14: Some((5, 5, Ray { extent: 6.0, ... }))`. The line is `intersect_tests.rs:601` here rather than 593, because of the merge with `main`. With the change they pass. That run stops at the hardware pass, so it doesn't show WARP's answer on the reverted emit. - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md`: exit 0. `python -I tests/test_d3d12_switches.py`: OK. - `cargo clippy -p leonida-shader --lib --tests`: no warning in `bvh.rs` or `emit/intersect.rs`. Not checked: AMD hardware, and the game with `-RayTracing`. The card here is the same vendor as yours, so this is a second machine, not a second vendor. GPUs tested: one NVIDIA desktop card + WARP. AI-assisted: yes (Claude Code).
upipibi commented 2026-12-02 06:16:51 +00:00

Closing this to free this account's only open-PR slot for a fix that runs by default: D3D12-TARGETS-AT-RESTORE, a release of a host-written texture at a shared address losing the older one. This path only runs with -RayTracing, so it can wait.

Nothing is lost. The branch stays at upipibi:d3d12/bvh-inverted-miss (1a8773e1f), with the tests and the red/green described above. Anyone may take it, or I'll reopen this when a slot frees.

Closing this to free this account's only open-PR slot for a fix that runs by default: D3D12-TARGETS-AT-RESTORE, a release of a host-written texture at a shared address losing the older one. This path only runs with `-RayTracing`, so it can wait. Nothing is lost. The branch stays at `upipibi:d3d12/bvh-inverted-miss` (`1a8773e1f`), with the tests and the red/green described above. Anyone may take it, or I'll reopen this when a slot frees.
Sign in to join this conversation.
No description provided.