skip to content

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%

answer

  1. keep or discard is binary
  2. MSAA only sees triangle edges
  3. alpha becomes a sample mask
  4. samples stay opaque and depth-tested
  5. distant mips thin the leaves

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.

solid answer

~50 s

A plain alpha test keeps or discards the whole fragment at a threshold such as 0.5, so a leaf card's edge is binary and crawls as the tree sways. MSAA alone does not help: it gets extra coverage only from triangle edges, and the leaf outline lies inside the card, decided once per pixel by the fragment program. Alpha-to-coverage converts alpha into a sample mask instead: with 4 samples, alpha 0.5 keeps about 2. Kept samples write colour and depth like opaque geometry, so foliage stays order-independent, and the resolve averages them into a soft edge. It depends on an MSAA target, gives only sample count plus one coverage levels per pixel, and needs alpha that ramps over about one pixel, often rescaled with its screen-space rate of change, and mips that do not thin the leaves at a distance.

go deeper

for a junior

Recall that an alpha test keeps or discards each fragment, and that alpha-to-coverage turns alpha into a share of MSAA samples instead.

for a middle

Explain why MSAA alone leaves cutout edges hard, how alpha maps to a sample mask, and why the result stays order-independent with depth writes on.

for a senior

Diagnose bald distant trees and screen-door dither, and fix them with coverage-preserving mips and an alpha ramp rescaled to about one pixel.

for a principal

Choose a foliage strategy the renderer can keep: alpha-to-coverage only fits if MSAA stays in the frame, otherwise budget for a dithered test and temporal filtering.

## Alpha testing: cheap, sharp and aliased Foliage is usually drawn as **cards**: a few quads per branch carrying a leaf texture whose alpha channel marks where the leaves are. The cheapest way to cut the leaves out is an **alpha test**: the fragment program compares alpha with a threshold such as 0.5 and discards the fragment below it. Surviving fragments are opaque, so they write depth and need no sorting, which is why alpha testing beats alpha blending for dense trees. The cost is a **binary edge**. Each pixel is either leaf or not, so leaf outlines form stair steps, and as the tree sways or the camera moves those steps crawl and sparkle. ## Why MSAA alone does not fix it **Multisample anti-aliasing** stores several coverage samples per pixel but normally runs the fragment program once per pixel. Extra coverage comes only from the **triangle edge**: samples inside the triangle are covered and samples outside are not. A leaf outline is not a triangle edge; it lies inside the card and is decided by the fragment program's single alpha test. Every covered sample in that pixel gets the same keep-or-discard verdict, so after the resolve the edge is as hard as before. How MSAA samples and resolves geometric edges in general is an anti-aliasing topic; here it matters only as the place alpha can be spread. ## How alpha-to-coverage works 1. The fragment program outputs alpha as usual. 2. The hardware converts alpha into a **sample mask**: with 4 samples, alpha 0.25 keeps about one sample, 0.5 about two and 1.0 all four. 3. That mask is combined with the triangle's own geometric coverage. 4. Kept samples are written as **opaque**: colour and depth, with no blending. 5. The **resolve** averages the samples, so a pixel half covered by leaf ends up half leaf colour and half background. Because every kept sample is opaque and depth-tested, the result does not depend on draw order. Foliage keeps the **order independence** of alpha testing and gains soft edges. ## What it depends on - **An MSAA target.** Without several samples per pixel there is nothing to spread alpha over. - **The sample count.** N samples give only N + 1 coverage levels per pixel, from zero to N kept samples: 5 levels at 4x, 9 at 8x. Some GPUs vary the pattern across neighbouring pixels to fake more levels, at the price of visible dither. It is a cutout technique, not translucency. - **A ramp about one pixel wide.** Blurry texture alpha spreads the transition over many pixels and makes leaves look soft and dithered; binary alpha gives nothing to convert. A common fix rescales alpha around the threshold by its screen-space rate of change, so the ramp spans about one pixel at any distance. - **Alpha that survives mipmapping**, covered below. ## Where it falls short | | Alpha test | Alpha-to-coverage | Alpha blend | |---|---|---|---| | Edge quality | hard, aliased | soft, N + 1 levels | smooth | | Needs sorting | no | no | yes | | Writes depth | yes | yes | normally no | | Needs MSAA | no | yes | no | | Real translucency | no | no | yes | - **Fixed patterns stack badly.** Many GPUs map an alpha value to a fixed sample pattern, so two overlapping leaves with alpha 0.5 can cover the same two samples and show 50% coverage, where two half-transparent layers should show 75%. - **Visible dither.** With few samples, intermediate alpha reads as a screen-door pattern, most visibly on large semi-transparent areas. - **Early depth testing.** As with an alpha test, letting the fragment program decide coverage can stop many GPUs from resolving depth before shading; a depth pre-pass for foliage is the usual workaround. Renderers that do not use MSAA approximate the same idea with a **dithered alpha test**: a per-pixel threshold pattern whose result a temporal filter averages over several frames. ## Keeping distant foliage from thinning Mip levels average alpha. If 30% of a leaf texture's texels are opaque and the rest are transparent, a small mip holds alpha near 0.3 everywhere. That is below a 0.5 threshold, so the alpha test discards almost everything and **distant trees go bald**. Two remedies: - **Coverage-preserving mips:** when generating each mip, scale its alpha so the fraction of texels that pass the threshold matches the full-resolution level. - **Alpha-to-coverage itself**, while alpha is used unsharpened: alpha 0.3 becomes roughly 30% sample coverage instead of nothing, so thin, distant foliage keeps about the right density. Once alpha is sharpened around the threshold, as above, coverage-preserving mips are needed as well.

  • Why does alpha-tested foliage thin out or vanish in the distance, and how do you stop it?
    Mips average alpha, so a texture whose leaves cover 30% of texels has alpha near 0.3 at small mips, below a 0.5 threshold, and almost every fragment is discarded. Generating mips that preserve coverage, by scaling each level's alpha so the same fraction of texels passes the threshold, keeps the density. Alpha-to-coverage also helps, because unsharpened alpha of 0.3 becomes partial sample coverage instead of nothing.
  • What do renderers without MSAA use instead of alpha-to-coverage for foliage?
    A dithered or stochastic alpha test: each pixel compares alpha with a threshold taken from a varying pattern, and a temporal filter averages the pattern over several frames into partial coverage. It keeps depth writes and needs no sort, but it leans on the temporal filter, so fast motion can make the foliage shimmer or smear.

  • Turning on MSAA anti-aliases alpha-tested leaf edges by itself.
  • Alpha-to-coverage blends foliage, so the cards must be sorted back to front.
  • Alpha-to-coverage works the same on a render target without MSAA.
  • Alpha-to-coverage gives as many opacity levels as an 8-bit alpha channel.
  • Distant foliage thins out because the depth buffer loses precision there.