Recompiler: watertight ray/triangle test for BVH triangle nodes, behind XCL_BVH_WATERTIGHT #3038

Open
tiagonleal wants to merge 6 commits from upstream-pr/3038 into main
tiagonleal commented 2026-11-27 16:02:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by tiagonleal on 2026-11-27T16:02:46Z.


Part of #3011. The triangle node of image_bvh_intersect_ray is emulated with a Möller-Trumbore test. For a ray through an edge shared by two triangles it can report a miss for both, which leaves a hole in whatever the title traces (shadows, occlusion, visibility). This PR adds, in the CPU reference, the HLSL prelude and the direct DXIL lowering, the watertight test of Woop, Benthin and Wald (JCGT 2013): the ray's dominant axis becomes z, the triangle is sheared into ray space, and the three signed edge functions decide the hit, so two triangles sharing an edge compute exactly negated values for it.

XCL_BVH_WATERTIGHT=1 (off by default) turns it on. It changes what a title sees, so as #3011 asks it is behind a switch first. The switch is read once (gpu::bvh::watertight) and documented in README.md.

  • Off: all three paths use Möller-Trumbore as before. The emitted HLSL is the same text as on main (the prelude constant is unchanged; a test checks that the off HLSL has no trace of the switch and holds the old triangle block), so DXC cache keys, which hash the text, stay the same. The translated cache identity is unchanged too. The DXIL lowering's off branch is the old code in the old order; that its bytes equal main's is not proven by a test.
  • On: the watertight block replaces the old one in the prelude text only for programs that call bvh_intersect, so only those get new DXC keys (a test checks that a program without BVH is the same in both positions). The translated disk cache's identity (recomp::pso) gets a tag, because its entries are keyed by the console program and would otherwise be shared between the two positions. The tag is on the identity of the whole cache, so turning the switch on retranslates every program once, including the ones without BVH nodes. Driver blob caches hash the DXIL or the pipeline stream, so they follow. I checked these keys by reading the code; no test flips the switch through the disk caches.

A case where the old test misses both triangles (pinned as a test):

  • shared edge A = (1.911541, -2.025442, 0.78372467), B = (1.911541, 2.740394, -0.50947857)
  • triangles [A, B, (2.1104007, -1.6796683, 9.487581)] and [B, A, (1.5455573, 0.7414906, -8.11272)]
  • ray origin (5.911541, -0.8080063, 0.59203976), direction (-1.0, -0.13060194, 0.000772506)

Möller-Trumbore misses both. The new test hits at least one.

Rebased onto main 75093adf, plus one new commit for the three binary32 cases in @thehollowedone's review. main gave translate_on_this_thread one more argument (ps_rt32_mask); the two calls in this PR's cfg(test) wrapper pass it on. Rebased on #3288: the watertight test only replaces the hit/t/det computation, so #3288's NaN/Inf ray inactivation still runs first and its triangle return mode (0 or 1) still selects the output layout; with the switch off the code is main's. Two of this PR's GPU tests built their BVH descriptor with word 3 = 0, which since #3288 selects return mode 0; they now set bit 24 (mode 1, barycentrics), as #3288's own tests do. All three reproduced on the old head. A coplanar ray (Moller-Trumbore determinant exactly 0, sheared determinant -2^-23) was a hit: a hit now also needs the determinant of the original ray and plane to have the sheared determinant's sign. A hit with d[kz] = 2^-60 was lost to an overflow: the weighted depths now use the unscaled axial distances and only the sign of d[kz], and the result equals the old test's bit for bit. A hit with an encoded determinant that rounds to 0 was returned with denominator -0: the encoded determinant must now be a normal number. The same change is in the CPU reference, the HLSL and the direct DXIL, with a CPU test (triangle_hit_review_cases_coplanar_overflow_zero_denominator) and a GPU test that compares both backends with the CPU bit for bit (gpu_bvh_watertight_review_cases_match_the_cpu). The BVH tests of the lib, this PR's and #3288's, pass on the new head in the suite run below (39 tests with bvh in their name); the XCL_DIRECT_DXIL=1 run was not repeated on the new head. The extra determinant costs about 9 multiplies and 6 adds per triangle on the watertight path. Not proven: that two triangles sharing an edge never both miss a ray that is nearly parallel to both. On size: about 650 changed lines, over the 400 target. 151 are the GPU tests in tests.rs, and the rest is one test written three times (the CPU reference in bvh.rs with its unit tests, the HLSL text, the direct DXIL lowering). I kept them in one PR because the two backends must match the reference bit for bit and are checked against it by the same tests.

