Skip to content

tests/semantic: pin image_bvh_intersect_ray treating a box child with min > max as a miss; cs_bvh_empty_box fixture - #3366

Open
kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:kap-bvh-emptybox
Open

kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:kap-bvh-emptybox

Conversation

@kapitche

@kapitche kapitche commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Main now treats a box child with min > max as a miss (measured on hardware there), so this PR no longer changes the translator. It is rebased to the tests only.

What it adds. tests/semantic/test_image_bvh_empty_box.py: hand-built box32 and box16 nodes, a hit control, an all-axes-empty child, an x-only-empty child; asserts the four returned child ids. The cs_bvh_empty_box GPU fixture, and one sentence in the Ray tracing row of docs/design.md.

Tests. Python 3.12.16, Linux (WSL2): the BVH semantic tests pass (14 OK). cs_bvh_empty_box passes 1/1 on the RTX 4080 SUPER and on the Intel UHD 770 (Windows 11).

AI-assisted.

@TheMrBeez

Copy link
Copy Markdown

Hardware check for SHADER-635: real RDNA2 treats an empty box as a miss, and this PR matches it on two GPUs. AI-assisted run, synthetic BVH only, no game files.

Nobody had measured the instruction itself yet (the reference and this fix both follow the console code), so I ran image_bvh64_intersect_ray natively on an AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02) through tools/hw_oracle. The kernel writes one box32 node (Mesa's layout: four child ids, then four min xyz, max xyz boxes) into the oracle's buffer and points the descriptor at it (s7 = 0x81000000, box sorting on, as in cs_bvh.asm):

  • child 0x40: min (0,0,1), max (1,1,2), the hit control
  • child 0x48: min (+inf,+inf,+inf), max (−inf,−inf,−inf), the builder's inactive marker
  • child 0x50: empty on z only, min.z 6 > max.z 4
  • child 0x58: empty on x only, min.x 1 > max.x 0

Ray A: origin (0.25, 0.25, 0), direction +z. Ray B: origin (0.25, 0.25, 10), direction −z, the negative-direction case from @jaybeebabageyus's claim. Inverse direction (inf, inf, ±1), t max 100.

ray A (+z) ray B (−z)
gfx1036 hardware (wave32 and wave64) 40 ffffffff ffffffff ffffffff 40 ffffffff ffffffff ffffffff
main f847748c, translated, gfx1036 iGPU and RTX 5080 (621.01) 48 40 58 50 48 50 58 40
this PR 56678952, translated, both GPUs 40 ffffffff ffffffff ffffffff ✓ 40 ffffffff ffffffff ffffffff ✓

So the hardware rejects all three kinds of empty box in both directions, including a box that is empty on one axis only. main returns every child, even the z-only empty box for the +z ray. The translated shader is a hand-written compute version of the same node and rays (written into the heap, descriptor base 0 as in cs_bvh.asm), and it passes spirv-val.

Also at this head: tests/semantic/test_image_bvh_empty_box.py OK, and run_gpu_tests.py --filter bvh (cs_bvh, cs_bvh32) passes on both GPUs. The docs/design.md Ray tracing row could cite this as a hardware measurement on gfx1036.

@TheMrBeez

TheMrBeez commented Nov 22, 2026 •

Copy link
Copy Markdown

Follow-up on NaN bounds, since this PR keeps the old NaN behaviour: measured natively on the same gfx1036, a box with a single NaN bound that the ray passes through is a hit in every position I tried (min.x, min.y, min.z, max.x, max.y, max.z). An all-NaN box is a miss. main/this PR reject four of the six single-NaN positions (min.x, min.y, max.x, max.y) and keep the other two (min.z, max.z). cs_bvh.asm's "inactive (NaN) box" {NAN, 0, 0, 1, 1, 1} is a hit on the hardware. Data and tables in #3373: #3373. The empty-box (min > max) part of this PR matches the hardware, as reported above. (AI-assisted)

@kapitche

kapitche commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Added a GPU fixture, cs_bvh_empty_box (compute, existing "dump" mode, so no C++ change). Lane 0 writes a box32 node into the heap: child 0 empty on every axis, child 1 the unit box, child 2 empty on x only, child 3 empty on z only. It runs image_bvh64_intersect_ray with a +z ray and a -z ray (sorting off). Expected: only child 1 comes back, ffffffff 12 ffffffff ffffffff twice (hex).

with this PR without it (parent translator)
RTX 4080 SUPER PASS FAIL, all four children returned: 0x11 0x12 0x13 0x14 twice
UHD 770 PASS FAIL, same

Run with scripts/run_gpu_tests.py --filter cs_bvh_empty_box (Windows, Python 3.13); check.py also reports ok on both. The GPU agrees with the interpreter. AI-assisted.

@davidesidoti

Copy link
Copy Markdown

Tested #3366 (56678952) and #3373 (fade4e4b), both on main f847748c, on AMD Radeon RX 9070 XT (RDNA4), Adrenalin 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11 build 26300, llvm-mc 21.1.8: pass for both.

This adds an RDNA4 run of the translated code only; the native-hardware data, including the open NaN-bound question, is in the comments by TheMrBeez above.

(AI-assisted test run.)

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE) with a raw hw_oracle kernel: image_bvh64_intersect_ray v[20:23], [...], s[4:7] on a hand-built box32 root node (the layout of cs_bvh.asm, nodes in the kernel's code after s_endpgm, descriptor base = that address >> 8, box_sort_en set like the fixture, ray origin (0.5, 0.5, -1), direction +z, inverse direction (inf, inf, 1), extent 100). Child ids 0x11..0x14; child 0 = [0,1]^3 (hit), child 1 = the box under test, children 2 and 3 = [5,6]^3 (misses). The four returned words:

child 1 box (min)-(max) hardware
control: miss box 11 ffffffff ffffffff ffffffff
z-inverted only (0,0,2)-(1,1,1) 11 ffffffff ffffffff ffffffff
x-inverted only (1,0,0)-(0,1,1) 11 ffffffff ffffffff ffffffff
y-inverted only (0,1,0)-(1,0,1) 11 ffffffff ffffffff ffffffff
all inverted (1,1,1)-(0,0,0) 11 ffffffff ffffffff ffffffff
all inverted around the ray (1,1,3)-(0,0,-2) 11 ffffffff ffffffff ffffffff
zero thickness in z (0,0,0.5)-(1,1,0.5) 11 12 ffffffff ffffffff
zero thickness in x (0.5,0,0)-(0.5,1,1) 11 12 ffffffff ffffffff
NaN in min.x, max.x, min.y, max.y or max.z 11 12 ffffffff ffffffff
NaN in min.z 12 11 ffffffff ffffffff

So the hardware culls an empty box (min > max on any one axis, or all three) and keeps a zero-thickness one, as this PR does. I ran the same 13 boxes through this PR's translation (semantic interpreter, support.translate_lines + mrt0_words, same ray and ids): every inverted and zero-thickness-in-z case gives the hardware's words. Two differences remain, neither caused by this PR: the interpreter returns a miss for the zero-thickness-in-x box when the ray sits exactly on its plane (x = 0.5, direction x = 0, so a 0 * inf slab term; the hardware returns a hit), and a NaN bound cannot be run in the interpreter at all (FMin of NaN); on the hardware a NaN anywhere in the bounds is a hit, not an inactive child. That also differs from cs_bvh.asm, whose fourth box has a NaN min.x and expects a miss: running that same BVH (root at 0x100, ray (0.25, 0.25, 0)) on gfx1036 returned 68 48 40 ffffffff (the NaN box first), not 48 40 ffffffff ffffffff. The kernel is local, not pushed. AI-assisted.

@Kam471

Kam471 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 34418fed21f97f041509b0d6a380f6a21808ef79 on Windows 11, NVIDIA GeForce RTX 5070 Ti (desktop, 16 GB), driver 621.24, Vulkan 1.4.357.

  • python -I scripts/run_gpu_tests.py --build ... --filter bvh -> 3/3 passed, 0 skipped, 0 failed (cs_bvh, cs_bvh32, cs_bvh_empty_box); translator 3 shaders, 3 pass spirv-val. The empty-box fixture returned 4294967295 18 4294967295 4294967295 4294967295 18 4294967295 4294967295.
  • python tests/semantic/test_image_bvh_empty_box.py -> 2 tests OK (without -I).

Nit: python -I tests/semantic/test_image_bvh_empty_box.py fails with ModuleNotFoundError: No module named support because -I removes tests/semantic from sys.path; the usual support bootstrap would fix the mandated invocation. No game files.

@bielfreedom

Copy link
Copy Markdown

Tested 34418fed (on its base f847748c) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, MSVC build of runtime/, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass.

  • cs_bvh_empty_box: PASS 4294967295 18 4294967295 4294967295 4294967295 18 4294967295 4294967295. On an AMD GPU this is the console's own BVH instruction family, so it's a useful data point for this rule.
  • scripts/run_gpu_tests.py: 205 shaders, 205 translated, 205 pass spirv-val; 186/200 passed, 14 skipped, 0 failures.
  • Full ctest: 129/129, 0 validation errors. tests/semantic: 7324 OK (1 expected failure).

AI-assisted (Claude Code). No game files.

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested 34418fe on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 186/200 passed, 14 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base f847748 on the same machine, every shared test has the same result, and the test this PR adds (cs_bvh_empty_box) passes on both GPUs.
  • python -m unittest discover -s tests/semantic: 7324 run, OK (expected failures=1)

No game files. AI-assisted.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 34418fe merged into main f847748c (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New test passes natively: cs_bvh_empty_box (4294967295 18 4294967295 4294967295 4294967295 18 4294967295 4294967295).
  • Whole run_gpu_tests.py: 186/200 native, 186/200 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 41/41.
  • tests/semantic/test_image_bvh_empty_box.py: 2 passed, 2 subtests passed.

No game files. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor Author

Rebased onto main aa2988c; conflicts in runtime/test/shaders/check.py and scripts/run_gpu_tests.py (main added cs_w32_andn2_exec and other fixture names to the same lists), kept main's lists and added cs_bvh_empty_box; tests: tests/semantic 7458 run (only the 10 failures that also fail on main), test_image_bvh_empty_box 2 OK, test_run_gpu_tests, test_run_gpu_tests_paths, test_shader_checks, test_docs_* OK.

@oVolkra

oVolkra commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 11dd7c2 (merged onto main aa2988c) on RTX 3060 (Ampere), driver 617.14, Windows 11, MSVC 14.44, RelWithDebInfo: pass

  • ctest: 153/153 pass, also with LEONIDA_VK_VALIDATION=1 (153/153).
  • scripts/run_gpu_tests.py: native 165/211 (46 skipped), with LEONIDA_W64_EMULATE=1 196/211 (15 skipped). Same as main at aa2988c. The PR adds one GPU fixture, so one more test than main.
  • Eight *_test.exe programs that ctest does not register (d3d12_clear, tiny_game_exit_code, vk_bindless, vk_compute, vk_sampler, vk_store, vk_texture, vk_xdxr_build) were run by hand, with and without validation: same results as on main.
  • No game files used.

AI-assisted: this check was run and written with Claude Code (Anthropic) on my machine; the numbers above are its unedited output.

@kapitche kapitche changed the title rdna2spirv: image_bvh_intersect_ray treats a box child with min > max as a miss tests/semantic: pin image_bvh_intersect_ray treating a box child with min > max as a miss; cs_bvh_empty_box fixture Nov 23, 2026
@junaydirfan

Copy link
Copy Markdown

Tested c74d3eb5 merged into main 98fa503f on Linux (CachyOS, kernel 7.3.5), llvm-mc 23.1.5, LEONIDA_W64_EMULATE=1, test programs built from main (this PR changes no C++): pass.

GPU driver run_gpu_tests.py main
Intel Raptor Lake-S iGPU (Xe-LP), subgroup 8..32 Mesa ANV 26.3.1 187/218 passed, 31 skipped, 0 failed 186/217
RTX 4080 Laptop GPU (Ada), subgroup 32 NVIDIA 620.73.04 202/218 passed, 16 skipped, 0 failed 201/217

New in this PR:

  • cs_bvh_empty_box: Intel PASS, 4294967295 18 4294967295 4294967295 4294967295 18 4294967295 4294967295; RTX 4080 PASS, 4294967295 18 4294967295 4294967295 4294967295 18 4294967295 4294967295

Every other test has the same result as on main on both GPUs.

One earlier Intel run of this PR crashed the runner while the machine was busy with other test suites. Three reruns on an idle GPU were clean, as were three main runs alongside, so I don't think it's this PR.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested c74d3eb596a3792c2ba2547f201089d911bdde30 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

The stock synthetic cs_bvh_empty_box fixture passed. A one-ULP inversion of the Z bounds exposed a directional discrepancy on the translated Vulkan path. I varied only child 3, keeping its X/Y bounds at [0,1] and maxZ at 1.0. With the fixture’s original rays, child 3 returned:

minZ (binary32 bits) From z = −1 toward +Z From z = 2 toward −Z
0x3f800000 (equal bounds) hit 0x14 hit 0x14
0x3f800001 (one ULP inverted) hit 0x14 miss
0x3f800002 (two ULPs inverted) miss miss

The one-ULP case repeated on a second run. Moving just the forward ray origin from z = −1 to z = 0 gave two misses for that same box. The other three children kept their expected results throughout. This is consistent with binary32 rounding: subtracting −1 rounds both one-ULP-case endpoints to 2.0. A native gfx1036 comparison using those exact bound words and origins would distinguish an ISA behavior from a translation discrepancy.

AI-assisted verification.

@undeadhuman37

Copy link
Copy Markdown
Contributor

Tested c74d3eb5 merged into main 98fa503f on NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 10.0.26200, Vulkan SDK 1.4.363.1, test programs from the existing runtime/build (this PR changes no C++): pass.

  • scripts/run_gpu_tests.py --filter cs_bvh_empty_box: PASS, words 0xFFFFFFFF 0x12 0xFFFFFFFF 0xFFFFFFFF 0xFFFFFFFF 0x12 0xFFFFFFFF 0xFFFFFFFF, as registered in both runners.
  • tests/semantic/test_image_bvh_empty_box.py, tests/test_run_gpu_tests.py, tests/test_shader_checks.py: OK.

AI-assisted (Claude Code).

@rhtnxe

rhtnxe commented Nov 23, 2026

Copy link
Copy Markdown

Tested c74d3eb5 merged into main 98fa503f (clean merge) on Apple M4 (Mac16,13, 10-core GPU, subgroup 4..32), MoltenVK 1.4.2 (Homebrew, loaded directly), macOS 27.2.1, llvm-mc 23.1.6, test programs built from main (this PR changes no C++): pass. First Apple/MoltenVK result here.

  • cs_bvh_empty_box: PASS 4294967295 18 4294967295 4294967295 4294967295 18 4294967295 4294967295, the same words as the other GPUs.
  • python3 -I -m unittest discover -s tests/semantic -p 'test_image_bvh_empty_box.py': 2 OK (box16, box32); all *bvh* semantic tests OK.
  • Full scripts/run_gpu_tests.py: 222 shaders, 221 translated, 221 pass spirv-val (ps_ij_computed is the expected PerVertexKHR skip on MoltenVK); 153/218 passed, 58 skipped, 7 failed. main on the same machine: 152/217, 58 skipped, the same 7 failures (cs_fma_fused, ps_ssharp and the five ps_vcmp* f16/NaN ones, the known MoltenVK 1.4.2 set in GPU test results (one comment per GPU and driver) #166). The only difference is the new cs_bvh_empty_box PASS.

I didn't run the one-ULP Z-bounds variant from the RTX 5090 comment. Synthetic fixture only, no game data. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor Author

Thanks for the one-ULP table. This PR only pins boxes whose min exceeds max by a wide margin (the fixture's empty children), measured on hardware in the earlier work it builds on, and its four asserted child ids are unchanged by your result. The one-ULP inversion is a different question: whether hardware or the translated path rounds the origin subtraction differently. I have no gfx1036 on this machine (only an RTX 4080 SUPER and an Intel UHD 770), so I have not reproduced your table and will not claim either side. If someone with a gfx1036 runs the three minZ words from the two origins through hw_oracle, that would settle it and could become its own small fixture; I will leave this PR as it is.

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.