Skip to content

RI::createQueryPool, RI::destroyQueryPool, RI::resetQueries

What it does

A query pool holds desc.count queries of a single QueryType that command lists can write into:

  • QueryType::eTimestamp: stores one 64-bit GPU tick count per query (see Timestamps).
  • QueryType::eOcclusion: stores one 64-bit count of samples that passed the depth and stencil tests.
  • QueryType::ePipelineStatistics: stores pipeline counter values for classic vertex-shader draws and compute dispatches.
  • QueryType::eMeshPipelineStatistics: stores pixel-shader and compute invocation counts, which are allowed around mesh draws where Vulkan forbids ePipelineStatistics.
  • QueryType::eMeshPrimitives: counts the primitives output by mesh shaders. On GPUs that cannot count mesh primitives, RI::createQueryPool reports an unsupported error.

Occlusion and counter queries (eOcclusion, ePipelineStatistics, eMeshPipelineStatistics, and eMeshPrimitives) are recorded with cmdBeginQuery / cmdEndQuery. Counter query results are laid out according to pipelineStatisticsLayout(device, type).

Use these functions to manage a pool's lifetime and reset state:

  • RI::createQueryPool(device, desc) creates a pool whose queries are already reset, so every query can be written immediately without an initial reset.
  • RI::resetQueries(device, pool, first, count) resets queries first through first + count - 1 on the CPU after their results have been read. It submits no GPU work, and it does nothing on D3D12 because D3D12 queries do not need to be reset.
  • RI::destroyQueryPool(device, pool) destroys the pool.

To record and read queries, write timestamps with cmdWriteTimestamp, copy query results to a buffer with cmdResolveQueries, and reset queries inside a command list with cmdResetQueries.

Signature

// <RI/RI.hpp>, namespace RI
QueryPool createQueryPool(Device &device, const QueryPoolDesc &desc);

From the header: Created reset (P71 (1)).

// <RI/RI.hpp>, namespace RI
void destroyQueryPool(Device &device, QueryPool &pool);
// <RI/RI.hpp>, namespace RI
void resetQueries(Device &device, QueryPool &pool, uint32 first, uint32 count);

From the header: Resets queries on the CPU, after their results were read (P71 (1)). No GPU cost.

// <RI/RI.hpp>, namespace RI
struct QueryPool {
    QueryType type = QueryType::eTimestamp;
    uint32 count = 0;
    // internal: backend-private state (not part of the API)
};
Field Type Default Description
type QueryType QueryType::eTimestamp
count uint32 0
// <RI/RI.hpp>, namespace RI
struct QueryPoolDesc {
    QueryType type = QueryType::eTimestamp;
    uint32 count = 0;
    std::string_view name; // empty: unnamed (P106)
};
Field Type Default Description
type QueryType QueryType::eTimestamp
count uint32 0
name std::string_view empty: unnamed (P106)
// <RI/RI.hpp>, namespace RI
enum class QueryType : uint8 {
    eTimestamp,          // one 64-bit tick count per query; ticks per second in DeviceLimits
    eOcclusion,          // one 64-bit count of samples that passed the depth and stencil tests
    ePipelineStatistics, // the classic counters, laid out as pipelineStatisticsLayout says (P71 (2))
    // P105: Vulkan forbids the classic counters during mesh draws. These two
    // are the counters that cost nothing measurable there; task and mesh
    // invocations stay native (they cost up to 65x on Vulkan).
    eMeshPipelineStatistics, // pixel and compute invocations, allowed around mesh draws
    eMeshPrimitives,         // primitives output by mesh shaders; D3D12 also counts task and mesh
                             // invocations with it (+130% measured). Unsupported where the GPU lacks it.
};
Value Meaning
eTimestamp one 64-bit tick count per query; ticks per second in DeviceLimits
eOcclusion one 64-bit count of samples that passed the depth and stencil tests
ePipelineStatistics the classic counters, laid out as pipelineStatisticsLayout says (P71 (2))
eMeshPipelineStatistics pixel and compute invocations, allowed around mesh draws
eMeshPrimitives primitives output by mesh shaders; D3D12 also counts task and mesh

