With the render thread running one frame behind the main thread, how does double-buffered game state let simulation and rendering overlap, and what does it cost?
answer
- two copies, one swap per frame
- the slower side, not the sum
- input shown one frame later
- deferred release of render data
basics
~20 sThe main thread simulates frame N+1 while the render thread prepares frame N from its own copy of render state, swapped at a sync point. Frame time becomes the slower thread's, not the sum, for about a frame of extra latency and a second copy.
solid answer
~40 sThe main thread writes everything the renderer needs (transforms, which objects exist, material parameters, camera) into one buffer while the render thread reads the other, the copy finished last frame. At a **sync point** they swap, so the main thread runs at most one frame ahead. Frame time becomes `max(main, render)` instead of `main + render`: in an illustrative 12 ms + 9 ms frame that is 12 ms (about 83 fps) instead of 21 ms (about 48 fps). The costs: what the player sees reflects input one frame period older, the copy costs memory and extraction time, gameplay can no longer write render-side data directly, and an object destroyed in gameplay must keep its render data alive until the render thread has finished with it.
go deeper
Recall that the main thread works on the next frame while the render thread prepares the previous one from its own copy of the scene state.
Explain the two buffers, the swap at the sync point, and why frame time becomes the slower thread's time instead of the sum of both.
Quantify the trade, frames gained against a frame of latency, and handle lifetimes of destroyed objects, stale renderer answers and late latching to recover responsiveness.
Decide whether the throughput is worth the latency for this game: a competitive shooter and a strategy game draw that line differently, and the choice shapes every system's ownership rules.
## The problem: two halves in series Without overlap, each frame runs gameplay and simulation on the main thread and then the CPU side of rendering, one after the other. In an illustrative frame where simulation takes 12 ms and render preparation 9 ms, the frame takes 21 ms, about 48 frames per second, and each half leaves the other's core idle for much of the frame. ## Double-buffered render state The fix is to let the render thread work on the **previous** frame while the main thread simulates the **next** one. They cannot share live data, since the main thread would be moving objects while the render thread reads them, so the engine keeps **two copies** of the render-relevant state: 1. During frame N+1, the main thread writes into buffer B: world transforms, the set of objects that exist, material parameters, lights and camera. 2. At the same time, the render thread reads buffer A, the state the main thread finished for frame N, and prepares its draws. 3. At the end of the frame both threads meet at a **sync point**, the buffers swap roles, and both start again. Only render-relevant state is buffered: a compact description of the scene, not the meshes and textures, which both sides share read-only. A common variant replaces the full copy with a **command queue**. Render-side mirror objects persist across frames, and the main thread enqueues changes ("this object moved", "this light was created", "destroy that instance") that the render thread applies at the start of its frame. The principle is the same: each side owns its data and only messages cross. ## What the overlap buys With the halves overlapped, frame time becomes the **slower of the two** instead of their sum: | Arrangement | Frame time (12 ms + 9 ms scenario) | Rate | |---|---|---| | Serial | 12 + 9 = 21 ms | about 48 fps | | Overlapped, one frame apart | max(12, 9) = 12 ms | about 83 fps | The sync point also bounds the gap. If the render thread is slower, the main thread waits at the swap instead of running further ahead, which keeps latency and memory bounded. ## What it costs - **Latency.** Input read at the start of frame N+1 is simulated during frame N+1 and prepared for drawing by the render thread in the following frame period. Counted in frame periods, one more frame stands between input and image than in the serial design. In milliseconds the trade is subtler: in the scenario, CPU-side input-to-hand-off is about 2 x 12 = 24 ms overlapped against 21 ms serial, slightly more latency for 75% more frames. - **Memory and extraction time.** Two copies of the render state, plus the cost of writing the copy every frame. - **Ownership discipline.** Gameplay code must never write render-side data directly; every change crosses through the buffer or the queue. - **Lifetimes.** When gameplay destroys an object, the render thread may still be drawing it from last frame's state. Its render data must be released later, after the render thread has finished that frame, typically by queuing the destruction. - **Stale answers.** Anything gameplay asks of the renderer, such as what was visible last frame or what lies under the cursor, describes an older frame. ## Clawing latency back - **Late latching**: the render thread reads the newest camera or input just before preparing its draws, so the view responds to the latest input even though the rest of the scene is a frame old. - Keep the render side short, so that it reaches the sync point early and the main thread never waits. - Remember that the GPU adds its own queue of frames in flight on top of this; how far the CPU may run ahead of the GPU is a separate, rendering-side setting.
- How can an engine claw back some of the latency the overlap adds?Late-latch what the player feels most: the render thread reads the newest camera or input just before preparing its draws. Keep the render side short so the main thread never waits at the sync point, and keep the main thread from running more than one frame ahead.
- Why not let the render thread read live game objects under a lock instead of copying them?Both threads would lock every object every frame, so they contend constantly and largely serialise again; the render thread could also see half the objects updated and half not, and draw an inconsistent frame. A copy gives a consistent frame for the price of extraction.
- When is a command queue to persistent render-side objects better than copying the whole render state?When most of the scene is static from frame to frame: the render side receives only the changes, so a frame where a few hundred objects move costs a few hundred messages instead of a full copy. A full copy is simpler and competitive when nearly everything changes every frame.
A newspaper prints today's edition from a locked final copy while reporters already write tomorrow's. The presses never wait for the reporters, so more editions come out, but every edition reports news that is a day old.
- Double-buffering game state adds no latency; it only costs memory.
- The render thread can safely read live game objects as long as it only reads.
- Every mesh and texture must be duplicated to fill the second buffer.
- Overlapped, the frame still takes main thread time plus render thread time.
- Gameplay can free an object's render data the moment the object is destroyed.