Timestamps¶
Measuring GPU time¶
The CPU cannot time GPU work with its own clock: the GPU runs later, and in parallel. Instead, the GPU writes timestamps, counter values taken as it reaches points in a command list. The difference between two timestamps, divided by the counter's frequency, is the GPU time between them.
Timestamps are queries kept in a query pool that you create with
createQueryPool (QueryType::eTimestamp). Measuring a range
of GPU work takes four steps:
- Write a timestamp into a query in the pool before and after the work you want to
measure (
cmdWriteTimestamp). - Resolve the queries into a buffer (
BufferUsage::eCopyDst), writing one 64-bit value per query (cmdResolveQueries). - After the submission's timeline point is reached, read the values from
the mapped
MemoryKind::eReadbackbuffer and divide the tick difference bydevice.limits.timestampFrequencyto get seconds. - Reset the queries before writing to them again (
resetQueries).
// 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});
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);
What a timestamp measures¶
A timestamp is written when every earlier command in the queue has finished. The difference between two timestamps is therefore the time the GPU took to finish the work between them, including any wait at a barrier. Work that overlaps the measured range from outside it is counted too: GPU time is wall time on the GPU, not "the cost of these commands alone".
Only differences are meaningful. Absolute values start at an arbitrary point and may wrap.
Resets¶
A query must be reset before it is written to again:
- A newly created pool starts in the reset state, so its queries can be written immediately.
- After the submission's timeline point is reached and you have read the results, call
resetQueries(device, pool, first, count). This is a CPU call with no GPU cost, and it does nothing on D3D12 because D3D12 queries do not need resets. - To reuse queries within the same command list, call
cmdResetQueriesoutside rendering sections to record the reset into the command list.
Resolving never resets queries, so a range can be resolved again until it is reset.
The frequency¶
device.limits.timestampFrequency is the number of timestamp ticks per second. Vulkan
reports a period (timestampPeriod, in nanoseconds per tick) and RI converts it to a
frequency; D3D12 reports the queue's frequency directly. Because RI v1 has only one queue
(the graphics queue), the frequency is stored on device.limits.
Other queries and the profiler¶
Occlusion and counter queries also use QueryPool (with one QueryType per pool): see
queries. The profiler helper measures
named, nested scopes with timestamps.