Culling and the frame

Many views of one scene

A game shows one camera in one window. Many programs need more: an editor with four viewports, a map application with a small map in the corner, a racing game in split screen, a security monitor in the scene that shows another camera. Each picture is a view: one camera's image of the scene, drawn into one place. This page explains how to draw several views in one frame: where each view goes, how a view becomes a texture, what each view must keep from frame to frame, and what an extra view costs. The last part shows how RASM does it.

In short

  • A view is a scene, a camera and an output: a part of the window (a viewport), or a texture.
  • Give a viewport as fractions of the window, and round each edge to a pixel. Views that touch then share their edge: no gap, no overlap.
  • Draw each view into a target of its own size, then copy it into its viewport in the last pass. Its post effects (bloom, TAA) stay inside it.
  • Views draw in list order: a later view goes over an earlier one. That is a picture-in-picture.
  • A view texture is a normal image: a material or the UI samples it. A pass after its writer sees this frame's picture, a pass before it sees the last frame's. A camera that sees its own screen gets the last frame: no cycle.
  • A view's history (last frame's depth and visibility, TAA) belongs to the view. Find it by what the view is (its camera and output), not by its place in the list.
  • An extra view costs a fixed part (culling, depth pyramid, small passes) and a part per pixel. Split one window in two and the pixel part stays about the same.
  • A view can go to another window. Each window has a swapchain of its own; one submit waits for every window's image and presents them all. A minimized window sits the frame out.

1What a view is

All four pictures below draw the same scene. Only the cameras and the places differ.

player 1 player 2

Split screen

Two cameras, each in half of the window.

main camera map

Picture-in-picture

A small view drawn over a big one: a map, a rear mirror.

perspective top front side

Editor viewports

Four views, often in panels the user resizes.

texture a camera's picture on a TV

Render to texture

The view's output is an image that a material or the UI shows.

So a view needs three things: the scene, the camera and the output. The rest of this page is about the output and about what a view keeps.

2Viewports: a rect of the window

A viewport says where a view goes in the window. Give it as fractions of the window, not in pixels: (0.5, 0, 0.5, 1) is the right half of any window. A resize then needs no new numbers.

a window of 1281 × 720 pixels, split at x = 0.5 left: (0, 0, 0.5, 1) right: (0.5, 0, 0.5, 1) 0 round(0.5 × 1281) = 641 1281 Round each edge, not each width: left is 0 to 641 (641 px), right 641 to 1281 (640 px). They share the edge at 641.
Rounding the width instead (640.5 → 640 for both) leaves one column of pixels that no view draws: a line of the clear color between the views.

Two more rules keep a viewport safe. Clamp the edges to the window, and keep at least one pixel, so a rect that is too small or partly outside still draws something defined.

Move the split and try the odd width: the two widths change, but the gap stays 0. Pick the width 7: every rect, the map too, keeps at least one pixel.

3One target per view

There are two ways to put two views in one window. You can draw both straight into the window's image, each with its own viewport. Or you can draw each view into a target of its own size and copy it into its viewport at the end, in the tone-map pass.

one target: the glow crosses the edge

Views share one image

Blurs (bloom) read across the edge, so light from one view glows into the next. A depth pyramid and a TAA history cover both views, so one view's culling and history read the other's pixels.

view A, 641 px view B, 640 px window a target per view, copied at the end

A target per view

Every effect stays inside its view. The target has the view's own size, so a pixel of it is a pixel of the screen. The cost is the memory of one more target per view.

The last pass, the tone map, reads the view's target and writes its rect of the window. It sets the viewport (where the picture goes) and the scissor (where pixels may change) to that rect.

4Draw order: a later view goes on top

Views that write the same window draw one after the other. The first one clears the whole image; each later one keeps what is there and draws only its rect. So the list order is the stacking order, as in a painting.

  1. clear the window to the background
  2. view A draws its rect: the whole window
  3. view B draws its rect: the corner, over A
  4. the UI draws over both

