Skip to content

rdna2spirv: BVH box test as measured on RDNA2: NaN planes skipped, near/far by direction sign (SHADER-650) - #3567

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
controldebils:shader-650-bvh-nan-box
Nov 23, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
controldebils:shader-650-bvh-nan-box

Conversation

@controldebils

@controldebils controldebils commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

SHADER-650 (claim, finding). AI-assisted (Claude Code).

Depends on: #3373 (SHADER-635, empty boxes). This branch is #3373's head plus one commit, which replaces its separate empty-box check: the measured box test below rejects an empty box on its own.

What and why

The box step of image_bvh_intersect_ray / image_bvh64_intersect_ray followed RADV's software path: per-axis FMin/FMax of the two slab values, the inverse direction clamped to ±FLT_MAX, any NaN in min.x culling the child, and children sorted by their unclamped entry distance. Native RDNA2 does something else, measured on an AMD Radeon RX 6900 XT (gfx1030) through tools/hw_oracle raw kernels that write a node into memory and trace one ray:

  • Per axis, the near plane is min and the far plane max when the inverse direction is ≥ 0, swapped otherwise. No min/max of the two slab values, so an empty box (min > max) is a miss by itself.
  • t = (plane − origin) × inverse direction as given; 0 × inf is NaN. Both the subtraction and the multiply round toward zero (811 measured cases; round-to-nearest predicts 769). So an inverted box whose two planes round to the same t is entered, as @kacper-serewis's case is on hardware.
  • tnear = max of the three near t, tfar = min of the three far t, each skipping NaN. All three near or all three far NaN is a miss.
  • Hit when max(tnear, 0) ≤ tfar and tnear < extent.
  • Sort key max(tnear, 0) through the network (0,2) (1,3) (0,1) (2,3) (1,2) with a strict < swap.

So a lone NaN bound leaves its plane open (the cs_bvh scene's "inactive" box {NaN, 0, 0, 1, 1, 1} is entered at t = 0), and a box plane exactly on the origin along a zero-direction axis is open as well.

Changes

  • tools/rdna2spirv.py: the box branch implements the rule above (OpIsNan selects, no FMin/FMax on a NaN, no FClamp of the inverse direction), the slab subtraction and multiply round toward zero exactly (new f32_sub_rtz / f32_mul_rtz: TwoSum error for the subtraction, Dekker split of the mantissas scaled to [1, 2) for the product's error sign, one ulp back toward zero when the nearest result went away from zero, finite overflow to ±FLT_MAX), and the sort network is reordered. Triangle nodes are unchanged.
  • docs/design.md, Ray tracing row: the measured box rule; the descriptor base is measured, not an assumption; the cs_bvh words.
  • runtime/test/vk_compute_test.cpp, cs_bvh.asm, cs_bvh32.asm: the scene's box result is 0x68 0x48 0x40 0xffffffff, what native gfx1030 returns for that exact scene (it was 0x48 0x40 miss miss, the emulation's answer).
  • tests/semantic/test_image_bvh_box_hw.py (new): 241 of the measured cases as data, each run through the translator and compared with the hardware's four words, plus 50 measured rounding cases (11 ties including @kacper-serewis's, and the 39 randomized ones round-to-nearest gets wrong).

Tested

Hardware, AMD Radeon RX 6900 XT (gfx1030), AMD driver 26.11.2, Windows 11:

  • 338 native cases: the empty-box set (38, BOX32 and BOX16, two rays), single-NaN set (32), a NaN grid (172: every NaN position and the other bound of that axis on five sides, plus 12 multi-NaN patterns, on four rays including -z and a negative x direction), 88 tie orders of four children, 7 planes on the origin, and the cs_bvh scene. The 32-bit form with a non-zero descriptor base and the 64-bit form with base 0 gave the same words in all 71 cases where both ran.
  • 811 rounding cases: 800 randomized slabs with origins 2^18 to 2^27 away, where the last bit of t decides hit or miss, plus 11 ties. Round-to-nearest for both ops predicts 769, round-toward-zero for both 811.
  • The translator on this branch matches all 338/338 and 811/811 bit for bit (interpreter). main matches 43 of the 338, rdna2spirv: an empty BVH box is a miss, not a box that spans everything #3373 alone 66; rdna2spirv: an empty BVH box is a miss, not a box that spans everything #3373's translator gets 745 of the 800 randomized.
  • scripts/run_gpu_tests.py --filter bvh: cs_bvh, cs_bvh32 PASS with the new words. Full suite: 184/199 passed, 15 skipped, 0 failed (runtime built from this branch with MSVC Build Tools 2022).

CPU:

Not measured: the extent boundary itself, and gfx1013 (the console) or RDNA1. An independent RDNA2/RDNA1 run of test_image_bvh_box_hw.py's nodes through tools/hw_oracle would be welcome; @TheMrBeez's gfx1036 measurement of the cs_bvh NaN box agrees.

No game data, keys or decryption.

🤖 Generated with Claude Code

bobcatchris and others added 2 commits November 22, 2026 14:26
…ar/far by direction sign (SHADER-650)

Native image_bvh_intersect_ray on an RX 6900 XT (gfx1030, tools/hw_oracle, 338 cases) picks each axis's near
and far plane by the sign of the inverse direction, takes the inverse direction as given (0 x inf is NaN),
reduces the three near and the three far t with NaN skipped (all-NaN is a miss), and sorts by max(tnear, 0)
through the network (0,2) (1,3) (0,1) (2,3) (1,2). A lone NaN bound leaves its plane open and an empty box
misses without a separate check, so ItsNotRexed#3373's empty-box test is folded into this rule.

