Introduction#
Rasterisation decides whether a pixel is covered by testing a single point, its centre. That yes-or-no answer is why slanted edges turn into staircases and thin objects break into dashes. These problems are called aliasing, and the “anti-aliasing” option in a game’s settings is about fixing them. This post compares the three classic approaches:
- Supersampling (SSAA): draw everything at a higher resolution, then shrink it
- Multisampling (MSAA): test coverage at several points, but run the shader only once
- Post-process anti-aliasing such as FXAA: find edges in the finished image and blur along them
More Samples per Pixel#
The underlying problem is that one sample per pixel can only say “fully covered” or “not covered”. A pixel that a triangle’s edge cuts through should really be a blend of the triangle and the background, in proportion to how much of the pixel each covers.
The diagram below shows one pixel, magnified, with a triangle’s edge passing through it (drag the edge’s ends). The dots are the points where coverage is tested. Compare one sample with four and eight.
One pixel, a triangle edge, and the points where coverage is tested
With four samples, the pixel can be 0%, 25%, 50%, 75% or 100% covered, so an edge gets in-between shades that smooth out the staircase. The positions aren’t a neat grid: they’re deliberately scattered (these are Direct3D’s standard patterns), so that nearly horizontal and nearly vertical edges, the ones where staircases are most visible, cross the samples at different points and still get several distinct shades.
Comparing the Methods#
The demo below draws a fan of thin spokes at a deliberately low resolution (enlarged so you can see each pixel), using each method for real on your graphics card. It spins by itself, so you can see how the aliasing crawls when things move (press Pause to study a still frame): that crawling is often more distracting than the staircases themselves.
The same scene with no anti-aliasing, supersampling, multisampling and an FXAA-style filter
Supersampling (SSAA)#
The brute-force method: draw the whole frame at twice the width and height (four times the pixels), then average each 2 × 2 block down to one pixel. It fixes every kind of aliasing, including inside textures and shaders, but it multiplies everything by four: fragment shader runs, memory and memory traffic. It’s rarely used in games except as a high-end option, though its idea lives on in rendering at a higher resolution and downscaling.
Multisampling (MSAA)#
The graphics card’s built-in solution, and a very clever one. Aliasing mostly happens at the edges of triangles, and it’s the coverage test that causes it, not the shading. So MSAA tests coverage and depth at several sample points per pixel (4 here), but runs the fragment shader only once per pixel for each triangle, and stores that one colour into every sample the triangle covers. At the end, a resolve step averages each pixel’s samples. Edges come out as smooth as supersampling, for a fraction of the shading cost.
It has limits: it only smooths triangle edges, not aliasing inside a surface, such as shimmering textures or shiny highlights. It needs memory for every sample. And it fits poorly with deferred renderers, which most modern games use for lighting: they store surface data in several render targets and light it later, and multisampling all of those is expensive. That’s a big reason it has become less common.
FXAA and post-process anti-aliasing#
The opposite approach: draw the image normally, with no anti-aliasing, then run one extra full-screen pass (post-processing) that looks for high-contrast edges and blurs along them, not across them. FXAA (Fast Approximate Anti-Aliasing, published by Timothy Lottes at NVIDIA in 2009) is the best known; the demo uses a simplified version of the same idea. It’s extremely cheap and works with any renderer. But it only sees the finished pixels, so it can’t recover detail that was lost: thin spokes that already broke into dashes stay broken, and it slightly softens the whole image, including text and textures.
The steps the demo's FXAA-style filter takes
For every pixel of the finished image:
- Measure brightness of the pixel and its four diagonal neighbours, using luma: 0.299 × red + 0.587 × green + 0.114 × blue (green counts most because our eyes are most sensitive to it).
- Skip flat areas: if the brightest and darkest of the five differ by only a little (less than 0.05, or an eighth of the brightest), it isn’t an edge, so the pixel is left alone. This keeps the filter from blurring everything.
- Find the edge’s direction: compare the top pair of neighbours with the bottom pair, and the left pair with the right pair. The edge runs at right angles to the way brightness changes fastest.
- Blur along it: average two samples taken a little way along the edge, one either side of the pixel, and, for a wider blur, two more a bit further out.
- Sanity check: if the wider average comes out brighter or darker than any of the original five pixels, it reached across into something else, so the closer average is used instead.
The real FXAA adds a search along the edge to find where it ends, which handles long, nearly horizontal edges much better.
MSAA is efficient partly because of compression in the hardware. In the middle of a triangle, all four samples of a pixel get the same colour, so graphics cards store a flag saying “all samples identical” and only spend memory traffic on the full set of samples along edges. WebGL2 lets a program draw into a multisampled image and then resolve it, which is what the demo does; the WebGL canvas itself is also multisampled by default. Today, most games use temporal anti-aliasing (TAA) instead: each frame, the sample position inside every pixel is shifted slightly, and the results are blended with previous frames (render targets explains the ping-pong trick behind it). Over a few frames, every pixel collects many samples, at the cost of some blur, and faint trails (“ghosting”) behind moving objects. Modern upscalers build on the same idea.
Summary#
- Aliasing comes from testing one point per pixel; anti-aliasing uses more information.
- SSAA draws at a higher resolution and averages: best quality, highest cost.
- MSAA tests coverage at several points but shades once per pixel per triangle: smooth edges, cheaply, but only edges.
- FXAA-style filters blur along edges in the finished image: very cheap, but can’t recover lost detail.
- Most modern games use temporal anti-aliasing, gathering samples over several frames.
That completes the Render Targets & Post-Processing series! Next, Lighting & Shadows makes surfaces look detailed and solid.
Further Reading#
- Matt Pettineo, A Quick Overview of MSAA: how multisampling really works, in detail.
- Timothy Lottes, FXAA (NVIDIA white paper, 2009): the original description of FXAA.
- Microsoft, D3D11_STANDARD_MULTISAMPLE_QUALITY_LEVELS: Direct3D’s standard sample patterns, with diagrams.
- Brian Karis, High-Quality Temporal Supersampling (SIGGRAPH 2014 Advances course): how Unreal Engine’s temporal anti-aliasing works.