skip to content

How do fences let the CPU record two or three frames ahead of the GPU, and what breaks when a per-frame buffer is reused early?

level: middleimportance: must knowfreq 52%

answer

  1. CPU and GPU overlap, not alternate
  2. a slot per frame in flight
  3. wait on the slot's last fence
  4. the GPU still reads that memory

basics

~20 s

The GPU signals a fence when a frame's work completes. Each frame in flight has its own buffers; before reusing a set, the CPU waits on the fence of the frame that last used it, or overwrites data the GPU still reads.

solid answer

~50 s

The CPU records frame N+1 while the GPU executes frame N, so the two overlap instead of alternating. Everything the CPU writes per frame (command memory pools, constant data, upload space) exists once per **frame in flight**, indexed by `frameNumber mod framesInFlight`. After submitting a frame, the renderer asks the queue to signal a **fence** value; before reusing that slot, the CPU waits until the fence reaches it. With two frames in flight, frame 7 waits on frame 5's fence. Without the wait, or with one shared copy, the CPU overwrites transforms the GPU is still reading, so objects jitter, and resetting a pool the GPU is executing can lose the device. More frames in flight give more CPU slack but cost memory and can add up to a frame of latency each when GPU-bound.

code

pseudocode · 16 lines
pseudocode
FRAMES_IN_FLIGHT = 2
slotFenceValue = [0, 0]          # fence value each slot last signalled
nextFenceValue = 1
frameNumber = 0

loop every frame:
    slot = frameNumber mod FRAMES_IN_FLIGHT
    waitUntil(fence.completedValue >= slotFenceValue[slot])
    resetCommandMemory(pools[slot])
    rewindConstantRing(constantRings[slot])
    commands = recordFrame(pools[slot], constantRings[slot])
    submit(graphicsQueue, commands)
    signal(graphicsQueue, fence, nextFenceValue)
    slotFenceValue[slot] = nextFenceValue
    nextFenceValue = nextFenceValue + 1
    frameNumber = frameNumber + 1

go deeper

for a junior

Recall that the CPU records ahead of the GPU and that a fence is how the CPU learns the GPU has finished a frame.

for a middle

Explain the slot index, the fence value stored per slot, and which frame's fence frame 7 waits on with two frames in flight, plus the symptoms of skipping the wait.

for a senior

Diagnose jitter or device loss from early reuse, extend the fence rule to deferred deletion, and justify two versus three frames in flight against latency and memory.

for a principal

Treat the frames-in-flight count as a latency and memory decision shared with gameplay and display pacing, and set it per platform from measured CPU variance.

## Why the CPU runs ahead The CPU records a frame's commands and the GPU executes them later. If the CPU recorded a frame, submitted it and then waited for the GPU to finish before starting the next one, each side would sit idle while the other worked, and the frame time would be roughly CPU time plus GPU time. Letting the CPU record frame N+1 while the GPU executes frame N overlaps them, so the frame time approaches the larger of the two instead. The number of frames the CPU may be ahead is the **frames-in-flight** count, usually two or three. ## What a fence is A **fence** is a synchronization object that the GPU signals and the CPU can test or wait on. The renderer submits a frame's command buffers and then asks the queue to signal the fence (in many APIs, to write an increasing value) once everything submitted before it has completed. Later the CPU asks whether the fence has reached the value recorded for that frame; if it has, the GPU is finished with that frame's memory. Fences are easy to confuse with the other synchronization primitives: | Primitive | Who waits | For what | |---|---|---| | Fence | the CPU | the GPU finishing submitted work | | Barrier | later GPU work in one queue | earlier GPU work in the same queue | | Queue-to-queue signal | one GPU queue | another queue's work (some APIs reuse the fence object for this) | ## The per-frame ring Anything the CPU writes for a frame and the GPU reads while executing it must exist **once per frame in flight**: - command memory pools and the command buffers recorded from them; - per-frame constant data, such as camera and object transforms; - per-frame upload space and transient binding tables; - query results the CPU reads back. The loop for each frame: 1. Compute the slot: `slot = frameNumber mod framesInFlight`. 2. Wait until the fence reaches the value recorded when this slot was last submitted, by frame `frameNumber - framesInFlight`. 3. Reset the slot's command memory pools and rewind its constant ring. 4. Record and submit, signal the fence with a new value, and store that value for the slot. With two frames in flight, frame 7 uses slot 1 and waits on frame 5's fence; frame 6, in slot 0, may still be executing, and that overlap is the point. Where the wait sits matters as much as whether it exists. Wait as late as possible, just before the CPU first writes into the slot, never straight after submitting: a renderer that blocks on the fence of the frame it has just submitted has quietly reduced itself to one frame in flight, and the CPU and GPU alternate again. ## What breaks when a buffer is reused too early Skipping step 2, or keeping a single copy of per-frame data, lets the CPU overwrite memory the GPU is still reading. Explicit APIs do not lock that memory for you. Typical symptoms: - objects jitter or tear, because some draws of frame N read frame N+1's transforms; - UI or particles flicker when their per-frame vertices are rewritten mid-draw; - a device-lost error or crash when a command memory pool is reset while the GPU is executing buffers from it; - bugs that vanish under a debugger or at a lower frame rate, because timing decides who wins the race. The same rule covers deletion. A resource released on the CPU must stay alive until the fence of the last frame that used it has passed, so renderers queue deletions against fence values and free them later. ## Choosing the count - **One** frame in flight serializes the CPU and the GPU. - **Two** overlaps them and is the common choice. - **Three** absorbs uneven CPU frames, at the cost of another copy of every per-frame resource and more latency. When the GPU is the bottleneck, the queue of recorded but unexecuted frames fills up, and each extra queued frame can add up to one frame time between reading input and showing its result. At a frame time of 33.3 ms, going from two to three frames in flight can add up to about 33 ms. How many swapchain images exist and how presentation is paced against the display are loop-timing choices covered with the game loop; the frames-in-flight count bounds how far CPU recording runs ahead, and the two numbers need not be equal.

  • How do you delete a texture safely when up to two frames may still reference it on the GPU?
    Do not free it at once. Push it onto a deferred-deletion queue tagged with the fence value of the current frame, and each frame free every entry whose fence value the GPU has reached. That guarantees no in-flight frame can still read the memory.
  • Why can the bug of a missing fence wait disappear when you attach a debugger?
    The bug is a race between the CPU writing a slot and the GPU reading it. A debugger, logging or a lower frame rate slows the CPU, so the GPU usually finishes the old frame before the CPU overwrites it. The race is still there; only the timing hides it.

A kitchen with two trays: the cook loads the next order onto a free tray while the waiter carries the other one out. Before loading a tray, the cook checks that it has come back empty; that check is the fence. Load a tray still out on the floor and two orders get mixed.

  • The CPU should wait for the GPU to finish each frame before recording the next.
  • One copy of the per-frame constant buffer is enough if it is written at the start of the frame.
  • More frames in flight always make the game feel smoother and more responsive.
  • A fence and a barrier are the same thing under two names.
  • The frames-in-flight count must always equal the number of swapchain images.