Skip to content

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) and RI::cmdEndQuery(cmd, pool, index) begin and end query index in pool, 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 forbids QueryType::ePipelineStatistics during 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 a PIPELINE_STATISTICS1 query, 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).meshPrimitives is RI::kNoCounter, and createQueryPool reports 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 a RI::PipelineStatisticsLayout holding the result's size in bytes and the byte offset of each 64-bit counter (type defaults to QueryType::ePipelineStatistics). RI does not convert counter layouts; any counter that the query type does not include, or that the GPU lacks, has offset RI::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.

// <RI/RI.hpp>, namespace RI
inline constexpr uint32 kNoCounter = ~0u;

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: vkCmdBeginQuery and vkCmdEndQuery; occlusion queries pass VK_QUERY_CONTROL_PRECISE_BIT (RI requires occlusionQueryPrecise). QueryType::ePipelineStatistics pools 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::eMeshPipelineStatistics pools request pixel and compute shader invocations (16 bytes). QueryType::eMeshPrimitives uses VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT (8 bytes; requires meshShaderQueries).
  • D3D12: BeginQuery and EndQuery with D3D12_QUERY_TYPE_OCCLUSION, or D3D12_QUERY_TYPE_PIPELINE_STATISTICS for both QueryType::ePipelineStatistics and QueryType::eMeshPipelineStatistics; the result is the 88-byte D3D12_QUERY_DATA_PIPELINE_STATISTICS struct, whose vertex counters stay 0 around mesh draws. QueryType::eMeshPrimitives uses D3D12_QUERY_TYPE_PIPELINE_STATISTICS1 (requires MeshShaderPipelineStatsSupported); the result is the 112-byte D3D12_QUERY_DATA_PIPELINE_STATISTICS1 struct, and pipelineStatisticsLayout sets meshPrimitives to the byte offset of MSPrimitives.

Rules and errors

  • A timestamp pool requires cmdWriteTimestamp, whereas occlusion and statistics pools require cmdBeginQuery and cmdEndQuery, and index must be less than pool.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 cmdEndRendering closes that section.
  • On Vulkan, a QueryType::ePipelineStatistics query must not be active during a mesh draw, and only one statistics query (QueryType::ePipelineStatistics or QueryType::eMeshPipelineStatistics) may be active at a time. The Vulkan validation layer reports both violations.
  • When using QueryType::eMeshPrimitives alongside 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, after cmdBeginRendering.
  • 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.