The translator now matches all 338 measured cases (main 43). cs_bvh/cs_bvh32 and vk_compute_test expect the
scene's NaN box at t = 0, as native gfx1030 returns. tests/semantic/test_image_bvh_box_hw.py holds 241 of the
measured cases. docs/design.md Ray tracing row updated; the descriptor base is measured too.

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

controldebils commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Cross-check on a second RDNA2 chip. @Vonsek posted nine native gfx1036 rows for this case in #3381 (comment) with the same rule this PR implements, found independently. I ran the same nine rows (box32, sorting on, including the dir x = -0.0 / inv -inf row and the origin-on-plane row) on the RX 6900 XT and through this branch's translator:

matches @Vonsek's gfx1036 words
native gfx1030 (RX 6900 XT, AMD 26.11.2) 9/9
this PR's translator (dc6068cc) 9/9
main's translator 0/9 (8 rows stop at GLSL.std.450 FMin of NaN (undefined) in the interpreter; the origin-on-plane row gives 40 48 - - because of the inv clamp)

So the box rule now has native results from gfx1030 and gfx1036, and the translator matches both. Thanks @Vonsek. AI-assisted (Claude Code).

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: dc6068cc merged onto main f847748c on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

  • scripts/run_gpu_tests.py: 182/199 passed, 15 skipped (plain main: 182/199 passed, 15 skipped).
  • Python tests this PR touches: 5 passed, 249 subtests passed.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At dc6068c, the new slab comparison can accept a finite inverted BOX32. With X bounds [1,0], Y and Z [0,67108864], origin (-33554432,0,0), direction (1,1,1), extent 67108864, the X distances round to the same float32. Interpreted SPIR-V from #3373 emitted four misses; #3567 returned four child IDs. The CPU reference misses. Retain ordered inversion detection and add this case, or qualify the contract based on native evidence. Native RDNA2 behavior for this input is unmeasured. Twelve focused tests and 80 other independent comparisons passed. AI-assisted CPU validation.

Native gfx1030 rounds both (plane - origin) and the multiply by the
inverse direction toward zero: 811 measured cases (800 randomized slabs
far from the origin plus 11 ties), round-to-nearest predicts 769,
round-toward-zero all 811. kacper-serewis's inverted box at
origin.x = -2^25 is a hit on hardware for this reason.

The translator emulates it exactly: TwoSum error for the subtraction,
a Dekker split of the scaled mantissas for the product's error sign,
one ulp back toward zero when the nearest result went away from zero,
finite overflow to +-FLT_MAX. 50 of the rounding cases are added to
test_image_bvh_box_hw.py.

AI-assisted (Claude Code).

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

Copy link
Copy Markdown
Contributor Author

Thanks @kacper-serewis, good catch on the rounding. I measured your case natively instead of guessing.

Your case is a hit on RDNA2. RX 6900 XT (gfx1030, AMD 26.11.2), tools/hw_oracle raw kernel, your exact node and ray (BOX32, X [1,0], Y/Z [0,2^26], origin (-2^25,0,0), direction (1,1,1), extent 2^26): the hardware returns the inverted child, not four misses. So ordered inversion detection would be wrong against native; an inverted box whose two planes round to the same t is entered.

That raised the real question, how the hardware rounds the slab distances, so I measured that too:

model of (plane − origin) × inv matches native, 811 cases
both round to nearest even (what dc6068c did) 769
both round toward zero 811

811 = 800 randomized slabs with origins 2^18 to 2^27 away (where the last bit decides hit or miss) plus 11 tie cases, yours among them.

So I made the translator exact (35f19f4): the subtraction and the multiply now round toward zero bit for bit in SPIR-V, using the exact error of each op (TwoSum for the subtraction, a Dekker split of the scaled mantissas for the product) and stepping one ulp toward zero when the nearest result went away from zero; finite overflow gives ±FLT_MAX.

  • 811/811 rounding cases and the earlier 338/338 match native bit for bit (interpreter).
  • tests/semantic/test_image_bvh_box_hw.py gains 50 of them as data (the 11 ties plus the 39 randomized ones where round-to-nearest gets it wrong). dc6068c fails 42 of those 50.
  • Your case is in there as a measured hit.

AI-assisted (Claude Code).

@im-a-cat

Copy link
Copy Markdown

Independent verification on NVIDIA GeForce RTX 4060 Ti (Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0, LEONIDA_W64_EMULATE=1):

Tested head 35f19f43 with a clean runtime build:

  • python -m unittest tests/semantic/test_image_bvh_box_hw.py: 2/2 passed (4.29s)
  • scripts/run_gpu_tests.py --filter cs_bvh:
    • PASS cs_bvh (RTX 4060 Ti, subgroup 32..32)
    • PASS cs_bvh32 (RTX 4060 Ti, subgroup 32..32)
  • Full GPU test suite (scripts/run_gpu_tests.py):
    • 184/199 passed, 15 skipped, 0 failed (identical to main baseline)
    • 204/204 test shaders pass spirv-val

Both BVH compute tests pass cleanly on 32-wide subgroups under wave64 emulation.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants