Skip to content

rdna2spirv: BVH box slab flushes subnormals to zero, as measured on RDNA2 (SHADER-650 follow-up) - #5887

Open
controldebils wants to merge 1 commit into
ItsNotRexed:mainfrom
controldebils:shader-650-subnormal-ftz
Open

controldebils wants to merge 1 commit into
ItsNotRexed:mainfrom
controldebils:shader-650-subnormal-ftz

Conversation

@controldebils

@controldebils controldebils commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #3567 (SHADER-650, merged): its round-toward-zero helpers turned a box slab product that underflows to zero into NaN, found by @tupacfan04 on #4957 (review). Measuring that case natively showed the wider rule: RDNA2 flushes subnormals to zero in the box test, and the translator kept them. AI-assisted (Claude Code).

What and why

f32_mul_rtz stepped one ulp toward zero whenever the significand product rounded up and the product was finite. For two normal operands whose product underflows to ±0, that step subtracts one from the bit pattern of zero: +0 becomes 0xFFFFFFFF (NaN). A NaN slab value is skipped, so the box is entered where the hardware misses it.

Measured on an AMD Radeon RX 6900 XT (gfx1030), AMD driver 26.11.2, Windows 11, tools/hw_oracle raw kernels: 21 BOX32 cases, each built so that keeping vs flushing (or NaN vs zero) changes the four returned words.

case hardware main
product of two normals that underflows to 0, on the far plane (0x00800800 * 0x00800801) t = 0: miss NaN, skipped: hit
subnormal or underflowed product on the far plane (4 rows) acts as 0 kept
subnormal box bound, minus origin 0 or 1 (6 rows) acts as 0 kept
subnormal inverse direction (3 rows) acts as 0 kept
subnormal ray origin (2 rows) acts as 0 kept
normal controls (−FLT_MIN bound, FLT_MIN origin, −1e-30, exact 0) (5 rows) unchanged match

Changes

  • tools/rdna2spirv.py: new _f32_ftz (a subnormal becomes a zero of the same sign) and _f32_normal. f32_sub_rtz and f32_mul_rtz flush both inputs and their result. f32_mul_rtz steps toward zero only when the product is normal, so a stepped zero can't happen.
  • tests/semantic/test_image_bvh_box_hw.py: the 21 cases as SUBNORMAL, with all four measured words.
  • docs/design.md, Ray tracing row: subnormals are flushed in every input and in both results.

Tested

Not measured: subnormal extents, BOX16 (its f16 bounds widen to normal f32 values), and triangles. No game data, keys or decryption.

🤖 Generated with Claude Code

…DNA2 (SHADER-650 follow-up)

ItsNotRexed#3567's round-toward-zero product stepped a product that underflows to
zero into NaN (found by tupacfan04 on ItsNotRexed#4957). Measured on an RX 6900 XT
(gfx1030, tools/hw_oracle, 21 BOX32 cases): the hardware flushes
subnormal box bounds, origins, inverse directions and both slab results
to zero, keeping the sign. f32_sub_rtz and f32_mul_rtz now flush their
inputs and results and step toward zero only for a normal product. The
21 cases are in test_image_bvh_box_hw.py as SUBNORMAL; main fails 16 of
them.

AI-assisted (Claude Code).

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

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1894146 on Intel UHD Graphics 630 (CFL GT2), Mesa ANV, subgroup 8..32: run_gpu_tests.py --filter bvh prints "PASS cs_bvh" and "PASS cs_bvh32" and "2/2 passed". tests/semantic test_image_bvh_box_hw runs 3 tests OK, and unittest discover -s tests -p "test_image_bvh*.py" runs 15 tests OK. With tools and tests/semantic from 561b65a through LEONIDA_TOOLS_DIR, test_subnormals_flush_to_zero fails 15 subtests ("FAILED (failures=15)"); the description says 16. The RDNA2 measurements behind the change were not reproduced, and the full tests/semantic suite was not run.

AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1894146 on AMD RADV RAPHAEL_MENDOCINO (Ryzen 9 9950X3D iGPU), subgroup 32..64: run_gpu_tests.py --filter bvh prints "PASS cs_bvh" and "PASS cs_bvh32" and "2/2 passed"; with LEONIDA_VK_DEVICE=NoSuchDevice both fail with "no Vulkan physical device matches" (0/2). tests/semantic test_image_bvh_box_hw runs 3 tests OK and discover -s tests/semantic -p "test_image_bvh*.py" runs 17 tests OK. These two GPU fixtures and the semantic tests do not execute a kernel with a subnormal box bound, so the RX 6900 XT subnormal rows in the description were not measured on this iGPU.

AI-assisted.

@Vyral1978

Copy link
Copy Markdown
Contributor

Tested 1894146 merged onto main 561b65a on NVIDIA RTX 3070 (driver 32.0.16.1088), Windows 11, MSVC 19.51.36243 (VS 18), Vulkan SDK 1.4.363.1: pass

  • Red before: this PR's test_image_bvh_box_hw.py against main's rdna2spirv.py fails 15 of the 21 SUBNORMAL subtests, not 16. "negative subnormal inverse direction 0x80000005" already passes on main.
  • With the PR: python -m unittest discover -s tests/semantic -p "test_image_bvh*.py" 17/17 OK; the full semantic suite runs 6151 tests, OK (4 skipped, 1 expected failure).
  • python -I scripts/run_gpu_tests.py --filter bvh: 2/2 translated, 2/2 spirv-val, cs_bvh and cs_bvh32 PASS on the RTX 3070 (subgroup 32), same as main.
  • Full ctest -j 4 --timeout 300: 237/238, identical to main (d3d12_indirect_dispatch_count also fails on main).

AI-assisted (Claude Code)

@dandasma

Copy link
Copy Markdown
Contributor

Tested 1894146d (on main 561b65a) on an AMD Radeon RX 9070 (RDNA4, discrete), AMD Windows driver 26.10.1, Windows 11, Python 3.12.10: no regression.

  • Full run_gpu_tests.py: 216/232, 16 skipped, the same tests and skips as main. --filter bvh: cs_bvh and cs_bvh32 pass in three runs.
  • The PR changes the translation of exactly those two modules (every other module is byte-identical to main's). But cs_bvh/cs_bvh32 also pass with main's tools/rdna2spirv.py, so their inputs don't reach a subnormal slab: on this card the GPU run shows that the new flush doesn't break normal boxes, not the flush itself.
  • test_image_bvh_box_hw (via unittest discover -s tests/semantic): 3 tests OK.

AI-assisted (Claude Code).

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.

4 participants