Skip to content

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.

  • beginFrame waits on device.graphicsQueue only if the next CommandList is still in flight on the GPU, resets it, begins recording, and returns a reference to it.
  • endFrame ends recording, submits the CommandList on device.graphicsQueue, advances to the next slot, and returns the submission's TimelinePoint.
  • frameIndex(ring) returns the current slot index (0 to framesInFlight - 1) while recording a frame. Use it between beginFrame and endFrame to 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

  • createFrameRing stores a pointer to the Device you pass in. Keep that Device alive and at the same address until destroyFrameRing returns; do not create a FrameRing from a local Device that you later copy or move.
  • Calling beginFrame twice without an intervening endFrame, or calling endFrame without an active beginFrame, 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]).