Skip to content

startup: lower triangle nodes in image_bvh_intersect_ray - #3105

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

R0Y00 merged 26 commits into
R0Y00:mainfrom
lambirou:startup/bvh-intersect-triangle

Conversation

@lambirou

@lambirou lambirou commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What changed

This is the triangle-node slice of image_bvh_intersect_ray, stacked on #3032 (binary16 box node). The triangle change is 5640a903; the head a68c9bd3 also merges the updated #3032 branch and current main so the tests run on the latest base. Rebased onto main after #3032 landed (b2f86a8d).

  • Accepts the 64-byte triangle node forms (kinds 0..3): five float3 vertices at bytes 0..59 and one ID dword at byte 60.
  • Decodes the kind-selected 8-bit ID map (i and j two bits each, third index (3-i-j)&3) and the Mesa RADV base vertex triple.
  • Emits x2w_bvh_intersect_triangle: the watertight ray/triangle test in binary32, triangle return mode 1 {t, det, v, w}, and the miss {+inf, 1, 0, 0}. The instruction does not test ray_extent for triangles.
  • Keeps the descriptor rules: base in 256-byte units, the size field is a count minus one in 64-byte units (size=0 is a one-unit span), and the 48-bit end-exclusive bound applies. Return mode 0 and reserved bits are refused by name.
  • Triangle kinds are removed from the box refusal list; the box32/box16 check now compiles with -Gis, matching the runtime.

Tests

python -B tests/startup_gfx2hlsl_bvh_triangle_check.py
ctest --preset <dev|release> --no-tests=error -R '^startup_gfx2hlsl_bvh'

The check reuses the 119 triangle witnesses in tests/gfx1030_bvh_box/check.py and the independent model in tests/gfx1030_bvh_box/bvh_model.py (71 unique node images, 476 result words). It covers all four kinds, every ID map byte, edges and vertices, outside/behind rays, zero and negative-zero directions, t=0, extent=0, 1024x scaling both ways, degenerate/malformed nodes, and the box kinds 6 and 7.

Decoded-extent controls: exact fits (0x00, 0) and (0x08, 1), too-short (0x08, 0), largest extent 0x03ffffff, above-4-GiB 0x04000000, plus a boundary dispatch where a triangle at offset zero with size 0 hits and a triangle at offset 64 with size 0 misses.

The dispatch passes on WARP (1414:008c, driver 10.0.26100.10068) and on an AMD Radeon RX 9060 XT (1002:7590, driver 32.0.32029.2010), in dev and release, with four mutants (determinant sign, ID shift, u term, miss words). The existing box32/box16 tests still pass 6/6 in both presets.

Windows checks #8253 is green on a68c9bd3, merged with main 63a30dc1.

Limits

This is a translator slice. It does not bind the node buffer on a D3D12 device, does not execute the native gfx1030 probe, and does not establish title behavior. The runtime manifest loader does not yet consume bvhbase. Return mode 0 is not modelled and is refused; the sort bit is ignored for triangles. Rebased after #3032 landed.

Refs #870, #2921, #2922, #3032, #3040.

AI-assisted.

Review update (2026-11-27, 263604fc)

JohnConnorNPC's first four requests are addressed; the stack still waits for #3032.

  • The translator control now asserts the full origin, direction and inverse-direction registers (v18..v26) in the generated call, so swapping the three float3 operands fails the portable check.
  • Box kinds 4 and 5 ignore the triangle return mode bit. The Python guard masks word 3 with 0x0effffff and x2w_bvh_descriptor_ok uses (word3 & 0xfeffffffu) == 0x80000000u. Portable and GPU dispatch cases cover both box kinds with bit 24 set; the triangle helper still requires mode 1.
  • tests/checks/python-linux/startup_gfx2hlsl_bvh_triangle.txt puts the triangle check in the Linux job.
  • The box32 note no longer lists triangles as refused, the triangle note distinguishes the four ID-map bytes from all 16 (i,j) pairs, kinds 6 and 7 are named instance and procedural, the witnesses are identified as model-derived, and both notes are linked from docs/experiments.md.

Receipt: startup_gfx2hlsl_bvh dispatch 23/23 cases and all mutants on WARP and the RX 9060 XT; startup_gfx2hlsl_bvh_triangle dispatch covers all 119 witnesses, four mutants and the boundary cases on both adapters; docs_links_check 98 files and 591 links; run_checks_fragments_check passes. Windows checks #8297 is green on 263604fc.

Adopt the FP32 BVH descriptor unit decode, 48-bit end-exclusive bound, positional unsorted results and positive-infinity miss keys for both box kinds. Binary16 fixtures are packed as half values and share the independent model; dev and release WARP and RX 9060 XT dispatch pass 21 cases plus six mutants.
Accept the 64-byte triangle node forms (kinds 0..3), decode their four ID maps and base vertex triples, and emit a watertight ray/triangle helper for return mode 1. The helper returns {t, det, v, w} and keeps the 48-bit descriptor rules. 119 CPU witnesses pass bit-for-bit on WARP and the RX 9060 XT in dev and release, with four negative mutations; the existing box tests remain green.

