Culling and the frame

Load and store ops

A render pass draws into images called attachments. At the start of the pass, the GPU must know what to do with the old contents of each attachment. At the end, it must know what to do with the new contents. These two choices are the load op and the store op. They are two small flags, but on many GPUs they decide how many bytes a pass moves. The last part shows how RASM picks them.

In short

  • Load op, at the start: LOAD keeps the old contents, CLEAR fills the image with one value, DONT_CARE starts with anything.
  • Store op, at the end: STORE keeps the new contents, DONT_CARE may throw them away, NONE does not write at all.
  • On a tile-based GPU (phones, Apple) the image lives in small on-chip memory while the pass runs. LOAD is a copy in and STORE is a copy out. CLEAR and DONT_CARE are free.
  • Rule: load only what an earlier pass drew and you still need. Store only what someone reads later.
  • Depth is the classic win: most frames never read depth after the last pass, so it does not need to be stored.
  • A render graph knows who drew an attachment before and who reads it after, so it can pick the ops by itself.

1Two questions per attachment

A render pass draws into one or more attachments: color images, and often one depth image. Before the first triangle, the GPU asks "what is in this image now?". After the last triangle, it asks "do you need what I drew?". The program answers both questions, once per attachment, when it begins the pass.

start of the pass end of the pass Attachment old contents, in memory load op what is in this image now? Render pass draws into it store op do you need what I drew? Attachment new contents, in memory
Every attachment has its own pair: each color attachment, and the depth (and stencil) attachment.

The load op has three answers. One of them moves bytes from memory to the GPU; the other two move none.

the image in memory; an op that moves bytes the image as the pass has it; an op that moves none
copy in in memory pass starts with

load: LOAD

The pass starts with what the image holds: the old contents are kept.

Use it when: an earlier pass drew into it and this pass draws on top: transparent objects over opaque ones, a UI over the scene.

Moves bytes: the image is read

no read one value in memory pass starts with

load: CLEAR

Every pixel starts as one clear value: a color, or a depth.

Use it when: this is the first pass on the image this frame, and some pixels may stay empty.

Moves no bytes

no read ? in memory pass starts with

load: DONT_CARE

The start contents are undefined. They can be old data, or garbage.

Use it when: the pass writes every pixel anyway: a full-screen post effect.

Moves no bytes

The store op has three answers too:

copy out the pass drew in memory

store: STORE

What the pass drew is written to the image in memory.

Use it when: a later pass reads it, it is shown on screen, or the next frame needs it.

Moves bytes: the image is written

no write ? the pass drew in memory

store: DONT_CARE

The new contents may be lost. Afterwards the image is undefined.

Use it when: nobody reads it after this pass. Depth after the last 3D pass is the usual case.

Moves no bytes

no write the pass only read memory keeps it

store: NONE

The pass did not change the image, so nothing is written back, and the old contents stay.

Use it when: the pass only reads the attachment, for example a depth test with depth writes off.

Moves no bytes

Every modern API has the same two choices, with different names:

APILoadStore
VulkanVK_ATTACHMENT_LOAD_OP_LOAD, _CLEAR, _DONT_CARE, _NONEVK_ATTACHMENT_STORE_OP_STORE, _DONT_CARE, _NONE
MetalMTLLoadActionLoad, Clear, DontCareMTLStoreActionStore, DontCare, MultisampleResolve
Direct3D 12 render passesbeginning access PRESERVE, CLEAR, DISCARD, NO_ACCESSending access PRESERVE, DISCARD, RESOLVE, NO_ACCESS
WebGPU"load", "clear""store", "discard"

In Vulkan they go into the attachment info that begins the pass, next to the clear value:

VkRenderingAttachmentInfo colorAttachment{
    .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
    .imageView = colorView,
    .imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL,
    .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
    .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
    .clearValue{.color{.float32{0.0f, 0.0f, 0.0f, 1.0f}}}};

2Why it matters: tile-based GPUs

There are two kinds of GPU. An immediate-mode GPU (most desktop GPUs) draws each triangle as it comes. Its pixels can land anywhere on the screen, so the color and depth images stay in video memory (VRAM), and a cache sits in between. A tile-based GPU (phones, Apple GPUs, many others) first sorts all the triangles of the pass into small screen tiles, for example 32×32 pixels. Then it draws one tile at a time, completely, in a small and very fast memory on the chip.

