Skip to content

RI::createSwapchain, RI::acquire, RI::present

What it does

A swapchain shows rendered frames in a window. RI creates no window; you create the window yourself and pass its OS handles as an RI::Window (that page shows how to fill one from a GLFW window).

  • RI::surfaceFormats(device, window) lists the (format, color space) pairs the window can show: ColorSpace::eSrgb, ColorSpace::eHdr10 (PQ, typically Format::eRGB10A2Unorm), or ColorSpace::eScRgb (linear, typically Format::eRGBA16Float). RI::presentModeSupported(device, window, mode) reports whether a present mode is available: PresentMode::eFifo (vertical sync; always available), PresentMode::eMailbox (no tearing; the newest frame replaces any waiting frame), or PresentMode::eImmediate (may tear).
  • RI::createSwapchain(device, desc) creates a swapchain for the window's size, format, and present mode; setting desc.imageCount to 0 lets RI choose a default count. swapchain.width, swapchain.height, and swapchain.imageCount report the actual dimensions and image count created.
  • Each frame, RI::acquire(device, swapchain) returns the screen texture (an RI::Texture with usage TextureUsage::eRenderTarget | TextureUsage::eCopyDst, plus TextureUsage::eStorage if desc.storage is set), blocking if no image is free. Draw into it like any texture, submit your work to the graphics queue (device.graphicsQueue), and call RI::present(queue, swapchain). You do not need to track an image index or copy from a separate render target.
  • RI::resizeSwapchain(device, swapchain, width, height) waits for the GPU to finish pending work and recreates the swapchain images at the new size. You decide when to call it—for example, after the window changes size or after an out-of-date error is reported.
  • RI::destroySwapchain(device, swapchain) waits for the GPU to finish pending work and destroys the swapchain.

Swapchain images are not frames in flight: how many frames the CPU records ahead of the GPU is up to you, controlled with RI::TimelinePoint waits; waiting on a single command list each frame is the simplest correct loop.

Signature

// <RI/RI.hpp>, namespace RI
Swapchain createSwapchain(Device &device, const SwapchainDesc &desc);
// <RI/RI.hpp>, namespace RI
Texture acquire(Device &device, Swapchain &swapchain);

From the header: The screen texture to draw into this frame (P63 (2)): usage eRenderTarget | eCopyDst (and eStorage if asked). Blocks while no image is free. Its contents are undefined after acquire.

// <RI/RI.hpp>, namespace RI
void present(Queue &queue, Swapchain &swapchain);

From the header: Shows the acquired texture once the work submitted before it has finished.

// <RI/RI.hpp>, namespace RI
void resizeSwapchain(Device &device, Swapchain &swapchain, uint32 width, uint32 height);

From the header: Waits for the GPU, then recreates the images at the new size (P43: the user decides when, e.g. after a window resize or an out-of-date report).

// <RI/RI.hpp>, namespace RI
struct SwapchainDesc {
    Window window;
    uint32 width = 0; // the window's client size in pixels
    uint32 height = 0;
    SurfaceFormat format; // one of surfaceFormats(device, window)
    PresentMode presentMode = PresentMode::eFifo;
    uint32 imageCount = 0; // 0: RI's default for the present mode (P43)
    // Compute writes to the screen texture (eStorage): only where
    // screenStorageSupported says so; never on D3D12 (P64 (5)).
    bool storage = false;
    std::string_view name; // empty: unnamed (P106)
};
Field Type Default Description
window Window
width uint32 0 the window's client size in pixels
height uint32 0
format SurfaceFormat one of surfaceFormats(device, window)
presentMode PresentMode PresentMode::eFifo
imageCount uint32 0 0: RI's default for the present mode (P43)
storage bool false Compute writes to the screen texture (eStorage): only where screenStorageSupported says so; never on D3D12 (P64 (5)).
name std::string_view empty: unnamed (P106)
// <RI/RI.hpp>, namespace RI
std::vector<SurfaceFormat> surfaceFormats(Device &device, const Window &window);

From the header: The (format, color space) pairs the window can show (P64 (1)).

Example

// RI creates no window: GLFW does, and windowOf gives RI its OS handles.
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow *window = glfwCreateWindow(1280, 720, "EquineRI", nullptr, nullptr);
const RI::Window osWindow = windowOf(window);