Example

// A pool of timestamp queries; it starts reset.
RI::QueryPool timestamps = RI::createQueryPool(device, {.type = RI::QueryType::eTimestamp, .count = 4});
// Results go to a buffer: here readback memory, read by the CPU.
RI::Memory readback = RI::allocateMemory(device, RI::MemoryKind::eReadback, 4096);
RI::Buffer ticks = RI::createBuffer(device, readback, 0, {.size = 4 * sizeof(uint64), .usage = RI::BufferUsage::eCopyDst});
// After the timeline point: ticks to milliseconds with the device's frequency.
const auto *values = static_cast<const uint64 *>(ticks.mapped);
const double ms = static_cast<double>(values[1] - values[0]) * 1000.0 /
                  static_cast<double>(device.limits.timestampFrequency);
std::printf("GPU time: %.3f ms\n", ms);
// Reset the queries before writing them again (a CPU call, no GPU cost).
RI::resetQueries(device, timestamps, 0, 2);

The same in native code

// A pool of timestamp queries; it starts reset.
RI::QueryPool timestamps = RI::createQueryPool(device, {.type = RI::QueryType::eTimestamp, .count = 4});
// Results go to a buffer: here readback memory, read by the CPU.
RI::Memory readback = RI::allocateMemory(device, RI::MemoryKind::eReadback, 4096);
RI::Buffer ticks = RI::createBuffer(device, readback, 0, {.size = 4 * sizeof(uint64), .usage = RI::BufferUsage::eCopyDst});
const VkQueryPoolCreateInfo queryInfo{
    .sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, .queryType = VK_QUERY_TYPE_TIMESTAMP, .queryCount = 4};
VkQueryPool queries = VK_NULL_HANDLE;
check(vkCreateQueryPool(device, &queryInfo, nullptr, &queries), "vkCreateQueryPool");
vkResetQueryPool(device, queries, 0, 4); // queries start unusable until reset (hostQueryReset)
// Ticks to seconds: timestampPeriod is nanoseconds per tick.
VkPhysicalDeviceProperties properties{};
vkGetPhysicalDeviceProperties(gpu, &properties);
const double ticksPerSecond = 1e9 / static_cast<double>(properties.limits.timestampPeriod);

What to watch for: a new Vulkan query pool is not reset on creation, so you must reset its queries before their first use (vkResetQueryPool from the CPU requires hostQueryReset, which is core in Vulkan 1.2 and required by RI). Writing to a query twice without a reset in between is invalid.

const D3D12_QUERY_HEAP_DESC queryDesc{.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP, .Count = 4};
ComPtr<ID3D12QueryHeap> queries;
check(device->CreateQueryHeap(&queryDesc, IID_PPV_ARGS(&queries)), "CreateQueryHeap");
// Ticks per second, per queue.
UINT64 ticksPerSecond = 0;
check(queue->GetTimestampFrequency(&ticksPerSecond), "GetTimestampFrequency");

What to watch for: a query heap needs no reset. The timestamp frequency belongs to the queue (GetTimestampFrequency).

Rules and errors

  • Reset queries only after the GPU is done with them (after the timeline point of the submission that wrote or resolved them).
  • In RI::resetQueries, first and count must stay within the pool (first + count <= pool.count). On Vulkan, an out-of-range slice reports a programmer error in validation builds.
  • RI::createQueryPool with QueryType::eMeshPrimitives reports an unsupported error if the GPU cannot count mesh primitives.
  • Destroy a pool only after the GPU has finished using it. QueryPool is a thin handle value: copies refer to the same underlying pool, and destroying one invalidates all copies.
  • Occlusion and counter queries (eOcclusion, ePipelineStatistics, eMeshPipelineStatistics, and eMeshPrimitives) are written with cmdBeginQuery and cmdEndQuery (queries).