From 2e3464a511b4031ced58c0db9966e93e70401893 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:41:55 -0600 Subject: [PATCH] vulkan_replay: a KHR TLAS build of instance pointers no longer ends prepare() From kevincommits' finding (item 1) in #1608. For a KHR TLAS build with D3D12_ELEMENTS_LAYOUT_ARRAY_OF_POINTERS, prepare() returned prepare_khr_instances' result directly. On success it left the command loop at that build: every later command of the list stayed default-initialized (a dispatch with no pipeline, a draw with no prepared draw), and the end-of-list query checks and the statistics and occlusion segment plans did not run. Only a failure returns there now, as on the XDXR path a few lines above. New CPU test replay_khr_tlas_pointers: the TLAS build followed by a dispatch and a BLAS build; both later commands are prepared (2 checks fail on main), and a failed staging still fails the list with its own result. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 7 + runtime/src/vulkan_replay.h | 5 +- .../test/replay_khr_tlas_pointers_test.cpp | 133 ++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 runtime/test/replay_khr_tlas_pointers_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3c0112060b..2dfd6728e5 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4643,6 +4643,13 @@ if(TARGET Vulkan::Headers) target_compile_options(replay_khr_as_copy_postbuild_test PRIVATE /W4 /permissive- /utf-8) endif() add_test(NAME replay_khr_as_copy_postbuild COMMAND replay_khr_as_copy_postbuild_test) + # A KHR TLAS build of instance pointers does not end the list's preparation (no GPU). + add_executable(replay_khr_tlas_pointers_test test/replay_khr_tlas_pointers_test.cpp) + target_link_libraries(replay_khr_tlas_pointers_test PRIVATE Vulkan::Headers) + if(WIN32) + target_compile_definitions(replay_khr_tlas_pointers_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + endif() + add_test(NAME replay_khr_tlas_pointers COMMAND replay_khr_tlas_pointers_test) # PreparedList::wrote_readback and readback_src_stages match the actual producer stage (depth24, rgb96, UAV clear; no GPU). add_executable(replay_readback_stages_test test/replay_readback_stages_test.cpp) target_link_libraries(replay_readback_stages_test PRIVATE Vulkan::Headers) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 4f3c476541..6f80ac1f9c 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -4197,7 +4197,10 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun } if (build->is_tlas && build->instance_pointers && build->instance_count) { if (!vk.prepare_khr_instances || !vk.host_at || !vk.buffer_at) return unsupported_at(__LINE__); - return vk.prepare_khr_instances(*build, p.khr_instances); + // Only a failure ends the list here, as on the XDXR path above: the commands after this one still + // have to be prepared, and the end-of-list checks and segment plans still have to run. + const ReplayResult staged = vk.prepare_khr_instances(*build, p.khr_instances); + if (staged != ReplayResult::success) return staged; } } } else if (const auto* copy = std::get_if(&command)) { diff --git a/runtime/test/replay_khr_tlas_pointers_test.cpp b/runtime/test/replay_khr_tlas_pointers_test.cpp new file mode 100644 index 0000000000..e373148378 --- /dev/null +++ b/runtime/test/replay_khr_tlas_pointers_test.cpp @@ -0,0 +1,133 @@ +// A KHR TLAS build whose instances are an array of pointers (D3D12_ELEMENTS_LAYOUT_ARRAY_OF_POINTERS) stages them with +// prepare_khr_instances. prepare() used to return that call's result straight away, so on success the commands after +// the build were never prepared (a later dispatch had no pipeline, a later build no structure) and the list's +// end-of-list checks were skipped. Only a failure ends the list there now, as on the XDXR path. No GPU: the +// acceleration structures, buffers and pipelines are fakes. +#include "../src/vulkan_replay.h" + +#include +#include +#include + +namespace { +using namespace leonida; + +int failures = 0; +void check(bool ok, const std::string& what) { + if (ok) return; + std::fprintf(stderr, "FAIL: %s\n", what.c_str()); + ++failures; +} + +template +T handle(std::uintptr_t n) { + return reinterpret_cast(n); +} + +VKAPI_ATTR void VKAPI_CALL fake_build(VkCommandBuffer, std::uint32_t, const VkAccelerationStructureBuildGeometryInfoKHR*, + const VkAccelerationStructureBuildRangeInfoKHR* const*) {} + +constexpr std::uint64_t tlas_va = 0x10000, blas_va = 0x20000, scratch_va = 0x30000, instances_va = 0x40000; +constexpr std::uintptr_t compute_id = 1; +const ComputePipeline compute = [] { + ComputePipeline p{}; + p.pipeline = handle(0x1000); + p.layout = handle(7); + return p; +}(); + +int staged = 0; +ReplayResult stage_result = ReplayResult::success; + +ReplayFunctions functions() { + ReplayFunctions vk{}; + vk.build_acceleration_structures = fake_build; + vk.resolve_acceleration_structure = [](std::uint64_t va) { + return va ? handle(std::uintptr_t(va)) : VkAccelerationStructureKHR(VK_NULL_HANDLE); + }; + vk.host_at = [](std::uint64_t, std::uint64_t, const std::uint8_t*&) { return HostReadStatus::absent; }; + vk.buffer_at = [](std::uint64_t, std::uint64_t&, VkBuffer& buffer, VkDeviceSize& offset) { + buffer = handle(9); + offset = 0; + return true; + }; + vk.prepare_khr_instances = [](const BuildAccelerationStructure&, VkDeviceAddress& out) { + ++staged; + out = 0x80000; // the staged instance array (kevincommits' repro value) + return stage_result; + }; + vk.bind_pipeline = +[](VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {}; + vk.push_constants = +[](VkCommandBuffer, VkPipelineLayout, VkShaderStageFlags, std::uint32_t, std::uint32_t, + const void*) {}; + vk.dispatch = +[](VkCommandBuffer, std::uint32_t, std::uint32_t, std::uint32_t) {}; + vk.compute_pipeline = [](std::uintptr_t id) -> const ComputePipeline* { return id == compute_id ? &compute : nullptr; }; + return vk; +} + +const ResourceBinding* resolve(ResourceId) { return nullptr; } + +BuildAccelerationStructure tlas_of_pointers() { + BuildAccelerationStructure b{}; + b.is_tlas = true; + b.dst_va = tlas_va; + b.scratch_va = scratch_va; + b.instances_va = instances_va; + b.instance_count = 2; + b.instance_pointers = true; + return b; +} + +BuildAccelerationStructure blas() { + BuildAccelerationStructure b{}; + b.dst_va = blas_va; + b.scratch_va = scratch_va; + return b; +} +} // namespace + +int main() { + const ReplayFunctions vk = functions(); + + // kevincommits' repro: the TLAS build, then a dispatch (and here a second build) after it. + { + CommandList list; + check(list.build_acceleration_structure(tlas_of_pointers()), "record the TLAS build"); + check(list.dispatch({compute_id, {}, 1, 1, 1}), "record the dispatch"); + check(list.build_acceleration_structure(blas()), "record the BLAS build"); + list.close(); + staged = 0; + stage_result = ReplayResult::success; + PreparedList prepared; + const ReplayResult r = prepare(list, resolve, vk, prepared); + check(r == ReplayResult::success, "prepare succeeds"); + check(staged == 1, "the pointer array is staged once"); + check(prepared.commands.size() == 3, "three prepared commands"); + if (prepared.commands.size() == 3) { + check(prepared.commands[0].khr_instances == 0x80000, "the TLAS build has its staged instances"); + check(prepared.commands[1].pipeline.pipeline == compute.pipeline, + "the dispatch after the TLAS build has its pipeline (was VK_NULL_HANDLE: dropped)"); + check(prepared.commands[2].accel_dst == handle(std::uintptr_t(blas_va)), + "the build after the TLAS build has its structure"); + } + } + + // A failed staging still ends the list with its result. + { + CommandList list; + list.build_acceleration_structure(tlas_of_pointers()); + list.dispatch({compute_id, {}, 1, 1, 1}); + list.close(); + staged = 0; + stage_result = ReplayResult::unresolved_resource; + PreparedList prepared; + check(prepare(list, resolve, vk, prepared) == ReplayResult::unresolved_resource, + "a failed staging fails the list with its own result"); + } + + if (failures) { + std::fprintf(stderr, "%d failure(s)\n", failures); + return 1; + } + std::printf("replay_khr_tlas_pointers: ok\n"); + return 0; +}