BVH sorted box hits: absent child can sort before a valid hit (reproducer and patch) #173

Open
opened 2026-11-26 03:37:32 +00:00 by github-bot · 2 comments
Owner

@⁠Bennet0k on GitHub · 2026-11-24 22:09 UTC · original

In the CPU BVH reference (bvh::boxes) and the matching DXIL emitter (emit/intersect.rs), an absent child (0xffffffff) can get a finite sort key when its stored bounds pass the slab test. The ID is still NONE, but it can sort ahead of a valid hit. This is a candidate correctness fix for review; I don't know whether the console guarantees disabled bounds for every absent child.

Small reproducer. Use a ray from (0,0,0) along +Z with extent 10. Slot 0 has ID NONE and bounds (-1,-1,1)..(1,1,2). Slot 1 has ID 8 and bounds (-1,-1,3)..(1,1,4). Leave the other slots absent. With sorting enabled the current logic can put NONE before 8. The patch gives absent children the same infinite distance as misses, so the result is [8,NONE,NONE,NONE]. The unsorted path keeps the original slot order. In the emitter the unsigned comparison child < 0xffffffff is equivalent to child != NONE.

Verification and limits. In a standalone model of the sorting network, all 24 permutations of four distinct finite distances were crossed with all 16 presence masks. The old logic disagreed with the valid-hits-first expectation in 264/384 cases; the patched model agreed in all 384. These are synthetic cases, not measured game frequencies. git apply --check passed on the source files used to prepare this patch. The included Rust test has not been executed and the emitted DXIL has not been run on hardware. This was found by AI-assisted source review.

What would establish relevance. A GPU parity test with the same fixture, or a capture counting absent slots with intersecting bounds preceding real hits, would distinguish a live case from defensive handling. It matters to traversal if the consumer stops at the first NONE. I have no evidence this explains the builder fault in #16, black lighting, or any frame-rate loss.

Complete patch (CPU reference, DXIL emitter, and regression test):

--- a/crates/victory-shader/src/bvh.rs
+++ b/crates/victory-shader/src/bvh.rs
@@ -197,7 +197,7 @@
         let enter = near[0].max(near[1]).max(near[2]);
         let leave = far[0].min(far[1]).min(far[2]);
         // A box whose minimum x is NaN is switched off.
-        if !b[0].is_nan() && leave >= enter.max(0.0) && enter < ray.extent {
+        if children[n] != NONE && !b[0].is_nan() && leave >= enter.max(0.0) && enter < ray.extent {
             ids[n] = children[n];
             distances[n] = enter;
         }
@@ -385,6 +385,20 @@
         // Outward rounding of the test helper.
         assert!(half_to_f32(f32_to_half(0.1, true)) >= 0.1);
         assert!(half_to_f32(f32_to_half(0.1, false)) <= 0.1);
+    }
+
+    #[test]
+    fn sorted_boxes_put_absent_children_after_real_hits() {
+        let bounds = [
+            [-1.0, -1.0, 1.0, 1.0, 1.0, 2.0],
+            [-1.0, -1.0, 3.0, 1.0, 1.0, 4.0],
+            [0.0; 6],
+            [0.0; 6],
+        ];
+        let ids = [NONE, 8, NONE, NONE];
+        let ray = Ray::new([0.0; 3], [0.0, 0.0, 1.0], 10.0);
+        assert_eq!(boxes(&ids, &bounds, &ray, true), [8, NONE, NONE, NONE]);
+        assert_eq!(boxes(&ids, &bounds, &ray, false), ids);
     }
 
     #[test]
--- a/crates/victory-shader/src/emit/intersect.rs
+++ b/crates/victory-shader/src/emit/intersect.rs
@@ -403,6 +403,8 @@
             let on = self.s.m.bin(c, Bin::Xor, off, on);
             let hit = self.s.m.bin(c, Bin::And, through, before);
             let hit = self.s.m.bin(c, Bin::And, hit, on);
+            let present = self.s.m.cmp(c, pred::IULT, children[n], none);
+            let hit = self.s.m.bin(c, Bin::And, hit, present);
             ids[n] = self.s.m.select(c, hit, children[n], none);
             distances[n] = self.s.m.select(c, hit, enter, infinity);
         }
