Skip to content

RI::cmdBeginRendering, RI::cmdEndRendering

What it does

RI::cmdBeginRendering(cmd, desc) opens a rendering section: the draws recorded until RI::cmdEndRendering(cmd) write to the attachments listed in desc. See Rendering sections for the ideas; this page lists the details.

RenderingDesc lists:

  • colors: up to 8 ColorAttachments, in the order the pixel shader's SV_Target0, SV_Target1, ... write them;
  • depth: an optional DepthAttachment.

Each attachment has a view (of a texture with TextureUsage::eRenderTarget for color or TextureUsage::eDepthStencil for depth), a load operation (eLoad, eClear, eDontCare; default eLoad), a store operation (eStore, eDontCare; default eStore), and a clear value used when the load operation is eClear. A depth attachment also has stencilLoad and stencilStore, and the read-only flags depthReadOnly and stencilReadOnly. Any attachment may name a single-sampled resolveTo view and a resolveMode so cmdEndRendering resolves the multisampled attachment into it. Shaders may sample the depth texture during a read-only section only if the texture was created with TextureUsage::eReadOnlyDepthSampling.

At cmdBeginRendering, RI:

  1. switches each attachment into its attachment layout when the backend needs it, discarding previous contents when the load operation is not eLoad;
  2. clears the attachments whose load operation is eClear;
  3. sets the viewport and scissor from (0, 0) to the minimum width and height across all attachments, with clip-space y pointing up on both backends (cmdSetViewport, cmdSetScissor change them until the section ends).

At cmdEndRendering, RI switches the attachments (and any resolve targets) back to their resting layouts.

Sections without attachments

RI::cmdBeginRendering(cmd, RI::RenderingSize{width, height}) opens a section with no attachments. Rasterization covers width × height pixels, and pixel shaders write only to storage views (ReadWriteViews), such as a shadow atlas written with InterlockedMin, voxelization, or a visibility buffer built with atomics. The viewport and scissor start covering (0, 0) to (width, height). Pipelines used in this section must have empty colorFormats, no depthFormat, and samples = 1.

RI::cmdBeginRendering(cmd, RI::RenderingSize{.width = 4096, .height = 4096});
RI::cmdSetPipeline(cmd, shadowPipeline); // no color or depth format
RI::cmdSetViewport(cmd, tile);
RI::cmdDrawMesh(cmd, groups, 1, 1);
RI::cmdEndRendering(cmd);
RI::cmdBarrier(cmd, RI::Stage::ePixel, RI::Stage::ePixel); // storage writes, then reads

The pixel shader's storage writes are ordered like any other storage writes, using cmdBarrier with Stage::ePixel. Vulkan passes the size as the render area of a dynamic-rendering section with zero attachments; D3D12 binds no render targets and bounds rasterization with the viewport and scissor alone.

Signature