Part of view A is drawn and then covered by view B. For a small map in a corner that is cheap. For two big views that overlap a lot, it is better to make view A's rect smaller.

5Render to texture

A view's output can also be a texture: a view texture. It is a normal color image. A material samples it like a photo (a TV, a mirror, a portal), and the UI shows it (an editor's viewport panel). Two questions follow: which frame does a reader see, and what happens when the camera sees its own screen.

frame N, passes in order security view: draw its TV shows T: frame N − 1 security view: tone map writes T main view: draw its TV shows T: frame N T reads after the write read before the write: still last frame's T
The texture is read only between its draws, never while it is an attachment. A camera that sees its own screen reads the picture of the frame before, so a TV inside a TV is one frame older at each step, and there is no cycle.

Two rules make this safe. The texture must live across frames: memory that another target reuses during the frame (transient memory) would hold garbage when a later frame reads it. And the texture must hold something defined before the first frame draws it: clear it when it is made.

Move the texture view in the list. Every view after it sees this frame's picture on its TV; the views before it, and the security camera itself, see the last frame's.

6What a view keeps from frame to frame

A view is not drawn from nothing each frame. Occlusion culling reads the depth pyramid and the visibility of the last frame, and TAA blends this frame into last frame's image. That history belongs to one view: another camera's history is a different picture.

last frame A B this frame C A B place 1 → place 1: C blends A's picture

Kept by place in the list

Adding, removing or moving one view hands every later view the wrong history: ghosts for a frame, and culling that draws too much or too little.

last frame A B this frame C A B C starts new; A and B keep their own

Kept by what the view is

The key is the camera and the output. A view keeps its targets and history while it is in the list, wherever it is. A new key starts with an empty history.

A view whose rect changes size keeps its key, but its targets change size. The textures are made again, and their old contents are gone, so its history starts over for one frame. The GPU may still read the old textures in the frames in flight, so making them again waits for those frames: the same short wait a window resize has.

7What an extra view costs

Each view runs the whole scene pipeline: culling, the depth pyramid, the draws, the transparent pass, its post effects and the tone map. Part of that cost is fixed per view. The rest grows with the view's pixels, so cutting one window into parts moves pixels around but adds only the fixed part.

1.32 ms3 views: the main camera in the whole window, a map in the corner, a 320 × 180 view texturelayout 1; 2.31 ms with shadows
1.55 ms4 views: split screen in two halves, the map and the view texturelayout 2; 3.25 ms with shadows
1.46 ms5 views: four quarters and the view texturelayout 3; 3.80 ms with shadows

Measured in RASM's example below, 1920 × 1080, Release, on an RTX 3060, with TAA and bloom on the big views and neither on the map. Four quarters cost less than two halves here: one quarter is the map, without TAA and bloom, so fewer pixels go through them. A small view (a map, a mirror) is cheap: skip its bloom and TAA, and it costs mostly the fixed part.

Shadows are part of the fixed cost, and they are per view: each shadowed view draws its own cascades, since they are fitted to its camera. In the example each perspective view draws the sun in four 2048 × 2048 cascades (the TV two of 1024): about 0.6 ms per view with four cascades, 0.4 ms for the TV, whatever the view's size. Give a small view fewer and smaller cascades, or none.

8Input per view

Each view's camera listens to its own rect of the window. A press inside the rect starts a drag that belongs to that view until the release, even when the mouse leaves the rect. The wheel goes to the view under the pointer. The rect in window points is the viewport's fractions times the window's size in points. The camera controllers guide draws it: two views, two inputs, one window.

9Views in other windows

An editor puts a viewport on a second monitor; a tool shows a preview in a window of its own. A window on screen needs a swapchain, its ring of images, made from the window's surface. So a second window is a second swapchain, and the frame now has two images to wait for and two to present.

