diff options
| author | QemmHD <stephenlethco2@gmail.com> | 2026-11-24 11:25:49 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-24 11:25:49 -0500 |
| commit | 3ed8e50b754eb9dcf9e3819eddbbbccd52fc1177 (patch) | |
| tree | d764c4cec00ef9d927a544b73e3e41419bd2080e /runtime/test | |
| parent | b9c7fc862010a19f6af9b44ab0578a4d5719d053 (diff) | |
| parent | 5a67c51a49c512c8b87a28dd7dae988d4ad11da9 (diff) | |
Merge pull request #488 from xroneyaa/fix/vulkan-host-at-aliasing
Merging.
**Why merged:** host_at passes the byte length to BufferVaIndex::find, so aliasing placed buffers resolve over the full range rather than at the start address only. The CMake change only adds the new test target.
**Why it's good:** vk_host_at_aliasing, buffer_va_index and vk_buffer_registration_oom all pass both plain and under validation.
Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Alone on main, its tests passed both plain and with LEONIDA_VK_VALIDATION=1. Cumulative: #388, #418, #421, #434, #470, #484, #485, #486 and #488 merged onto main 1504c1af6 (a225e593e). There the repo-consistency set gives 566 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 306/308. The 2 failures are the same pre-existing ones as main (d3d12_execute_indirect_validation, d3d12_depth_resolve), with the same skip set. git merge-tree against current main 98a977b4b: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes.
AI-assisted (Claude Code, Claude Opus 5.5).
Diffstat (limited to 'runtime/test')
| -rw-r--r-- | runtime/test/vk_host_at_aliasing_test.cpp | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/runtime/test/vk_host_at_aliasing_test.cpp b/runtime/test/vk_host_at_aliasing_test.cpp new file mode 100644 index 000000000..06ce1946d --- /dev/null +++ b/runtime/test/vk_host_at_aliasing_test.cpp | |||
| @@ -0,0 +1,257 @@ | |||
| 1 | // VulkanContext::replay_.host_at resolution across aliasing placed buffers (via KHR TLAS array-of-pointers staging). | ||
| 2 | // Public runtime classes with fake Vulkan procedures: no loader, GPU, or game data. | ||
| 3 | #include "../src/vulkan_backend.h" | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstdio> | ||
| 8 | #include <cstring> | ||
| 9 | #include <type_traits> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | int failures = 0; | ||
| 15 | int live_buffers = 0; | ||
| 16 | int live_memories = 0; | ||
| 17 | int mapped_count = 0; | ||
| 18 | int as_builds = 0; | ||
| 19 | bool build_saw_array_of_pointers = false; | ||
| 20 | VkDeviceAddress build_instances_address = 0; | ||
| 21 | std::array<std::uint8_t, 256> stage_bytes{}; | ||
| 22 | std::vector<std::uint64_t> staged_addresses; | ||
| 23 | |||
| 24 | void expect(bool ok, const char* what) { | ||
| 25 | if (!ok) { | ||
| 26 | std::fprintf(stderr, "FAIL: %s\n", what); | ||
| 27 | ++failures; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | template<typename Handle> | ||
| 32 | Handle handle(std::uintptr_t value) { | ||
| 33 | if constexpr (std::is_pointer_v<Handle>) | ||
| 34 | return reinterpret_cast<Handle>(value); | ||
| 35 | else | ||
| 36 | return static_cast<Handle>(value); | ||
| 37 | } | ||
| 38 | |||
| 39 | constexpr VkDeviceAddress kStageDeviceAddress = 0xfeed0000ull; | ||
| 40 | |||
| 41 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_pool(VkDevice, const VkCommandPoolCreateInfo*, | ||
| 42 | const VkAllocationCallbacks*, VkCommandPool* out) { | ||
| 43 | *out = handle<VkCommandPool>(1); | ||
| 44 | return VK_SUCCESS; | ||
| 45 | } | ||
| 46 | VKAPI_ATTR void VKAPI_CALL mock_destroy_pool(VkDevice, VkCommandPool, const VkAllocationCallbacks*) {} | ||
| 47 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_semaphore(VkDevice, const VkSemaphoreCreateInfo*, | ||
| 48 | const VkAllocationCallbacks*, VkSemaphore* out) { | ||
| 49 | *out = handle<VkSemaphore>(1); | ||
| 50 | return VK_SUCCESS; | ||
| 51 | } | ||
| 52 | VKAPI_ATTR void VKAPI_CALL mock_destroy_semaphore(VkDevice, VkSemaphore, const VkAllocationCallbacks*) {} | ||
| 53 | VKAPI_ATTR VkResult VKAPI_CALL mock_semaphore_counter(VkDevice, VkSemaphore, std::uint64_t* out) { | ||
| 54 | *out = UINT64_MAX; | ||
| 55 | return VK_SUCCESS; | ||
| 56 | } | ||
| 57 | VKAPI_ATTR VkResult VKAPI_CALL mock_queue_wait_idle(VkQueue) { return VK_SUCCESS; } | ||
| 58 | VKAPI_ATTR VkResult VKAPI_CALL mock_allocate_commands(VkDevice, const VkCommandBufferAllocateInfo*, | ||
| 59 | VkCommandBuffer* out) { | ||
| 60 | *out = handle<VkCommandBuffer>(1); | ||
| 61 | return VK_SUCCESS; | ||
| 62 | } | ||
| 63 | VKAPI_ATTR VkResult VKAPI_CALL mock_reset_command(VkCommandBuffer, VkCommandBufferResetFlags) { return VK_SUCCESS; } | ||
| 64 | VKAPI_ATTR VkResult VKAPI_CALL mock_begin_command(VkCommandBuffer, const VkCommandBufferBeginInfo*) { return VK_SUCCESS; } | ||
| 65 | VKAPI_ATTR VkResult VKAPI_CALL mock_end_command(VkCommandBuffer) { return VK_SUCCESS; } | ||
| 66 | VKAPI_ATTR VkResult VKAPI_CALL mock_queue_submit(VkQueue, std::uint32_t, const VkSubmitInfo*, VkFence) { | ||
| 67 | return VK_SUCCESS; | ||
| 68 | } | ||
| 69 | VKAPI_ATTR void VKAPI_CALL mock_cmd_barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, | ||
| 70 | VkDependencyFlags, std::uint32_t, const VkMemoryBarrier*, std::uint32_t, | ||
| 71 | const VkBufferMemoryBarrier*, std::uint32_t, | ||
| 72 | const VkImageMemoryBarrier*) {} | ||
| 73 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_buffer(VkDevice, const VkBufferCreateInfo*, const VkAllocationCallbacks*, | ||
| 74 | VkBuffer* out) { | ||
| 75 | ++live_buffers; | ||
| 76 | *out = handle<VkBuffer>(0x100 + live_buffers); | ||
| 77 | return VK_SUCCESS; | ||
| 78 | } | ||
| 79 | VKAPI_ATTR void VKAPI_CALL mock_destroy_buffer(VkDevice, VkBuffer, const VkAllocationCallbacks*) { --live_buffers; } | ||
| 80 | VKAPI_ATTR void VKAPI_CALL mock_buffer_memory_reqs(VkDevice, VkBuffer, VkMemoryRequirements* reqs) { | ||
| 81 | reqs->size = stage_bytes.size(); | ||
| 82 | reqs->alignment = 16; | ||
| 83 | reqs->memoryTypeBits = 1u; | ||
| 84 | } | ||
| 85 | VKAPI_ATTR VkResult VKAPI_CALL mock_allocate_memory(VkDevice, const VkMemoryAllocateInfo*, const VkAllocationCallbacks*, | ||
| 86 | VkDeviceMemory* out) { | ||
| 87 | ++live_memories; | ||
| 88 | *out = handle<VkDeviceMemory>(0x200 + live_memories); | ||
| 89 | return VK_SUCCESS; | ||
| 90 | } | ||
| 91 | VKAPI_ATTR void VKAPI_CALL mock_free_memory(VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) { --live_memories; } | ||
| 92 | VKAPI_ATTR VkResult VKAPI_CALL mock_bind_buffer_memory(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) { | ||
| 93 | return VK_SUCCESS; | ||
| 94 | } | ||
| 95 | VKAPI_ATTR VkResult VKAPI_CALL mock_map_memory(VkDevice, VkDeviceMemory, VkDeviceSize offset, VkDeviceSize, | ||
| 96 | VkMemoryMapFlags, void** data) { | ||
| 97 | ++mapped_count; | ||
| 98 | *data = stage_bytes.data() + offset; | ||
| 99 | return VK_SUCCESS; | ||
| 100 | } | ||
| 101 | VKAPI_ATTR void VKAPI_CALL mock_unmap_memory(VkDevice, VkDeviceMemory) { --mapped_count; } | ||
| 102 | VKAPI_ATTR VkDeviceAddress VKAPI_CALL mock_buffer_device_address(VkDevice, const VkBufferDeviceAddressInfo*) { | ||
| 103 | return kStageDeviceAddress; | ||
| 104 | } | ||
| 105 | VKAPI_ATTR void VKAPI_CALL mock_build_as(VkCommandBuffer, std::uint32_t info_count, | ||
| 106 | const VkAccelerationStructureBuildGeometryInfoKHR* infos, | ||
| 107 | const VkAccelerationStructureBuildRangeInfoKHR* const* ranges) { | ||
| 108 | ++as_builds; | ||
| 109 | if (info_count && infos && infos[0].geometryCount && infos[0].pGeometries && | ||
| 110 | infos[0].pGeometries[0].geometryType == VK_GEOMETRY_TYPE_INSTANCES_KHR) { | ||
| 111 | const auto& inst = infos[0].pGeometries[0].geometry.instances; | ||
| 112 | build_saw_array_of_pointers = inst.arrayOfPointers == VK_TRUE; | ||
| 113 | build_instances_address = inst.data.deviceAddress; | ||
| 114 | const std::uint32_t n = ranges && ranges[0] ? ranges[0][0].primitiveCount : 0; | ||
| 115 | staged_addresses.resize(n); | ||
| 116 | for (std::uint32_t i = 0; i < n; ++i) | ||
| 117 | std::memcpy(&staged_addresses[i], stage_bytes.data() + 8ull * i, sizeof(std::uint64_t)); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mock_proc(VkDevice, const char* name) { | ||
| 122 | #define PROC(vk_name, impl) \ | ||
| 123 | if (std::strcmp(name, #vk_name) == 0) return reinterpret_cast<PFN_vkVoidFunction>(impl) | ||
| 124 | PROC(vkCreateCommandPool, mock_create_pool); | ||
| 125 | PROC(vkDestroyCommandPool, mock_destroy_pool); | ||
| 126 | PROC(vkCreateSemaphore, mock_create_semaphore); | ||
| 127 | PROC(vkDestroySemaphore, mock_destroy_semaphore); | ||
| 128 | PROC(vkGetSemaphoreCounterValue, mock_semaphore_counter); | ||
| 129 | PROC(vkQueueWaitIdle, mock_queue_wait_idle); | ||
| 130 | PROC(vkAllocateCommandBuffers, mock_allocate_commands); | ||
| 131 | PROC(vkResetCommandBuffer, mock_reset_command); | ||
| 132 | PROC(vkBeginCommandBuffer, mock_begin_command); | ||
| 133 | PROC(vkEndCommandBuffer, mock_end_command); | ||
| 134 | PROC(vkQueueSubmit, mock_queue_submit); | ||
| 135 | PROC(vkCmdPipelineBarrier, mock_cmd_barrier); | ||
| 136 | PROC(vkCreateBuffer, mock_create_buffer); | ||
| 137 | PROC(vkDestroyBuffer, mock_destroy_buffer); | ||
| 138 | PROC(vkGetBufferMemoryRequirements, mock_buffer_memory_reqs); | ||
| 139 | PROC(vkAllocateMemory, mock_allocate_memory); | ||
| 140 | PROC(vkFreeMemory, mock_free_memory); | ||
| 141 | PROC(vkBindBufferMemory, mock_bind_buffer_memory); | ||
| 142 | PROC(vkMapMemory, mock_map_memory); | ||
| 143 | PROC(vkUnmapMemory, mock_unmap_memory); | ||
| 144 | PROC(vkGetBufferDeviceAddress, mock_buffer_device_address); | ||
| 145 | PROC(vkCmdBuildAccelerationStructuresKHR, mock_build_as); | ||
| 146 | #undef PROC | ||
| 147 | return nullptr; | ||
| 148 | } | ||
| 149 | |||
| 150 | leonida::ResourceBinding make_buffer(std::uintptr_t id, std::uint64_t va, std::uint64_t size, | ||
| 151 | const std::uint8_t* host) { | ||
| 152 | leonida::ResourceBinding b{}; | ||
| 153 | b.buffer = handle<VkBuffer>(id); | ||
| 154 | b.va = va; | ||
| 155 | b.size = size; | ||
| 156 | b.host = host; | ||
| 157 | return b; | ||
| 158 | } | ||
| 159 | |||
| 160 | leonida::ReplayResult run_tlas_pointers(leonida::VulkanContext& ctx, std::uint64_t dst_va, std::uint64_t pointers_va, | ||
| 161 | std::uint32_t instance_count) { | ||
| 162 | leonida::BuildAccelerationStructure b{}; | ||
| 163 | b.is_tlas = true; | ||
| 164 | b.dst_va = dst_va; | ||
| 165 | b.scratch_va = 0x20000; | ||
| 166 | b.instances_va = pointers_va; | ||
| 167 | b.instance_count = instance_count; | ||
| 168 | b.instance_pointers = true; | ||
| 169 | |||
| 170 | leonida::CommandList list; | ||
| 171 | expect(list.build_acceleration_structure(std::move(b)) && list.close(), "invalid TLAS test command list"); | ||
| 172 | leonida::VulkanQueue queue(ctx, handle<VkQueue>(1), 0); | ||
| 173 | expect(queue.ok(), "mock VulkanQueue did not initialize"); | ||
| 174 | const leonida::CommandList* lists[] = {&list}; | ||
| 175 | return queue.execute(lists); | ||
| 176 | } | ||
| 177 | |||
| 178 | } // namespace | ||
| 179 | |||
| 180 | int main() { | ||
| 181 | constexpr std::uint64_t kHeapBase = 0x1000000ull; | ||
| 182 | constexpr std::uint64_t kDstVa = 0x10000ull; | ||
| 183 | constexpr std::uint64_t kInstVa = 0x4000ull; | ||
| 184 | constexpr std::uint64_t kPtrVa = 0x8000ull; | ||
| 185 | constexpr std::uint64_t kGpuVa = 0xc000ull; | ||
| 186 | constexpr std::uint64_t kBaseVa = 0xe000ull; | ||
| 187 | |||
| 188 | { | ||
| 189 | leonida::VulkanContext ctx(mock_proc, handle<VkDevice>(1), false, false, true, false); | ||
| 190 | VkPhysicalDeviceMemoryProperties mem{}; | ||
| 191 | mem.memoryHeapCount = 1; | ||
| 192 | mem.memoryHeaps[0].size = 1024 * 1024; | ||
| 193 | mem.memoryTypes[0].propertyFlags = | ||
| 194 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 195 | mem.memoryTypes[0].heapIndex = 0; | ||
| 196 | mem.memoryTypeCount = 1; | ||
| 197 | ctx.set_occlusion_resolve_memory(mem); | ||
| 198 | ctx.set_heap_base(kHeapBase); | ||
| 199 | ctx.register_acceleration_structure(kDstVa, handle<VkAccelerationStructureKHR>(0x500)); | ||
| 200 | |||
| 201 | // Backing buffer covering both 64-byte instance descriptors. | ||
| 202 | std::array<std::uint8_t, 128> inst_storage{}; | ||
| 203 | ctx.add_resource(1, make_buffer(1, kInstVa, inst_storage.size(), inst_storage.data())); | ||
| 204 | |||
| 205 | // Case 1: Tied-start aliasing upload buffers: ID 10 holds 64 bytes, higher-ID 20 holds only 8 bytes. | ||
| 206 | // host_at(kPtrVa, 16) must pick ID 10 instead of failing on ID 20. | ||
| 207 | std::array<std::uint64_t, 8> big_ptrs{kInstVa, kInstVa + 64}; | ||
| 208 | std::array<std::uint64_t, 1> small_ptrs{kInstVa + 64}; | ||
| 209 | ctx.add_resource(10, make_buffer(10, kPtrVa, big_ptrs.size() * sizeof(std::uint64_t), | ||
| 210 | reinterpret_cast<const std::uint8_t*>(big_ptrs.data()))); | ||
| 211 | ctx.add_resource(20, make_buffer(20, kPtrVa, small_ptrs.size() * sizeof(std::uint64_t), | ||
| 212 | reinterpret_cast<const std::uint8_t*>(small_ptrs.data()))); | ||
| 213 | |||
| 214 | as_builds = 0; | ||
| 215 | staged_addresses.clear(); | ||
| 216 | expect(run_tlas_pointers(ctx, kDstVa, kPtrVa, 2) == leonida::ReplayResult::success, | ||
| 217 | "host_at must resolve aliasing larger placed upload buffer for full byte range"); | ||
| 218 | expect(as_builds == 1 && build_saw_array_of_pointers && build_instances_address == kStageDeviceAddress, | ||
| 219 | "TLAS build did not consume staged KHR pointer buffer"); | ||
| 220 | expect(staged_addresses.size() == 2 && staged_addresses[0] == kHeapBase + kInstVa && | ||
| 221 | staged_addresses[1] == kHeapBase + kInstVa + 64, | ||
| 222 | "staged instance device addresses came from wrong aliasing buffer"); | ||
| 223 | |||
| 224 | // Case 2: Request larger than every aliasing buffer (80 > 64) still fails with invalid_copy. | ||
| 225 | as_builds = 0; | ||
| 226 | expect(run_tlas_pointers(ctx, kDstVa, kPtrVa, 10) == leonida::ReplayResult::invalid_copy && as_builds == 0, | ||
| 227 | "oversized range past every aliasing buffer must fail"); | ||
| 228 | |||
| 229 | // Case 3: Non-CPU-written (DEFAULT/READBACK, host == nullptr) buffer returns unsupported without falling through. | ||
| 230 | ctx.add_resource(30, make_buffer(30, kGpuVa, 64, nullptr)); | ||
| 231 | expect(run_tlas_pointers(ctx, kDstVa, kGpuVa, 2) == leonida::ReplayResult::unsupported, | ||
| 232 | "GPU-only buffer must return unsupported"); | ||
| 233 | |||
| 234 | // Case 4: Offset aliasing: small placed buffer starting inside a larger CPU-mapped placed buffer. | ||
| 235 | std::array<std::uint64_t, 16> offset_big{}; | ||
| 236 | offset_big[4] = kInstVa; | ||
| 237 | offset_big[5] = kInstVa + 64; | ||
| 238 | std::array<std::uint64_t, 1> offset_small{kInstVa}; | ||
| 239 | ctx.add_resource(40, make_buffer(40, kBaseVa, offset_big.size() * sizeof(std::uint64_t), | ||
| 240 | reinterpret_cast<const std::uint8_t*>(offset_big.data()))); | ||
| 241 | ctx.add_resource(41, make_buffer(41, kBaseVa + 32, offset_small.size() * sizeof(std::uint64_t), | ||
| 242 | reinterpret_cast<const std::uint8_t*>(offset_small.data()))); | ||
| 243 | as_builds = 0; | ||
| 244 | staged_addresses.clear(); | ||
| 245 | expect(run_tlas_pointers(ctx, kDstVa, kBaseVa + 32, 2) == leonida::ReplayResult::success && as_builds == 1, | ||
| 246 | "host_at at offset must fall back to aliasing container buffer holding full range"); | ||
| 247 | expect(staged_addresses.size() == 2 && staged_addresses[0] == kHeapBase + kInstVa && | ||
| 248 | staged_addresses[1] == kHeapBase + kInstVa + 64, | ||
| 249 | "offset aliasing staged wrong addresses"); | ||
| 250 | } | ||
| 251 | |||
| 252 | expect(live_buffers == 0 && live_memories == 0 && mapped_count == 0, | ||
| 253 | "mock staging buffers/memories leaked after VulkanContext teardown"); | ||
| 254 | if (failures) return 1; | ||
| 255 | std::puts("vk_host_at_aliasing: ok"); | ||
| 256 | return 0; | ||
| 257 | } | ||