Skip to content

Rendering sections

What a rendering section is

Draws take place inside a rendering section, a stretch of the command list opened by cmdBeginRendering and closed by cmdEndRendering. Most sections bind attachments—up to 8 color textures and one depth (and stencil) texture—though you can also open a section without attachments by passing a RenderingSize when pixel shaders write only to storage views. There are no render pass or framebuffer objects: you describe the section inline where you record it.

// Clear both attachments at the start of the section and keep the results.
RI::cmdBeginRendering(cmd, {
    .colors = {{.view = color.view, .load = RI::LoadOp::eClear, .clear = {.color = {0.1f, 0.1f, 0.1f, 1.0f}}}},
    .depth = RI::DepthAttachment{.view = depth.view,
                                 .load = RI::LoadOp::eClear,
                                 .stencilLoad = RI::LoadOp::eClear,
                                 .clear = {.depth = 1.0f, .stencil = 0}},
});
RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, DrawRoot{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f});
RI::cmdDraw(cmd, 3); // three vertices, one instance
// Draw again into the top-left quarter only; the next section starts with
// the whole attachments again.
RI::cmdSetViewport(cmd, {.x = 0.0f, .y = 0.0f, .width = 128.0f, .height = 128.0f});
RI::cmdSetScissor(cmd, {.x = 0, .y = 0, .width = 128, .height = 128});
RI::cmdDraw(cmd, 3);
RI::cmdEndRendering(cmd);

Each attachment is a texture view of a texture created with TextureUsage::eRenderTarget (color) or TextureUsage::eDepthStencil (depth).

Load and store operations

Each attachment specifies what happens to its contents at the start and at the end of the section.

At the start (LoadOp) Meaning
eLoad (default) keep what the texture holds
eClear set every texel to the attachment's clear value
eDontCare the contents are not needed, because the draws will cover every texel
At the end (StoreOp) Meaning
eStore (default) keep what the section wrote
eDontCare the results are not needed after the section (for example, a depth buffer used only for this pass)

Use eClear or eDontCare whenever you do not need the previous contents, so the GPU can skip reading them. A depth-stencil attachment has separate operations for its stencil aspect: stencilLoad and stencilStore.

Clear values. ClearValue::color holds four floats for color attachments; for unsigned integer formats, pass whole numbers (for example, 2.0f clears to 2). ClearValue::depth (default 1.0f) and ClearValue::stencil (default 0) set the clear values for the depth attachment.

Load operations do not replace a discard: a texture that takes over another resource's memory must be discarded with cmdDiscard first, even if its section clears it.

Viewport and scissor

At the start of every section, RI sets the viewport and the scissor to cover the attachments: from (0, 0) to the size of the smallest attachment (or the given RenderingSize), with depth from 0 to 1. A view of mip 1 of a 1024×1024 texture is 512×512, so the section's default viewport and scissor are 512×512. Inside the section, you can change them with cmdSetViewport and cmdSetScissor (for example, for split-screen, shadow-atlas tiles, or clipped UI); they are reset to cover the attachments again when the next section begins.

Clip space points up on both backends

The same vertex shader produces the same image on both backends. In clip space, x goes right, y goes up, and depth goes from 0 (near) to 1 (far), as in D3D12; the top-left texel of a texture is at clip-space (-1, 1).

Vulkan's clip space has y pointing down. RI flips it with a negative viewport height (a standard Vulkan 1.1 feature): the viewport starts at the bottom of the target and has height -height. The projected winding of triangles is flipped with it, so frontCounterClockwise means the same thing on both backends.

Read-only depth

Some effects, such as soft particles or decals, test against the depth buffer while sampling it in the pixel shader. Normally a texture cannot be an attachment and a shader input at once. Set depthReadOnly (and stencilReadOnly, for a format with stencil) on the depth attachment, and the section uses a read-only depth layout that allows both. A texture sampled this way must be created with TextureUsage::eReadOnlyDepthSampling:

// Depth stays bound for testing while shaders sample it (P75; the texture
// has eReadOnlyDepthSampling, P97). Draws in this section must not write
// depth; loads keep the contents.
RI::cmdBeginRendering(cmd, {
    .colors = {{.view = color.view}}, // eLoad / eStore: keep what is there
    .depth = RI::DepthAttachment{.view = depth.view, .depthReadOnly = true, .stencilReadOnly = true},
});
// ... draws whose pipeline has depth.write == false, sampling depthOnly ...
RI::cmdEndRendering(cmd);

