Drawing

Temporal anti-aliasing

MSAA takes several samples of a pixel in one frame, at a cost in memory, and only for triangle edges. Temporal anti-aliasing (TAA) takes one sample per frame, at a different point of the pixel each frame, and blends the frames. Over a few frames each pixel sees many points, and edges, thin lines, texture detail and highlights all get smooth, for the cost of one full-screen pass. The hard part is motion: the frames before must be found where things were then. This page explains the jitter, the history, motion vectors, how to stop ghosts and blur, and when to start over. The last part shows how RASM does it.

In short

  • Each frame moves the projection by a sub-pixel jitter, so the one sample per pixel lands somewhere else in it. A short sequence (Halton) covers the pixel evenly in 8 frames.
  • A history image keeps the blend of the frames before: each frame adds about 10 % of its own color.
  • Things move, so the history is read where the point was last frame: motion vectors, from the last frame's object transforms and camera.
  • What the history saw that is gone now (a surface that moved away) would ghost. The history is clipped to the colors around the pixel this frame.
  • Resampling every frame blurs, so the history is read with a sharp filter (Catmull-Rom), and textures with a negative mip bias. Bright pixels are weighted down so they do not flicker.
  • On a camera cut, a resize or the first frame, the history is dropped.

1The jitter: a different sample each frame

The rasterizer samples each pixel at its center. Move the whole projection by a fraction of a pixel and it samples another point. TAA moves it every frame along a short sequence that fills the pixel evenly, the Halton sequence in bases 2 and 3:

the sample of frame n, numbered
1 2 3 4 5 6 7 8 one pixel, 8 frames the offset, in clip space clip.x += jitter.x · clip.w jitter.x = 2 · offset.x / width clip.y += jitter.y · clip.w jitter.y = 2 · offset.y / height offset: the Halton point minus 0.5, in pixels, −0.5 to 0.5. Times w, so it is the same shift on screen at every depth, for a perspective and an orthographic camera alike.
A random point each frame would clump and leave gaps; the Halton points fill the pixel evenly at any count. The sequence repeats every 8 frames, so a still image settles into a fixed pattern.

Blending the frames then averages the pixel over these points: an edge that covers a third of the pixel ends at a third of the way between its two colors, as with MSAA. Try it on one edge pixel:

Move the edge and watch the green line settle on the edge's share of the pixel. Raise this frame's weight to 1: the pixel flickers between 0 and 1, which is the jitter without a history. A small weight is smooth but slow to follow changes.

2The history and motion vectors

The history is last frame's output. Each frame mixes a small share of the new frame into it: output = lerp(history, current, 0.1), an exponential mean of about the last 10 frames. In a still scene that is all. But when the camera or an object moves, last frame's color for this pixel is somewhere else in the history. A motion vector says where:

last frame (the history) this frame uv − motion motion = uv now − uv last frame the history is read here
The scene passes write the motion of every pixel into a second color target. It comes from each point's clip position this frame and last frame: last frame's object transform and last frame's camera.

motion = (ndcnow − ndclast) · (0.5, −0.5)

ndcnow
the point through this frame's camera, without the jitter, divided by w
ndclast
the same point of the model through last frame's object transform and last frame's camera
(0.5, −0.5)
from NDC (y up) to texture coordinates (v down)

So the renderer keeps, for every object, the transform it drew last frame, and for every view, last frame's view-projection. A still object under a moving camera has motion too: only the camera changed. The sky is infinitely far: only the camera's turn moves it. At an object's outline, the pixel takes the motion of the nearest surface among its 3 × 3 neighbors, so the outline moves with the object and not with what is behind it.

3Ghosts and clipping

The history is wrong where what it saw is gone: a surface moved away and uncovered what is behind it (a disocclusion), a light turned off, an object left the screen. Read through the motion of what is there now, the history still holds the old color, and fades only by 10 % a frame: a ghost trail.

The fix: what the pixel shows this frame is most likely among the colors around it this frame. So the history is clipped into the range of the 3 × 3 neighbors' colors: their mean plus or minus one standard deviation, in YCoCg (brightness and two color differences, which fits colors tighter than RGB). A history color outside that box is moved toward its center until it is inside.

Move the object at 1 to 3 pixels a frame with clipping off: a trail follows it. Turn clipping on: only the pixel right next to the object, which has the object among its neighbors, keeps a little of it.
each channel cut on its own

Clamp

Each channel is cut to the box. Simple, but the result can be a color that was in neither.

along the line to the center

Clip

The history moves toward the box's center until it is inside: it keeps its direction, so the hue changes less.

mean ± σ, inside min…max

Variance box

Mean ± one standard deviation of the 9 neighbors: tighter than their minimum and maximum, so one odd neighbor does not open the box wide.

4Staying sharp, staying calm

A sharp resample

The history is read at a point between texels every frame. Bilinear filtering would blur it a little each time; a Catmull-Rom filter (5 bilinear taps) keeps edges sharp.

