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:
authorromainveurrier <romain.veurrier@gmail.com>2026-11-22 14:04:34 +0100
committerromainveurrier <romain.veurrier@gmail.com>2026-11-22 14:04:34 +0100
commit1c58d6cfbc63ef3eb41d332c8aa443bb65ac4b4d (patch)
treead1aafa19ed74e0b550af8081fd7024b32596c83 /runtime/src
parent51a028115446da42565fa75d4657e82c214afc2d (diff)
D3D12: BuildRaytracingAccelerationStructure writes XDXR BVH4 records
The game's translated RT shaders read the console's XDXR records, which a VkAccelerationStructureKHR is not (docs/d3d12_vulkan.md 4.8). The standalone Vulkan device now builds them with src/xdxr_build.h, unless LEONIDA_RT_AS=khr keeps #743's KHR builds (the proposal in the maintainer decisions of #1548). - VulkanDeviceInfo::xdxr_acceleration_structures; the standalone device sets it with bufferDeviceAddress and then leaves the KHR extensions off, so acceleration-structure buffers keep their console VAs. - VulkanContext owns an XdxrBuilder; replay checks the inputs while the list is prepared (R32G32B32/R32G32 vertices, none/R16/R32 indices, addresses, counts) and records the build with heap_base added to every VA and as the bias for instance BLAS addresses. - GetRaytracingAccelerationStructurePrebuildInfo answers from XdxrLayout, exactly; an update rebuilds, so it needs the same scratch. - The geometry's vertex format is mapped per DXGI format (an unknown one is refused instead of read as R32G32B32), and its D3D12 flags reach the build. - test/d3d12_xdxr_rt_test.cpp: a two-geometry BLAS (a soup; an R16-indexed grid with stride 16 and a transform) and a TLAS of three instances through the D3D12 API; ctest d3d12_xdxr_rt + d3d12_xdxr_rt_check. - tools/xdxr_bvh.py: check-build compares TLAS hits by (geometry, primitive), and skips (77) when the build step wrote no cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/command_list.h1
-rw-r--r--runtime/src/vulkan_backend.h68
-rw-r--r--runtime/src/vulkan_replay.h24
3 files changed, 84 insertions, 9 deletions
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
index 5891e7533..3830608a2 100644
--- a/runtime/src/command_list.h
+++ b/runtime/src/command_list.h
@@ -578,6 +578,7 @@ struct RtTriangleGeometry {
578 std::uint32_t index_type = 0; // 0=NONE, 1=UINT16, 2=UINT32 578 std::uint32_t index_type = 0; // 0=NONE, 1=UINT16, 2=UINT32
579 std::uint64_t transform_va = 0; 579 std::uint64_t transform_va = 0;
580 bool opaque = true; 580 bool opaque = true;
581 std::uint32_t flags = 0; // D3D12_RAYTRACING_GEOMETRY_FLAGS (bit 0 opaque, bit 1 no duplicate any-hit)
581}; 582};
582 583
583// BLAS or TLAS build command (VK_KHR_acceleration_structure). 584// BLAS or TLAS build command (VK_KHR_acceleration_structure).
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 7006911c2..90adb7577 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -14,6 +14,7 @@
14#include "vulkan_depth24.h" 14#include "vulkan_depth24.h"
15#include "vulkan_scratch.h" 15#include "vulkan_scratch.h"
16#include "vulkan_replay.h" 16#include "vulkan_replay.h"
17#include "xdxr_build.h"
17 18
18#include <algorithm> 19#include <algorithm>
19#include <atomic> 20#include <atomic>
@@ -646,8 +647,9 @@ class VulkanContext {
646public: 647public:
647 // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). 648 // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication).
648 // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. 649 // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`.
650 // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress.
649 VulkanContext(PFN_vkGetDeviceProcAddr get, VkDevice device, bool separate_depth_stencil_layouts, 651 VulkanContext(PFN_vkGetDeviceProcAddr get, VkDevice device, bool separate_depth_stencil_layouts,
650 bool conditional_rendering = false, bool ray_tracing = false) 652 bool conditional_rendering = false, bool ray_tracing = false, bool xdxr = false)
651 : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) { 653 : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) {
652 loaded_ = vk.load(get, device); 654 loaded_ = vk.load(get, device);
653 replay_.pipeline_barrier = vk.vkCmdPipelineBarrier; 655 replay_.pipeline_barrier = vk.vkCmdPipelineBarrier;
@@ -711,7 +713,34 @@ public:
711 } 713 }
712 return false; 714 return false;
713 }; 715 };
714 if (ray_tracing_ && vk.vkCmdBuildAccelerationStructuresKHR) { 716 if (xdxr && loaded_) {
717 xdxr_ = std::make_unique<XdxrBuilder>(get, device);
718 if (xdxr_->loaded()) {
719 replay_.xdxr_check = [](const BuildAccelerationStructure& b) {
720 XdxrLayout layout;
721 return xdxr_inputs(b, nullptr, 0) && layout.compute(xdxr_elements(b), b.is_tlas) && b.dst_va &&
722 b.scratch_va && (!b.is_tlas || !b.instance_count || b.instances_va);
723 };
724 replay_.xdxr_record = [this](VkCommandBuffer cb, const BuildAccelerationStructure& b) {
725 // Every address is a console VA in the GPU address arena: the device address is heap_base + VA,
726 // and the BLAS addresses in instance descs get the same bias.
727 const std::uint64_t base = replay_.heap_base;
728 XdxrLayout layout;
729 layout.compute(xdxr_elements(b), b.is_tlas);
730 if (b.is_tlas) {
731 xdxr_->record_tlas(cb, layout, b.instances_va ? base + b.instances_va : 0, base + b.dst_va,
732 base + b.scratch_va, base);
733 } else {
734 std::vector<XdxrTriangles> geometries;
735 xdxr_inputs(b, &geometries, base);
736 xdxr_->record_blas(cb, layout, geometries, base + b.dst_va, base + b.scratch_va);
737 }
738 };
739 } else {
740 xdxr_.reset();
741 }
742 }
743 if (!xdxr_ && ray_tracing_ && vk.vkCmdBuildAccelerationStructuresKHR) {
715 replay_.build_acceleration_structures = vk.vkCmdBuildAccelerationStructuresKHR; 744 replay_.build_acceleration_structures = vk.vkCmdBuildAccelerationStructuresKHR;
716 replay_.resolve_acceleration_structure = [this](std::uint64_t va) { 745 replay_.resolve_acceleration_structure = [this](std::uint64_t va) {
717 const auto it = acceleration_structures_.find(va); 746 const auto it = acceleration_structures_.find(va);
@@ -883,6 +912,40 @@ public:
883 } 912 }
884 913
885 bool ray_tracing_supported() const { return ray_tracing_ && vk.vkCmdBuildAccelerationStructuresKHR != nullptr; } 914 bool ray_tracing_supported() const { return ray_tracing_ && vk.vkCmdBuildAccelerationStructuresKHR != nullptr; }
915 bool xdxr_acceleration_structures() const { return xdxr_ != nullptr; }
916
917 // Triangles (BLAS) or instances (TLAS) a build has: what XdxrLayout is computed from, for the build and for
918 // GetRaytracingAccelerationStructurePrebuildInfo.
919 static std::uint32_t xdxr_elements(const BuildAccelerationStructure& b) {
920 if (b.is_tlas) return b.instance_count;
921 std::uint64_t n = 0;
922 for (const auto& t : b.triangles) n += (t.index_type ? t.index_count : t.vertex_count) / 3;
923 return n > 0xffffffffu ? 0xffffffffu : std::uint32_t(n);
924 }
925 // The geometries as the builder reads them, with `base` added to their VAs; false for an input it cannot read
926 // (a vertex format other than R32G32B32_FLOAT or R32G32_FLOAT, an index type other than none/R16/R32).
927 static bool xdxr_inputs(const BuildAccelerationStructure& b, std::vector<XdxrTriangles>* out, std::uint64_t base) {
928 if (b.is_tlas) return true;
929 for (const auto& t : b.triangles) {
930 if (t.vertex_format != VK_FORMAT_R32G32B32_SFLOAT && t.vertex_format != VK_FORMAT_R32G32_SFLOAT) return false;
931 if (t.index_type > 2) return false;
932 const std::uint32_t count = (t.index_type ? t.index_count : t.vertex_count) / 3;
933 if (count && !t.vertex_va) return false;
934 if (count && t.index_type && !t.index_va) return false;
935 if (!out) continue;
936 XdxrTriangles g;
937 g.vertices = t.vertex_va ? base + t.vertex_va : 0;
938 g.vertex_stride = t.vertex_stride;
939 g.xy_only = t.vertex_format == VK_FORMAT_R32G32_SFLOAT;
940 g.indices = t.index_type && t.index_va ? base + t.index_va : 0;
941 g.index_type = t.index_type;
942 g.triangle_count = count;
943 g.transform = t.transform_va ? base + t.transform_va : 0;
944 g.flags = t.flags | (t.opaque ? 1u : 0u);
945 out->push_back(g);
946 }
947 return true;
948 }
886 949
887 void register_acceleration_structure(std::uint64_t va, VkAccelerationStructureKHR as) { 950 void register_acceleration_structure(std::uint64_t va, VkAccelerationStructureKHR as) {
888 if (!va || !as) return; 951 if (!va || !as) return;
@@ -1031,6 +1094,7 @@ private:
1031 VkDevice device_; 1094 VkDevice device_;
1032 bool loaded_ = false; 1095 bool loaded_ = false;
1033 bool ray_tracing_ = false; 1096 bool ray_tracing_ = false;
1097 std::unique_ptr<XdxrBuilder> xdxr_; // set: acceleration structures are XDXR BVH4 records
1034 std::atomic<bool> device_lost_{false}; 1098 std::atomic<bool> device_lost_{false};
1035 alignas(64) std::shared_mutex resource_mutex_; // shared for prepare lookups, unique for add/remove 1099 alignas(64) std::shared_mutex resource_mutex_; // shared for prepare lookups, unique for add/remove
1036 alignas(64) std::mutex tracker_mutex_; 1100 alignas(64) std::mutex tracker_mutex_;
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index ca27b1063..fb2e9334b 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -779,6 +779,10 @@ struct ReplayFunctions {
779 PFN_vkCmdDispatchIndirect dispatch_indirect = nullptr; 779 PFN_vkCmdDispatchIndirect dispatch_indirect = nullptr;
780 // Optional VK_KHR_acceleration_structure: BuildRaytracingAccelerationStructure. 780 // Optional VK_KHR_acceleration_structure: BuildRaytracingAccelerationStructure.
781 PFN_vkCmdBuildAccelerationStructuresKHR build_acceleration_structures = nullptr; 781 PFN_vkCmdBuildAccelerationStructuresKHR build_acceleration_structures = nullptr;
782 // XDXR BVH4 builds (xdxr_build.h), used instead of the KHR ones when set: `xdxr_check` refuses inputs the builder
783 // cannot read while the list is prepared, `xdxr_record` records the build.
784 std::function<bool(const BuildAccelerationStructure&)> xdxr_check;
785 std::function<void(VkCommandBuffer, const BuildAccelerationStructure&)> xdxr_record;
782 std::function<VkAccelerationStructureKHR(std::uint64_t)> resolve_acceleration_structure; 786 std::function<VkAccelerationStructureKHR(std::uint64_t)> resolve_acceleration_structure;
783 // Preparation only allocates a slot; capture records the GPU snapshot at the SetPredication command. 787 // Preparation only allocates a slot; capture records the GPU snapshot at the SetPredication command.
784 std::function<bool(VkBuffer, VkDeviceSize, PredicateCapture&)> prepare_predicate; 788 std::function<bool(VkBuffer, VkDeviceSize, PredicateCapture&)> prepare_predicate;
@@ -2203,13 +2207,17 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2203 return ReplayResult::unsupported; 2207 return ReplayResult::unsupported;
2204 } 2208 }
2205 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) { 2209 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) {
2206 if (!vk.build_acceleration_structures || !vk.resolve_acceleration_structure) 2210 if (vk.xdxr_check) {
2207 return ReplayResult::unsupported; 2211 if (!vk.xdxr_check(*build)) return ReplayResult::unsupported;
2208 p.accel_dst = vk.resolve_acceleration_structure(build->dst_va); 2212 } else {
2209 if (!p.accel_dst) return ReplayResult::unresolved_resource; 2213 if (!vk.build_acceleration_structures || !vk.resolve_acceleration_structure)
2210 if (build->update && build->src_va) { 2214 return ReplayResult::unsupported;
2211 p.accel_src = vk.resolve_acceleration_structure(build->src_va); 2215 p.accel_dst = vk.resolve_acceleration_structure(build->dst_va);
2212 if (!p.accel_src) return ReplayResult::unresolved_resource; 2216 if (!p.accel_dst) return ReplayResult::unresolved_resource;
2217 if (build->update && build->src_va) {
2218 p.accel_src = vk.resolve_acceleration_structure(build->src_va);
2219 if (!p.accel_src) return ReplayResult::unresolved_resource;
2220 }
2213 } 2221 }
2214 } else if (const auto* dispatch = std::get_if<Dispatch>(&command)) { 2222 } else if (const auto* dispatch = std::get_if<Dispatch>(&command)) {
2215 if (!vk.bind_pipeline || !vk.push_constants || !vk.dispatch || !vk.compute_pipeline) 2223 if (!vk.bind_pipeline || !vk.push_constants || !vk.dispatch || !vk.compute_pipeline)
@@ -2924,6 +2932,8 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2924 } 2932 }
2925 } 2933 }
2926 } 2934 }
2935 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command); build && vk.xdxr_record) {
2936 vk.xdxr_record(command_buffer, *build);
2927 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) { 2937 } else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) {
2928 // The build's addresses are console VAs into the GPU address arena (heap_base is its device address). 2938 // The build's addresses are console VAs into the GPU address arena (heap_base is its device address).
2929 // A null VA (no index buffer, no transform) stays null: heap_base + 0 would name the arena's zero page. 2939 // A null VA (no index buffer, no transform) stays null: heap_base + 0 would name the arena's zero page.