Skip to content

startup: lower the FP32 box case of image_bvh_intersect_ray - #2922

Merged
R0Y00 merged 4 commits into
R0Y00:mainfrom
lambirou:startup/bvh-intersect-lowering
Nov 27, 2026
Merged

R0Y00 merged 4 commits into
R0Y00:mainfrom
lambirou:startup/bvh-intersect-lowering

Conversation

@lambirou

Copy link
Copy Markdown
Contributor

What changed

This is the first translation slice of image_bvh_intersect_ray in tools/startup/gfx2hlsl.py.

The supported slice is deliberately narrow:

  • 32-bit form only; image_bvh64_intersect_ray is refused by name.
  • FP32 box node (kind 5, 128 bytes): four child pointers, four boxes, extent clipping, nearest-first sorting when the descriptor box-sort bit is set, 0xffffffff for misses.
  • A straight-line compute program with a constant node pointer and a constant inline four-word BVH T#. The descriptor base becomes a ByteAddressBuffer binding in the manifest.
  • Every other case is refused by name with no partial output: triangle nodes, 16-bit box nodes, other kinds, the 64-bit form, A16, non-sequential addresses, loaded descriptors, an unaligned base, a growing box, a descriptor type other than 8, a zero/oversized size, a node outside the size, an absolute base and a pixel stage.

The emitted HLSL helper follows the public Mesa RADV box test and sorting network (MIT), including the inactive-box rule, the inverse-direction clamp and equal-key ordering. No code from the GPL-3.0 DexNom/VIctory reference is copied.

Tests

python -B tests/startup_gfx2hlsl_bvh_check.py

Output ends with:

dxc compile: ok
startup gfx2hlsl bvh box32: all ok

The check covers the CPU reference against the existing gfx1030 box fixture, the emitted sort network, the sort-bit negative control, the all-miss result, manifest and buffer binding, the four-result call, sort-disabled translation, and one refusal control per unsupported shape (no partial output). DXC compiled the emitted HLSL.

Existing startup_gfx2hlsl_{op_refusals,refusals,compute_controls} still pass.

Limits

This slice does not run on a GPU in the game path. It becomes useful once the compute Dispatch path can bind and dispatch the translated program. The triangle and 16-bit box cases are the next slices; the CPU model and witness set in #2921 are the oracle for them.

Refs #870, #1412, #1268.

AI-assisted.

Copy link
Copy Markdown
Contributor Author

CI caught a manifest regression in the first head: the new bvhbase entry evaluated source[2] for every resource, so ordinary table resources raised IndexError. Fixed in 2ec454f9 by formatting the BVH base only for the bvhbase kind. The Linux and Windows checks are green on the new head.

Copy link
Copy Markdown
Contributor Author

Clarification for review: the bvhbase manifest line is translator-only in this slice. The compute manifest loader does not yet parse srv N buf base ... size ..., so a dispatch using this translation is refused as an unsupported manifest line rather than bound at the wrong address. The PR proves the translation, the manifest shape and the DXC compile; runtime binding of a 40-bit console base is a separate slice. If you prefer, I can hold the manifest line behind a translator flag until the runtime consumes it.

Copy link
Copy Markdown
Contributor Author

A stacked binary16 slice is ready on lambirou:startup/bvh-intersect-box16 at 1fd5475d, pending this PR. It adds the 64-byte kind-4 box node with f16tof32, keeps the same sort/descriptor rules, compiles both helpers with DXC, and dispatches five FP32 plus five binary16 cases on WARP: all ten exact. I am holding the seventh PR until this one lands so its diff does not repeat this branch.

@KelboLive KelboLive 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.

PR #2922 by @lambirou -- startup: lower the FP32 box case of image_bvh_intersect_ray

What works

  • Solid foundational work establishing the 0x100 linear layout in gfx10_layout.h with careful derivation from AMD addrlib
  • The linear_mode = 0 constant elegantly extends block_log2() to accept mode 0 without changing the tiled modes switch logic
  • supports() check is correct: mode == linear_mode bypasses the block_log2 >= 12 requirement since linear has no tail
  • Test infrastructure is excellent -- linear_order_control (expected failure with largest mip first), linear_addrlib (338K element addresses), and the unpadded negative control all add strong coverage
  • Documentation accurately restates addrlib's linear mode algorithm in clear, testable terms