Immediate-mode GPU Shader cores and depth test triangles in order, anywhere on screen every pixel Cache (small) misses and evictions VRAM color and depth images The image is in memory the whole time. Tile-based GPU the screen, in tiles Tile memory on chip, one tile at a time LOAD ↑ STORE ↓ VRAM color and depth images CLEAR fills the tile on chip: no read from memory. DONT_CARE skips the copy: no memory traffic at all. The image is copied in and out once per tile.
On a tile-based GPU the depth test and blending never touch VRAM. Only the load op and the store op do. They are the pass's whole memory traffic for its attachments.

So on a tile-based GPU, the load and store ops are not details. A LOAD reads the whole image from memory, and a STORE writes the whole image. Memory traffic costs time and, on a phone, battery and heat. CLEAR costs nothing: the tile memory is filled with the clear value on the chip.

Desktop GPUs care too

An immediate-mode GPU has no copy in or out, but the ops still help it:

The gain on desktop is smaller and depends on the GPU and the driver. But the right ops never cost anything, so set them right everywhere.

3Worked example: the bytes

Take a 1920×1080 screen with an RGBA8 color image (4 bytes per pixel) and a 32-bit depth image (also 4 bytes per pixel). Count the bytes a tile-based GPU moves between tile memory and VRAM. (MB here means 1,000,000 bytes.)

8.3 MBcolor, RGBA81920 × 1080 × 4 = 8,294,400 bytes
8.3 MBdepth, 32-bit float1920 × 1080 × 4 = 8,294,400 bytes

One pass, with three choices of ops. Each amber block is one image that crosses between tile memory and VRAM:

ops (color · depth) read from memory written to memory per frame · at 60 fps color LOAD, STORE depth LOAD, STORE color 8.3 depth 8.3 color 8.3 depth 8.3 33.2 MB · 1.99 GB/s 16.6 MB read + 16.6 MB written color CLEAR, STORE depth CLEAR, STORE 0: cleared on chip color 8.3 depth 8.3 16.6 MB · 1.00 GB/s 0 read + 16.6 MB written color CLEAR, STORE depth CLEAR, DONT_CARE 0: cleared on chip color 8.3 not stored 8.3 MB · 0.50 GB/s 0 read + 8.3 MB written
The same pass, the same image on screen, and 4 times fewer bytes.

Now a frame with three passes on the same images: opaque objects, then transparent objects (which test against the opaque depth), then a UI on top (with no depth). An arrow up is a LOAD, an arrow down is a STORE; each one moves 8.3 MB.

on chip: tile memory Opaque colordepth CLEAR → STORE CLEAR → STORE 8.3 8.3 Transparent colordepth LOAD → STORE LOAD → DONT_CARE 8.3 8.3 8.3 not stored UI colordepth LOAD → STORE no depth 8.3 8.3 VRAM: the color image and the depth image 8.3 + 8.3 = 16.6 MB 16.6 + 8.3 = 24.9 MB 16.6 MB Total: 58,060,800 bytes ≈ 58.1 MB → 3.48 GB/s at 60 fps
Clears and DONT_CARE draw no arrow: they cost nothing. The transparent pass pays most, because it must load what the opaque pass stored.

Compare this frame with two other ways to write it:

Three passes at 1920×1080, bytes per frame Every pass: LOAD + STORE, color and depth OpaqueTransparentUI 99.5 MB · 5.97 GB/s Ops chosen per pass (the figure above) OpaqueTransp.UI 58.1 MB · 3.48 GB/s One merged pass: CLEAR, store color only 8.3 MB · 0.50 GB/s
4 pixels of bar per MB. Good ops save about 40% here. Merging the passes (section 5) saves much more, because the images stay on the chip between the passes.
How much is that?

A phone has a few tens of GB/s of memory bandwidth in total, shared with the CPU. A few GB/s for one image is a large part of it.

These numbers are the tile-based model. An immediate-mode GPU moves bytes in a different way, so the numbers do not apply there directly.

4Choosing the ops

Two questions decide everything. For the load op: "do I need what is already there?". For the store op: "will anybody read what I drew?".

Load op Did an earlier pass draw it this frame, and does this pass need that image? yes LOAD no Does the pass write every pixel? yes DONT_CARE no CLEAR LOAD reads the image. CLEAR and DONT_CARE are free. Store op Will anything read it after this pass? A later pass, the screen, the next frame yes STORE no DONT_CARE STORE writes the image. DONT_CARE is free. A pass that only reads the attachment and never writes it: NONE, if the API has it.
"Read later" means any read: a later pass that loads it, a pass that samples it as a texture, the screen, or the next frame.