Copy link
Copy Markdown
Contributor Author

Head is 004fcd04, stacked on #3032. startup_gfx2hlsl_bvh passes 6/6 in dev and release on WARP and the RX 9060 XT: box32/box16 plus 119 triangle witnesses and four mutants. Windows checks #8207 is pending. The triangle change itself is 5640a903; I will retarget/ rebase after #3032 lands.

Copy link
Copy Markdown
Contributor Author

Windows checks #8207 completed successfully on 004fcd04.

Size 0 is a one-unit span, so a binary16 node at offset zero accepts it and an FP32 node needs size 1 at offset zero. Use the decoded extent in the Python guard and both HLSL helpers, with exact-fit, too-short, upper-bound and boundary-dispatch controls.
Size is a count minus one in 64-byte units: size 0 covers a triangle at offset zero, while a triangle in the next unit needs size 1. Cover exact fits, a too-short neighbor, the 4 GiB upper bound and the HLSL boundary dispatch on WARP and hardware.

Copy link
Copy Markdown
Contributor Author

Propagated the decoded-extent rule to this stacked PR on 070b12de: the triangle helper uses (offset >> 6) > word2 and x2w_bvh_triangle_descriptor_ok allows word2 = 0 with the same < 0x04000000 upper bound.

Controls: exact fits (node 0x00, size 0) and (0x08, 1), too-short (0x08, 0), largest extent 0x03ffffff, above-4-GiB 0x04000000, plus a boundary dispatch where a triangle at offset zero with size 0 hits and a triangle at offset 64 with size 0 misses. startup_gfx2hlsl_bvh is 6/6 in dev and release on WARP and the RX 9060 XT. Windows checks are running on the new head.

Copy link
Copy Markdown
Contributor Author

Windows checks #8240 completed successfully on 070b12de.

Copy link
Copy Markdown
Contributor Author

