RI::cmdBeginQuery, RI::cmdEndQuery, RI::pipelineStatisticsLayout¶
What it does¶
Occlusion and counter queries measure GPU work recorded into a query pool of type
QueryType::eOcclusion, QueryType::ePipelineStatistics, QueryType::eMeshPipelineStatistics, or
QueryType::eMeshPrimitives:
RI::cmdBeginQuery(cmd, pool, index)andRI::cmdEndQuery(cmd, pool, index)begin and end queryindexinpool, measuring the commands recorded between them in the same command list.- An occlusion query (
QueryType::eOcclusion) writes one 64-bit value counting the samples that passed the depth and stencil tests (an exact count on both backends). - A pipeline-statistics query (
QueryType::ePipelineStatistics) counts vertices, primitives, and shader invocations for vertex-pipeline draws and compute dispatches. - A mesh-pipeline statistics query (
QueryType::eMeshPipelineStatistics) counts only pixel-shader and compute invocations. Use it around mesh draws, because Vulkan forbidsQueryType::ePipelineStatisticsduring mesh draws. - A mesh-primitives query (
QueryType::eMeshPrimitives) counts the primitives output by mesh shaders. It is free on Vulkan (measured on an RTX 3080), but on D3D12 it maps to aPIPELINE_STATISTICS1query, which also counts task and mesh invocations and made a frame 2.3x slower in the same measurement (docs/measurements/statistics/). On GPUs that cannot count it (such as RADV),pipelineStatisticsLayout(device, QueryType::eMeshPrimitives).meshPrimitivesisRI::kNoCounter, andcreateQueryPoolreports the pool type as unsupported. - Because the layout of a counter query's result differs by backend and query type,
RI::pipelineStatisticsLayout(device, type)returns aRI::PipelineStatisticsLayoutholding the result'ssizein bytes and the byte offset of each 64-bit counter (typedefaults toQueryType::ePipelineStatistics). RI does not convert counter layouts; any counter that the query type does not include, or that the GPU lacks, has offsetRI::kNoCounter. The statistics helper reads any counter query result into one struct.
Task and mesh invocation counters are not exposed in RI: on Vulkan (RTX 3080), the mesh invocation counter made a frame 65x slower and the task invocation counter made it 2x slower. Use the native APIs (native access) when you need them.
Copy query results to a buffer with cmdResolveQueries (each occlusion query
result takes 8 bytes; each counter-query result takes layout.size bytes), read them after the
submission reaches its timeline point, and reset the queries with
resetQueries.
Signature¶
// <RI/RI.hpp>, namespace RI
void cmdBeginQuery(CommandList &cmd, const QueryPool &pool, uint32 index);
From the header: Occlusion and pipeline-statistics queries (P71): begin and end around the commands to measure, in the same command list.
// <RI/RI.hpp>, namespace RI
void cmdEndQuery(CommandList &cmd, const QueryPool &pool, uint32 index);
// <RI/RI.hpp>, namespace RI
PipelineStatisticsLayout pipelineStatisticsLayout(Device &device, QueryType type = QueryType::ePipelineStatistics);
From the header: Where each counter is in a result of the counter query type (P71 (2), P105); every counter is kNoCounter for timestamp and occlusion queries.
// <RI/RI.hpp>, namespace RI
PipelineStatisticsLayout pipelineStatisticsLayout(Device &device, QueryType type = QueryType::ePipelineStatistics);
From the header: Where each counter is in a result of the counter query type (P71 (2), P105); every counter is kNoCounter for timestamp and occlusion queries.
From the header: Where each counter of a counter query's result is (P71 (2), P105): byte offsets of 64-bit values inside one query's result of size bytes. The layouts differ per backend; RI reports them instead of converting. A counter the query type does not hold, or the GPU lacks, is kNoCounter.
Example¶
RI::QueryPool statistics = RI::createQueryPool(device, {.type = RI::QueryType::ePipelineStatistics, .count = 1});
const RI::PipelineStatisticsLayout stats = RI::pipelineStatisticsLayout(device);
RI::Buffer results = RI::createBuffer(device, readbackMemory, 1 << 20,
{.size = stats.size, .usage = RI::BufferUsage::eCopyDst});
RI::cmdBegin(cmd);
RI::cmdBeginQuery(cmd, statistics, 0);
// ... draws and dispatches to measure ...
RI::cmdEndQuery(cmd, statistics, 0);
RI::cmdResolveQueries(cmd, statistics, 0, 1, results, 0);
RI::cmdEnd(cmd);
RI::wait(device.graphicsQueue, RI::submit(device.graphicsQueue, cmd));
uint64 computeInvocations = 0;
std::memcpy(&computeInvocations, static_cast<std::byte *>(results.mapped) + stats.computeShaderInvocations, 8);
How RI does it¶
- Vulkan:
vkCmdBeginQueryandvkCmdEndQuery; occlusion queries passVK_QUERY_CONTROL_PRECISE_BIT(RI requiresocclusionQueryPrecise).QueryType::ePipelineStatisticspools request seven base counters (input vertices, input primitives, vertex shader invocations, clipping invocations, clipping primitives, pixel shader invocations, and compute shader invocations), plus geometry and tessellation counters when the GPU supports them; Vulkan writes only the requested counters, in bit order.QueryType::eMeshPipelineStatisticspools request pixel and compute shader invocations (16 bytes).QueryType::eMeshPrimitivesusesVK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT(8 bytes; requiresmeshShaderQueries). - D3D12:
BeginQueryandEndQuerywithD3D12_QUERY_TYPE_OCCLUSION, orD3D12_QUERY_TYPE_PIPELINE_STATISTICSfor bothQueryType::ePipelineStatisticsandQueryType::eMeshPipelineStatistics; the result is the 88-byteD3D12_QUERY_DATA_PIPELINE_STATISTICSstruct, whose vertex counters stay 0 around mesh draws.QueryType::eMeshPrimitivesusesD3D12_QUERY_TYPE_PIPELINE_STATISTICS1(requiresMeshShaderPipelineStatsSupported); the result is the 112-byteD3D12_QUERY_DATA_PIPELINE_STATISTICS1struct, andpipelineStatisticsLayoutsetsmeshPrimitivesto the byte offset ofMSPrimitives.
Rules and errors¶
- A timestamp pool requires
cmdWriteTimestamp, whereas occlusion and statistics pools requirecmdBeginQueryandcmdEndQuery, andindexmust be less thanpool.count(mismatching the pool type or passing an out-of-range index is a programmer error in validation builds). - A query begun inside a rendering section must end before
cmdEndRenderingcloses that section. - On Vulkan, a
QueryType::ePipelineStatisticsquery must not be active during a mesh draw, and only one statistics query (QueryType::ePipelineStatisticsorQueryType::eMeshPipelineStatistics) may be active at a time. The Vulkan validation layer reports both violations. - When using
QueryType::eMeshPrimitivesalongside another statistics query, begin the mesh-primitives query before the statistics query, or measure it alone. If begun inside an open statistics query, NVIDIA's D3D12 driver counts no primitives (nor task or mesh invocations) without any warning from the debug layer; when begun first, both queries count correctly. Validation builds report the reversed nesting order as a programmer error on both backends so the same code works everywhere. - Statistics count all GPU work between begin and end, including the work of RI's own
operations and of the driver: a clear (
LoadOp::eClear,cmdClearTexture) or a layout switch may run as a compute shader on some GPUs. To measure only your draws, begin the query inside the rendering section, aftercmdBeginRendering. - How counters count is up to the GPU. AMD counts pixel-shader invocations per 2x2 quad, so 1024 covered pixels may read 256; helper invocations may be counted elsewhere. Compare counts on one GPU, not across GPUs.