Try the choices on one color image of the worked example (1920×1080, RGBA8, on a tile-based GPU).

the image in memory; an op that moves bytes the image on the chip; an op that moves none what the pass draws undefined contents
Choose DONT_CARE as the load op while the pass draws only some pixels, and see what reaches memory. Then let the pass draw every pixel, and find the pair of ops that moves the fewest bytes.
SituationLoadStore
Main 3D pass, first on its imagesCLEAR color and depthSTORE color; depth only if a later pass needs it
A pass that draws on top (transparent objects, decals, UI)LOADSTORE color; depth as above
Full-screen post effect into a new imageDONT_CARESTORE
Shadow mapCLEAR depthSTORE depth: the lighting pass samples it
Depth that only the last 3D pass usesCLEAR or LOADDONT_CARE
The swapchain imageCLEAR, or DONT_CARE if every pixel is drawnSTORE: the screen reads it

5The same idea in other places

from UNDEFINED ? CLEAR ✓ ? LOAD ? ✗

Discard in a barrier

An image barrier can change the layout from UNDEFINED. That also says "I do not need the old contents", like DONT_CARE. A pass that clears or does not care can use it: the driver may skip decompressing the old data. A pass that loads must not: the barrier would throw away what it wants to load. See GPU synchronization.

two passes pass 1 pass 2 STORE LOAD VRAM one pass pass 1 + 2, on chip no copy between VRAM

Merging passes

Two passes in a row on the same images pay a STORE and a LOAD in between. If they are one pass, the images stay on the chip and both copies go away (the last bar in section 3). Vulkan subpasses and tile-local reads let a pass even read the previous step's pixel while it is still on the chip, for example for deferred lighting.

Multisampling (MSAA) shows the store op at its best. A 4× MSAA image has 4 samples per pixel, but the screen needs only one. At the end of the pass, the GPU resolves the samples to one value, and stores only that.

covered by the triangle written to memory
on chip MSAA color: 4 samples per pixel resolve average the 4 samples of each pixel resolved color: 1 value per pixel 1/40 4/42/4 DONT_CARE: no write STORE VRAM MSAA image 33.2 MB, never written Resolved image 8.3 MB written
1920×1080, RGBA8, 4× MSAA: the MSAA image is 4 × 8.3 = 33.2 MB. A resolve is just another store op: Metal names it a store action, Direct3D 12 an ending access.
Memoryless

With load CLEAR and store DONT_CARE, the MSAA image is never read or written in memory. A tile-based GPU then needs no memory for it at all.

Vulkan: a transient attachment in lazily allocated memory. Metal: memoryless storage.

The same works for depth that is cleared at the start and not stored at the end: it can be memoryless too, as long as no other pass needs it.

6How a render graph picks the ops

A render graph knows every pass, in the order they run, and what each pass writes and reads. That is exactly what the two questions need. Here is a frame with four passes, and the ops the graph finds for each image:

1 Opaque 2 Transparent 3 Tonemap 4 UI HDR color CLEAR → STORE LOAD → STORE samples it Depth CLEAR → STORE LOAD → DONT_CARE Swapchain DONT_CARE → STORE LOAD → STORE to the screen
Each box is one pass drawing into one image: its load op, then its store op. The purple pill is a read as a texture, which also counts as "read later". Tonemap draws every pixel, so it does not care. Nobody reads depth after Transparent, so it is not stored. The screen reads the swapchain.

After sorting the passes, the graph walks them in order:

  1. Load op. For each attachment of the pass, look at the last earlier pass that drew it. If there is one, and the pass draws on top, LOAD. If there is none, CLEAR, or DONT_CARE when the pass says it covers every pixel.
  2. Store op. Look forward: is there a later pass that loads the image or samples it? Is it the swapchain, or an image kept for the next frame? Then STORE. Otherwise DONT_CARE.
  3. Barriers. A pass that does not LOAD can use the UNDEFINED layout in its barrier (section 5). A pass that loads keeps the layout.
  4. Optional: merge. Neighbouring passes on the same images, with nothing in between that samples them, can become one pass, so the LOAD and STORE between them go away.