Merged the updated #3032 branch and main 63a30dc1; head is now a68c9bd3. Windows checks #8253 is green, with the same 6/6 dev+release result on WARP and the RX 9060 XT.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the careful slice: the witness reuse, the boundary dispatch and the mutants make this easy to follow. I ran
the Linux side at a68c9bd3: startup_gfx2hlsl_bvh_triangle_check.py, startup_gfx2hlsl_bvh_check.py and
gfx1030_bvh_box/check.py --model-only all pass. DXC and GPU weren't available here. A few requests before this is
ready:

  1. Test the operands of the translated call. The dispatch writes its own x2w_bvh_intersect_triangle(...) call, so
    only the helper text is shared with the translator. The translator controls check the descriptor words, the node and
    v32..v35, but not the origin, direction and inv_dir arguments, which this PR wires up for the first time. When
    I swapped direction and inv_dir in the triangle call in op_image_bvh_intersect_ray (and, separately, passed the
    direction as the origin), both BVH checks stayed green. The dispatch wouldn't see it either. The fixture's v18..v26
    hold distinct values, so asserting the three float3(...) in the call line would cover it. Dispatching the translated
    shader once would cover it too.
  2. Triangle return mode on box nodes. refuses box return mode 1 makes a box node refuse when word 3 has bit 24
    set. The reference model's descriptor() sets that bit by default, and all 66 box witnesses use it.
    bvh_model.intersect() reads the bit only for triangle kinds, and one T# normally covers both node types. Could box
    nodes ignore bit 24, in the Python guard and in x2w_bvh_descriptor_ok? If there's a source that requires it to be 0
    for boxes, a pointer to it would help.
  3. Run the triangle check in PR CI. The pull-request CI doesn't run it: it isn't in tests/checks/python-linux/,
    and the pr ctest preset doesn't match it. It passes on Linux without DXC, so a
    tests/checks/python-linux/startup_gfx2hlsl_bvh_triangle.txt would put it in the Linux job.
  4. Docs.
    • docs/experiments/startup-gfx2hlsl-bvh-box32.md still starts its "Refused by name" list with "Triangle nodes
      (kinds 0..3)".
    • Neither BVH note is linked from docs/experiments.md.
    • Small wording points in the triangle note:
      • "every ID map byte": the witnesses cover 8 of the 16 (i, j) pairs per kind, so "each of the four ID map bytes"
        may be meant.
      • Kinds 6 and 7 are instance and procedural nodes in the model, not box kinds.
      • The 119 witnesses come from the CPU model (tests: model the console BVH node set #2921), not a hardware capture. It would help to say that the
        dispatch shows the HLSL matches the model, not RDNA 2 hardware.
  5. Merge order. As you noted, this should wait for startup: lower the binary16 BVH box node #3032 and then be rebased, since it currently carries startup: lower the binary16 BVH box node #3032's
    commits and the merges.

Happy to re-run the Linux checks on a new head.

This comment was AI-assisted.

- assert the origin, direction and inverse-direction operands of the translated call
- ignore the triangle return mode bit for box nodes in the translator guard and helper
- add the triangle check to the Linux CI fragment
- correct the BVH experiment notes and link both notes from experiments.md
@lambirou

Copy link
Copy Markdown
Contributor Author

@JohnConnorNPC thanks for the detailed review. Points 1-4 are addressed on 263604fc; point 5 still waits for #3032.

  1. The translator control now asserts float3(F(v18), F(v19), F(v20)), float3(F(v21), F(v22), F(v23)), float3(F(v24), F(v25), F(v26)) in the emitted call, so the origin, direction and inverse-direction operands are pinned independently.
  2. Box kinds 4 and 5 ignore bit 24. The Python guard uses (vals[3] & 0x0effffff) == 0 for boxes, and x2w_bvh_descriptor_ok uses (word3 & 0xfeffffffu) == 0x80000000u. Portable controls plus GPU dispatch cases exercise both box kinds with bit 24 set; the triangle helper still requires mode 1.
  3. tests/checks/python-linux/startup_gfx2hlsl_bvh_triangle.txt is added, so the Linux job runs the check.
  4. The box32 note no longer lists triangles as refused; the triangle note now says each of the four ID-map bytes (8 of 16 (i,j) pairs per kind), names kinds 6/7 as instance and procedural, identifies the 119 witnesses as model-derived, and both notes are linked from docs/experiments.md.
  5. The branch remains stacked on startup: lower the binary16 BVH box node #3032. I will retarget/rebase it once startup: lower the binary16 BVH box node #3032 lands.

Local receipt: startup_gfx2hlsl_bvh dispatch 23/23 plus mutants on WARP and the RX 9060 XT; startup_gfx2hlsl_bvh_triangle covers 119 witnesses, four mutants and the boundary cases on both adapters; docs_links_check 98 files/591 links; run_checks_fragments_check passes. Windows checks are running on this head.

@lambirou

Copy link
Copy Markdown
Contributor Author

Windows checks #8297 completed successfully on 263604fc.

R0Y00 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

#3032 is now on main as 5b145ca9. Please rebuild this branch on current main with the triangle-only delta so the squashed box16 work is not duplicated in the diff. Keep the decoded extent and 48-bit boundary controls alongside the existing triangle witnesses. I’ll recheck the new head.

…-triangle

# Conflicts:
#	docs/experiments/startup-gfx2hlsl-bvh-box32.md
#	tests/startup_gfx2hlsl_bvh_check.py
#	tools/startup/gfx2hlsl.py
@lambirou

Copy link
Copy Markdown
Contributor Author

#3032 landed on main as 5b145ca9. I merged current main (afe70bd7) into this branch; the head is now b2f86a8d and the PR is no longer stacked.

The conflicts were limited to tools/startup/gfx2hlsl.py, tests/startup_gfx2hlsl_bvh_check.py and the box32 experiment note. I kept the triangle lowering plus the box return-mode controls on top of the merged box implementation. No upstream translator or test change outside the BVH section was dropped.

Local checks on b2f86a8d: startup_gfx2hlsl_bvh 23/23 dispatch cases and all mutants on WARP; startup_gfx2hlsl_bvh_triangle all 119 witnesses, four mutants and the boundary cases on WARP; docs_links_check 109 files and 598 links; run_checks_fragments_check passes. Windows checks are running on the new head.

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

Independently reviewed b2f86a8d over main at 2b7ebf18. The triangle path's 64-byte records respect the decoded extent from #3032. I checked the box return-mode controls and the triangle readback, ordering, and boundary controls.

I built offscreen_check from source and ran ctest --test-dir <build> --no-tests=error -j1 -V -R '^startup_gfx2hlsl_bvh' in Debug and Release (/O2): 6/6 passed in each, with no skips. Both sets exercised WARP 10.0.26100.9818 and an NVIDIA GeForce RTX 4060 Laptop GPU (driver 32.0.16.1656) on Windows 11 build 26200.9974. The D3D12 debug layer reported zero errors and warnings. Hosted Windows passed, and the hosted /O2 job was skipped. The subsequent main change from #3175 adds a separate scene-colour reporting tool and documentation. I verified the current trial merge has identical BVH implementation and test contents.

git diff --check flags extra end-of-file blank lines in three new files. They do not affect runtime or test behavior.

@R0Y00
R0Y00 merged commit c15a73a into R0Y00:main Nov 27, 2026
5 checks passed
@lambirou
lambirou deleted the startup/bvh-intersect-triangle branch November 27, 2026 22:54
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