Skip to content

Recompiler: image_bvh_intersect_ray misses triangles behind the ray origin - #3622

Draft
KDenKC wants to merge 1 commit into
xcl-dev:mainfrom
KDenKC:pr/rt-tsign
Draft

KDenKC wants to merge 1 commit into
xcl-dev:mainfrom
KDenKC:pr/rt-tsign

Conversation

@KDenKC

@KDenKC KDenKC commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Part of #3011. The triangle form of image_bvh_intersect_ray (the HLSL bvh_intersect and the direct-DXIL lowering) checked the barycentric signs against the determinant but not the sign of the distance: a triangle behind the ray's origin came back as a hit with a negative t·det / det. The hardware reports hits ahead of the origin only, and a trace program can rely on that: a shadow ray that starts on a surface then finds the triangles behind its start as blockers.

A hit now also needs t > 0 when det > 0 and t < 0 when det < 0, on both paths: a ray that starts on the triangle (t = 0) misses it too, as the console's leaf test has it (a miss when t·det times the sign of det is not above 0). The CPU form of the node test (gpu::bvh::triangle_hit), which the GPU tests compare the translator against, had the same gap, so the two agreed on the wrong answer and no test caught it; it gets the same check. No runtime path calls the CPU form (the CPU walk behind bvh_want follows child ids, not rays).

Overlaps #3038 (the same three functions; its watertight form, behind a switch, leaves t's sign to the traversal): whichever lands second carries the check into the other form.

What is in it

  • Recompiler: image_bvh_intersect_ray misses a triangle behind the ray's origin: gpu/recomp/emit.rs (HLSL prelude), gpu/dxil/lower_image.rs, gpu/bvh.rs (triangle_hit); tests gpu_bvh_triangle_behind_the_origin_is_missed (gpu/recomp/tests.rs: rays ahead, pointing away, starting past and starting on the triangle, both windings) and triangle_hit_misses_a_triangle_behind_the_origin (gpu/bvh.rs).

The table of node-test cases with hand-stated answers (box and triangle nodes) that was a second commit here becomes a Tests: PR of its own, as it tests code this PR does not change.

The change alters the HLSL prelude and the DXIL lowering, so the translated programs that use image_bvh_intersect_ray get new cache keys and are translated again once.

Tested

  • Head e62ee138 on main 1c8b5514, RTX 3090 VM (NVIDIA 621.24, Intel Core i7-10700T, Windows 11 build 26300.9974, Rust 1.99.1, debug), against main 1c8b5514 on the same machine:
    • cargo test -p xcl --lib -- bvh --nocapture --test-threads=1: 28 passed, 0 failed.
    • cargo test --workspace --no-fail-fast -- --test-threads=4: 2903 passed, 5 failed, 51 ignored (main: 2901 passed, 5 failed, 51 ignored; the 2 more are this PR's tests). The same 5 failures on both, main's own on this machine: title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, the 3 stream_recheck_* tests, e2e test_title_runs_and_game_folder_is_untouched. SKIPPED: 28 (22 no wave64, 2 no heap windows, 4 no debug layer). The run leaves out the 3 uma_driver coherent-window remake tests that kill the lib binary on this driver.
  • RTX 5090 32 GB, driver 621.40, Ryzen 7 9800X3D, Windows 11 Pro build 26200.9974, Rust 1.99.1, debug, an earlier head d3af2c26 (t ≥ 0, with the table of cases):
    • cargo build --workspace: ok.
    • cargo test -p xcl --lib -- bvh --test-threads=4: 30 passed, 0 failed, with XCL_DIRECT_DXIL=0 (HLSL) and with XCL_DIRECT_DXIL=1.
    • Without the fix (shader, lowering and triangle_hit reverted), triangle_hit_misses_a_triangle_behind_the_origin fails; gpu_bvh_triangle_behind_the_origin_is_missed failed before the shader fix on both paths.
    • cargo test --workspace --no-fail-fast -- --test-threads=4: 2913 passed, 0 failed, 51 ignored. SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 driver cache cannot be disabled).

Regression run

  • To follow: a scripts/smoke.ps1 pair (main and this head) on the RTX 5090.

Not tested

  • In the world: whether rooms and shadows change. A run on the RTX 5090 and one on an RTX 4080 are in progress.
  • AMD and Intel GPUs; GPUs with 64-lane waves (the wave64 tests skip on this adapter).

