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.
Split screen
Two cameras, each in half of the window.
Picture-in-picture
A small view drawn over a big one: a map, a rear mirror.
Editor viewports
Four views, often in panels the user resizes.
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.
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.
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.
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.
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.
- clear the window to the background
- view A draws its rect: the whole window
- view B draws its rect: the corner, over A
- 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.
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.
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.
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.
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.
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.
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.
A swapchain each
Its own surface, images, size and resize. A resize of one window rebuilds only its swapchain.
Events carry their window
Each camera takes its own window's events. The UI stays in the main window.
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).
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.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.
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
| Part | This page | RASM |
|---|---|---|
| Viewport | fractions, edges rounded | ViewRect; 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 view | a target of the view's size | Each 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 view | skip them for small views | View::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 view | a point of the window to a view's pixel | getViewPixel turns a mouse position into the pixel of the view under it, and Renderer::pick asks that view what it drew there (Picking). |
| View texture | kept across frames, cleared when made | A 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 last | after the writer: this frame | render 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 key | camera and output | ViewState 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. |
| Resize | a short wait for the frames in flight | Renderer::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. |
| Input | one rect each | getInputArea(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. |
| Windows | a swapchain each, one submit | Renderer::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
| What | Where |
|---|---|
| The types | View, ViewOutput, ViewRect, ViewToggle, ViewPixels, getViewPixels, getInputArea in include/rasm/view.h and src/renderer/view.cpp |
| The list of views | ViewGraph::render, ViewGraph::build, ViewGraph::addView and ViewState in src/renderer/viewGraph.h and src/renderer/viewGraph.cpp |
| Into a rect or a texture | Renderer::addToneMapPass and getToneMapPipeline (one pipeline per output format, MAX_TONE_MAP_FORMATS) in src/renderer/toneMap.cpp |
| Readers of view textures | Renderer::addScenePasses in src/renderer/gpuScene.cpp; Renderer::addUiPass in src/renderer/ui.cpp |
| Kept TAA history | TaaState, getTaaHistoryDesc in src/renderer/taa.h; the private Renderer::addTaaPass with a history in src/renderer/taa.cpp |
| Resize and clear | Renderer::resizeRenderTarget in src/renderer/renderer.cpp; RenderContext::resizeRenderTarget and createAttachments in src/rhi/renderContext.cpp |
| Shadows per view | ShadowSet, 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 |
| Tests | tests/viewTests.cpp (rects and input areas); the views, views_window2 and views_shadows image tests in tests/image/imageTests.cpp |
| Windows | Renderer::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 |
| Example | examples/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 |
- 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
- Vulkan specification, "Fixed-Function Vertex Post-Processing: Controlling the Viewport", and "Scissor Test".
- Yuriy O'Donnell, "FrameGraph: Extensible Rendering Architecture in Frostbite" (GDC 2017): views as parts of one frame graph.
- Unreal Engine documentation, "Scene Capture": render to texture and its cost.