In a read-only depth section:

  • pipelines must not write depth (DepthState::write must be false, whatever test is). In validation builds, setting a depth-writing pipeline is a programmer error: cmdSetPipeline: the pipeline writes depth, but the rendering section's depth is read-only (P75);
  • keep the load operation at eLoad (the default): a read-only attachment is not cleared;
  • nothing is stored for the read-only aspects, whatever the store operation says;
  • shaders may sample the depth texture only if it has TextureUsage::eReadOnlyDepthSampling. Without that flag, a read-only section is still allowed for depth testing alone, but shaders must not sample the texture inside it: on Vulkan without unified layouts, a sampled depth texture rests in the layout fastest for ordinary sampling, which is not the read-only depth layout, and RI switches it around the section. RI cannot see bindless reads, so this rule is not checked.

Around the section: RI's layout switches

cmdBeginRendering switches each attachment into its attachment layout when the backend needs it, and cmdEndRendering switches it back to its resting layout. You do not write any layout transitions yourself; see Image layouts are RI's business. What you do write is the barrier between the section and the commands that read its results:

// The section's writes, then shaders that sample color and depth.
RI::cmdBarrier(cmd, RI::Stage::eRenderTarget | RI::Stage::eDepthStencil, RI::Stage::eCompute | RI::Stage::ePixel);

Inside a section

Inside a rendering section, you can bind graphics pipelines and state (cmdSetPipeline with a graphics pipeline whose attachment formats and sample count match the section, cmdSetHeap, cmdSetRootData, cmdSetViewport, cmdSetScissor, cmdSetStencilReference, cmdSetIndexBuffer), record draws (cmdDraw, cmdDrawIndexed, cmdDrawMesh, and graphics cmdExecuteIndirect calls), and record timestamps, queries, or debug markers.

Operations that modify resources outside rasterization or change the section are not allowed: clears, copies, discards, cmdResolve, query resets and resolves, and another cmdBeginRendering. In validation builds these are reported as programmer errors—for example, cmdClearTexture: not allowed inside a rendering section (cmdEndRendering first). Barriers belong outside sections too: end the section, record the barrier, and begin the next one.

In native code

// Clear both attachments at the start of the section and keep the results.
RI::cmdBeginRendering(cmd, {
    .colors = {{.view = color.view, .load = RI::LoadOp::eClear, .clear = {.color = {0.1f, 0.1f, 0.1f, 1.0f}}}},
    .depth = RI::DepthAttachment{.view = depth.view,
                                 .load = RI::LoadOp::eClear,
                                 .stencilLoad = RI::LoadOp::eClear,
                                 .clear = {.depth = 1.0f, .stencil = 0}},
});
RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, DrawRoot{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f});
RI::cmdDraw(cmd, 3); // three vertices, one instance
// Draw again into the top-left quarter only; the next section starts with
// the whole attachments again.
RI::cmdSetViewport(cmd, {.x = 0.0f, .y = 0.0f, .width = 128.0f, .height = 128.0f});
RI::cmdSetScissor(cmd, {.x = 0, .y = 0, .width = 128, .height = 128});
RI::cmdDraw(cmd, 3);
RI::cmdEndRendering(cmd);

Vulkan uses dynamic rendering (vkCmdBeginRendering, Vulkan 1.3), with the layout transitions written by hand:

// Into attachment layouts. The old layout is UNDEFINED because both are
// cleared: their previous contents are not needed.
transition(cmd, color, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
           VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_NONE,
           VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
           VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT);
transition(cmd, depth, VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
           VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
           VK_ACCESS_2_NONE, kDepthStages,
           VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);

