upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorzneel <14263028+zneel@users.noreply.github.com>2026-11-22 20:16:04 +0100
committerzneel <14263028+zneel@users.noreply.github.com>2026-11-22 23:30:06 +0100
commitb8c07f8ccfe866987a54ff52e0e0e9a050d2ac8a (patch)
treee36521241de0927aca478a3acd1173ca010ea744 /runtime/src
parenta3d682840fb2e2332b1f5ea40915d9a5fc25eac1 (diff)
XDXR builder: pair sibling triangles into TRI0/TRI1 leaves
A bottom-level build without ALLOW_UPDATE now pairs triangles, as the console builder does in its offline BLASes. The pass that writes the nodes above the leaves pairs a triangle with a later sibling (among that node's at most four children) that shares an edge in the opposite direction (the same vertex bits), from the same geometry, both active. The first triangle is rotated to put the shared edge in slots 1 -> 2, the sibling's third vertex goes in slot 3, its primitive in PrimitiveIndex2, and each node type's remap byte returns D3D12's barycentrics for its own vertex order. The node keeps one child per triangle with its own box, so the tree, the layout and the sizes do not change; the TRI1 child points at the first leaf and the sibling's leaf is left unused. Pairs never cross nodes: one thread owns each rewritten leaf and each parent-table entry. A refit rewrites the leaves one triangle each, so the layer does not pair builds with ALLOW_UPDATE (xdxr_pair), and record_blas ignores `pair` for updates. check-build now compares barycentrics as well as hits, and checks a case's "pairs" field: at least one TRI1 node, or none. New cases: paired grids (BOX32 and BOX16), a transform, a mixed scene, the radix path, and two that must not pair (pairs split across geometries, edges shared in the same direction), plus a D3D12 BLAS without ALLOW_UPDATE. Mutating the geometry or the direction rule, or the remap byte, fails check-build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/vulkan_backend.h9
-rw-r--r--runtime/src/xdxr_build.comp78
-rw-r--r--runtime/src/xdxr_build.h10
3 files changed, 90 insertions, 7 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index f47abccb3..8c8a19d2f 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -897,7 +897,8 @@ public:
897 } else { 897 } else {
898 std::vector<XdxrTriangles> geometries; 898 std::vector<XdxrTriangles> geometries;
899 xdxr_inputs(b, &geometries, base); 899 xdxr_inputs(b, &geometries, base);
900 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va, from); 900 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va, from,
901 xdxr_pair(b));
901 } 902 }
902 }; 903 };
903 replay_.xdxr_copy = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dst, std::uint64_t bytes) { 904 replay_.xdxr_copy = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dst, std::uint64_t bytes) {
@@ -1116,6 +1117,12 @@ public:
1116 static bool xdxr_box16(const BuildAccelerationStructure& b) { 1117 static bool xdxr_box16(const BuildAccelerationStructure& b) {
1117 return !b.is_tlas && (b.build_flags & VK_BUILD_ACCELERATION_STRUCTURE_LOW_MEMORY_BIT_KHR) != 0; 1118 return !b.is_tlas && (b.build_flags & VK_BUILD_ACCELERATION_STRUCTURE_LOW_MEMORY_BIT_KHR) != 0;
1118 } 1119 }
1120 // Whether a bottom-level build pairs sibling triangles into TRI0/TRI1 leaves: not when the structure may be
1121 // updated (ALLOW_UPDATE), since a refit rewrites the leaves one triangle each. Pairing changes neither the layout
1122 // nor the tree, so prebuild info does not depend on it.
1123 static bool xdxr_pair(const BuildAccelerationStructure& b) {
1124 return !b.is_tlas && !b.update && (b.build_flags & VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR) == 0;
1125 }
1119 // Triangles (BLAS) or instances (TLAS) a build has: what XdxrLayout is computed from, for the build and for 1126 // Triangles (BLAS) or instances (TLAS) a build has: what XdxrLayout is computed from, for the build and for
1120 // GetRaytracingAccelerationStructurePrebuildInfo. 1127 // GetRaytracingAccelerationStructurePrebuildInfo.
1121 static std::uint32_t xdxr_elements(const BuildAccelerationStructure& b) { 1128 static std::uint32_t xdxr_elements(const BuildAccelerationStructure& b) {
diff --git a/runtime/src/xdxr_build.comp b/runtime/src/xdxr_build.comp
index 04ec353e6..6191543d6 100644
--- a/runtime/src/xdxr_build.comp
+++ b/runtime/src/xdxr_build.comp
@@ -60,9 +60,9 @@ const uint SMALL_SORT_MAX = 1024u; // xdxr_build.h XdxrLayout::small_so
60const uint RADIX_BITS = 4u, RADIX_BINS = 16u, RADIX_PASSES = 8u; 60const uint RADIX_BITS = 4u, RADIX_BINS = 16u, RADIX_PASSES = 8u;
61const uint RADIX_TILE = 256u; // elements per workgroup in count and scatter: 4 chunks of 64 61const uint RADIX_TILE = 256u; // elements per workgroup in count and scatter: 4 chunks of 64
62const uint INVALID = 0xffffffffu; 62const uint INVALID = 0xffffffffu;
63const uint TRI0 = 0u, BOX16 = 4u, BOX32 = 5u, INSTANCE = 6u; 63const uint TRI0 = 0u, TRI1 = 1u, BOX16 = 4u, BOX32 = 5u, INSTANCE = 6u;
64const uint REMAP_IDENTITY = 0x09u; 64const uint REMAP_IDENTITY = 0x09u;
65const uint FLAG_TLAS = 1u, FLAG_BOX16 = 2u; // pc.flags; xdxr_build.h XdxrBuilder flag_tlas, flag_box16 65const uint FLAG_TLAS = 1u, FLAG_BOX16 = 2u, FLAG_PAIR = 4u; // pc.flags; xdxr_build.h XdxrBuilder flag_*
66const uint FLAG_BOTTOM_LEVEL = 0x80000000u, VERSION_2403 = 6u, INST_IDENTITY_ROTATION = 0x80000000u, 66const uint FLAG_BOTTOM_LEVEL = 0x80000000u, VERSION_2403 = 6u, INST_IDENTITY_ROTATION = 0x80000000u,
67 INST_FLAGS_MASK = 0xc0000000u; 67 INST_FLAGS_MASK = 0xc0000000u;
68 68
@@ -87,7 +87,8 @@ layout(push_constant) uniform Push {
87 uint nodes_off; 87 uint nodes_off;
88 uint parents_off; 88 uint parents_off;
89 uint total_size; 89 uint total_size;
90 uint flags; // FLAG_TLAS: elements are 128-byte instances; FLAG_BOX16: inner nodes are 64-byte BOX16 90 uint flags; // FLAG_TLAS: elements are 128-byte instances; FLAG_BOX16: inner nodes are 64-byte BOX16;
91 // FLAG_PAIR: pair sibling triangles that share an edge into TRI0/TRI1 leaves
91} pc; 92} pc;
92 93
93// ---- 64-bit addresses as uvec2 ---- 94// ---- 64-bit addresses as uvec2 ----
@@ -429,11 +430,80 @@ void radix_scatter(uint w, uint t) {
429} 430}
430 431
431// ---- MODE_LEVEL ---- 432// ---- MODE_LEVEL ----
433// TRI1 pairing (FLAG_PAIR, bottom level, the level above the leaves): among one node's children, a triangle and a
434// later sibling that shares an edge with it, traversed in the opposite direction (the same two vertices, bit for bit),
435// from the same geometry and both active, go into the first one's leaf: TRI0 reads slots (0, 1, 2) and TRI1 (1, 3, 2)
436// (docs/bvh.md 7a), so the first triangle is rotated to put the shared edge in slots 1 -> 2, the sibling's third
437// vertex goes in slot 3, its primitive index in PrimitiveIndex2 and its remap byte in byte 1. The node keeps one child
438// per triangle, each with its own box, so the tree is unchanged: the TRI1 child points at the first triangle's leaf
439// and the sibling's own leaf is left unreferenced. Both pointers have this node as parent. Pairs never cross nodes,
440// so no other thread touches these leaves. The barycentric remap byte of a node type returns D3D12's (b1, b2): bits
441// [1:0] and [3:2] are the slots holding the triangle's vertices 1 and 2.
442uint leaf_off(uint e) { return pc.elements_off + 64u * e; }
443uvec3 leaf_vertex(uint e, uint k) {
444 uint o = leaf_off(e) + 12u * k;
445 return uvec3(load(pc.dst, o), load(pc.dst, o + 4u), load(pc.dst, o + 8u));
446}
447void store_vertex(uint e, uint slot, uvec3 v) {
448 uint o = leaf_off(e) + 12u * slot;
449 store(pc.dst, o, v.x); store(pc.dst, o + 4u, v.y); store(pc.dst, o + 8u, v.z);
450}
451void pair_children(uint n, out uint kids[4]) {
452 uint e[4];
453 uvec3 v[4][3];
454 uint geo[4];
455 bool live[4], used[4];
456 uint count = min(4u, pc.arg0 - 4u * n);
457 for (uint c = 0u; c < 4u; ++c) { kids[c] = INVALID; live[c] = false; used[c] = false; e[c] = 0u; geo[c] = 0u; }
458 for (uint c = 0u; c < count; ++c) {
459 e[c] = load(pc.scratch, vals_off() + 4u * (4u * n + c));
460 kids[c] = element_ptr(e[c]);
461 geo[c] = load(pc.dst, leaf_off(e[c]) + 48u);
462 for (uint k = 0u; k < 3u; ++k) v[c][k] = leaf_vertex(e[c], k);
463 vec3 lo, hi;
464 load_box(e[c], lo, hi);
465 live[c] = !box_empty(lo, hi); // a triangle with a NaN vertex is inactive: never paired
466 }
467 for (uint c = 0u; c < count; ++c) {
468 if (used[c] || !live[c]) continue;
469 used[c] = true;
470 for (uint d = c + 1u; d < count; ++d) {
471 if (used[d] || !live[d] || geo[d] != geo[c]) continue;
472 uint k = 3u, kd = 3u;
473 for (uint a = 0u; a < 3u && k == 3u; ++a) {
474 uvec3 p = v[c][a], q = v[c][(a + 1u) % 3u]; // edge p -> q of the first triangle
475 if (all(equal(p, q))) continue;
476 for (uint b = 0u; b < 3u; ++b) {
477 if (all(equal(v[d][b], q)) && all(equal(v[d][(b + 1u) % 3u], p))) { k = a; kd = b; break; }
478 }
479 }
480 if (k == 3u) continue;
481 used[d] = true;
482 // the first triangle as (p0, p1, p2) with the shared edge p1 -> p2; slot 3 is the sibling's third vertex
483 store_vertex(e[c], 0u, v[c][(k + 2u) % 3u]);
484 store_vertex(e[c], 1u, v[c][k]);
485 store_vertex(e[c], 2u, v[c][(k + 1u) % 3u]);
486 store_vertex(e[c], 3u, v[d][(kd + 2u) % 3u]);
487 store(pc.dst, leaf_off(e[c]) + 56u, load(pc.dst, leaf_off(e[d]) + 52u));
488 // TRI0 reads (p0, p1, p2): vertex m of the first triangle is in slot (m + 4 - k) % 3. TRI1 reads
489 // (p1, p3, p2): vertex m of the sibling is in slot (m + 5 - kd) % 3.
490 uint remap0 = (5u - k) % 3u | ((6u - k) % 3u) << 2;
491 uint remap1 = (6u - kd) % 3u | ((7u - kd) % 3u) << 2;
492 store(pc.dst, leaf_off(e[c]) + 60u, remap0 | remap1 << 8);
493 kids[d] = (kids[c] & ~7u) | TRI1;
494 break;
495 }
496 }
497}
498
432void build_node(uint n) { 499void build_node(uint n) {
433 uint node = pc.first + n; 500 uint node = pc.first + n;
434 uint me = node_ptr(node); 501 uint me = node_ptr(node);
435 uint o = pc.nodes_off + node_size() * node; 502 uint o = pc.nodes_off + node_size() * node;
436 vec3 ulo = vec3(uintBitsToFloat(0x7f800000u)), uhi = -ulo; 503 vec3 ulo = vec3(uintBitsToFloat(0x7f800000u)), uhi = -ulo;
504 uint kids[4];
505 bool pairing = pc.arg2 != 1u && pc.arg1 == INVALID && !is_tlas() && (pc.flags & FLAG_PAIR) != 0u;
506 if (pairing) pair_children(n, kids);
437 for (uint c = 0u; c < 4u; ++c) { 507 for (uint c = 0u; c < 4u; ++c) {
438 uint entry = 4u * n + c; 508 uint entry = 4u * n + c;
439 uint child = INVALID; 509 uint child = INVALID;
@@ -456,7 +526,7 @@ void build_node(uint n) {
456 } else if (entry < pc.arg0) { 526 } else if (entry < pc.arg0) {
457 if (pc.arg1 == INVALID) { 527 if (pc.arg1 == INVALID) {
458 uint e = load(pc.scratch, vals_off() + 4u * entry); 528 uint e = load(pc.scratch, vals_off() + 4u * entry);
459 child = element_ptr(e); 529 child = pairing ? kids[c] : element_ptr(e);
460 load_box(e, lo, hi); 530 load_box(e, lo, hi);
461 if (is_tlas()) store(pc.dst, pc.elements_off + 128u * e + 116u, me); 531 if (is_tlas()) store(pc.dst, pc.elements_off + 128u * e + 116u, me);
462 } else { 532 } else {
diff --git a/runtime/src/xdxr_build.h b/runtime/src/xdxr_build.h
index f932bc159..683125ff0 100644
--- a/runtime/src/xdxr_build.h
+++ b/runtime/src/xdxr_build.h
@@ -120,10 +120,16 @@ public:
120 // destination itself, or another one, which is copied first) was built from the same topology, and is refitted 120 // destination itself, or another one, which is copied first) was built from the same topology, and is refitted
121 // to the new positions. Its tree is kept and only its boxes change, which is valid for any motion and costs no 121 // to the new positions. Its tree is kept and only its boxes change, which is valid for any motion and costs no
122 // sort, though a refitted tree traverses more slowly as the geometry moves away from where it was built. 122 // sort, though a refitted tree traverses more slowly as the geometry moves away from where it was built.
123 //
124 // `pair`: sibling triangles that share an edge go into one leaf as TRI0 and TRI1 (xdxr_build.comp MODE_LEVEL).
125 // The layout and the tree are the same either way. A refit rewrites every leaf as one triangle, so a structure
126 // that will be updated must be built without pairing (the layer pairs only builds without ALLOW_UPDATE), and an
127 // update ignores `pair`.
123 bool record_blas(VkCommandBuffer cb, const XdxrLayout& layout, const std::vector<XdxrTriangles>& geometries, 128 bool record_blas(VkCommandBuffer cb, const XdxrLayout& layout, const std::vector<XdxrTriangles>& geometries,
124 std::uint64_t dst, std::uint64_t scratch, std::uint64_t update_from = 0) { 129 std::uint64_t dst, std::uint64_t scratch, std::uint64_t update_from = 0, bool pair = false) {
125 if (!loaded_ || layout.tlas) return false; 130 if (!loaded_ || layout.tlas) return false;
126 Push p = base(layout, dst, scratch); 131 Push p = base(layout, dst, scratch);
132 if (pair && !update_from) p.flags |= flag_pair;
127 if (update_from) refit_begin(cb, layout, p, update_from); else begin(cb, layout, p); 133 if (update_from) refit_begin(cb, layout, p, update_from); else begin(cb, layout, p);
128 std::uint32_t first = 0; 134 std::uint32_t first = 0;
129 for (std::size_t g = 0; g < geometries.size(); ++g) { 135 for (std::size_t g = 0; g < geometries.size(); ++g) {
@@ -212,7 +218,7 @@ private:
212 std::uint32_t mode = 0, count = 0, first = 0, arg0 = 0, arg1 = 0, arg2 = 0, geometry = 0, elements = 0, 218 std::uint32_t mode = 0, count = 0, first = 0, arg0 = 0, arg1 = 0, arg2 = 0, geometry = 0, elements = 0,
213 sort_size = 0, elements_off = 0, nodes_off = 0, parents_off = 0, total_size = 0, flags = 0; 219 sort_size = 0, elements_off = 0, nodes_off = 0, parents_off = 0, total_size = 0, flags = 0;
214 }; 220 };
215 enum : std::uint32_t { flag_tlas = 1, flag_box16 = 2 }; // xdxr_build.comp FLAG_TLAS, FLAG_BOX16 221 enum : std::uint32_t { flag_tlas = 1, flag_box16 = 2, flag_pair = 4 }; // xdxr_build.comp FLAG_*
216 static_assert(sizeof(Push) == 104, "push constants must match xdxr_build.comp"); 222 static_assert(sizeof(Push) == 104, "push constants must match xdxr_build.comp");
217 223
218 static Addr split(std::uint64_t a) { return {std::uint32_t(a), std::uint32_t(a >> 32)}; } 224 static Addr split(std::uint64_t a) { return {std::uint32_t(a), std::uint32_t(a >> 32)}; }