skip to content

Transparency and Particles

Why transparent surfaces break the depth buffer's order independence: sorting, premultiplied alpha, OIT and particle overdraw. Interviews ask why intersecting glass sorts wrong and what OIT trades.

on this pageshow

explore

questions

6

Why do renderers draw transparent surfaces after all opaque ones, sorted back to front, with depth testing on but depth writes off?

level: juniorimportance: must knowfreq 65%

answer

  1. blending reads what is already drawn
  2. order matters for the over operator
  3. opaque geometry supplies the background
  4. walls must still hide windows
  5. a vanished pane versus a wrong tint

basics

~20 s

Blending 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 s

With 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

for a junior

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.

for a middle

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.

for a senior

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.

for a principal

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.
open as a page

How does a GPU particle system emit, age, move and kill particles each frame and keep a compact list of the living without CPU readback?

level: middleimportance: should knowfreq 38%

basics

~20 s

GPU particles live in a fixed pool with a stack of free slots and alive index lists. Passes pop slots to emit, age and move particles, free the expired, and append survivors to a new list whose atomic count drives an indirect draw.

open as a page

What is premultiplied alpha, and why does it remove dark fringes from filtered sprites and let one blend mode mix additive and alpha-blended particles?

level: middleimportance: should knowfreq 50%

basics

~20 s

Premultiplied alpha stores colour already multiplied by alpha, so transparent texels are zero and add nothing when filtered. The blend becomes C = Cs + (1 - As) * Cd, which is additive when alpha is zero, so one blend state covers both.

open as a page

Glass panes in a greenhouse intersect, and seen from inside they blend in the wrong order even when sorted per object; why, and what do order-independent transparency techniques trade to fix it?

level: seniorimportance: should knowfreq 48%

basics

~20 s

Per-object sorting picks one order for a whole mesh, but intersecting panes are each in front on different pixels, so no object order is right. Order-independent transparency resolves order per pixel: depth peeling costs passes, per-pixel lists cost memory, weighted blending approximates.

open as a page

A smoky explosion of thousands of particles costs several milliseconds of GPU time when the camera is inside it and shows hard lines where puffs meet the ground; why, and how do you fix both?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Inside the cloud each billboard covers much of the screen, so every pixel blends dozens of layers: overdraw sets the cost. Render particles at reduced resolution, trim quads, fade near the camera. Soft particles fade alpha against scene depth, removing the hard lines.

open as a page

For tree foliage drawn as alpha-tested cards, what does alpha-to-coverage improve over a plain alpha test, and what does it depend on?

level: middleimportance: nice to knowfreq 32%

basics

~20 s

Alpha-to-coverage turns alpha into a share of MSAA samples, so leaf edges resolve soft instead of as a hard aliased step, still writing depth without sorting. It needs an MSAA target and an alpha ramp about one pixel wide.

open as a page