Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
dcac757
d3d12: serialize and deserialize XDXR acceleration structures (RT-XDX…
kawaii-shakal Nov 24, 2026
c779bd3
xdxr: a paired BLAS keeps one 64-byte leaf a pair (RT-XDXR-PAIR-DENSE)
kawaii-shakal Nov 24, 2026
a98d676
xdxr: write a pair's leaf when placing it, not in place (RT-XDXR-PAIR…
kawaii-shakal Nov 24, 2026
acb9bb5
test: the XDXR postbuild readback-stage case takes the postbuild type…
kawaii-shakal Nov 25, 2026
489d2b2
test: the XDXR postbuild readback-stage case takes the postbuild type…
kawaii-shakal Nov 25, 2026
78d3d92
Merge upstream XDXR address handling into serialization
kawaii-shakal Nov 25, 2026
fb2ea85
Merge updated XDXR serialization into dense pairing
kawaii-shakal Nov 25, 2026
8f7ea15
Size title-placed XDXR scratch for dense pairing
kawaii-shakal Nov 25, 2026
b8d18a9
Merge upstream main into XDXR serialization
codex Nov 25, 2026
7e63e1d
Merge updated XDXR serialization into dense pairing
codex Nov 25, 2026
dbf2361
Merge upstream/main into rt-xdxr-pair-dense
claude Nov 26, 2026
0980d4e
Merge upstream/main into rt-xdxr-serialize
claude Nov 26, 2026
fe8128b
Merge upstream main into PR #626
kawaii-shakal Nov 28, 2026
72c6ad1
Merge updated XDXR serialization branch into dense pairing
kawaii-shakal Nov 28, 2026
2c2710a
Merge upstream XDXR dispatch guards into serialization
kawaii-shakal Nov 28, 2026
ad0869a
Merge commit 'refs/codex-conf-now/ready/626' into HEAD
kawaii-shakal Nov 28, 2026
60771f8
Merge upstream main and keep the current project status in README
kawaii-shakal Nov 29, 2026
9d10175
Merge upstream main into the PR branch
kawaii-shakal Nov 29, 2026
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
72 changes: 56 additions & 16 deletions docs/bvh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1099,26 +1099,42 @@ zeroed 64-byte unit after the last node it wrote, and the header's size counts i
64-byte node would otherwise be read past the structure, or past a compacted copy of it.

**TRI1 pairing.** A bottom-level build without `ALLOW_UPDATE` pairs triangles into TRI0/TRI1 leaves, as the console
builder does in its offline BLASes (§4a, check 4 of "How a match is verified"). The pass that writes the nodes
pairs each triangle child of a node with a later triangle child of the same node (among its at most four children)
builder does in its offline BLASes (§4a, check 4 of "How a match is verified"). A pass after the tree is known
(`MODE_PAIR`) pairs each triangle child of a node with a later triangle child of the same node (among its at most four children)
when four things hold. The two must share an edge in opposite directions, with the same two vertices bit for bit. They must come from
the same geometry, since a leaf holds one `GeometryIndexAndFlags`. And both must be active. The first triangle is
rotated so the shared edge is slots 1 -> 2, the sibling's third vertex goes in slot 3, its primitive index in
`PrimitiveIndex2`, and each node type's remap byte returns D3D12's barycentrics for its own vertex order. The node
still has one child per triangle, each with its own box: the TRI1 child points at the first triangle's leaf, and the
sibling's own leaf is left unused. The tree, the layout and the sizes are therefore the same as without pairing.
Nothing is saved yet: reclaiming the unused leaves would need a compaction that moves records and rewrites pointers,
and compaction here copies bytes. What pairing gives is the console's leaf format, with a pair's two triangles in
one 64-byte record. Because pairs never cross nodes, one thread owns each leaf it rewrites, and both pointers of a
pair have the same parent, so the parent table has one writer per entry. The pass records each triangle's pointer
still has one child per triangle, each with its own box: the TRI1 child points at the first triangle's leaf, so the
tree is the same as without pairing.

The leaves are laid out densely, one 64-byte record a pair, as the console's are (§4a: one `Leaf` per entry, an entry
holding the second primitive). A paired build writes its leaves to scratch first; once the pairs are chosen, a scan
over the leaves that stay (the dropped ones are the siblings') gives each its place, they are copied into the
element region in element order, and the nodes start right after the last one. `offsetToNodes` (+40) says where, and
the nodes, their pointers and the parent table are written for those places directly, so nothing is moved
afterwards. `numElements` stays the triangle count, as `build_blas` writes it, the count of triangle pointers. The
build is therefore 64 bytes smaller per pair: its header's size, `COMPACTED_SIZE` and `CURRENT_SIZE`, and what a
`COMPACT` copy keeps. The prebuild room is unchanged (a leaf per triangle, since the pairs are not known before the
build); the scratch grows by 80 bytes a triangle for the leaves and their bookkeeping, and only for a bottom-level
build without `ALLOW_UPDATE` (`XdxrLayout::pair`, which `GetRaytracingAccelerationStructurePrebuildInfo` reads from
the same flag). On the 288-triangle grid of `vk_xdxr_build_test`, 80 pairs make it 34,048 bytes against 39,168
unpaired; the 7,200-triangle grid's 2,208 pairs, 846,848 against 988,160 (14%). The pairing step only records each
pair and its rotation; the step that copies the leaves into the structure writes a pair's leaf from the two staged
ones, so no invocation stores to a word it loaded earlier in the same dispatch (the SPIRV-Cross reordering in MoltenVK
1.4.2 noted in `xdxr_build.comp`; rewriting the first triangle's staged leaf in place lost pairs on an Apple M5 Pro).
Because pairs never cross nodes, one thread owns each pair, and both pointers of a
pair have the same parent, so the parent table has one writer per entry. The node pass records each triangle's pointer
(TRI0, or TRI1 for the sibling) in scratch, so the bottom-up box pass finds the sibling's slot through the first
triangle's leaf. On a sorted grid of 7,200 triangles, 2,208 of the 3,600 possible pairs form (2,625 with the tree of four
consecutive elements: a radix-tree node often has an inner node among its children, and only triangle children
pair). A refit rewrites every leaf as one triangle, so a structure that may be updated is
not paired. **ASSUMPTION:** that pairing only siblings is close enough. The console builder's pairing rule is not
traced; `tools/xdxr_bvh.py` `build_blas(pair=True)` pairs greedily over the whole mesh instead, and both traverse
the same way. `check-build` compares barycentrics as well as hits, and checks that a paired case has TRI1 nodes and
that cases that must not pair (other geometry, same direction) have none.
the same way. `check-build` compares barycentrics as well as hits, and checks that a paired case has TRI1 nodes,
that cases that must not pair (other geometry, same direction) have none, and that no 64-byte unit of a BLAS's
element region is left without a triangle pointer (`unreferenced_leaves`: the unused sibling leaves the builder
kept before it laid the leaves out densely).

The node count depends on the keys. A kept node has two children only when both of its binary children are
elements, so with N elements there are at most (3N - 2) / 4 nodes. `XdxrLayout` computes the result and scratch
Expand Down Expand Up @@ -1155,12 +1171,36 @@ names its BLASes by address, which a clone keeps. The size comes from the source
a copy can follow a build in the same command list, and it is bounded by the destination resource. A build's size
is the nodes it wrote, so `COMPACTED_SIZE` and `CURRENT_SIZE` (from
`EmitRaytracingAccelerationStructurePostbuildInfo` or the postbuild descs of a build) are both
`64 * (totalSizeIn64BNodes + 1)`, and compaction gives back the room the layout left for more nodes. Serialization, deserialization and the tools-visualization
mode are refused at replay as unsupported, not faked. In `LEONIDA_RT_AS=khr` mode the postbuild descs stay ignored,
as before.

Not done yet: serialization; a compaction that reclaims the leaves that pairing leaves unused. A surface-area
collapse of the radix tree, or treelet restructuring, would trim a few more steps.
`64 * (totalSizeIn64BNodes + 1)`, and compaction gives back the room the layout left for more nodes and, in a
paired build, the 64 bytes a pair its leaves no longer take. The
tools-visualization copy mode and postbuild type are refused at replay as unsupported, not faked. In
`LEONIDA_RT_AS=khr` mode the postbuild descs stay ignored and serialization is refused, as before.

**Serialization.** `CopyRaytracingAccelerationStructure` with `SERIALIZE` writes D3D12's serialized form
(`xdxr_build.comp` `MODE_SERIALIZE`): the 56-byte `D3D12_SERIALIZED_RAYTRACING_ACCELERATION_STRUCTURE_HEADER`, then
`NumBottomLevelAccelerationStructurePointersAfterHeader` 8-byte BLAS addresses, then the structure's bytes as they
are. The header's driver matching identifier is the layer's own (`xdxr_build.h` `xdxr_serialized_id`: the GUID is the
ASCII `Leonida XDXR AS `, the versioning data the header version 6 and the serialized layout version 1), and
`CheckDriverMatchingIdentifier` answers `COMPATIBLE_WITH_DEVICE` for it, `INCOMPATIBLE_VERSION` for the same GUID
with other versioning data, and `UNRECOGNIZED` otherwise (always, with `LEONIDA_RT_AS=khr`). A TLAS carries one
pointer per INSTANCE record, in record order, read from the record's BLAS address (+60, 256-byte units; 0 for an
instance without a BLAS); a BLAS carries none. The deserialized size is the structure's own. `DESERIALIZE` copies the
bytes back and writes each INSTANCE record's BLAS address from the pointer list, so an application that moved its
BLASes (deserialized them elsewhere) and rewrote the pointers gets a TLAS that names them; the rest of the record
stays, since a deserialized BLAS is the same structure and its records count from its own start. The `SERIALIZATION`
postbuild desc (16 bytes) gives the serialized size, `56 + 8 * pointers + 64 * (totalSizeIn64BNodes + 1)`, and the
pointer count. Both copies are bounded by the destination resource only, since a serialized structure is larger than
the structure. The payload after the pointers is this layer's: the console's own serialized form (`XboxBvhBuilder.dll`
writes `SERIALIZATION` postbuild info in a handler of its own, [xdxr.md](xdxr.md) section 4) is not measured, and a
blob with the console's identifier would be `UNRECOGNIZED`. `d3d12_xdxr_rt_test` serializes a BLAS and a three-instance
TLAS, deserializes the BLAS to another address, rewrites the TLAS's pointers to it and deserializes the TLAS; the
copies must equal the originals byte for byte but for the three BLAS addresses, and `d3d12_xdxr_rt_check` traces
1,000 rays through each against brute force (RTX 5070 Ti, NVIDIA 617.42, and the Ryzen 9 7900X's Radeon 610M, AMD
2026.04.17, Windows 11: 0 rays differ).