AI use

  • Claude Code (Claude Opus 5.5, Claude Sonnet 5.5), Codex (GPT-6.2-Sol), Antigravity (Gemini 4 Neon, Gemini 4 Hydrogen): an agent swarm wrote, tested, reviewed and described this change.

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory 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) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head d3af2c2, diff read, not a merge-check; draft, checks red):

  • Unrelated test cases in the fix PR. The second commit adds gpu/recomp/bvh_ref_tests.rs, whose 9 box-node cases (~83-91: slot order, sorting, tmax, origin inside or behind, axis-parallel and oblique rays) test code this PR does not change, and whose triangle cases repeat gpu_bvh_triangle_behind_the_origin_is_missed and triangle_hit_misses_a_triangle_behind_the_origin. CONTRIBUTING.md puts a test-only change in its own PR.
  • Regression run missing. The PR changes the runtime (gpu/recomp/emit.rs, gpu/dxil/lower_image.rs) and Regression run has no xcl-smoke blocks yet, which is why PR description is red.

Fine: the t-sign check is the same in all three forms (HLSL t >= 0 / t <= 0 by the sign of det, DXIL FOGe / FOLe, the CPU triangle_hit) with the same NaN and -0.0 results; the expected values match the node layouts; no runtime path calls triangle_hit or Bvh::trace. #3038 touches the same three functions without this check, as the description says.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@github-actions github-actions Bot added needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up review (head 5b6e35d, since d3af2c2, diff read, not a merge-check; draft, checks red):

  • Unrelated test cases in the fix PR: still open. bvh_ref_tests.rs still carries the 9 box-node cases for code this PR does not change; they would fit a separate Tests: PR.
  • Regression run missing: still open ("To follow").

New: none. Since d3af2c26 an origin on the triangle (t = 0) is a miss in all three forms: HLSL t > 0 / t < 0 (emit.rs ~6469), DXIL FOGt / FOLt (lower_image.rs ~313, ~324) and triangle_hit (bvh.rs ~267), with a case in both the CPU test and the reference table for each winding.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

…s origin

The node test's triangle form (HLSL bvh_intersect and the direct-DXIL
lowering) checked the barycentric signs against the denominator but not
the sign of the distance: a triangle behind the ray's origin came back as
a hit with a negative t / det. The hardware reports hits ahead of the
origin only, and a trace program can rely on that: a shadow ray that
starts on a surface would find the triangles behind its start as blockers.

A hit now also needs t > 0 when det > 0 and t < 0 when det < 0, on both
paths: a triangle the ray starts on (t = 0) is a miss too, as in the
console's own CPU form of the leaf test, which misses t_num * sign(t_denom)
<= 0 and returns (inf, 1, 0, 0).

The CPU form of the node test (gpu::bvh::triangle_hit, which the GPU tests
compare the translator against) had the same gap, so CPU and GPU agreed on
the wrong answer and no test caught it; it gets the same check. No runtime
path calls it (the CPU walk behind bvh_want follows child ids, not rays).

Test: gpu_bvh_triangle_behind_the_origin_is_missed (a triangle ahead of
the origin is hit at t = 5; pointing away from it, starting past it and
starting on it miss; both windings). It fails before the change on the
HLSL path and with XCL_DIRECT_DXIL=1.
triangle_hit_misses_a_triangle_behind_the_origin is the same on the CPU;
it fails before the change too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up review (head e62ee13, since 5b6e35d, diff read, not a merge-check; draft, checks red):

  • Unrelated test cases in the fix PR: fixed. bvh_ref_tests.rs and its mod line are gone; the t = 0 case moved into gpu_bvh_triangle_behind_the_origin_is_missed (gpu/recomp/tests.rs ~6417-6445) as a fourth lane pattern, alongside the CPU case in bvh.rs.
  • Regression run missing: still open ("To follow").

New: none.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@KDenKC

KDenKC commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@Foniass Thanks. Both points taken, head now e62ee138:

  • Unrelated test cases: the table of node-test cases (bvh_ref_tests.rs) is out of this PR. It becomes a Tests: PR of its own, stacked on this one, because its triangle cases include the t = 0 rule. The fix's own GPU test now also covers a ray that starts on the triangle (t = 0), next to the rays ahead, pointing away and starting past it, in both windings.
  • Regression run: still to follow. It needs the RTX 5090, which is busy with in-world runs at the moment; the xcl-smoke pair of main and this head goes in when it is free.

AI use: an agent swarm: Claude Opus 5.5, Claude Sonnet 5.5, GPT-6.2-Sol, Gemini 4 Neon and Gemini 4 Hydrogen.

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: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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants