Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/statistics_sum.comp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down
70 changes: 33 additions & 37 deletions runtime/src/statistics_sum_spirv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
13 changes: 5 additions & 8 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down Expand Up @@ -2443,15 +2443,12 @@ class VulkanQueue : private QueueHolds<VkSemaphore>::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;
Expand Down
24 changes: 20 additions & 4 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading
Loading