Drawing

MSAA and alpha-to-coverage

A pixel is a small square, but the rasterizer asks one question per pixel: is its center inside the triangle? The answer is yes or no, so a slanted edge becomes a staircase. Multisample anti-aliasing (MSAA) asks the question at several points in the pixel and keeps the share that is covered, while it still shades each pixel only once. This page explains how, what it costs, how to turn the samples into one color when the light is HDR, and how cut-out materials (leaves, fences) get smooth edges with alpha-to-coverage. The last part shows how RASM does it.

In short

  • MSAA keeps coverage and depth per sample (2, 4 or 8 per pixel) and runs the fragment shader once per pixel per triangle. Edges get the share of the pixel they cover; the inside of a triangle costs what it did.
  • The image holds every sample, so it takes N times the memory. A resolve turns the samples of each pixel into one color for the passes after it.
  • A plain mean of HDR samples keeps bright edges jagged: one sky sample of 8 outweighs three dark ones. Weighting each sample by 1 / (1 + brightness) keeps the edge's in-between shade after tone mapping.
  • MSAA does not smooth a cut-out's edge, which discard makes inside a triangle. Alpha-to-coverage covers samples by alpha instead; a sharpened alpha keeps the edge where the cutoff puts it.
  • A pass that reads the depth (like a Hi-Z build) must read every sample, or a resolved depth: there is no plain mean of depths.

1The problem: one sample per pixel

A triangle's edge crosses a grid of pixels. With one sample at each pixel's center, a pixel is either fully the triangle's color or not at all. With four samples, a pixel the edge cuts takes the triangle's color in proportion to the samples inside.

a sample inside the triangle a sample outside the triangle's edge
1 sample per pixel: all or nothing 4 samples per pixel: the share covered Below the line is the triangle. Gray steps on the right are 1, 2 or 3 of 4 samples covered.
The staircase on the left crawls when the triangle moves by less than a pixel, which is most of the time. On the right a pixel changes by a quarter at a time.

2How MSAA works

The obvious fix is to draw the whole image larger and shrink it (supersampling), which shades every sample. MSAA splits the work:

Supersampling

Every sample runs the fragment shader. Smooth everywhere, also inside textures and highlights.

N shader runs per pixel

MSAA

Coverage and the depth test run per sample. The shader runs once per pixel and its color goes to every covered sample.

1 shader run per pixel and triangle

Where the cost goes

Only pixels on edges run the shader more than once: once per triangle that touches them. The inside of a triangle costs what it did.

memory and bandwidth: N samples stored

MSAA smooths what the rasterizer decides: triangle edges and the depth test where surfaces cross. It does not smooth what the shader decides inside a triangle: thin highlights on shiny surfaces, texture detail, or a discard. Each sample stores a color and a depth, so the image is N times as large:

1920 × 1080Color RGBA16FDepth D32Together
1 sample15.8 MiB7.9 MiB23.7 MiB
2 samples31.6 MiB15.8 MiB47.5 MiB
4 samples63.3 MiB31.6 MiB94.9 MiB
8 samples126.6 MiB63.3 MiB189.8 MiB

GPUs compress such images: when every sample of a pixel holds the same color, which is true away from edges, they read and write it once. So the bandwidth grows less than the memory.

3The resolve: one color from N samples

Later passes (bloom, tone mapping, the screen) want one color per pixel. The resolve makes it. GPUs offer one in the hardware, at the end of a render pass (a resolve attachment): the plain mean of the samples. For an image in display range that is right. For HDR light it is not.

Take one edge pixel: two samples see the sky (8.0), two see a dark roof (0.05). The screen shows bytes after the tone curve (AgX):

248the sky alone8.0 through AgX
70the roof alone0.05 through AgX
159ideal: half of each on screen(248 + 70) / 2
239plain mean, 4.03(8 + 8 + 0.05 + 0.05) / 4, then AgX
197weighted mean, 0.88each sample × 1 / (1 + value), then AgX

The plain mean is almost the sky: one bright sample outweighs the dark ones, so where bright meets dark, the stair stays. That is the most common edge of all: a roof, a tree or a character against the sky. The fix is to average after a tone curve and undo the curve. With the curve x / (1 + x), which can be undone, that is the same as a weighted mean:

resolved = Σ cs ws / Σ ws

ws = 1 / (1 + luminance(cs) · exposure)

cs
the color of sample s, linear HDR light
ws
small for a bright sample, about 1 for a dark one
exposure
the camera's, so "bright" means bright on screen

