skip to content

A post-processing pass reads stale or half-written data from the colour target the previous pass rendered, but only on some GPUs; what barrier is missing, and how do you place barriers without stalling the GPU?

level: seniorimportance: should knowfreq 40%

answer

  1. works here, breaks there
  2. read after write on one resource
  3. order, flush, transition
  4. batch and narrow, drain once

basics

~20 s

A barrier from colour-target write to shader read is missing; it orders the passes, makes the writes visible to texture reads and transitions the target's layout. Batch transitions per pass boundary and narrow their stages so the GPU drains once.

solid answer

~50 s

Where an API leaves hazard tracking to the application, nothing in one queue guarantees a later pass sees an earlier pass's writes, so a **read-after-write** on the colour target needs a barrier that does three jobs: an **execution dependency** (the post-process shader waits for colour output to finish), **memory visibility** (render-target caches written back, texture reads not served stale data) and a **layout transition**, which on many GPUs decompresses the target. It worked on one GPU only because that driver or cache path happened to serialize and flush. Find it with the API's debug validation and synchronization checks, and verify the fix on the GPU that failed. To avoid stalls, **batch** every transition at a pass boundary into one barrier, **narrow** each to the producing and consuming stages, place it late or split it where the API allows, and skip transitions between two reads.

code

pseudocode · 10 lines
pseudocode
drawLightingPass(writes: hdrColour)
dispatchBloom(writes: bloomTexture)

# one batched barrier at the pass boundary, narrowed to the stages involved
barrier([
    { resource: hdrColour,     after: COLOUR_OUTPUT_WRITE, before: FRAGMENT_SHADER_READ },
    { resource: bloomTexture,  after: COMPUTE_WRITE,       before: FRAGMENT_SHADER_READ }
])

drawPostProcess(reads: hdrColour, bloomTexture)

go deeper

for a junior

Recall that when one pass writes a texture and a later pass reads it, a barrier between them is needed for the read to see the data.

for a middle

Explain the three jobs of a write-to-read barrier and the three hazards, and why a layout transition can cost GPU time.

for a senior

Diagnose a missing barrier from a GPU-specific corruption, confirm it with synchronization validation, and cut bubbles by batching, narrowing, splitting and skipping transitions.

for a principal

Decide how the team keeps synchronization correct over time: hand-placed barriers with validation in every test run, or barriers derived from a frame graph.

## What a barrier is for Modern GPUs run many passes' worth of work concurrently and keep data in caches that are not automatically coherent with each other. Where an API leaves hazard tracking to the application, nothing inside one queue guarantees that a later command sees the results of an earlier one; some APIs can track hazards automatically for resources created that way, at a CPU cost. A **barrier** is the command that creates that guarantee between two uses of one resource. It covers three **hazards**: - **read-after-write**: a pass samples what an earlier pass rendered (this scenario); - **write-after-read**: a pass overwrites what an earlier pass is still reading; - **write-after-write**: two passes write one resource and the final value must be the later one. ## The three jobs of a render-target-to-sampled barrier For a colour target that one pass rendered and a post-processing pass samples, the barrier does three things: 1. **Execution dependency.** The consumer's shader stage must not start reading until the producer's colour-output stage has finished writing. 2. **Memory visibility.** Colour writes may still sit in render-target caches; they must be written back, and the caches the texture reads go through must not serve stale lines. 3. **Layout and compression transition.** Many GPUs store render targets in a compressed or tiled form, with fast-clear metadata, that their texture units cannot read directly. The transition can **decompress** the target or resolve that metadata, which is real GPU work. A write-after-read hazard usually needs only the first job, because the reader left nothing in a cache to write back. ## Why it works on some GPUs and not others Without the barrier the result depends on hardware and driver. One GPU's driver may drain its pipeline at a render-target change anyway, or its caches may be coherent on that path, so the post-process pass happens to see finished data. Another GPU legitimately overlaps the two passes, or samples the compressed target without decompressing it, and shows stale, blocky or half-written regions. The bug is in the application, not the second GPU. The API's debug validation with synchronization checking reports most missing barriers, naming the resource and the two accesses, and it belongs in regular test runs on every platform. ## Where too many barriers cost Each barrier that waits on a producer stage holds back the consumer work behind it until the producer drains. The GPU's units go idle while the producer's last work finishes and the consumer ramps up: a **bubble**. A renderer that issues a separate barrier for every resource, or uses a barrier that waits on all prior work at all stages everywhere, pays that bubble many times per frame, plus any decompression the transitions trigger. This is also the shape of the classic stall in which a renderer records thousands of draws efficiently and then loses the gain at one wide transition. Guidelines: - **Batch** transitions that happen at the same point into one barrier, so the GPU drains once, not once per resource. - **Narrow** each barrier: wait only on the stage that wrote (colour output, compute) and block only the stage that reads (fragment or compute shading). - **Place late or split**: where an API supports it, begin the transition right after the producer and end it just before the consumer, so independent work runs in between. - **Skip** needless transitions: two reads in a row need no barrier, and a depth target that is both tested and sampled read-only can stay in one read state. - **Fill bubbles** with independent work, such as compute on another queue, where the GPU supports it. | Approach | Correct | GPU cost | |---|---|---| | No barrier | wrong on some GPUs | none until it breaks | | Wide barrier per resource | yes | a drain for each resource | | Batched, narrow barriers | yes | one short drain per pass boundary | | Split barriers | yes | the drain partly hidden behind other work | ## Diagnosing the scenario 1. Run with debug validation and synchronization checking enabled; it names the colour target and the write and read that conflict. 2. Insert a barrier on that colour target from colour-target write to shader read, between the two passes. 3. Verify on the GPU that showed the bug; a fix checked only on the GPU that already looked right proves nothing. 4. Review the frame's barriers as a whole: batch and narrow them, or let a frame graph derive them from declared reads and writes. The fix is one barrier; the lesson is that synchronization correctness is tested by the platforms that overlap work most, never by the one that happens to serialize it.

  • A compute pass writes indirect draw arguments that a later draw reads; what barrier does that need?
    A read-after-write barrier on the argument buffer from compute-shader write to indirect-argument read. The GPU fetches indirect arguments at a stage before any shader runs, so the barrier must name that early stage as the consumer, or the draw may read a count the compute pass has not finished writing.
  • Why does a write-after-read hazard usually need a cheaper barrier than a read-after-write one?
    The earlier pass only read the resource, so it left no dirty cache lines to write back. The later writer only has to wait until those reads have finished, which is an execution dependency without the memory-visibility work.
  • When does filling barrier bubbles with async compute fail to help?
    When the compute work competes for the same bottleneck as the graphics work around it, such as memory bandwidth, or when it adds cross-queue waits that create new bubbles. The overlap must be checked with GPU timing on each platform rather than assumed.

  • If the frame renders correctly on my GPU, the synchronization is correct.
  • Barriers are only needed when the CPU reads back GPU results.
  • A barrier on all stages after every pass is a safe default with no real cost.
  • Layout transitions are bookkeeping and never touch the image data.
  • Draws and passes on one queue always finish in order, so render passes need no barriers.