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

Open
slopgobblercobbler wants to merge 1 commit from upstream-pr/1633 into main
slopgobblercobbler commented 2026-11-24 18:26:39 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-24T18:26:39Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:5477976773 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-24T18:26:39Z. --- ## 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
gut0to commented 2026-11-24 18:30:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:30:50Z.

Review: COMMENTED


AI-assisted review; I checked the determinant and inverse values in the new test.

<!-- greyfinch-sync:ivall/xcl:review:6045080847 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#pullrequestreview-6045080847). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:30:50Z. **Review:** COMMENTED --- AI-assisted review; I checked the determinant and inverse values in the new test.
gut0to commented 2026-11-24 19:08:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:30:51Z.

Code review location: crates/xcl/src/gpu/bvh.rs; line 230

@@ -210,13 +217,19 @@ fn apply(m: &[[f32; 4]; 3], p: [f32; 3]) -> [f32; 3] {
 }
 
 fn transform_aabb(m: &[[f32; 4]; 3], (mn, mx): Aabb) -> Aabb {
+    if m.iter().flatten().any(|x| !x.is_finite()) || mn.iter().chain(&mx).any(|x| !x.is_finite()) {
+        return ([0.0; 3], [0.0; 3]);
+    }
     bounds((0..8).map(|k| apply(m, [if k & 1 == 0 { mn[0] } else { mx[0] }, if k & 2 == 0 { mn[1] } else { mx[1] }, if k & 4 == 0 { mn[2] } else { mx[2] }])))
 }
 
 /// inverse of an affine object → world, in the instance node's order [t, row 0, row 1, row 2]
-fn world_to_object(m: &[[f32; 4]; 3]) -> [f32; 12] {
+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() {

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.

<!-- greyfinch-sync:ivall/xcl:review-comment:4684699951 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#discussion_r4684699951). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:30:51Z. **Code review location:** `crates/xcl/src/gpu/bvh.rs`; line 230 ~~~~diff @@ -210,13 +217,19 @@ fn apply(m: &[[f32; 4]; 3], p: [f32; 3]) -> [f32; 3] { } fn transform_aabb(m: &[[f32; 4]; 3], (mn, mx): Aabb) -> Aabb { + if m.iter().flatten().any(|x| !x.is_finite()) || mn.iter().chain(&mx).any(|x| !x.is_finite()) { + return ([0.0; 3], [0.0; 3]); + } bounds((0..8).map(|k| apply(m, [if k & 1 == 0 { mn[0] } else { mx[0] }, if k & 2 == 0 { mn[1] } else { mx[1] }, if k & 4 == 0 { mn[2] } else { mx[2] }]))) } /// inverse of an affine object → world, in the instance node's order [t, row 0, row 1, row 2] -fn world_to_object(m: &[[f32; 4]; 3]) -> [f32; 12] { +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() { ~~~~ --- 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.
Adika713 commented 2026-11-24 21:08:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Adika713 on 2026-11-24T19:12:36Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7141759152 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7141759152). Originally posted by [Adika713](https://github.com/Adika713) on 2026-11-24T19:12:36Z. --- 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.
Anon64 commented 2026-11-24 21:08:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Anon64 on 2026-11-24T19:33:16Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7142263603 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7142263603). Originally posted by [Anon64](https://github.com/Anon64) on 2026-11-24T19:33:16Z. --- 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.
throny commented 2026-11-24 21:09:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-24T20:15:42Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7143267007 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7143267007). Originally posted by [throny](https://github.com/throny) on 2026-11-24T20:15:42Z. --- <!-- 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.
DavidM1911 commented 2026-11-24 21:09:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T20:56:10Z.


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

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

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7144254025 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7144254025). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T20:56:10Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. Thanks!
throny commented 2026-11-24 23:08:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-24T23:07:11Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7147035367 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7147035367). Originally posted by [throny](https://github.com/throny) on 2026-11-24T23:07:11Z. --- <!-- 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.
throny commented 2026-11-25 01:10:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-25T00:15:34Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7148320639 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7148320639). Originally posted by [throny](https://github.com/throny) on 2026-11-25T00:15:34Z. --- <!-- 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.
upstream-sync closed this pull request 2026-11-25 23:07:30 +00:00
CalvyPZ commented 2026-11-25 23:09:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by CalvyPZ on 2026-11-25T22:41:30Z.


This PR has been marked as a duplicate and closed after multiple agreeing checks in #1607. If you believe this is incorrect please re-open with why.

<!-- greyfinch-sync:ivall/xcl:comment:7176673539 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7176673539). Originally posted by [CalvyPZ](https://github.com/CalvyPZ) on 2026-11-25T22:41:30Z. --- This PR has been marked as a duplicate and closed after multiple agreeing checks in #1607. If you believe this is incorrect please re-open with why.
upstream-sync reopened this pull request 2026-11-26 00:04:29 +00:00
github-actions[bot] commented 2026-11-29 20:44:33 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T20:44:33Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7262321715 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7262321715). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T20:44:33Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 06:20:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:20:30Z.


Triage (#1607) – head d94e880

#1688 (draft, opened later) fixes the same #1488 problem differently. It zeroes world_to_object only for a zero or non-finite determinant or a non-finite inverse. This PR also rejects |det| <= 1e-30 and guards build_top, transform_aabb and Bvh::bounds.
bvh.rs:world_to_object: the 1e-30 cutoff also zeroes small transforms that can be inverted (the tiny case, scale 1e-10/1e-11, has a representable inverse that #1688 keeps). The added !is_singular && in build_top changes nothing, since an identity 3x3 is never singular.
bvh.rs:Bvh::bounds: main already handles an empty root (#1322, merged) by returning the inverted empty box ([inf], [-inf]). This PR returns a zero box, which would add the origin to any bounds merged with it. On rebase, keep main's version.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302761553 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1633#issuecomment-7302761553). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:20:30Z. --- **Triage (#1607)** – head d94e880 #1688 (draft, opened later) fixes the same #1488 problem differently. It zeroes world_to_object only for a zero or non-finite determinant or a non-finite inverse. This PR also rejects |det| <= 1e-30 and guards build_top, transform_aabb and Bvh::bounds. bvh.rs:world_to_object: the 1e-30 cutoff also zeroes small transforms that can be inverted (the `tiny` case, scale 1e-10/1e-11, has a representable inverse that #1688 keeps). The added `!is_singular &&` in build_top changes nothing, since an identity 3x3 is never singular. bvh.rs:Bvh::bounds: main already handles an empty root (#1322, merged) by returning the inverted empty box ([inf], [-inf]). This PR returns a zero box, which would add the origin to any bounds merged with it. On rebase, keep main's version. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1633:upstream-pr/1633'
git switch 'upstream-pr/1633'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/1633'
git switch 'upstream-pr/1633'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1633'
git switch 'upstream-pr/1633'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1633'
git switch 'main'
git merge --squash 'upstream-pr/1633'
git switch 'main'
git merge --ff-only 'upstream-pr/1633'
git switch 'main'
git merge 'upstream-pr/1633'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!1623
No description provided.