VkRenderingAttachmentInfo colorAttachment{
    .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
    .imageView = colorView,
    .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
    .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
    .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
};
colorAttachment.clearValue.color = {{0.1f, 0.1f, 0.1f, 1.0f}};
VkRenderingAttachmentInfo depthAttachment{
    .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
    .imageView = depthView,
    .imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
    .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
    .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
};
depthAttachment.clearValue.depthStencil = {.depth = 1.0f, .stencil = 0};
const VkRenderingInfo rendering{
    .sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
    .renderArea = {.offset = {0, 0}, .extent = {kWidth, kHeight}},
    .layerCount = 1,
    .colorAttachmentCount = 1,
    .pColorAttachments = &colorAttachment,
    .pDepthAttachment = &depthAttachment,
    .pStencilAttachment = &depthAttachment, // the same view: its stencil aspect
};
vkCmdBeginRendering(cmd, &rendering);

// A negative height flips y, so clip space points up as in D3D12.
const VkViewport viewport{.x = 0.0f,
                          .y = static_cast<float>(kHeight),
                          .width = static_cast<float>(kWidth),
                          .height = -static_cast<float>(kHeight),
                          .minDepth = 0.0f,
                          .maxDepth = 1.0f};
const VkRect2D scissor{.offset = {0, 0}, .extent = {kWidth, kHeight}};
vkCmdSetViewport(cmd, 0, 1, &viewport);
vkCmdSetScissor(cmd, 0, 1, &scissor);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
const DrawRoot root{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f};
vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_ALL, 0, sizeof(root), &root);
vkCmdDraw(cmd, 3, 1, 0, 0);
vkCmdEndRendering(cmd);
// Out of attachment layouts, into the layouts shaders read: this barrier
// also makes the attachment writes visible to the pixel and compute shaders.
transition(cmd, color, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
           VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
           VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
           VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
           VK_ACCESS_2_SHADER_SAMPLED_READ_BIT);
transition(cmd, depth, VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
           VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
           kDepthStages, VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
           VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | kDepthStages,
           VK_ACCESS_2_SHADER_SAMPLED_READ_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT);

D3D12 has no section object: render targets are bound, cleared with commands, and drawn to; the transitions are texture barriers:

// The textures are in RENDER_TARGET and DEPTH_STENCIL_WRITE already (above);
// otherwise, a texture barrier into those layouts comes first.
cmd->OMSetRenderTargets(1, &rtv, FALSE, &dsv);
const float clearColor[4] = {0.1f, 0.1f, 0.1f, 1.0f};
cmd->ClearRenderTargetView(rtv, clearColor, 0, nullptr); // clears are commands, not load ops
cmd->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
const D3D12_VIEWPORT viewport{0.0f, 0.0f, static_cast<float>(kWidth), static_cast<float>(kHeight), 0.0f, 1.0f};
const D3D12_RECT scissor{0, 0, static_cast<LONG>(kWidth), static_cast<LONG>(kHeight)};
cmd->RSSetViewports(1, &viewport);
cmd->RSSetScissorRects(1, &scissor);
ID3D12DescriptorHeap *heaps[] = {viewHeap.Get()};
cmd->SetDescriptorHeaps(1, heaps); // before the root signature that indexes it
cmd->SetGraphicsRootSignature(rootSignature.Get());
cmd->SetPipelineState(pipeline.Get());
cmd->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
const DrawRoot root{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f};
cmd->SetGraphicsRoot32BitConstants(0, sizeof(root) / 4, &root, 0);
cmd->DrawInstanced(3, 1, 0, 0);
// There is no "end": the next barrier moves the textures to where shaders
// read them, and makes the attachment writes visible.
textureBarrier(cmd.Get(), color.Get(), D3D12_BARRIER_LAYOUT_RENDER_TARGET, D3D12_BARRIER_LAYOUT_SHADER_RESOURCE,
               D3D12_BARRIER_SYNC_RENDER_TARGET, D3D12_BARRIER_ACCESS_RENDER_TARGET,
               D3D12_BARRIER_SYNC_PIXEL_SHADING | D3D12_BARRIER_SYNC_COMPUTE_SHADING,
               D3D12_BARRIER_ACCESS_SHADER_RESOURCE);
textureBarrier(cmd.Get(), depth.Get(), D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE,
               D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ, D3D12_BARRIER_SYNC_DEPTH_STENCIL,
               D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE,
               D3D12_BARRIER_SYNC_PIXEL_SHADING | D3D12_BARRIER_SYNC_DEPTH_STENCIL,
               D3D12_BARRIER_ACCESS_SHADER_RESOURCE | D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ);