GPU profiler¶
What it does¶
<RI/Helpers/Profiler.hpp> measures named, nested scopes on the GPU.
beginScope and endScope each write a timestamp and open or close a marker region, so
the scopes appear in RenderDoc, PIX, and Nsight (debugging).
createProfiler allocates a query pool and readback buffer for up to maxScopes scopes
per frame (256 by default).
Each frame, call resolveProfiler before cmdEnd to copy the written timestamps into
readback memory; all opened scopes must be closed first. Once the GPU reaches the frame's
TimelinePoint, call profilerResults to read each ScopeTime (name, depth, and
milliseconds) in beginScope order and reset the profiler for the next frame.
Calling beginScope beyond maxScopes, endScope with no scope open, or
resolveProfiler with a scope still open reports a programmer error.
One Profiler serves one frame at a time because it holds a single query pool and
readback buffer. When several frames are in flight, keep one Profiler per frame and
index them with frameIndex(ring) (frame ring).
The profiler keeps a pointer to the Device you created it with: keep the device at the
same address, and alive, until you call destroyProfiler.
Example¶
RI::Helpers::FrameRing ring = RI::Helpers::createFrameRing(device, 2);
RI::Helpers::Profiler profiler = RI::Helpers::createProfiler(device);
RI::TimelinePoint lastFrame;
for (int frame = 0; frame < 3; ++frame) {
RI::CommandList &cmd = RI::Helpers::beginFrame(ring); // waits only if this list is in flight
RI::Helpers::beginScope(profiler, cmd, "frame");
RI::Helpers::beginScope(profiler, cmd, "clear");
RI::cmdClearTexture(cmd, texture.view, {.color = {0.0f, 0.0f, 0.0f, 1.0f}});
RI::Helpers::endScope(profiler, cmd);
RI::Helpers::endScope(profiler, cmd);
RI::Helpers::resolveProfiler(profiler, cmd);
lastFrame = RI::Helpers::endFrame(ring);
// The times are readable once the frame's point is reached.
RI::wait(device.graphicsQueue, lastFrame);
for (const RI::Helpers::ScopeTime &scope : RI::Helpers::profilerResults(profiler)) {
std::printf("%*s%s: %.3f ms\n", int(scope.depth * 2), "", scope.name.c_str(), scope.milliseconds);
}
}
How to roll your own¶
Create a timestamp QueryPool with 2 * maxScopes queries and a MemoryKind::eReadback
buffer sized for uint64 timestamps. Record cmdWriteTimestamp at the start and end of
each scope, along with cmdBeginMarker and cmdEndMarker if you want the scopes in GPU
debuggers, and call cmdResolveQueries before cmdEnd to copy the timestamps into the
readback buffer. Once the frame's TimelinePoint is reached, read the uint64 ticks
from the mapped buffer, convert differences to milliseconds using
device.limits.timestampFrequency (ticks per second), and call resetQueries on the
pool before reusing it on the next frame.