mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:18:37 +00:00
Merge pull request #598 from jarvixaiassistant-glitch/fix/old-10063-xdxr-bvh-nan-inactive
xdxr_bvh: handle D3D12 inactive primitives and NaN bounds (OLD-10063)
This commit is contained in:
@ -311,6 +311,115 @@ class ClosestHit(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
X.build_tlas([X.Instance(inst.transform, 0x12340, info["root"], info["bounds"])])
|
||||
|
||||
def test_inactive_nan_triangles_produce_empty_boxes_and_valid_blas(self):
|
||||
# D3D12 marks inactive triangles with a NaN vertex (e.g. v0.x = NaN); xdxr_build.comp gives them an empty
|
||||
# box (+inf, -inf) and never pairs them. Both 1-level and multi-level builds must validate and trace cleanly.
|
||||
nan_sharing_edge = [[math.nan, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
|
||||
active = [[[2.0 * i, 0.0, 0.0], [2.0 * i + 1.0, 0.0, 0.0], [2.0 * i, 1.0, 0.0]] for i in range(6)]
|
||||
tris = [nan_sharing_edge, *active]
|
||||
for box16 in (False, True):
|
||||
for pair in (False, True):
|
||||
with self.subTest(box16=box16, pair=pair):
|
||||
data, info = X.build_blas(tris, box16=box16, pair=pair)
|
||||
self.assertEqual(X.validate(data, expect_bottom=True), [])
|
||||
self.assertEqual(X.box16_rounding(data), [])
|
||||
self.assertEqual(info["bounds"], ([0.0, 0.0, 0.0], [11.0, 1.0, 0.0]))
|
||||
if pair:
|
||||
self.assertEqual(X.count_pairs(data), 0)
|
||||
mem = X.Memory()
|
||||
mem.place(0x10000, data)
|
||||
for i, tri in enumerate(active, start=1):
|
||||
cx = sum(p[0] for p in tri) / 3.0
|
||||
cy = sum(p[1] for p in tri) / 3.0
|
||||
o, d = [cx - 0.5, cy - 0.5, 1.0], [0.5, 0.5, -1.0]
|
||||
got = X.trace(mem, 0x10000, o, d)
|
||||
want = X.brute_force([(None, 0xFF, tris)], o, d)
|
||||
self.assertTrue(same_hit(got, want), (i, got, want))
|
||||
self.assertEqual(got["primitive"], i)
|
||||
|
||||
def test_all_inactive_triangles_blas_is_valid_and_misses(self):
|
||||
tris = [[[math.nan, float(i), 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]] for i in range(6)]
|
||||
for box16 in (False, True):
|
||||
with self.subTest(box16=box16):
|
||||
data, info = X.build_blas(tris, box16=box16, pair=True)
|
||||
self.assertEqual(X.validate(data, expect_bottom=True), [])
|
||||
self.assertEqual(X.box16_rounding(data), [])
|
||||
self.assertEqual(info["bounds"], ([math.inf] * 3, [-math.inf] * 3))
|
||||
self.assertEqual(X.count_pairs(data), 0)
|
||||
mem = X.Memory()
|
||||
mem.place(0x10000, data)
|
||||
self.assertIsNone(X.trace(mem, 0x10000, [0.25, 0.25, 1.0], [0.0, 0.0, -1.0]))
|
||||
|
||||
def test_inactive_tlas_instances_get_empty_boxes(self):
|
||||
active_blas, active_info = X.build_blas([TRI])
|
||||
empty_blas, empty_info = X.build_blas([[[math.nan, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]])
|
||||
mem = X.Memory()
|
||||
va_active = mem.place(0x100000, active_blas)
|
||||
va_empty = mem.place(0x200000, empty_blas)
|
||||
ident = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
|
||||
zero = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
|
||||
insts = [
|
||||
X.Instance(zero, 0, X.INVALID, active_info["bounds"], mask=0xFF), # null BLAS, singular matrix
|
||||
X.Instance(ident, va_active, active_info["root"], active_info["bounds"], mask=0), # zero mask
|
||||
X.Instance(ident, va_empty, empty_info["root"], empty_info["bounds"], mask=0xFF), # empty BLAS
|
||||
X.Instance([[1, 0, 0, 3], [0, 1, 0, 0], [0, 0, 1, 0]], va_active, active_info["root"],
|
||||
active_info["bounds"], mask=0xFF),
|
||||
X.Instance(ident, 0, X.INVALID, empty_info["bounds"], mask=0xFF), # 5 instances -> 2-level TLAS
|
||||
]
|
||||
for box16 in (False, True):
|
||||
with self.subTest(box16=box16):
|
||||
tlas, info = X.build_tlas(insts, box16=box16)
|
||||
self.assertEqual(X.validate(tlas, expect_bottom=False), [])
|
||||
self.assertEqual(info["bounds"], ([3.0, 0.0, 0.0], [4.0, 1.0, 0.0]))
|
||||
va_tlas = mem.place(0x300000 + (0x10000 if box16 else 0), tlas)
|
||||
scene = [
|
||||
(zero, 0xFF, []),
|
||||
(ident, 0, [TRI]),
|
||||
(ident, 0xFF, [[[math.nan, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]]),
|
||||
([[1, 0, 0, 3], [0, 1, 0, 0], [0, 0, 1, 0]], 0xFF, [TRI]),
|
||||
(ident, 0xFF, []),
|
||||
]
|
||||
for o, d in (([3.25, 0.25, 1.0], [0.0, 0.0, -1.0]), ([0.25, 0.25, 1.0], [0.0, 0.0, -1.0])):
|
||||
got = X.trace(mem, va_tlas, o, d)
|
||||
want = X.brute_force(scene, o, d)
|
||||
self.assertTrue(same_hit(got, want), (o, got, want))
|
||||
self.assertEqual(X.trace(mem, va_tlas, [3.25, 0.25, 1.0], [0.0, 0.0, -1.0])["instance"], 3)
|
||||
|
||||
def test_pair_triangles_skips_zero_length_edges(self):
|
||||
# Two degenerate triangles sharing only a zero-length edge (0,0,0)->(0,0,0) must not pair.
|
||||
tris = [
|
||||
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0]],
|
||||
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
|
||||
]
|
||||
data, info = X.build_blas(tris, pair=True)
|
||||
self.assertEqual(X.validate(data, expect_bottom=True), [])
|
||||
self.assertEqual(X.count_pairs(data), 0)
|
||||
self.assertEqual(info["leaves"], 2)
|
||||
|
||||
def test_box_test_misses_nan_bounds(self):
|
||||
for d in ([0.5, 0.5, -1.0], [0.0, 0.0, -1.0]):
|
||||
with self.subTest(d=d):
|
||||
inv = X._inverse_dir(d)
|
||||
self.assertEqual(X._box_test(10.0, [0.0, 0.0, 1.0], d, inv, [math.nan, 0.0, 0.0], [math.nan, 1.0, 0.0]),
|
||||
math.inf)
|
||||
self.assertEqual(X._box_test(10.0, [0.0, 0.0, 1.0], d, inv, [0.0, 0.0, 0.0], [1.0, math.nan, 0.0]),
|
||||
math.inf)
|
||||
|
||||
def test_nan_transform_falls_back_to_zero_inverse_in_tlas(self):
|
||||
_, active_info = X.build_blas([TRI])
|
||||
nan_rot = [[math.nan, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
|
||||
nan_trans = [[1, 0, 0, math.nan], [0, 1, 0, 0], [0, 0, 1, 0]]
|
||||
for matrix in (nan_rot, nan_trans):
|
||||
with self.subTest(matrix=matrix):
|
||||
with self.assertRaises(ValueError):
|
||||
X.invert_affine(matrix)
|
||||
inst = X.Instance(matrix, 0x10000, active_info["root"], active_info["bounds"])
|
||||
tlas, info = X.build_tlas([inst])
|
||||
self.assertEqual(X.validate(tlas, expect_bottom=False), [])
|
||||
self.assertEqual(info["bounds"], ([math.inf] * 3, [-math.inf] * 3))
|
||||
off = X.ptr_offset(struct.unpack_from("<I", tlas, X.ptr_offset(info["root"]))[0])
|
||||
self.assertEqual(struct.unpack_from("<12f", tlas, off + 64), (0.0,) * 12)
|
||||
|
||||
|
||||
class Validate(unittest.TestCase):
|
||||
def setUp(self):
|
||||
@ -353,6 +462,13 @@ class Validate(unittest.TestCase):
|
||||
def test_short_buffer(self):
|
||||
self.assertEqual(X.validate(b"\0" * 10), ["buffer shorter than the 64-byte header"])
|
||||
|
||||
def test_nan_child_box_at_root_is_rejected(self):
|
||||
data, info = X.build_blas([TRI])
|
||||
b = bytearray(data)
|
||||
root = X.ptr_offset(info["root"])
|
||||
struct.pack_into("<f", b, root + 16, math.nan)
|
||||
self.assertTrue(any("NaN" in e for e in X.validate(bytes(b), expect_bottom=True)))
|
||||
|
||||
|
||||
|
||||
def console_layout(data, k):
|
||||
|
||||
@ -144,6 +144,20 @@ def encode_box16(lo, hi):
|
||||
|
||||
|
||||
# ---- geometry helpers ----
|
||||
def _empty_box():
|
||||
return [INF, INF, INF], [-INF, -INF, -INF]
|
||||
|
||||
|
||||
def _box_empty(box):
|
||||
lo, hi = box
|
||||
return not all(lo[c] <= hi[c] for c in range(3))
|
||||
|
||||
|
||||
def _tri_inactive(tri):
|
||||
"""Treat a NaN in any vertex component as inactive (matches runtime/src/xdxr_build.comp for static builds)."""
|
||||
return any(math.isnan(c) for p in tri for c in p)
|
||||
|
||||
|
||||
def _bounds(points):
|
||||
lo = [min(p[c] for p in points) for c in range(3)]
|
||||
hi = [max(p[c] for p in points) for c in range(3)]
|
||||
@ -151,8 +165,11 @@ def _bounds(points):
|
||||
|
||||
|
||||
def _union(boxes):
|
||||
lo = [min(b[0][c] for b in boxes) for c in range(3)]
|
||||
hi = [max(b[1][c] for b in boxes) for c in range(3)]
|
||||
live = [b for b in boxes if not _box_empty(b)]
|
||||
if not live:
|
||||
return _empty_box()
|
||||
lo = [min(b[0][c] for b in live) for c in range(3)]
|
||||
hi = [max(b[1][c] for b in live) for c in range(3)]
|
||||
return lo, hi
|
||||
|
||||
|
||||
@ -170,8 +187,8 @@ def invert_affine(m):
|
||||
det = (a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1])
|
||||
- a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0])
|
||||
+ a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]))
|
||||
if det == 0:
|
||||
raise ValueError("singular instance transform")
|
||||
if det == 0 or not math.isfinite(det):
|
||||
raise ValueError("singular or non-finite instance transform")
|
||||
inv = [[0.0] * 3 for _ in range(3)]
|
||||
for r in range(3):
|
||||
for c in range(3):
|
||||
@ -179,20 +196,31 @@ def invert_affine(m):
|
||||
c1, c2 = (r + 1) % 3, (r + 2) % 3
|
||||
inv[r][c] = (a[r1][c1] * a[r2][c2] - a[r1][c2] * a[r2][c1]) / det
|
||||
t = [row[3] for row in m]
|
||||
return [inv[r] + [-(inv[r][0] * t[0] + inv[r][1] * t[1] + inv[r][2] * t[2])] for r in range(3)]
|
||||
out = [inv[r] + [-(inv[r][0] * t[0] + inv[r][1] * t[1] + inv[r][2] * t[2])] for r in range(3)]
|
||||
if any(not math.isfinite(x) for row in out for x in row):
|
||||
raise ValueError("singular or non-finite instance transform")
|
||||
return out
|
||||
|
||||
|
||||
# ---- tree construction (ASSUMPTION: the heuristic is ours; any valid BVH4 traverses the same) ----
|
||||
def _split4(items, centre):
|
||||
"""Split items into up to four groups by two median cuts along the widest centroid axes."""
|
||||
"""Split items into up to four groups by two median cuts along the widest centroid axes.
|
||||
Items with an empty box (``centre(i) is None``) sort last, matching xdxr_build.comp MODE_MORTON."""
|
||||
def cut(group):
|
||||
if len(group) < 2:
|
||||
return [group]
|
||||
lo, hi = _bounds([centre(i) for i in group])
|
||||
axis = max(range(3), key=lambda c: hi[c] - lo[c])
|
||||
group = sorted(group, key=lambda i: centre(i)[axis])
|
||||
mid = len(group) // 2
|
||||
return [group[:mid], group[mid:]]
|
||||
live = [i for i in group if centre(i) is not None]
|
||||
if not live:
|
||||
mid = len(group) // 2
|
||||
return [group[:mid], group[mid:]]
|
||||
empty = [i for i in group if centre(i) is None]
|
||||
if len(live) >= 2:
|
||||
lo, hi = _bounds([centre(i) for i in live])
|
||||
axis = max(range(3), key=lambda c: hi[c] - lo[c])
|
||||
live = sorted(live, key=lambda i: centre(i)[axis])
|
||||
ordered = live + empty
|
||||
mid = len(ordered) // 2
|
||||
return [ordered[:mid], ordered[mid:]]
|
||||
out = []
|
||||
for half in cut(items):
|
||||
out.extend(g for g in cut(half) if g)
|
||||
@ -275,36 +303,38 @@ def _write_header(buf, flags, end, n_elements, bounds, nodes_off, root, elements
|
||||
nodes_off, 0, root, elements_off, parents_off, 0)
|
||||
|
||||
|
||||
def _remap_for(tri, order):
|
||||
"""Remap byte that returns D3D's barycentrics (the weights of the triangle's own vertices 1 and 2) when the
|
||||
node reads the vertices in ``order``: the edge function of slot s weighs order[s]."""
|
||||
order = [tuple(p) for p in order]
|
||||
return order.index(tuple(tri[1])) | order.index(tuple(tri[2])) << 2
|
||||
|
||||
|
||||
def _pair_triangles(tris):
|
||||
"""Greedy pairing of triangles that share an edge into one leaf (TRI0 = v0 v1 v2, TRI1 = v1 v3 v2). Returns a
|
||||
list of leaves, each a list of (slots, remap, prim) entries plus the five vertex slots."""
|
||||
"""Greedy pairing of active triangles that share an edge into one leaf (TRI0 = v0 v1 v2, TRI1 = v1 v3 v2).
|
||||
Inactive (NaN) triangles and zero-length edges are never paired, matching runtime/src/xdxr_build.comp. Returns a
|
||||
list of leaves, each a list of (slots, remap, prim) entries plus the four vertex slots."""
|
||||
leaves = []
|
||||
used = [False] * len(tris)
|
||||
inactive = [_tri_inactive(t) for t in tris]
|
||||
edges = {}
|
||||
for i, t in enumerate(tris):
|
||||
if inactive[i]:
|
||||
continue
|
||||
for k in range(3):
|
||||
a, b = tuple(t[k]), tuple(t[(k + 1) % 3])
|
||||
if a == b:
|
||||
continue
|
||||
edges.setdefault((b, a), []).append((i, k)) # an opposite-winding neighbour has edge (b, a)
|
||||
for i, t in enumerate(tris):
|
||||
if used[i]:
|
||||
continue
|
||||
used[i] = True
|
||||
mate = None
|
||||
for k in range(3):
|
||||
a, b = tuple(t[k]), tuple(t[(k + 1) % 3])
|
||||
for j, kj in edges.get((a, b), []):
|
||||
if not used[j]:
|
||||
mate = (k, j, kj)
|
||||
if not inactive[i]:
|
||||
for k in range(3):
|
||||
a, b = tuple(t[k]), tuple(t[(k + 1) % 3])
|
||||
if a == b:
|
||||
continue
|
||||
for j, kj in edges.get((a, b), []):
|
||||
if not used[j]:
|
||||
mate = (k, j, kj)
|
||||
break
|
||||
if mate:
|
||||
break
|
||||
if mate:
|
||||
break
|
||||
if not mate:
|
||||
v = [list(t[0]), list(t[1]), list(t[2]), list(t[2])]
|
||||
leaves.append((v, [(TRI0, REMAP_IDENTITY, i)]))
|
||||
@ -316,7 +346,11 @@ def _pair_triangles(tris):
|
||||
q = tris[j]
|
||||
p3 = q[(kj + 2) % 3] # the mate's vertex off the shared edge
|
||||
v = [list(p0), list(p1), list(p2), list(p3)]
|
||||
leaves.append((v, [(TRI0, _remap_for(t, (p0, p1, p2)), i), (TRI1, _remap_for(q, (p1, p3, p2)), j)]))
|
||||
# TRI0 reads (p0, p1, p2): vertex m of t is in slot (m + 4 - k) % 3.
|
||||
# TRI1 reads (p1, p3, p2): vertex m of q is in slot (m + 5 - kj) % 3.
|
||||
remap0 = ((5 - k) % 3) | (((6 - k) % 3) << 2)
|
||||
remap1 = ((6 - kj) % 3) | (((7 - kj) % 3) << 2)
|
||||
leaves.append((v, [(TRI0, remap0, i), (TRI1, remap1, j)]))
|
||||
return leaves
|
||||
|
||||
|
||||
@ -335,10 +369,11 @@ def build_blas(triangles, geometry_index=0, flags=0, box16=False, pair=False, op
|
||||
leaves = [([list(t[0]), list(t[1]), list(t[2]), list(t[2])], [(TRI0, REMAP_IDENTITY, i)])
|
||||
for i, t in enumerate(tris)]
|
||||
# the tree's leaves are triangle references (leaf record, node type): a paired record is reached through two
|
||||
# pointers, each with its own triangle's box
|
||||
# pointers, each with its own triangle's box. Inactive (NaN) triangles get an empty box (+inf, -inf).
|
||||
refs = [(h, kind, prim) for h, (_, entries) in enumerate(leaves) for kind, _, prim in entries]
|
||||
ref_box = {r: _bounds(tris[r[2]]) for r in refs}
|
||||
centre = lambda r: [(ref_box[r][0][c] + ref_box[r][1][c]) * 0.5 for c in range(3)]
|
||||
ref_box = {r: _empty_box() if _tri_inactive(tris[r[2]]) else _bounds(tris[r[2]]) for r in refs}
|
||||
centre = lambda r: (None if _box_empty(ref_box[r])
|
||||
else [(ref_box[r][0][c] + ref_box[r][1][c]) * 0.5 for c in range(3)])
|
||||
root = _build_tree(refs, centre, lambda r: ref_box[r])
|
||||
inner = _inner_nodes(root)
|
||||
parents_off, elements_off, nodes_off, end, node_size = _layout(len(leaves), LEAF_SIZE, inner, box16)
|
||||
@ -386,9 +421,13 @@ class Instance:
|
||||
|
||||
|
||||
def _world_box(inst):
|
||||
if inst.blas_va == 0 or (inst.mask & 0xFF) == 0 or _box_empty(inst.blas_bounds):
|
||||
return _empty_box()
|
||||
lo, hi = inst.blas_bounds
|
||||
corners = [[(lo, hi)[(i >> c) & 1][c] for c in range(3)] for i in range(8)]
|
||||
pts = [_transform_point(inst.transform, p) for p in corners]
|
||||
if any(math.isnan(c) for p in pts for c in p):
|
||||
return _empty_box()
|
||||
return _bounds(pts)
|
||||
|
||||
|
||||
@ -401,7 +440,8 @@ def build_tlas(instances, box16=False):
|
||||
raise ValueError("BLAS addresses must be 256-byte aligned")
|
||||
boxes = {i: _world_box(inst) for i, inst in enumerate(instances)}
|
||||
handles = list(range(len(instances)))
|
||||
centre = lambda h: [(boxes[h][0][c] + boxes[h][1][c]) * 0.5 for c in range(3)]
|
||||
centre = lambda h: (None if _box_empty(boxes[h])
|
||||
else [(boxes[h][0][c] + boxes[h][1][c]) * 0.5 for c in range(3)])
|
||||
root = _build_tree(handles, centre, lambda h: boxes[h])
|
||||
inner = _inner_nodes(root)
|
||||
parents_off, elements_off, nodes_off, end, node_size = _layout(len(instances), INSTANCE_SIZE, inner, box16)
|
||||
@ -409,9 +449,16 @@ def build_tlas(instances, box16=False):
|
||||
inst_ptrs = {}
|
||||
for h, inst in enumerate(instances):
|
||||
off = elements_off + h * INSTANCE_SIZE
|
||||
inv = invert_affine(inst.transform)
|
||||
if _box_empty(boxes[h]):
|
||||
try:
|
||||
inv = invert_affine(inst.transform)
|
||||
except ValueError:
|
||||
inv = [[0.0] * 4 for _ in range(3)]
|
||||
else:
|
||||
inv = invert_affine(inst.transform)
|
||||
identity_rot = all(inst.transform[r][c] == (1.0 if r == c else 0.0) for r in range(3) for c in range(3))
|
||||
internal = (inst.blas_root & ~INST_FLAGS_MASK) | (INST_IDENTITY_ROTATION if identity_rot else 0)
|
||||
inst_root = INVALID if inst.blas_va == 0 else inst.blas_root
|
||||
internal = (inst_root & ~INST_FLAGS_MASK) | (INST_IDENTITY_ROTATION if identity_rot else 0)
|
||||
struct.pack_into("<12f", buf, off, *[x for row in inst.transform for x in row])
|
||||
struct.pack_into("<4I", buf, off + 48, (inst.instance_id & 0xFFFFFF) | (inst.mask & 0xFF) << 24,
|
||||
(inst.contribution & 0xFFFFFF) | (inst.flags & 0xFF) << 24, internal,
|
||||
@ -461,7 +508,7 @@ class Memory:
|
||||
|
||||
def _box_test(extent, org, d, inv, lo, hi):
|
||||
"""Entry distance of the ray into the box, or inf on a miss (TestXboxBvh4Node / RayBoxTest)."""
|
||||
if any(lo[c] > hi[c] for c in range(3)):
|
||||
if not all(lo[c] <= hi[c] for c in range(3)):
|
||||
return INF
|
||||
tnear, tfar = 0.0, extent
|
||||
for c in range(3):
|
||||
@ -635,7 +682,7 @@ def brute_force(scene, org, d, tmax=INF, mask=0xFF):
|
||||
``scene`` is a list of (transform or None, mask, triangles); the result names the scene index as instance."""
|
||||
best = None
|
||||
for idx, (m, imask, tris) in enumerate(scene):
|
||||
if m is not None and not imask & mask:
|
||||
if not tris or (m is not None and not imask & mask):
|
||||
continue
|
||||
if m is None:
|
||||
lo, ld = org, d
|
||||
@ -735,7 +782,9 @@ def validate(data, expect_bottom=None):
|
||||
lo, hi = b[:3], b[3:]
|
||||
if k == INVALID:
|
||||
continue
|
||||
if box and not all(box[0][c] <= lo[c] and hi[c] <= box[1][c] for c in range(3)):
|
||||
if any(math.isnan(c) for c in (*lo, *hi)):
|
||||
errs.append(f"child box {i} of {p:#x} contains NaN")
|
||||
elif box and not all(box[0][c] <= lo[c] and hi[c] <= box[1][c] for c in range(3)):
|
||||
errs.append(f"child box {i} of {p:#x} is not inside its parent's box")
|
||||
stack.append((k, (lo, hi), p))
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user