Skip to content
Closed
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
32 changes: 27 additions & 5 deletions docs/bvh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1155,11 +1155,33 @@ 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
`64 * (totalSizeIn64BNodes + 1)`, and compaction gives back the room the layout left for more nodes. 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 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.

```powershell
Expand Down
6 changes: 4 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -5077,8 +5077,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
29 changes: 20 additions & 9 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5460,7 +5460,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 @@ -5472,15 +5473,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 @@ -26588,12 +26589,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
10 changes: 6 additions & 4 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1305,13 +1305,15 @@ class VulkanContext {
xdxr_pair(b));
}
};
replay_.xdxr_copy = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dst, std::uint64_t bytes) {
replay_.xdxr_copy = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dst, std::uint64_t bytes,
std::uint32_t mode, std::uint64_t source_bytes) {
const std::uint64_t base = replay_.heap_base;
xdxr_->record_copy(cb, base + src, base + dst, bytes);
xdxr_->record_copy(cb, base + src, base + dst, bytes, mode, source_bytes);
};
replay_.xdxr_postbuild = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dest) {
replay_.xdxr_postbuild = [this](VkCommandBuffer cb, std::uint64_t src, std::uint64_t dest,
std::uint32_t type) {
const std::uint64_t base = replay_.heap_base;
xdxr_->record_postbuild(cb, base + src, base + dest);
xdxr_->record_postbuild(cb, base + src, base + dest, type);
};
} else {
xdxr_.reset();
Expand Down
39 changes: 28 additions & 11 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -1372,10 +1372,14 @@ struct ReplayFunctions {
std::function<bool(const BuildAccelerationStructure&)> xdxr_check;
std::function<void(VkCommandBuffer, const BuildAccelerationStructure&, const XdxrBlasTable&)> xdxr_record;
std::function<ReplayResult(VkDeviceAddress& address, std::uint32_t& count)> prepare_xdxr_table;
// CopyRaytracingAccelerationStructure (CLONE, COMPACT) of XDXR structures: `bytes` is the room the destination
// resource has; and one postbuild value (COMPACTED_SIZE, CURRENT_SIZE) of the structure at `src` to `dest`.
std::function<void(VkCommandBuffer, std::uint64_t src_va, std::uint64_t dst_va, std::uint64_t bytes)> xdxr_copy;
std::function<void(VkCommandBuffer, std::uint64_t src_va, std::uint64_t dest_va)> xdxr_postbuild;
// CopyRaytracingAccelerationStructure (CLONE, COMPACT, SERIALIZE, DESERIALIZE: the D3D12 copy mode) of XDXR
// structures: the byte counts bound the destination and source; and one postbuild desc (COMPACTED_SIZE,
// SERIALIZATION, CURRENT_SIZE: the D3D12 info type) of the structure at `src` to `dest`.
std::function<void(VkCommandBuffer, std::uint64_t src_va, std::uint64_t dst_va, std::uint64_t bytes,
std::uint32_t mode, std::uint64_t source_bytes)>
xdxr_copy;
std::function<void(VkCommandBuffer, std::uint64_t src_va, std::uint64_t dest_va, std::uint32_t type)>
xdxr_postbuild;
std::function<VkAccelerationStructureKHR(std::uint64_t)> resolve_acceleration_structure;
// CopyRaytracingAccelerationStructure (CLONE, COMPACT) and COMPACTED_SIZE postbuild on KHR acceleration structures.
PFN_vkCmdCopyAccelerationStructureKHR copy_acceleration_structure = nullptr;
Expand Down Expand Up @@ -1522,6 +1526,7 @@ struct Prepared {
VkAccelerationStructureKHR accel_dst = VK_NULL_HANDLE;
VkDeviceAddress khr_instances = 0; // staged ARRAY_OF_POINTERS instance list for a KHR TLAS; 0: use the VA directly
std::uint64_t xdxr_bytes = 0; // CopyAccelerationStructure: the most bytes the copy may write
std::uint64_t xdxr_source_bytes = 0;
XdxrBlasTable xdxr_blases; // an XDXR TLAS build: how its instances' BLAS addresses are translated
VkAccelerationStructureKHR accel_src = VK_NULL_HANDLE;
std::uint32_t khr_postbuild = ~0u; // index into PreparedList::khr_postbuilds; ~0u: skipped or XDXR
Expand Down Expand Up @@ -1933,6 +1938,10 @@ inline void clip(std::span<const Rect> rects, VkExtent2D extent, std::uint32_t l
}
}

// Bytes one XDXR postbuild desc takes at its destination: D3D12's SERIALIZATION desc (type 2) is two UINT64s, the
// COMPACTED_SIZE and CURRENT_SIZE descs one (xdxr_build.h XdxrBuilder::postbuild_size).
inline constexpr std::uint64_t xdxr_postbuild_stride(std::uint32_t type) { return type == 2 ? 16 : 8; }

inline constexpr VkPipelineStageFlags2 queue_stages_copy =
VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT | VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT |
VK_PIPELINE_STAGE_2_HOST_BIT | VK_PIPELINE_STAGE_2_TRANSFER_BIT | VK_PIPELINE_STAGE_2_COPY_BIT;
Expand Down Expand Up @@ -4923,15 +4932,17 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
if (vk.xdxr_copy) {
// XDXR structures: the destination's room bounds the copy, as the size is only known to the GPU. A
// side the layer resolved into a copy of the title's memory brings its room (CopyAccelerationStructure).
if (!vk.buffer_at || copy->mode > 1) return unsupported_at(__LINE__);
if (!vk.buffer_at || copy->mode > 4 || copy->mode == 2) return unsupported_at(__LINE__);
std::uint64_t room = copy->dst_room ? copy->dst_room : ~std::uint64_t(0);
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceSize offset = 0;
if (!copy->dst_room && !vk.buffer_at(copy->dst_va, room, buffer, offset)) return unresolved_at(__LINE__);
std::uint64_t source = copy->src_room ? copy->src_room : ~std::uint64_t(0);
if (!copy->src_room && !vk.buffer_at(copy->src_va, source, buffer, offset))
return unresolved_at(__LINE__);
p.xdxr_bytes = std::min(room, source);
// Serialization adds a header and pointer list. Deserialization removes them.
p.xdxr_bytes = copy->mode >= 3 ? room : std::min(room, source);
p.xdxr_source_bytes = source;
} else {
if (vk.queue_stages == detail::queue_stages_copy || !vk.copy_acceleration_structure ||
!vk.resolve_acceleration_structure || copy->mode > 1)
Expand All @@ -4944,13 +4955,16 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
}
} else if (const auto* info = std::get_if<AccelerationStructurePostbuild>(&command)) {
if (vk.xdxr_postbuild) {
if (!vk.buffer_at || (info->type != 0 && info->type != 3)) return unsupported_at(__LINE__);
std::uint64_t room = info->dest_room ? info->dest_room : 8 * info->sources.size();
// TOOLS_VISUALIZATION (1) has no XDXR form; SERIALIZATION descs are 16 bytes, the sizes 8.
if (!vk.buffer_at || (info->type != 0 && info->type != 2 && info->type != 3))
return unsupported_at(__LINE__);
const std::uint64_t need = detail::xdxr_postbuild_stride(info->type) * info->sources.size();
std::uint64_t room = info->dest_room ? info->dest_room : need;
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceSize offset = 0;
if (!info->dest_room && !vk.buffer_at(info->dest_va, room, buffer, offset))
return unresolved_at(__LINE__);
if (room < 8 * info->sources.size()) return ReplayResult::invalid_copy;
if (room < need) return ReplayResult::invalid_copy;
if (!info->sources.empty()) {
// dest_va is resolved by VA and may target a host-readable buffer; XDXR postbuild writes from compute.
out.wrote_readback = true;
Expand Down Expand Up @@ -6970,7 +6984,8 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_
} else if (const auto* copy = std::get_if<CopyAccelerationStructure>(&command)) {
if constexpr (Checkpoints) checkpoints->record(command_buffer, i, CheckpointKind::copy);
if (vk.xdxr_copy) {
vk.xdxr_copy(command_buffer, copy->src_va, copy->dst_va, p.xdxr_bytes);
vk.xdxr_copy(command_buffer, copy->src_va, copy->dst_va, p.xdxr_bytes, copy->mode,
p.xdxr_source_bytes);
cache.invalidate_compute();
} else {
VkCopyAccelerationStructureInfoKHR copy_info{VK_STRUCTURE_TYPE_COPY_ACCELERATION_STRUCTURE_INFO_KHR};
Expand All @@ -6983,8 +6998,10 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_
} else if (const auto* postbuild = std::get_if<AccelerationStructurePostbuild>(&command);
postbuild && !p.skip) {
if (vk.xdxr_postbuild) {
const std::uint64_t stride = detail::xdxr_postbuild_stride(postbuild->type);
for (std::size_t s = 0; s < postbuild->sources.size(); ++s)
vk.xdxr_postbuild(command_buffer, postbuild->sources[s], postbuild->dest_va + 8 * s);
vk.xdxr_postbuild(command_buffer, postbuild->sources[s], postbuild->dest_va + stride * s,
postbuild->type);
if (!postbuild->sources.empty()) cache.invalidate_compute();
} else if (p.khr_postbuild < prepared.khr_postbuilds.size()) {
const auto& pb = prepared.khr_postbuilds[p.khr_postbuild];
Expand Down
Loading
Loading