diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3ae29163e5..8ffc52693b 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3288,23 +3288,34 @@ 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 without the mesh-primitives query below. +- **The clipping invocations of a mesh segment** (D3D-MESH-CLIPPING-COUNTER). D3D-291 leaves `CInvocations` and + `CPrimitives` of a mesh segment at 0, because Vulkan has no clipping counter during a mesh draw. Where the device + has the `meshShaderQueries` feature of `VK_EXT_mesh_shader`, the number of primitives the mesh shader emitted + comes from a `VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT` query instead: a third pool of the submission, begun + and ended with each mesh segment, next to the two the statistics split needs. Its count is copied to field 5 of the + segment's record and `statistics_sum.comp` adds it as `CInvocations`; `CPrimitives` stays 0, because Vulkan has + no clipped-primitive counter and D3D12's `CPrimitives` is the clipper's output (0 when a triangle is dropped, 2 + when it is split). The count is not D3D12's number: Vulkan counts a primitive the mesh shader culled with + `gl_CullPrimitiveEXT`, which D3D12 does not count, so a mesh segment's `CInvocations` can be higher than D3D12's. + Without the feature the field reads 0, as in D3D-291. - **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/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index d0f3b528aa..c0ff3cf8da 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -175,6 +175,9 @@ struct VulkanDeviceInfo { // shaders have no amplification stage). Mesh-shader PSOs and DispatchMesh work, and CheckFeatureSupport reports // D3D12_MESH_SHADER_TIER_1; without it mesh PSOs are refused and the tier is NOT_SUPPORTED. bool mesh_shader = false; + // VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so the clipping invocations of a mesh + // segment come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Without it they read 0. + bool mesh_shader_queries = false; // The core features enabled on `device`. Graphics PSOs that need independentBlend, fillModeNonSolid, // depthBiasClamp or dualSrcBlend are refused unless it is set here. VkPhysicalDeviceFeatures features{}; diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 0273f9339b..1b7466630f 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5120,6 +5120,7 @@ class Device final info.conditional_rendering, info.ray_tracing, info.xdxr_acceleration_structures && info.buffer_device_address); + state_.context->mesh_queries = info.mesh_shader_queries; if (info.physical && state_.format_properties) { state_.context->set_color_attachment_format( [physical = info.physical, get_props = state_.format_properties](VkFormat format) { @@ -9490,6 +9491,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &mesh_features}; vkGetPhysicalDeviceFeatures2(info.physical, &features); info.mesh_shader = mesh_features.meshShader == VK_TRUE; + info.mesh_shader_queries = mesh_features.meshShaderQueries == VK_TRUE; } } if (!info.mesh_shader) @@ -9526,6 +9528,8 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin // are not used. VkPhysicalDeviceMeshShaderFeaturesEXT enable_mesh{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT}; enable_mesh.meshShader = VK_TRUE; + // The mesh-primitives query, which counts the primitives a mesh shader emits (D3D-MESH-CLIPPING-COUNTER). + enable_mesh.meshShaderQueries = info.mesh_shader_queries ? VK_TRUE : VK_FALSE; void* ext_chain = nullptr; if (info.mesh_shader) { diff --git a/runtime/src/statistics_sum.comp b/runtime/src/statistics_sum.comp index c4960347c3..c4a02a459b 100644 --- a/runtime/src/statistics_sum.comp +++ b/runtime/src/statistics_sum.comp @@ -2,9 +2,11 @@ // 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. +// The clipping invocations of a mesh segment come from the mesh-primitives count of that segment, copied to +// counter 5 (D3D-MESH-CLIPPING-COUNTER); without that count the field stays 0. layout(local_size_x = 11) in; layout(set = 0, binding = 0, std430) buffer QueryResults { uvec2 total[11]; @@ -15,7 +17,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 == 5u || 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..a147cd4dfd 100644 --- a/runtime/src/statistics_sum_spirv.h +++ b/runtime/src/statistics_sum_spirv.h @@ -7,7 +7,7 @@ namespace leonida { // From statistics_sum.comp. Compile with glslangValidator -V --target-env vulkan1.0 -Os -g0, // then encode the little-endian SPIR-V as uint32_t words. No shader compiler is needed at runtime build time. inline constexpr std::uint32_t statistics_sum_spirv[] = { - 0x07230203, 0x00010000, 0x0008000b, 0x00000080, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, + 0x07230203, 0x00010000, 0x0008000b, 0x00000084, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x00060010, 0x00000004, 0x00000011, 0x0000000b, 0x00000001, 0x00000001, 0x00040047, 0x0000000b, 0x0000000b, 0x0000001b, @@ -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, 0x0000006f, 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,39 @@ 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, + 0x0004002b, 0x00000006, 0x0000002b, 0x00000005, 0x0004002b, 0x00000006, 0x0000002f, 0x00000007, + 0x0004002b, 0x00000006, 0x00000033, 0x0000000a, 0x0004002b, 0x00000006, 0x0000003d, 0x00000008, + 0x0004002b, 0x00000006, 0x0000005b, 0x00000001, 0x0006002c, 0x00000009, 0x0000006f, 0x00000013, + 0x0000005b, 0x0000005b, 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, + 0x00000028, 0x0000000c, 0x000500aa, 0x0000001f, 0x0000002c, 0x0000000f, 0x0000002b, 0x000500a6, + 0x0000001f, 0x0000002d, 0x00000029, 0x0000002c, 0x000500aa, 0x0000001f, 0x00000030, 0x0000000f, + 0x0000002f, 0x000500a6, 0x0000001f, 0x00000031, 0x0000002d, 0x00000030, 0x000500aa, 0x0000001f, + 0x00000034, 0x0000000f, 0x00000033, 0x000500a6, 0x0000001f, 0x00000035, 0x00000031, 0x00000034, + 0x000500ab, 0x0000001f, 0x00000039, 0x00000028, 0x0000000c, 0x000500a7, 0x0000001f, 0x0000003c, + 0x00000039, 0x00000034, 0x000600a9, 0x00000006, 0x0000003f, 0x0000003c, 0x0000003d, 0x0000000f, + 0x000200f9, 0x00000041, 0x000200f8, 0x00000041, 0x000700f5, 0x00000010, 0x00000082, 0x0000001e, + 0x00000005, 0x00000083, 0x00000042, 0x000700f5, 0x00000006, 0x00000071, 0x0000000c, 0x00000005, + 0x0000006b, 0x00000042, 0x000400f6, 0x00000043, 0x00000042, 0x00000000, 0x000200f9, 0x00000045, + 0x000200f8, 0x00000045, 0x000300f7, 0x00000048, 0x00000000, 0x000400fa, 0x00000035, 0x00000047, + 0x00000048, 0x000200f8, 0x00000047, 0x00050041, 0x00000026, 0x0000004a, 0x00000024, 0x0000001a, + 0x0004003d, 0x00000006, 0x0000004b, 0x0000004a, 0x000500b0, 0x0000001f, 0x0000004c, 0x00000071, + 0x0000004b, 0x000200f9, 0x00000048, 0x000200f8, 0x00000048, 0x000700f5, 0x0000001f, 0x0000004d, + 0x00000035, 0x00000045, 0x0000004c, 0x00000047, 0x000400fa, 0x0000004d, 0x00000042, 0x00000043, + 0x000200f8, 0x00000042, 0x00050084, 0x00000006, 0x00000050, 0x00000071, 0x00000013, 0x00050080, + 0x00000006, 0x00000052, 0x00000050, 0x0000003f, 0x00060041, 0x0000001c, 0x00000053, 0x00000018, + 0x00000025, 0x00000052, 0x0004003d, 0x00000010, 0x00000054, 0x00000053, 0x00050051, 0x00000006, + 0x00000057, 0x00000082, 0x00000000, 0x00050051, 0x00000006, 0x00000059, 0x00000054, 0x00000000, + 0x00050080, 0x00000006, 0x0000005a, 0x00000057, 0x00000059, 0x00050051, 0x00000006, 0x0000005d, + 0x00000054, 0x00000001, 0x000500b0, 0x0000001f, 0x00000061, 0x0000005a, 0x00000057, 0x000600a9, + 0x00000006, 0x00000062, 0x00000061, 0x0000005b, 0x0000000c, 0x00050080, 0x00000006, 0x00000063, + 0x0000005d, 0x00000062, 0x00050051, 0x00000006, 0x00000065, 0x00000082, 0x00000001, 0x00050080, + 0x00000006, 0x00000066, 0x00000065, 0x00000063, 0x00050050, 0x00000010, 0x00000083, 0x0000005a, + 0x00000066, 0x00050080, 0x00000006, 0x0000006b, 0x00000071, 0x00000025, 0x000200f9, 0x00000041, + 0x000200f8, 0x00000043, 0x0003003e, 0x0000001d, 0x00000082, 0x000100fd, 0x00010038, }; } // namespace leonida diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e3e4defe89..e323ab22cf 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -244,12 +244,16 @@ 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 - // 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 { + // 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). A mesh segment's clipping invocations come from + // `primitives_queries`, the mesh-primitives count of that segment (D3D-MESH-CLIPPING-COUNTER); it is null when + // the device has no meshShaderQueries, and the field then reads 0. + void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, + VkQueryPool primitives_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 + constexpr VkDeviceSize clipping_slot = 5 * word_size; // CInvocations, where the mesh-primitives count goes begin_scratch_write(cb); vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); bind(cb, statistics_pipeline_); @@ -258,9 +262,18 @@ class OcclusionResolve { std::uint32_t chunk = 1; while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) ++chunk; + if (in_mesh) { + // The mesh pool holds only the fragment and compute counters, so the clipping field of a mesh + // segment reads 0 unless the mesh-primitives count fills it (D3D-MESH-CLIPPING-COUNTER). + vk_.vkCmdFillBuffer(cb, scratch_, result, chunk * result, 0); + barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); + } vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, result + (in_mesh ? mesh_slot : 0), result, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); + if (in_mesh && primitives_queries) + vk_.vkCmdCopyQueryPoolResults(cb, primitives_queries, first, chunk, scratch_, result + clipping_slot, + result, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, shader_access); dispatch(cb, chunk, 1, in_mesh); @@ -923,6 +936,10 @@ struct TileRange { #endif class VulkanContext { public: + // mesh_queries: VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so a mesh segment's + // clipping invocations can come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Set by the device + // that created the context. + bool mesh_queries = false; // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. @@ -2394,9 +2411,11 @@ class VulkanQueue : private QueueHolds::Holder { occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); }; replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, - const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, - VkBuffer dst, VkDeviceSize offset) { - occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); + VkQueryPool primitives_pool, const std::uint8_t* mesh, + std::uint32_t first, std::uint32_t count, VkBuffer dst, + VkDeviceSize offset) { + occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, primitives_pool, mesh, first, count, dst, + offset); }; replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { @@ -2443,18 +2462,26 @@ 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; + // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type, + // which the pipeline-statistics pool cannot hold. The device needs the meshShaderQueries feature, + // or the clipping field of a mesh segment stays 0. + if (context_.mesh_queries) { + VkQueryPoolCreateInfo primitives{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; + primitives.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT; + primitives.queryCount = statistics_segments; + if (context_.vk.vkCreateQueryPool(statistics->device, &primitives, nullptr, + &statistics->primitives_pool) != VK_SUCCESS) + return ReplayResult::unsupported; + } } } // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 889bb71f72..bda530a7a6 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -716,6 +716,9 @@ struct OcclusionSegmentPool { // segment index whether it runs there. Empty when no segment of the submission holds a mesh draw. VkQueryPool mesh_pool = VK_NULL_HANDLE; std::vector mesh; + // The mesh-primitives pool of the mesh segments (D3D-MESH-CLIPPING-COUNTER): one query per segment, begun and + // ended with the mesh segment. Null when the device has no meshShaderQueries. + VkQueryPool primitives_pool = VK_NULL_HANDLE; OcclusionSegmentPool() = default; OcclusionSegmentPool(const OcclusionSegmentPool&) = delete; @@ -723,6 +726,7 @@ struct OcclusionSegmentPool { ~OcclusionSegmentPool() { if (pool) destroy(device, pool, nullptr); if (mesh_pool) destroy(device, mesh_pool, nullptr); + if (primitives_pool) destroy(device, primitives_pool, nullptr); } }; @@ -875,9 +879,12 @@ struct ReplayFunctions { VkBuffer dst, VkDeviceSize offset)> resolve_binary_occlusion; // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS, outside conditional // rendering: the segments of a statistics query split around resolves (D3D-162) and mesh draws (D3D-291). - // Segment i is in the second pool when mesh[i] is set (mesh may be null: none is). - std::function + // Segment i is in the second pool when mesh[i] is set (mesh may be null: none is). The third pool holds the + // mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER); it is null when the device has no + // meshShaderQueries. + std::function sum_statistics; // Sums `count` binary-occlusion segments and normalizes the total to a 64-bit 0 or 1, outside conditional // rendering: a binary query split over physical segments because it overlaps other occlusion queries (D3D-252). @@ -958,6 +965,8 @@ struct QueryResolveRun { bool sum = false; // Statistics segments: the mesh-compatible pool and the kind of each segment (OcclusionSegmentPool::mesh). VkQueryPool mesh_pool = VK_NULL_HANDLE; + // The mesh-primitives pool of the mesh segments (D3D-MESH-CLIPPING-COUNTER); null without meshShaderQueries. + VkQueryPool primitives_pool = VK_NULL_HANDLE; const std::uint8_t* mesh = nullptr; }; @@ -2300,6 +2309,8 @@ class PreparedList { // For each of those segments, whether it holds mesh draws and so runs in statistics_mesh_pool (D3D-291). std::vector statistics_mesh; VkQueryPool statistics_mesh_pool = VK_NULL_HANDLE; + // The mesh-primitives pool of the list's mesh segments (D3D-MESH-CLIPPING-COUNTER). + VkQueryPool statistics_primitives_pool = VK_NULL_HANDLE; // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it. @@ -2344,10 +2355,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) { @@ -3191,6 +3218,7 @@ class QueryReplay { retain(statistics_pool); list.statistics_pool = statistics_pool->pool; list.statistics_mesh_pool = statistics_pool->mesh_pool; + list.statistics_primitives_pool = statistics_pool->primitives_pool; list.statistics_base = statistics_base; } else if (mesh) { // Unsplit, the query would be active around a mesh draw with counters Vulkan forbids there. @@ -3247,7 +3275,8 @@ class QueryReplay { const bool any_mesh = kinds && std::find(kinds + saved.first, kinds + saved.first + saved.count, std::uint8_t(1)) != kinds + saved.first + saved.count; run = {saved.segments->pool, saved.first, saved.count, run.offset, - saved.count > 1 || any_mesh, saved.segments->mesh_pool, kinds}; + saved.count > 1 || any_mesh, saved.segments->mesh_pool, + saved.segments->primitives_pool, kinds}; if (any_mesh && !statistics) return ReplayResult::invalid_query; if (!can_sum || (run.sum && !uncounted)) return ReplayResult::unsupported; if (statistics) needs_statistics_sum |= run.sum; @@ -3314,6 +3343,9 @@ inline void end_statistics_segment(const PreparedList& list, std::uint32_t segme const ReplayFunctions& vk) { if (segment == PreparedQuery::no_segment) return; vk.end_query(cb, statistics_segment_pool(list, segment), list.statistics_base + segment); + // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER). + if (list.statistics_primitives_pool && list.statistics_mesh[segment]) + vk.end_query(cb, list.statistics_primitives_pool, list.statistics_base + segment); } inline void begin_statistics_segment(const PreparedList& list, std::uint32_t segment, VkCommandBuffer cb, @@ -3322,6 +3354,12 @@ inline void begin_statistics_segment(const PreparedList& list, std::uint32_t seg const auto pool = statistics_segment_pool(list, segment); vk.reset_query_pool(cb, pool, list.statistics_base + segment, 1); vk.begin_query(cb, pool, list.statistics_base + segment, 0); + // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type, which the + // mesh statistics pool cannot hold. + if (list.statistics_primitives_pool && list.statistics_mesh[segment]) { + vk.reset_query_pool(cb, list.statistics_primitives_pool, list.statistics_base + segment, 1); + vk.begin_query(cb, list.statistics_primitives_pool, list.statistics_base + segment, 0); + } } } // namespace detail @@ -3382,7 +3420,8 @@ inline void emit_query(const PreparedList& list, const detail::PreparedQuery& qu if (!run.pool) { vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0); } else if (run.sum && resolve.type == d3d12_query::pipeline_statistics) { - vk.sum_statistics(cb, run.pool, run.mesh_pool, run.mesh, run.first, run.count, dst, run.offset); + vk.sum_statistics(cb, run.pool, run.mesh_pool, run.primitives_pool, run.mesh, run.first, run.count, + dst, run.offset); } else if (run.sum && binary) { vk.sum_binary_occlusion(cb, run.pool, run.first, run.count, dst, run.offset); } else if (run.sum || binary) { diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index df430c11a6..1b99b8d0b1 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -138,6 +138,7 @@ int main() { info.buffer_device_address = true; info.shader_dir = library.string(); info.mesh_shader = mesh; + info.mesh_shader_queries = mesh && d.mesh_shader_queries; info.pipeline_statistics_query = d.pipeline_statistics_query; return info; }; @@ -405,8 +406,15 @@ 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. + // Where the device has the meshShaderQueries feature, the clipping invocations of a mesh segment come + // from a mesh-primitives query instead (D3D-MESH-CLIPPING-COUNTER): the number of primitives the mesh + // shader emitted that reached the fragment shader. How many that is depends on the driver, so the counts + // are compared with each other and not written down. if (d.pipeline_statistics_query) { const D3D12_QUERY_HEAP_DESC statistics_desc{D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS, 3, 0}; Ref statistics; @@ -472,11 +480,26 @@ 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); + // The clipping invocations of the mesh segments (D3D-MESH-CLIPPING-COUNTER). Without the feature they + // read 0; with it they are the mesh-primitives count of those segments. + const auto mesh_clipping = mixed.CInvocations - vertex_only.CInvocations; + if (d.mesh_shader_queries) { + // The count has to be wired up, not merely self-consistent. A layer that reported the feature but + // never created the third pool would leave both counts at 0, and 0 >= 0 would still hold. How many + // primitives the mesh shader emits is the driver's, so ask for a positive count and not for a + // number: the mesh draw above fills the top-left quadrant, so it emits at least one. + EXPECT(mesh_clipping >= mesh_only.CInvocations); + EXPECT(mesh_only.CInvocations > 0); + } else { + EXPECT(mesh_clipping == 0); + } + EXPECT(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); + // Vulkan has no clipped-primitive counter, so the clipping primitives of a mesh segment stay 0. + EXPECT(mesh_only.CPrimitives == 0 && mesh_only.PSInvocations > 0); + if (!d.mesh_shader_queries) EXPECT(mesh_only.CInvocations == 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..1e3da7c29b 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,10 @@ 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::set primitives_pools; // the mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER) 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 +115,12 @@ 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 + : primitives_pools.count(p) ? 1u + : (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]); } } @@ -239,8 +247,8 @@ struct MockReplay { VkDeviceSize off) { helper(false, false, p, f, n, b, off); }; - v.sum_statistics = [helper](VkCommandBuffer, VkQueryPool p, VkQueryPool, const std::uint8_t*, unsigned f, - unsigned n, VkBuffer b, VkDeviceSize off) { + v.sum_statistics = [helper](VkCommandBuffer, VkQueryPool p, VkQueryPool, VkQueryPool, const std::uint8_t*, + unsigned f, unsigned n, VkBuffer b, VkDeviceSize off) { helper(true, false, p, f, n, b, off); }; v.resolve_binary_occlusion = [helper](VkCommandBuffer, VkQueryPool p, unsigned f, unsigned n, VkBuffer b, @@ -286,26 +294,33 @@ 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 mesh-primitives count, the fragment and the compute invocations, + // and they land in CInvocations, PSInvocations and CSInvocations (statistics_sum.comp, D3D-291 and + // D3D-MESH-CLIPPING-COUNTER). + if (mesh && statistics && j != 5 && 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 +353,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) { @@ -348,7 +364,8 @@ void test_helper_chunks() { } } else { if (mode == MockReplay::statistics) { - helper.sum_statistics({}, qp, VK_NULL_HANDLE, nullptr, first, n, dst, output_offset); + helper.sum_statistics({}, qp, VK_NULL_HANDLE, VK_NULL_HANDLE, nullptr, first, n, dst, + output_offset); } else { helper.sum({}, qp, first, n, dst, output_offset); } @@ -363,6 +380,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); @@ -388,10 +407,53 @@ void test_helper_chunks() { expect(c.word(dst, output_offset) == reset_sum, "sum accumulator reset between calls"); c.mode = MockReplay::statistics; c.values[qp][first_query].fill(reset_counter); - helper.sum_statistics({}, qp, VK_NULL_HANDLE, nullptr, first_query, 1, dst, output_offset); + helper.sum_statistics({}, qp, VK_NULL_HANDLE, VK_NULL_HANDLE, nullptr, first_query, 1, dst, output_offset); 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; + constexpr std::uint64_t generated = 5; // the primitives a mesh segment emits (D3D-MESH-CLIPPING-COUNTER) + const std::uint8_t mesh_flags[mesh_segments] = {1, 1, 1}; + auto mesh_pool = handle(3); + auto primitives_pool = handle(4); + c.mesh_pools.insert(mesh_pool); + c.primitives_pools.insert(primitives_pool); + c.values[mesh_pool].resize(mesh_segments); + c.values[primitives_pool].resize(mesh_segments); + std::uint64_t fragment_total = 0, compute_total = 0, generated_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 + c.values[primitives_pool][i][0] = generated + i; // the primitives the mesh shader emitted + fragment_total += c.values[mesh_pool][i][0]; + compute_total += c.values[mesh_pool][i][1]; + generated_total += c.values[primitives_pool][i][0]; + } + 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, primitives_pool, mesh_flags, 0, mesh_segments, dst, output_offset); + for (unsigned j = 0; j < statistics_counters; ++j) { + const std::uint64_t v = j == 5 ? generated_total : 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 CInvocations, 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() == 2 && c.copies[0].pool == mesh_pool && c.copies[1].pool == primitives_pool, + "the mesh segment is read from the mesh pool and the mesh-primitives 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..87056a4990 100644 --- a/runtime/test/replay_query_predication_test.cpp +++ b/runtime/test/replay_query_predication_test.cpp @@ -127,8 +127,8 @@ int main() { vk.query_pool = [&](QueryHeapId id) -> QueryPool* { return id == 1 ? &heap : id == 6 ? &statistics_heap : nullptr; }; - vk.sum_statistics = [](VkCommandBuffer, VkQueryPool pool, VkQueryPool, const std::uint8_t*, std::uint32_t first, - std::uint32_t count, VkBuffer, VkDeviceSize) { + vk.sum_statistics = [](VkCommandBuffer, VkQueryPool pool, VkQueryPool, VkQueryPool, const std::uint8_t*, + std::uint32_t first, std::uint32_t count, VkBuffer, VkDeviceSize) { g_trace->events.push_back(query_event("sum_statistics", pool, first) + "+" + std::to_string(count)); }; vk.resolve_binary_occlusion = [](VkCommandBuffer, VkQueryPool, std::uint32_t, std::uint32_t, VkBuffer, @@ -685,14 +685,18 @@ int main() { auto segments = std::make_shared(); segments->pool = handle(9); segments->mesh_pool = handle(10); + segments->primitives_pool = handle(11); segments->mesh = prepared.statistics_mesh; segments->destroy = mock_destroy_pool; QueryReplay queries; expect(queries.plan(prepared, {}, 0, true, segments, 0) == ReplayResult::success, "plan mixed list"); expect(prepared.statistics_mesh_pool == handle(10), "mixed list: the mesh pool is the list's"); + expect(prepared.statistics_primitives_pool == handle(11), + "mixed list: the mesh-primitives pool is the list's"); expect(q[6].resolves.size() == 1 && q[6].resolves[0].sum && q[6].resolves[0].first == 3 && q[6].resolves[0].count == 1 && q[6].resolves[0].mesh_pool == handle(10) && - q[6].resolves[0].mesh && q[6].resolves[0].mesh[3] == 1, + q[6].resolves[0].primitives_pool == handle(11) && q[6].resolves[0].mesh && + q[6].resolves[0].mesh[3] == 1, "mixed list: the mesh-only query's resolve sums its one mesh segment"); expect(queries.needs_statistics_sum, "mixed list: the statistics sum helper is needed"); @@ -755,6 +759,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; diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 5cccd1836e..678d369e28 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -118,6 +118,7 @@ class Device { bool conservative_rasterization = false; // VK_EXT_conservative_rasterization enabled where available bool request_mesh_shader = false; // set before create(): enable VK_EXT_mesh_shader (meshShader) where available bool mesh_shader = false; // VK_EXT_mesh_shader and meshShader are enabled + bool mesh_shader_queries = false; // the meshShaderQueries feature is enabled on the device bool debug_utils = false; // VK_EXT_debug_utils is enabled on the instance // The subgroup sizes of the device, with subgroupSizeControl and computeFullSubgroups enabled where it has them. leonida::SubgroupSizeCaps subgroup; @@ -523,10 +524,13 @@ class Device { VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &mesh_features}; instance_fn("vkGetPhysicalDeviceFeatures2")(physical, &query); mesh_shader = mesh_features.meshShader == VK_TRUE; + mesh_shader_queries = mesh_features.meshShaderQueries == VK_TRUE; } if (mesh_shader) { mesh_features = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT, *v13_.next()}; mesh_features.meshShader = VK_TRUE; + // The mesh-primitives query, which counts the primitives a mesh shader emits (D3D-MESH-CLIPPING-COUNTER). + mesh_features.meshShaderQueries = mesh_shader_queries ? VK_TRUE : VK_FALSE; *v13_.next() = &mesh_features; } enable.occlusionQueryPrecise = features.features.occlusionQueryPrecise; // query heaps, where available