diff options
| author | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 15:47:22 +0000 |
|---|---|---|
| committer | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 15:47:22 +0000 |
| commit | b721585aae2efb5e78cf7ee2467fe8c4d5e0ca35 (patch) | |
| tree | 62803d5697e35182026fc910b64827a108ffc819 /runtime/src/vulkan_backend.h | |
| parent | c6f15b16283081c72123e61ca08cf5eb83e301e2 (diff) | |
| parent | 6de3970e15dc2ef8542ea1a289105736e2241624 (diff) | |
Merge remote-tracking branch 'origin/main' into exe/import-fail-fast
# Conflicts:
# docs/README.md
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index dc042bf73..e659a762d 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -96,16 +96,17 @@ struct VulkanDeviceFunctions { | |||
| 96 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ | 96 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ |
| 97 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ | 97 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ |
| 98 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ | 98 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ |
| 99 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) \ | 99 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ |
| 100 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ | 100 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ |
| 101 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) | 101 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) |
| 102 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; | 102 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; |
| 103 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) | 103 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) |
| 104 | #undef LEONIDA_VK_MEMBER | 104 | #undef LEONIDA_VK_MEMBER |
| 105 | 105 | ||
| 106 | // The Vulkan 1.3 entry points may be null on older devices, the extension ones (conditional rendering) when the | 106 | // The Vulkan 1.3 entry points may be null on older devices, the extension ones (conditional rendering, mesh |
| 107 | // extension is not enabled, and the query functions are optional (a list that uses queries without them is | 107 | // shaders) when the extension is not enabled, and the query functions are optional (a list that uses queries |
| 108 | // unsupported). The compute and descriptor functions are needed for normalized or summed occlusion results. | 108 | // without them is unsupported). The compute and descriptor functions are needed for normalized or summed |
| 109 | // occlusion results. | ||
| 109 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | 110 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { |
| 110 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(leonida::device_proc_or_khr(get, device, #name)); | 111 | #define LEONIDA_VK_LOAD(name) name = reinterpret_cast<PFN_##name>(leonida::device_proc_or_khr(get, device, #name)); |
| 111 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) | 112 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_LOAD) |
| @@ -211,7 +212,8 @@ public: | |||
| 211 | write.pBufferInfo = &storage; | 212 | write.pBufferInfo = &storage; |
| 212 | vk_.vkUpdateDescriptorSets(device_, 1, &write, 0, nullptr); | 213 | vk_.vkUpdateDescriptorSets(device_, 1, &write, 0, nullptr); |
| 213 | 214 | ||
| 214 | const VkPushConstantRange count{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(std::uint32_t)}; | 215 | // The count, and for statistics_sum.comp whether the segments are mesh-pool results. |
| 216 | const VkPushConstantRange count{VK_SHADER_STAGE_COMPUTE_BIT, 0, 2 * sizeof(std::uint32_t)}; | ||
| 215 | VkPipelineLayoutCreateInfo layout{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | 217 | VkPipelineLayoutCreateInfo layout{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; |
| 216 | layout.setLayoutCount = 1; | 218 | layout.setLayoutCount = 1; |
| 217 | layout.pSetLayouts = &set_layout_; | 219 | layout.pSetLayouts = &set_layout_; |
| @@ -231,21 +233,28 @@ public: | |||
| 231 | create_pipeline(statistics_sum_spirv, statistics_pipeline_)); | 233 | create_pipeline(statistics_sum_spirv, statistics_pipeline_)); |
| 232 | } | 234 | } |
| 233 | 235 | ||
| 234 | // Adds `count` pipeline-statistics results (11 counters each) into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at | 236 | // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the |
| 235 | // dst + offset: the segments of a statistics query that was split around helper dispatches (D3D-162). | 237 | // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). |
| 236 | void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, std::uint32_t first, std::uint32_t count, | 238 | // Segment i is in `mesh_queries` when mesh[i] is set (four counters: clipping, fragment and compute; see |
| 237 | VkBuffer dst, VkDeviceSize offset) const { | 239 | // statistics_sum.comp), otherwise in `queries` (all eleven). |
| 240 | void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, const std::uint8_t* mesh, | ||
| 241 | std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { | ||
| 238 | constexpr VkDeviceSize result = statistics_counters * word_size; | 242 | constexpr VkDeviceSize result = statistics_counters * word_size; |
| 243 | constexpr VkDeviceSize mesh_slot = 5 * word_size; // CInvocations, where the mesh pool's first counter goes | ||
| 239 | begin_scratch_write(cb); | 244 | begin_scratch_write(cb); |
| 240 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); | 245 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); |
| 241 | bind(cb, statistics_pipeline_); | 246 | bind(cb, statistics_pipeline_); |
| 242 | while (count) { | 247 | while (count) { |
| 243 | const auto chunk = std::min(count, statistics_capacity); | 248 | const bool in_mesh = mesh && mesh[first]; |
| 244 | vk_.vkCmdCopyQueryPoolResults(cb, queries, first, chunk, scratch_, result, result, | 249 | std::uint32_t chunk = 1; |
| 250 | while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) | ||
| 251 | ++chunk; | ||
| 252 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, | ||
| 253 | result + (in_mesh ? mesh_slot : 0), result, | ||
| 245 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | 254 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 246 | barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, | 255 | barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, |
| 247 | shader_access); | 256 | shader_access); |
| 248 | dispatch(cb, chunk, 1); | 257 | dispatch(cb, chunk, 1, in_mesh); |
| 249 | first += chunk; | 258 | first += chunk; |
| 250 | count -= chunk; | 259 | count -= chunk; |
| 251 | if (count) barrier(cb, compute, transfer, shader_access, VK_ACCESS_TRANSFER_WRITE_BIT); | 260 | if (count) barrier(cb, compute, transfer, shader_access, VK_ACCESS_TRANSFER_WRITE_BIT); |
| @@ -339,8 +348,9 @@ private: | |||
| 339 | vk_.vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout_, 0, 1, &set_, 0, nullptr); | 348 | vk_.vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout_, 0, 1, &set_, 0, nullptr); |
| 340 | } | 349 | } |
| 341 | 350 | ||
| 342 | void dispatch(VkCommandBuffer cb, std::uint32_t chunk, std::uint32_t groups) const { | 351 | void dispatch(VkCommandBuffer cb, std::uint32_t chunk, std::uint32_t groups, bool mesh = false) const { |
| 343 | vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(chunk), &chunk); | 352 | const std::uint32_t range[2] = {chunk, mesh ? 1u : 0u}; |
| 353 | vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(range), range); | ||
| 344 | vk_.vkCmdDispatch(cb, groups, 1, 1); | 354 | vk_.vkCmdDispatch(cb, groups, 1, 1); |
| 345 | } | 355 | } |
| 346 | 356 | ||
| @@ -956,6 +966,7 @@ public: | |||
| 956 | replay_.set_stencil_reference = vk.vkCmdSetStencilReference; | 966 | replay_.set_stencil_reference = vk.vkCmdSetStencilReference; |
| 957 | replay_.set_blend_constants = vk.vkCmdSetBlendConstants; | 967 | replay_.set_blend_constants = vk.vkCmdSetBlendConstants; |
| 958 | replay_.draw = vk.vkCmdDraw; | 968 | replay_.draw = vk.vkCmdDraw; |
| 969 | replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled | ||
| 959 | replay_.draw_indexed = vk.vkCmdDrawIndexed; | 970 | replay_.draw_indexed = vk.vkCmdDrawIndexed; |
| 960 | replay_.graphics_pipeline = [this](std::uintptr_t id) -> const GraphicsPipeline* { | 971 | replay_.graphics_pipeline = [this](std::uintptr_t id) -> const GraphicsPipeline* { |
| 961 | const auto it = graphics_pipelines_.find(id); | 972 | const auto it = graphics_pipelines_.find(id); |
| @@ -2276,9 +2287,10 @@ private: | |||
| 2276 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | 2287 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { |
| 2277 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); | 2288 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); |
| 2278 | }; | 2289 | }; |
| 2279 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | 2290 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, |
| 2280 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | 2291 | const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, |
| 2281 | occlusion_resolve_->sum_statistics(cb, pool, first, count, dst, offset); | 2292 | VkBuffer dst, VkDeviceSize offset) { |
| 2293 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); | ||
| 2282 | }; | 2294 | }; |
| 2283 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | 2295 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, |
| 2284 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | 2296 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { |
| @@ -2314,10 +2326,31 @@ private: | |||
| 2314 | }; | 2326 | }; |
| 2315 | std::shared_ptr<OcclusionSegmentPool> physical, statistics; | 2327 | std::shared_ptr<OcclusionSegmentPool> physical, statistics; |
| 2316 | if (segments && !segment_pool(VK_QUERY_TYPE_OCCLUSION, segments, physical)) return ReplayResult::unsupported; | 2328 | if (segments && !segment_pool(VK_QUERY_TYPE_OCCLUSION, segments, physical)) return ReplayResult::unsupported; |
| 2317 | // Without the compute helper a split query could not be summed: leave statistics queries whole, as before. | 2329 | // Without the compute helper a split query could not be summed: leave statistics queries whole, as before |
| 2318 | if (statistics_segments && can_resolve_occlusion && | 2330 | // (QueryReplay::plan refuses a list whose statistics query holds a mesh draw then). |
| 2319 | !segment_pool(VK_QUERY_TYPE_PIPELINE_STATISTICS, statistics_segments, statistics)) | 2331 | if (statistics_segments && can_resolve_occlusion) { |
| 2320 | return ReplayResult::unsupported; | 2332 | if (!segment_pool(VK_QUERY_TYPE_PIPELINE_STATISTICS, statistics_segments, statistics)) |
| 2333 | return ReplayResult::unsupported; | ||
| 2334 | for (const auto& list : prepared) | ||
| 2335 | statistics->mesh.insert(statistics->mesh.end(), list.statistics_mesh.begin(), | ||
| 2336 | list.statistics_mesh.end()); | ||
| 2337 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { | ||
| 2338 | statistics->mesh.clear(); | ||
| 2339 | } else { | ||
| 2340 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping | ||
| 2341 | // invocations and primitives, fragment and compute invocations (D3D-291). | ||
| 2342 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | ||
| 2343 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; | ||
| 2344 | info.queryCount = statistics_segments; | ||
| 2345 | info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | | ||
| 2346 | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | | ||
| 2347 | VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | | ||
| 2348 | VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; | ||
| 2349 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != | ||
| 2350 | VK_SUCCESS) | ||
| 2351 | return ReplayResult::unsupported; | ||
| 2352 | } | ||
| 2353 | } | ||
| 2321 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after | 2354 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after |
| 2322 | // resource_lock is unlocked. Hold tracker_mutex_ across plan -> emit -> submit -> commit so concurrent queues | 2355 | // resource_lock is unlocked. Hold tracker_mutex_ across plan -> emit -> submit -> commit so concurrent queues |
| 2323 | // see submission-order query state and remove_query_pool() waits until commit finishes. | 2356 | // see submission-order query state and remove_query_pool() waits until commit finishes. |