taa.renderFeature
This package holds more than this page lists. See all of its files in its world.
README.md
taa.renderFeature
One compute pass in the temporal stage and the depth it restates for the
passes after it, its sharpening in the display
stage after the debanding and the edge antialiasing, and a per-frame offset to where the projection
samples.
What it does
A pixel is one sample of a continuous image. Which point inside the pixel that sample is taken at is normally fixed forever, and that is what makes an edge resolve to the same staircase in every frame — the frames agree because they all ask the same question.
This pass moves the sample. Each render the projection is offset by a sub-pixel
amount (renderer.setProjectionJitter), so consecutive renders are different
estimates of the same image, and the pass keeps a running result across them.
After a handful of frames that result holds detail no single frame contains: a
silhouette becomes a gradient, a specular highlight stops flashing on and off as
it crosses the sample point, fine texture stops crawling.
Where the samples go matters. The offsets come from a Halton sequence in bases 2 and 3 — successive points of a low-discrepancy sequence avoid each other, so a short run of frames covers the pixel evenly, where independent random points clump and leave gaps and a regular grid repeats as a visible texture. The two bases are coprime, so the axes do not fall into step. Eight offsets is enough to resolve an edge and short enough that the result refreshes before anything drifts.
Carrying the result forward
The accumulated result is only usable at a pixel where it describes the same surface. These tests decide that, in order.
Where to look. @scene.motion gives the screen-space velocity of the surface
under each pixel — where it was in the previous frame. The velocity is read from
the closest-depth pixel of the 3x3 neighbourhood rather than from the pixel
itself: along a moving silhouette the pixel's own velocity belongs to whichever
of the two surfaces won the depth test, and the nearer surface is the one whose
edge is sweeping across the pixel. The history is read there through a
Catmull-Rom spline rather than bilinearly: a bilinear read averages the four
texels around the point, so a history reprojected a fraction of a pixel every
frame softens a little more each frame, and a slow pan blurs thin lines away.
Whether to look at all. A reprojection landing outside the frame has nothing recorded there. That pixel starts its own accumulation from the current frame rather than inventing one.
Whether something used to stand in front. Each render also records how far from the camera every pixel's surface stood (a second history target). Where the previous render held a surface well in front of the one here now, what reprojects onto the pixel is whatever used to cover it, and the pixel starts over from the current frame. The farthest of the four recorded texels around the reprojected point is the one compared, so a thin object whose edge falls on a different side of a texel each render does not read as having moved away.
Whether the answer is still about this surface. Something newly uncovered reprojects to wherever the thing that was covering it went, and reads as a perfectly valid sample of the wrong surface. That is the smear temporal antialiasing is known for. The neighbourhood's own colour statistics bound it: the 3x3 block around the pixel describes what colours belong there now, so the carried sample is clipped into the range the mean and standard deviation of that block describe before it is trusted.
Mean and spread rather than min and max, because a single bright texel stretches the extremes far enough to admit a sample that is nothing like the pixel, while the standard deviation stays honest about how much the block actually varies.
The clip runs in YCoCg and along the line from the neighbourhood mean. Luminance is one axis there, so the box tracks a neighbourhood that varies mostly in brightness far more tightly than an RGB box, whose corners admit colours the neighbourhood never contained. Moving the sample along a line rather than clamping three channels independently keeps its hue, so the result is a colour between two the frame actually holds.
How much of each frame goes in
Every colour is weighed in a reversible tonemapped space (each divided by one plus its luminance, and the result expanded again after), so a highlight many times brighter than its surroundings counts in proportion to how it looks rather than to how much light it carries, and the pixel holds steady as the jitter crosses one bright sample.
The frame's sample is not the pixel's own raster sample alone. Each render's
projection moves every sample by the same sub-pixel offset, so the pass knows
where each of the 3x3 samples around the pixel landed, and weights each by its
distance from the pixel's centre (temporal_reconstruct). The pixel's own
sample flips between the two sides of every edge the jitter crosses; the
weighted estimate moves little as the offset changes.
Every pixel's history records how many renders it holds (a third history target, which also carries the spread below). This render's estimate takes its share as one sample among them, so a history a few renders long settles in as many renders as it holds, from a cut, from a disocclusion and from a camera stop alike. How many a history may hold depends on whether its surface moves:
- Still (its surface moved under a quarter of a pixel since the previous
render):
stillSamplesof them, several whole passes through the jitter sequence, so the accumulation settles on the average of the renders the sequence draws instead of rippling as the samples visit the pixel. - Moving: as many as
historyWeightkeeps, fewer asmotionRejectionshortens the memory for fast motion; twice that where the history passes the clip untouched (it then differs from the estimate only by where the jitter put it), and the weight's own count where the clip moved it, so real change is followed.
A still pixel also carries how far the jitter moves its estimates: their variance about the history, kept over about one pass through the jitter sequence. An edge thinner than the 3x3 samples, a blade of grass or a frond seen from a distance, drops out of some renders' neighbourhood altogether, so the box those samples span says nothing of it and the clip would pull the history to whichever side the jitter landed on. An estimate within two standard deviations of that spread is the jitter, and the history stands unclipped; one past three is a change, and the clip follows it. The spread is trusted once it has seen two passes through the sequence, and a pixel that moves or is disoccluded starts it over.
Sharpening the shown picture
sharpness runs rcas, a contrast-adaptive sharpening of the picture after the
display transform, in the display stage. It reads what the accumulation
produced and writes only what is shown, so it is applied once per frame and
never compounds through the history. Its lobe is limited per pixel and its
result held inside the range the pixel and its four neighbours span, so an edge
gets steeper without a bright halo along a dark line or a dark one along a
bright line.
Velocity and the offset
The offset is folded into the projection, so it would otherwise show up in
velocity as a sub-pixel wobble on a scene that is not moving. The engine
publishes the offset alongside the matrix and the vertex stage subtracts it back
out, so @scene.motion describes the surfaces and a still scene reports no
motion however the samples are placed.
What the passes after it read
The resolved picture samples every pixel at its centre; the raster under it sampled this render's offset. An effect after the resolve that draws from the scene's depth (an outline found where the depth steps, a pattern laid on the surfaces through the world position the depth reconstructs) would draw from the raster and move by up to half a pixel on every frame of a still view, over a picture that holds still.
So the resolve is followed by two passes in the same stage. stable_depth
keeps, for every pixel, the depth sample taken nearest the pixel centre for as
long as no surface around the pixel moves and the sample still lies among the
depths its neighbourhood holds; over one pass through the offsets a still pixel
settles on one sample and keeps it. A fragment pass whose output is
@scene.depth (restates = true) then restates the frame's depth from it:
every pass after it reads that depth through @scene.depth, the post
framework's scene_depth and zero::scene_depth, and reads the camera with no
sub-pixel offset through @frame.camera and engine.view_proj /
engine.inv_view_proj. Those two passes name @scene.depth and the kept
depth in whenRead: on a frame where nothing after them reads the depth (a
pass binding @scene.depth or drawing with a shader that samples it, or an
effect of the post-process chain that samples it) they are left out, and the
first render that has such a reader again starts the kept depth over from its
own. A third pass, whose output is @frame.camera (restates = true),
restates the camera on every frame, so a pass after the resolve that reads
the camera and no depth reads it with no sub-pixel offset as well. A pass
ahead of them reads the raster's own depth, which every render lays again
before its first phase, and keeps neither. On the deferred path
stable_surface keeps the G-buffer normal and material of the same sample,
and two more passes restate
@scene.normal and @scene.material from them, so the passes after the resolve
that read the G-buffer (the rest of the afterLighting phase) see the surface
the restated depth describes. Those three passes name the two channels in
whenRead: on a frame where no pass reads what they leave in @scene.normal
or @scene.material (a pass after them, or one ahead of the shading reading
the G-buffer the previous frame left, such as screen-space reflections) they
are left out, and the first render that has such a
reader again starts the kept surface over from its own. Lines and patterns an
effect draws there hold still on a still view and move with the surfaces on a
moving one. A pass ahead
of the resolve (occlusion, reflections, fog, depth of field) reads the
raster's own depth and surface, which describe the same scene, and
__renderpass.schedule() reports no such read.
Every view keeps its own result
A frame draws several views: the viewport, every render-to-texture camera, every
capture. Each runs this pass against its own picture, at its own size, so each
keeps its own running result. The result lives in a history target
(renderer.texture.create { history = true }), which holds one pair of images
per view: for the length of a view's render the pass resolves into the target's
guid and reads previous, the image that view's previous render resolved. A
minimap or a capture drawn beside the viewport neither reads the viewport's
result nor disturbs it.
Each view also walks the sample sequence on its own count of renders. Its first render, and its first after a cut, samples the pixel centre and starts the result from that render alone, which is all a one-shot capture ever draws.
A capture drawing the picture the viewport drew (the viewport's camera and lens, at the viewport's size, admitting the same of the scene, lit and shaded the same way) carries the viewport's result on instead. Its render reads the image the viewport's latest render resolved, samples where the viewport's next render samples, and resolves into the viewport's other image, the one the viewport's next render resolves over. So it shows the viewport's settled accumulation, and the viewport reads its own result on its next render as if the capture had not drawn.
The colour images are rgba16f, so an accumulated highlight brighter than white
keeps its value instead of clipping on the way through. The distance images are
r32f, the temporal images rgba16f (how many renders each pixel's history
holds and the spread of its estimates), and the stable depth images rgba32f
(the kept depth and how far from the pixel centre it was sampled).
Parameters
taa_params carries the tuning and rcas_params the sharpness, both written by
@builtin::systems.antiAliasing.temporalAntiAliasing. taa_state is the history
target's state, written by the engine before each render of a view: whether
previous holds that view's previous result, how many renders in a row have
carried it, and the render's sub-pixel offset. A render with no result to carry
begins from its own sample.