one frame, three windows acquire window 1 acquire window 2 window 3: minimized one submit waits: image 1, image 2 passes draw both windows signals: present 1, present 2 present window 1 present window 2 Each window's passes are their own: the first clears its image, later ones load it. A window with no image this frame (minimized, being resized) is skipped with its passes. The frame is skipped only when no window has an image.
One submit for all windows keeps the frame's work in one place on the GPU timeline: the frame fence covers every window. Presenting a window does not wait for another window's display.

Three more things change with a second window. The surface of the new window must be one the GPU's queue can present to; a GPU checks that per surface. The input now comes from two windows, so every event carries the window it happened in, and a view's camera reads only its own window's events. And closing a window is no longer quitting: closing the main window ends the program, closing another one is an event the program answers by removing that window.

window 1 window 2

A swapchain each

Its own surface, images, size and resize. A resize of one window rebuilds only its swapchain.

event: w2 w2's camera the UI reads the main window's

Events carry their window

Each camera takes its own window's events. The UI stays in the main window.

main: quit w2: an event the program removes w2

Closing is not quitting

Only the main window's close ends the program; the others become an event.

10Pitfalls

A line of the background color between two views
The viewports were rounded by width, not by edge. Round each edge, so touching views share it.
The glow of a lamp in one view shows in the next
Both views share one HDR image, and the blur reads across the edge. Give each view its own target.
Ghosts for one frame when a view is added
The histories are kept by list place. Key them by the view's camera and output.
A stretched picture in a narrow view
The projection used the window's aspect ratio. Fit it to the view's target instead.
The TV shows garbage, or a validation error about an image layout
The view texture was transient, or never cleared, so a reader found undefined data. Keep it across frames and clear it when it is made.
Dragging in one view turns another view's camera
Both cameras read the whole window's input. Give each camera the input of its own rect, and of its own window.
Closing a second window quits the program
The close of any window was treated as quit. Quit only on the main window's; remove the other one.
The whole program stops drawing when one window is minimized
A minimized window was allowed to skip the frame. Skip only that window, and the frame only when no window can show.

11In RASM

Renderer::render(views, count) draws a list of Views in one frame: each is a scene, a camera and a ViewOutput, which is a ViewRect of the window or of a target. Renderer::render(scene, camera) is the same with one view. ViewOutput::window puts a view into another window from Renderer::addWindow. examples/20_views shows split screen, a map, four views, a TV in the scene and a second window; examples/19_embedding a second window of the application's own (PlatformType::NATIVE).

A hidden view: exports exportView adds a view of its own to the graph after yours, with a copy of the camera and a target of one tile and its guard band, and draws a still of any size there, one tile sample a frame (Tiled offscreen export). The graph is built again when an export starts or ends.
the application and the engine graph passes targets
render(views, count) a View: scene, camera, output ViewGraph::render same list and sizes as last frame: run the graph; else build it again kept per view, found by its camera and output target ViewState scene_hdr, scene_hdr_1, ... TAA history + TaaState with TAA only its scene view Hi-Z, visibility, light clusters the graph, per view in list order scene passes read earlier view textures MSAA resolve or TAA View::antiAliasing bloom View::bloom Tone Map into the rect: window or target UI reads every view texture A view texture is a target the application makes: one color, no depth (R8G8B8A8_SRGB). A later view's tone map over the window loads it, so the list order is the stacking order.
The graph is built again when a view is added, removed, moved or resized; the kept state outlives it, so the other views keep their culling history and TAA. A frame with the same list allocates nothing.
const rasm::ViewRect leftHalf = {.x = 0.0f, .y = 0.0f, .width = 0.5f, .height = 1.0f};
const rasm::ViewRect rightHalf = {.x = 0.5f, .y = 0.0f, .width = 0.5f, .height = 1.0f};
const rasm::ViewRect corner = {.x = 0.75f, .y = 0.0f, .width = 0.25f, .height = 0.4f};
const rasm::View views[] = {
    {.scene = scene, .camera = security, .output = {.target = tv}},
    {.scene = scene, .camera = left, .output = {.rect = leftHalf}},
    {.scene = scene, .camera = right, .output = {.rect = rightHalf}},
    {.scene = scene, .camera = map, .output = {.rect = corner}, .bloom = rasm::ViewToggle::OFF},
};
engine.render(views, 4);

