GPU BVH: guard singular and zero-scale instance determinants (#1488) #1633

Open
github-bot wants to merge 1 commit from github-bot/gh-1633 into main AGit
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 18:26 UTC · original
slopgobblercobbler:fix/bvh-singular-transforms → main · +173 −3 · 1 files · 1 commits · open

Summary

Guards against singular and zero-scale instance transform determinants in crates/xcl/src/gpu/bvh.rs (Issue #1488, finding XCL-BT-021):

  1. Singular & zero-scale determinant protection:
    • In bvh::world_to_object, the 3x3 cofactor matrix was unconditionally divided by det (row.map(|x| x / det)).
    • Titles routinely disable or cull dynamic raytracing instances without rebuilding instance buffers by writing an all-zero or zero-scale 3x4 transform matrix (det == 0.0), populating inverse transform words with NaN / ±inf.
    • Added upfront guard if det.abs() <= 1e-30 || !det.is_finite() returning a safe zero transform [0.0; 12], plus finite post-checks.
  2. TLAS instance handling in build_top:
    • In build_top, when an instance matrix is singular or zero-scale (det.abs() <= 1e-30 || !det.is_finite()), clear the translation_only bit so rays do not bypass the inverse transform on culled instances, and sanitize non-finite elements.
    • Guarded transform_aabb and Bvh::bounds against degenerate / invalid inputs.
  3. Unit tests:
    • Added world_to_object_singular_and_zero_scale_fallbacks testing all-zero, zero-scale with translation, axis-singular, linearly dependent/rank-deficient matrices, underflow, and exact inverse matrix preservation for invertible transforms.
    • Added build_top_handles_singular_and_zero_scale_instances building TLAS with all-zero, zero-scale, singular, NaN, and active instances, validating finite bounds and clean scene ray traversal.

Changes

  • crates/xcl/src/gpu/bvh.rs:
    • Determinant underflow and non-finite guards in world_to_object.
    • Singular instance culling in build_top.
    • Synthetic unit test suite.

Tested

  • Windows 11 x64, Rust MSVC stable.
  • cargo test -p xcl -- bvh: 12 passed, 0 failed.
  • Clean diff: zero trailing whitespace, clean rebase on latest upstream/main, GPL-2.0-only compliant, zero retail game data or keys.

Not Tested

  • Real-time DXR 1.1 hardware acceleration structure traversal with dynamic BLAS compaction.

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 18:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633) `slopgobblercobbler:fix/bvh-singular-transforms` → `main` · +173 −3 · 1 files · 1 commits · open ## Summary Guards against singular and zero-scale instance transform determinants in `crates/xcl/src/gpu/bvh.rs` (Issue #1488, finding `XCL-BT-021`): 1. **Singular & zero-scale determinant protection**: - In `bvh::world_to_object`, the 3x3 cofactor matrix was unconditionally divided by `det` (`row.map(|x| x / det)`). - Titles routinely disable or cull dynamic raytracing instances without rebuilding instance buffers by writing an all-zero or zero-scale 3x4 transform matrix (`det == 0.0`), populating inverse transform words with `NaN` / `±inf`. - Added upfront guard `if det.abs() <= 1e-30 || !det.is_finite()` returning a safe zero transform `[0.0; 12]`, plus finite post-checks. 2. **TLAS instance handling in `build_top`**: - In `build_top`, when an instance matrix is singular or zero-scale (`det.abs() <= 1e-30 || !det.is_finite()`), clear the `translation_only` bit so rays do not bypass the inverse transform on culled instances, and sanitize non-finite elements. - Guarded `transform_aabb` and `Bvh::bounds` against degenerate / invalid inputs. 3. **Unit tests**: - Added `world_to_object_singular_and_zero_scale_fallbacks` testing all-zero, zero-scale with translation, axis-singular, linearly dependent/rank-deficient matrices, underflow, and exact inverse matrix preservation for invertible transforms. - Added `build_top_handles_singular_and_zero_scale_instances` building TLAS with all-zero, zero-scale, singular, NaN, and active instances, validating finite bounds and clean scene ray traversal. ## Changes - `crates/xcl/src/gpu/bvh.rs`: - Determinant underflow and non-finite guards in `world_to_object`. - Singular instance culling in `build_top`. - Synthetic unit test suite. ## Tested - Windows 11 x64, Rust MSVC stable. - `cargo test -p xcl -- bvh`: 12 passed, 0 failed. - Clean diff: zero trailing whitespace, clean rebase on latest `upstream/main`, GPL-2.0-only compliant, zero retail game data or keys. ## Not Tested - Real-time DXR 1.1 hardware acceleration structure traversal with dynamic BLAS compaction. ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
In bvh::world_to_object, check if det.abs() <= 1e-30 or !det.is_finite() before
dividing by det (finding XCL-BT-021 from issue #1488). Titles routinely cull or
disable dynamic TLAS instances by writing all-zero or zero-scale transform
matrices; dividing by zero previously populated the emitted instance node's
world_to_object inverse transform words with NaN or +/-inf, propagating NaNs
through ray traversal.

For singular, zero-scale, or non-finite transforms, world_to_object now returns
a safe zero transform ([0.0; 12]). In build_top, cleanly handle singular and
zero-scale instances by clearing the translation_only bit so rays cannot bypass
the inverse transform, and sanitize non-finite transforms in o2w and bounds
calculations.

Added unit tests world_to_object_singular_and_zero_scale_fallbacks and
build_top_handles_singular_and_zero_scale_instances verifying zero-transform
fallbacks on all-zero, zero-scale, rank-deficient, det-underflow, and NaN/inf
inputs, and clean miss handling during top-level scene traversal.

Co-Authored-By: Antigravity <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:00:04 +00:00
Author
Owner

@⁠Adika713 on GitHub · 2026-11-24 19:12 UTC · original

I independently checked the determinant cutoff concern and reproduced the arithmetic from the PR’s tiny test: determinant 1e-31, while the inverse diagonal is finite and f32-representable (1e11, 1e10, 1e10). Thus the absolute cutoff rejects a valid transform. I tested the alternative in a clean local worktree at upstream c160d86: reject only zero/non-finite determinant, then check the serialized f32 inverse for non-finite values. Added regression coverage for the small invertible transform and an inverse that overflows f32. cargo test -p xcl --lib gpu::bvh:: passed (8 passed, 0 failed, 0 ignored); two existing Atomic::fetch_update deprecation warnings. Not yet pushed or smoke-tested on a retail title.

**@⁠Adika713** on GitHub · 2026-11-24 19:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7141759152) I independently checked the determinant cutoff concern and reproduced the arithmetic from the PR’s `tiny` test: determinant 1e-31, while the inverse diagonal is finite and f32-representable (1e11, 1e10, 1e10). Thus the absolute cutoff rejects a valid transform. I tested the alternative in a clean local worktree at upstream c160d86: reject only zero/non-finite determinant, then check the serialized f32 inverse for non-finite values. Added regression coverage for the small invertible transform and an inverse that overflows f32. `cargo test -p xcl --lib gpu::bvh::` passed (8 passed, 0 failed, 0 ignored); two existing Atomic::fetch_update deprecation warnings. Not yet pushed or smoke-tested on a retail title.
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 19:33 UTC · original

Regression run for this PR: head d94e880, based on main 9e3831e. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s, in one batch with the main run.

main 9e3831e:

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1536 (PresentX 1536)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 97380 MB

this PR:

xcl-smoke v1
commit: d94e8802bc79d643030ae0a3529ceadc274d981a
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1561 (PresentX 1561)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 95641 MB

No device removals or crashes; 101 % of main's frames, 98 % of its peak commit.

Peak commit on this machine is mostly the pipeline preload's DXC compiles (#1035) and varies from run to run, so it says little about this PR.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

**@⁠Anon64** on GitHub · 2026-11-24 19:33 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7142263603) Regression run for this PR: head `d94e880`, based on `main` `9e3831e`. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, `XCL_NETWORK=0`, A pressed every 4 s from 20 s, `scripts/smoke.ps1` on a retail title for 60 s, in one batch with the `main` run. main `9e3831e`: ``` xcl-smoke v1 commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1536 (PresentX 1536) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 97380 MB ``` this PR: ``` xcl-smoke v1 commit: d94e8802bc79d643030ae0a3529ceadc274d981a gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1561 (PresentX 1561) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 95641 MB ``` No device removals or crashes; 101 % of main's frames, 98 % of its peak commit. Peak commit on this machine is mostly the pipeline preload's DXC compiles (#1035) and varies from run to run, so it says little about this PR. **Not tested:** the whole suite; whether the title reaches the changed code in these 60 s. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
Author
Owner

@⁠throny on GitHub · 2026-11-24 20:15 UTC · original

Two contributors checked independently and found that #1688 solves the same problem (for #1488) in a different way: #1607, group D40. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

**@⁠throny** on GitHub · 2026-11-24 20:15 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7143267007) <!-- xcl-1607-hint:D40 --> Two contributors checked independently and found that #1688 solves the same problem (for #1488) in a different way: #1607, group D40. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 20:56 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored.

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 20:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7144254025) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. Thanks!
Author
Owner

