Skip to content

tests: model the console BVH node set - #2921

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
lambirou:tests/bvh-node-model
Nov 27, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
lambirou:tests/bvh-node-model

Conversation

@lambirou

Copy link
Copy Markdown
Contributor

What changed

This adds a pure-Python CPU model of the console BVH node test, image_bvh_intersect_ray / image_bvh64_intersect_ray, and extends the existing gfx1030 fixture with a model-only witness set.

The model covers:

  • FP32 box nodes (kind 5, 128 bytes): four child pointers, four boxes, extent clipping, nearest-first sorting when descriptor dword 1 bit 31 is set, 0xffffffff for misses and absent children.
  • binary16 box nodes (kind 4, 64 bytes): the same layout with six binary16 bounds per box, widened for the test.
  • triangle nodes (kinds 0-3): five vertices, the per-triangle ID byte, the watertight ray/triangle test, {t_num, t_denom, I_num, J_num} in the triangle return mode, +inf/1/0/0 on a miss.
  • node pointer decoding: low three bits kind, (pointer >> 3) << 6 from the 40-bit descriptor base in 256-byte units, and the 64-bit absolute form.

The node formats and box test follow Mesa RADV's public software emulation (MIT); the triangle test is the watertight formulation RADV uses. No game files or raw captures are included.

Tests

python -B tests/gfx1030_bvh_box/check.py --model-only

Output:

model_witness_cases=315 node=231 pointer=84 mismatches=0

The 315 cases cover 66 FP32 boxes, 46 binary16 boxes, 119 triangles and 84 pointer decodings, including hits, misses, ties, shared edges, negative zero, NaN vertices, zero direction, disabled children, vertex permutations and the ID byte's high bits. The existing five-ray FP32 assertions still pass.

tests/docs_links_check.py passes (546 links). The native five-ray GPU fixture was not run here: LLVM 15, LLD 15 and the HIP runtime are unavailable on this host. The HIP kernel and assembly are unchanged.

Limits

The model is a reference for a translator, not a translation. Native gfx1030 readback remains pending, and the triangle return mode 0 is outside this slice.

Refs #870, #1268.

AI-assisted.

Add a pure-Python model of image_bvh_intersect_ray for FP32 and
binary16 box nodes and triangle nodes. The model covers descriptor
and node pointer decoding, box extent clipping, stable nearest-first
sorting, watertight triangle tests and the mode-1 numerator results.

The model-only witness set has 315 literal cases: 231 node cases
(66 FP32 box, 46 binary16 box, 119 triangle) and 84 pointer cases.
They cover hits, misses, ties, shared edges, negative zero, NaN,
zero directions, disabled children, f16 rounding and permutations.
The HIP kernel, the code object and the existing five-ray native
case are unchanged.

Model-only: python -B tests/gfx1030_bvh_box/check.py --model-only
Result: model_witness_cases=315 node=231 pointer=84 mismatches=0

Copy link
Copy Markdown
Contributor Author

Added tests/checks/python-linux/bvh_node_model.txt so the 315-case model-only witness set runs in the Linux Python job, not only by hand. python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux --jobs 2 passes all six fragments locally, including model_witness_cases=315 node=231 pointer=84 mismatches=0.

@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 #2921 by @lambirou -- tests: model the console BVH node set

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.

Reviewed c59a56f2453ce73196c3a80a54a7f38e9770ded0. I ran python -B tests/gfx1030_bvh_box/check.py --model-only in the integration tree for current main. All 315 model witnesses matched (231 node and 84 pointer cases), and the reversed-sort and zero-fill controls both produced the expected mismatch. The CPU model and source-linked formulas are clearly scoped. Native gfx1030 execution was outside this check. Required Windows CI passed.

@R0Y00
R0Y00 merged commit 2944b05 into R0Y00:main Nov 27, 2026
5 checks passed
@thepatrickworld

Copy link
Copy Markdown
Contributor

Independent Linux/Python result on c59a56f2 (= merge 2944b050). Post-merge receipt; measurements only.

