TAA - Neighbor clamping
Task
Enhance the provided Temporal Anti-Aliasing (TAA) implementation by adding neighbor clamping based on color. This technique helps suppress ghosting and color bleeding by constraining the reprojected color within the local neighborhood of the current frame.
Theory
In Temporal Anti-Aliasing (TAA), velocity reprojection helps align history with moving objects, reducing ghosting. But even with velocity reprojection, ghosting can persist due to:
- Disocclusion. New pixels revealed behind moving objects have no valid history, so they get blended with irrelevant data.
- Incorrect motion vectors. Transparent objects, particles, or post-processing may produce invalid motion data.
- History accumulation. Even correctly reprojected pixels can accumulate outdated color if not clamped or rejected.
- Low-resolution velocity buffers. Aliased or quantized motion vectors lead to misaligned reprojection.
Neighbor Clamping
Neighbor clamping helps prevent visual artifacts by limiting how much a pixel’s historical color can deviate from its current neighborhood. It’s a simple but powerful technique that helps eliminate ghosting and edge bleeding.
How It Works
1. Sample neighboring pixel colors:
vec4 c0 = texture(currentFrame, uv + vec2(+1.0, 0.0) * texelSize);
vec4 c1 = texture(currentFrame, uv + vec2( 0.0,+1.0) * texelSize);
vec4 c2 = texture(currentFrame, uv + vec2(-1.0, 0.0) * texelSize);
vec4 c3 = texture(currentFrame, uv + vec2( 0.0,-1.0) * texelSize);
2. Compute their min and max bounds:
vec4 minColor = min(currColor, min(c0, min(c1, min(c2, c3))));
vec4 maxColor = min(currColor, max(c0, max(c1, max(c2, c3))));
3. Clamp the historical color to stay within those bounds:
historyColor = clamp(historyColor, minColor, maxColor);
4. Blend the clamped history with the current frame.