Introduction#
Glass, water, smoke, fire, ghosts, user interfaces: games are full of things you can partly see through. The depth buffer handles solid objects beautifully, letting them be drawn in any order. See-through objects break that, and they’re one of the classic headaches of real-time graphics. This post covers:
- Alpha and the blend equation the graphics card uses to mix colours
- The common blend modes: normal, additive, multiply and premultiplied alpha
- Why transparent objects must be sorted and drawn back to front
Alpha and the Blend Equation#
Alongside red, green and blue, a colour can carry a fourth number, alpha (α), which usually means opacity: 1 is fully solid, 0 is invisible, 0.5 lets half the background through.
When blending is turned on, the graphics card doesn’t simply overwrite the pixel in the framebuffer. In the output merging stage (the pipeline), it combines the new fragment’s colour, the source, with the colour already there, the destination, using a formula you choose. For a normal see-through surface it’s:
At α = 0.3, the result is 30% the new surface and 70% whatever was behind it. A program chooses this formula with a single setting: which number to multiply the source by (here α), and which to multiply the destination by (here 1 − α).
Notice that blending reads the destination: the colour already in the framebuffer. So the result depends on what has been drawn before.
Blend Modes#
By changing the two multipliers, the same hardware gives several useful effects. Try them below, with the opacity slider.
Three overlapping discs drawn with different blend modes
- Normal alpha blending is a see-through layer. Where the discs overlap, whichever was drawn last (blue) looks to be on top.
- Additive adds light. Overlaps get brighter, red and green make yellow, and all three make white, just like the coloured lights in Pixels and Colour. It’s used for fire, sparks, lasers and glows. Notice it barely shows over the light background but shines over the dark band. It also doesn’t care about order: addition gives the same result either way round.
- Multiply can only darken, like layers of tinted glass or the coloured filters used on stage lights. It’s used for stains, shadows and decals. It doesn’t care about order either.
- Premultiplied alpha looks identical to normal alpha here. The difference is where the multiplication by α happens: the colour is stored already multiplied by its own alpha, so the blend becomes
source + destination × (1 − α), where source is the (already multiplied) colour being drawn, destination is the colour already on screen, and α is the source’s alpha.
Why premultiplied alpha?#
If it looks the same, why bother? Because colours stored without premultiplying go wrong when they’re filtered or blended in stages. Imagine a texture of a leaf, whose transparent texels are stored as invisible black. When bilinear filtering (part 2 of Textures) blends a green edge texel with a transparent black one, the result is a half-transparent dark green, and the leaf gets a dark fringe. With premultiplied alpha, a transparent texel is (0, 0, 0, 0) and contributes nothing, so filtering just works. It also makes it possible to blend several layers together first and combine the result with the scene later, and lets one blend mode do both normal and additive blending (an α of 0 with a non-zero colour adds light). For these reasons, many engines store and blend premultiplied colours throughout.
Why Order Matters#
Here’s the big problem. Solid objects use the depth buffer to sort themselves out, but a transparent surface can’t simply hide what’s behind it: it has to blend with it. So whatever is behind it must already be in the framebuffer when the transparent surface is drawn. That means transparent objects must be drawn after all the solid ones, and in order from back to front.
The demo below has four see-through panes in front of and behind a solid ball. Drag to look around from different angles, and try the options.
Four transparent panes: draw order and depth writes
With a fixed order and depth writes on, turn the camera round: from some angles, panes vanish where they overlap. A pane drawn early writes its depth, so a pane behind it drawn later fails the depth test and is never blended in. Turning off depth writes stops things vanishing, but the colours still change with the order. Only sorted, far to near looks right from every angle. (The usual recipe is to sort, and keep the depth test on but depth writes off for transparent objects, so they’re still hidden correctly behind solid ones.)
Sorting has limits though. Engines sort whole objects by distance, not individual triangles, so an object that overlaps itself, or two that pass through each other, can still go wrong, and no single order is correct for every pixel. That’s why games sometimes use alpha testing instead (each pixel is either fully solid or discarded, so the depth buffer works again; it’s common for foliage and fences), or more advanced order-independent transparency techniques.
Blending is done by fixed-function hardware next to the framebuffer, often called the ROPs (render output units). It needs to read the existing colour and write a new one, so every blended pixel costs extra memory traffic. Transparent effects like smoke are usually many large, overlapping layers, each blended over every pixel it covers: a big cloud can blend dozens of times per pixel. This overdraw is why particle effects (smoke, sparks and dust made of many small overlapping images) are a common performance problem, and why they’re often drawn into a lower-resolution render target and then scaled up. The Overdraw post explores the cost.
Summary#
- Alpha stores opacity; the blend equation mixes the new colour (source) with what’s already there (destination).
- Normal, additive and multiply blending use the same hardware with different multipliers.
- Premultiplied alpha stores colour × α, which filters and composites correctly.
- Transparent objects must be drawn after solid ones, sorted back to front, usually without writing depth.
Further Reading#
- LearnOpenGL, Blending: blend functions, alpha testing and sorting in OpenGL.
- Tom Forsyth, Premultiplied alpha: the classic argument for premultiplying everything.
- Thomas Porter and Tom Duff, Compositing Digital Images (SIGGRAPH 1984): the paper that defined alpha compositing.
- Morgan McGuire and Louis Bavoil, Weighted Blended Order-Independent Transparency (JCGT 2013): a popular way to avoid sorting.