Environment

  • The PR merged at 2026-11-27T05:41:11Z while this ran, so this is a post-merge receipt on the merged tree.
    The three files it depends on are byte-identical (LF-normalised) between the head I measured and the merge
    commit — bvh_model.py c54fb83a03823cd4, check.py 3051cdaa7f08993b, bvh_node_model.txt
    9f6513856010e87c — and current origin/main (6aa59440) still carries the same three blobs.
  • Linux host, CPython 3.11.16; also rerun under 3.14. No GPU, no LLVM 15, no HIP runtime, so --model-only is the only path that can run here.
  • Worktree at the PR head c59a56f2453ce73196c3a80a54a7f38e9770ded0, git status --porcelain --untracked-files=no empty.

Commands and raw output

$ python3 -B tests/gfx1030_bvh_box/check.py --model-only
sorted reversed-sort control word=0 expected=0000001d actual=00000025
all_miss zero-fill control word=0 expected=ffffffff actual=00000000
model_witness_cases=315 node=231 pointer=84 mismatches=0
$ echo $?
0

$ python3.14 -B tests/gfx1030_bvh_box/check.py --model-only
model_witness_cases=315 node=231 pointer=84 mismatches=0

$ python3 -B tests/docs_links_check.py
Self-test passed.
Checked 30 Markdown files and 546 links.

$ python3 -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux -j 4
...
all 6 checks passed

The counts reproduce, and the new fragment does reach CI before merge: the python-linux job has no if:,
the workflow's pull_request trigger has no paths: filter, and that job runs
--from-dir tests/checks/python-linux, where bvh_node_model.txt now lives.

What the 315 cases pin

I injected 23 one-line mutants of bvh_model.py through sys.modules['bvh_model'] before importing
check.py, then ran --model-only; the unmutated module reports 0 failures through the same path before and
after the matrix, and the file on disk is unchanged. Mutant, what it changes, and the first literal failure
(expected/actual):

M1  fmax(0.0, enter) dropped                     14  box32/origin-12.5-0-e1e+09 word=0 0000020c/00000105
M2  enter < extent        -> enter <= extent      8  box32/extent-20 word=1 ffffffff/0000020c
M3  sort distances[b] <   -> <=                  20  box32/tie-sorted word=0 00000105/00000400
M4  inverse ±FLT_MAX clamp dropped                1  box32/origin-0--1-e1e+09 word=0 00000105/ffffffff
M6  absent child INVALID  -> 0                    6  box32/mixed-sorted word=1 00000304/ffffffff
M7  miss fill INVALID     -> 0                   56  box32/extent-35 word=3 ffffffff/00000000
M8  bounds subtracted the other way              90  box32/four-sorted word=0 00000105/ffffffff
M10 det sign ignored in the ahead test           46  tri/kind0-center word=0 c1800000/7f800000
M11 ahead t >= 0          -> t > 0                4  tri/kind0-t0 word=0 80000000/7f800000
M14 mixed-sign rejection dropped                  4  tri/kind0-outside word=0 7f800000/c1800000
M15 descriptor base in 64-byte units             20  ptr/base40-0-k0 (0,16384)/(0,4096)
M16 address & MASK48      -> & MASK32            10  ptr/highbase-0-k0 (0,78187493376)/(0,878082048)
M17 kind mask 7           -> 3                  162  box32/four-sorted word=0 00000105/7f800000
M18 node stride <<6       -> <<4                 54  ptr/base0-64-k0 (0,64)/(0,0)
M19 box16 bounds read as f32                     --  struct.error: unpack_from requires a buffer of at least 76 bytes
M20 box routing changed                         165  box16/four-sorted word=0 00000105/7f800000
M21 wide address & MASK64 -> & MASK48             3  ptr/wide-ffffffffffffffc0-k0
M22 t without the w term                         84  tri/kind0-center word=0 c1800000/c1400000
M23 det sign unused for the t sign               44  tri/kind0-center word=0 c1800000/7f800000

19 of the 21 non-equivalent mutants fail the run: 18 through a named witness mismatch, and M19 through
struct.error rather than a wrong value. Two others (below) are provably equivalent Python rewrites and are
excluded from the denominator. So the box test, the sort stability, the absent-child and miss fills, the
descriptor-relative addressing and the triangle barycentric sign are all pinned by these literals.

