Skip to content

Instantly share code, notes, and snippets.

@Erkaman
Last active April 25, 2023 02:32
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Erkaman/f24ef6bd7499be363e6c99d116d8734d to your computer and use it in GitHub Desktop.
Save Erkaman/f24ef6bd7499be363e6c99d116d8734d to your computer and use it in GitHub Desktop.
rudimentary temporal anti-aliasing solution, that is good as a starting point for more advanced TAA techniques.
/*
The MIT License (MIT)
Copyright (c) 2018 Eric Arnebäck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// the shader is applied as a fullscreen pass(fragment shader), that implements temporal anti-aliasing
// fullscreen UV coordinates. (0,0) in top-left, (1,1) in right-bottom for my engine.
in vec2 fsUv;
out vec4 fragColor;
uniform int uFirstFrame;
uniform vec2 uPixelSize;
/*
In my engine, this is the HDR image that we get AFTER the deferred lighting has been done,
and BEFORE the tonemapping is applied.
hdrTex = HDR image of current frame
histHdrTex = HDR image of previous frame.
*/
uniform sampler2D hdrTex;
uniform sampler2D histHdrTex;
// velocity texture.
uniform sampler2D velocityTex;
void main() {
vec3 c;
if(uFirstFrame == 1) {
// first frame, no blending at all.
c.xyz = texture2D(hdrTex, fsUv.xy).xyz;
} else {
vec3 neighbourhood[9];
neighbourhood[0] = texture2D(hdrTex, fsUv.xy + vec2(-1, -1) * uPixelSize ).xyz;
neighbourhood[1] = texture2D(hdrTex, fsUv.xy + vec2(+0, -1) * uPixelSize ).xyz;
neighbourhood[2] = texture2D(hdrTex, fsUv.xy + vec2(+1, -1) * uPixelSize ).xyz;
neighbourhood[3] = texture2D(hdrTex, fsUv.xy + vec2(-1, +0) * uPixelSize ).xyz;
neighbourhood[4] = texture2D(hdrTex, fsUv.xy + vec2(+0, +0) * uPixelSize ).xyz;
neighbourhood[5] = texture2D(hdrTex, fsUv.xy + vec2(+1, +0) * uPixelSize ).xyz;
neighbourhood[6] = texture2D(hdrTex, fsUv.xy + vec2(-1, +1) * uPixelSize ).xyz;
neighbourhood[7] = texture2D(hdrTex, fsUv.xy + vec2(+0, +1) * uPixelSize ).xyz;
neighbourhood[8] = texture2D(hdrTex, fsUv.xy + vec2(+1, +1) * uPixelSize ).xyz;
vec3 nmin = neighbourhood[0];
vec3 nmax = neighbourhood[0];
for(int i = 1; i < 9; ++i) {
nmin = min(nmin, neighbourhood[i]);
nmax = max(nmax, neighbourhood[i]);
}
vec2 vel = texture2D(velocityTex, fsUv.xy).xy;
vec2 histUv = fsUv.xy + vel.xy;
// sample from history buffer, with neighbourhood clamping.
vec3 histSample = clamp(texture2D(histHdrTex, histUv).xyz, nmin, nmax);
// blend factor
float blend = 0.05;
bvec2 a = greaterThan(histUv, vec2(1.0, 1.0));
bvec2 b = lessThan(histUv, vec2(0.0, 0.0));
// if history sample is outside screen, switch to aliased image as a fallback.
blend = any(bvec2(any(a), any(b))) ? 1.0 : blend;
vec3 curSample = neighbourhood[4];
// finally, blend current and clamped history sample.
c = mix(histSample, curSample, vec3(blend) );
}
fragColor = vec4(c.xyz, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment