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
discardmakes 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.
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 × 1080 | Color RGBA16F | Depth D32 | Together |
|---|---|---|---|
| 1 sample | 15.8 MiB | 7.9 MiB | 23.7 MiB |
| 2 samples | 31.6 MiB | 15.8 MiB | 47.5 MiB |
| 4 samples | 63.3 MiB | 31.6 MiB | 94.9 MiB |
| 8 samples | 126.6 MiB | 63.3 MiB | 189.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):
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:
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
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.
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:
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.
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;
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
| Part | This page | RASM |
|---|---|---|
| Sample counts | 2, 4, 8 | RenderTargetDesc::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. |
| Pipelines | one per sample count | PipelineDesc::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. |
| Resolve | weighted mean | Renderer::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-outs | sharpened alpha-to-coverage | The 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 reads | every sample, or a resolved depth | Every sample: hizMain reads SceneData::depthSamples samples of each pixel through Sampler2DMS (loadDepth). No depth resolve. |
| Shading | once per pixel | The 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:
| Samples | Hi-Z | MSAA Resolve | Draw passes (Scene 1, 2, Transparent) | Frame's transient memory |
|---|---|---|---|---|
| 1 | 0.06 ms | – | 0.67 ms | 41.3 MiB |
| 2 | 0.11 ms | 0.12 ms | 0.92 ms | 67.5 MiB |
| 4 | 0.16 ms | 0.14 ms | 0.98 ms | 112.5 MiB |
| 8 | 0.41 ms | 0.38 ms | 0.81 ms | 208.1 MiB |
Where it lives in code
| What | Where |
|---|---|
| Sample counts | RenderTargetDesc::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 count | ShadedPipelines and GpuScene::shaded in src/renderer/gpuScene.h; getShadedPipelines and prepareSceneView in src/renderer/gpuScene.cpp |
| Alpha-to-coverage | getCoverage and toTarget in shaders/passes/rasm.slang |
| Hi-Z | loadDepth in shaders/passes/hiz.slang; SceneData::depthSamples |
| Resolve | Renderer::addMsaaResolvePass in src/renderer/msaaResolve.cpp; shaders/passes/msaaResolve.slang; gfx::ResolvePushConstants |
| The default frame | ViewGraph::render in src/renderer/viewGraph.cpp with EngineConfig::antiAliasing |
| Examples | examples/09_gltf_materials and examples/02_gltf: --aa msaa2|msaa4|msaa8 (--aa taa for TAA) |
- 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
- Matt Pettineo, "MSAA Overview" and "MSAA Resolve Filters" (therealmjp.github.io, 2012): how MSAA works, and resolves of HDR light.
- Brian Karis, "Tone mapping" (graphicrants.blogspot.com, 2013): the 1 / (1 + luma) weights.
- Ben Golus, "Anti-aliased Alpha Test: The Esoteric Alpha To Coverage" (2017): the sharpened alpha.