diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3ae29163e5..eb8f117403 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3288,23 +3288,24 @@ Implemented (`QueryHeap` in `runtime/shims/d3d12_vulkan_device.cpp`, the query c recording. The resolve fallback is unchanged: only resolves needing dispatches are refused there. - **Mesh draws during a pipeline-statistics query** (D3D-291). D3D12 allows `DispatchMesh` inside a statistics query and reads its input-assembly, vertex, geometry and tessellation fields as 0. Vulkan forbids `vkCmdDrawMeshTasksEXT` - while a query with those counters is active (`VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076`), and the heap's - pool has all eleven. So a statistics query that holds a mesh draw is split as above, and a split before a draw stays - outside its rendering scope. A segment's first draw decides its pool: the submission's full pool, or a second pool - with only clipping invocations and primitives, fragment and compute invocations. A later draw of the other kind ends + while a query with the input-assembly, vertex, geometry, tessellation or clipping counters is active + (`VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076`), and the heap's pool has all eleven. So a statistics query + that holds a mesh draw is split as above, and a split before a draw stays outside its rendering scope. A segment's first draw decides its pool: the submission's full pool, or a second pool + with only the fragment and compute invocations, the two the VUID leaves allowed. A later draw of the other kind ends the segment and begins one of its own kind; dispatches and segments without draws use the full pool, and - `ExecuteIndirect` draws count as vertex draws. The sum copies a mesh segment's four counters into fields 5 to 8 of - its record, and `statistics_sum.comp` adds them as `CInvocations`, `CPrimitives`, `PSInvocations` and - `CSInvocations`, so the other fields get nothing from it. A query that holds only mesh draws is one mesh segment, - resolved through the same sum. Without the compute helper there is no segment pool and such a list is refused - (`unsupported`) rather than run unsplit; so is one on the single-list `replay()` (D3D-399), whose - `create_segment_pool` gives no mesh pool. Overlapping statistics queries (D3D-252) split the same way inside their - shared segments: a draw of the other kind than the open segment's opens a new one, so every query that segment - belongs to sums both kinds. `replay_query_predication_test` (cases 15 and 16) checks the split points, the segment - kinds and the resolve plan on the CPU. `d3d12_mesh_test` resolves a query around a vertex, two mesh and a vertex - draw, one around a mesh draw and one around the two vertex draws: the vertex fields count only the two triangles, - and the mixed query has more clipping and pixel-shader invocations than the vertex-only one. On the RX 9060 XT - (RADV), the clipping counts add up exactly (2 + 10 = 12). + `ExecuteIndirect` draws count as vertex draws. The sum copies a mesh segment's two counters into fields 7 and 8 of + its record, and `statistics_sum.comp` adds them as `PSInvocations` and `CSInvocations`, so the other fields get + nothing from it. D3D12 counts the clipping invocations and primitives of a mesh pipeline, but Vulkan has no counter + for them during a mesh draw, so `CInvocations` and `CPrimitives` read 0 for a mesh segment. A query that holds only + mesh draws is one mesh segment, resolved through the same sum. Without the compute helper there is no segment pool + and such a list is refused (`unsupported`) rather than run unsplit; so is one on the single-list `replay()` (D3D-399), + whose `create_segment_pool` gives no mesh pool. Overlapping statistics queries (D3D-252) split the same way inside + their shared segments: a draw of the other kind than the open segment's opens a new one, so every query that segment + belongs to sums both kinds. `replay_query_predication_test` (cases 15 to 17) checks the split points, the segment + kinds, the resolve plan and the counters the mesh pool may enable on the CPU. `d3d12_mesh_test` resolves a query + around a vertex, two mesh and a vertex draw, one around a mesh draw and one around the two vertex draws: the vertex + fields count only the two triangles, the mixed query has the same clipping count as the vertex-only one and more + pixel-shader invocations, and the mesh-only query has no clipping count. - **Timestamps.** `GetTimestampFrequency` is `1e9 / timestampPeriod` (1 GHz on the test GPU). Every D3D12 queue shares one `VkQueue` for now (question 12), so they share one frequency. - **Timestamp resolves on MoltenVK.** On Apple GPUs, MoltenVK delays every `vkCmdWriteTimestamp` to the end of diff --git a/runtime/src/statistics_sum.comp b/runtime/src/statistics_sum.comp index c4960347c3..2478030d42 100644 --- a/runtime/src/statistics_sum.comp +++ b/runtime/src/statistics_sum.comp @@ -2,9 +2,9 @@ // Adds the D3D12_QUERY_DATA_PIPELINE_STATISTICS segments of one split pipeline-statistics query (D3D-162): one // invocation per counter, in the order of VkQueryPipelineStatisticFlagBits, which is the D3D12 field order. -// `mesh` segments come from the pool a mesh draw can run in (D3D-291), which counts only clipping, fragment and -// compute: they are copied to counters 5..8 of their record, so CSInvocations sits at 8 and the input-assembly, -// vertex, geometry and tessellation counters (0..4, 8 and 9 of the D3D12 order) add nothing. +// `mesh` segments come from the pool a mesh draw can run in (D3D-291), which counts only the fragment and compute +// invocations: they are copied to counters 7 and 8 of their record, so PSInvocations takes the fragment one and +// CSInvocations the compute one, and every other counter adds nothing. layout(local_size_x = 11) in; layout(set = 0, binding = 0, std430) buffer QueryResults { uvec2 total[11]; @@ -15,7 +15,7 @@ layout(push_constant) uniform ResolveRange { uint count; uint mesh; }; void main() { const uint counter = gl_LocalInvocationID.x; uvec2 sum = total[counter]; - const bool counted = mesh == 0u || (counter >= 5u && counter <= 7u) || counter == 10u; + const bool counted = mesh == 0u || counter == 7u || counter == 10u; const uint source = mesh != 0u && counter == 10u ? 8u : counter; for (uint i = 0; counted && i < count; ++i) { uvec2 segment = segments[i * 11 + source]; diff --git a/runtime/src/statistics_sum_spirv.h b/runtime/src/statistics_sum_spirv.h index 9c67f04298..b482a57482 100644 --- a/runtime/src/statistics_sum_spirv.h +++ b/runtime/src/statistics_sum_spirv.h @@ -16,7 +16,7 @@ inline constexpr std::uint32_t statistics_sum_spirv[] = { 0x00050048, 0x00000016, 0x00000001, 0x00000023, 0x00000058, 0x00040047, 0x00000018, 0x00000021, 0x00000000, 0x00040047, 0x00000018, 0x00000022, 0x00000000, 0x00030047, 0x00000022, 0x00000002, 0x00050048, 0x00000022, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x00000022, 0x00000001, - 0x00000023, 0x00000004, 0x00040047, 0x00000072, 0x0000000b, 0x00000019, 0x00020013, 0x00000002, + 0x00000023, 0x00000004, 0x00040047, 0x0000006b, 0x0000000b, 0x00000019, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000006, 0x00000020, 0x00000000, 0x00040017, 0x00000009, 0x00000006, 0x00000003, 0x00040020, 0x0000000a, 0x00000001, 0x00000009, 0x0004003b, 0x0000000a, 0x0000000b, 0x00000001, 0x0004002b, 0x00000006, 0x0000000c, 0x00000000, 0x00040020, @@ -28,42 +28,38 @@ inline constexpr std::uint32_t statistics_sum_spirv[] = { 0x00000002, 0x00000010, 0x00020014, 0x0000001f, 0x0004001e, 0x00000022, 0x00000006, 0x00000006, 0x00040020, 0x00000023, 0x00000009, 0x00000022, 0x0004003b, 0x00000023, 0x00000024, 0x00000009, 0x0004002b, 0x00000019, 0x00000025, 0x00000001, 0x00040020, 0x00000026, 0x00000009, 0x00000006, - 0x0004002b, 0x00000006, 0x0000002e, 0x00000005, 0x0004002b, 0x00000006, 0x00000031, 0x00000007, - 0x0004002b, 0x00000006, 0x00000036, 0x0000000a, 0x0004002b, 0x00000006, 0x00000040, 0x00000008, - 0x0004002b, 0x00000006, 0x0000005e, 0x00000001, 0x0006002c, 0x00000009, 0x00000072, 0x00000013, - 0x0000005e, 0x0000005e, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, - 0x00000005, 0x00050041, 0x0000000d, 0x0000000e, 0x0000000b, 0x0000000c, 0x0004003d, 0x00000006, - 0x0000000f, 0x0000000e, 0x00060041, 0x0000001c, 0x0000001d, 0x00000018, 0x0000001a, 0x0000000f, - 0x0004003d, 0x00000010, 0x0000001e, 0x0000001d, 0x00050041, 0x00000026, 0x00000027, 0x00000024, - 0x00000025, 0x0004003d, 0x00000006, 0x00000028, 0x00000027, 0x000500aa, 0x0000001f, 0x00000029, - 0x00000028, 0x0000000c, 0x000400a8, 0x0000001f, 0x0000002a, 0x00000029, 0x000300f7, 0x0000002c, - 0x00000000, 0x000400fa, 0x0000002a, 0x0000002b, 0x0000002c, 0x000200f8, 0x0000002b, 0x000500ae, - 0x0000001f, 0x0000002f, 0x0000000f, 0x0000002e, 0x000500b2, 0x0000001f, 0x00000032, 0x0000000f, - 0x00000031, 0x000500a7, 0x0000001f, 0x00000033, 0x0000002f, 0x00000032, 0x000200f9, 0x0000002c, - 0x000200f8, 0x0000002c, 0x000700f5, 0x0000001f, 0x00000034, 0x00000029, 0x00000005, 0x00000033, - 0x0000002b, 0x000500aa, 0x0000001f, 0x00000037, 0x0000000f, 0x00000036, 0x000500a6, 0x0000001f, - 0x00000038, 0x00000034, 0x00000037, 0x000500ab, 0x0000001f, 0x0000003c, 0x00000028, 0x0000000c, - 0x000500a7, 0x0000001f, 0x0000003f, 0x0000003c, 0x00000037, 0x000600a9, 0x00000006, 0x00000042, - 0x0000003f, 0x00000040, 0x0000000f, 0x000200f9, 0x00000044, 0x000200f8, 0x00000044, 0x000700f5, - 0x00000010, 0x0000007f, 0x0000001e, 0x0000002c, 0x0000007c, 0x00000045, 0x000700f5, 0x00000006, - 0x0000007d, 0x0000000c, 0x0000002c, 0x0000006e, 0x00000045, 0x000400f6, 0x00000046, 0x00000045, - 0x00000000, 0x000200f9, 0x00000048, 0x000200f8, 0x00000048, 0x000300f7, 0x0000004b, 0x00000000, - 0x000400fa, 0x00000038, 0x0000004a, 0x0000004b, 0x000200f8, 0x0000004a, 0x00050041, 0x00000026, - 0x0000004d, 0x00000024, 0x0000001a, 0x0004003d, 0x00000006, 0x0000004e, 0x0000004d, 0x000500b0, - 0x0000001f, 0x0000004f, 0x0000007d, 0x0000004e, 0x000200f9, 0x0000004b, 0x000200f8, 0x0000004b, - 0x000700f5, 0x0000001f, 0x00000050, 0x00000038, 0x00000048, 0x0000004f, 0x0000004a, 0x000400fa, - 0x00000050, 0x00000045, 0x00000046, 0x000200f8, 0x00000045, 0x00050084, 0x00000006, 0x00000053, - 0x0000007d, 0x00000013, 0x00050080, 0x00000006, 0x00000055, 0x00000053, 0x00000042, 0x00060041, - 0x0000001c, 0x00000056, 0x00000018, 0x00000025, 0x00000055, 0x0004003d, 0x00000010, 0x00000057, - 0x00000056, 0x00050051, 0x00000006, 0x0000005a, 0x0000007f, 0x00000000, 0x00050051, 0x00000006, - 0x0000005c, 0x00000057, 0x00000000, 0x00050080, 0x00000006, 0x0000005d, 0x0000005a, 0x0000005c, - 0x00050051, 0x00000006, 0x00000060, 0x00000057, 0x00000001, 0x000500b0, 0x0000001f, 0x00000064, - 0x0000005d, 0x0000005a, 0x000600a9, 0x00000006, 0x00000065, 0x00000064, 0x0000005e, 0x0000000c, - 0x00050080, 0x00000006, 0x00000066, 0x00000060, 0x00000065, 0x00050051, 0x00000006, 0x00000068, - 0x0000007f, 0x00000001, 0x00050080, 0x00000006, 0x00000069, 0x00000068, 0x00000066, 0x00050050, - 0x00000010, 0x0000007c, 0x0000005d, 0x00000069, 0x00050080, 0x00000006, 0x0000006e, 0x0000007d, - 0x00000025, 0x000200f9, 0x00000044, 0x000200f8, 0x00000046, 0x0003003e, 0x0000001d, 0x0000007f, - 0x000100fd, 0x00010038, + 0x0004002b, 0x00000006, 0x0000002b, 0x00000007, 0x0004002b, 0x00000006, 0x0000002f, 0x0000000a, + 0x0004002b, 0x00000006, 0x00000039, 0x00000008, 0x0004002b, 0x00000006, 0x00000057, 0x00000001, + 0x0006002c, 0x00000009, 0x0000006b, 0x00000013, 0x00000057, 0x00000057, 0x00050036, 0x00000002, + 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x00050041, 0x0000000d, 0x0000000e, + 0x0000000b, 0x0000000c, 0x0004003d, 0x00000006, 0x0000000f, 0x0000000e, 0x00060041, 0x0000001c, + 0x0000001d, 0x00000018, 0x0000001a, 0x0000000f, 0x0004003d, 0x00000010, 0x0000001e, 0x0000001d, + 0x00050041, 0x00000026, 0x00000027, 0x00000024, 0x00000025, 0x0004003d, 0x00000006, 0x00000028, + 0x00000027, 0x000500aa, 0x0000001f, 0x00000029, 0x00000028, 0x0000000c, 0x000500aa, 0x0000001f, + 0x0000002c, 0x0000000f, 0x0000002b, 0x000500a6, 0x0000001f, 0x0000002d, 0x00000029, 0x0000002c, + 0x000500aa, 0x0000001f, 0x00000030, 0x0000000f, 0x0000002f, 0x000500a6, 0x0000001f, 0x00000031, + 0x0000002d, 0x00000030, 0x000500ab, 0x0000001f, 0x00000035, 0x00000028, 0x0000000c, 0x000500a7, + 0x0000001f, 0x00000038, 0x00000035, 0x00000030, 0x000600a9, 0x00000006, 0x0000003b, 0x00000038, + 0x00000039, 0x0000000f, 0x000200f9, 0x0000003d, 0x000200f8, 0x0000003d, 0x000700f5, 0x00000010, + 0x0000007e, 0x0000001e, 0x00000005, 0x0000007f, 0x0000003e, 0x000700f5, 0x00000006, 0x0000006d, + 0x0000000c, 0x00000005, 0x00000067, 0x0000003e, 0x000400f6, 0x0000003f, 0x0000003e, 0x00000000, + 0x000200f9, 0x00000041, 0x000200f8, 0x00000041, 0x000300f7, 0x00000044, 0x00000000, 0x000400fa, + 0x00000031, 0x00000043, 0x00000044, 0x000200f8, 0x00000043, 0x00050041, 0x00000026, 0x00000046, + 0x00000024, 0x0000001a, 0x0004003d, 0x00000006, 0x00000047, 0x00000046, 0x000500b0, 0x0000001f, + 0x00000048, 0x0000006d, 0x00000047, 0x000200f9, 0x00000044, 0x000200f8, 0x00000044, 0x000700f5, + 0x0000001f, 0x00000049, 0x00000031, 0x00000041, 0x00000048, 0x00000043, 0x000400fa, 0x00000049, + 0x0000003e, 0x0000003f, 0x000200f8, 0x0000003e, 0x00050084, 0x00000006, 0x0000004c, 0x0000006d, + 0x00000013, 0x00050080, 0x00000006, 0x0000004e, 0x0000004c, 0x0000003b, 0x00060041, 0x0000001c, + 0x0000004f, 0x00000018, 0x00000025, 0x0000004e, 0x0004003d, 0x00000010, 0x00000050, 0x0000004f, + 0x00050051, 0x00000006, 0x00000053, 0x0000007e, 0x00000000, 0x00050051, 0x00000006, 0x00000055, + 0x00000050, 0x00000000, 0x00050080, 0x00000006, 0x00000056, 0x00000053, 0x00000055, 0x00050051, + 0x00000006, 0x00000059, 0x00000050, 0x00000001, 0x000500b0, 0x0000001f, 0x0000005d, 0x00000056, + 0x00000053, 0x000600a9, 0x00000006, 0x0000005e, 0x0000005d, 0x00000057, 0x0000000c, 0x00050080, + 0x00000006, 0x0000005f, 0x00000059, 0x0000005e, 0x00050051, 0x00000006, 0x00000061, 0x0000007e, + 0x00000001, 0x00050080, 0x00000006, 0x00000062, 0x00000061, 0x0000005f, 0x00050050, 0x00000010, + 0x0000007f, 0x00000056, 0x00000062, 0x00050080, 0x00000006, 0x00000067, 0x0000006d, 0x00000025, + 0x000200f9, 0x0000003d, 0x000200f8, 0x0000003f, 0x0003003e, 0x0000001d, 0x0000007e, 0x000100fd, + 0x00010038, }; } // namespace leonida diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e3e4defe89..89d0f00ddd 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -244,12 +244,12 @@ class OcclusionResolve { // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). - // Segment i is in `mesh_queries` when mesh[i] is set (four counters: clipping, fragment and compute; see + // Segment i is in `mesh_queries` when mesh[i] is set (two counters: fragment and compute; see // statistics_sum.comp), otherwise in `queries` (all eleven). void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { constexpr VkDeviceSize result = statistics_counters * word_size; - constexpr VkDeviceSize mesh_slot = 5 * word_size; // CInvocations, where the mesh pool's first counter goes + constexpr VkDeviceSize mesh_slot = 7 * word_size; // PSInvocations, where the mesh pool's first counter goes begin_scratch_write(cb); vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); bind(cb, statistics_pipeline_); @@ -2443,15 +2443,12 @@ class VulkanQueue : private QueueHolds::Holder { if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { statistics->mesh.clear(); } else { - // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping - // invocations and primitives, fragment and compute invocations (D3D-291). + // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): the + // fragment and compute invocations, and no other (D3D-291). VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; info.queryCount = statistics_segments; - info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | - VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | - VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | - VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; + info.pipelineStatistics = kMeshStatisticsFlags; if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != VK_SUCCESS) return ReplayResult::unsupported; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 889bb71f72..b1f6fab382 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -2344,10 +2344,26 @@ inline ReplayResult plan_occlusion_segments(PreparedList& out) { // Helper dispatches must not contribute to the application's CSInvocations (D3D-162). Query resolves and // ordinary helper commands share one sequence of physical statistics segments. Resolve dispatch requirements // depend on heap state at planning time, so every non-timestamp resolve splits conservatively. -// A mesh draw cannot run while a query with the input-assembly, vertex, geometry or tessellation counters is active -// (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076), though D3D12 allows it and reads those fields as 0. A query -// that holds a mesh draw is split as well (D3D-291): a segment's first draw decides whether it runs in the full pool -// or in the mesh one (statistics_mesh), and a later draw of the other kind ends it and begins one of its own kind. +// The counters a mesh draw may count: VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 leaves the input +// assembly, vertex, geometry, tessellation and clipping bits out of the pipelineStatistics flags of every pool that +// is active during a mesh draw, so a mesh segment runs in a pool with the fragment and compute counters only +// (D3D-291). D3D12 counts the clipping invocations and primitives of a mesh pipeline, but Vulkan has no counter for +// them during a mesh draw, so those two fields read 0. +inline constexpr VkQueryPipelineStatisticFlags kMeshStatisticsFlags = + VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; +// The bits the same VUID forbids in a pool that is active during a mesh draw. +inline constexpr VkQueryPipelineStatisticFlags kForbiddenMeshStatisticsFlags = + VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT | VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT | + VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT | VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT | + VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | + VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | + VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT | + VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT; +// A mesh draw cannot run while a query with the input-assembly, vertex, geometry, tessellation or clipping counters +// is active (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076), though D3D12 allows it and reads those fields as +// 0. A query that holds a mesh draw is split as well (D3D-291): a segment's first draw decides whether it runs in +// the full pool or in the mesh one (statistics_mesh), and a later draw of the other kind ends it and begins one of +// its own kind. // Dispatches and segments without draws use the full pool; ExecuteIndirect draws count as vertex draws. Each draw // has its own rendering scope, so a split before one stays outside rendering, as a helper's does. inline void plan_statistics_segments(PreparedList& out) { diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index df430c11a6..ef854a43ef 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -405,8 +405,11 @@ int main() { // query 2 the two vertex draws alone. A mesh draw has no input assembly or vertex shader, so those fields // count only the two full-screen triangles (3 vertices, 1 primitive each). How many clipping and pixel // shader invocations a draw makes is the driver's (RADV's PSInvocations is not the pixel count), so query - // 0 is checked against query 2: more of both is the mesh segment's share. Under LEONIDA_VK_VALIDATION=1 the - // run must stay free of VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076. + // 0 is checked against query 2: more pixel shader invocations is the mesh segment's share. A mesh segment + // runs in a pool that counts the fragment and compute invocations only, because + // VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 leaves the clipping counters out of a pool that is + // active during a mesh draw; D3D12 counts the clipping invocations and primitives of a mesh pipeline, so + // those two fields read 0 here. Under LEONIDA_VK_VALIDATION=1 the run must stay free of the VUID. if (d.pipeline_statistics_query) { const D3D12_QUERY_HEAP_DESC statistics_desc{D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS, 3, 0}; Ref statistics; @@ -472,11 +475,11 @@ int main() { EXPECT(vertex_only.IAVertices == 6 && vertex_only.IAPrimitives == 2); EXPECT(mixed.GSInvocations == 0 && mixed.GSPrimitives == 0 && mixed.HSInvocations == 0 && mixed.DSInvocations == 0 && mixed.CSInvocations == 0); - EXPECT(mixed.CInvocations > vertex_only.CInvocations && mixed.PSInvocations > vertex_only.PSInvocations); + EXPECT(mixed.CInvocations == vertex_only.CInvocations && mixed.PSInvocations > vertex_only.PSInvocations); EXPECT(mesh_only.IAVertices == 0 && mesh_only.IAPrimitives == 0 && mesh_only.VSInvocations == 0 && mesh_only.GSInvocations == 0 && mesh_only.GSPrimitives == 0 && mesh_only.HSInvocations == 0 && mesh_only.DSInvocations == 0 && mesh_only.CSInvocations == 0); - EXPECT(mesh_only.CInvocations > 0 && mesh_only.PSInvocations > 0); + EXPECT(mesh_only.CInvocations == 0 && mesh_only.CPrimitives == 0 && mesh_only.PSInvocations > 0); } else { std::printf("pipelineStatisticsQuery not available: the D3D-291 statistics check is skipped\n"); } diff --git a/runtime/test/replay_query_edges_test.cpp b/runtime/test/replay_query_edges_test.cpp index 820dc0dc19..03b8465eac 100644 --- a/runtime/test/replay_query_edges_test.cpp +++ b/runtime/test/replay_query_edges_test.cpp @@ -1,6 +1,7 @@ -// Query heap boundaries, resolve helper chunking, arena offsets, query reuse across lists/submissions, and -// predication around copies. These edge cases are only reached by GPU tests today; this CPU mock lets CI -// without a GPU check replay planning, emitted commands, destination guards, and pool ownership on submit failure. +// Query heap boundaries, resolve helper chunking, arena offsets, query reuse across lists/submissions, the mesh +// pool of a statistics query and predication around copies. These edge cases are only reached by GPU tests today; +// this CPU mock lets CI without a GPU check replay planning, emitted commands, destination guards, and pool ownership +// on submit failure. // Shader arithmetic and Vulkan entry points are mocked; no driver is loaded. No GPU or game data. #include "../src/vulkan_backend.h" @@ -61,7 +62,9 @@ struct MockReplay { std::vector copies; std::vector> resets; std::set statistics_pools; + std::set mesh_pools; // pools with the two counters a mesh draw allows (D3D-291) std::vector chunks; + std::vector meshes; // statistics_sum.comp's mesh flag, one per chunk std::vector groups; std::vector transfers; std::vector events; @@ -111,8 +114,10 @@ struct MockReplay { copies.push_back({p, first, count, off, stride}); auto& q = values.at(p); expect(first <= q.size() && count <= q.size() - first, "mock query access out of bounds"); + // A mesh pool counts two counters, not the eleven of the full pool (D3D-291). + const unsigned counters = mesh_pools.count(p) ? 2u : (stride == statistics_size ? statistics_counters : 1u); for (unsigned i = 0; i < count; ++i) { - for (unsigned j = 0; j < (stride == statistics_size ? statistics_counters : 1u); ++j) { + for (unsigned j = 0; j < counters; ++j) { word(b, off + i * stride + word_size * j, q[first + i][j]); } } @@ -286,26 +291,32 @@ void test_helper_chunks() { const VkDescriptorSet*, unsigned, const unsigned*) {}; vk.vkCmdPushConstants = +[](VkCommandBuffer, VkPipelineLayout, VkShaderStageFlags, unsigned off, unsigned size, const void*p) { - // The count, then statistics_sum.comp's mesh flag (D3D-291), which no segment here sets. + // The count, then statistics_sum.comp's mesh flag (D3D-291). expect(off == 0 && size == 2 * sizeof(unsigned), "chunk push constants"); - expect(static_cast(p)[1] == 0, "chunk push constants: no mesh segment"); - MockReplay::current->chunks.push_back(*static_cast(p)); + MockReplay::current->chunks.push_back(static_cast(p)[0]); + MockReplay::current->meshes.push_back(static_cast(p)[1]); }; vk.vkCmdDispatch = +[](VkCommandBuffer, unsigned x, unsigned y, unsigned z) { auto& c = *MockReplay::current; c.groups.push_back(x); expect(y == 1 && z == 1, "dispatch dimensions"); auto n = c.chunks.back(); + const bool mesh = c.meshes.back() != 0; if (c.mode == MockReplay::binary) { for (unsigned i = 0; i < n; ++i) { c.word(VK_NULL_HANDLE, word_size * i, c.word(VK_NULL_HANDLE, word_size * i) != 0); } } else { - for (unsigned j = 0; j < (c.mode == MockReplay::statistics ? statistics_counters : 1u); ++j) { - unsigned width = c.mode == MockReplay::statistics ? statistics_counters : 1; + const bool statistics = c.mode == MockReplay::statistics; + for (unsigned j = 0; j < (statistics ? statistics_counters : 1u); ++j) { + // A mesh segment counts the fragment and compute invocations only, and they land in PSInvocations + // and CSInvocations (statistics_sum.comp, D3D-291). + if (mesh && statistics && j != 7 && j != 10) continue; + const unsigned source = mesh && j == 10 ? 8 : j; + unsigned width = statistics ? statistics_counters : 1; auto total = c.word(VK_NULL_HANDLE, word_size * j); for (unsigned i = 0; i < n; ++i) { - total += c.word(VK_NULL_HANDLE, word_size * (width * (i + 1) + j)); + total += c.word(VK_NULL_HANDLE, word_size * (width * (i + 1) + source)); } c.word(VK_NULL_HANDLE, word_size * j, total); } @@ -338,6 +349,7 @@ void test_helper_chunks() { auto expected = c.buffers[dst]; c.copies.clear(); c.chunks.clear(); + c.meshes.clear(); c.groups.clear(); c.transfers.clear(); if (mode == MockReplay::binary) { @@ -363,6 +375,8 @@ void test_helper_chunks() { expect(c.buffers[dst] == expected, "numeric helper result and destination guard bytes"); expect(c.chunks == (extra ? std::vector{capacity, 1} : std::vector{capacity}), "exact chunk sizes"); + expect(std::all_of(c.meshes.begin(), c.meshes.end(), [](unsigned m) { return m == 0; }), + "no mesh segment in these chunks"); expect(c.copies.size() == 1 + extra && c.copies[0].first == first && (!extra || c.copies[1].first == first + capacity), "chunk query indices end exactly at heap boundary"); std::vector expected_groups(1 + extra, 1); @@ -392,6 +406,43 @@ void test_helper_chunks() { for (unsigned j = 0; j < statistics_counters; ++j) { expect(c.word(dst, output_offset + word_size * j) == reset_counter, "statistics accumulator reset"); } + + // A statistics query that holds a mesh draw is split so the mesh segments run in the mesh pool (D3D-291). That + // pool counts the fragment and compute invocations only, so the sum copies them into the PSInvocations and + // CSInvocations fields of their record, and the clipping fields a mesh draw cannot count stay 0. + { + constexpr unsigned mesh_segments = 3; + constexpr std::uint64_t fragment = 40; + constexpr std::uint64_t compute = 7; + const std::uint8_t mesh_flags[mesh_segments] = {1, 1, 1}; + auto mesh_pool = handle(3); + c.mesh_pools.insert(mesh_pool); + c.values[mesh_pool].resize(mesh_segments); + std::uint64_t fragment_total = 0, compute_total = 0; + for (unsigned i = 0; i < mesh_segments; ++i) { + c.values[mesh_pool][i][0] = fragment + i; // the fragment shader invocations + c.values[mesh_pool][i][1] = compute + i; // the compute shader invocations + fragment_total += c.values[mesh_pool][i][0]; + compute_total += c.values[mesh_pool][i][1]; + } + c.buffers[dst].assign(buffer_bytes, guard_byte); + auto expected = c.buffers[dst]; + c.copies.clear(); + c.chunks.clear(); + c.meshes.clear(); + c.groups.clear(); + c.transfers.clear(); + helper.sum_statistics({}, qp, mesh_pool, mesh_flags, 0, mesh_segments, dst, output_offset); + for (unsigned j = 0; j < statistics_counters; ++j) { + const std::uint64_t v = j == 7 ? fragment_total : j == 10 ? compute_total : 0; + std::memcpy(expected.data() + output_offset + word_size * j, &v, word_size); + } + expect(c.buffers[dst] == expected, + "a mesh segment fills PSInvocations and CSInvocations, and no other field"); + expect(c.meshes.size() == 1 && c.meshes[0] == 1, "the mesh chunk says its segments are mesh ones"); + expect(c.copies.size() == 1 && c.copies[0].pool == mesh_pool, + "the mesh segment is read from the mesh pool"); + } } void test_query_state() { diff --git a/runtime/test/replay_query_predication_test.cpp b/runtime/test/replay_query_predication_test.cpp index 3ef0b484ab..e15ded58fd 100644 --- a/runtime/test/replay_query_predication_test.cpp +++ b/runtime/test/replay_query_predication_test.cpp @@ -755,6 +755,21 @@ int main() { expect(q[3].statistics_first == 1 && q[3].statistics_count == 3, "overlap-mesh: query 1 sums segments 1-3"); } + // 17. D3D-291: the pool a mesh draw runs in must not enable a counter + // VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 forbids, or the validation layer reports the VUID on + // every DispatchMesh inside a statistics query. The VUID leaves the fragment and compute invocations + // allowed, and those are the two the mesh pool counts; the nine it names are the rest of the eleven. + { + constexpr VkQueryPipelineStatisticFlags all_counters = 0x7ff; // the counters of a D3D12 statistics heap + expect((kMeshStatisticsFlags & kForbiddenMeshStatisticsFlags) == 0, + "the mesh pool enables none of the counters the VUID forbids"); + expect((all_counters & ~kForbiddenMeshStatisticsFlags) == kMeshStatisticsFlags, + "the mesh pool counts every counter the VUID leaves allowed"); + expect(kMeshStatisticsFlags == (VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | + VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT), + "the mesh pool counts the fragment and compute invocations"); + } + if (failures) return 1; std::puts("replay_query_predication_test: query resolves suspend conditional rendering around fills, copies and dispatches"); return 0;