// <RI/RI.hpp>, namespace RI
void cmdBeginRendering(CommandList &cmd, const RenderingDesc &desc);
void cmdBeginRendering(CommandList &cmd, RenderingSize size);
  • D3D12 header: A section without attachments (P105): rasterization covers size, pixel shaders write only storage, and pipelines have no color or depth format and one sample.
  • D3D12 header: Rendering sections (P40). RI switches attachment layouts around the section when its backend needs to (A', P68); user code is the same everywhere (P16).
  • Vulkan header: A section without attachments (P105): rasterization covers size, pixel shaders write only storage, and pipelines have no color or depth format and one sample.
  • Vulkan header: Rendering sections (P40). RI switches attachment layouts around the section when its backend needs to (A', P68); user code is the same everywhere (P16).
// <RI/RI.hpp>, namespace RI
void cmdEndRendering(CommandList &cmd);
// <RI/RI.hpp>, namespace RI
struct RenderingDesc {
    std::vector<ColorAttachment> colors;
    std::optional<DepthAttachment> depth;
};

From the header: A rendering section. The viewport and scissor cover the attachments until cmdSetViewport or cmdSetScissor changes them (P103).

Field Type Default Description
colors std::vector<ColorAttachment>
depth std::optional<DepthAttachment>
// <RI/RI.hpp>, namespace RI
struct RenderingSize {
    uint32 width = 0;
    uint32 height = 0;
};

From the header: The size a rendering section without attachments rasterizes (P105): cmdBeginRendering(cmd, RenderingSize{...}); 1..16384 each.

Field Type Default Description
width uint32 0
height uint32 0
// <RI/RI.hpp>, namespace RI
struct ColorAttachment {
    TextureView view;
    LoadOp load = LoadOp::eLoad;
    StoreOp store = StoreOp::eStore;
    ClearValue clear;
    // Resolve into this single-sampled view at cmdEndRendering (P52); a view
    // with format eUndefined (the default): no resolve.
    TextureView resolveTo;
    ResolveMode resolveMode = ResolveMode::eAverage;
};
Field Type Default Description
view TextureView
load LoadOp LoadOp::eLoad
store StoreOp StoreOp::eStore
clear ClearValue
resolveTo TextureView Resolve into this single-sampled view at cmdEndRendering (P52); a view with format eUndefined (the default): no resolve.
resolveMode ResolveMode ResolveMode::eAverage
// <RI/RI.hpp>, namespace RI
struct DepthAttachment {
    TextureView view;
    LoadOp load = LoadOp::eLoad;
    StoreOp store = StoreOp::eStore;
    LoadOp stencilLoad = LoadOp::eLoad;
    StoreOp stencilStore = StoreOp::eStore;
    ClearValue clear; // depth and stencil
    bool depthReadOnly = false;   // shaders may sample it during the section if it has eReadOnlyDepthSampling (P75, P97)
    bool stencilReadOnly = false;
    // Resolve into this single-sampled view at cmdEndRendering (P52, P76),
    // depth and stencil with the same mode; format eUndefined: no resolve.
    TextureView resolveTo;
    ResolveMode resolveMode = ResolveMode::eMin;
};
Field Type Default Description
view TextureView
load LoadOp LoadOp::eLoad
store StoreOp StoreOp::eStore
stencilLoad LoadOp LoadOp::eLoad
stencilStore StoreOp StoreOp::eStore
clear ClearValue depth and stencil
depthReadOnly bool false shaders may sample it during the section if it has eReadOnlyDepthSampling (P75, P97)
stencilReadOnly bool false
resolveTo TextureView Resolve into this single-sampled view at cmdEndRendering (P52, P76), depth and stencil with the same mode; format eUndefined: no resolve.
resolveMode ResolveMode ResolveMode::eMin
// <RI/RI.hpp>, namespace RI
enum class LoadOp : uint8 { eLoad, eClear, eDontCare };

From the header: Load and store operations of rendering attachments (P40).

Value Meaning
eLoad
eClear
eDontCare
// <RI/RI.hpp>, namespace RI
enum class StoreOp : uint8 { eStore, eDontCare };
Value Meaning
eStore
eDontCare
// <RI/RI.hpp>, namespace RI
struct ClearValue {
    std::array<float, 4> color{0.0f, 0.0f, 0.0f, 0.0f};
    float depth = 1.0f;
    uint8 stencil = 0;
};

From the header: Clear values: color for color textures, depth and stencil otherwise.

Field Type Default Description
color std::array<float, 4> {0.0f, 0.0f, 0.0f, 0.0f}
depth float 1.0f
stencil uint8 0

Example

// 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);

A read-only depth section, sampling the depth texture while testing against it:

// 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);

The same 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);
// 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);
// Depth bound for testing while shaders sample it: a read-only layout,
// usable by the attachment and by shaders at once. Nothing is stored.
VkRenderingAttachmentInfo readOnlyDepth{
    .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
    .imageView = depthView,
    .imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
    .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
    .storeOp = VK_ATTACHMENT_STORE_OP_NONE,
};
const VkRenderingInfo readOnlyRendering{
    .sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
    .renderArea = {.offset = {0, 0}, .extent = {kWidth, kHeight}},
    .layerCount = 1,
    .pDepthAttachment = &readOnlyDepth,
    .pStencilAttachment = &readOnlyDepth,
};
vkCmdBeginRendering(cmd, &readOnlyRendering);
// ... draws with depthWriteEnable = VK_FALSE ...
vkCmdEndRendering(cmd);

What to watch for: each VkRenderingAttachmentInfo names the layout the image is in during the section, so the transition comes first. A depth-stencil view is given twice, as depth and as stencil attachment. The negative viewport height makes clip space y-up like D3D12 (Vulkan 1.1 core). With VK_KHR_unified_image_layouts, GENERAL everywhere replaces these transitions.

// 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);
// DEPTH_STENCIL_READ allows depth tests and shader reads at once; bind the
// read-only DSV. Draws must not write depth.
cmd->OMSetRenderTargets(0, nullptr, FALSE, &dsvReadOnly);
// ... draws with DepthWriteMask = ZERO, sampling the depth SRV ...

What to watch for: render targets are bound by CPU descriptor (OMSetRenderTargets), and clearing is a command (ClearRenderTargetView), not a load operation. There is no "end": the next barrier moves the textures on. A read-only section needs a read-only DSV and the DEPTH_STENCIL_READ layout.

Rules and errors

  • A RenderingDesc has at least one attachment, and at most 8 colors (programmer error, in every build); a section without attachments takes the RenderingSize form.
  • A RenderingSize is 1 to 16384 pixels wide and high (programmer error in validation builds).
  • Color attachments need TextureUsage::eRenderTarget usage, and the depth attachment needs TextureUsage::eDepthStencil. On D3D12, RI reports a programmer error otherwise; on Vulkan, the Vulkan validation layer reports it.
  • Attachment views should cover one mip; a view with several layers renders to all of them (layered rendering).
  • Only one rendering section may be open at a time on a command list, and cmdEndRendering requires an open section (programmer error in validation builds on Vulkan, and in every build on D3D12).
  • An attachment or a resolveTo target placed over memory another resource used must be discarded first (validation builds report this at cmdBeginRendering).
  • In a read-only depth section, also set stencilReadOnly for formats with stencil, keep load (and stencilLoad) at eLoad so the attachment is not cleared, and use pipelines that do not write depth (validation builds report a depth-writing pipeline as a programmer error at cmdSetPipeline).
  • Each pipeline used in the section must match the attachments' formats and sample count (createGraphicsPipeline).
  • StoreOp::eDontCare is a hint: on D3D12 it has no effect today.
  • For MSAA resolve at the end of a section (resolveTo and resolveMode), see cmdResolve for supported formats, modes, and validation rules.