@⁠throny on GitHub · 2026-11-24 23:07 UTC · original

Two contributors checked independently and found that #1322 solves the same problem in a different way: #1607, group D69. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

**@⁠throny** on GitHub · 2026-11-24 23:07 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7147035367) <!-- xcl-1607-hint:D69 --> Two contributors checked independently and found that #1322 solves the same problem in a different way: #1607, group D69. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint from the community triage in #1607, not a maintainer decision. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
Author
Owner

@⁠throny on GitHub · 2026-11-25 00:15 UTC · original

Two contributors checked independently and found that #1521 #1688 solves the same problem (for #1488) in a different way: #1607, group D52. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

**@⁠throny** on GitHub · 2026-11-25 00:15 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7148320639) <!-- xcl-1607-hint:D52 --> Two contributors checked independently and found that #1521 #1688 solves the same problem (for #1488) in a different way: #1607, group D52. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint from the community triage in #1607, not a maintainer decision. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
@ -218,2 +227,4 @@
pub fn world_to_object(m: &[[f32; 4]; 3]) -> [f32; 12] {
let a = |r: usize, c: usize| m[r][c] as f64;
let det = a(0, 0) * (a(1, 1) * a(2, 2) - a(1, 2) * a(2, 1)) - a(0, 1) * (a(1, 0) * a(2, 2) - a(1, 2) * a(2, 0)) + a(0, 2) * (a(1, 0) * a(2, 1) - a(1, 1) * a(2, 0));
if det.abs() <= 1e-30 || !det.is_finite() {
Author
Owner

@⁠gut0to on GitHub · 2026-11-24 18:30 UTC · original

This absolute determinant cutoff rejects an invertible transform. The new tiny test uses diagonal scales 1e-11, 1e-10, 1e-10: its determinant is 1e-31, but its inverse diagonal (1e11, 1e10, 1e10) is finite and representable as f32. Returning an all-zero inverse for this instance makes its ray transform degenerate and can lose hits. Could singularity handling use a scale-relative test or validate the computed inverse for finiteness, and keep this tiny transform as a valid case? AI-assisted review; I verified these values.

**@⁠gut0to** on GitHub · 2026-11-24 18:30 UTC · [original](https://github.com/xcl-dev/xcl/pull/1633#discussion_r4684699951) This absolute determinant cutoff rejects an invertible transform. The new `tiny` test uses diagonal scales `1e-11, 1e-10, 1e-10`: its determinant is `1e-31`, but its inverse diagonal (`1e11, 1e10, 1e10`) is finite and representable as `f32`. Returning an all-zero inverse for this instance makes its ray transform degenerate and can lose hits. Could singularity handling use a scale-relative test or validate the computed inverse for finiteness, and keep this tiny transform as a valid case? AI-assisted review; I verified these values.
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/gpu/bvh.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1633/head:github-bot/gh-1633'
git switch 'github-bot/gh-1633'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1633'
git switch 'github-bot/gh-1633'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1633'
git switch 'github-bot/gh-1633'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1633'
git switch 'main'
git merge --squash 'github-bot/gh-1633'
git switch 'main'
git merge --ff-only 'github-bot/gh-1633'
git switch 'main'
git merge 'github-bot/gh-1633'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.