diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-25 23:37:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 23:37:24 +0100 |
| commit | 5fab9396763ca4d052af8abc71f29ad200e7b5ef (patch) | |
| tree | a8a751a8bc7e630bc6346d348b1405b2121de134 /runtime/src | |
| parent | 1bf5eeafd7041db163a6f027ba6145262f70e48a (diff) | |
| parent | 844519ab597c910797606f14ea10e0d95e99210e (diff) | |
Merge pull request #1630 from BubsMcgee/stream-output
d3d12: real SO_STATISTICS query pools (D3D-302)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 13 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 68 |
2 files changed, 72 insertions, 9 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index ecc2eef9c..68ab9b194 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -130,7 +130,8 @@ struct VulkanDeviceFunctions { | |||
| 130 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \ | 130 | X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \ |
| 131 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ | 131 | X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ |
| 132 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ | 132 | X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ |
| 133 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) | 133 | X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) \ |
| 134 | X(vkCmdBeginQueryIndexedEXT) X(vkCmdEndQueryIndexedEXT) | ||
| 134 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; | 135 | #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; |
| 135 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) | 136 | LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) |
| 136 | #undef LEONIDA_VK_MEMBER | 137 | #undef LEONIDA_VK_MEMBER |
| @@ -165,6 +166,9 @@ struct VulkanDeviceFunctions { | |||
| 165 | return vkCmdBeginRendering && vkCmdEndRendering && vkCmdBindVertexBuffers2 && vkCmdSetPrimitiveTopology && | 166 | return vkCmdBeginRendering && vkCmdEndRendering && vkCmdBindVertexBuffers2 && vkCmdSetPrimitiveTopology && |
| 166 | vkCmdSetPrimitiveRestartEnable; | 167 | vkCmdSetPrimitiveRestartEnable; |
| 167 | } | 168 | } |
| 169 | // VK_EXT_transform_feedback is enabled on the device: the indexed-query entry points are loaded, so an | ||
| 170 | // SO_STATISTICS heap can be a real transform-feedback pool (D3D-302). | ||
| 171 | bool transform_feedback = false; | ||
| 168 | }; | 172 | }; |
| 169 | 173 | ||
| 170 | // Both kinds of compute resolve use the same storage buffer, descriptor set and pipeline layout. | 174 | // Both kinds of compute resolve use the same storage buffer, descriptor set and pipeline layout. |
| @@ -1090,6 +1094,8 @@ public: | |||
| 1090 | GraphicsIndirectCapabilities indirect = {}) | 1094 | GraphicsIndirectCapabilities indirect = {}) |
| 1091 | : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) { | 1095 | : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) { |
| 1092 | loaded_ = vk.load(get, device); | 1096 | loaded_ = vk.load(get, device); |
| 1097 | // VK_EXT_transform_feedback: enabled on the device when its indexed-query entry points resolved (D3D-302). | ||
| 1098 | vk.transform_feedback = vk.vkCmdBeginQueryIndexedEXT && vk.vkCmdEndQueryIndexedEXT; | ||
| 1093 | if (loaded_ && gpu_labels_.initialize(device, get)) { | 1099 | if (loaded_ && gpu_labels_.initialize(device, get)) { |
| 1094 | replay_.gpu_label = [this](VkCommandBuffer cb, const label::Write& write) { gpu_labels_.record(cb, write); }; | 1100 | replay_.gpu_label = [this](VkCommandBuffer cb, const label::Write& write) { gpu_labels_.record(cb, write); }; |
| 1095 | } | 1101 | } |
| @@ -1125,6 +1131,11 @@ public: | |||
| 1125 | replay_.reset_query_pool = vk.vkCmdResetQueryPool; | 1131 | replay_.reset_query_pool = vk.vkCmdResetQueryPool; |
| 1126 | replay_.begin_query = vk.vkCmdBeginQuery; | 1132 | replay_.begin_query = vk.vkCmdBeginQuery; |
| 1127 | replay_.end_query = vk.vkCmdEndQuery; | 1133 | replay_.end_query = vk.vkCmdEndQuery; |
| 1134 | // VK_EXT_transform_feedback (D3D-302), loaded when the device enabled it: indexed SO_STATISTICS queries. | ||
| 1135 | if (vk.transform_feedback) { | ||
| 1136 | replay_.begin_query_indexed = vk.vkCmdBeginQueryIndexedEXT; | ||
| 1137 | replay_.end_query_indexed = vk.vkCmdEndQueryIndexedEXT; | ||
| 1138 | } | ||
| 1128 | replay_.write_timestamp = vk.vkCmdWriteTimestamp; | 1139 | replay_.write_timestamp = vk.vkCmdWriteTimestamp; |
| 1129 | replay_.copy_query_pool_results = vk.vkCmdCopyQueryPoolResults; | 1140 | replay_.copy_query_pool_results = vk.vkCmdCopyQueryPoolResults; |
| 1130 | replay_.bind_pipeline = vk.vkCmdBindPipeline; | 1141 | replay_.bind_pipeline = vk.vkCmdBindPipeline; |
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 449f8de5d..d84f26ccf 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -844,14 +844,19 @@ struct OcclusionResult { | |||
| 844 | // A D3D12 query heap on Vulkan. The host tracks which queries hold a result. A resolve of a query that | 844 | // A D3D12 query heap on Vulkan. The host tracks which queries hold a result. A resolve of a query that |
| 845 | // was never ended writes zeros instead of waiting on it. | 845 | // was never ended writes zeros instead of waiting on it. |
| 846 | struct QueryPool { | 846 | struct QueryPool { |
| 847 | // Null for a stream-output statistics heap: the layer has no stream output (PSOs with StreamOutput entries are | 847 | // Null for a stream-output statistics heap on a device without VK_EXT_transform_feedback (D3D-302): every |
| 848 | // refused), so every SO_STATISTICS_STREAMn query counts 0 written and 0 needed, and a resolve fills zeros. | 848 | // SO_STATISTICS_STREAMn query then counts 0 written and 0 needed, and a resolve fills zeros. With the feature |
| 849 | // it is a VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT pool, begun/ended indexed on the stream. | ||
| 849 | VkQueryPool pool = VK_NULL_HANDLE; | 850 | VkQueryPool pool = VK_NULL_HANDLE; |
| 850 | std::uint32_t heap_type = 0; // d3d12_query_heap | 851 | std::uint32_t heap_type = 0; // d3d12_query_heap |
| 851 | std::uint32_t count = 0; | 852 | std::uint32_t count = 0; |
| 852 | bool precise = false; // occlusionQueryPrecise: D3D12_QUERY_TYPE_OCCLUSION counts exact samples | 853 | bool precise = false; // occlusionQueryPrecise: D3D12_QUERY_TYPE_OCCLUSION counts exact samples |
| 853 | std::vector<bool> ended; | 854 | std::vector<bool> ended; |
| 854 | std::unordered_map<std::uint32_t, OcclusionResult> occlusion; | 855 | std::unordered_map<std::uint32_t, OcclusionResult> occlusion; |
| 856 | // Transform-feedback streams the pool can query (D3D-302): 0 when it has no pool, otherwise the device's | ||
| 857 | // maxTransformFeedbackStreams clamped to D3D12's four. SO_STATISTICS_STREAMn with n at or above this runs nothing | ||
| 858 | // and resolves zero. | ||
| 859 | std::uint32_t streams = 0; | ||
| 855 | }; | 860 | }; |
| 856 | 861 | ||
| 857 | // A compute pipeline as replay needs it. code_base is the console VA of the shader's FSSM blob (push-constant | 862 | // A compute pipeline as replay needs it. code_base is the console VA of the shader's FSSM blob (push-constant |
| @@ -1175,6 +1180,10 @@ struct ReplayFunctions { | |||
| 1175 | PFN_vkCmdResetQueryPool reset_query_pool = nullptr; // queries, with fill_buffer for unavailable results | 1180 | PFN_vkCmdResetQueryPool reset_query_pool = nullptr; // queries, with fill_buffer for unavailable results |
| 1176 | PFN_vkCmdBeginQuery begin_query = nullptr; | 1181 | PFN_vkCmdBeginQuery begin_query = nullptr; |
| 1177 | PFN_vkCmdEndQuery end_query = nullptr; | 1182 | PFN_vkCmdEndQuery end_query = nullptr; |
| 1183 | // VK_EXT_transform_feedback (D3D-302): SO_STATISTICS queries begin/end indexed on a stream. Null when the device | ||
| 1184 | // lacks the feature. | ||
| 1185 | PFN_vkCmdBeginQueryIndexedEXT begin_query_indexed = nullptr; | ||
| 1186 | PFN_vkCmdEndQueryIndexedEXT end_query_indexed = nullptr; | ||
| 1178 | PFN_vkCmdWriteTimestamp write_timestamp = nullptr; | 1187 | PFN_vkCmdWriteTimestamp write_timestamp = nullptr; |
| 1179 | PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; | 1188 | PFN_vkCmdCopyQueryPoolResults copy_query_pool_results = nullptr; |
| 1180 | // Set only where a copy of timestamp results is not ordered after the timestamps written before it (MoltenVK on | 1189 | // Set only where a copy of timestamp results is not ordered after the timestamps written before it (MoltenVK on |
| @@ -3387,6 +3396,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 3387 | out.unpredicated_copies = 0; | 3396 | out.unpredicated_copies = 0; |
| 3388 | const bool queries = vk.reset_query_pool && vk.begin_query && vk.end_query && vk.write_timestamp && | 3397 | const bool queries = vk.reset_query_pool && vk.begin_query && vk.end_query && vk.write_timestamp && |
| 3389 | vk.copy_query_pool_results && vk.fill_buffer && vk.query_pool; | 3398 | vk.copy_query_pool_results && vk.fill_buffer && vk.query_pool; |
| 3399 | // An SO_STATISTICS query on a real transform-feedback heap additionally needs the indexed begin/end (D3D-302). | ||
| 3400 | const bool so_queries = vk.begin_query_indexed && vk.end_query_indexed; | ||
| 3390 | // Vulkan allows one active query per type. Overlapping ordinary and binary occlusion share one physical | 3401 | // Vulkan allows one active query per type. Overlapping ordinary and binary occlusion share one physical |
| 3391 | // segment pool, and overlapping statistics split into the statistics pool (D3D-252); a physical query of a | 3402 | // segment pool, and overlapping statistics split into the statistics pool (D3D-252); a physical query of a |
| 3392 | // type is still open at most once at any point, because each event closes the one open slot and opens a new | 3403 | // type is still open at most once at any point, because each event closes the one open slot and opens a new |
| @@ -3394,6 +3405,10 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 3394 | VkDeviceAddress indirect_list_budget = 0; | 3405 | VkDeviceAddress indirect_list_budget = 0; |
| 3395 | std::size_t ordinary_active = 0, binary_active = 0, statistics_active_count = 0; | 3406 | std::size_t ordinary_active = 0, binary_active = 0, statistics_active_count = 0; |
| 3396 | bool overlaps = false, statistics_overlaps = false, statistics_active = false; | 3407 | bool overlaps = false, statistics_overlaps = false, statistics_active = false; |
| 3408 | // Streams with an active SO_STATISTICS query in this list, one bit each (D3D-302). Two active transform-feedback | ||
| 3409 | // queries on one stream break VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753 anywhere in the command buffer, | ||
| 3410 | // whatever their pools, so prepare refuses a second Begin on an active stream. | ||
| 3411 | std::uint32_t so_active = 0; | ||
| 3397 | out.wrote_readback = false; | 3412 | out.wrote_readback = false; |
| 3398 | out.readback_src_stages = 0; | 3413 | out.readback_src_stages = 0; |
| 3399 | 3414 | ||
| @@ -3942,7 +3957,23 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 3942 | overlaps |= begin && (ordinary_active || binary_active); | 3957 | overlaps |= begin && (ordinary_active || binary_active); |
| 3943 | if (begin) ++binary_active; else --binary_active; | 3958 | if (begin) ++binary_active; else --binary_active; |
| 3944 | } else if (is_so_statistics(range.type)) { | 3959 | } else if (is_so_statistics(range.type)) { |
| 3945 | // No Vulkan query runs for it (QueryPool::pool is null), so it overlaps nothing. | 3960 | // A real transform-feedback heap queries on a stream (D3D-302). Stream n runs only when the |
| 3961 | // heap's pool covers it; then the indexed commands are required. Without a pool (the feature is | ||
| 3962 | // off or has no transformFeedbackQueries) or past maxTransformFeedbackStreams it runs nothing | ||
| 3963 | // and resolves zero. Two overlapping queries on one stream cannot both be active in one command | ||
| 3964 | // buffer, even in two heaps (VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753), so a second Begin | ||
| 3965 | // is refused. | ||
| 3966 | const std::uint32_t stream = range.type - d3d12_query::so_statistics_stream0; | ||
| 3967 | if (heap->pool && stream < heap->streams) { | ||
| 3968 | if (!so_queries) return unsupported_at(__LINE__); | ||
| 3969 | const std::uint32_t bit = 1u << stream; // stream < heap->streams <= 4 | ||
| 3970 | if (begin) { | ||
| 3971 | if (so_active & bit) return ReplayResult::invalid_query; | ||
| 3972 | so_active |= bit; | ||
| 3973 | } else { | ||
| 3974 | so_active &= ~bit; | ||
| 3975 | } | ||
| 3976 | } | ||
| 3946 | } else { | 3977 | } else { |
| 3947 | statistics_overlaps |= begin && statistics_active_count; | 3978 | statistics_overlaps |= begin && statistics_active_count; |
| 3948 | if (begin) ++statistics_active_count; else --statistics_active_count; | 3979 | if (begin) ++statistics_active_count; else --statistics_active_count; |
| @@ -4256,12 +4287,16 @@ public: | |||
| 4256 | query.statistics_end != detail::PreparedQuery::no_segment; | 4287 | query.statistics_end != detail::PreparedQuery::no_segment; |
| 4257 | const std::uint32_t size = query_result_size(resolve.type); | 4288 | const std::uint32_t size = query_result_size(resolve.type); |
| 4258 | const auto offset = list.commands[query.command].binding.buffer_offset + resolve.offset; | 4289 | const auto offset = list.commands[query.command].binding.buffer_offset + resolve.offset; |
| 4290 | // A stream past the pool's maxTransformFeedbackStreams ran nothing (D3D-302): its resolve is zeros. | ||
| 4291 | const std::uint32_t so_stream = | ||
| 4292 | is_so_statistics(resolve.type) ? resolve.type - d3d12_query::so_statistics_stream0 : 0; | ||
| 4293 | const bool so_runs = !is_so_statistics(resolve.type) || so_stream < query.heap->streams; | ||
| 4259 | for (std::uint32_t i = 0; i < resolve.count; ++i) { | 4294 | for (std::uint32_t i = 0; i < resolve.count; ++i) { |
| 4260 | const auto index = resolve.first + i; | 4295 | const auto index = resolve.first + i; |
| 4261 | const auto value = lookup(*query.heap, index); | 4296 | const auto value = lookup(*query.heap, index); |
| 4262 | if (binary && value.ended && !uncounted) return unsupported_at(__LINE__); | 4297 | if (binary && value.ended && !uncounted) return unsupported_at(__LINE__); |
| 4263 | needs_binary |= binary && value.ended; | 4298 | needs_binary |= binary && value.ended; |
| 4264 | detail::QueryResolveRun run{value.ended ? query.heap->pool : VK_NULL_HANDLE, | 4299 | detail::QueryResolveRun run{value.ended && so_runs ? query.heap->pool : VK_NULL_HANDLE, |
| 4265 | index, 1, offset + VkDeviceSize(i) * size}; | 4300 | index, 1, offset + VkDeviceSize(i) * size}; |
| 4266 | if (value.occlusion.segments) { | 4301 | if (value.occlusion.segments) { |
| 4267 | if (resolve.type != d3d12_query::occlusion && !binary && !statistics) | 4302 | if (resolve.type != d3d12_query::occlusion && !binary && !statistics) |
| @@ -4385,17 +4420,34 @@ inline void emit_query(const PreparedList& list, const detail::PreparedQuery& qu | |||
| 4385 | begin_statistics(); | 4420 | begin_statistics(); |
| 4386 | return; | 4421 | return; |
| 4387 | } | 4422 | } |
| 4388 | // A heap without a Vulkan pool (stream-output statistics: the layer has no stream output) runs nothing for | 4423 | // A heap without a Vulkan pool (stream-output statistics on a device without VK_EXT_transform_feedback) runs |
| 4389 | // BeginQuery and EndQuery; its resolves were planned as zero fills. | 4424 | // nothing for BeginQuery and EndQuery; its resolves were planned as zero fills. |
| 4390 | if (!heap.pool && !std::holds_alternative<ResolveQueries>(command)) return; | 4425 | if (!heap.pool && !std::holds_alternative<ResolveQueries>(command)) return; |
| 4426 | // An SO_STATISTICS_STREAMn query begins/ends indexed on that stream (D3D-302): the query type's stream index is | ||
| 4427 | // the `index` argument of vkCmdBeginQueryIndexedEXT / vkCmdEndQueryIndexedEXT. A stream past the pool's | ||
| 4428 | // maxTransformFeedbackStreams runs nothing, like a heap without a pool: its resolves were planned as zero fills. | ||
| 4429 | const auto so_stream = [](std::uint32_t type) -> std::uint32_t { | ||
| 4430 | return is_so_statistics(type) ? type - d3d12_query::so_statistics_stream0 : 0; | ||
| 4431 | }; | ||
| 4432 | const auto so_runs = [&](std::uint32_t type) { | ||
| 4433 | return !is_so_statistics(type) || so_stream(type) < heap.streams; | ||
| 4434 | }; | ||
| 4391 | if (const auto* begin = std::get_if<BeginQuery>(&command)) { | 4435 | if (const auto* begin = std::get_if<BeginQuery>(&command)) { |
| 4436 | if (!so_runs(begin->type)) return; | ||
| 4392 | vk.reset_query_pool(cb, heap.pool, begin->index, 1); | 4437 | vk.reset_query_pool(cb, heap.pool, begin->index, 1); |
| 4393 | const bool precise = begin->type == d3d12_query::occlusion && heap.precise; | 4438 | if (is_so_statistics(begin->type)) { |
| 4394 | vk.begin_query(cb, heap.pool, begin->index, precise ? VK_QUERY_CONTROL_PRECISE_BIT : 0); | 4439 | vk.begin_query_indexed(cb, heap.pool, begin->index, so_stream(begin->type), 0); |
| 4440 | } else { | ||
| 4441 | const bool precise = begin->type == d3d12_query::occlusion && heap.precise; | ||
| 4442 | vk.begin_query(cb, heap.pool, begin->index, precise ? VK_QUERY_CONTROL_PRECISE_BIT : 0); | ||
| 4443 | } | ||
| 4395 | } else if (const auto* end = std::get_if<EndQuery>(&command)) { | 4444 | } else if (const auto* end = std::get_if<EndQuery>(&command)) { |
| 4445 | if (!so_runs(end->type)) return; | ||
| 4396 | if (end->type == d3d12_query::timestamp) { | 4446 | if (end->type == d3d12_query::timestamp) { |
| 4397 | vk.reset_query_pool(cb, heap.pool, end->index, 1); | 4447 | vk.reset_query_pool(cb, heap.pool, end->index, 1); |
| 4398 | vk.write_timestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, heap.pool, end->index); | 4448 | vk.write_timestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, heap.pool, end->index); |
| 4449 | } else if (is_so_statistics(end->type)) { | ||
| 4450 | vk.end_query_indexed(cb, heap.pool, end->index, so_stream(end->type)); | ||
| 4399 | } else { | 4451 | } else { |
| 4400 | vk.end_query(cb, heap.pool, end->index); | 4452 | vk.end_query(cb, heap.pool, end->index); |
| 4401 | } | 4453 | } |