Skip to content

RI::cmdSetViewport, RI::cmdSetScissor, RI::cmdSetStencilReference

What it does

Inside a rendering section, these commands override the section's viewport, scissor rectangle, and stencil reference value:

  • RI::cmdSetViewport(cmd, viewport) sets the viewport: the rectangle of the render target that clip space maps to (x, y, width, and height in pixels from the top-left corner), and the depth range it maps to (minDepth and maxDepth, 0.0f to 1.0f by default). For example, a full-screen triangle drawn with a viewport covering the left half of the target fills only the left half.
  • RI::cmdSetScissor(cmd, scissor) sets the scissor rectangle: pixels outside this rectangle are discarded and not written. Unlike the viewport, the scissor clips the output without scaling or stretching it.
  • RI::cmdSetStencilReference(cmd, reference) sets the stencil reference, the value the stencil test compares against and StencilOp::eReplace writes. Each graphics pipeline has its own default (stencil.reference), which cmdSetPipeline applies; once you call cmdSetStencilReference, your reference stays active across pipeline switches until the next cmdSetStencilReference call or the end of the rendering section. This lets you tag different objects (such as a material ID or an outline) without creating separate pipelines.

Each setting remains in effect until the next call or the end of the section. Every cmdBeginRendering resets the viewport and scissor to cover the attachments (or the RenderingSize in a section without attachments) and clears any stencil reference override so pipelines use their own stencil.reference again. Common uses include split-screen rendering, writing tiles of a shadow atlas, and clipping UI panels to their rectangle.

Coordinates are the same on both backends: y goes down from the top-left corner of the target, while clip space keeps y pointing up (clip space).

Signature

// <RI/RI.hpp>, namespace RI
void cmdSetViewport(CommandList &cmd, const Viewport &viewport);

From the header: Inside a rendering section, until it ends or the next call (P103).

// <RI/RI.hpp>, namespace RI
void cmdSetScissor(CommandList &cmd, const Rect &scissor);
// <RI/RI.hpp>, namespace RI
void cmdSetStencilReference(CommandList &cmd, uint8 reference);

From the header: The stencil reference for the rest of the rendering section, over the one each pipeline brings (P104).

// <RI/RI.hpp>, namespace RI
struct Viewport {
    float x = 0.0f;
    float y = 0.0f;
    float width = 0.0f;
    float height = 0.0f;
    float minDepth = 0.0f;
    float maxDepth = 1.0f;
};

From the header: The viewport of a rendering section (P103): the rectangle of the render target that clip space maps to, in pixels from the top-left corner, and the range depth maps to. cmdBeginRendering sets it to the whole attachments.

Field Type Default Description
x float 0.0f
y float 0.0f
width float 0.0f
height float 0.0f
minDepth float 0.0f
maxDepth float 1.0f
// <RI/RI.hpp>, namespace RI
struct Rect {
    int32 x = 0;
    int32 y = 0;
    uint32 width = 0;
    uint32 height = 0;
};

From the header: A rectangle in pixels from the top-left corner: a scissor (P103; pixels outside it are not written, cmdBeginRendering sets it to the attachments), or the texels a copy covers (P108).

Field Type Default Description
x int32 0
y int32 0
width uint32 0
height uint32 0

Example

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

The same in native code

// 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 flips the viewport so clip space is y-up, as on D3D12.
const VkViewport viewport{.x = 0.0f, .y = 128.0f, .width = 128.0f, .height = -128.0f,
                          .minDepth = 0.0f, .maxDepth = 1.0f};
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
const VkRect2D scissor{.offset = {0, 0}, .extent = {128, 128}};
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
vkCmdSetStencilReference(commandBuffer, VK_STENCIL_FACE_FRONT_AND_BACK, 3);

What to watch for: viewport, scissor, and stencil reference are dynamic state in the pipelines RI creates, and RI applies the pipeline's stencil.reference when you call cmdSetPipeline (unless overridden in the current section). Passing a negative height flips the viewport vertically so y starts at its bottom edge.

const D3D12_VIEWPORT viewport{0.0f, 0.0f, 128.0f, 128.0f, 0.0f, 1.0f};
list->RSSetViewports(1, &viewport);
const D3D12_RECT scissor{0, 0, 128, 128}; // left, top, right, bottom
list->RSSetScissorRects(1, &scissor);
list->OMSetStencilRef(3);

What to watch for: D3D12_RECT specifies the scissor by its edges (left, top, right, bottom) rather than its size, and D3D12 always applies a scissor test—if you do not set a scissor rectangle, nothing is drawn.

Rules and errors

  • All three commands must be called inside an open rendering section (programmer error in validation builds).
  • The viewport's width and height must be greater than 0, and minDepth and maxDepth must lie between 0 and 1 inclusive; the scissor's x and y must be 0 or greater (programmer error in validation builds).
  • A native section inside a rendering section may change the viewport, scissor, or stencil reference; endNative restores RI's active values for the section.