diff options
| author | romainveurrier <romain.veurrier@gmail.com> | 2026-11-22 14:04:34 +0100 |
|---|---|---|
| committer | romainveurrier <romain.veurrier@gmail.com> | 2026-11-22 14:04:34 +0100 |
| commit | 1c58d6cfbc63ef3eb41d332c8aa443bb65ac4b4d (patch) | |
| tree | ad1aafa19ed74e0b550af8081fd7024b32596c83 /runtime/src/vulkan_backend.h | |
| parent | 51a028115446da42565fa75d4657e82c214afc2d (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/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 68 |
1 files changed, 66 insertions, 2 deletions
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 { | |||
| 646 | public: | 647 | public: |
| 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_; |