**@⁠Bennet0k** on GitHub · 2026-11-24 22:09 UTC · [original](https://github.com/fxceless/victory-fork/issues/173) In the CPU BVH reference (`bvh::boxes`) and the matching DXIL emitter (`emit/intersect.rs`), an absent child (`0xffffffff`) can get a finite sort key when its stored bounds pass the slab test. The ID is still NONE, but it can sort ahead of a valid hit. This is a candidate correctness fix for review; I don't know whether the console guarantees disabled bounds for every absent child. **Small reproducer.** Use a ray from (0,0,0) along +Z with extent 10. Slot 0 has ID NONE and bounds (-1,-1,1)..(1,1,2). Slot 1 has ID 8 and bounds (-1,-1,3)..(1,1,4). Leave the other slots absent. With sorting enabled the current logic can put NONE before 8. The patch gives absent children the same infinite distance as misses, so the result is [8,NONE,NONE,NONE]. The unsorted path keeps the original slot order. In the emitter the unsigned comparison `child < 0xffffffff` is equivalent to `child != NONE`. **Verification and limits.** In a standalone model of the sorting network, all 24 permutations of four distinct finite distances were crossed with all 16 presence masks. The old logic disagreed with the valid-hits-first expectation in 264/384 cases; the patched model agreed in all 384. These are synthetic cases, not measured game frequencies. `git apply --check` passed on the source files used to prepare this patch. The included Rust test has not been executed and the emitted DXIL has not been run on hardware. This was found by AI-assisted source review. **What would establish relevance.** A GPU parity test with the same fixture, or a capture counting absent slots with intersecting bounds preceding real hits, would distinguish a live case from defensive handling. It matters to traversal if the consumer stops at the first NONE. I have no evidence this explains the builder fault in #16, black lighting, or any frame-rate loss. **Complete patch** (CPU reference, DXIL emitter, and regression test): ```diff --- a/crates/victory-shader/src/bvh.rs +++ b/crates/victory-shader/src/bvh.rs @@ -197,7 +197,7 @@ let enter = near[0].max(near[1]).max(near[2]); let leave = far[0].min(far[1]).min(far[2]); // A box whose minimum x is NaN is switched off. - if !b[0].is_nan() && leave >= enter.max(0.0) && enter < ray.extent { + if children[n] != NONE && !b[0].is_nan() && leave >= enter.max(0.0) && enter < ray.extent { ids[n] = children[n]; distances[n] = enter; } @@ -385,6 +385,20 @@ // Outward rounding of the test helper. assert!(half_to_f32(f32_to_half(0.1, true)) >= 0.1); assert!(half_to_f32(f32_to_half(0.1, false)) <= 0.1); + } + + #[test] + fn sorted_boxes_put_absent_children_after_real_hits() { + let bounds = [ + [-1.0, -1.0, 1.0, 1.0, 1.0, 2.0], + [-1.0, -1.0, 3.0, 1.0, 1.0, 4.0], + [0.0; 6], + [0.0; 6], + ]; + let ids = [NONE, 8, NONE, NONE]; + let ray = Ray::new([0.0; 3], [0.0, 0.0, 1.0], 10.0); + assert_eq!(boxes(&ids, &bounds, &ray, true), [8, NONE, NONE, NONE]); + assert_eq!(boxes(&ids, &bounds, &ray, false), ids); } #[test] --- a/crates/victory-shader/src/emit/intersect.rs +++ b/crates/victory-shader/src/emit/intersect.rs @@ -403,6 +403,8 @@ let on = self.s.m.bin(c, Bin::Xor, off, on); let hit = self.s.m.bin(c, Bin::And, through, before); let hit = self.s.m.bin(c, Bin::And, hit, on); + let present = self.s.m.cmp(c, pred::IULT, children[n], none); + let hit = self.s.m.bin(c, Bin::And, hit, present); ids[n] = self.s.m.select(c, hit, children[n], none); distances[n] = self.s.m.select(c, hit, enter, infinity); } ```
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:33 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-24 22:58 UTC · original

Independent CPU differential on exact refs. I used source-built packed 16-bit and 32-bit nodes, an intersecting NONE slot, valid IDs 0 and 0xffff_fffe, strict extent, and equal-distance hits with a nearer hit ahead of them.

  • Main 2119b0e failed the two sorted-hit checks. The intersecting NONE could precede valid hits.
  • #213 (1625a12), closed #217 (f446030), and #230 (6354761) passed both checks and the all-sentinel, all-zero and unsorted controls. All three emitter diffs add the same unsigned sentinel predicate in the common box path.

This confirms the overlap. #213 also includes emitted-DXIL parity tests for both node widths and both addressing forms. I ran the CPU differential locally. Its Windows D3D12 results are in the independent reports on #213.

**@⁠avt0m4t48** on GitHub · 2026-11-24 22:58 UTC · [original](https://github.com/fxceless/victory-fork/issues/173#issuecomment-7146871008) Independent CPU differential on exact refs. I used source-built packed 16-bit and 32-bit nodes, an intersecting `NONE` slot, valid IDs `0` and `0xffff_fffe`, strict extent, and equal-distance hits with a nearer hit ahead of them. - Main `2119b0e` failed the two sorted-hit checks. The intersecting `NONE` could precede valid hits. - #213 (`1625a12`), closed #217 (`f446030`), and #230 (`6354761`) passed both checks and the all-sentinel, all-zero and unsorted controls. All three emitter diffs add the same unsigned sentinel predicate in the common box path. This confirms the overlap. #213 also includes emitted-DXIL parity tests for both node widths and both addressing forms. I ran the CPU differential locally. Its Windows D3D12 results are in the independent reports on #213.
Author
Owner

@⁠FordMustang0288 on GitHub · 2026-11-25 19:36 UTC · original

Independent GPU parity result for the fixture this issue asks for, on different silicon and driver from #213 — offered as a second datapoint, not a duplicate fix.

Result: the four parity tests pass with the emitted DXIL running on hardware, over both node widths, both addressing forms (resource table and title memory), and both sort modes:

  • 4 passed, 0 failed; DXIL validated by Microsoft's dxil.dll
  • selected adapter read from the device LUID: NVIDIA GeForce RTX 3080 10GB, vendor 0x10de, device 0x2206, software=false
  • 337 cases per run, five hardware completions
  • the runner also asserts that no DXGI: Hiding actual GPU masking appears in the log, so the reported adapter cannot be a substitution

Coverage in the regression itself goes slightly past the original patch: both packed box widths, absent children with finite intersecting bounds (not just switched-off bounds), the boundary child ids 0 and 0xffff_fffe, and all-absent nodes, in sorted and unsorted modes.

Two notes that may be useful for review:

DXVK's WARP adapter is not real software coverage here. EnumWarpAdapter is unsupported on this setup and returns the first hardware adapter, so adapter=warp completion lines carry software=false and mean the same 3080 ran again. I assert on the LUID and the software flag rather than on the label — worth knowing if other reports cite WARP results under Wine.

The production path never validates emitted DXIL. crates/victory-gpu/src/pipeline/host.rs builds D3D12_COMPUTE_PIPELINE_STATE_DESC and calls CreateComputePipelineState directly; the only reference to the validator in that file is inside #[cfg(test)]. So a bad module surfaces as E_INVALIDARG at pipeline creation with no shader-level diagnosis — the log says "shader translated" because the translator finished, not because the bytes were accepted. I hit exactly that with four compute programs in the game world and only worked out later that validation had never run. A switch to validate in production, or just counting validation failures, would turn that class into a named hash.

Implementation here overlaps #213, so I am deferring to it and have not opened a competing PR.

**@⁠FordMustang0288** on GitHub · 2026-11-25 19:36 UTC · [original](https://github.com/fxceless/victory-fork/issues/173#issuecomment-7172531882) Independent GPU parity result for the fixture this issue asks for, on different silicon and driver from #213 — offered as a second datapoint, not a duplicate fix. **Result: the four parity tests pass with the emitted DXIL running on hardware**, over both node widths, both addressing forms (resource table and title memory), and both sort modes: - 4 passed, 0 failed; DXIL validated by Microsoft's `dxil.dll` - selected adapter read from the device LUID: `NVIDIA GeForce RTX 3080 10GB`, vendor `0x10de`, device `0x2206`, `software=false` - 337 cases per run, five hardware completions - the runner also asserts that no `DXGI: Hiding actual GPU` masking appears in the log, so the reported adapter cannot be a substitution Coverage in the regression itself goes slightly past the original patch: both packed box widths, absent children with finite intersecting bounds (not just switched-off bounds), the boundary child ids `0` and `0xffff_fffe`, and all-absent nodes, in sorted and unsorted modes. Two notes that may be useful for review: **DXVK's WARP adapter is not real software coverage here.** `EnumWarpAdapter` is unsupported on this setup and returns the first hardware adapter, so `adapter=warp` completion lines carry `software=false` and mean the same 3080 ran again. I assert on the LUID and the software flag rather than on the label — worth knowing if other reports cite WARP results under Wine. **The production path never validates emitted DXIL.** `crates/victory-gpu/src/pipeline/host.rs` builds `D3D12_COMPUTE_PIPELINE_STATE_DESC` and calls `CreateComputePipelineState` directly; the only reference to the validator in that file is inside `#[cfg(test)]`. So a bad module surfaces as `E_INVALIDARG` at pipeline creation with no shader-level diagnosis — the log says "shader translated" because the translator finished, not because the bytes were accepted. I hit exactly that with four compute programs in the game world and only worked out later that validation had never run. A switch to validate in production, or just counting validation failures, would turn that class into a named hash. Implementation here overlaps #213, so I am deferring to it and have not opened a competing PR.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/fxceless--victory-fork#173
No description provided.