// Pick a format the window shows: sRGB 8-bit if possible (P64).
const std::vector<RI::SurfaceFormat> formats = RI::surfaceFormats(device, osWindow);
const RI::SurfaceFormat srgb{RI::Format::eBGRA8Srgb, RI::ColorSpace::eSrgb};
const RI::SurfaceFormat format = std::ranges::find(formats, srgb) != formats.end() ? srgb : formats.front();
int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
RI::Swapchain swapchain = RI::createSwapchain(device, {.window = osWindow,
                                                       .width = static_cast<uint32>(width),
                                                       .height = static_cast<uint32>(height),
                                                       .format = format,
                                                       .presentMode = RI::PresentMode::eFifo});
while (glfwWindowShouldClose(window) == GLFW_FALSE) {
    glfwPollEvents();
    int newWidth = 0, newHeight = 0;
    glfwGetFramebufferSize(window, &newWidth, &newHeight);
    if (newWidth == 0 || newHeight == 0) {
        continue; // minimized
    }
    if (static_cast<uint32>(newWidth) != swapchain.width || static_cast<uint32>(newHeight) != swapchain.height) {
        RI::resizeSwapchain(device, swapchain, static_cast<uint32>(newWidth), static_cast<uint32>(newHeight));
    }
    // The screen texture of this frame: draw into it like any texture.
    RI::Texture screen = RI::acquire(device, swapchain);
    RI::wait(device.graphicsQueue, last); // one command list: wait before reusing it
    RI::resetCommandList(cmd);
    RI::cmdBegin(cmd);
    RI::cmdBeginRendering(cmd, {.colors = {{.view = screen.view,
                                             .load = RI::LoadOp::eClear,
                                             .clear = {.color = {0.1f, 0.2f, 0.4f, 1.0f}}}}});
    // ... draws ...
    RI::cmdEndRendering(cmd);
    RI::cmdEnd(cmd);
    last = RI::submit(device.graphicsQueue, cmd);
    RI::present(device.graphicsQueue, swapchain); // after the work submitted before it
}
// Before destroying anything the frames used, wait for the last one.
RI::wait(device.graphicsQueue, last);

How RI does it

  • Vulkan: creates a VkSurfaceKHR for the window system (Win32, Xlib, XCB, or Wayland) and a VkSwapchainKHR. RI hides the semaphores: acquire acquires an image and submits a prerecorded layout transition that waits on the acquire semaphore and moves the image from VK_IMAGE_LAYOUT_UNDEFINED to its resting layout; present submits a layout transition to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR that signals the image's present semaphore, then calls vkQueuePresentKHR. Both internal submissions run in submission order on the graphics queue (before and after your work, respectively) and advance its timeline. The default image count is the surface's minimum plus one, clamped to the surface's supported range.
  • D3D12: creates a flip-model DXGI swapchain (DXGI_SWAP_EFFECT_FLIP_DISCARD, 3 buffers by default). Back buffers rest in D3D12_BARRIER_LAYOUT_COMMON (which is also the present layout), so RI's layout transitions around rendering sections and clears leave them ready to present. acquire waits on the fence until the current back buffer's last frame is done; present calls IDXGISwapChain::Present and signals the fence. Neither submits a command list of its own. sRGB formats use a UNORM buffer with sRGB render-target views, as flip model requires. PresentMode::eFifo presents with sync interval 1, PresentMode::eMailbox with sync interval 0, and PresentMode::eImmediate with sync interval 0 and DXGI_PRESENT_ALLOW_TEARING when tearing is supported.

Rules and errors

  • The screen texture's contents are undefined after acquire: clear it or overwrite it completely before presenting.
  • Compute writes to the screen are not portable: RI::screenStorageSupported(device, window, format) reports whether desc.storage (usage TextureUsage::eStorage) is supported. D3D12 never allows it, reporting ErrorCategory::eUnsupported if desc.storage is set. To write the screen from compute on every backend, write into a normal texture and copy it into the screen texture.
  • Call all swapchain functions on the device thread (the thread that called createDevice).
  • Call acquire and present in pairs: calling acquire twice without present, or calling present without a matching acquire, reports ErrorCategory::eProgrammerError.
  • Passing a format, color space, or present mode that the window does not support to createSwapchain reports ErrorCategory::eUnsupported; check surfaceFormats and presentModeSupported first.
  • An out-of-date swapchain (for example, after the window changes) is reported on the error channel as ErrorCategory::eSwapchainOutOfDate; call resizeSwapchain to recreate it.
  • D3D12 accepts WindowSystem::eWin32 windows only, reporting ErrorCategory::eUnsupported for other window systems.