bias −1: this one

A mip bias

The frames together see more detail than one frame. Textures are read one mip sharper (bias −1), or the image looks soft; the blend hides the extra aliasing that would cause in one frame.

compressed

Brightness weights

HDR light can hold one very bright sample (a specular sparkle) that flashes in and out. Blending colors compressed by c / (1 + luminance) and turning the result back weighs it down, the same weights as an HDR MSAA resolve.

5When to start over

The history only helps when last frame shows the same place. It is dropped, and the output is the current frame alone for a frame, when:

CaseWhy
the first framethere is no history yet
a resizethe history's pixels no longer match the screen's
a camera cutthe application jumps to another shot; it says so with a call
a camera jumpthe camera turned or moved so far in one frame that almost nothing of the history fits; the history would show as a ghost of the old view
a point now outside last frame's screenfor that pixel only: there is no history for it

A jump needs a threshold. A turn has a natural one, an angle; a move needs a length that fits the scene's scale, which the camera's near plane gives (a scene in meters has a near plane of about 0.1 m, one in kilometers a larger one). Fast motion that is not a jump keeps the history, but gives the current frame more weight, since fast-moving history is resampled more and blurs.

6TAA and MSAA

MSAATAA
What it smoothstriangle edges and cut-outs (with alpha-to-coverage)everything: edges, thin lines, textures, specular highlights
MemoryN samples of color and depth (95 MiB at 4x, 1080p)a history (two copies) and a motion image
Costmore where there are edgesone full-screen pass, the motion output
Weaknessesno help inside trianglesa little blur, ghosts where clipping fails (see-through surfaces), the first frames after a cut
Needsnothing moremotion vectors, last frame's transforms, a jittered projection

Both together cost both and help little more, so engines usually pick one. Deferred renderers and games with much fine detail use TAA; forward renderers, VR and tools that need a crisp image use MSAA.

7Pitfalls

Trails behind moving objects
No clipping, or wrong motion: an object whose last transform is not kept moves with zero motion and smears.
A still image shakes by a pixel
The jitter is in the image: a pass after TAA read the jittered scene, or the motion was computed with the jitter in it.
Everything is a little blurry
Bilinear history resampling and no mip bias. Use a sharp filter and a negative bias; a light sharpen after TAA is the last resort.
See-through surfaces smear
A glass pane over a moving background keeps the background's motion; its own color is clipped against neighbors that mix both. A known limit of TAA.
A camera cut shows the old shot for a moment
The history was not dropped. Reset it on cuts.
Specular sparkles flicker
Blending raw HDR: weight by brightness.

8In RASM

The scene target gets a second color, MOTION_FORMAT (R16G16_SFLOAT), which the scene passes fill. Renderer::addTaaPass jitters the view and adds the TAA compute pass, which writes a history target that bloom and the tone map read. With Renderer::render, one line turns it on, before the engine is made:

config.antiAliasing = rasm::AntiAliasing::TAA;
CPU, per frame graph passes targets and tables
syncGpuScene instance rows: world, previousWorld prepareSceneView jitter, last view-projection, mip bias scene passes jittered, color + motion scene_hdr: color, motion scene_hdr: depth TAA (compute) nearest motion, clip, blend scene_hdr_taa, last frame's copy readPrevious, read at uv − motion scene_hdr_taa, this frame's copy Bloom, Tone Map
The history target is a storage target with a copy per frame slot (history buffers): the pass reads last frame's copy and writes this frame's, and the graph moves each between SAMPLED and STORAGE.

Last frame's transforms