One branch the set never observes

if direction[kz] < 0.0: kx, ky = ky, kx (bvh_model.py:188) is reached by four witnesses —
tri/kind0-behind .. tri/kind3-behind, direction (0, 0, -1) — but all four miss, identically with and
without the swap, because a global sign flip leaves the ahead test invariant.

A triangle that hits with a negative dominant component separates the two. Node: kind 0, byte 60 = 0x01
(i = 1, j = 0, first = 2, so a = v2, b = v1, c = v0), vertices (0,0), (1,0), (0,1) at z = -4;
ray origin (0.2, 0.2, 0), direction (0, 0, -1), inverse (inf, inf, -1), descriptor(0, sort=True):

delivered  c0800000 bf800001 be4cccce bf19999a    -4.0, -1.0000001, -0.2, -0.6
no swap    40800000 3f800001 3e4cccce 3f19999a    +4.0, +1.0000001, +0.2, +0.6

The same triangle at z = +4 with direction (0, 0, +1) returns 40800000 3f800001 3e4cccce 3f19999a,
exactly the second row, so the two differ by an overall sign of all four words. That is not a hit/miss
difference: t/det is +4 in both (t = -4 with det = -1 is ahead, not behind), and every quotient is
sign-invariant. Only a raw-word expectation can see it — which is what the native comparison is for. Worth
noting that 44 hitting triangles do return det < 0, so that path is covered; it is the axis swap under a
negative dominant component that is not. Adding the mirrored case would pin whichever sign is chosen; if the
sign is meant to follow the console, it has to come from the native readback (or RADV), not from the model.

The other three survivors

  • first = (3 - i - j) & 3 -> first = 3 - i - j: a Python no-op. 3-i-j lies in [-3, 3] and mapped is a
    four-tuple, so mapped[-k] is mapped[4-k], exactly what & 3 computes. No input can kill it.
  • TRIANGLE_VERTICES[kind & 3] + (0,) -> + (1,): equivalent. The appended slot is selected whenever
    first, i or j is 3; in every such id byte another of the three selected indices equals it
    (first == 3 requires i + j ≡ 0 mod 4, so i or j is 3, or i == j), two vertices coincide, det is
    exactly zero, and both versions miss. A sweep of 1024 (kind, id byte) combinations, 192 of them hitting,
    shows no separation, but the structural argument is the proof.
  • if math.isnan(lo[0]) -> math.isnan(low[0]): it survives the set, and it is separable. On the existing
    box32/infinite-bounds node, set inverse[0] = 0.0 and keep everything else: the delivered model returns
    00000304 0000020c 00000105 00000400, the mutant 00000105 00000400 ffffffff ffffffff. Box 2 stores
    lo[0] = -inf, so the computed bound is -inf * 0 = NaN; the mutant reads that as the absent-slot marker
    and drops the box. The documented rule ties the marker to the stored bound, which supports the delivered
    version — but the input is undocumented rather than excluded (the doc only states zero direction ->
    infinite inverse, not its converse), so I would not add a case for it.

What this fixture ties to hardware, and what it does not

model_check() (line 78) exercises reference() (line 59), not bvh_model, so the five-ray assertions
quoted in the body are not evidence about the new model; witness_check() (line 105) is its only consumer.
The native path compares its ten readbacks against the RAYS literals (line 1050), not against bvh_model,
so the pending native run will not validate the model either. If the model is meant to predict the console
words, the native path has to push the same cases through it.

Limits

  • One host, no GPU, no LLVM/HIP: nothing here is a native measurement, and the ten native readbacks were not
    executed.
  • Every expected value is a literal in the same file, so the set is a regression lock on bvh_model.py. The
    independent oracle it rests on is the RADV derivation cited in the docstring, which is not in the tree.
    Green here says "the model still does what the literals say", not "the model matches the console".
  • The mutants are my selection, not the set the fixture documents for itself.
  • No node witness passes wide=True; the 64-bit form is pinned only by sixteen POINTER_WITNESSES calling
    node_address directly. descriptor(return_barycentrics=False) is never built, so the mode-0 ValueError
    is unexercised.

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.

4 participants