upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_backend.h
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 18:48:12 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 18:48:12 -0500
commitfb2afdccc2314d7b5a59fac10e6d6df7f5df35ab (patch)
tree9e0b7e2e39b42823f148d135ce781e57fab52155 /runtime/src/vulkan_backend.h
parent862890c893eb19eb39eb2fd95e7786e42ff4b18c (diff)
parent864337351bc11a250db77c2fcdd3ba6f1d0f455f (diff)
Merge main into RT-QUEUE18 (#403)
Merged on top of the merge made for #396 (main 507029d1e plus RT-QUEUE35, RT-ALLOC9 and RT-LIST139). No conflicts. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_backend.h')
-rw-r--r--runtime/src/vulkan_backend.h100
1 files changed, 79 insertions, 21 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index e3e4defe8..ab6786a51 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -101,6 +101,7 @@ struct VulkanDeviceFunctions {
101 X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ 101 X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \
102 X(vkCmdPushConstants) X(vkCmdDispatch) \ 102 X(vkCmdPushConstants) X(vkCmdDispatch) \
103 X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ 103 X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \
104 X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \
104 X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ 105 X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \
105 X(vkCmdSetBlendConstants) X(vkCmdDraw) \ 106 X(vkCmdSetBlendConstants) X(vkCmdDraw) \
106 X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ 107 X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \
@@ -244,12 +245,16 @@ public:
244 245
245 // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the 246 // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the
246 // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). 247 // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291).
247 // Segment i is in `mesh_queries` when mesh[i] is set (four counters: clipping, fragment and compute; see 248 // Segment i is in `mesh_queries` when mesh[i] is set (two counters: fragment and compute; see
248 // statistics_sum.comp), otherwise in `queries` (all eleven). 249 // statistics_sum.comp), otherwise in `queries` (all eleven). A mesh segment's clipping invocations come from
249 void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, const std::uint8_t* mesh, 250 // `primitives_queries`, the mesh-primitives count of that segment (D3D-MESH-CLIPPING-COUNTER); it is null when
250 std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { 251 // the device has no meshShaderQueries, and the field then reads 0.
252 void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries,
253 VkQueryPool primitives_queries, const std::uint8_t* mesh, std::uint32_t first,
254 std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const {
251 constexpr VkDeviceSize result = statistics_counters * word_size; 255 constexpr VkDeviceSize result = statistics_counters * word_size;
252 constexpr VkDeviceSize mesh_slot = 5 * word_size; // CInvocations, where the mesh pool's first counter goes 256 constexpr VkDeviceSize mesh_slot = 7 * word_size; // PSInvocations, where the mesh pool's first counter goes
257 constexpr VkDeviceSize clipping_slot = 5 * word_size; // CInvocations, where the mesh-primitives count goes
253 begin_scratch_write(cb); 258 begin_scratch_write(cb);
254 vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); 259 vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0);
255 bind(cb, statistics_pipeline_); 260 bind(cb, statistics_pipeline_);
@@ -258,9 +263,18 @@ public:
258 std::uint32_t chunk = 1; 263 std::uint32_t chunk = 1;
259 while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) 264 while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh)
260 ++chunk; 265 ++chunk;
266 if (in_mesh) {
267 // The mesh pool holds only the fragment and compute counters, so the clipping field of a mesh
268 // segment reads 0 unless the mesh-primitives count fills it (D3D-MESH-CLIPPING-COUNTER).
269 vk_.vkCmdFillBuffer(cb, scratch_, result, chunk * result, 0);
270 barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
271 }
261 vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, 272 vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_,
262 result + (in_mesh ? mesh_slot : 0), result, 273 result + (in_mesh ? mesh_slot : 0), result,
263 VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); 274 VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
275 if (in_mesh && primitives_queries)
276 vk_.vkCmdCopyQueryPoolResults(cb, primitives_queries, first, chunk, scratch_, result + clipping_slot,
277 result, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
264 barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, 278 barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT,
265 shader_access); 279 shader_access);
266 dispatch(cb, chunk, 1, in_mesh); 280 dispatch(cb, chunk, 1, in_mesh);
@@ -923,6 +937,10 @@ struct TileRange {
923#endif 937#endif
924class VulkanContext { 938class VulkanContext {
925public: 939public:
940 // mesh_queries: VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so a mesh segment's
941 // clipping invocations can come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Set by the device
942 // that created the context.
943 bool mesh_queries = false;
926 // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). 944 // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication).
927 // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. 945 // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`.
928 // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. 946 // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress.
@@ -970,6 +988,8 @@ public:
970 replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer; 988 replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer;
971 replay_.set_viewport = vk.vkCmdSetViewport; 989 replay_.set_viewport = vk.vkCmdSetViewport;
972 replay_.set_scissor = vk.vkCmdSetScissor; 990 replay_.set_scissor = vk.vkCmdSetScissor;
991 replay_.set_viewport_with_count = vk.vkCmdSetViewportWithCount;
992 replay_.set_scissor_with_count = vk.vkCmdSetScissorWithCount;
973 replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology; 993 replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology;
974 replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; 994 replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable;
975 replay_.set_stencil_reference = vk.vkCmdSetStencilReference; 995 replay_.set_stencil_reference = vk.vkCmdSetStencilReference;
@@ -1000,7 +1020,7 @@ public:
1000 // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write. 1020 // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write.
1001 replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) { 1021 replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) {
1002 BufferVaIndex<ResourceId>::Hit hit{}; 1022 BufferVaIndex<ResourceId>::Hit hit{};
1003 if (!buffer_vas_.find(va, hit)) return HostReadStatus::absent; 1023 if (!buffer_vas_.find(va, hit, bytes)) return HostReadStatus::absent;
1004 const ResourceBinding* b = resolve(hit.id); 1024 const ResourceBinding* b = resolve(hit.id);
1005 if (!b) return HostReadStatus::absent; 1025 if (!b) return HostReadStatus::absent;
1006 if (!b->host) return HostReadStatus::not_cpu_written; 1026 if (!b->host) return HostReadStatus::not_cpu_written;
@@ -1268,6 +1288,11 @@ public:
1268 replay_.zero_size = size; 1288 replay_.zero_size = size;
1269 } 1289 }
1270 1290
1291 void set_depth_range_unrestricted(bool enabled) {
1292 std::unique_lock lock(resource_mutex_);
1293 replay_.depth_range_unrestricted = enabled;
1294 }
1295
1271 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. 1296 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears.
1272 void set_color_attachment_format(std::function<bool(VkFormat)> check) { 1297 void set_color_attachment_format(std::function<bool(VkFormat)> check) {
1273 std::unique_lock lock(resource_mutex_); 1298 std::unique_lock lock(resource_mutex_);
@@ -1483,12 +1508,23 @@ public:
1483 void note_bindless_upload() { bindless_upload_pending_ = true; } 1508 void note_bindless_upload() { bindless_upload_pending_ = true; }
1484 1509
1485 std::uint64_t completed_retire_ticket() { 1510 std::uint64_t completed_retire_ticket() {
1486 std::lock_guard lock(submit_mutex_); 1511 bool lost = false;
1487 if (is_device_lost()) return 0; 1512 std::uint64_t ticket = 0;
1488 return retire_tickets_.completed([this](VkSemaphore semaphore, std::uint64_t value) { 1513 {
1489 std::uint64_t done = 0; 1514 std::lock_guard lock(submit_mutex_);
1490 return vk.vkGetSemaphoreCounterValue(device_, semaphore, &done) == VK_SUCCESS && done >= value; 1515 if (is_device_lost()) return 0;
1491 }); 1516 ticket = retire_tickets_.completed([this, &lost](VkSemaphore semaphore, std::uint64_t value) {
1517 std::uint64_t done = 0;
1518 const VkResult result = vk.vkGetSemaphoreCounterValue(device_, semaphore, &done);
1519 if (result == VK_ERROR_DEVICE_LOST) lost = true;
1520 return result == VK_SUCCESS && done >= value;
1521 });
1522 }
1523 // Outside submit_mutex_, as in submit(): notifying the fence dispatcher can run callbacks that submit.
1524 // A DEVICE_LOST seen here is the first Vulkan operation to discover it; it must not be mistaken for
1525 // "this ticket has not completed".
1526 if (lost) mark_device_lost();
1527 return lost ? 0 : ticket;
1492 } 1528 }
1493 1529
1494 void drain_deleted_resources(std::uint64_t current_ticket) { 1530 void drain_deleted_resources(std::uint64_t current_ticket) {
@@ -2394,9 +2430,11 @@ private:
2394 occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); 2430 occlusion_resolve_->normalize(cb, pool, first, count, dst, offset);
2395 }; 2431 };
2396 replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, 2432 replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool,
2397 const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, 2433 VkQueryPool primitives_pool, const std::uint8_t* mesh,
2398 VkBuffer dst, VkDeviceSize offset) { 2434 std::uint32_t first, std::uint32_t count, VkBuffer dst,
2399 occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); 2435 VkDeviceSize offset) {
2436 occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, primitives_pool, mesh, first, count, dst,
2437 offset);
2400 }; 2438 };
2401 replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, 2439 replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first,
2402 std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { 2440 std::uint32_t count, VkBuffer dst, VkDeviceSize offset) {
@@ -2443,18 +2481,26 @@ private:
2443 if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { 2481 if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) {
2444 statistics->mesh.clear(); 2482 statistics->mesh.clear();
2445 } else { 2483 } else {
2446 // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping 2484 // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): the
2447 // invocations and primitives, fragment and compute invocations (D3D-291). 2485 // fragment and compute invocations, and no other (D3D-291).
2448 VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; 2486 VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
2449 info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; 2487 info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS;
2450 info.queryCount = statistics_segments; 2488 info.queryCount = statistics_segments;
2451 info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | 2489 info.pipelineStatistics = kMeshStatisticsFlags;
2452 VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT |
2453 VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT |
2454 VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
2455 if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != 2490 if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) !=
2456 VK_SUCCESS) 2491 VK_SUCCESS)
2457 return ReplayResult::unsupported; 2492 return ReplayResult::unsupported;
2493 // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type,
2494 // which the pipeline-statistics pool cannot hold. The device needs the meshShaderQueries feature,
2495 // or the clipping field of a mesh segment stays 0.
2496 if (context_.mesh_queries) {
2497 VkQueryPoolCreateInfo primitives{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
2498 primitives.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT;
2499 primitives.queryCount = statistics_segments;
2500 if (context_.vk.vkCreateQueryPool(statistics->device, &primitives, nullptr,
2501 &statistics->primitives_pool) != VK_SUCCESS)
2502 return ReplayResult::unsupported;
2503 }
2458 } 2504 }
2459 } 2505 }
2460 // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after 2506 // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after
@@ -2515,6 +2561,18 @@ private:
2515 discard(); 2561 discard();
2516 return ReplayResult::unsupported; 2562 return ReplayResult::unsupported;
2517 } 2563 }
2564 // D3D12 ends every ExecuteCommandLists with all its work done and its writes visible to the next one on the
2565 // queue (implicit state decay relies on it), and a list may then touch the same memory without a barrier:
2566 // two submissions copying into one buffer, say. Vulkan orders submissions on a queue only by what their
2567 // barriers say, so this one starts with a full memory barrier against everything submitted before it
2568 // (synchronization validation: WRITE_AFTER_WRITE between vk_queue_test's back-to-back copies).
2569 if (context_.vk.vkCmdPipelineBarrier) { // a mocked context may leave it out
2570 VkMemoryBarrier full{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
2571 full.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
2572 full.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
2573 context_.vk.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
2574 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &full, 0, nullptr, 0, nullptr);
2575 }
2518 if (!tracker_lock.owns_lock()) tracker_lock.lock(); 2576 if (!tracker_lock.owns_lock()) tracker_lock.lock();
2519 if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { 2577 if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) {
2520 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 2578 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;