The store op needs to know the future, so it is set after the whole order is known: either in a second walk backwards, or by fixing the previous writer when a later pass loads.

7Pitfalls

Garbage, flicker, or black, often only on some GPUs
A DONT_CARE store, and a later pass reads the image. An immediate-mode GPU may keep the data by luck, a tile-based one drops it. STORE whenever anything reads after.
Objects flicker, vanish, or draw over each other
Depth is DONT_CARE, but the next pass LOADs depth, so the depth test runs against garbage. The pass before a depth LOAD must STORE depth.
Old or random pixels from a previous frame show through
LOAD on the first use of an image (or on the swapchain). On a tiler it also costs a full read. Use CLEAR, or DONT_CARE when every pixel is drawn.
A correct image, but the pass reads the whole image and then writes it again
Clearing with a draw call, or with a clear command after a LOAD. Use the CLEAR load op: it is free.
The loaded contents are lost, even though the op is LOAD
An UNDEFINED layout in the barrier before a LOAD. Keep the old layout when the pass loads.
Every split adds a STORE and a LOAD of the same images
Splitting one pass into many small ones. Keep draws to the same images in one pass when you can.
Bugs that hide on desktop

A wrong DONT_CARE often "works" on a desktop GPU, because the image in VRAM happens to keep its old data. The same code shows garbage on a phone or a Mac. The Vulkan validation layers do not catch it, because DONT_CARE is legal. Test on a tile-based GPU, or look at the image in a frame debugger.

8In RASM

RASM uses Vulkan dynamic rendering, so the ops go into VulkanCommandBuffer::beginRendering. A pass describes them with three flags of AttachmentOps: loadColor, loadDepth and storeDepth. Its two other flags are not ops: firstWrite says a cube's other faces may be dropped, and encodedColor draws the swapchain through its UNORM view, for the UI. The render graph sets them for each pass in RenderGraph::assignAttachmentOps, when it compiles. A frame written by hand passes them to Renderer::beginPass.

Render graph: passes on one target run in add order 1st pass color CLEAR → STORE depth CLEAR → STORE (2nd loads) 2nd pass color LOAD → STORE depth LOAD → STORE (3rd loads) last pass color LOAD → STORE depth LOAD → DONT_CARE Depth is stored only when a later pass on the target loads it, or a pass reads it (readDepth). The swapchain has no depth. The scene passes with occlusion culling: a depth read between two passes Scene 1 color CLEAR → STORE depth CLEAR → STORE Hi-Z, compute readDepth: depth SAMPLED Cull 2 compute Scene 2 color LOAD → STORE depth LOAD → STORE Transparent color LOAD → STORE depth LOAD → DONT_CARE With TAA, Transparent stores depth too: the TAA pass reads it (readDepth). endFrame, on the swapchain "Clear" pass only if no pass drew it: CLEAR UI, or the stats overlay color LOAD → STORE, no depth present none when headless
The graph never clears a target twice in a frame, and never loads one that nothing drew. A depth read between two passes on one target makes the pass before it store depth, and the pass after it loads depth.

Passes that draw to one target already run in the order they were added: compile adds an edge from each writer of a target to the next one. So assignAttachmentOps only walks the sorted order once and remembers the last writer of each target (the swapchain counts as one target). When a pass finds an earlier writer, it loads color and depth (only color on the swapchain, which has no depth), and it sets storeDepth on that earlier pass. A pass that reads a target's depth (readDepth) sets storeDepth on the target's last writer before it, too. RenderGraph::execute then passes Pass::attachmentOps to beginPass unchanged.

beginPass also does the barrier from section 5. It calls transitionImageLayout with discard for each attachment that it clears, so the old layout is VK_IMAGE_LAYOUT_UNDEFINED. An attachment that it loads keeps its layout, and the barrier orders the earlier pass's writes before the load.

