diff options
| author | Rain156 <77776581+Rain156@users.noreply.github.com> | 2026-11-24 21:40:18 +0800 |
|---|---|---|
| committer | Rain156 <77776581+Rain156@users.noreply.github.com> | 2026-11-24 21:40:18 +0800 |
| commit | 1b1d4edd2dea9ef1fbabfadb8e3d719be39e38eb (patch) | |
| tree | b9a8aa144a60538b022cdf92f85cd6ee679d171b /runtime/src/vulkan_backend.h | |
| parent | abbd7d9b27dd624bc1446ced9539b78a0193c1ba (diff) | |
| parent | 47c6511747c93aa3fb908d7782ca96173d0eead3 (diff) | |
Merge current main and retain mesh statistics in the first-run rendering context
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 72c716ed1..23b14c0bb 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -245,12 +245,16 @@ public: | |||
| 245 | 245 | ||
| 246 | // 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 |
| 247 | // 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). |
| 248 | // 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 |
| 249 | // 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 |
| 250 | 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 |
| 251 | 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 { | ||
| 252 | constexpr VkDeviceSize result = statistics_counters * word_size; | 255 | constexpr VkDeviceSize result = statistics_counters * word_size; |
| 253 | 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 | ||
| 254 | begin_scratch_write(cb); | 258 | begin_scratch_write(cb); |
| 255 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); | 259 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); |
| 256 | bind(cb, statistics_pipeline_); | 260 | bind(cb, statistics_pipeline_); |
| @@ -259,9 +263,18 @@ public: | |||
| 259 | std::uint32_t chunk = 1; | 263 | std::uint32_t chunk = 1; |
| 260 | 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) |
| 261 | ++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 | } | ||
| 262 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, | 272 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, |
| 263 | result + (in_mesh ? mesh_slot : 0), result, | 273 | result + (in_mesh ? mesh_slot : 0), result, |
| 264 | 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); | ||
| 265 | 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, |
| 266 | shader_access); | 279 | shader_access); |
| 267 | dispatch(cb, chunk, 1, in_mesh); | 280 | dispatch(cb, chunk, 1, in_mesh); |
| @@ -924,6 +937,10 @@ struct TileRange { | |||
| 924 | #endif | 937 | #endif |
| 925 | class VulkanContext { | 938 | class VulkanContext { |
| 926 | public: | 939 | public: |
| 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; | ||
| 927 | // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). | 944 | // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). |
| 928 | // 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`. |
| 929 | // 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. |
| @@ -2524,9 +2541,11 @@ private: | |||
| 2524 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); | 2541 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); |
| 2525 | }; | 2542 | }; |
| 2526 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, | 2543 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, |
| 2527 | const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, | 2544 | VkQueryPool primitives_pool, const std::uint8_t* mesh, |
| 2528 | VkBuffer dst, VkDeviceSize offset) { | 2545 | std::uint32_t first, std::uint32_t count, VkBuffer dst, |
| 2529 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); | 2546 | VkDeviceSize offset) { |
| 2547 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, primitives_pool, mesh, first, count, dst, | ||
| 2548 | offset); | ||
| 2530 | }; | 2549 | }; |
| 2531 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | 2550 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, |
| 2532 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | 2551 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { |
| @@ -2573,18 +2592,26 @@ private: | |||
| 2573 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { | 2592 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { |
| 2574 | statistics->mesh.clear(); | 2593 | statistics->mesh.clear(); |
| 2575 | } else { | 2594 | } else { |
| 2576 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping | 2595 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): the |
| 2577 | // invocations and primitives, fragment and compute invocations (D3D-291). | 2596 | // fragment and compute invocations, and no other (D3D-291). |
| 2578 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | 2597 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; |
| 2579 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; | 2598 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; |
| 2580 | info.queryCount = statistics_segments; | 2599 | info.queryCount = statistics_segments; |
| 2581 | info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | | 2600 | info.pipelineStatistics = kMeshStatisticsFlags; |
| 2582 | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | | ||
| 2583 | VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | | ||
| 2584 | VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; | ||
| 2585 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != | 2601 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != |
| 2586 | VK_SUCCESS) | 2602 | VK_SUCCESS) |
| 2587 | return ReplayResult::unsupported; | 2603 | return ReplayResult::unsupported; |
| 2604 | // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type, | ||
| 2605 | // which the pipeline-statistics pool cannot hold. The device needs the meshShaderQueries feature, | ||
| 2606 | // or the clipping field of a mesh segment stays 0. | ||
| 2607 | if (context_.mesh_queries) { | ||
| 2608 | VkQueryPoolCreateInfo primitives{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | ||
| 2609 | primitives.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT; | ||
| 2610 | primitives.queryCount = statistics_segments; | ||
| 2611 | if (context_.vk.vkCreateQueryPool(statistics->device, &primitives, nullptr, | ||
| 2612 | &statistics->primitives_pool) != VK_SUCCESS) | ||
| 2613 | return ReplayResult::unsupported; | ||
| 2614 | } | ||
| 2588 | } | 2615 | } |
| 2589 | } | 2616 | } |
| 2590 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after | 2617 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after |