Repository navigation
Recompiler: watertight ray/triangle test for BVH triangle nodes, behind XCL_BVH_WATERTIGHT - #3038
tiagonleal wants to merge 6 commits into
Conversation
|
Source review of head
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. |
54d35fb to
d098726
Compare
…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.
|
@thehollowedone thanks, all three reproduced on
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. |
…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.
d098726 to
3774703
Compare
3774703 to
81f7d76
Compare
…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.
…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.
81f7d76 to
c9f14cf
Compare
…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.
c9f14cf to
f43bedd
Compare
…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.
f43bedd to
5a2d0fa
Compare
…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.
b8e2132 to
ac7bf38
Compare
…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.
ac7bf38 to
5b45711
Compare
|
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. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human. |
|
merge-check AI-written and posted by Foniass without a human check of each point. |
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. 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 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 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(cb80246, 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.