Shadows per view

A view's shadows (up to MAX_VIEW_SHADOWS, 4) are ShadowDescs of lights of its scene. ViewGraph::addView adds addShadowPasses for each one before the view's scene passes. Cascades and LODs follow the view's own camera (Cascaded shadow maps), so views of different cameras never share the maps' contents. They share the maps' memory: one set per light, map size and map count, whatever the number of views. Each view draws the set in turn, as the picture shows.

from above: each camera's own cascades camera A camera B one frame: one set of maps, drawn in turn shadow_cascade_0 .. 3 holds A's cascades 4 maps, 2048 x 2048, D32: 64 MiB then B's cascades A: Cascade 0-3 clear, draw A: scene passes read the maps B: Cascade 0-3 clear, draw B: scene passes read the maps write after read: the graph runs B's draws after A's readers the frame's graphics queue, in order A view without shadows adds no pass and no map.
Every reader of A's maps (Scene 1, Scene 2, Transparent, and the Hi-Z of A's own cascades) runs before B's first cascade pass, which clears the map (Pass::clearsOutput) instead of loading A's depth. Two views of one camera (the TV and a window rect of the security camera) get a set each; so do views whose map size or cascade count differ.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Viewportfractions, edges roundedViewRect; getViewPixels rounds each edge, clamps it to the image and keeps at least 1 × 1. The tone-map pass sets the viewport and the scissor to it when it runs, so a window resize moves it.
Target per viewa target of the view's sizeEach view of render(views, count) has its own HDR target (scene_hdr, then scene_hdr_1, ...), made with Renderer::createRenderTarget so it outlives the graph, at the rect's size in pixels. Bloom and MSAA resolve targets are the graph's, transient.
Post effects per viewskip them for small viewsView::bloom (ViewToggle) and View::antiAliasing; DEFAULT takes the EngineConfig value. A change of anti-aliasing makes the view's targets again.
See-through per view—Each view with weighted see-through draws has its own weighted target (SceneView::weightedTarget, the scene target's size and samples) and its own weights' range, made the first frame it has such draws (weighted blended transparency).
Input per viewa point of the window to a view's pixelgetViewPixel turns a mouse position into the pixel of the view under it, and Renderer::pick asks that view what it drew there (Picking).
View texturekept across frames, cleared when madeA target the application makes with one color and no depth. Every target that is not transient starts cleared to its clearColor and SAMPLED. getRenderTargetTexture of a target without history is one texture for both frame slots, so a material can keep it.
This frame or lastafter the writer: this framerender makes each view's scene passes read the view textures listed before it (addScenePasses's sampled) and the UI pass read them all (addUiPass's shown), so they run after the writers. A material that samples its own view's texture, or a later one's, sees the last frame.
History by keycamera and outputViewState keys on the camera and ViewOutput::target (the rect is not part of it). It keeps the HDR target, the TAA history and its TaaState; the scene view lives on because the new graph holds it before the old one lets go. Two views with one key: the second is left out with an error.
Resizea short wait for the frames in flightRenderer::resizeRenderTarget waits for the values both queues signaled last, only when the size in pixels changes, and keeps the texture handles and bindless slots (RenderTarget::version counts the new textures, so TAA starts over). A window resize waits for the device to be idle.
Shadows—View::shadows and View::shadowCount: each view draws its shadows from its own camera, into a ShadowSet the ViewGraph keeps across graphs (one per light, map size and map count; a second view of the same camera gets its own), so a list change, or a new distance or splitLambda, keeps the shadow scene views, their Hi-Z and the label fades of the shaded views. A set no view uses any more is destroyed through the release queue. exportView draws no shadows (D58).
Limits—MAX_SCENE_VIEWS (32) scene views, shadow maps included (a view with four cascades takes five, D20); MAX_CAMERAS (32). A view past the limit is left out with an error.
Inputone rect eachgetInputArea(window, rect) gives the InputArea for a view's CameraInputState (Camera controllers). The built-in controller moves the camera of the first view in the window.
Windowsa swapchain each, one submitRenderer::addWindow(WindowDesc): an SDL window, the application's (WindowDesc::native, NATIVE) or an offscreen image (HEADLESS), at most MAX_WINDOWS − 1 (7). RenderContext::createSwapchain makes the main window's swapchain on the device's surface and another's on a surface of its own (vkGetPhysicalDeviceSurfaceSupportKHR on the graphics queue). beginFrame acquires each window that can show (Renderer::WindowFrame); SubmitInfo::waitsForSwapchainImage and presentsSwapchainImage cover every acquired window, through SubmitDesc::acquireSwapchains and presentSwapchains; endFrame clears a window no pass drew and presents each. PassBuilder::writeSwapchain(window) makes each window's image a resource of its own; the graph skips a pass whose window has no image. removeWindow waits for the device to be idle; a view that still names the window is left out. InputEvent::window tags every event, and closing a window other than the main one is InputEventType::WINDOW_CLOSE. readFrame(window) copies a window's image.

Where it lives in code

WhatWhere
The typesView, ViewOutput, ViewRect, ViewToggle, ViewPixels, getViewPixels, getInputArea in include/rasm/view.h and src/renderer/view.cpp
The list of viewsViewGraph::render, ViewGraph::build, ViewGraph::addView and ViewState in src/renderer/viewGraph.h and src/renderer/viewGraph.cpp
Into a rect or a textureRenderer::addToneMapPass and getToneMapPipeline (one pipeline per output format, MAX_TONE_MAP_FORMATS) in src/renderer/toneMap.cpp
Readers of view texturesRenderer::addScenePasses in src/renderer/gpuScene.cpp; Renderer::addUiPass in src/renderer/ui.cpp
Kept TAA historyTaaState, getTaaHistoryDesc in src/renderer/taa.h; the private Renderer::addTaaPass with a history in src/renderer/taa.cpp
Resize and clearRenderer::resizeRenderTarget in src/renderer/renderer.cpp; RenderContext::resizeRenderTarget and createAttachments in src/rhi/renderContext.cpp
Shadows per viewShadowSet, ViewGraph::getShadowMaps in src/renderer/viewGraph.cpp; the private Renderer::addShadowPasses with the set's maps in src/renderer/gpuScene.cpp; Pass::clearsOutput and RenderGraph::assignAttachmentOps in src/renderer/renderGraph.cpp
Teststests/viewTests.cpp (rects and input areas); the views, views_window2 and views_shadows image tests in tests/image/imageTests.cpp
WindowsRenderer::addWindow, removeWindow, getWindow, updateSwapchain, beginFrame, endFrame in src/renderer/renderer.cpp; RenderContext::createSwapchain, destroySwapchain, submit in src/rhi/renderContext.cpp; VulkanDevice::createSwapchain, VulkanSwapchain, VulkanQueue::submit in src/rhi/vulkan/; SdlPlatform::pollEvents in src/platform/sdlPlatform.cpp
Exampleexamples/20_views/main.cpp: keys 1, 2, 3 for the layouts, N to open the second window again, --bench for the numbers above (--no-shadows without the shadows, --msaa for MSAA in the side view); examples/19_embedding/main.cpp: the application's second window
Keep these true
  • A view's kept state is found by its camera and output target, never by its place in the list.
  • A frame with the same list and sizes compares and runs: no graph build, no heap allocation (the views image test counts them).
  • A view texture is not transient and starts cleared: any pass may sample it at any point of the frame.
  • Resizing a target waits only for the frames in flight, and only when its size in pixels changes.
  • A view's shadows are drawn from its own camera, right before its scene passes; its first pass on each map clears it, and the graph orders it after every reader of the view before.

Further reading