The weighted mean lands much nearer the ideal. It is not exact, since the real tone curve is not x / (1 + x), but the edge keeps a visible in-between shade. Move the sky's brightness and the samples below:

Raise the sky from 1 to 64 with two samples on the roof: the plain mean stays near the sky's byte, the weighted mean stays near the ideal. With 0 or 4 samples on the roof there is no edge, and all three agree.
A pass instead of a resolve attachment

A resolve attachment can only take the plain mean (or the minimum, maximum or first sample). The weighted mean needs a pass that reads the samples: a compute pass, or a full-screen draw. On desktop GPUs both read the samples once and cost about the same. On tile-based GPUs a resolve attachment can resolve on the chip, without storing the samples, but only for an image that no pass reads in between; a scene drawn in two passes with a depth read between them stores its samples anyway.

4Cut-outs: alpha-to-coverage

A leaf or a fence is often one quad with a texture whose alpha says where the shape is. The shader drops the pixels below a cutoff with discard. The edge is now inside the triangle, decided by the shader, and the shader runs once per pixel: all samples of the pixel are kept or dropped together. MSAA does nothing for it.

Alpha-to-coverage lets the shader's output alpha choose how many samples are covered: alpha 0.5 covers half of them. Two ways to feed it:

Alpha itself

The soft alpha of a texture spreads over many pixels: the leaf turns see-through near its edge, and the cutoff no longer decides the edge.

Sharpened alpha

(alpha − cutoff) / fwidth(alpha) + 0.5: 0.5 exactly at the cutoff, from 0 to 1 over one pixel. The edge stays where the cutoff puts it, and is smooth.

fwidth = |ddx| + |ddy|

fwidth

The change of alpha to the next pixel, from the GPU's screen-space derivatives. Far away it is large, near it is small, so the soft band is always about one pixel wide.

Move the cutoff: the sharpened row's edge follows the dashed line, as discard's does, but over a ramp of samples. Make alpha change slowly (a near leaf) and fast (a far one): the sharpened ramp stays about one pixel wide; alpha as coverage does not.

Alpha-to-coverage only helps with several samples per pixel: with one sample, it can only cover all or nothing at 0.5, which is a cutoff of 0.5 for every material. So a renderer uses it only when the target has samples, and discard otherwise. Shadow maps have one sample and keep discard.

5Passes that read the depth

A multisampled depth has N values per pixel. A pass that reads it, like the build of a Hi-Z pyramid, must say which one it wants. A plain mean of depths is a depth where no surface is, so the choices are:

MS depth resolved Hi-Z

Resolve the depth first

The farthest sample of each pixel into a second depth. One more full-size image and pass; resolve attachments for depth min or max are optional in Vulkan and missing in WebGPU.

1 Hi-Z texel

Read every sample

The Hi-Z already reduces 2 × 2 pixels to one texel: with 4 samples it reads 16 values instead of 4, and keeps the farthest. The same answer, with no second image.

6Pitfalls

Edges against a bright sky still look jagged
The samples were averaged as HDR light. Resolve with weights, or after tone mapping.
Leaves and fences stay jagged with MSAA
They are cut out with discard, inside the triangle. Use alpha-to-coverage.
Cut-outs turn see-through, or their edge moves
Alpha-to-coverage with the texture's soft alpha. Sharpen it around the cutoff.
A shiny surface still sparkles
MSAA shades once per pixel, so highlights inside a triangle alias as before. That needs a temporal method (TAA) or shading per sample.
The pipeline does not match the pass
A pipeline is made for a sample count: one drawing into a 4x target must be made with 4 samples, the depth too.
Out of memory at 8x
Color and depth grow with the samples: 190 MiB at 1080p for RGBA16F and D32. 4x is the common choice.

7In RASM

A render target takes RenderTargetDesc::samples, and the scene passes draw into it with pipelines made for that count. Renderer::addMsaaResolvePass adds the weighted resolve, a compute pass that writes a single-sample storage target; bloom and the tone map read that. With Renderer::render, one line turns it on, before the engine is made:

config.antiAliasing = rasm::AntiAliasing::MSAA_4X;
graph passes targets shader variants
scene passes Scene 1, Scene 2, Transparent scene_hdr, 4 samples RGBA16F + D32 per sample MSAA Resolve weighted mean, compute MASK: sharpened alpha + alpha-to-coverage Hi-Z farthest of every sample scene_hdr_resolved 1 sample, storage target Bloom, Tone Map The shadow maps stay at 1 sample, with discard for MASK.
The shaded view's pipelines are made per sample count (ShadedPipelines, one set each for 1, 2, 4 and 8, its debug view pipelines included, and one more for a single-sample target with a motion color, MOTION_VARIANT), so views with and without MSAA can share a frame.