Not done yet: a surface-area collapse of the radix tree, or treelet restructuring, would trim a few more steps; the
room the layout keeps for the most nodes a tree can have, and for a leaf per triangle, is only given back by a
`COMPACT` copy.

```powershell
python -I tools/xdxr_bvh.py selftest --seed 1 --rays 400 # build BLASes, compare closest hits with brute force
Expand Down
6 changes: 4 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -4910,8 +4910,10 @@ Vulkan AS would be invisible to them. Two paths:
DEFAULT and READBACK lists are refused as `unsupported` before submission, since their mapped bytes can still hold
an earlier frame's pointers when the CPU prepares the build. Completed staging buffers
are reused, so a per-frame rebuild does not allocate memory each frame. Updates refit the source structure instead of rebuilding it, `CopyRaytracingAccelerationStructure` clones and compacts, and the
postbuild compacted and current sizes are written ([bvh.md](bvh.md) §7a). Serialization and the Xbox
batched/streaming builds are not implemented. The
postbuild compacted and current sizes are written ([bvh.md](bvh.md) §7a), and so are serialization,
deserialization (with the BLAS pointers the application rewrites) and the `SERIALIZATION` postbuild info, under
the layer's own driver matching identifier ([bvh.md](bvh.md), *Serialization*). The Xbox batched/streaming builds
are not implemented. The
reported `RaytracingTier` is unchanged (`NOT_SUPPORTED`). Checked by `runtime/test/d3d12_xdxr_rt_test.cpp` through
the D3D12 API.

Expand Down
2 changes: 1 addition & 1 deletion docs/xdxr.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Default for type 0's `0x08` value when it is negative (or when there is no type-
| `InfoType` | In `XboxBvhBuilder.dll` |
|---|---|
| 0 `COMPACTED_SIZE`, 3 `CURRENT_SIZE` | written by one handler |
| 2 `SERIALIZATION` | written by another handler |
| 2 `SERIALIZATION` | written by another handler (the layer writes its own serialized form; [bvh.md](bvh.md), *Serialization*) |
| 1 `TOOLS_VISUALIZATION` and other values | ignored, no error |
| `0x10` `D3D12XBOX_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_OFFLINE_STATISTICS` | thrown as `E_INVALIDARG` by `0xe6a0` before any work, for every build: the DLL only supports it for offline BVH builds |

Expand Down
45 changes: 30 additions & 15 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4261,7 +4261,7 @@ struct DeviceState {
leonida::XdxrLayout layout;
// A count the format cannot hold: left as it was, and refused when the list is prepared (xdxr_check).
if (!layout.compute(leonida::VulkanContext::xdxr_elements(b), b.is_tlas,
leonida::VulkanContext::xdxr_box16(b)))
leonida::VulkanContext::xdxr_box16(b), leonida::VulkanContext::xdxr_pair(b)))
return true;
const std::uint64_t title_dst = b.dst_va;
std::uint64_t out = 0;
Expand Down Expand Up @@ -4327,7 +4327,8 @@ struct DeviceState {
// copy by it). The source's size comes from its build when the layer saw it, else from its header only.
bool resolve_xdxr_copy(leonida::CopyAccelerationStructure& c) {
if (!context || !context->xdxr_acceleration_structures()) return true;
const std::uint64_t known = bvh_known_size(c.src_va);
const bool deserialize = c.mode == leonida::XdxrBuilder::copy_deserialize;
const std::uint64_t known = deserialize ? 0 : bvh_known_size(c.src_va);
const std::uint64_t want = known ? known : leonida::XdxrLayout::header_size;
const std::uint64_t title_dst = c.dst_va;
std::uint64_t out = 0, room = 0;
Expand All @@ -4339,15 +4340,15 @@ struct DeviceState {
return bvh_refuse(bvh_copy_dst, c.dst_va, want), false;
c.dst_va = out;
c.dst_room = room;
if (known) bvh_note_size(title_dst, known);
if (known && c.mode != leonida::XdxrBuilder::copy_serialize) bvh_note_size(title_dst, known);
return true;
}

// A postbuild: its destination (8 bytes a source) and each source structure's header, which it reads.
// A postbuild: its destination and each source structure's header, which it reads.
bool resolve_xdxr_postbuild(leonida::AccelerationStructurePostbuild& info) {
if (!context || !context->xdxr_acceleration_structures()) return true;
std::uint64_t out = 0, room = 0;
const std::uint64_t bytes = 8ull * info.sources.size();
const std::uint64_t bytes = std::uint64_t(leonida::XdxrBuilder::postbuild_size(info.type)) * info.sources.size();
if (!bvh_va(info.dest_va, bytes, true, out, &room) || (out & 7))
return bvh_refuse(bvh_postbuild_dst, info.dest_va, bytes), false;
info.dest_va = out;
Expand Down Expand Up @@ -18117,12 +18118,22 @@ class Device final
return E_INVALIDARG;
}

// STUB
// Serialized acceleration structures this device writes start with leonida::xdxr_serialized_id (xdxr_build.h):
// the same GUID and versioning data are compatible, the same GUID with other versioning data is another layout.
// With LEONIDA_RT_AS=khr nothing is serialized (the KHR path refuses SERIALIZE), so nothing is recognised.
D3D12_DRIVER_MATCHING_IDENTIFIER_STATUS STDMETHODCALLTYPE CheckDriverMatchingIdentifier(
D3D12_SERIALIZED_DATA_TYPE type, const D3D12_SERIALIZED_DATA_DRIVER_MATCHING_IDENTIFIER*) override {
return type == D3D12_SERIALIZED_DATA_RAYTRACING_ACCELERATION_STRUCTURE
? D3D12_DRIVER_MATCHING_IDENTIFIER_UNRECOGNIZED
: D3D12_DRIVER_MATCHING_IDENTIFIER_UNSUPPORTED_TYPE;
D3D12_SERIALIZED_DATA_TYPE type, const D3D12_SERIALIZED_DATA_DRIVER_MATCHING_IDENTIFIER* id) override {
if (type != D3D12_SERIALIZED_DATA_RAYTRACING_ACCELERATION_STRUCTURE)
return D3D12_DRIVER_MATCHING_IDENTIFIER_UNSUPPORTED_TYPE;
if (!id || !state_.info.xdxr_acceleration_structures || !state_.info.buffer_device_address)
return D3D12_DRIVER_MATCHING_IDENTIFIER_UNRECOGNIZED;
static_assert(sizeof(D3D12_SERIALIZED_DATA_DRIVER_MATCHING_IDENTIFIER) == sizeof(leonida::xdxr_serialized_id));
const auto* ours = reinterpret_cast<const unsigned char*>(leonida::xdxr_serialized_id);
if (std::memcmp(&id->DriverOpaqueGUID, ours, sizeof(GUID)) != 0)
return D3D12_DRIVER_MATCHING_IDENTIFIER_UNRECOGNIZED;
return std::memcmp(id->DriverOpaqueVersioningData, ours + sizeof(GUID), sizeof(id->DriverOpaqueVersioningData)) == 0
? D3D12_DRIVER_MATCHING_IDENTIFIER_COMPATIBLE_WITH_DEVICE
: D3D12_DRIVER_MATCHING_IDENTIFIER_INCOMPATIBLE_VERSION;
}

// STUB
Expand Down Expand Up @@ -18331,14 +18342,18 @@ class Device final
}
auto& vk = state_.context->vk;
if (state_.context->xdxr_acceleration_structures()) {
// XDXR records: the layout depends only on the triangle or instance count and on whether the nodes are
// BOX16 (MINIMIZE_MEMORY, bottom level; xdxr_build.h). Its size has room for the most nodes a tree of
// that many elements can have; a build's own size, in its header, is what COMPACTED_SIZE reports. An
// update refits (no sort); the build's scratch size covers it.
// XDXR records: the layout depends only on the triangle or instance count, on whether the nodes are
// BOX16 (MINIMIZE_MEMORY, bottom level; xdxr_build.h) and, for its scratch, on whether the triangles may
// pair (a bottom level without ALLOW_UPDATE). Its size has room for the most nodes a tree of that many
// elements can have and a leaf per triangle; a build's own size, in its header, is what COMPACTED_SIZE
// reports. An update refits (no sort); the build's scratch size covers it.
leonida::BuildAccelerationStructure build{};
build.is_tlas = is_tlas;
if (desc->Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_MINIMIZE_MEMORY)
build.build_flags |= VK_BUILD_ACCELERATION_STRUCTURE_LOW_MEMORY_BIT_KHR;
// a bottom-level build without ALLOW_UPDATE pairs its triangles, which takes more scratch
if (desc->Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE)
build.build_flags |= VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR;
if (build.is_tlas) {
build.instance_count = desc->NumDescs;
} else {
Expand All @@ -18357,7 +18372,7 @@ class Device final
}
leonida::XdxrLayout layout;
if (!layout.compute(leonida::VulkanContext::xdxr_elements(build), build.is_tlas,
leonida::VulkanContext::xdxr_box16(build)))
leonida::VulkanContext::xdxr_box16(build), leonida::VulkanContext::xdxr_pair(build)))
return;
out->ResultDataMaxSizeInBytes = layout.size;
out->ScratchDataSizeInBytes = layout.scratch;
Expand Down
Loading
Loading