Skip to content

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

Open
tiagonleal wants to merge 6 commits into
xcl-dev:mainfrom
tiagonleal:bvh-watertight-triangle
Open

tiagonleal wants to merge 6 commits into
xcl-dev:mainfrom
tiagonleal:bvh-watertight-triangle

Conversation

@tiagonleal

@tiagonleal tiagonleal commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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. 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 D3D12.X: deduplicate concurrent stand-in creation and harden shell COM methods #2369; Tests: the stand-in race test keeps the let-go pipeline on its thread #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 (cb80246, 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

Copy link
Copy Markdown
Contributor

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.

@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch from 54d35fb to d098726 Compare November 28, 2026 14:35
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 28, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@tiagonleal

Copy link
Copy Markdown
Contributor Author

@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.

tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 28, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch from d098726 to 3774703 Compare November 28, 2026 16:07
@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch from 3774703 to 81f7d76 Compare November 28, 2026 18:17
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 28, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 29, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch from 81f7d76 to c9f14cf Compare November 29, 2026 12:01
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:docs-ci Documentation, scripts, workflows, repo governance area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Nov 29, 2026
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 29, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch from c9f14cf to f43bedd Compare November 29, 2026 13:12
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 29, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch from f43bedd to 5a2d0fa Compare November 29, 2026 14:31
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 29, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@tiagonleal
tiagonleal force-pushed the bvh-watertight-triangle branch 2 times, most recently from b8e2132 to ac7bf38 Compare November 29, 2026 23:19
tiagonleal added a commit to tiagonleal/xcl that referenced this pull request Nov 29, 2026
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
The triangle node was emulated with Moller-Trumbore in the CPU reference
(gpu/bvh.rs), the HLSL prelude (bvh_intersect) and the DXIL lowering. Its
inside test is not consistent between two triangles that share an edge, so
a ray exactly through the edge can miss both: holes in traced shadows and
visibility. The new test is the watertight one (Woop, Benthin, Wald, JCGT
2013): shear so the ray runs along its largest axis, then the signs of
the three edge functions decide. A shared edge's function is exactly
negated in the other triangle, so at least one of them hits.

Adapted from victory-fork (GPL-3.0-or-later), which follows Mesa RADV's
software emulation of the instruction (MIT). The result encoding is
unchanged (t*det, det, I*det, J*det, miss = inf, 1, 0, 0); edge-on and NaN
still miss and t is still not tested in the node. HLSL uses precise and
DXIL requires DisableMathRefactoring so no product is fused.

Tests: shared edge and shared vertex rays along every axis and both ray
signs (fails with the old test), agreement with Moller-Trumbore on random
rays, degenerate / in-plane / NaN rays, and a GPU test of a triangle pair's
shared edge.
Follow-up to the watertight triangle test. The sheared determinant is
Moller-Trumbore's divided by |d[kz]|: t, det and the barycentric
numerators are now multiplied back by |d[kz]| after the hit decision in
the CPU reference, the HLSL prelude and the DXIL lowering, so the four
dwords keep the scale XCL returned before (checked against the old test
on random rays). A non-finite t or det (tiny d[kz], huge shear) is a miss.

Tests: exact dyadic rays through a shared edge (both vertex orders) and
a fan's shared vertex on every axis and sign; a pinned ray through a
shared edge that Moller-Trumbore misses in both triangles; axis choice on
ties; non-finite t. With the old test the pinned, random-edge and
non-finite tests fail; the exact dyadic cases pass with both.

Limit: the DXIL module drops DisableMathRefactoring when it also uses
FMad, and a driver may then fuse the edge-function products, which loses
the exact negation across a shared edge (no worse than before). Provenance:
Mesa RADV src/amd/vulkan/nir/radv_nir_rt_common.c
intersect_ray_amd_software_tri (MIT), via victory-fork (GPL-3.0-or-later).
…TIGHT

The watertight test changes what a title's ray tracing sees, so as xcl-dev#3011
asks for a port it is now behind a switch, off by default:
XCL_BVH_WATERTIGHT=1 selects it, otherwise the CPU reference, the HLSL
prelude and the DXIL lowering use Moller-Trumbore exactly as before.

The switch is read once (gpu::bvh::watertight). The HLSL gets it as a
positions; the translated cache's identity (recomp::pso) gets a tag when
it is on, so the existing entries stay valid with it off and a stored
watertight translation is never taken for a Moller-Trumbore one. Tests
set it per thread (carried over to the DXIL translator's thread), never
through the process environment.

Tests: the pinned shared-edge ray misses both triangles with the switch
off and hits with it on; the emitted HLSL and the direct DXIL differ
between the two positions. Provenance: victory-fork 9a9091d (fxceless),
after Mesa RADV intersect_ray_amd_software_tri (Copyright 2021 Google,
MIT); README documents the switch.
With the switch off the emitted HLSL is now byte for byte what it was
before: the #define and the #if/#else in the BVH prelude went into every
shader's text, so every DXC cache key changed and every user would have
compiled all shaders again. The watertight triangle block now replaces
Moller-Trumbore's in the prelude text in Rust, only when the switch is on
and only for programs that call bvh_intersect. The DXIL lowering creates
its zero constant where it did before in the Moller-Trumbore branch.

Tests: the off HLSL has no trace of the switch and holds the old triangle
block, a program without BVH is the same in both positions; the GPU test
adds the pinned pair Moller-Trumbore misses in both triangles, so a GPU
hit there shows the switch reached the translation. The random near-edge
test is dropped (the exact and pinned cases cover it) and three small CPU
tests are merged into one. The provenance note moves to the file headers.
…ominators

Three finite binary32 inputs from the xcl-dev#3038 review, reproduced on the CPU
reference, the HLSL and the direct DXIL before this change:

- coplanar ray (Moller-Trumbore det exactly 0, rounded sheared det -2^-23):
  returned a hit. The hit now also needs Moller-Trumbore's det of the
  original ray and plane (f32, same operation order in all three) to have
  the sheared det's sign.
- tiny d[kz] (2^-60): u*p0.z etc. overflowed and a finite interior hit
  (-2^80, -1, -1/4, -1/4) was lost. The weighted depths now use the
  unscaled axial distances and only d[kz]'s sign is applied (exact), which
  is t*det at Moller-Trumbore's scale without the 1/d[kz] and |d[kz]|
  round trip. Now returns Moller-Trumbore's values bit for bit.
- encoded det = sheared det * |d[kz]| rounding to 0 (-2^-80 * 2^-80):
  returned a hit with denominator -0. The encoded det must now be a normal
  number, so it is never 0, also on a GPU that flushes subnormals.

The edge functions and their sign decision are unchanged, so the shared
edge property is the same. Default path (XCL_BVH_WATERTIGHT off) untouched.
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 493dbd3
head: 5b45711
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.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:docs-ci Documentation, scripts, workflows, repo governance area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs:amd-test Needs a test report or merge-check on an AMD GPU needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants