Introduction#
A graphics card can run a fragment shader for millions of pixels in a few milliseconds. It does that with a very different design from a normal processor, and that design explains a lot of shader “rules of thumb” that otherwise sound like folklore: why if statements can be slow, why texture reads don’t stall everything, and why using too many variables can hurt. This post, the first in the GPU Performance series, covers:
- SIMD execution: running one instruction on many pixels at once, in groups called warps or wavefronts
- Divergence: what happens when pixels in a group want to take different branches
- Latency hiding: how a graphics card keeps busy while waiting for memory
The demos here are simulations, simplified to show the idea: real hardware is more complicated, but works on these principles.
One Instruction, Many Pixels#
A normal processor core is built to run one stream of instructions as fast as possible, with lots of clever hardware to predict branches and reorder work. A graphics card instead has thousands of simple arithmetic units, and saves hardware by making them share. A group of copies of your shader, one per pixel (or per vertex), all execute the same instruction at the same time, each on its own data. This is SIMD: single instruction, multiple data.
The groups have different names on different hardware: NVIDIA calls them warps (32 copies), AMD calls them wavefronts (64 copies on its older designs; newer ones can use 32 or 64), Apple calls them SIMD groups (32), and the graphics APIs call them subgroups or waves. For a fragment shader, the copies in a group are usually neighbouring pixels on screen, made up of the 2×2 quads described in the ddx and ddy posts.
Sharing one instruction stream between 32 copies means the hardware only has to fetch, decode and schedule each instruction once for all 32, so almost all of the chip can be spent on arithmetic. The price is that the copies must stay in lockstep.
Divergence: When Pixels Disagree#
So what happens with an if?
if (insideTheCircle) {
colour = expensiveLighting(); // the "if" path
} else {
colour = skyColour(); // the "else" path
}
If every pixel in a group agrees, the group simply runs the path they all need. But if some want the if and some the else, the group can’t split up. It runs both paths, one after the other, and for each path it switches off (masks) the lanes that don’t want it. Those lanes sit idle. This is called divergence.
The demo below shows a 32 × 16 patch of pixels, split into groups (outlined). Orange pixels take the expensive path and blue pixels the cheap one. Each group is labelled with the number of cycles it takes (a cycle is one tick of the processor’s clock, long enough for one instruction); divergent groups are outlined in red. Click or tap a group to see its timeline: one column per lane (each copy in a group is called a lane), one row per cycle, grey where a lane is switched off.
Simulated branch divergence in groups of shader copies
Things to try:
- Inside a circle: only the groups along the circle’s edge diverge. Most groups are entirely inside or outside, so they run just one path. This is the normal case in real shaders, and why branches are usually fine: neighbouring pixels tend to agree.
- Alternate columns or Random: every group diverges, every group pays for both paths, and efficiency collapses. A branch that changes from pixel to pixel is the expensive kind.
- Switch to groups of 64: bigger groups are more likely to straddle an edge, so more of them diverge.
So the rule of thumb isn’t “branches are slow”. It’s: branches are cheap when neighbouring pixels agree, and expensive when they don’t. A branch on a uniform value (the same for every pixel) never diverges at all.
Hiding Latency#
The other big difference is how a graphics card deals with memory. Reading a texture can take hundreds of cycles if the data isn’t already in a cache, a small, fast memory of recently used data. A normal processor relies on large caches and predicting what you’ll need. A graphics card mostly doesn’t wait at all: it keeps many groups loaded and ready on each of its processor cores at once, and whenever the running group has to wait for memory, it switches to another group that’s ready, in a single cycle.
The simulation below shows one arithmetic unit shared by several groups. Each group does a few cycles of maths, then waits for a memory read. The top row shows whether the arithmetic unit is busy (green) or idle (red).
Simulated latency hiding: groups taking turns while others wait for memory
With only one or two groups, the arithmetic unit is idle most of the time. Add groups until it’s always busy: their waits overlap, and the latency is hidden. More maths between memory reads helps too, because each group keeps the unit busy for longer.
The rules the simulation follows
Each cycle, the maths unit runs one cycle of work for one group that’s ready (not waiting), taking turns in order. A group that finishes its maths sends off a memory read and waits that many cycles before it’s ready again. If no group is ready, the unit sits idle (red).
That gives a simple rule of thumb. Each group spends (maths + wait) cycles for every maths cycles of real work, so to keep the unit busy all the time you need about:
With a 30-cycle wait and 4 cycles of maths, that’s 1 + 7.5 = 8.5, so 9 groups. Real memory waits can be hundreds of cycles, which is why graphics cards keep so many groups loaded at once.
How many groups fit on a core at once is called occupancy, and it’s limited by the core’s fixed pool of fast storage for each copy’s variables, the registers. A shader that needs many registers (lots of variables alive at once) leaves room for fewer groups, so there are fewer to switch between while waiting for memory. That’s why a long, complicated shader can be slow even if most of its work is cheap, why shader compilers work hard to reduce register use, and why GPU profiling tools always report occupancy.
Summary#
- Graphics cards run shaders in groups (warps or wavefronts) of 32 or 64 that execute in lockstep.
- When copies in a group take different branches, the group runs both paths, with lanes masked: divergence.
- Branches are cheap when neighbouring pixels agree, and costly when they don’t.
- GPUs hide memory latency by switching between many resident groups; how many fit is occupancy, limited by registers.
Further Reading#
- Fabian Giesen, A trip through the Graphics Pipeline, part 8: pixel shaders, quads and how they’re scheduled.
- Wikipedia, Single instruction, multiple threads: the execution model, with links to each vendor’s architecture.
- AMD GPUOpen, Occupancy explained: registers, occupancy and latency hiding on real hardware.
- NVIDIA, CUDA C++ Programming Guide: SIMT Architecture: warps and divergence from the hardware maker.