Issues

  • supports() currently allows block-compressed formats (BC1-BC7) in linear mode via the fallthrough -- BC formats are block-based and don't have linear row semantics; consider gating them out explicitly like 1D does in #2929
  • The check script in startup_address_console_check.py builds linear backing bytes from addrlib equations. This is correct but the linear path could break if the hardcoded equations drift from addrlib; a comment linking to the Mesa commit that derives these equations would help

Recommendations

  • Good discipline in including the negative control (unpadded rows test) to verify the 256-byte pitch requirement
  • Consider adding a TODO or assert in supports() that blocks linear mode for BC formats until explicitly validated against addrlib

Overall

A well-researched, well-tested implementation. The linear layout extension is cleanly separated from tiled modes. Minor concern about BC format handling in linear mode, but the existing refusal behavior is safe. Ready for merge.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The Python controls, DXC compile and five registered WARP helper cases pass on 2ec454f9 integrated with main 22107932 (Windows 11 build 26200.9974, SDK 10.0.26100, WARP 10.0.26100.9818, debug layer enabled, zero errors). Two cases are still missing from the fixture.

  1. With the sort bit clear, the nonadjacent comparisons can reorder hits whose keys are equal. I ran the existing raw-UAV harness with children (0x25, disabled, 0x2d, 0x35), depths (4, 1, 2, 3), the existing forward ray and extent 19. CPU reference gives (0x25, 0x2d, 0x35, 0xffffffff) in child order. The current CPU network model and actual WARP readback both return (0x25, 0x35, 0x2d, 0xffffffff). Please explicitly compact in original order when sorting is disabled and include this three-hit case before carrying the helper into the stacked binary16 slice.
  2. The GPU miss-fill control says it checks all-miss case 2, but it reads mutant[128:132], which is sorted case 0. The original case 0 already contains hits, so the current != (INVALID,) * 4 assertion passes even if the mutation does nothing. Please compare case 2 (136:140) and assert a predicted mismatch against its unmodified all-miss result.

Copy link
Copy Markdown
Contributor Author

Both findings are fixed in 94e90292, on top of current main 3e9542a6 (the branch now includes the merge).

  1. Sorting disabled no longer runs the compare-exchange network. The helper explicitly compacts h0..h3 in child order and returns 0xffffffff for the remaining words. The fixture now has the three-hit node (0x25, disabled, 0x2d, 0x35) with depths (4, 1, 2, 3), the existing forward ray and extent 19; both the CPU reference and the WARP dispatch return (0x25, 0x2d, 0x35, 0xffffffff).
  2. The miss-fill control now reads case 2 at words 136..139, not the sorted case 0 at 128..131, and asserts the mutated all-miss case is no longer four 0xffffffff words.

Local result: startup_gfx2hlsl_bvh_dispatch_warp passes all six cases, including the new unsorted case, and the miss-fill control passes. The controls/DXC run also passes. I will carry the same compaction into the stacked binary16 branch before submitting it.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The two findings are resolved at 94e90292 on current main 3e9542a6. With sorting disabled, the original three-hit counterexample now preserves child order. The updated miss-fill mutation examines the all-miss case and detects changed values there.

I rebuilt offscreen_check and ran ctest --preset <dev|release> --no-tests=error -V -R '^startup_gfx2hlsl_bvh'. Both presets passed 3/3 with no skips, including six exact cases and the miss-fill negative control on WARP and RTX 4060 Laptop. Windows 11 build 26200.9974, SDK 10.0.26100 DXC, WARP driver 10.0.26100.9818, RTX driver 32.0.16.1656, debug layer enabled with zero errors or warnings. Hosted Windows, Linux Python and gfx1013 checks are green. This confirms the translator and the standalone helper readback. A runtime binding for the BVH manifest remains a separate slice.

@R0Y00
R0Y00 merged commit 0d22858 into R0Y00:main Nov 27, 2026
5 checks passed
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.

3 participants