// A hand-written frame: a world pass, then a UI pass on top of it
engine.beginPass("World", {.storeDepth = true});
// ... draw the world
engine.endPass();
engine.beginPass("UI", {.loadColor = true, .loadDepth = true});
// ... draw the UI
engine.endPass();

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Load opsLOAD, CLEAR, DONT_CARE (and NONE)LOAD or CLEAR only (loadColor, loadDepth). No DONT_CARE load: a full-screen pass clears first. On a tile-based GPU a clear is as cheap.
Store opsSTORE, DONT_CARE, NONEColor is always STORE. Depth is STORE with storeDepth, else DONT_CARE. No NONE.
Per attachmentone pair per attachmentOne loadColor for all color attachments of a pass (multiple render targets). A stencil aspect shares depth's ops.
Clear valuesany, per attachmentColor: the target's clearColor, or BACKGROUND_COLOR for the swapchain. Depth: always 0, the far plane in reversed-Z.
Who picks the opsthe graph, from writers before and readers afterThe graph, from the writers before and the depth reads after (RenderGraph::assignAttachmentOps, review item D9). The first pass on a target clears it; you cannot ask it to load or to not care. One later pass clears too: a pass with Pass::clearsOutput, the first shadow pass of a view whose maps an earlier view drew this frame (shadows per view).
Store of coloronly if read laterAlways. The graph does not check readers: endPass leaves a render target's color sampled for any later pass (B10).
Store of depthonly if read laterOnly if a later pass on the target loads it or reads it. With EngineConfig::occlusionCulling (the default) the scene's Hi-Z pass reads it, so Scene 1 stores it (occlusion culling). Transparent loads it after Scene 2, and the TAA pass reads it after Transparent.
Discard in the barrierUNDEFINED when not loadingThe same: beginPass transitions with discard when the pass clears. A cube target is the exception: a pass draws one face, which counts as a target of its own for the ops, but the barrier covers all six, so only the cube's first pass of the frame, which gets firstWrite, may discard; the later ones keep the faces drawn before (Spot and point light shadows).
Memoryless attachmentstransient, lazily allocatedNone. TextureUsage::TRANSIENT_ATTACHMENT exists but nothing uses it. Transient render targets share memory instead (memory aliasing); their first pass always clears.
MSAA resolvea store variantNot a store op: a multisampled scene is resolved by a compute pass, MSAA Resolve, which weights each sample by its brightness, something a resolve attachment can't do (MSAA).
Merging passesone pass, or subpassesNone. Each pass is its own rendering scope; the scene's early and late draws are two passes, Scene 1 and Scene 2.
History imagesSTORE, the next frame reads itColor is stored anyway. Each frame's copy is cleared by its first pass; last frame's copy is sampled, not loaded (history buffers). A new copy is cleared once at creation by an empty rendering scope (RenderContext::clearNewHistory).
Swapchain with no passthe screen still reads itRenderer::endFrame runs a "Clear" pass, so the image is in a known state when it is presented.

Where it lives in code

WhatWhere
The three flagsAttachmentOps in include/rasm/render/resource.h; a graph pass keeps them in Pass::attachmentOps
Flags to Vulkan opsVulkanCommandBuffer::beginRendering in src/rhi/vulkan/vulkanCommandBuffer.cpp
The graph picks themRenderGraph::assignAttachmentOps, called by RenderGraph::compile, in src/renderer/renderGraph.cpp
Barrier with discardRenderer::beginPass in src/renderer/renderer.cpp
Discard means old usage unknownTextureStates::transition in src/rhi/textureStates.cpp
The depth read between the scene passesRenderer::addScenePasses in src/renderer/gpuScene.cpp; PassBuilder::readDepth in src/renderer/renderGraph.cpp
The "Clear" passRenderer::endFrame in src/renderer/renderer.cpp
Stats overlay passRenderer::drawStatsOverlay in src/renderer/statsOverlay.cpp
UI pass, and the one endFrame addsRenderer::addUiPass, Renderer::drawUiFallback in src/renderer/ui.cpp
Clearing new history copiesRenderContext::clearNewHistory in src/rhi/renderContext.cpp
A frame that uses the graphexamples/01_render_graph/main.cpp: each pass draws its own target once, so every pass clears
Keep these true
  • Passes that draw to one target must keep running in add order. assignAttachmentOps depends on the edges that compile adds between them.
  • A pass that loads depth needs the pass before it to store depth. A pass that samples a depth declares readDepth, so the pass that drew it stores it. The swapchain has no depth, so its passes load and store only color.
  • Clear only with discard, load only without it. A discard before a load drops the contents.
  • Color stays stored: later passes sample render targets without telling the pass that drew them.
  • The depth clear value 0 pairs with the VK_COMPARE_OP_GREATER_OR_EQUAL depth test (reversed-Z). Change both or neither.
  • The first pass on a transient target must draw it: its memory held another target before.

Further reading