Each instance row of the GPU scene keeps previousWorld, the rows of the affine world matrix the frame before drew (48 of the row's 164 bytes). It is written only through GpuTable::setRow in the frame's sync, like every row. A sub-mesh that a clip moves (keyframe animation) gets its matrices, previousWorld included, from the Animate pass instead, which reads last frame's matrix from the other frame slot; the shaders read both kinds through getPreviousWorldPosition:

  1. updateTransformrows: new world, previousWorld = the last synced matrix
  2. syncthe new matrix becomes the synced one
  3. this framemotion = new − old
  4. next sync, no moverows again: previousWorld = world
  5. next frameno motion

A skinned or morphed vertex moves inside its mesh too, so one matrix cannot say where it was. The Skin pass writes the posed vertices to one buffer per frame slot, and a vertex keeps its index in both. So the frame before's position is the same vertex in the other slot's buffer (SceneData::previousSkinnedVertsPtr), read by getPreviousObjectPosition and then moved by previousWorld:

Skinned Vertex Buffer 0: the frame before's Skinned Vertex Buffer 1: this frame's, from the Skin pass 7 7 motion = now(7) − before(7) reset (skinnedMotion = 0): before = now
The two slots swap every frame. A new entity, a play that jumps the time, the first setMorphWeights after none, a clearMorphWeights and a growth of the buffers have no frame before, so the Animate pass writes skinnedMotion = 0 for that frame and the shader takes this frame's vertex for both. A smooth change of the weights is motion, like a clip.

Where RASM follows this page, and where it differs

PartThis pageRASM
JitterHalton (2, 3)getTaaJitter: points 1 to TAA_JITTER_PHASES (8), from a counter per scene view (SceneView::jitterFrame), so the same frames give the same pixels. writeViewMotion puts it in the projection as a translation of clip space, which removeJitter takes out of the motion and the sky.
Motionlast transforms, last cameraInstanceData::previousWorld (or an animated sub-mesh's InstanceTransform::previousWorld, from the Animate pass; a skinned or morphed vertex's last position from the other slot's skinned vertices), SceneData::previousViewProjection and jitter. Each view draws in its camera's render space, whose origin moves with the camera, so the rows' last translation is stored as last minus now, and the view keeps last frame's view-projection in world space, in doubles (SceneView::previousViewProjection), moved into this frame's render space (large worlds); the uber, MASK and sky pipelines of a target with a motion color are compiled with RASM_MOTION_VECTORS. A BLEND draw writes zero motion with alpha 0, which its premultiplied blend turns into "keep": see-through surfaces keep the motion behind them.
See-through motion—A sorted see-through draw keeps the motion behind it, a vector feature lays its own over it by its alpha. Weighted surfaces sum motion × alpha × weight, and the composite lays their mean motion over the motion behind by their coverage; additive ones lay theirs by their alpha (weighted blended transparency).
The passnearest motion, clip, sharp resample, weightstaaMain in shaders/passes/taa.slang: the nearest of 3 × 3 depths picks the motion; the history through 5 Catmull-Rom taps; clipped to mean ± 1 σ in YCoCg; blended at 10 % (30 % when the pixel moves 8 pixels a frame or more), on colors compressed by 1 / (1 + luminance · exposure).
Mip biasabout −1TAA_TEXTURE_LOD_BIAS (−1) in SceneData::textureLodBias, read by sampleMaterial for every material texture of a jittered view.
Resetsfirst frame, resize, cut, jumpThe pass keeps its last size, its history's RenderTarget::version (a resize or a new swapchain makes new textures, at any size, under the same handles), camera and Camera::historyResets, in a TaaState. Renderer::resetCameraHistory counts that up. A jump is a turn above TAA_JUMP_ANGLE (30°) or a move above TAA_JUMP_NEAR_PLANES (100) near planes, for an orthographic camera half its width (isCameraJump).
With MSAAusually one or the otherOne or the other: addTaaPass refuses a multisampled source, and AntiAliasing is one value.
Sharpenlast resortNone: side by side with no anti-aliasing and MSAA 4x, the result was not clearly softer.

What it costs

0.37 msthe TAA pass at 1920 × 1080examples/09_gltf_materials --aa taa, RTX 3060, Release, median of 5 runs
+0.13 msthe draw passes with the motion outputScene 1, Scene 2, Transparent: 0.88 → 1.02 ms
7.9 MiBthe motion target at 1080pR16G16_SFLOAT, transient
31.6 MiBthe history at 1080ptwo RGBA16F copies, for the graph's life
+6 MiBthe instance table48 B of the 160 B row × 65536 rows × 2 frame slots

Where it lives in code

WhatWhere
The passRenderer::addTaaPass in src/renderer/taa.cpp; shaders/passes/taa.slang; gfx::TaaPushConstants
Jitter and jumpsgetTaaJitter, isCameraJump and the constants in src/renderer/taa.h; tested in tests/taaTests.cpp
MotionwriteViewMotion and Renderer::writeInstanceRows in src/renderer/gpuScene.cpp; removeJitter, getMotion in shaders/lib/core.slang; shaders/passes/rasm.slang and shaders/passes/sky.slang
Storage historyRenderTargetDesc::storage with history: one storage slot per copy (RenderTarget::storageSlots); the graph moves last frame's copy to SAMPLED for readPrevious
The default frameViewGraph::addView in src/renderer/viewGraph.cpp with EngineConfig::antiAliasing or View::antiAliasing: each view keeps its history target and TaaState in its ViewState, so a new graph (another view added, removed or moved) goes on with the history (Many views of one scene)
Examplesexamples/09_gltf_materials (its lantern turns 1° a frame) and examples/02_gltf (its models turn): --aa taa
Keep these true
  • Instance rows change only through setRow, and the frame's sync is what turns a moved entity's matrix into last frame's.
  • The motion and the sky take the jitter out; nothing after TAA reads the jittered scene.
  • The same frames give the same pixels: the jitter comes from a frame counter, never from time or random numbers.
  • A new kind of moving geometry also needs last frame's vertices, or it smears. Skinning and morph targets get them from the Skin pass's other slot.

Further reading