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, typicallyFormat::eRGB10A2Unorm), orColorSpace::eScRgb(linear, typicallyFormat::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), orPresentMode::eImmediate(may tear).RI::createSwapchain(device, desc)creates a swapchain for the window's size, format, and present mode; settingdesc.imageCountto0lets RI choose a default count.swapchain.width,swapchain.height, andswapchain.imageCountreport the actual dimensions and image count created.- Each frame,
RI::acquire(device, swapchain)returns the screen texture (anRI::Texturewith usageTextureUsage::eRenderTarget | TextureUsage::eCopyDst, plusTextureUsage::eStorageifdesc.storageis set), blocking if no image is free. Draw into it like any texture, submit your work to the graphics queue (device.graphicsQueue), and callRI::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¶
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.
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
VkSurfaceKHRfor the window system (Win32, Xlib, XCB, or Wayland) and aVkSwapchainKHR. RI hides the semaphores:acquireacquires an image and submits a prerecorded layout transition that waits on the acquire semaphore and moves the image fromVK_IMAGE_LAYOUT_UNDEFINEDto its resting layout;presentsubmits a layout transition toVK_IMAGE_LAYOUT_PRESENT_SRC_KHRthat signals the image's present semaphore, then callsvkQueuePresentKHR. 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 inD3D12_BARRIER_LAYOUT_COMMON(which is also the present layout), so RI's layout transitions around rendering sections and clears leave them ready to present.acquirewaits on the fence until the current back buffer's last frame is done;presentcallsIDXGISwapChain::Presentand signals the fence. Neither submits a command list of its own. sRGB formats use aUNORMbuffer with sRGB render-target views, as flip model requires.PresentMode::eFifopresents with sync interval 1,PresentMode::eMailboxwith sync interval 0, andPresentMode::eImmediatewith sync interval 0 andDXGI_PRESENT_ALLOW_TEARINGwhen 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 whetherdesc.storage(usageTextureUsage::eStorage) is supported. D3D12 never allows it, reportingErrorCategory::eUnsupportedifdesc.storageis 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
acquireandpresentin pairs: callingacquiretwice withoutpresent, or callingpresentwithout a matchingacquire, reportsErrorCategory::eProgrammerError. - Passing a format, color space, or present mode that the window does not support to
createSwapchainreportsErrorCategory::eUnsupported; checksurfaceFormatsandpresentModeSupportedfirst. - An out-of-date swapchain (for example, after the window changes) is reported on the
error channel as
ErrorCategory::eSwapchainOutOfDate; callresizeSwapchainto recreate it. - D3D12 accepts
WindowSystem::eWin32windows only, reportingErrorCategory::eUnsupportedfor other window systems.