Why do renderers draw transparent surfaces after all opaque ones, sorted back to front, with depth testing on but depth writes off?
answer
- blending reads what is already drawn
- order matters for the over operator
- opaque geometry supplies the background
- walls must still hide windows
- a vanished pane versus a wrong tint
basics
~20 sBlending mixes each surface with the colour already drawn, and over depends on order. So opaque geometry goes first, transparent surfaces follow back to front, the depth test hides glass behind walls, and no depth writes keeps transparent surfaces from rejecting each other.
solid answer
~50 sWith straight alpha the over operator is `C = As * Cs + (1 - As) * Cd`: the new colour is mixed with whatever is already in the target, so the target must already hold everything behind the surface. Drawing all opaque geometry first provides that background and fills the depth buffer. Transparent objects are then sorted by view distance and drawn back to front, because over is not commutative: red glass over blue is not blue over red. The depth test stays on so a wall in front of a window still hides it. Depth writes go off because per-object sorting is never perfect: a nearer transparent surface that wrote depth would make a farther one drawn after it fail the test and vanish, instead of merely blending in a slightly wrong order. Commutative modes such as additive blending need no sort at all.
go deeper
Recall the recipe in order: opaque first, then transparent back to front, depth test on, depth writes off. Be ready to say what each step prevents.
Write the over operator, show that swapping two layers changes the pixel, and explain why a stray depth write makes a pane vanish instead of blending slightly wrong.
Explain where per-object sorting fails, with intersecting and self-overlapping meshes, what strict depth order costs in batching, and which effects can move to an order-free blend mode.
Weigh how far content rules, mesh splitting and order-free blend modes can carry a project's transparency before a dedicated order-independent technique earns its cost.
## What a blend computes When a fragment of a transparent surface reaches blending, the GPU does not simply write its colour. It reads the colour already stored in the target, the **destination** `Cd`, and combines it with the new fragment's colour, the **source** `Cs`, using the fragment's opacity `As`. The usual mode is the **over operator**. With **straight (non-premultiplied) alpha**, which this answer assumes throughout: `C = As * Cs + (1 - As) * Cd` A pane with `As = 0.3` contributes 30% of its own colour and lets 70% of whatever is behind it through. The formula carries a hard requirement: everything behind the surface must **already be in the target** when the surface is drawn. ## Why the order of layers matters Take a red pane `(1, 0, 0)` and a blue pane `(0, 0, 1)`, both with alpha 0.5, over a black background. - **Blue first, then red:** after blue the pixel is `(0, 0, 0.5)`; red over that gives `(0.5, 0, 0.25)`, a red-leaning purple. - **Red first, then blue:** after red the pixel is `(0.5, 0, 0)`; blue over that gives `(0.25, 0, 0.5)`, a blue-leaning purple. The over operator is **not commutative**: the layer drawn last dominates. Drawing transparent surfaces **back to front**, the painter's algorithm, makes the last layer drawn the nearest one, which is what a viewer expects to dominate. ## The standard recipe 1. Draw all **opaque** geometry first, with depth testing and depth writes on. This fills the colour target with the background and the depth buffer with the nearest opaque surface at every pixel. 2. Collect the transparent objects and compute one sort key per object, usually its distance from the camera or its depth along the view direction, taken at the centre of its bounds. 3. Sort them **farthest first** and draw them in that order with blending enabled. 4. Keep the **depth test on** and turn **depth writes off** for this pass. ## Why the depth test stays on and depth writes go off - **Depth test on:** a window behind a wall must stay hidden. Testing against the opaque depth from step 1 rejects transparent fragments that lie behind opaque geometry. - **Depth writes off:** the sort is per object and therefore approximate. If a transparent surface wrote depth and a farther transparent surface happened to be drawn after it, the farther one would fail the depth test and **disappear**. With writes off, the same sorting mistake costs only a slightly wrong blend order, which is far less visible than a missing pane. - **Opaque depth stays clean:** later passes that read the scene depth, such as particles fading against the surfaces behind them, see only opaque geometry. A deliberate exception exists: a transparent object can be given its own depth pre-pass so only its nearest surface shows, for example a ghostly character that should not reveal its own inside. That trades correct layering for a clean silhouette; it is a per-effect choice, not the default. ## Where per-object sorting breaks One sort key per object gives one order for every pixel the object covers. That fails when: - two objects **intersect**, so each is in front of the other on different parts of the screen; - one mesh **overlaps itself**, such as a glass bottle or a looped ribbon, because triangles inside one draw blend in the order the index data lists them, not in depth order; - a large object sits near a small one, and the large object's centre is a poor stand-in for its nearest part. The fixes run from splitting meshes and sorting triangles to **order-independent transparency**, which resolves order per pixel at a cost in passes, memory or accuracy. ## Blend modes that do not need sorting | Mode | Formula | Order-dependent? | Typical use | |---|---|---|---| | Over, straight alpha | `As * Cs + (1 - As) * Cd` | yes | glass, smoke, UI panels | | Additive | `Cs + Cd`, often `As * Cs + Cd` | no | fire, sparks, glows | | Multiplicative | `Cs * Cd` | no | tinted glass seen as a filter, darkening decals | Addition and multiplication are commutative, so **additive** and **multiplicative** effects can be drawn in any order and batched freely, up to rounding. The price is expressiveness: additive light can only brighten and cannot hide what is behind it, so dark, occluding smoke still needs the over operator and a sort. Sorting also costs more than the sort itself. Drawing strictly by depth breaks batching by material, because neighbours in depth rarely share a shader or textures, so a scene with many transparent objects pays in state changes as well as in sorting work.
- How do you pick the sort key for transparent objects, and what does sorting cost?Usually each object's distance from the camera, or its depth along the view direction, taken at a point such as the centre of its bounds. Both approximate per-pixel order, so large objects can sort wrong while their centres look right. The sort runs per frame over only the transparent list, so it is cheap; the bigger cost is that strict depth order breaks batching by material, since neighbours in depth rarely share state.
- Why can additive particles skip the sort?Additive blending computes `C = Cs + Cd`, often with `Cs` scaled by alpha, and addition is commutative and associative, so any draw order gives the same sum up to rounding. Multiplicative blending is order-free for the same reason. That is why fire, sparks and glows are often additive and batched in one draw. The catch is that additive light only brightens: it cannot draw dark smoke or hide what lies behind it.
A painter glazing a canvas works from the background forward: each translucent glaze tints what is already on the canvas beneath it. Apply a near glaze before the far one and the colours stack in the wrong order, and no later step can undo it.
- Transparent objects should be drawn front to back, like opaque ones, to save fill rate.
- Turning the depth test off is what makes transparent blending work.
- Sorting objects by their centres gives the correct order at every pixel.
- Blend order does not matter because the GPU averages all the layers.
- Depth writes must stay on so transparent surfaces occlude each other correctly.