Where RASM follows this page, and where it differs

PartThis pageRASM
Sample counts2, 4, 8RenderTargetDesc::samples: 1, 2, 4 or 8 (no more, though some GPUs have 16), whatever the device can draw and sample for color and depth (RenderContext::supportsSampleCount). A multisampled target has colors and no history, storage or cube. EngineConfig::antiAliasing (NONE, MSAA_2X, MSAA_4X, MSAA_8X) falls back to the most the device has, with a warning. Its other value, TAA, draws one sample and blends frames instead (TAA): addScenePasses takes a MOTION_FORMAT color only on a target with one sample.
Pipelinesone per sample countPipelineDesc::samples and alphaToCoverage, compared and hashed like every field. Renderer::createPipeline(name, source, renderTarget) takes the target's count; RenderContext::bindPipeline skips the draws of a pipeline whose count does not match the pass.
Resolveweighted meanRenderer::addMsaaResolvePass: the MSAA Resolve compute pass, resolveMain in shaders/passes/msaaResolve.slang, weights 1 / (1 + luminance · exposure) with the camera's exposure. It writes <source>_resolved, a transient storage target of the source's size. addBloomPasses and addToneMapPass refuse a multisampled source.
Cut-outssharpened alpha-to-coverageThe MASK pipeline of a multisampled view is fragMain with RASM_ALPHA_TO_COVERAGE: no discard, alpha = getCoverage, alpha-to-coverage on. Where alpha does not change (no edge), getCoverage covers all or nothing by the cutoff, as discard does, so a cutoff of 1 on opaque texels stays solid. Cut-outs still thin out in the far mips (D25): the sharpening keeps the cutoff's edge, but the mips' alpha is still averaged.
Depth readsevery sample, or a resolved depthEvery sample: hizMain reads SceneData::depthSamples samples of each pixel through Sampler2DMS (loadDepth). No depth resolve.
Shadingonce per pixelThe same; the light clusters do not read depth, so they are unchanged.
See-through layers—Weighted blended surfaces (weighted blended transparency) keep their sums per sample: the view's weighted target has the scene target's samples, and the composite runs once per sample (RASM_PER_SAMPLE, sample-rate shading), or takes each pixel's mean where RenderContext::hasSampleRateShading is false.

What it costs

examples/09_gltf_materials --gpu-times --render-size 1920 1080 --aa …, RTX 3060, Release, the median of 5 runs. The scene is light, so the GPU does not run at full clock and the draw passes vary from run to run by more than MSAA adds to them; the Hi-Z and the resolve grow with the samples:

SamplesHi-ZMSAA ResolveDraw passes (Scene 1, 2, Transparent)Frame's transient memory
10.06 ms–0.67 ms41.3 MiB
20.11 ms0.12 ms0.92 ms67.5 MiB
40.16 ms0.14 ms0.98 ms112.5 MiB
80.41 ms0.38 ms0.81 ms208.1 MiB

Where it lives in code

WhatWhere
Sample countsRenderTargetDesc::samples, TextureDesc::samples, PipelineDesc::samples, PipelineDesc::alphaToCoverage in include/rasm/render/resource.h; checked in RenderContext::createRenderTarget and createTexture (src/rhi/renderContext.cpp); the device's counts from VulkanDevice::supportedSampleCounts
Pipelines per countShadedPipelines and GpuScene::shaded in src/renderer/gpuScene.h; getShadedPipelines and prepareSceneView in src/renderer/gpuScene.cpp
Alpha-to-coveragegetCoverage and toTarget in shaders/passes/rasm.slang
Hi-ZloadDepth in shaders/passes/hiz.slang; SceneData::depthSamples
ResolveRenderer::addMsaaResolvePass in src/renderer/msaaResolve.cpp; shaders/passes/msaaResolve.slang; gfx::ResolvePushConstants
The default frameViewGraph::render in src/renderer/viewGraph.cpp with EngineConfig::antiAliasing
Examplesexamples/09_gltf_materials and examples/02_gltf: --aa msaa2|msaa4|msaa8 (--aa taa for TAA)
Keep these true
  • Every pipeline that draws into a target has the target's sample count; the shaded view's pipelines come from the set of its count.
  • A pass that reads a multisampled image reads it as one (Sampler2DMS), sample by sample; the bloom and tone map read the resolved target.
  • The Hi-Z keeps the farthest sample, never a mean.
  • Alpha-to-coverage only with more than one sample; shadow maps keep discard.

Further reading