Frame ring¶
What it does¶
<RI/Helpers/FrameRing.hpp> provides RI::Helpers::FrameRing. createFrameRing(device, framesInFlight)
creates a ring with framesInFlight CommandList objects (2 by default) and cycles through
them so the CPU can record frame n + 1 while the GPU executes frame n.
beginFramewaits ondevice.graphicsQueueonly if the nextCommandListis still in flight on the GPU, resets it, begins recording, and returns a reference to it.endFrameends recording, submits theCommandListondevice.graphicsQueue, advances to the next slot, and returns the submission'sTimelinePoint.frameIndex(ring)returns the current slot index (0toframesInFlight - 1) while recording a frame. Use it betweenbeginFrameandendFrameto pick your per-frame copy of data that the CPU writes while the GPU may still be reading an earlier frame's copy (such as upload buffers or root data arrays).
Acquiring and presenting swapchain images remain your responsibility (swapchain).
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);
}
}
Shutdown¶
destroyFrameRing waits on device.graphicsQueue for every frame still in flight, destroys the
CommandList objects, and clears the ring. Call it before destroying anything the frames used
(pipelines, buffers, textures, heaps), because GPU resources may only be destroyed after the GPU
has finished using them.
// Waits for every frame still in flight: destroy the ring first, then what the frames used.
RI::Helpers::destroyFrameRing(ring);
RI::destroyPipeline(device, pipeline);
Anything you replace while frames are in flight, such as a depth texture after the window is
resized, follows the same rule: wait for the TimelinePoint values returned by endFrame (or keep
the old resource alive until those points are reached) before destroying it.
Rules and errors¶
createFrameRingstores a pointer to theDeviceyou pass in. Keep thatDevicealive and at the same address untildestroyFrameRingreturns; do not create aFrameRingfrom a localDevicethat you later copy or move.- Calling
beginFrametwice without an interveningendFrame, or callingendFramewithout an activebeginFrame, is reported as a programmer error (ErrorCategory::eProgrammerError).
How to roll your own¶
Keep an array of N CommandList objects alongside each list's last submitted TimelinePoint.
Before reusing list i, call wait(queue, points[i]), resetCommandList(lists[i]), and
cmdBegin(lists[i]). After recording, call cmdEnd(lists[i]) and store
points[i] = submit(queue, lists[i]).