Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4671,6 +4671,8 @@ LLVM and LLD 15 build the code object and verify one instruction of each
kind in its disassembly. The CPU witnesses and mutations pass independently.
Native readback remains pending, including the raw miss and disabled-slot
words. The probe leaves translator behavior and form coverage unchanged.
The node model and its witness set are described in
[bvh-node-model.md](experiments/bvh-node-model.md).

### gfx1013 2D explicit-LOD image gathers

Expand Down
102 changes: 102 additions & 0 deletions docs/experiments/bvh-node-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Console BVH node model and witness set

Refs [#1759](https://github.com/R0Y00/Xbox2Windows/issues/1759),
[#2413](https://github.com/R0Y00/Xbox2Windows/issues/2413) and
[#2603](https://github.com/R0Y00/Xbox2Windows/pull/2603). This note covers the
pure-Python node model in `tests/gfx1030_bvh_box/bvh_model.py` and the
model-only witness set in `tests/gfx1030_bvh_box/check.py --model-only`. The
HIP kernel, the gfx1030 code object and the shader translator are unchanged.

## Node formats

A node pointer has its kind in the low three bits. The rest addresses a
64-byte node as `(pointer >> 3) << 6`. The 32-bit form adds that offset to a
40-bit base held in descriptor DWORD 0 and the low byte of DWORD 1, in
256-byte units. The 64-bit form uses the absolute pointer.

| Kind | Node | Bytes | Layout |
|---|---|---|---|
| 0-3 | triangle 0-3 | 64 | five vertices, three f32 each, bytes 0-59; ID DWORD at byte 60 |
| 4 | box, f16 bounds | 64 | four child pointers, then four boxes of six f16 from byte 16 |
| 5 | box, f32 bounds | 128 | four child pointers, then four boxes of six f32 from byte 16; bytes 112-127 unused |
| 6, 7 | instance, procedural | - | not read by this instruction |

`TRIANGLE_VERTICES` is `((0, 1, 2), (1, 3, 2), (2, 3, 4), (2, 4, 0))`.
Triangle `k` uses those three node vertices. The ID byte for triangle `k`
selects the vertices weighted by I and J in bits 1:0 and 3:2; the remaining
vertex is `3 - I - J`. Bits 7:4 of the ID byte are ignored. A malformed ID
that names the fourth map nibble reads node vertex 0, as the arithmetic
lands.

Descriptor DWORD 1 bit 31 enables nearest-first box sorting. DWORD 3 bit 24
selects triangle return mode 1. DWORD 3 bits 31:28 hold the resource type,
`0x8`.

## Results

A box node returns four child pointers. A hit is included at its entry
distance; a miss or an absent child is `0xffffffff`. Sorting uses a strict
comparison, so equal entries keep their child order; misses sort last when
sorting is enabled.

Triangle return mode 1 is `{t_num, t_denom, I_num, J_num}`, all as f32. On a
miss it is `{+inf, 1.0, 0.0, 0.0}`. The instruction does not test the ray
extent for triangles. Return mode 0 is not modeled in this slice.

## Assumptions

- Arithmetic is binary32 at each step, rounding to nearest even. Python
floats only carry the values; the result DWORDs are the f32 bit patterns.
- The inverse direction is an instruction input. The box test clamps it to
`+-FLT_MAX` before multiplying, so an origin exactly on a face does not
create `0 * infinity`.
- `fmin`/`fmax` return the non-NaN operand when one is NaN. A NaN minimum x
marks an absent box. Other NaN box components are outside the defined
input.
- A box is hit when `leave >= max(0, enter)` and `enter < extent`. An entry
exactly at the extent is not a hit. A box containing the origin can be hit
at a negative entry.
- The triangle test is the watertight Woop, Benthin and Wald formulation used
by RADV. A ray exactly through an edge or vertex hits both triangles that
share it. A NaN vertex, a zero determinant or a triangle behind the ray
misses.
- A zero direction gives an infinite inverse; the triangle shear then
produces NaN and the triangle misses.

These choices follow Mesa RADV's MIT-licensed software emulation and the
RDNA 2 ISA descriptor tables. They are a CPU model, not a native measurement:
hardware rounding, which of two equally near boxes is returned first, and the
exact edge case remain assumptions. No code is copied from the GPL-3.0
VIctory reference.

## Witness set

The model-only run has 315 cases: 231 node cases and 84 pointer-decoding
cases. The node cases are 66 f32 box, 46 f16 box and 119 triangle cases. The
expected four-DWORD results are literals in `check.py`; the runner does not
recompute them with the model. The existing five-ray FP32 box assertions in
`model_check()` still run before the witness set.

Coverage includes sorted and unsorted hits, misses, ties, extent clipping,
disabled and zero child pointers, inverted and infinite bounds, negative
zero, NaN bounds and vertices, zero directions, f16 rounding, subnormals and
65504, all six triangle vertex permutations for each kind, ID high bits,
malformed IDs, shared edges and vertices, t = 0, triangles behind the ray,
and degenerate triangles. Pointer cases cover every kind, nonzero and
high bases, 48-bit wrap and the absolute 64-bit form.

## Reproduce

```sh
python3 -B tests/gfx1030_bvh_box/check.py --model-only
```

The command needs no GPU, HIP library or code object. The native five-ray
case still needs LLVM/LLD 15 and a gfx1030 HIP device:

```sh
sh tests/gfx1030_bvh_box/build.sh
python3 -B tests/gfx1030_bvh_box/check.py --device 0
```

No game files, dumps or third-party source are part of this fixture.
2 changes: 2 additions & 0 deletions tests/checks/python-linux/bvh_node_model.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Pure-Python witness set for the console BVH node formats (no GPU, no LLVM).
tests/gfx1030_bvh_box/check.py --model-only
235 changes: 235 additions & 0 deletions tests/gfx1030_bvh_box/bvh_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
# SPDX-License-Identifier: MIT
"""CPU model of the console BVH node test.

This models one ``image_bvh_intersect_ray`` / ``image_bvh64_intersect_ray``
call: a ray and a node pointer in, four result DWORDs out. Node traversal,
the stack and instance transforms belong to the calling shader and are not
part of this module.

The node formats and the box test follow the public software emulation in
Mesa's RADV driver (MIT). The triangle test is the watertight formulation
from Woop, Benthin and Wald, JCGT 2013, as RADV uses it. The result layout
is the 40-bit ray-tracing descriptor's triangle return mode 1:
``{t_num, t_denom, I_num, J_num}``. Arithmetic is binary32 at every step,
rounding to nearest even; scalar Python floats only carry the values.
"""

import math
import struct


MASK32 = (1 << 32) - 1
MASK48 = (1 << 48) - 1
MASK64 = (1 << 64) - 1
INVALID = 0xffffffff

NODE_BOX16 = 4
NODE_BOX32 = 5
SORT_ENABLE = 1 << 31
RETURN_BARYCENTRICS = 1 << 24

TRIANGLE_VERTICES = ((0, 1, 2), (1, 3, 2), (2, 3, 4), (2, 4, 0))
MISS = (math.inf, 1.0, 0.0, 0.0)
FLT_MAX = struct.unpack("<f", struct.pack("<I", 0x7f7fffff))[0]


def f32(value):
"""Round a Python float to binary32, nearest even."""
if math.isnan(value) or math.isinf(value):
return value
try:
return struct.unpack("<f", struct.pack("<f", value))[0]
except OverflowError:
return math.inf if value > 0.0 else -math.inf


def f32_bits(value):
return struct.unpack("<I", struct.pack("<f", f32(value)))[0]


def bits_f32(word):
return struct.unpack("<f", struct.pack("<I", word & MASK32))[0]


def fadd(a, b):
return f32(a + b)


def fsub(a, b):
return f32(a - b)


def fmul(a, b):
return f32(a * b)


def fmin(a, b):
# IEEE minNum: the non-NaN operand when one is NaN.
if math.isnan(a):
return b
if math.isnan(b):
return a
return a if a <= b else b


def fmax(a, b):
# IEEE maxNum: the non-NaN operand when one is NaN.
if math.isnan(a):
return b
if math.isnan(b):
return a
return a if a >= b else b


def node_address(descriptor, pointer, wide=False):
"""Return ``(kind, byte address)`` for a node pointer.

The low three bits are the node kind. The rest addresses a 64-byte
node: ``(pointer >> 3) << 6``. The 32-bit form adds that offset to a
40-bit base held in descriptor DWORD 0 and the low byte of DWORD 1, in
256-byte units. The 64-bit form uses the absolute pointer.
"""
kind = pointer & 7
if wide:
return kind, ((pointer >> 3) << 6) & MASK64
base = (((descriptor[1] & 0xff) << 32) | (descriptor[0] & MASK32)) << 8
return kind, (base + ((pointer >> 3) << 6)) & MASK48


def descriptor(base=0, *, sort=True, return_barycentrics=True, size=0x3ffffffffff):
"""Build the four descriptor DWORDs a node test reads.

The base argument is a 256-byte aligned byte address. The DWORDs hold
it in 256-byte units, as the instruction's resource does.
"""
assert base & 0xff == 0, "the descriptor base is 256-byte aligned"
units = base >> 8
return (
units & MASK32,
((units >> 32) & 0xff) | (SORT_ENABLE if sort else 0),
size & MASK32,
((size >> 32) & 0x3fffff)
| (RETURN_BARYCENTRICS if return_barycentrics else 0)
| (8 << 28),
)


def _load_box(data, offset, kind, index):
if kind == NODE_BOX32:
start = offset + 16 + 24 * index
return struct.unpack_from("<6f", data, start)
start = offset + 16 + 12 * index
return struct.unpack_from("<6e", data, start)


def _box_entry(lo, hi, origin, inverse, extent):
# RADV removes infinities so an origin exactly on a face does not turn
# zero times infinity into NaN. max then min is the order its NIR uses.
inv = [fmin(fmax(inverse[i], -FLT_MAX), FLT_MAX) for i in range(3)]
low = [fmul(fsub(lo[i], origin[i]), inv[i]) for i in range(3)]
high = [fmul(fsub(hi[i], origin[i]), inv[i]) for i in range(3)]
enter = fmax(fmax(fmin(low[0], high[0]), fmin(low[1], high[1])), fmin(low[2], high[2]))
leave = fmin(fmin(fmax(low[0], high[0]), fmax(low[1], high[1])), fmax(low[2], high[2]))
# min.x is the inactive-slot marker. Other NaN components are outside
# the defined input; fmin/fmax above use the non-NaN operand.
if math.isnan(lo[0]):
return None
if leave >= fmax(0.0, enter) and enter < extent:
return enter
return None


def _boxes(data, offset, kind, origin, inverse, extent, sort):
children = struct.unpack_from("<4I", data, offset)
indices = [INVALID] * 4
distances = [math.inf] * 4
for i in range(4):
if children[i] == INVALID:
continue
coords = _load_box(data, offset, kind, i)
enter = _box_entry(coords[:3], coords[3:], origin, inverse, extent)
if enter is not None:
indices[i] = children[i]
distances[i] = enter
if sort:
# RADV's five-comparator network. A strict comparison leaves equal
# entries in child order.
for a, b in ((0, 1), (2, 3), (0, 2), (1, 3), (1, 2)):
if distances[b] < distances[a]:
distances[a], distances[b] = distances[b], distances[a]
indices[a], indices[b] = indices[b], indices[a]
return indices


def _node_vertex(data, offset, index):
return struct.unpack_from("<3f", data, offset + 12 * index)


def _triangle(data, offset, kind, origin, direction, inverse):
id_dword = struct.unpack_from("<I", data, offset + 60)[0]
id_byte = (id_dword >> (8 * (kind & 3))) & 0xff
i = id_byte & 3
j = (id_byte >> 2) & 3
first = (3 - i - j) & 3
# The fourth map nibble is zero in the node format; a malformed ID that
# names it reads the node's first vertex, as the arithmetic lands.
mapped = TRIANGLE_VERTICES[kind & 3] + (0,)
a, b, c = (_node_vertex(data, offset, mapped[n]) for n in (first, i, j))

size = (abs(direction[0]), abs(direction[1]), abs(direction[2]))
if size[0] >= size[1]:
kz = 0 if size[0] >= size[2] else 2
elif size[1] >= size[2]:
kz = 1
else:
kz = 2
kx = (kz + 1) % 3
ky = (kx + 1) % 3
if direction[kz] < 0.0:
kx, ky = ky, kx

sz = inverse[kz]
sx = fmul(direction[kx], sz)
sy = fmul(direction[ky], sz)

def shear(vertex):
rel = [fsub(vertex[n], origin[n]) for n in range(3)]
return (
fsub(rel[kx], fmul(sx, rel[kz])),
fsub(rel[ky], fmul(sy, rel[kz])),
fmul(sz, rel[kz]),
)

ax, ay, az = shear(a)
bx, by, bz = shear(b)
cx, cy, cz = shear(c)
u = fsub(fmul(cx, by), fmul(cy, bx))
v = fsub(fmul(ax, cy), fmul(ay, cx))
w = fsub(fmul(bx, ay), fmul(by, ax))

behind = u < 0.0 or v < 0.0 or w < 0.0
before = u > 0.0 or v > 0.0 or w > 0.0
det = fadd(u, fadd(v, w))
t = fadd(fadd(fmul(u, az), fmul(v, bz)), fmul(w, cz))
ahead = (det > 0.0 and t >= 0.0) or (det < 0.0 and t <= 0.0)
if (behind and before) or not ahead:
return MISS
# The instruction does not test the ray extent for triangles.
return f32(t), f32(det), f32(v), f32(w)


def intersect(data, descriptor_words, pointer, *, origin, direction, inverse, extent,
wide=False):
"""Return the four result DWORDs for one node and one ray."""
kind, address = node_address(descriptor_words, pointer, wide)
sort = bool(descriptor_words[1] & SORT_ENABLE)
if kind == NODE_BOX32 or kind == NODE_BOX16:
return _boxes(data, address, kind, origin, inverse, extent, sort)
if kind <= 3:
if not descriptor_words[3] & RETURN_BARYCENTRICS:
raise ValueError("triangle return mode 0 is not modeled")
# The instruction does not test the ray extent for triangles.
values = _triangle(data, address, kind, origin, direction, inverse)
return [f32_bits(value) for value in values]
# Instance and procedural nodes are not read by this instruction.
return [f32_bits(value) for value in MISS]
Loading
Loading