Recompiler: watertight ray/triangle test for BVH triangle nodes, behind XCL_BVH_WATERTIGHT #3038
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3038"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by tiagonleal on 2026-11-27T16:02:46Z.
Part of #3011. The triangle node of
image_bvh_intersect_rayis 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 inREADME.md.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 equalmain's is not proven by a test.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):
Möller-Trumbore misses both. The new test hits at least one.
Rebased onto
main75093adf, plus one new commit for the three binary32 cases in @thehollowedone's review.maingavetranslate_on_this_threadone more argument (ps_rt32_mask); the two calls in this PR'scfg(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 ismain'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 withd[kz]= 2^-60 was lost to an overflow: the weighted depths now use the unscaled axial distances and only the sign ofd[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 withbvhin their name); theXCL_DIRECT_DXIL=1run 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 intests.rs, and the rest is one test written three times (the CPU reference inbvh.rswith 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_hitpicks the watertight test (triangle_hit_watertight) or the old one (triangle_hit_mt) by the switch.dominant_axispicks x before y before z on ties.crates/xcl/src/gpu/recomp/emit.rs: the same steps as an HLSL block markedprecise, swapped into the prelude text when the switch is on.crates/xcl/src/gpu/dxil/lower_image.rs: the same steps, withDisableMathRefactoring; 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.THIRD_PARTY.mdand notes in the file headers ofbvh.rs,emit.rsandlower_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.rstriangle, written by @fxceless. It follows Mesa RADV's software emulation of the instruction,src/amd/vulkan/nir/radv_nir_rt_common.cintersect_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_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 withXCL_DIRECT_DXIL=1). The existing GPU BVH tests run with it off, against the old CPU reference.Tested
RTX 5070 Ti 16 GB (driver 617.42), Ryzen 7 9800X3D, 32 GB RAM, Windows 11 Pro 26300, branch head
54d35fb9onmainf9a0cb80.cargo test -p xcl --lib -j 4 -- gpu::bvh gpu::recomp gpu::dxil::core_tests: 434 passed, 0 failed, 21 ignored; the same withXCL_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 aregpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3, which fail the same way onmainon this machine. The new tests are among the passed.SKIPPEDon this adapter: the wave64 GPU tests (no wave64) andheap_window_out_of_memory_falls_back_to_the_switch_form(no heap windows); none of the BVH tests.main75093adf(head5b45711d):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 ishle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, which fails the same way onmain(it came in with #2369; #3474 fixes it). The two other failures of the first run arehle::d3d12x::pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retryandhle::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.maincb802468(a slightly newermaincommit than the one under the branch) on the same machine: 2809 passed, 1 failed (the same stand-in test), 51 ignored. 27SKIPPEDlines in every log (22no wave64, 3 f16/f32 per-lane cases refused, 2no 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,maincb802468(cb8024684c, a slightly newer commit than themain75093adfunder the branch) first, one run each. Both runs hadXCL_PREPARE=0and the same warm DXIL cache. A warm-up run ofmaincame before them and is not shown. A second run ofmainafter the PR runs gave 1684 frames, so differences of under a hundred frames between runs are noise here.main(cb802468):This PR (
5b45711d):This PR with
XCL_BVH_WATERTIGHT=1: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
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.1/ddifferently from the CPU, so the GPU test checks each side for a hit instead of comparing them.tests.rs, andbvh.rs(+284) is the CPU reference with its tests. 25 of the removed lines are the old DXIL lowering moved one indent level into theelsebranch.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.
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:(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.(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 newu*p0.z,i*p1.z, andj*p2.zproducts overflow before multiplication byabs(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.(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), butdet*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.
Imported from ivall/xcl. Originally posted by tiagonleal on 2026-11-28T14:37:07Z.
@thehollowedone thanks, all three reproduced on
54d35fb9exactly as you wrote them. The head is nowd0987261(rebased ontomaine429936c, one new commit):d[kz], as you suggested. Your case returns(-2^80, -1, -1/4, -1/4), bit for bit what the old test returns.!= 0so 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T09:23:55Z.
Triage (#1607) – head
5b45711No 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T12:33:44Z.
merge-check
base:
493dbd3dd0head:
5b45711ddemerge: 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_WATERTIGHTunset the HLSL is the same text (tested) and the DXILelsebranch 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,iweights v1,jweights v2). Findings: (1) Turning it on empties the whole translated cache. The tag atpso.rs~386 goes on the cache-wideidentity(), so every program retranslates, not only BVH ones. That follows theXCL_X_LDS_ORDERpattern, but the description's "own cache entries" suggests otherwise. (2) Not fully watertight. Thefacingcheck (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 withXCL_DIRECT_DXIL=1on 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_drivercoherent-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.
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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.