Skip to content

RI::cmdWriteTimestamp, RI::cmdResolveQueries, RI::cmdResetQueries

What it does

These three functions record timestamp and query operations into a command list (see Timestamps):

  • RI::cmdWriteTimestamp(cmd, pool, index) writes the GPU's tick count into query index of a QueryType::eTimestamp pool once every earlier command has finished. It is allowed both inside and outside rendering sections.
  • RI::cmdResolveQueries(cmd, pool, first, count, buffer, offset) copies count query results starting at first into buffer at byte offset offset: one 64-bit value per query for timestamps and occlusion, and pipelineStatisticsLayout(device, pool.type).size bytes per query for statistics and counter types. It waits on the GPU for the queries to be written and never resets them.
  • RI::cmdResetQueries(cmd, pool, first, count) resets count queries starting at first in the command list so they can be written again later in the same list (a no-op on D3D12, where queries need no reset). Most programs reset on the CPU instead, with resetQueries, after reading the results.

To read the results on the CPU, resolve into a buffer backed by MemoryKind::eReadback memory, wait until the submission reaches its timeline point, and divide tick differences by device.limits.timestampFrequency (DeviceLimits) to convert ticks to seconds.

Signature

// <RI/RI.hpp>, namespace RI
void cmdWriteTimestamp(CommandList &cmd, const QueryPool &pool, uint32 index);

From the header: Timestamps (P51, P71): written once all earlier work has finished.

// <RI/RI.hpp>, namespace RI
void cmdResolveQueries(CommandList &cmd, const QueryPool &pool, uint32 first, uint32 count, const Buffer &buffer,
                       uint64 offset);

From the header: Copies count results to buffer at offset (a multiple of 8): one 64-bit value per timestamp or occlusion query, pipelineStatisticsLayout(device, type).size bytes per query of a counter type (P105).

// <RI/RI.hpp>, namespace RI
void cmdResetQueries(CommandList &cmd, const QueryPool &pool, uint32 first, uint32 count);

Example

RI::cmdWriteTimestamp(cmd, timestamps, 0); // when all earlier work is done
RI::cmdWriteTimestamp(cmd, timestamps, 1);
// Copy both results (64-bit each) to the buffer.
RI::cmdResolveQueries(cmd, timestamps, 0, 2, ticks, 0);
// 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);

Resetting in a command list:

// Or reset them in a command list, outside rendering sections.
RI::resetCommandList(cmd);
RI::cmdBegin(cmd);
RI::cmdResetQueries(cmd, timestamps, 2, 2);
RI::cmdWriteTimestamp(cmd, timestamps, 2);
RI::cmdEnd(cmd);
RI::wait(device.graphicsQueue, RI::submit(device.graphicsQueue, cmd));

The same in native code

RI::cmdWriteTimestamp(cmd, timestamps, 1);
// Copy both results (64-bit each) to the buffer.
RI::cmdResolveQueries(cmd, timestamps, 0, 2, ticks, 0);
// ALL_COMMANDS: written once every earlier command has finished.
vkCmdWriteTimestamp2(cmd, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, queries, 0);
vkCmdWriteTimestamp2(cmd, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, queries, 1);
// 64-bit results; WAIT: the copy waits until both timestamps are written.
vkCmdCopyQueryPoolResults(cmd, queries, 0, 2, ticks, 0, sizeof(uint64_t),
                          VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
// Make the copy visible to the CPU.
const VkMemoryBarrier2 toHost{.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
                              .srcStageMask = VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
                              .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT,
                              .dstStageMask = VK_PIPELINE_STAGE_2_HOST_BIT,
                              .dstAccessMask = VK_ACCESS_2_HOST_READ_BIT};
const VkDependencyInfo hostDependency{
    .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &toHost};
vkCmdPipelineBarrier2(cmd, &hostDependency);
const auto *values = static_cast<const uint64_t *>(ticksMapped);
std::printf("GPU time: %.3f ms\n", static_cast<double>(values[1] - values[0]) * 1000.0 / ticksPerSecond);
// A query must be reset before it is written again: on the CPU, or with
// vkCmdResetQueryPool in a command buffer (outside a render pass).
vkResetQueryPool(device, queries, 0, 2);

What to watch for: vkCmdWriteTimestamp2 takes a pipeline stage; VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT waits for all earlier work. vkCmdCopyQueryPoolResults needs VK_QUERY_RESULT_64_BIT (otherwise results are 32-bit) and VK_QUERY_RESULT_WAIT_BIT (otherwise unfinished queries are skipped). Because the copy is a transfer write, you must make it visible to the CPU with a memory barrier to VK_PIPELINE_STAGE_2_HOST_BIT (RI's cmdEnd inserts this barrier for you).

cmd->EndQuery(queries.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 0); // timestamps have no BeginQuery
cmd->EndQuery(queries.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 1);
// 64-bit results to the buffer. No reset step: D3D12 query heaps need none.
cmd->ResolveQueryData(queries.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, ticks.Get(), 0);
void *mapped = nullptr;
const D3D12_RANGE readRange{0, 2 * sizeof(UINT64)};
check(ticks->Map(0, &readRange, &mapped), "Map");
const auto *values = static_cast<const UINT64 *>(mapped);
std::printf("GPU time: %.3f ms\n",
            static_cast<double>(values[1] - values[0]) * 1000.0 / static_cast<double>(ticksPerSecond));

What to watch for: a timestamp is recorded with EndQuery and no matching BeginQuery. ResolveQueryData writes 64-bit values, and the destination buffer offset must be a multiple of 8.

Rules and errors

  • cmdWriteTimestamp: pool must have type QueryType::eTimestamp, and index must be less than pool.count (programmer error in validation builds on both backends).
  • cmdResolveQueries: buffer must have BufferUsage::eCopyDst (programmer error in all builds on both backends). offset must be a multiple of 8, buffer must have at least count * stride bytes available from offset (where stride is 8 bytes for timestamps and occlusion, or pipelineStatisticsLayout(device, pool.type).size bytes for counter types), and first + count must be at most pool.count (programmer error in validation builds on both backends).
  • cmdResetQueries: first + count must be at most pool.count (programmer error in validation builds on Vulkan; on D3D12 cmdResetQueries is a no-op).
  • cmdResolveQueries and cmdResetQueries are not allowed inside rendering sections (programmer error in validation builds on both backends for cmdResolveQueries, and on Vulkan for cmdResetQueries).
  • A query must be reset between two writes on Vulkan (D3D12 needs no reset, and cmdResetQueries and resetQueries do nothing there).
  • No barrier is needed between cmdWriteTimestamp and cmdResolveQueries, nor before the CPU reads the buffer after the timeline point (cmdEnd makes GPU writes visible to the CPU).