What is in it

  • crates/xcl/src/gpu/bvh.rs: triangle_hit picks the watertight test (triangle_hit_watertight) or the old one (triangle_hit_mt) by the switch. dominant_axis picks x before y before z on ties.
  • crates/xcl/src/gpu/recomp/emit.rs: the same steps as an HLSL block marked precise, swapped into the prelude text when the switch is on.
  • crates/xcl/src/gpu/dxil/lower_image.rs: the same steps, with DisableMathRefactoring; the old lowering when the switch is off.
  • crates/xcl/src/gpu/recomp/pso.rs: the translated cache identity. crates/xcl/src/gpu/dxil/kernel.rs: in tests only, the setting is carried over to the translator's thread.
  • With the switch on, the result keeps its encoding and scale: (t·det, det, two barycentric numerators chosen by the id bits), miss = (inf, 1, 0, 0), determinant sign for facing. The values are scaled back by |d[kz]| after the hit decision so the determinant matches the old one. A non-finite t or det is a miss.
  • THIRD_PARTY.md and notes in the file headers of bvh.rs, emit.rs and lower_image.rs (source commit and date of changes).

Upstream and credit: victory-fork (https://github.com/fxceless/victory-fork, GPL-3.0-or-later) 9a9091d, crates/victory-shader/src/bvh.rs triangle, written by @fxceless. It follows Mesa RADV's software emulation of the instruction, src/amd/vulkan/nir/radv_nir_rt_common.c intersect_ray_amd_software_tri (Copyright © 2021 Google, MIT), which credits the paper by Sven Woop, Carsten Benthin and Ingo Wald. Thanks to @fxceless and the RADV developers. Changes from upstream: f32 edge functions (Mesa uses f64), no t sign test in the node, XCL's result scale, and the switch.

Tests

  • triangle_hit_pinned_shared_edge_moller_trumbore_misses: the case above. Through the switch: off misses both triangles, on hits at least one.
  • triangle_hit_exact_shared_edge_and_vertex: dyadic coordinates, rays exactly through a shared edge (both vertex orders) and through the shared vertex of an 8-triangle fan, every dominant axis, both signs.
  • triangle_hit_agrees_with_moller_trumbore_on_generic_rays: same hit/miss away from edges, same determinant sign and magnitude, t and barycentrics within 1e-4.
  • triangle_hit_degenerate_non_finite_and_axis_ties: degenerate, in-plane, zero and NaN rays miss, a non-finite t misses with the node's miss encoding, and ties pick the first axis.
  • bvh_watertight_switch_changes_the_hlsl_and_the_dxil: off, the HLSL holds the old triangle block and nothing of the switch; on, the watertight block; a program without BVH is the same in both; the direct DXIL bytes differ.
  • GPU test gpu_bvh_triangle_pair_shared_edge_is_watertight (switch on): rays through shared edges on every axis, plus the pinned pair in the last two lanes, where the test also checks that the old CPU test misses both. The GPU hits there, so the switch reaches the translation (passes with the HLSL path and with XCL_DIRECT_DXIL=1). The existing GPU BVH tests run with it off, against the old CPU reference.
  • Tests set the switch for their own thread only, never through the process environment.

Tested

RTX 5070 Ti 16 GB (driver 617.42), Ryzen 7 9800X3D, 32 GB RAM, Windows 11 Pro 26300, branch head 54d35fb9 on main f9a0cb80.

  • cargo test -p xcl --lib -j 4 -- gpu::bvh gpu::recomp gpu::dxil::core_tests: 434 passed, 0 failed, 21 ignored; the same with XCL_DIRECT_DXIL=1: 434 passed, 0 failed, 21 ignored.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4, debug: 1466 passed, 4 failed, 47 ignored. The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3, which fail the same way on main on this machine. The new tests are among the passed. SKIPPED on this adapter: the wave64 GPU tests (no wave64) and heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows); none of the BVH tests.
  • After the rebase onto main 75093adf (head 5b45711d): cargo test --workspace --no-fail-fast -j 8 -- --test-threads=4, two full runs. First run: 2813 passed, 3 failed, 51 ignored. Second run: 2815 passed, 1 failed, 51 ignored. The failure in both is hle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, which fails the same way on main (it came in with #2369; #3474 fixes it). The two other failures of the first run are hle::d3d12x::pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retry and hle::d3d12x::pso_async::used_tests::retained_submit_demand_restores_one_exact_generation_after_address_reuse: they passed in the second full run, alone 6 of 6 each, and in two runs of their module (181 passed, 0 failed); this PR does not touch that code. main cb802468 (a slightly newer main commit than the one under the branch) on the same machine: 2809 passed, 1 failed (the same stand-in test), 51 ignored. 27 SKIPPED lines in every log (22 no wave64, 3 f16/f32 per-lane cases refused, 2 no heap windows). The other results in this section are from the earlier heads named in them.

Regression run
scripts/smoke.ps1 -Seconds 60 -Title 9cc6a39e (Newer Retail Game), release builds of both commits in clean trees, main cb802468 (cb8024684c, a slightly newer commit than the main 75093adf under the branch) first, one run each. Both runs had XCL_PREPARE=0 and the same warm DXIL cache. A warm-up run of main came before them and is not shown. A second run of main after the PR runs gave 1684 frames, so differences of under a hundred frames between runs are noise here.

main (cb802468):

xcl-smoke v1
commit: cb8024684c7babcb0569ecda2217a54b8bedafb1
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1662 (PresentX 1662)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16196 MB

This PR (5b45711d):

xcl-smoke v1
commit: 5b45711dde12f3877d669bea5200a24c0c2b800e
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1678 (PresentX 1678)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15802 MB

This PR with XCL_BVH_WATERTIGHT=1:

xcl-smoke v1
commit: 5b45711dde12f3877d669bea5200a24c0c2b800e
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16151 MB

The switch prints no log line of its own, so the run with it on does not show whether the title reached a BVH triangle node.

Not tested

  • The console's own rule for a ray exactly on an edge is not documented anywhere I found. The tests assert at least one hit, not exactly one.
  • When a shader also uses a fused multiply-add, XCL drops the DXIL no-refactoring flag (gpu/shader.rs), so a driver may fuse the edge products there and the exact negation is lost. That is no worse than before, but not watertight.
  • The edge functions are in single precision. Very close to an edge this can report a hit just outside it where Mesa, which recomputes them in double, would report a miss.
  • The GPU may round 1/d differently from the CPU, so the GPU test checks each side for a hit instead of comparing them.
  • No title was checked for a visible difference with the switch on.
  • Not tested on AMD or Intel adapters, or on Linux/Wine.
  • The branch is +654 / -27 over six commits; 151 of the added lines are the GPU tests in tests.rs, and bvh.rs (+284) is the CPU reference with its tests. 25 of the removed lines are the old DXIL lowering moved one indent level into the else branch.

Related open PRs in the same code: #1322, #2087, #2302, #2913.

AI use

Claude Code with Claude Fable 5.2 and Anthropic subagents (Claude Opus 5.5, Claude Sonnet 5.5) helped developing with the supervision of @tiagonleal.

<!-- greyfinch-sync:ivall/xcl:issue:5529588207 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3038). Originally posted by [tiagonleal](https://github.com/tiagonleal) on 2026-11-27T16:02:46Z. --- Part of #3011. The triangle node of `image_bvh_intersect_ray` is emulated with a Möller-Trumbore test. For a ray through an edge shared by two triangles it can report a miss for both, which leaves a hole in whatever the title traces (shadows, occlusion, visibility). This PR adds, in the CPU reference, the HLSL prelude and the direct DXIL lowering, the watertight test of Woop, Benthin and Wald (JCGT 2013): the ray's dominant axis becomes z, the triangle is sheared into ray space, and the three signed edge functions decide the hit, so two triangles sharing an edge compute exactly negated values for it. **`XCL_BVH_WATERTIGHT=1`** (off by default) turns it on. It changes what a title sees, so as #3011 asks it is behind a switch first. The switch is read once (`gpu::bvh::watertight`) and documented in `README.md`. - Off: all three paths use Möller-Trumbore as before. The emitted HLSL is the same text as on `main` (the prelude constant is unchanged; a test checks that the off HLSL has no trace of the switch and holds the old triangle block), so DXC cache keys, which hash the text, stay the same. The translated cache identity is unchanged too. The DXIL lowering's off branch is the old code in the old order; that its bytes equal `main`'s is not proven by a test. - On: the watertight block replaces the old one in the prelude text only for programs that call `bvh_intersect`, so only those get new DXC keys (a test checks that a program without BVH is the same in both positions). The translated disk cache's identity (`recomp::pso`) gets a tag, because its entries are keyed by the console program and would otherwise be shared between the two positions. The tag is on the identity of the whole cache, so turning the switch on retranslates every program once, including the ones without BVH nodes. Driver blob caches hash the DXIL or the pipeline stream, so they follow. I checked these keys by reading the code; no test flips the switch through the disk caches. **A case where the old test misses both triangles** (pinned as a test): - shared edge A = (1.911541, -2.025442, 0.78372467), B = (1.911541, 2.740394, -0.50947857) - triangles [A, B, (2.1104007, -1.6796683, 9.487581)] and [B, A, (1.5455573, 0.7414906, -8.11272)] - ray origin (5.911541, -0.8080063, 0.59203976), direction (-1.0, -0.13060194, 0.000772506) Möller-Trumbore misses both. The new test hits at least one. *Rebased onto `main` `75093adf`, plus one new commit for the three binary32 cases in @thehollowedone's review.* `main` gave `translate_on_this_thread` one more argument (`ps_rt32_mask`); the two calls in this PR's `cfg(test)` wrapper pass it on. Rebased on #3288: the watertight test only replaces the hit/t/det computation, so #3288's NaN/Inf ray inactivation still runs first and its triangle return mode (0 or 1) still selects the output layout; with the switch off the code is `main`'s. Two of this PR's GPU tests built their BVH descriptor with word 3 = 0, which since #3288 selects return mode 0; they now set bit 24 (mode 1, barycentrics), as #3288's own tests do. All three reproduced on the old head. A coplanar ray (Moller-Trumbore determinant exactly 0, sheared determinant -2^-23) was a hit: a hit now also needs the determinant of the original ray and plane to have the sheared determinant's sign. A hit with `d[kz]` = 2^-60 was lost to an overflow: the weighted depths now use the unscaled axial distances and only the sign of `d[kz]`, and the result equals the old test's bit for bit. A hit with an encoded determinant that rounds to 0 was returned with denominator -0: the encoded determinant must now be a normal number. The same change is in the CPU reference, the HLSL and the direct DXIL, with a CPU test (`triangle_hit_review_cases_coplanar_overflow_zero_denominator`) and a GPU test that compares both backends with the CPU bit for bit (`gpu_bvh_watertight_review_cases_match_the_cpu`). The BVH tests of the lib, this PR's and #3288's, pass on the new head in the suite run below (39 tests with `bvh` in their name); the `XCL_DIRECT_DXIL=1` run was not repeated on the new head. The extra determinant costs about 9 multiplies and 6 adds per triangle on the watertight path. Not proven: that two triangles sharing an edge never both miss a ray that is nearly parallel to both. On size: about 650 changed lines, over the 400 target. 151 are the GPU tests in `tests.rs`, and the rest is one test written three times (the CPU reference in `bvh.rs` with its unit tests, the HLSL text, the direct DXIL lowering). I kept them in one PR because the two backends must match the reference bit for bit and are checked against it by the same tests. **What is in it** - `crates/xcl/src/gpu/bvh.rs`: `triangle_hit` picks the watertight test (`triangle_hit_watertight`) or the old one (`triangle_hit_mt`) by the switch. `dominant_axis` picks x before y before z on ties. - `crates/xcl/src/gpu/recomp/emit.rs`: the same steps as an HLSL block marked `precise`, swapped into the prelude text when the switch is on. - `crates/xcl/src/gpu/dxil/lower_image.rs`: the same steps, with `DisableMathRefactoring`; the old lowering when the switch is off. - `crates/xcl/src/gpu/recomp/pso.rs`: the translated cache identity. `crates/xcl/src/gpu/dxil/kernel.rs`: in tests only, the setting is carried over to the translator's thread. - With the switch on, the result keeps its encoding and scale: (t·det, det, two barycentric numerators chosen by the id bits), miss = (inf, 1, 0, 0), determinant sign for facing. The values are scaled back by |d[kz]| after the hit decision so the determinant matches the old one. A non-finite t or det is a miss. - `THIRD_PARTY.md` and notes in the file headers of `bvh.rs`, `emit.rs` and `lower_image.rs` (source commit and date of changes). **Upstream and credit:** victory-fork (https://github.com/fxceless/victory-fork, GPL-3.0-or-later) `9a9091d`, `crates/victory-shader/src/bvh.rs` `triangle`, written by @fxceless. It follows Mesa RADV's software emulation of the instruction, `src/amd/vulkan/nir/radv_nir_rt_common.c` `intersect_ray_amd_software_tri` (Copyright © 2021 Google, MIT), which credits the paper by Sven Woop, Carsten Benthin and Ingo Wald. Thanks to @fxceless and the RADV developers. Changes from upstream: f32 edge functions (Mesa uses f64), no t sign test in the node, XCL's result scale, and the switch. **Tests** - `triangle_hit_pinned_shared_edge_moller_trumbore_misses`: the case above. Through the switch: off misses both triangles, on hits at least one. - `triangle_hit_exact_shared_edge_and_vertex`: dyadic coordinates, rays exactly through a shared edge (both vertex orders) and through the shared vertex of an 8-triangle fan, every dominant axis, both signs. - `triangle_hit_agrees_with_moller_trumbore_on_generic_rays`: same hit/miss away from edges, same determinant sign and magnitude, t and barycentrics within 1e-4. - `triangle_hit_degenerate_non_finite_and_axis_ties`: degenerate, in-plane, zero and NaN rays miss, a non-finite t misses with the node's miss encoding, and ties pick the first axis. - `bvh_watertight_switch_changes_the_hlsl_and_the_dxil`: off, the HLSL holds the old triangle block and nothing of the switch; on, the watertight block; a program without BVH is the same in both; the direct DXIL bytes differ. - GPU test `gpu_bvh_triangle_pair_shared_edge_is_watertight` (switch on): rays through shared edges on every axis, plus the pinned pair in the last two lanes, where the test also checks that the old CPU test misses both. The GPU hits there, so the switch reaches the translation (passes with the HLSL path and with `XCL_DIRECT_DXIL=1`). The existing GPU BVH tests run with it off, against the old CPU reference. - Tests set the switch for their own thread only, never through the process environment. **Tested** RTX 5070 Ti 16 GB (driver 617.42), Ryzen 7 9800X3D, 32 GB RAM, Windows 11 Pro 26300, branch head `54d35fb9` on `main` `f9a0cb80`. - `cargo test -p xcl --lib -j 4 -- gpu::bvh gpu::recomp gpu::dxil::core_tests`: 434 passed, 0 failed, 21 ignored; the same with `XCL_DIRECT_DXIL=1`: 434 passed, 0 failed, 21 ignored. - `cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4`, debug: 1466 passed, 4 failed, 47 ignored. The 4 failures are `gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3`, which fail the same way on `main` on this machine. The new tests are among the passed. `SKIPPED` on this adapter: the wave64 GPU tests (no wave64) and `heap_window_out_of_memory_falls_back_to_the_switch_form` (no heap windows); none of the BVH tests. - After the rebase onto `main` `75093adf` (head `5b45711d`): `cargo test --workspace --no-fail-fast -j 8 -- --test-threads=4`, two full runs. First run: 2813 passed, 3 failed, 51 ignored. Second run: 2815 passed, 1 failed, 51 ignored. The failure in both is `hle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline`, which fails the same way on `main` (it came in with #2369; #3474 fixes it). The two other failures of the first run are `hle::d3d12x::pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retry` and `hle::d3d12x::pso_async::used_tests::retained_submit_demand_restores_one_exact_generation_after_address_reuse`: they passed in the second full run, alone 6 of 6 each, and in two runs of their module (181 passed, 0 failed); this PR does not touch that code. `main` `cb802468` (a slightly newer `main` commit than the one under the branch) on the same machine: 2809 passed, 1 failed (the same stand-in test), 51 ignored. 27 `SKIPPED` lines in every log (22 `no wave64`, 3 f16/f32 per-lane cases refused, 2 `no heap windows`). The other results in this section are from the earlier heads named in them. **Regression run** `scripts/smoke.ps1 -Seconds 60 -Title 9cc6a39e` (Newer Retail Game), release builds of both commits in clean trees, `main` `cb802468` (cb8024684c7babcb0569ecda2217a54b8bedafb1, a slightly newer commit than the `main` `75093adf` under the branch) first, one run each. Both runs had `XCL_PREPARE=0` and the same warm DXIL cache. A warm-up run of `main` came before them and is not shown. A second run of `main` after the PR runs gave 1684 frames, so differences of under a hundred frames between runs are noise here. `main` (`cb802468`): ``` xcl-smoke v1 commit: cb8024684c7babcb0569ecda2217a54b8bedafb1 gpu: NVIDIA GeForce RTX 5070 Ti title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1662 (PresentX 1662) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 16196 MB ``` This PR (`5b45711d`): ``` xcl-smoke v1 commit: 5b45711dde12f3877d669bea5200a24c0c2b800e gpu: NVIDIA GeForce RTX 5070 Ti title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1678 (PresentX 1678) presents per 2 s (last 3): 60 61 60 device removals: 0 crashes: 0 peak commit: 15802 MB ``` This PR with `XCL_BVH_WATERTIGHT=1`: ``` xcl-smoke v1 commit: 5b45711dde12f3877d669bea5200a24c0c2b800e gpu: NVIDIA GeForce RTX 5070 Ti title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 16151 MB ``` The switch prints no log line of its own, so the run with it on does not show whether the title reached a BVH triangle node. **Not tested** - The console's own rule for a ray exactly on an edge is not documented anywhere I found. The tests assert at least one hit, not exactly one. - When a shader also uses a fused multiply-add, XCL drops the DXIL no-refactoring flag (`gpu/shader.rs`), so a driver may fuse the edge products there and the exact negation is lost. That is no worse than before, but not watertight. - The edge functions are in single precision. Very close to an edge this can report a hit just outside it where Mesa, which recomputes them in double, would report a miss. - The GPU may round `1/d` differently from the CPU, so the GPU test checks each side for a hit instead of comparing them. - No title was checked for a visible difference with the switch on. - Not tested on AMD or Intel adapters, or on Linux/Wine. - The branch is +654 / -27 over six commits; 151 of the added lines are the GPU tests in `tests.rs`, and `bvh.rs` (+284) is the CPU reference with its tests. 25 of the removed lines are the old DXIL lowering moved one indent level into the `else` branch. Related open PRs in the same code: #1322, #2087, #2302, #2913. **AI use** Claude Code with Claude Fable 5.2 and Anthropic subagents (Claude Opus 5.5, Claude Sonnet 5.5) helped developing with the supervision of @tiagonleal.
thehollowedone commented 2026-11-28 02:34:47 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-28T02:34:47Z.


Source review of head 54d35fb: I checked the new triangle arithmetic in the CPU, HLSL, and direct DXIL paths. Three finite-input binary32 cases need regressions before enabling this path:

  1. A coplanar ray can be accepted. Triangle (0,0,0), (1,1,1), (4,5,6); origin (-1,-2,-3); direction (1,2,3). The original determinant is exactly zero, but the rounded sheared edge sum is -2^-23, so the new test returns a hit. This contradicts the stated edge-on miss rule. Please classify an ambiguous determinant against the original ray and triangle plane, without broadly rejecting small valid triangles.
  2. A finite interior hit can be discarded by an intermediate overflow. Triangle (0,0,2^20), (2^30,0,2^20), (0,2^30,2^20); origin (2^28,2^28,0); direction (0,0,2^-60). The existing test returns (t·det, det, i·det, j·det) = (-2^80,-1,-1/4,-1/4). The new u*p0.z, i*p1.z, and j*p2.z products overflow before multiplication by abs(d[kz]), and the new path returns a miss. The weighted axial depths can instead use the unscaled vertex-minus-origin axial coordinates, with the dominant direction's sign applied to the final numerator; this avoids the reciprocal and final rescale on that numerator.
  3. A hit can have a zero encoded denominator. Triangle (0,0,1), (2^-40,0,1), (0,2^-40,1); origin (2^-42,2^-42,0); direction (0,0,2^-80). The sheared determinant is nonzero (-2^-80), but det*abs(d[kz]) rounds to zero. The current test returns a hit with denominator -0. Gate on the encoded determinant too, so traversal never receives a zero denominator.

The relevant CPU calculation is mirrored by HLSL and direct DXIL, so the corresponding regressions and changes should cover all three. These are source and binary32 arithmetic checks; I did not run a build or GPU test for this review. AI use: Codex (GPT-6.2 Sol) independently checked the arithmetic and drafted this review; Codex (GPT-6.2 Astra) audited the source.

<!-- greyfinch-sync:ivall/xcl:comment:7232458020 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3038#issuecomment-7232458020). Originally posted by [thehollowedone](https://github.com/thehollowedone) on 2026-11-28T02:34:47Z. --- Source review of head [`54d35fb`](https://github.com/xcl-dev/xcl/commit/54d35fb97ce7b00b6b0c7780a66addaf0a7d73b3): I checked the new triangle arithmetic in the CPU, HLSL, and direct DXIL paths. Three finite-input binary32 cases need regressions before enabling this path: 1. **A coplanar ray can be accepted.** Triangle `(0,0,0), (1,1,1), (4,5,6)`; origin `(-1,-2,-3)`; direction `(1,2,3)`. The original determinant is exactly zero, but the rounded sheared edge sum is `-2^-23`, so the new test returns a hit. This contradicts the stated edge-on miss rule. Please classify an ambiguous determinant against the original ray and triangle plane, without broadly rejecting small valid triangles. 2. **A finite interior hit can be discarded by an intermediate overflow.** Triangle `(0,0,2^20), (2^30,0,2^20), (0,2^30,2^20)`; origin `(2^28,2^28,0)`; direction `(0,0,2^-60)`. The existing test returns `(t·det, det, i·det, j·det) = (-2^80,-1,-1/4,-1/4)`. The new `u*p0.z`, `i*p1.z`, and `j*p2.z` products overflow before multiplication by `abs(d[kz])`, and the new path returns a miss. The weighted axial depths can instead use the **unscaled** vertex-minus-origin axial coordinates, with the dominant direction's sign applied to the final numerator; this avoids the reciprocal and final rescale on that numerator. 3. **A hit can have a zero encoded denominator.** Triangle `(0,0,1), (2^-40,0,1), (0,2^-40,1)`; origin `(2^-42,2^-42,0)`; direction `(0,0,2^-80)`. The sheared determinant is nonzero (`-2^-80`), but `det*abs(d[kz])` rounds to zero. The current test returns a hit with denominator `-0`. Gate on the encoded determinant too, so traversal never receives a zero denominator. The relevant [CPU calculation](https://github.com/xcl-dev/xcl/blob/54d35fb97ce7b00b6b0c7780a66addaf0a7d73b3/crates/xcl/src/gpu/bvh.rs#L294-L306) is mirrored by HLSL and direct DXIL, so the corresponding regressions and changes should cover all three. These are source and binary32 arithmetic checks; I did not run a build or GPU test for this review. AI use: Codex (GPT-6.2 Sol) independently checked the arithmetic and drafted this review; Codex (GPT-6.2 Astra) audited the source.
tiagonleal commented 2026-11-28 14:37:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by tiagonleal on 2026-11-28T14:37:07Z.


@thehollowedone thanks, all three reproduced on 54d35fb9 exactly as you wrote them. The head is now d0987261 (rebased onto main e429936c, one new commit):

  1. Coplanar ray: a hit now also needs the determinant of the original ray and plane to have the sheared determinant's sign. For your case it is exactly 0, so the ray misses.
  2. Overflow: the weighted depths use the unscaled axial distances and only the sign of d[kz], as you suggested. Your case returns (-2^80, -1, -1/4, -1/4), bit for bit what the old test returns.
  3. Zero denominator: the encoded determinant must be a normal number. I used that instead of != 0 so that a GPU that flushes subnormals can't hand the traversal a zero either. Your case misses; the same small triangle with a unit direction still hits.

The CPU reference, the HLSL and the direct DXIL have the same change, and a new GPU test compares both backends with the CPU bit for bit on your three cases. One thing I did not prove: that two triangles sharing an edge never both miss a ray that is nearly parallel to both.

AI use: Claude Code wrote this comment (Claude Fable 5.2); Claude Opus 5.5 wrote the change and ran the tests on an RTX 5070 Ti.

<!-- greyfinch-sync:ivall/xcl:comment:7240804193 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3038#issuecomment-7240804193). Originally posted by [tiagonleal](https://github.com/tiagonleal) on 2026-11-28T14:37:07Z. --- @thehollowedone thanks, all three reproduced on `54d35fb9` exactly as you wrote them. The head is now `d0987261` (rebased onto `main` `e429936c`, one new commit): 1. Coplanar ray: a hit now also needs the determinant of the original ray and plane to have the sheared determinant's sign. For your case it is exactly 0, so the ray misses. 2. Overflow: the weighted depths use the unscaled axial distances and only the sign of `d[kz]`, as you suggested. Your case returns `(-2^80, -1, -1/4, -1/4)`, bit for bit what the old test returns. 3. Zero denominator: the encoded determinant must be a normal number. I used that instead of `!= 0` so that a GPU that flushes subnormals can't hand the traversal a zero either. Your case misses; the same small triangle with a unit direction still hits. The CPU reference, the HLSL and the direct DXIL have the same change, and a new GPU test compares both backends with the CPU bit for bit on your three cases. One thing I did not prove: that two triangles sharing an edge never both miss a ray that is nearly parallel to both. **AI use:** Claude Code wrote this comment (Claude Fable 5.2); Claude Opus 5.5 wrote the change and ran the tests on an RTX 5070 Ti.
Foniass commented 2026-12-01 09:23:55 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T09:23:55Z.


Triage (#1607) – head 5b45711

No open or merged PR makes the same change; the other #3011 PRs port different parts (#3026 RT leave-out, #3040/#3042 wave-local, #3081 BC7) and #2913 only changes the miss sentinel and node guard. No problems found in the diff: off by default, the HLSL stays byte-identical with the switch off, and the switch gets its own cache identity.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7306642630 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3038#issuecomment-7306642630). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T09:23:55Z. --- **Triage (#1607)** – head 5b45711 No open or merged PR makes the same change; the other #3011 PRs port different parts (#3026 RT leave-out, #3040/#3042 wave-local, #3081 BC7) and #2913 only changes the miss sentinel and node guard. No problems found in the diff: off by default, the HLSL stays byte-identical with the switch off, and the switch gets its own cache identity. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Foniass commented 2026-12-01 12:33:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T12:33:44Z.


merge-check
base: 493dbd3dd0
head: 5b45711dde
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2908 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+654/-27). With XCL_BVH_WATERTIGHT unset the HLSL is the same text (tested) and the DXIL else branch is main's code in main's order. With it set, triangle nodes use a watertight test that keeps main's output format: (t*det, det, i, j) at Möller-Trumbore scale with det's sign for facing (the algebra checks out: tz * sign(d[kz]) = t * det_mt, i weights v1, j weights v2). Findings: (1) Turning it on empties the whole translated cache. The tag at pso.rs ~386 goes on the cache-wide identity(), so every program retranslates, not only BVH ones. That follows the XCL_X_LDS_ORDER pattern, but the description's "own cache entries" suggests otherwise. (2) Not fully watertight. The facing check (bvh.rs ~323-324, emit.rs ~6306-6307) compares against a separately rounded Möller-Trumbore det per triangle, so for rays nearly parallel to two triangles sharing an edge both can still miss (the author says it's unproven). (3) On the direct DXIL path the exact negation is lost when a module uses FMad (lower_image.rs ~295-298), and the review cases weren't rerun with XCL_DIRECT_DXIL=1 on this head. (4) About twice the ALU per triangle node when on (an fdiv, the shear, the extra det), not measured on an RT scene. Fine: the miss encoding is unchanged and NaN rays are still handled first (#3288). The GPU tests cover rays exactly through shared edges and vertices on all axes, a pinned pair that Möller-Trumbore misses, and the three review cases bit for bit against the CPU. Under 800 lines with the size explained. Neither test is bit-exact to RDNA2's own tie rules, which aren't documented. No smoke run: off, the shader text is identical, and nothing shows the title tracing rays in 60 s.
performance: no smoke run (the default path doesn't reach this change); not measured. With the switch off, the shader text is identical to main's, so no smoke run. The first run of this merge lost the lib test binary to the known uma_driver coherent-window abort; this is the rerun.
verdict: safe to merge (off by default; the description should say that turning it on retranslates every program, not only BVH ones)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7311317953 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3038#issuecomment-7311317953). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T12:33:44Z. --- merge-check base: 493dbd3dd0cd50b4c3e8d28f73ab9b504fcd8158 head: 5b45711dde12f3877d669bea5200a24c0c2b800e merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2908 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none review: read the whole diff (+654/-27). With `XCL_BVH_WATERTIGHT` unset the HLSL is the same text (tested) and the DXIL `else` branch is main's code in main's order. With it set, triangle nodes use a watertight test that keeps main's output format: `(t*det, det, i, j)` at Möller-Trumbore scale with det's sign for facing (the algebra checks out: `tz * sign(d[kz]) = t * det_mt`, `i` weights v1, `j` weights v2). Findings: (1) **Turning it on empties the whole translated cache.** The tag at `pso.rs` ~386 goes on the cache-wide `identity()`, so every program retranslates, not only BVH ones. That follows the `XCL_X_LDS_ORDER` pattern, but the description's "own cache entries" suggests otherwise. (2) **Not fully watertight.** The `facing` check (`bvh.rs` ~323-324, `emit.rs` ~6306-6307) compares against a separately rounded Möller-Trumbore det per triangle, so for rays nearly parallel to two triangles sharing an edge both can still miss (the author says it's unproven). (3) On the direct DXIL path the exact negation is lost when a module uses FMad (`lower_image.rs` ~295-298), and the review cases weren't rerun with `XCL_DIRECT_DXIL=1` on this head. (4) About twice the ALU per triangle node when on (an fdiv, the shear, the extra det), not measured on an RT scene. Fine: the miss encoding is unchanged and NaN rays are still handled first (#3288). The GPU tests cover rays exactly through shared edges and vertices on all axes, a pinned pair that Möller-Trumbore misses, and the three review cases bit for bit against the CPU. Under 800 lines with the size explained. Neither test is bit-exact to RDNA2's own tie rules, which aren't documented. No smoke run: off, the shader text is identical, and nothing shows the title tracing rays in 60 s. performance: no smoke run (the default path doesn't reach this change); not measured. With the switch off, the shader text is identical to main's, so no smoke run. The first run of this merge lost the lib test binary to the known `uma_driver` coherent-window abort; this is the rerun. verdict: safe to merge (off by default; the description should say that turning it on retranslates every program, not only BVH ones) hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
tiagonleal commented 2026-12-02 09:42:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by tiagonleal on 2026-12-02T09:42:26Z.


Right, the tag is on the identity of the whole translated cache, so turning the switch on retranslates every program once, including the ones without BVH. I added that to the description.

  • Tiago
<!-- greyfinch-sync:ivall/xcl:comment:7341362143 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3038#issuecomment-7341362143). Originally posted by [tiagonleal](https://github.com/tiagonleal) on 2026-12-02T09:42:26Z. --- Right, the tag is on the identity of the whole translated cache, so turning the switch on retranslates every program once, including the ones without BVH. I added that to the description. - Tiago
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/3038:upstream-pr/3038'
git switch 'upstream-pr/3038'

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/3038'
git switch 'upstream-pr/3038'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3038'
git switch 'upstream-pr/3038'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3038'
git switch 'main'
git merge --squash 'upstream-pr/3038'
git switch 'main'
git merge --ff-only 'upstream-pr/3038'
git switch 'main'
git merge 'upstream-pr/3038'
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
xcl-dev/xcl!3038
No description provided.