upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 21:40:18 +0800
committerRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 21:40:18 +0800
commit1b1d4edd2dea9ef1fbabfadb8e3d719be39e38eb (patch)
treeb9a8aa144a60538b022cdf92f85cd6ee679d171b /runtime/src
parentabbd7d9b27dd624bc1446ced9539b78a0193c1ba (diff)
parent47c6511747c93aa3fb908d7782ca96173d0eead3 (diff)
Merge current main and retain mesh statistics in the first-run rendering context
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/statistics_sum.comp10
-rw-r--r--runtime/src/statistics_sum_spirv.h65
-rw-r--r--runtime/src/vulkan_backend.h55
-rw-r--r--runtime/src/vulkan_replay.h57
4 files changed, 126 insertions, 61 deletions
diff --git a/runtime/src/statistics_sum.comp b/runtime/src/statistics_sum.comp
index c4960347c..c4a02a459 100644
--- a/runtime/src/statistics_sum.comp
+++ b/runtime/src/statistics_sum.comp
@@ -2,9 +2,11 @@
2 2
3// Adds the D3D12_QUERY_DATA_PIPELINE_STATISTICS segments of one split pipeline-statistics query (D3D-162): one 3// Adds the D3D12_QUERY_DATA_PIPELINE_STATISTICS segments of one split pipeline-statistics query (D3D-162): one
4// invocation per counter, in the order of VkQueryPipelineStatisticFlagBits, which is the D3D12 field order. 4// invocation per counter, in the order of VkQueryPipelineStatisticFlagBits, which is the D3D12 field order.
5// `mesh` segments come from the pool a mesh draw can run in (D3D-291), which counts only clipping, fragment and 5// `mesh` segments come from the pool a mesh draw can run in (D3D-291), which counts only the fragment and compute
6// compute: they are copied to counters 5..8 of their record, so CSInvocations sits at 8 and the input-assembly, 6// invocations: they are copied to counters 7 and 8 of their record, so PSInvocations takes the fragment one and
7// vertex, geometry and tessellation counters (0..4, 8 and 9 of the D3D12 order) add nothing. 7// CSInvocations the compute one, and every other counter adds nothing.
8// The clipping invocations of a mesh segment come from the mesh-primitives count of that segment, copied to
9// counter 5 (D3D-MESH-CLIPPING-COUNTER); without that count the field stays 0.
8layout(local_size_x = 11) in; 10layout(local_size_x = 11) in;
9layout(set = 0, binding = 0, std430) buffer QueryResults { 11layout(set = 0, binding = 0, std430) buffer QueryResults {
10 uvec2 total[11]; 12 uvec2 total[11];
@@ -15,7 +17,7 @@ layout(push_constant) uniform ResolveRange { uint count; uint mesh; };
15void main() { 17void main() {
16 const uint counter = gl_LocalInvocationID.x; 18 const uint counter = gl_LocalInvocationID.x;
17 uvec2 sum = total[counter]; 19 uvec2 sum = total[counter];
18 const bool counted = mesh == 0u || (counter >= 5u && counter <= 7u) || counter == 10u; 20 const bool counted = mesh == 0u || counter == 5u || counter == 7u || counter == 10u;
19 const uint source = mesh != 0u && counter == 10u ? 8u : counter; 21 const uint source = mesh != 0u && counter == 10u ? 8u : counter;
20 for (uint i = 0; counted && i < count; ++i) { 22 for (uint i = 0; counted && i < count; ++i) {
21 uvec2 segment = segments[i * 11 + source]; 23 uvec2 segment = segments[i * 11 + source];
diff --git a/runtime/src/statistics_sum_spirv.h b/runtime/src/statistics_sum_spirv.h
index 9c67f0429..a147cd4df 100644
--- a/runtime/src/statistics_sum_spirv.h
+++ b/runtime/src/statistics_sum_spirv.h
@@ -7,7 +7,7 @@ namespace leonida {
7// From statistics_sum.comp. Compile with glslangValidator -V --target-env vulkan1.0 -Os -g0, 7// From statistics_sum.comp. Compile with glslangValidator -V --target-env vulkan1.0 -Os -g0,
8// then encode the little-endian SPIR-V as uint32_t words. No shader compiler is needed at runtime build time. 8// then encode the little-endian SPIR-V as uint32_t words. No shader compiler is needed at runtime build time.
9inline constexpr std::uint32_t statistics_sum_spirv[] = { 9inline constexpr std::uint32_t statistics_sum_spirv[] = {
10 0x07230203, 0x00010000, 0x0008000b, 0x00000080, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, 10 0x07230203, 0x00010000, 0x0008000b, 0x00000084, 0x00000000, 0x00020011, 0x00000001, 0x0006000b,
11 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 11 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001,
12 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x00060010, 0x00000004, 12 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x00060010, 0x00000004,
13 0x00000011, 0x0000000b, 0x00000001, 0x00000001, 0x00040047, 0x0000000b, 0x0000000b, 0x0000001b, 13 0x00000011, 0x0000000b, 0x00000001, 0x00000001, 0x00040047, 0x0000000b, 0x0000000b, 0x0000001b,
@@ -16,7 +16,7 @@ inline constexpr std::uint32_t statistics_sum_spirv[] = {
16 0x00050048, 0x00000016, 0x00000001, 0x00000023, 0x00000058, 0x00040047, 0x00000018, 0x00000021, 16 0x00050048, 0x00000016, 0x00000001, 0x00000023, 0x00000058, 0x00040047, 0x00000018, 0x00000021,
17 0x00000000, 0x00040047, 0x00000018, 0x00000022, 0x00000000, 0x00030047, 0x00000022, 0x00000002, 17 0x00000000, 0x00040047, 0x00000018, 0x00000022, 0x00000000, 0x00030047, 0x00000022, 0x00000002,
18 0x00050048, 0x00000022, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x00000022, 0x00000001, 18 0x00050048, 0x00000022, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x00000022, 0x00000001,
19 0x00000023, 0x00000004, 0x00040047, 0x00000072, 0x0000000b, 0x00000019, 0x00020013, 0x00000002, 19 0x00000023, 0x00000004, 0x00040047, 0x0000006f, 0x0000000b, 0x00000019, 0x00020013, 0x00000002,
20 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000006, 0x00000020, 0x00000000, 0x00040017, 20 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000006, 0x00000020, 0x00000000, 0x00040017,
21 0x00000009, 0x00000006, 0x00000003, 0x00040020, 0x0000000a, 0x00000001, 0x00000009, 0x0004003b, 21 0x00000009, 0x00000006, 0x00000003, 0x00040020, 0x0000000a, 0x00000001, 0x00000009, 0x0004003b,
22 0x0000000a, 0x0000000b, 0x00000001, 0x0004002b, 0x00000006, 0x0000000c, 0x00000000, 0x00040020, 22 0x0000000a, 0x0000000b, 0x00000001, 0x0004002b, 0x00000006, 0x0000000c, 0x00000000, 0x00040020,
@@ -28,42 +28,39 @@ inline constexpr std::uint32_t statistics_sum_spirv[] = {
28 0x00000002, 0x00000010, 0x00020014, 0x0000001f, 0x0004001e, 0x00000022, 0x00000006, 0x00000006, 28 0x00000002, 0x00000010, 0x00020014, 0x0000001f, 0x0004001e, 0x00000022, 0x00000006, 0x00000006,
29 0x00040020, 0x00000023, 0x00000009, 0x00000022, 0x0004003b, 0x00000023, 0x00000024, 0x00000009, 29 0x00040020, 0x00000023, 0x00000009, 0x00000022, 0x0004003b, 0x00000023, 0x00000024, 0x00000009,
30 0x0004002b, 0x00000019, 0x00000025, 0x00000001, 0x00040020, 0x00000026, 0x00000009, 0x00000006, 30 0x0004002b, 0x00000019, 0x00000025, 0x00000001, 0x00040020, 0x00000026, 0x00000009, 0x00000006,
31 0x0004002b, 0x00000006, 0x0000002e, 0x00000005, 0x0004002b, 0x00000006, 0x00000031, 0x00000007, 31 0x0004002b, 0x00000006, 0x0000002b, 0x00000005, 0x0004002b, 0x00000006, 0x0000002f, 0x00000007,
32 0x0004002b, 0x00000006, 0x00000036, 0x0000000a, 0x0004002b, 0x00000006, 0x00000040, 0x00000008, 32 0x0004002b, 0x00000006, 0x00000033, 0x0000000a, 0x0004002b, 0x00000006, 0x0000003d, 0x00000008,
33 0x0004002b, 0x00000006, 0x0000005e, 0x00000001, 0x0006002c, 0x00000009, 0x00000072, 0x00000013, 33 0x0004002b, 0x00000006, 0x0000005b, 0x00000001, 0x0006002c, 0x00000009, 0x0000006f, 0x00000013,
34 0x0000005e, 0x0000005e, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 34 0x0000005b, 0x0000005b, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8,
35 0x00000005, 0x00050041, 0x0000000d, 0x0000000e, 0x0000000b, 0x0000000c, 0x0004003d, 0x00000006, 35 0x00000005, 0x00050041, 0x0000000d, 0x0000000e, 0x0000000b, 0x0000000c, 0x0004003d, 0x00000006,
36 0x0000000f, 0x0000000e, 0x00060041, 0x0000001c, 0x0000001d, 0x00000018, 0x0000001a, 0x0000000f, 36 0x0000000f, 0x0000000e, 0x00060041, 0x0000001c, 0x0000001d, 0x00000018, 0x0000001a, 0x0000000f,
37 0x0004003d, 0x00000010, 0x0000001e, 0x0000001d, 0x00050041, 0x00000026, 0x00000027, 0x00000024, 37 0x0004003d, 0x00000010, 0x0000001e, 0x0000001d, 0x00050041, 0x00000026, 0x00000027, 0x00000024,
38 0x00000025, 0x0004003d, 0x00000006, 0x00000028, 0x00000027, 0x000500aa, 0x0000001f, 0x00000029, 38 0x00000025, 0x0004003d, 0x00000006, 0x00000028, 0x00000027, 0x000500aa, 0x0000001f, 0x00000029,
39 0x00000028, 0x0000000c, 0x000400a8, 0x0000001f, 0x0000002a, 0x00000029, 0x000300f7, 0x0000002c, 39 0x00000028, 0x0000000c, 0x000500aa, 0x0000001f, 0x0000002c, 0x0000000f, 0x0000002b, 0x000500a6,
40 0x00000000, 0x000400fa, 0x0000002a, 0x0000002b, 0x0000002c, 0x000200f8, 0x0000002b, 0x000500ae, 40 0x0000001f, 0x0000002d, 0x00000029, 0x0000002c, 0x000500aa, 0x0000001f, 0x00000030, 0x0000000f,
41 0x0000001f, 0x0000002f, 0x0000000f, 0x0000002e, 0x000500b2, 0x0000001f, 0x00000032, 0x0000000f, 41 0x0000002f, 0x000500a6, 0x0000001f, 0x00000031, 0x0000002d, 0x00000030, 0x000500aa, 0x0000001f,
42 0x00000031, 0x000500a7, 0x0000001f, 0x00000033, 0x0000002f, 0x00000032, 0x000200f9, 0x0000002c, 42 0x00000034, 0x0000000f, 0x00000033, 0x000500a6, 0x0000001f, 0x00000035, 0x00000031, 0x00000034,
43 0x000200f8, 0x0000002c, 0x000700f5, 0x0000001f, 0x00000034, 0x00000029, 0x00000005, 0x00000033, 43 0x000500ab, 0x0000001f, 0x00000039, 0x00000028, 0x0000000c, 0x000500a7, 0x0000001f, 0x0000003c,
44 0x0000002b, 0x000500aa, 0x0000001f, 0x00000037, 0x0000000f, 0x00000036, 0x000500a6, 0x0000001f, 44 0x00000039, 0x00000034, 0x000600a9, 0x00000006, 0x0000003f, 0x0000003c, 0x0000003d, 0x0000000f,
45 0x00000038, 0x00000034, 0x00000037, 0x000500ab, 0x0000001f, 0x0000003c, 0x00000028, 0x0000000c, 45 0x000200f9, 0x00000041, 0x000200f8, 0x00000041, 0x000700f5, 0x00000010, 0x00000082, 0x0000001e,
46 0x000500a7, 0x0000001f, 0x0000003f, 0x0000003c, 0x00000037, 0x000600a9, 0x00000006, 0x00000042, 46 0x00000005, 0x00000083, 0x00000042, 0x000700f5, 0x00000006, 0x00000071, 0x0000000c, 0x00000005,
47 0x0000003f, 0x00000040, 0x0000000f, 0x000200f9, 0x00000044, 0x000200f8, 0x00000044, 0x000700f5, 47 0x0000006b, 0x00000042, 0x000400f6, 0x00000043, 0x00000042, 0x00000000, 0x000200f9, 0x00000045,
48 0x00000010, 0x0000007f, 0x0000001e, 0x0000002c, 0x0000007c, 0x00000045, 0x000700f5, 0x00000006, 48 0x000200f8, 0x00000045, 0x000300f7, 0x00000048, 0x00000000, 0x000400fa, 0x00000035, 0x00000047,
49 0x0000007d, 0x0000000c, 0x0000002c, 0x0000006e, 0x00000045, 0x000400f6, 0x00000046, 0x00000045, 49 0x00000048, 0x000200f8, 0x00000047, 0x00050041, 0x00000026, 0x0000004a, 0x00000024, 0x0000001a,
50 0x00000000, 0x000200f9, 0x00000048, 0x000200f8, 0x00000048, 0x000300f7, 0x0000004b, 0x00000000, 50 0x0004003d, 0x00000006, 0x0000004b, 0x0000004a, 0x000500b0, 0x0000001f, 0x0000004c, 0x00000071,
51 0x000400fa, 0x00000038, 0x0000004a, 0x0000004b, 0x000200f8, 0x0000004a, 0x00050041, 0x00000026, 51 0x0000004b, 0x000200f9, 0x00000048, 0x000200f8, 0x00000048, 0x000700f5, 0x0000001f, 0x0000004d,
52 0x0000004d, 0x00000024, 0x0000001a, 0x0004003d, 0x00000006, 0x0000004e, 0x0000004d, 0x000500b0, 52 0x00000035, 0x00000045, 0x0000004c, 0x00000047, 0x000400fa, 0x0000004d, 0x00000042, 0x00000043,
53 0x0000001f, 0x0000004f, 0x0000007d, 0x0000004e, 0x000200f9, 0x0000004b, 0x000200f8, 0x0000004b, 53 0x000200f8, 0x00000042, 0x00050084, 0x00000006, 0x00000050, 0x00000071, 0x00000013, 0x00050080,
54 0x000700f5, 0x0000001f, 0x00000050, 0x00000038, 0x00000048, 0x0000004f, 0x0000004a, 0x000400fa, 54 0x00000006, 0x00000052, 0x00000050, 0x0000003f, 0x00060041, 0x0000001c, 0x00000053, 0x00000018,
55 0x00000050, 0x00000045, 0x00000046, 0x000200f8, 0x00000045, 0x00050084, 0x00000006, 0x00000053, 55 0x00000025, 0x00000052, 0x0004003d, 0x00000010, 0x00000054, 0x00000053, 0x00050051, 0x00000006,
56 0x0000007d, 0x00000013, 0x00050080, 0x00000006, 0x00000055, 0x00000053, 0x00000042, 0x00060041, 56 0x00000057, 0x00000082, 0x00000000, 0x00050051, 0x00000006, 0x00000059, 0x00000054, 0x00000000,
57 0x0000001c, 0x00000056, 0x00000018, 0x00000025, 0x00000055, 0x0004003d, 0x00000010, 0x00000057, 57 0x00050080, 0x00000006, 0x0000005a, 0x00000057, 0x00000059, 0x00050051, 0x00000006, 0x0000005d,
58 0x00000056, 0x00050051, 0x00000006, 0x0000005a, 0x0000007f, 0x00000000, 0x00050051, 0x00000006, 58 0x00000054, 0x00000001, 0x000500b0, 0x0000001f, 0x00000061, 0x0000005a, 0x00000057, 0x000600a9,
59 0x0000005c, 0x00000057, 0x00000000, 0x00050080, 0x00000006, 0x0000005d, 0x0000005a, 0x0000005c, 59 0x00000006, 0x00000062, 0x00000061, 0x0000005b, 0x0000000c, 0x00050080, 0x00000006, 0x00000063,
60 0x00050051, 0x00000006, 0x00000060, 0x00000057, 0x00000001, 0x000500b0, 0x0000001f, 0x00000064, 60 0x0000005d, 0x00000062, 0x00050051, 0x00000006, 0x00000065, 0x00000082, 0x00000001, 0x00050080,
61 0x0000005d, 0x0000005a, 0x000600a9, 0x00000006, 0x00000065, 0x00000064, 0x0000005e, 0x0000000c, 61 0x00000006, 0x00000066, 0x00000065, 0x00000063, 0x00050050, 0x00000010, 0x00000083, 0x0000005a,
62 0x00050080, 0x00000006, 0x00000066, 0x00000060, 0x00000065, 0x00050051, 0x00000006, 0x00000068, 62 0x00000066, 0x00050080, 0x00000006, 0x0000006b, 0x00000071, 0x00000025, 0x000200f9, 0x00000041,
63 0x0000007f, 0x00000001, 0x00050080, 0x00000006, 0x00000069, 0x00000068, 0x00000066, 0x00050050, 63 0x000200f8, 0x00000043, 0x0003003e, 0x0000001d, 0x00000082, 0x000100fd, 0x00010038,
64 0x00000010, 0x0000007c, 0x0000005d, 0x00000069, 0x00050080, 0x00000006, 0x0000006e, 0x0000007d,
65 0x00000025, 0x000200f9, 0x00000044, 0x000200f8, 0x00000046, 0x0003003e, 0x0000001d, 0x0000007f,
66 0x000100fd, 0x00010038,
67}; 64};
68 65
69} // namespace leonida 66} // namespace leonida
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 72c716ed1..23b14c0bb 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -245,12 +245,16 @@ public:
245 245
246 // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the 246 // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the
247 // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). 247 // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291).
248 // Segment i is in `mesh_queries` when mesh[i] is set (four counters: clipping, fragment and compute; see 248 // Segment i is in `mesh_queries` when mesh[i] is set (two counters: fragment and compute; see
249 // statistics_sum.comp), otherwise in `queries` (all eleven). 249 // statistics_sum.comp), otherwise in `queries` (all eleven). A mesh segment's clipping invocations come from
250 void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, const std::uint8_t* mesh, 250 // `primitives_queries`, the mesh-primitives count of that segment (D3D-MESH-CLIPPING-COUNTER); it is null when
251 std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { 251 // the device has no meshShaderQueries, and the field then reads 0.
252 void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries,
253 VkQueryPool primitives_queries, const std::uint8_t* mesh, std::uint32_t first,
254 std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const {
252 constexpr VkDeviceSize result = statistics_counters * word_size; 255 constexpr VkDeviceSize result = statistics_counters * word_size;
253 constexpr VkDeviceSize mesh_slot = 5 * word_size; // CInvocations, where the mesh pool's first counter goes 256 constexpr VkDeviceSize mesh_slot = 7 * word_size; // PSInvocations, where the mesh pool's first counter goes
257 constexpr VkDeviceSize clipping_slot = 5 * word_size; // CInvocations, where the mesh-primitives count goes
254 begin_scratch_write(cb); 258 begin_scratch_write(cb);
255 vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); 259 vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0);
256 bind(cb, statistics_pipeline_); 260 bind(cb, statistics_pipeline_);
@@ -259,9 +263,18 @@ public:
259 std::uint32_t chunk = 1; 263 std::uint32_t chunk = 1;
260 while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) 264 while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh)
261 ++chunk; 265 ++chunk;
266 if (in_mesh) {
267 // The mesh pool holds only the fragment and compute counters, so the clipping field of a mesh
268 // segment reads 0 unless the mesh-primitives count fills it (D3D-MESH-CLIPPING-COUNTER).
269 vk_.vkCmdFillBuffer(cb, scratch_, result, chunk * result, 0);
270 barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
271 }
262 vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, 272 vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_,
263 result + (in_mesh ? mesh_slot : 0), result, 273 result + (in_mesh ? mesh_slot : 0), result,
264 VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); 274 VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
275 if (in_mesh && primitives_queries)
276 vk_.vkCmdCopyQueryPoolResults(cb, primitives_queries, first, chunk, scratch_, result + clipping_slot,
277 result, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
265 barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, 278 barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT,
266 shader_access); 279 shader_access);
267 dispatch(cb, chunk, 1, in_mesh); 280 dispatch(cb, chunk, 1, in_mesh);
@@ -924,6 +937,10 @@ struct TileRange {
924#endif 937#endif
925class VulkanContext { 938class VulkanContext {
926public: 939public:
940 // mesh_queries: VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so a mesh segment's
941 // clipping invocations can come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Set by the device
942 // that created the context.
943 bool mesh_queries = false;
927 // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). 944 // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication).
928 // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. 945 // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`.
929 // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. 946 // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress.
@@ -2524,9 +2541,11 @@ private:
2524 occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); 2541 occlusion_resolve_->normalize(cb, pool, first, count, dst, offset);
2525 }; 2542 };
2526 replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, 2543 replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool,
2527 const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, 2544 VkQueryPool primitives_pool, const std::uint8_t* mesh,
2528 VkBuffer dst, VkDeviceSize offset) { 2545 std::uint32_t first, std::uint32_t count, VkBuffer dst,
2529 occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); 2546 VkDeviceSize offset) {
2547 occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, primitives_pool, mesh, first, count, dst,
2548 offset);
2530 }; 2549 };
2531 replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, 2550 replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first,
2532 std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { 2551 std::uint32_t count, VkBuffer dst, VkDeviceSize offset) {
@@ -2573,18 +2592,26 @@ private:
2573 if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { 2592 if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) {
2574 statistics->mesh.clear(); 2593 statistics->mesh.clear();
2575 } else { 2594 } else {
2576 // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping 2595 // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): the
2577 // invocations and primitives, fragment and compute invocations (D3D-291). 2596 // fragment and compute invocations, and no other (D3D-291).
2578 VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; 2597 VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
2579 info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; 2598 info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS;
2580 info.queryCount = statistics_segments; 2599 info.queryCount = statistics_segments;
2581 info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | 2600 info.pipelineStatistics = kMeshStatisticsFlags;
2582 VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT |
2583 VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT |
2584 VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
2585 if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != 2601 if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) !=
2586 VK_SUCCESS) 2602 VK_SUCCESS)
2587 return ReplayResult::unsupported; 2603 return ReplayResult::unsupported;
2604 // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type,
2605 // which the pipeline-statistics pool cannot hold. The device needs the meshShaderQueries feature,
2606 // or the clipping field of a mesh segment stays 0.
2607 if (context_.mesh_queries) {
2608 VkQueryPoolCreateInfo primitives{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
2609 primitives.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT;
2610 primitives.queryCount = statistics_segments;
2611 if (context_.vk.vkCreateQueryPool(statistics->device, &primitives, nullptr,
2612 &statistics->primitives_pool) != VK_SUCCESS)
2613 return ReplayResult::unsupported;
2614 }
2588 } 2615 }
2589 } 2616 }
2590 // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after 2617 // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 60896ae74..cdaf89321 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -733,6 +733,9 @@ struct OcclusionSegmentPool {
733 // segment index whether it runs there. Empty when no segment of the submission holds a mesh draw. 733 // segment index whether it runs there. Empty when no segment of the submission holds a mesh draw.
734 VkQueryPool mesh_pool = VK_NULL_HANDLE; 734 VkQueryPool mesh_pool = VK_NULL_HANDLE;
735 std::vector<std::uint8_t> mesh; 735 std::vector<std::uint8_t> mesh;
736 // The mesh-primitives pool of the mesh segments (D3D-MESH-CLIPPING-COUNTER): one query per segment, begun and
737 // ended with the mesh segment. Null when the device has no meshShaderQueries.
738 VkQueryPool primitives_pool = VK_NULL_HANDLE;
736 739
737 OcclusionSegmentPool() = default; 740 OcclusionSegmentPool() = default;
738 OcclusionSegmentPool(const OcclusionSegmentPool&) = delete; 741 OcclusionSegmentPool(const OcclusionSegmentPool&) = delete;
@@ -740,6 +743,7 @@ struct OcclusionSegmentPool {
740 ~OcclusionSegmentPool() { 743 ~OcclusionSegmentPool() {
741 if (pool) destroy(device, pool, nullptr); 744 if (pool) destroy(device, pool, nullptr);
742 if (mesh_pool) destroy(device, mesh_pool, nullptr); 745 if (mesh_pool) destroy(device, mesh_pool, nullptr);
746 if (primitives_pool) destroy(device, primitives_pool, nullptr);
743 } 747 }
744}; 748};
745 749
@@ -909,9 +913,12 @@ struct ReplayFunctions {
909 VkBuffer dst, VkDeviceSize offset)> resolve_binary_occlusion; 913 VkBuffer dst, VkDeviceSize offset)> resolve_binary_occlusion;
910 // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS, outside conditional 914 // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS, outside conditional
911 // rendering: the segments of a statistics query split around resolves (D3D-162) and mesh draws (D3D-291). 915 // rendering: the segments of a statistics query split around resolves (D3D-162) and mesh draws (D3D-291).
912 // Segment i is in the second pool when mesh[i] is set (mesh may be null: none is). 916 // Segment i is in the second pool when mesh[i] is set (mesh may be null: none is). The third pool holds the
913 std::function<void(VkCommandBuffer, VkQueryPool, VkQueryPool mesh_pool, const std::uint8_t* mesh, 917 // mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER); it is null when the device has no
914 std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset)> 918 // meshShaderQueries.
919 std::function<void(VkCommandBuffer, VkQueryPool, VkQueryPool mesh_pool, VkQueryPool primitives_pool,
920 const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, VkBuffer dst,
921 VkDeviceSize offset)>
915 sum_statistics; 922 sum_statistics;
916 // Sums `count` binary-occlusion segments and normalizes the total to a 64-bit 0 or 1, outside conditional 923 // Sums `count` binary-occlusion segments and normalizes the total to a 64-bit 0 or 1, outside conditional
917 // rendering: a binary query split over physical segments because it overlaps other occlusion queries (D3D-252). 924 // rendering: a binary query split over physical segments because it overlaps other occlusion queries (D3D-252).
@@ -1001,6 +1008,8 @@ struct QueryResolveRun {
1001 bool sum = false; 1008 bool sum = false;
1002 // Statistics segments: the mesh-compatible pool and the kind of each segment (OcclusionSegmentPool::mesh). 1009 // Statistics segments: the mesh-compatible pool and the kind of each segment (OcclusionSegmentPool::mesh).
1003 VkQueryPool mesh_pool = VK_NULL_HANDLE; 1010 VkQueryPool mesh_pool = VK_NULL_HANDLE;
1011 // The mesh-primitives pool of the mesh segments (D3D-MESH-CLIPPING-COUNTER); null without meshShaderQueries.
1012 VkQueryPool primitives_pool = VK_NULL_HANDLE;
1004 const std::uint8_t* mesh = nullptr; 1013 const std::uint8_t* mesh = nullptr;
1005}; 1014};
1006 1015
@@ -2488,6 +2497,8 @@ public:
2488 // For each of those segments, whether it holds mesh draws and so runs in statistics_mesh_pool (D3D-291). 2497 // For each of those segments, whether it holds mesh draws and so runs in statistics_mesh_pool (D3D-291).
2489 std::vector<std::uint8_t> statistics_mesh; 2498 std::vector<std::uint8_t> statistics_mesh;
2490 VkQueryPool statistics_mesh_pool = VK_NULL_HANDLE; 2499 VkQueryPool statistics_mesh_pool = VK_NULL_HANDLE;
2500 // The mesh-primitives pool of the list's mesh segments (D3D-MESH-CLIPPING-COUNTER).
2501 VkQueryPool statistics_primitives_pool = VK_NULL_HANDLE;
2491 // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them 2502 // Copies, resolves and UAV clears that run as transfers, recorded while a predicate was set. D3D12 skips them
2492 // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run 2503 // when the predicate says so, but Vulkan's conditional rendering does not cover transfer commands, so they run
2493 // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it. 2504 // unconditionally (docs/d3d12_vulkan.md section 4.7). Counted so the caller can report it.
@@ -2543,10 +2554,26 @@ inline bool has_helper_dispatch(const PreparedList& out, std::size_t i) {
2543// Helper dispatches must not contribute to the application's CSInvocations (D3D-162). Query resolves and 2554// Helper dispatches must not contribute to the application's CSInvocations (D3D-162). Query resolves and
2544// ordinary helper commands share one sequence of physical statistics segments. Resolve dispatch requirements 2555// ordinary helper commands share one sequence of physical statistics segments. Resolve dispatch requirements
2545// depend on heap state at planning time, so every non-timestamp resolve splits conservatively. 2556// depend on heap state at planning time, so every non-timestamp resolve splits conservatively.
2546// A mesh draw cannot run while a query with the input-assembly, vertex, geometry or tessellation counters is active 2557// The counters a mesh draw may count: VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 leaves the input
2547// (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076), though D3D12 allows it and reads those fields as 0. A query 2558// assembly, vertex, geometry, tessellation and clipping bits out of the pipelineStatistics flags of every pool that
2548// that holds a mesh draw is split as well (D3D-291): a segment's first draw decides whether it runs in the full pool 2559// is active during a mesh draw, so a mesh segment runs in a pool with the fragment and compute counters only
2549// or in the mesh one (statistics_mesh), and a later draw of the other kind ends it and begins one of its own kind. 2560// (D3D-291). D3D12 counts the clipping invocations and primitives of a mesh pipeline, but Vulkan has no counter for
2561// them during a mesh draw, so those two fields read 0.
2562inline constexpr VkQueryPipelineStatisticFlags kMeshStatisticsFlags =
2563 VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
2564// The bits the same VUID forbids in a pool that is active during a mesh draw.
2565inline constexpr VkQueryPipelineStatisticFlags kForbiddenMeshStatisticsFlags =
2566 VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT | VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT |
2567 VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT | VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT |
2568 VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT |
2569 VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT |
2570 VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT |
2571 VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT;
2572// A mesh draw cannot run while a query with the input-assembly, vertex, geometry, tessellation or clipping counters
2573// is active (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076), though D3D12 allows it and reads those fields as
2574// 0. A query that holds a mesh draw is split as well (D3D-291): a segment's first draw decides whether it runs in
2575// the full pool or in the mesh one (statistics_mesh), and a later draw of the other kind ends it and begins one of
2576// its own kind.
2550// Dispatches and segments without draws use the full pool; ExecuteIndirect draws count as vertex draws. Each draw 2577// Dispatches and segments without draws use the full pool; ExecuteIndirect draws count as vertex draws. Each draw
2551// has its own rendering scope, so a split before one stays outside rendering, as a helper's does. 2578// has its own rendering scope, so a split before one stays outside rendering, as a helper's does.
2552inline void plan_statistics_segments(PreparedList& out) { 2579inline void plan_statistics_segments(PreparedList& out) {
@@ -3519,6 +3546,7 @@ public:
3519 retain(statistics_pool); 3546 retain(statistics_pool);
3520 list.statistics_pool = statistics_pool->pool; 3547 list.statistics_pool = statistics_pool->pool;
3521 list.statistics_mesh_pool = statistics_pool->mesh_pool; 3548 list.statistics_mesh_pool = statistics_pool->mesh_pool;
3549 list.statistics_primitives_pool = statistics_pool->primitives_pool;
3522 list.statistics_base = statistics_base; 3550 list.statistics_base = statistics_base;
3523 } else if (mesh) { 3551 } else if (mesh) {
3524 // Unsplit, the query would be active around a mesh draw with counters Vulkan forbids there. 3552 // Unsplit, the query would be active around a mesh draw with counters Vulkan forbids there.
@@ -3575,7 +3603,8 @@ public:
3575 const bool any_mesh = kinds && std::find(kinds + saved.first, kinds + saved.first + saved.count, 3603 const bool any_mesh = kinds && std::find(kinds + saved.first, kinds + saved.first + saved.count,
3576 std::uint8_t(1)) != kinds + saved.first + saved.count; 3604 std::uint8_t(1)) != kinds + saved.first + saved.count;
3577 run = {saved.segments->pool, saved.first, saved.count, run.offset, 3605 run = {saved.segments->pool, saved.first, saved.count, run.offset,
3578 saved.count > 1 || any_mesh, saved.segments->mesh_pool, kinds}; 3606 saved.count > 1 || any_mesh, saved.segments->mesh_pool,
3607 saved.segments->primitives_pool, kinds};
3579 if (any_mesh && !statistics) return ReplayResult::invalid_query; 3608 if (any_mesh && !statistics) return ReplayResult::invalid_query;
3580 if (!can_sum || (run.sum && !uncounted)) return ReplayResult::unsupported; 3609 if (!can_sum || (run.sum && !uncounted)) return ReplayResult::unsupported;
3581 if (statistics) needs_statistics_sum |= run.sum; 3610 if (statistics) needs_statistics_sum |= run.sum;
@@ -3642,6 +3671,9 @@ inline void end_statistics_segment(const PreparedList& list, std::uint32_t segme
3642 const ReplayFunctions& vk) { 3671 const ReplayFunctions& vk) {
3643 if (segment == PreparedQuery::no_segment) return; 3672 if (segment == PreparedQuery::no_segment) return;
3644 vk.end_query(cb, statistics_segment_pool(list, segment), list.statistics_base + segment); 3673 vk.end_query(cb, statistics_segment_pool(list, segment), list.statistics_base + segment);
3674 // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER).
3675 if (list.statistics_primitives_pool && list.statistics_mesh[segment])
3676 vk.end_query(cb, list.statistics_primitives_pool, list.statistics_base + segment);
3645} 3677}
3646 3678
3647inline void begin_statistics_segment(const PreparedList& list, std::uint32_t segment, VkCommandBuffer cb, 3679inline void begin_statistics_segment(const PreparedList& list, std::uint32_t segment, VkCommandBuffer cb,
@@ -3650,6 +3682,12 @@ inline void begin_statistics_segment(const PreparedList& list, std::uint32_t seg
3650 const auto pool = statistics_segment_pool(list, segment); 3682 const auto pool = statistics_segment_pool(list, segment);
3651 vk.reset_query_pool(cb, pool, list.statistics_base + segment, 1); 3683 vk.reset_query_pool(cb, pool, list.statistics_base + segment, 1);
3652 vk.begin_query(cb, pool, list.statistics_base + segment, 0); 3684 vk.begin_query(cb, pool, list.statistics_base + segment, 0);
3685 // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type, which the
3686 // mesh statistics pool cannot hold.
3687 if (list.statistics_primitives_pool && list.statistics_mesh[segment]) {
3688 vk.reset_query_pool(cb, list.statistics_primitives_pool, list.statistics_base + segment, 1);
3689 vk.begin_query(cb, list.statistics_primitives_pool, list.statistics_base + segment, 0);
3690 }
3653} 3691}
3654 3692
3655} // namespace detail 3693} // namespace detail
@@ -3710,7 +3748,8 @@ inline void emit_query(const PreparedList& list, const detail::PreparedQuery& qu
3710 if (!run.pool) { 3748 if (!run.pool) {
3711 vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0); 3749 vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0);
3712 } else if (run.sum && resolve.type == d3d12_query::pipeline_statistics) { 3750 } else if (run.sum && resolve.type == d3d12_query::pipeline_statistics) {
3713 vk.sum_statistics(cb, run.pool, run.mesh_pool, run.mesh, run.first, run.count, dst, run.offset); 3751 vk.sum_statistics(cb, run.pool, run.mesh_pool, run.primitives_pool, run.mesh, run.first, run.count,
3752 dst, run.offset);
3714 } else if (run.sum && binary) { 3753 } else if (run.sum && binary) {
3715 vk.sum_binary_occlusion(cb, run.pool, run.first, run.count, dst, run.offset); 3754 vk.sum_binary_occlusion(cb, run.pool, run.first, run.count, dst, run.offset);
3716 } else if (run.sum || binary) { 3755 } else if (run.sum || binary) {