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.
The load op has three answers. One of them moves bytes from memory to the GPU; the other two move none.
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
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
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:
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
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
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:
| API | Load | Store |
|---|---|---|
| Vulkan | VK_ATTACHMENT_LOAD_OP_LOAD, _CLEAR, _DONT_CARE, _NONE | VK_ATTACHMENT_STORE_OP_STORE, _DONT_CARE, _NONE |
| Metal | MTLLoadActionLoad, Clear, DontCare | MTLStoreActionStore, DontCare, MultisampleResolve |
| Direct3D 12 render passes | beginning access PRESERVE, CLEAR, DISCARD, NO_ACCESS | ending 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.
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:
- Fast clears. Many desktop GPUs keep images compressed in blocks, with a little metadata per block. A CLEAR can just mark every block as "clear" in the metadata, without writing the pixels.
- No decompression. DONT_CARE at the start lets the driver skip work on the old contents, such as decompressing them.
- Depth. A depth image that is not stored may never need to be written out of the GPU's compressed form.
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.)
One pass, with three choices of ops. Each amber block is one image that crosses between tile memory and VRAM:
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.
Compare this frame with two other ways to write it:
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?".
Try the choices on one color image of the worked example (1920×1080, RGBA8, on a tile-based GPU).
| Situation | Load | Store |
|---|---|---|
| Main 3D pass, first on its images | CLEAR color and depth | STORE color; depth only if a later pass needs it |
| A pass that draws on top (transparent objects, decals, UI) | LOAD | STORE color; depth as above |
| Full-screen post effect into a new image | DONT_CARE | STORE |
| Shadow map | CLEAR depth | STORE depth: the lighting pass samples it |
| Depth that only the last 3D pass uses | CLEAR or LOAD | DONT_CARE |
| The swapchain image | CLEAR, or DONT_CARE if every pixel is drawn | STORE: the screen reads it |
5The same idea in other places
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.
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.
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:
After sorting the passes, the graph walks them in order:
- 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.
- 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.
- Barriers. A pass that does not LOAD can use the UNDEFINED layout in its barrier (section 5). A pass that loads keeps the layout.
- 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.
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.
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
| Part | This page | RASM |
|---|---|---|
| Load ops | LOAD, 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 ops | STORE, DONT_CARE, NONE | Color is always STORE. Depth is STORE with storeDepth, else DONT_CARE. No NONE. |
| Per attachment | one pair per attachment | One loadColor for all color attachments of a pass (multiple render targets). A stencil aspect shares depth's ops. |
| Clear values | any, per attachment | Color: the target's clearColor, or BACKGROUND_COLOR for the swapchain. Depth: always 0, the far plane in reversed-Z. |
| Who picks the ops | the graph, from writers before and readers after | The 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 color | only if read later | Always. The graph does not check readers: endPass leaves a render target's color sampled for any later pass (B10). |
| Store of depth | only if read later | Only 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 barrier | UNDEFINED when not loading | The 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 attachments | transient, lazily allocated | None. TextureUsage::TRANSIENT_ATTACHMENT exists but nothing uses it. Transient render targets share memory instead (memory aliasing); their first pass always clears. |
| MSAA resolve | a store variant | Not 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 passes | one pass, or subpasses | None. Each pass is its own rendering scope; the scene's early and late draws are two passes, Scene 1 and Scene 2. |
| History images | STORE, the next frame reads it | Color 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 pass | the screen still reads it | Renderer::endFrame runs a "Clear" pass, so the image is in a known state when it is presented. |
Where it lives in code
| What | Where |
|---|---|
| The three flags | AttachmentOps in include/rasm/render/resource.h; a graph pass keeps them in Pass::attachmentOps |
| Flags to Vulkan ops | VulkanCommandBuffer::beginRendering in src/rhi/vulkan/vulkanCommandBuffer.cpp |
| The graph picks them | RenderGraph::assignAttachmentOps, called by RenderGraph::compile, in src/renderer/renderGraph.cpp |
| Barrier with discard | Renderer::beginPass in src/renderer/renderer.cpp |
| Discard means old usage unknown | TextureStates::transition in src/rhi/textureStates.cpp |
| The depth read between the scene passes | Renderer::addScenePasses in src/renderer/gpuScene.cpp; PassBuilder::readDepth in src/renderer/renderGraph.cpp |
| The "Clear" pass | Renderer::endFrame in src/renderer/renderer.cpp |
| Stats overlay pass | Renderer::drawStatsOverlay in src/renderer/statsOverlay.cpp |
| UI pass, and the one endFrame adds | Renderer::addUiPass, Renderer::drawUiFallback in src/renderer/ui.cpp |
| Clearing new history copies | RenderContext::clearNewHistory in src/rhi/renderContext.cpp |
| A frame that uses the graph | examples/01_render_graph/main.cpp: each pass draws its own target once, so every pass clears |
- Passes that draw to one target must keep running in add order.
assignAttachmentOpsdepends on the edges thatcompileadds 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_EQUALdepth 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
- The Vulkan specification: Render Pass Load Operations and Render Pass Store Operations, and Lazily Allocated Memory.
- Arm, Mali GPU Best Practices: the sections on render passes and attachments.
- Khronos Vulkan Samples: the "render passes" and "MSAA" performance samples, with bandwidth numbers.
- Apple, Metal Best Practices Guide: "Load and Store Actions" and "Memoryless Render Targets".