瀏覽代碼

Remove SMAA and add TemporalAA instead

Panagiotis Christopoulos Charitos 8 年之前
父節點
當前提交
941171e692

+ 4 - 5
shaders/Blit.frag.glsl

@@ -5,13 +5,12 @@
 
 #include "shaders/Common.glsl"
 
-layout(ANKI_TEX_BINDING(0, 0)) uniform lowp sampler2D uTex;
+layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_tex;
 
-layout(location = 0) in vec2 inTexCoords;
-
-layout(location = 0) out vec3 outColor;
+layout(location = 0) in vec2 in_uv;
+layout(location = 0) out vec3 out_col;
 
 void main()
 {
-	outColor = texture(uTex, inTexCoords).rgb;
+	out_col = textureLod(u_tex, in_uv, 0.0).rgb;
 }

+ 1 - 0
shaders/ClusterLightCommon.glsl

@@ -18,6 +18,7 @@ struct LightingUniforms
 	uvec4 tileCount;
 	mat4 invViewProjMat;
 	mat4 prevViewProjMat;
+	mat4 invProjMat;
 };
 
 // Point light

+ 24 - 23
shaders/Is.frag.glsl

@@ -113,11 +113,25 @@ void main()
 	vec2 ndc = UV_TO_NDC(in_uv);
 
 	// Get frag pos in view space
-	vec3 fragPos;
-	fragPos.z = u_lightingUniforms.projectionParams.z / (u_lightingUniforms.projectionParams.w + depth);
-	fragPos.xy = ndc * u_lightingUniforms.projectionParams.xy * fragPos.z;
+	vec4 fragPos4 = u_lightingUniforms.invProjMat * vec4(ndc, UV_TO_NDC(depth), 1.0);
+	vec3 fragPos = fragPos4.xyz / fragPos4.w;
 	vec3 viewDir = normalize(-fragPos);
 
+	// Get world position
+	vec3 worldPos;
+	vec2 oldUv;
+	{
+		vec4 worldPos4 = u_lightingUniforms.invViewProjMat * vec4(ndc, UV_TO_NDC(depth), 1.0);
+		worldPos4 = worldPos4 / worldPos4.w;
+		worldPos = worldPos4.xyz;
+
+		// Project to get old ndc
+		vec4 oldNdc4 = u_lightingUniforms.prevViewProjMat * vec4(worldPos, 1.0);
+		vec2 oldNdc = oldNdc4.xy / oldNdc4.w;
+
+		oldUv = NDC_TO_UV(oldNdc);
+	}
+
 	// Decode GBuffer
 	vec3 normal;
 	vec3 diffCol;
@@ -138,22 +152,8 @@ void main()
 	emission = gbuffer.emission;
 
 	// Get SSAO
-	vec3 worldPos;
-
-	{
-		vec4 worldPos4 = u_lightingUniforms.invViewProjMat * vec4(ndc, UV_TO_NDC(depth), 1.0);
-		worldPos4 = worldPos4 / worldPos4.w;
-		worldPos = worldPos4.xyz;
-
-		// Project to get old ndc
-		vec4 oldNdc4 = u_lightingUniforms.prevViewProjMat * worldPos4;
-		vec2 oldNdc = oldNdc4.xy / oldNdc4.w;
-
-		vec2 oldUv = NDC_TO_UV(oldNdc);
-
-		float ssao = texture(u_ssaoTex, oldUv).r;
-		diffCol *= ssao;
-	}
+	float ssao = texture(u_ssaoTex, oldUv).r;
+	diffCol *= ssao;
 
 	// Get counts and offsets
 	uint clusterIdx =
@@ -179,7 +179,7 @@ void main()
 	float a2 = pow(roughness, 2.0);
 
 	// Ambient and emissive color
-	out_color = diffCol * emission;
+	vec3 outC = diffCol * emission;
 
 	// Point lights
 	count = u_lightIndices[idxOffset++];
@@ -200,7 +200,7 @@ void main()
 			lambert *= shadow;
 		}
 
-		out_color += (specC + diffC) * (att * max(subsurface, lambert));
+		outC += (specC + diffC) * (att * max(subsurface, lambert));
 	}
 
 	// Spot lights
@@ -221,7 +221,7 @@ void main()
 			lambert *= shadow;
 		}
 
-		out_color += (diffC + specC) * (att * spot * max(subsurface, lambert));
+		outC += (diffC + specC) * (att * spot * max(subsurface, lambert));
 	}
 
 #if INDIRECT_ENABLED
@@ -240,9 +240,10 @@ void main()
 	vec3 specIndirect, diffIndirect;
 	readIndirect(idxOffset, worldPos, worldR, worldNormal, reflLod, specIndirect, diffIndirect);
 
-	out_color += specIndirect * specIndirectTerm + diffIndirect * diffCol;
+	outC += specIndirect * specIndirectTerm + diffIndirect * diffCol;
 #endif
 
+	out_color = outC;
 #if 0
 	count = scount;
 	if(count == 0)

+ 2 - 23
shaders/Pps.frag.glsl

@@ -7,22 +7,12 @@
 #include "shaders/Tonemapping.glsl"
 #include "shaders/Functions.glsl"
 
-#if SMAA_ENABLED
-#define SMAA_GLSL_4
-#define SMAA_INCLUDE_PS 1
-#define SMAA_INCLUDE_VS 0
-#include "shaders/SMAA.hlsl"
-#endif
-
 #define BLUE_NOISE 1
 
 layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_isRt;
 layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_ppsBloomLfRt;
 layout(ANKI_TEX_BINDING(0, 2)) uniform sampler3D u_lut;
 layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2DArray u_blueNoise;
-#if SMAA_ENABLED
-layout(ANKI_TEX_BINDING(0, 4)) uniform sampler2D u_smaaBlendTex;
-#endif
 #if DBG_ENABLED
 layout(ANKI_TEX_BINDING(0, 5)) uniform sampler2D u_dbgRt;
 #endif
@@ -37,16 +27,7 @@ layout(std140, ANKI_SS_BINDING(0, 0)) readonly buffer s0_
 	vec4 u_averageLuminancePad3;
 };
 
-#if NVIDIA_LINK_ERROR_WORKAROUND
-layout(location = 0) in vec4 in_uv;
-#else
 layout(location = 0) in vec2 in_uv;
-#endif
-
-#if SMAA_ENABLED
-layout(location = 1) in vec4 in_smaaOffset;
-#endif
-
 layout(location = 0) out vec3 out_color;
 
 const vec2 TEX_OFFSET = vec2(1.0 / float(FBO_WIDTH), 1.0 / float(FBO_HEIGHT));
@@ -86,7 +67,7 @@ vec3 gammaCorrectionRgb(in vec3 gamma, in vec3 col)
 
 vec3 sharpen(in sampler2D tex, in vec2 texCoords)
 {
-	const float sharpenFactor = 0.25;
+	const float sharpenFactor = 0.15;
 
 	vec3 col = textureLod(tex, texCoords, 0.0).rgb;
 
@@ -134,8 +115,6 @@ void main()
 
 #if SHARPEN_ENABLED
 	out_color = sharpen(u_isRt, uv);
-#elif SMAA_ENABLED
-	out_color = SMAANeighborhoodBlendingPS(uv, in_smaaOffset, u_isRt, u_smaaBlendTex).rgb;
 #else
 	out_color = textureLod(u_isRt, uv, 0.0).rgb;
 #endif
@@ -159,7 +138,7 @@ void main()
 
 #if 0
 	{
-		out_color = vec3(textureLod(u_ppsBloomLfRt, uv, 0.0).rgb);
+		out_color = vec3(textureLod(u_isRt, uv, 0.0).rgb);
 	}
 #endif
 

+ 0 - 37
shaders/Pps.vert.glsl

@@ -1,37 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-
-#if SMAA_ENABLED
-#define SMAA_GLSL_4
-#define SMAA_INCLUDE_PS 0
-#define SMAA_INCLUDE_VS 1
-#include "shaders/SMAA.hlsl"
-#endif
-
-#if NVIDIA_LINK_ERROR_WORKAROUND
-layout(location = 0) out vec4 out_uv;
-#else
-layout(location = 0) out vec2 out_uv;
-#endif
-
-#if SMAA_ENABLED
-layout(location = 1) out vec4 out_smaaOffset;
-#endif
-
-void main(void)
-{
-	const vec2 POSITIONS[3] = vec2[](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
-	vec2 pos = POSITIONS[gl_VertexID];
-	out_uv.xy = pos * 0.5 + 0.5;
-
-	ANKI_WRITE_POSITION(vec4(pos, 0.0, 1.0));
-
-#if SMAA_ENABLED
-	out_smaaOffset = vec4(0.0);
-	SMAANeighborhoodBlendingVS(out_uv.xy, out_smaaOffset);
-#endif
-}

+ 0 - 1372
shaders/SMAA.hlsl

@@ -1,1372 +0,0 @@
-/**
- * Copyright (C) 2013 Jorge Jimenez ([email protected])
- * Copyright (C) 2013 Jose I. Echevarria ([email protected])
- * Copyright (C) 2013 Belen Masia ([email protected])
- * Copyright (C) 2013 Fernando Navarro ([email protected])
- * Copyright (C) 2013 Diego Gutierrez ([email protected])
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * 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. As clarification, there
- * is no requirement that the copyright notice and permission be included in
- * binary distributions 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.
- */
-
-
-/**
- *                  _______  ___  ___       ___           ___
- *                 /       ||   \/   |     /   \         /   \
- *                |   (---- |  \  /  |    /  ^  \       /  ^  \
- *                 \   \    |  |\/|  |   /  /_\  \     /  /_\  \
- *              ----)   |   |  |  |  |  /  _____  \   /  _____  \
- *             |_______/    |__|  |__| /__/     \__\ /__/     \__\
- * 
- *                               E N H A N C E D
- *       S U B P I X E L   M O R P H O L O G I C A L   A N T I A L I A S I N G
- *
- *                         http://www.iryoku.com/smaa/
- *
- * Hi, welcome aboard!
- * 
- * Here you'll find instructions to get the shader up and running as fast as
- * possible.
- *
- * IMPORTANTE NOTICE: when updating, remember to update both this file and the
- * precomputed textures! They may change from version to version.
- *
- * The shader has three passes, chained together as follows:
- *
- *                           |input|------------------.
- *                              v                     |
- *                    [ SMAA*EdgeDetection ]          |
- *                              v                     |
- *                          |edgesTex|                |
- *                              v                     |
- *              [ SMAABlendingWeightCalculation ]     |
- *                              v                     |
- *                          |blendTex|                |
- *                              v                     |
- *                [ SMAANeighborhoodBlending ] <------.
- *                              v
- *                           |output|
- *
- * Note that each [pass] has its own vertex and pixel shader. Remember to use
- * oversized triangles instead of quads to avoid overshading along the
- * diagonal.
- *
- * You've three edge detection methods to choose from: luma, color or depth.
- * They represent different quality/performance and anti-aliasing/sharpness
- * tradeoffs, so our recommendation is for you to choose the one that best
- * suits your particular scenario:
- *
- * - Depth edge detection is usually the fastest but it may miss some edges.
- *
- * - Luma edge detection is usually more expensive than depth edge detection,
- *   but catches visible edges that depth edge detection can miss.
- *
- * - Color edge detection is usually the most expensive one but catches
- *   chroma-only edges.
- *
- * For quickstarters: just use luma edge detection.
- *
- * The general advice is to not rush the integration process and ensure each
- * step is done correctly (don't try to integrate SMAA T2x with predicated edge
- * detection from the start!). Ok then, let's go!
- *
- *  1. The first step is to create two RGBA temporal render targets for holding
- *     |edgesTex| and |blendTex|.
- *
- *     In DX10 or DX11, you can use a RG render target for the edges texture.
- *     In the case of NVIDIA GPUs, using RG render targets seems to actually be
- *     slower.
- *
- *     On the Xbox 360, you can use the same render target for resolving both
- *     |edgesTex| and |blendTex|, as they aren't needed simultaneously.
- *
- *  2. Both temporal render targets |edgesTex| and |blendTex| must be cleared
- *     each frame. Do not forget to clear the alpha channel!
- *
- *  3. The next step is loading the two supporting precalculated textures,
- *     'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as
- *     C++ headers, and also as regular DDS files. They'll be needed for the
- *     'SMAABlendingWeightCalculation' pass.
- *
- *     If you use the C++ headers, be sure to load them in the format specified
- *     inside of them.
- *
- *     You can also compress 'areaTex' and 'searchTex' using BC5 and BC4
- *     respectively, if you have that option in your content processor pipeline.
- *     When compressing then, you get a non-perceptible quality decrease, and a
- *     marginal performance increase.
- *
- *  4. All samplers must be set to linear filtering and clamp.
- *
- *     After you get the technique working, remember that 64-bit inputs have
- *     half-rate linear filtering on GCN.
- *
- *     If SMAA is applied to 64-bit color buffers, switching to point filtering
- *     when accesing them will increase the performance. Search for
- *     'SMAASamplePoint' to see which textures may benefit from point
- *     filtering, and where (which is basically the color input in the edge
- *     detection and resolve passes).
- *
- *  5. All texture reads and buffer writes must be non-sRGB, with the exception
- *     of the input read and the output write in
- *     'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in
- *     this last pass are not possible, the technique will work anyway, but
- *     will perform antialiasing in gamma space.
- *
- *     IMPORTANT: for best results the input read for the color/luma edge 
- *     detection should *NOT* be sRGB.
- *
- *  6. Before including SMAA.h you'll have to setup the render target metrics,
- *     the target and any optional configuration defines. Optionally you can
- *     use a preset.
- *
- *     You have the following targets available: 
- *         SMAA_HLSL_3
- *         SMAA_HLSL_4
- *         SMAA_HLSL_4_1
- *         SMAA_GLSL_3 *
- *         SMAA_GLSL_4 *
- *
- *         * (See SMAA_INCLUDE_VS and SMAA_INCLUDE_PS below).
- *
- *     And four presets:
- *         SMAA_PRESET_LOW          (%60 of the quality)
- *         SMAA_PRESET_MEDIUM       (%80 of the quality)
- *         SMAA_PRESET_HIGH         (%95 of the quality)
- *         SMAA_PRESET_ULTRA        (%99 of the quality)
- *
- *     For example:
- *         #define SMAA_RT_METRICS float4(1.0 / 1280.0, 1.0 / 720.0, 1280.0, 720.0)
- *         #define SMAA_HLSL_4
- *         #define SMAA_PRESET_HIGH
- *         #include "SMAA.h"
- *
- *     Note that SMAA_RT_METRICS doesn't need to be a macro, it can be a
- *     uniform variable. The code is designed to minimize the impact of not
- *     using a constant value, but it is still better to hardcode it.
- *
- *     Depending on how you encoded 'areaTex' and 'searchTex', you may have to
- *     add (and customize) the following defines before including SMAA.h:
- *          #define SMAA_AREATEX_SELECT(sample) sample.rg
- *          #define SMAA_SEARCHTEX_SELECT(sample) sample.r
- *
- *     If your engine is already using porting macros, you can define
- *     SMAA_CUSTOM_SL, and define the porting functions by yourself.
- *
- *  7. Then, you'll have to setup the passes as indicated in the scheme above.
- *     You can take a look into SMAA.fx, to see how we did it for our demo.
- *     Checkout the function wrappers, you may want to copy-paste them!
- *
- *  8. It's recommended to validate the produced |edgesTex| and |blendTex|.
- *     You can use a screenshot from your engine to compare the |edgesTex|
- *     and |blendTex| produced inside of the engine with the results obtained
- *     with the reference demo.
- *
- *  9. After you get the last pass to work, it's time to optimize. You'll have
- *     to initialize a stencil buffer in the first pass (discard is already in
- *     the code), then mask execution by using it the second pass. The last
- *     pass should be executed in all pixels.
- *
- *
- * After this point you can choose to enable predicated thresholding,
- * temporal supersampling and motion blur integration:
- *
- * a) If you want to use predicated thresholding, take a look into
- *    SMAA_PREDICATION; you'll need to pass an extra texture in the edge
- *    detection pass.
- *
- * b) If you want to enable temporal supersampling (SMAA T2x):
- *
- * 1. The first step is to render using subpixel jitters. I won't go into
- *    detail, but it's as simple as moving each vertex position in the
- *    vertex shader, you can check how we do it in our DX10 demo.
- *
- * 2. Then, you must setup the temporal resolve. You may want to take a look
- *    into SMAAResolve for resolving 2x modes. After you get it working, you'll
- *    probably see ghosting everywhere. But fear not, you can enable the
- *    CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro.
- *    Check out SMAA_DECODE_VELOCITY if your velocity buffer is encoded.
- *
- * 3. The next step is to apply SMAA to each subpixel jittered frame, just as
- *    done for 1x.
- *
- * 4. At this point you should already have something usable, but for best
- *    results the proper area textures must be set depending on current jitter.
- *    For this, the parameter 'subsampleIndices' of
- *    'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x
- *    mode:
- *
- *    @SUBSAMPLE_INDICES
- *
- *    | S# |  Camera Jitter   |  subsampleIndices    |
- *    +----+------------------+---------------------+
- *    |  0 |  ( 0.25, -0.25)  |  float4(1, 1, 1, 0)  |
- *    |  1 |  (-0.25,  0.25)  |  float4(2, 2, 2, 0)  |
- *
- *    These jitter positions assume a bottom-to-top y axis. S# stands for the
- *    sample number.
- *
- * More information about temporal supersampling here:
- *    http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
- *
- * c) If you want to enable spatial multisampling (SMAA S2x):
- *
- * 1. The scene must be rendered using MSAA 2x. The MSAA 2x buffer must be
- *    created with:
- *      - DX10:     see below (*)
- *      - DX10.1:   D3D10_STANDARD_MULTISAMPLE_PATTERN or
- *      - DX11:     D3D11_STANDARD_MULTISAMPLE_PATTERN
- *
- *    This allows to ensure that the subsample order matches the table in
- *    @SUBSAMPLE_INDICES.
- *
- *    (*) In the case of DX10, we refer the reader to:
- *      - SMAA::detectMSAAOrder and
- *      - SMAA::msaaReorder
- *
- *    These functions allow to match the standard multisample patterns by
- *    detecting the subsample order for a specific GPU, and reordering
- *    them appropriately.
- *
- * 2. A shader must be run to output each subsample into a separate buffer
- *    (DX10 is required). You can use SMAASeparate for this purpose, or just do
- *    it in an existing pass (for example, in the tone mapping pass, which has
- *    the advantage of feeding tone mapped subsamples to SMAA, which will yield
- *    better results).
- *
- * 3. The full SMAA 1x pipeline must be run for each separated buffer, storing
- *    the results in the final buffer. The second run should alpha blend with
- *    the existing final buffer using a blending factor of 0.5.
- *    'subsampleIndices' must be adjusted as in the SMAA T2x case (see point
- *    b).
- *
- * d) If you want to enable temporal supersampling on top of SMAA S2x
- *    (which actually is SMAA 4x):
- *
- * 1. SMAA 4x consists on temporally jittering SMAA S2x, so the first step is
- *    to calculate SMAA S2x for current frame. In this case, 'subsampleIndices'
- *    must be set as follows:
- *
- *    | F# | S# |   Camera Jitter    |    Net Jitter     |   subsampleIndices   |
- *    +----+----+--------------------+-------------------+----------------------+
- *    |  0 |  0 |  ( 0.125,  0.125)  |  ( 0.375, -0.125) |  float4(5, 3, 1, 3)  |
- *    |  0 |  1 |  ( 0.125,  0.125)  |  (-0.125,  0.375) |  float4(4, 6, 2, 3)  |
- *    +----+----+--------------------+-------------------+----------------------+
- *    |  1 |  2 |  (-0.125, -0.125)  |  ( 0.125, -0.375) |  float4(3, 5, 1, 4)  |
- *    |  1 |  3 |  (-0.125, -0.125)  |  (-0.375,  0.125) |  float4(6, 4, 2, 4)  |
- *
- *    These jitter positions assume a bottom-to-top y axis. F# stands for the
- *    frame number. S# stands for the sample number.
- *
- * 2. After calculating SMAA S2x for current frame (with the new subsample
- *    indices), previous frame must be reprojected as in SMAA T2x mode (see
- *    point b).
- *
- * e) If motion blur is used, you may want to do the edge detection pass
- *    together with motion blur. This has two advantages:
- *
- * 1. Pixels under heavy motion can be omitted from the edge detection process.
- *    For these pixels we can just store "no edge", as motion blur will take
- *    care of them.
- * 2. The center pixel tap is reused.
- *
- * Note that in this case depth testing should be used instead of stenciling,
- * as we have to write all the pixels in the motion blur pass.
- *
- * That's it!
- */
-
-//-----------------------------------------------------------------------------
-// SMAA Presets
-
-/**
- * Note that if you use one of these presets, the following configuration
- * macros will be ignored if set in the "Configurable Defines" section.
- */
-
-#if defined(SMAA_PRESET_LOW)
-#define SMAA_THRESHOLD 0.15
-#define SMAA_MAX_SEARCH_STEPS 4
-#define SMAA_DISABLE_DIAG_DETECTION
-#define SMAA_DISABLE_CORNER_DETECTION
-#elif defined(SMAA_PRESET_MEDIUM)
-#define SMAA_THRESHOLD 0.1
-#define SMAA_MAX_SEARCH_STEPS 8
-#define SMAA_DISABLE_DIAG_DETECTION
-#define SMAA_DISABLE_CORNER_DETECTION
-#elif defined(SMAA_PRESET_HIGH)
-#define SMAA_THRESHOLD 0.1
-#define SMAA_MAX_SEARCH_STEPS 16
-#define SMAA_MAX_SEARCH_STEPS_DIAG 8
-#define SMAA_CORNER_ROUNDING 25
-#elif defined(SMAA_PRESET_ULTRA)
-#define SMAA_THRESHOLD 0.05
-#define SMAA_MAX_SEARCH_STEPS 32
-#define SMAA_MAX_SEARCH_STEPS_DIAG 16
-#define SMAA_CORNER_ROUNDING 25
-#endif
-
-//-----------------------------------------------------------------------------
-// Configurable Defines
-
-/**
- * SMAA_THRESHOLD specifies the threshold or sensitivity to edges.
- * Lowering this value you will be able to detect more edges at the expense of
- * performance. 
- *
- * Range: [0, 0.5]
- *   0.1 is a reasonable value, and allows to catch most visible edges.
- *   0.05 is a rather overkill value, that allows to catch 'em all.
- *
- *   If temporal supersampling is used, 0.2 could be a reasonable value, as low
- *   contrast edges are properly filtered by just 2x.
- */
-#ifndef SMAA_THRESHOLD
-#define SMAA_THRESHOLD 0.1
-#endif
-
-/**
- * SMAA_DEPTH_THRESHOLD specifies the threshold for depth edge detection.
- * 
- * Range: depends on the depth range of the scene.
- */
-#ifndef SMAA_DEPTH_THRESHOLD
-#define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD)
-#endif
-
-/**
- * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the
- * horizontal/vertical pattern searches, at each side of the pixel.
- *
- * In number of pixels, it's actually the double. So the maximum line length
- * perfectly handled by, for example 16, is 64 (by perfectly, we meant that
- * longer lines won't look as good, but still antialiased).
- *
- * Range: [0, 112]
- */
-#ifndef SMAA_MAX_SEARCH_STEPS
-#define SMAA_MAX_SEARCH_STEPS 16
-#endif
-
-/**
- * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the
- * diagonal pattern searches, at each side of the pixel. In this case we jump
- * one pixel at time, instead of two.
- *
- * Range: [0, 20]
- *
- * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16 
- * steps), but it can have a significant impact on older machines.
- *
- * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing.
- */
-#ifndef SMAA_MAX_SEARCH_STEPS_DIAG
-#define SMAA_MAX_SEARCH_STEPS_DIAG 8
-#endif
-
-/**
- * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded.
- *
- * Range: [0, 100]
- *
- * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing.
- */
-#ifndef SMAA_CORNER_ROUNDING
-#define SMAA_CORNER_ROUNDING 25
-#endif
-
-/**
- * If there is an neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times
- * bigger contrast than current edge, current edge will be discarded.
- *
- * This allows to eliminate spurious crossing edges, and is based on the fact
- * that, if there is too much contrast in a direction, that will hide
- * perceptually contrast in the other neighbors.
- */
-#ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR
-#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0
-#endif
-
-/**
- * Predicated thresholding allows to better preserve texture details and to
- * improve performance, by decreasing the number of detected edges using an
- * additional buffer like the light accumulation buffer, object ids or even the
- * depth buffer (the depth buffer usage may be limited to indoor or short range
- * scenes).
- *
- * It locally decreases the luma or color threshold if an edge is found in an
- * additional buffer (so the global threshold can be higher).
- *
- * This method was developed by Playstation EDGE MLAA team, and used in 
- * Killzone 3, by using the light accumulation buffer. More information here:
- *     http://iryoku.com/aacourse/downloads/06-MLAA-on-PS3.pptx 
- */
-#ifndef SMAA_PREDICATION
-#define SMAA_PREDICATION 0
-#endif
-
-/**
- * Threshold to be used in the additional predication buffer. 
- *
- * Range: depends on the input, so you'll have to find the magic number that
- * works for you.
- */
-#ifndef SMAA_PREDICATION_THRESHOLD
-#define SMAA_PREDICATION_THRESHOLD 0.01
-#endif
-
-/**
- * How much to scale the global threshold used for luma or color edge
- * detection when using predication.
- *
- * Range: [1, 5]
- */
-#ifndef SMAA_PREDICATION_SCALE
-#define SMAA_PREDICATION_SCALE 2.0
-#endif
-
-/**
- * How much to locally decrease the threshold.
- *
- * Range: [0, 1]
- */
-#ifndef SMAA_PREDICATION_STRENGTH
-#define SMAA_PREDICATION_STRENGTH 0.4
-#endif
-
-/**
- * Temporal reprojection allows to remove ghosting artifacts when using
- * temporal supersampling. We use the CryEngine 3 method which also introduces
- * velocity weighting. This feature is of extreme importance for totally
- * removing ghosting. More information here:
- *    http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
- *
- * Note that you'll need to setup a velocity buffer for enabling reprojection.
- * For static geometry, saving the previous depth buffer is a viable
- * alternative.
- */
-#ifndef SMAA_REPROJECTION
-#define SMAA_REPROJECTION 0
-#endif
-
-/**
- * SMAA_REPROJECTION_WEIGHT_SCALE controls the velocity weighting. It allows to
- * remove ghosting trails behind the moving object, which are not removed by
- * just using reprojection. Using low values will exhibit ghosting, while using
- * high values will disable temporal supersampling under motion.
- *
- * Behind the scenes, velocity weighting removes temporal supersampling when
- * the velocity of the subsamples differs (meaning they are different objects).
- *
- * Range: [0, 80]
- */
-#ifndef SMAA_REPROJECTION_WEIGHT_SCALE
-#define SMAA_REPROJECTION_WEIGHT_SCALE 30.0
-#endif
-
-/**
- * On some compilers, discard cannot be used in vertex shaders. Thus, they need
- * to be compiled separately.
- */
-#ifndef SMAA_INCLUDE_VS
-#define SMAA_INCLUDE_VS 1
-#endif
-#ifndef SMAA_INCLUDE_PS
-#define SMAA_INCLUDE_PS 1
-#endif
-
-//-----------------------------------------------------------------------------
-// Texture Access Defines
-
-#ifndef SMAA_AREATEX_SELECT
-#if defined(SMAA_HLSL_3)
-#define SMAA_AREATEX_SELECT(sample) sample.ra
-#else
-#define SMAA_AREATEX_SELECT(sample) sample.rg
-#endif
-#endif
-
-#ifndef SMAA_SEARCHTEX_SELECT
-#define SMAA_SEARCHTEX_SELECT(sample) sample.r
-#endif
-
-#ifndef SMAA_DECODE_VELOCITY
-#define SMAA_DECODE_VELOCITY(sample) sample.rg
-#endif
-
-//-----------------------------------------------------------------------------
-// Non-Configurable Defines
-
-#define SMAA_AREATEX_MAX_DISTANCE 16
-#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
-#define SMAA_AREATEX_PIXEL_SIZE (1.0 / float2(160.0, 560.0))
-#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)
-#define SMAA_SEARCHTEX_SIZE float2(66.0, 33.0)
-#define SMAA_SEARCHTEX_PACKED_SIZE float2(64.0, 16.0)
-#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0)
-
-//-----------------------------------------------------------------------------
-// Porting Functions
-
-#if defined(SMAA_HLSL_3)
-#define SMAATexture2D(tex) sampler2D tex
-#define SMAATexturePass2D(tex) tex
-#define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
-#define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
-#define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0))
-#define SMAASample(tex, coord) tex2D(tex, coord)
-#define SMAASamplePoint(tex, coord) tex2D(tex, coord)
-#define SMAASampleOffset(tex, coord, offset) tex2D(tex, coord + offset * SMAA_RT_METRICS.xy)
-#define SMAA_FLATTEN [flatten]
-#define SMAA_BRANCH [branch]
-#endif
-#if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1)
-SamplerState LinearSampler { Filter = MIN_MAG_LINEAR_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };
-SamplerState PointSampler { Filter = MIN_MAG_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };
-#define SMAATexture2D(tex) Texture2D tex
-#define SMAATexturePass2D(tex) tex
-#define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, 0)
-#define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, 0)
-#define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset)
-#define SMAASample(tex, coord) tex.Sample(LinearSampler, coord)
-#define SMAASamplePoint(tex, coord) tex.Sample(PointSampler, coord)
-#define SMAASampleOffset(tex, coord, offset) tex.Sample(LinearSampler, coord, offset)
-#define SMAA_FLATTEN [flatten]
-#define SMAA_BRANCH [branch]
-#define SMAATexture2DMS2(tex) Texture2DMS<float4, 2> tex
-#define SMAALoad(tex, pos, sample) tex.Load(pos, sample)
-#if defined(SMAA_HLSL_4_1)
-#define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0)
-#endif
-#endif
-#if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4)
-#define SMAATexture2D(tex) sampler2D tex
-#define SMAATexturePass2D(tex) tex
-#define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0)
-#define SMAASampleLevelZeroPoint(tex, coord) textureLod(tex, coord, 0.0)
-#define SMAASampleLevelZeroOffset(tex, coord, offset) textureLodOffset(tex, coord, 0.0, offset)
-#define SMAASample(tex, coord) texture(tex, coord)
-#define SMAASamplePoint(tex, coord) texture(tex, coord)
-#define SMAASampleOffset(tex, coord, offset) texture(tex, coord, offset)
-#define SMAA_FLATTEN
-#define SMAA_BRANCH
-#define lerp(a, b, t) mix(a, b, t)
-#define saturate(a) clamp(a, 0.0, 1.0)
-#if defined(SMAA_GLSL_4)
-#define mad(a, b, c) fma(a, b, c)
-#define SMAAGather(tex, coord) textureGather(tex, coord)
-#else
-#define mad(a, b, c) (a * b + c)
-#endif
-#define float2 vec2
-#define float3 vec3
-#define float4 vec4
-#define int2 ivec2
-#define int3 ivec3
-#define int4 ivec4
-#define bool2 bvec2
-#define bool3 bvec3
-#define bool4 bvec4
-#endif
-
-#if !defined(SMAA_HLSL_3) && !defined(SMAA_HLSL_4) && !defined(SMAA_HLSL_4_1) && !defined(SMAA_GLSL_3) && !defined(SMAA_GLSL_4) && !defined(SMAA_CUSTOM_SL)
-#error you must define the shading language: SMAA_HLSL_*, SMAA_GLSL_* or SMAA_CUSTOM_SL
-#endif
-
-//-----------------------------------------------------------------------------
-// Misc functions
-
-/**
- * Gathers current pixel, and the top-left neighbors.
- */
-float3 SMAAGatherNeighbours(float2 texcoord,
-                            float4 offset[3],
-                            SMAATexture2D(tex)) {
-    #ifdef SMAAGather
-    return SMAAGather(tex, texcoord + SMAA_RT_METRICS.xy * float2(-0.5, -0.5)).grb;
-    #else
-    float P = SMAASamplePoint(tex, texcoord).r;
-    float Pleft = SMAASamplePoint(tex, offset[0].xy).r;
-    float Ptop  = SMAASamplePoint(tex, offset[0].zw).r;
-    return float3(P, Pleft, Ptop);
-    #endif
-}
-
-/**
- * Adjusts the threshold by means of predication.
- */
-float2 SMAACalculatePredicatedThreshold(float2 texcoord,
-                                        float4 offset[3],
-                                        SMAATexture2D(predicationTex)) {
-    float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(predicationTex));
-    float2 delta = abs(neighbours.xx - neighbours.yz);
-    float2 edges = step(SMAA_PREDICATION_THRESHOLD, delta);
-    return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges);
-}
-
-/**
- * Conditional move:
- */
-void SMAAMovc(bool2 cond, inout float2 variable, float2 value) {
-    SMAA_FLATTEN if (cond.x) variable.x = value.x;
-    SMAA_FLATTEN if (cond.y) variable.y = value.y;
-}
-
-void SMAAMovc(bool4 cond, inout float4 variable, float4 value) {
-    SMAAMovc(cond.xy, variable.xy, value.xy);
-    SMAAMovc(cond.zw, variable.zw, value.zw);
-}
-
-
-#if SMAA_INCLUDE_VS
-//-----------------------------------------------------------------------------
-// Vertex Shaders
-
-/**
- * Edge Detection Vertex Shader
- */
-void SMAAEdgeDetectionVS(float2 texcoord,
-                         out float4 offset[3]) {
-    offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), texcoord.xyxy);
-    offset[1] = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0,  1.0), texcoord.xyxy);
-    offset[2] = mad(SMAA_RT_METRICS.xyxy, float4(-2.0, 0.0, 0.0, -2.0), texcoord.xyxy);
-}
-
-/**
- * Blend Weight Calculation Vertex Shader
- */
-void SMAABlendingWeightCalculationVS(float2 texcoord,
-                                     out float2 pixcoord,
-                                     out float4 offset[3]) {
-    pixcoord = texcoord * SMAA_RT_METRICS.zw;
-
-    // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
-    offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-0.25, -0.125,  1.25, -0.125), texcoord.xyxy);
-    offset[1] = mad(SMAA_RT_METRICS.xyxy, float4(-0.125, -0.25, -0.125,  1.25), texcoord.xyxy);
-
-    // And these for the searches, they indicate the ends of the loops:
-    offset[2] = mad(SMAA_RT_METRICS.xxyy,
-                    float4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS),
-                    float4(offset[0].xz, offset[1].yw));
-}
-
-/**
- * Neighborhood Blending Vertex Shader
- */
-void SMAANeighborhoodBlendingVS(float2 texcoord,
-                                out float4 offset) {
-    offset = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0,  1.0), texcoord.xyxy);
-}
-#endif // SMAA_INCLUDE_VS
-
-#if SMAA_INCLUDE_PS
-//-----------------------------------------------------------------------------
-// Edge Detection Pixel Shaders (First Pass)
-
-/**
- * Luma Edge Detection
- *
- * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
- * thus 'colorTex' should be a non-sRGB texture.
- */
-float2 SMAALumaEdgeDetectionPS(float2 texcoord,
-                               float4 offset[3],
-                               SMAATexture2D(colorTex)
-                               #if SMAA_PREDICATION
-                               , SMAATexture2D(predicationTex)
-                               #endif
-                               ) {
-    // Calculate the threshold:
-    #if SMAA_PREDICATION
-    float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, SMAATexturePass2D(predicationTex));
-    #else
-    float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD);
-    #endif
-
-    // Calculate lumas:
-    float3 weights = float3(0.2126, 0.7152, 0.0722);
-    float L = dot(SMAASamplePoint(colorTex, texcoord).rgb, weights);
-
-    float Lleft = dot(SMAASamplePoint(colorTex, offset[0].xy).rgb, weights);
-    float Ltop  = dot(SMAASamplePoint(colorTex, offset[0].zw).rgb, weights);
-
-    // We do the usual threshold:
-    float4 delta;
-    delta.xy = abs(L - float2(Lleft, Ltop));
-    float2 edges = step(threshold, delta.xy);
-
-    // Then discard if there is no edge:
-    if (dot(edges, float2(1.0, 1.0)) == 0.0)
-        discard;
-
-    // Calculate right and bottom deltas:
-    float Lright = dot(SMAASamplePoint(colorTex, offset[1].xy).rgb, weights);
-    float Lbottom  = dot(SMAASamplePoint(colorTex, offset[1].zw).rgb, weights);
-    delta.zw = abs(L - float2(Lright, Lbottom));
-
-    // Calculate the maximum delta in the direct neighborhood:
-    float2 maxDelta = max(delta.xy, delta.zw);
-
-    // Calculate left-left and top-top deltas:
-    float Lleftleft = dot(SMAASamplePoint(colorTex, offset[2].xy).rgb, weights);
-    float Ltoptop = dot(SMAASamplePoint(colorTex, offset[2].zw).rgb, weights);
-    delta.zw = abs(float2(Lleft, Ltop) - float2(Lleftleft, Ltoptop));
-
-    // Calculate the final maximum delta:
-    maxDelta = max(maxDelta.xy, delta.zw);
-    float finalDelta = max(maxDelta.x, maxDelta.y);
-
-    // Local contrast adaptation:
-    edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
-
-    return edges;
-}
-
-/**
- * Color Edge Detection
- *
- * IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and
- * thus 'colorTex' should be a non-sRGB texture.
- */
-float2 SMAAColorEdgeDetectionPS(float2 texcoord,
-                                float4 offset[3],
-                                SMAATexture2D(colorTex)
-                                #if SMAA_PREDICATION
-                                , SMAATexture2D(predicationTex)
-                                #endif
-                                ) {
-    // Calculate the threshold:
-    #if SMAA_PREDICATION
-    float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, predicationTex);
-    #else
-    float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD);
-    #endif
-
-    // Calculate color deltas:
-    float4 delta;
-    float3 C = SMAASamplePoint(colorTex, texcoord).rgb;
-
-    float3 Cleft = SMAASamplePoint(colorTex, offset[0].xy).rgb;
-    float3 t = abs(C - Cleft);
-    delta.x = max(max(t.r, t.g), t.b);
-
-    float3 Ctop  = SMAASamplePoint(colorTex, offset[0].zw).rgb;
-    t = abs(C - Ctop);
-    delta.y = max(max(t.r, t.g), t.b);
-
-    // We do the usual threshold:
-    float2 edges = step(threshold, delta.xy);
-
-    // Then discard if there is no edge:
-    if (dot(edges, float2(1.0, 1.0)) == 0.0)
-        discard;
-
-    // Calculate right and bottom deltas:
-    float3 Cright = SMAASamplePoint(colorTex, offset[1].xy).rgb;
-    t = abs(C - Cright);
-    delta.z = max(max(t.r, t.g), t.b);
-
-    float3 Cbottom  = SMAASamplePoint(colorTex, offset[1].zw).rgb;
-    t = abs(C - Cbottom);
-    delta.w = max(max(t.r, t.g), t.b);
-
-    // Calculate the maximum delta in the direct neighborhood:
-    float2 maxDelta = max(delta.xy, delta.zw);
-
-    // Calculate left-left and top-top deltas:
-    float3 Cleftleft  = SMAASamplePoint(colorTex, offset[2].xy).rgb;
-    t = abs(C - Cleftleft);
-    delta.z = max(max(t.r, t.g), t.b);
-
-    float3 Ctoptop = SMAASamplePoint(colorTex, offset[2].zw).rgb;
-    t = abs(C - Ctoptop);
-    delta.w = max(max(t.r, t.g), t.b);
-
-    // Calculate the final maximum delta:
-    maxDelta = max(maxDelta.xy, delta.zw);
-    float finalDelta = max(maxDelta.x, maxDelta.y);
-
-    // Local contrast adaptation:
-    edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
-
-    return edges;
-}
-
-float linearizeDepth_(in float depth, in float zNear, in float zFar)
-{
-	return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
-}
-
-
-/**
- * Depth Edge Detection
- */
-float2 SMAADepthEdgeDetectionPS(float2 texcoord,
-                                float4 offset[3],
-                                SMAATexture2D(depthTex)) {
-    float3 neighbours = textureGather(SMAATexturePass2D(depthTex), texcoord).xyz;
-
-	neighbours.x = linearizeDepth_(neighbours.x, 0.1, 500.0);
-	neighbours.y = linearizeDepth_(neighbours.y, 0.1, 500.0);
-	neighbours.z = linearizeDepth_(neighbours.z, 0.1, 500.0);
-
-    float2 delta = abs(neighbours.xx - float2(neighbours.y, neighbours.z));
-    float2 edges = step(0.001, delta);
-
-    //if (dot(edges, float2(1.0, 1.0)) == 0.0)
-    //    discard;
-
-    return edges;
-}
-
-//-----------------------------------------------------------------------------
-// Diagonal Search Functions
-
-#if !defined(SMAA_DISABLE_DIAG_DETECTION)
-
-/**
- * Allows to decode two binary values from a bilinear-filtered access.
- */
-float2 SMAADecodeDiagBilinearAccess(float2 e) {
-    // Bilinear access for fetching 'e' have a 0.25 offset, and we are
-    // interested in the R and G edges:
-    //
-    // +---G---+-------+
-    // |   x o R   x   |
-    // +-------+-------+
-    //
-    // Then, if one of these edge is enabled:
-    //   Red:   (0.75 * X + 0.25 * 1) => 0.25 or 1.0
-    //   Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0
-    //
-    // This function will unpack the values (mad + mul + round):
-    // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1
-    e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75);
-    return round(e);
-}
-
-float4 SMAADecodeDiagBilinearAccess(float4 e) {
-    e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75);
-    return round(e);
-}
-
-/**
- * These functions allows to perform diagonal pattern searches.
- */
-float2 SMAASearchDiag1(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) {
-    float4 coord = float4(texcoord, -1.0, 1.0);
-    float3 t = float3(SMAA_RT_METRICS.xy, 1.0);
-    while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
-           coord.w > 0.9) {
-        coord.xyz = mad(t, float3(dir, 1.0), coord.xyz);
-        e = SMAASampleLevelZero(edgesTex, coord.xy).rg;
-        coord.w = dot(e, float2(0.5, 0.5));
-    }
-    return coord.zw;
-}
-
-float2 SMAASearchDiag2(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) {
-    float4 coord = float4(texcoord, -1.0, 1.0);
-    coord.x += 0.25 * SMAA_RT_METRICS.x; // See @SearchDiag2Optimization
-    float3 t = float3(SMAA_RT_METRICS.xy, 1.0);
-    while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
-           coord.w > 0.9) {
-        coord.xyz = mad(t, float3(dir, 1.0), coord.xyz);
-
-        // @SearchDiag2Optimization
-        // Fetch both edges at once using bilinear filtering:
-        e = SMAASampleLevelZero(edgesTex, coord.xy).rg;
-        e = SMAADecodeDiagBilinearAccess(e);
-
-        // Non-optimized version:
-        // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g;
-        // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r;
-
-        coord.w = dot(e, float2(0.5, 0.5));
-    }
-    return coord.zw;
-}
-
-/** 
- * Similar to SMAAArea, this calculates the area corresponding to a certain
- * diagonal distance and crossing edges 'e'.
- */
-float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset) {
-    float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist);
-
-    // We do a scale and bias for mapping to texel space:
-    texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
-
-    // Diagonal areas are on the second half of the texture:
-    texcoord.x += 0.5;
-
-    // Move to proper place, according to the subpixel offset:
-    texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
-
-    // Do it!
-    return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord));
-}
-
-/**
- * This searches for diagonal patterns and returns the corresponding weights.
- */
-float2 SMAACalculateDiagWeights(SMAATexture2D(edgesTex), SMAATexture2D(areaTex), float2 texcoord, float2 e, float4 subsampleIndices) {
-    float2 weights = float2(0.0, 0.0);
-
-    // Search for the line ends:
-    float4 d;
-    float2 end;
-    if (e.r > 0.0) {
-        d.xz = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0,  1.0), end);
-        d.x += float(end.y > 0.9);
-    } else
-        d.xz = float2(0.0, 0.0);
-    d.yw = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, -1.0), end);
-
-    SMAA_BRANCH
-    if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
-        // Fetch the crossing edges:
-        float4 coords = mad(float4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
-        float4 c;
-        c.xy = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1,  0)).rg;
-        c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1,  0)).rg;
-        c.yxwz = SMAADecodeDiagBilinearAccess(c.xyzw);
-
-        // Non-optimized version:
-        // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
-        // float4 c;
-        // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1,  0)).g;
-        // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0,  0)).r;
-        // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1,  0)).g;
-        // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r;
-
-        // Merge crossing edges at each side into a single value:
-        float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw);
-
-        // Remove the crossing edge if we didn't found the end of the line:
-        SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0));
-
-        // Fetch the areas for this line:
-        weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.z);
-    }
-
-    // Search for the line ends:
-    d.xz = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, -1.0), end);
-    if (SMAASampleLevelZeroOffset(edgesTex, texcoord, int2(1, 0)).r > 0.0) {
-        d.yw = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, 1.0), end);
-        d.y += float(end.y > 0.9);
-    } else
-        d.yw = float2(0.0, 0.0);
-
-    SMAA_BRANCH
-    if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
-        // Fetch the crossing edges:
-        float4 coords = mad(float4(-d.x, -d.x, d.y, d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
-        float4 c;
-        c.x  = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1,  0)).g;
-        c.y  = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, -1)).r;
-        c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1,  0)).gr;
-        float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw);
-
-        // Remove the crossing edge if we didn't found the end of the line:
-        SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0));
-
-        // Fetch the areas for this line:
-        weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.w).gr;
-    }
-
-    return weights;
-}
-#endif
-
-//-----------------------------------------------------------------------------
-// Horizontal/Vertical Search Functions
-
-/**
- * This allows to determine how much length should we add in the last step
- * of the searches. It takes the bilinearly interpolated edge (see 
- * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
- * crossing edges are active.
- */
-float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset) {
-    // The texture is flipped vertically, with left and right cases taking half
-    // of the space horizontally:
-    float2 scale = SMAA_SEARCHTEX_SIZE * float2(0.5, -1.0);
-    float2 bias = SMAA_SEARCHTEX_SIZE * float2(offset, 1.0);
-
-    // Scale and bias to access texel centers:
-    scale += float2(-1.0,  1.0);
-    bias  += float2( 0.5, -0.5);
-
-    // Convert from pixel coordinates to texcoords:
-    // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped)
-    scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
-    bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
-
-    // Lookup the search texture:
-    return SMAA_SEARCHTEX_SELECT(SMAASampleLevelZero(searchTex, mad(scale, e, bias)));
-}
-
-/**
- * Horizontal/vertical search functions for the 2nd pass.
- */
-float SMAASearchXLeft(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
-    /**
-     * @PSEUDO_GATHER4
-     * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
-     * sample between edge, thus fetching four edges in a row.
-     * Sampling with different offsets in each direction allows to disambiguate
-     * which edges are active from the four fetched ones.
-     */
-    float2 e = float2(0.0, 1.0);
-    while (texcoord.x > end && 
-           e.g > 0.8281 && // Is there some edge not activated?
-           e.r == 0.0) { // Or is there a crossing edge that breaks the line?
-        e = SMAASampleLevelZero(edgesTex, texcoord).rg;
-        texcoord = mad(-float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
-    }
-
-    float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0), 3.25);
-    return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
-
-    // Non-optimized version:
-    // We correct the previous (-0.25, -0.125) offset we applied:
-    // texcoord.x += 0.25 * SMAA_RT_METRICS.x;
-
-    // The searches are bias by 1, so adjust the coords accordingly:
-    // texcoord.x += SMAA_RT_METRICS.x;
-
-    // Disambiguate the length added by the last step:
-    // texcoord.x += 2.0 * SMAA_RT_METRICS.x; // Undo last step
-    // texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) * SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0);
-    // return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
-}
-
-float SMAASearchXRight(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
-    float2 e = float2(0.0, 1.0);
-    while (texcoord.x < end && 
-           e.g > 0.8281 && // Is there some edge not activated?
-           e.r == 0.0) { // Or is there a crossing edge that breaks the line?
-        e = SMAASampleLevelZero(edgesTex, texcoord).rg;
-        texcoord = mad(float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
-    }
-    float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.5), 3.25);
-    return mad(-SMAA_RT_METRICS.x, offset, texcoord.x);
-}
-
-float SMAASearchYUp(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
-    float2 e = float2(1.0, 0.0);
-    while (texcoord.y > end && 
-           e.r > 0.8281 && // Is there some edge not activated?
-           e.g == 0.0) { // Or is there a crossing edge that breaks the line?
-        e = SMAASampleLevelZero(edgesTex, texcoord).rg;
-        texcoord = mad(-float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
-    }
-    float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.0), 3.25);
-    return mad(SMAA_RT_METRICS.y, offset, texcoord.y);
-}
-
-float SMAASearchYDown(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
-    float2 e = float2(1.0, 0.0);
-    while (texcoord.y < end && 
-           e.r > 0.8281 && // Is there some edge not activated?
-           e.g == 0.0) { // Or is there a crossing edge that breaks the line?
-        e = SMAASampleLevelZero(edgesTex, texcoord).rg;
-        texcoord = mad(float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
-    }
-    float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.5), 3.25);
-    return mad(-SMAA_RT_METRICS.y, offset, texcoord.y);
-}
-
-/** 
- * Ok, we have the distance and both crossing edges. So, what are the areas
- * at each side of current edge?
- */
-float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset) {
-    // Rounding prevents precision errors of bilinear filtering:
-    float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * float2(e1, e2)), dist);
-    
-    // We do a scale and bias for mapping to texel space:
-    texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
-
-    // Move to proper place, according to the subpixel offset:
-    texcoord.y = mad(SMAA_AREATEX_SUBTEX_SIZE, offset, texcoord.y);
-
-    // Do it!
-    return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord));
-}
-
-//-----------------------------------------------------------------------------
-// Corner Detection Functions
-
-void SMAADetectHorizontalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) {
-    #if !defined(SMAA_DISABLE_CORNER_DETECTION)
-    float2 leftRight = step(d.xy, d.yx);
-    float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
-
-    rounding /= leftRight.x + leftRight.y; // Reduce blending for pixels in the center of a line.
-
-    float2 factor = float2(1.0, 1.0);
-    factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0,  1)).r;
-    factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1,  1)).r;
-    factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, -2)).r;
-    factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, -2)).r;
-
-    weights *= saturate(factor);
-    #endif
-}
-
-void SMAADetectVerticalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) {
-    #if !defined(SMAA_DISABLE_CORNER_DETECTION)
-    float2 leftRight = step(d.xy, d.yx);
-    float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
-
-    rounding /= leftRight.x + leftRight.y;
-
-    float2 factor = float2(1.0, 1.0);
-    factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2( 1, 0)).g;
-    factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2( 1, 1)).g;
-    factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(-2, 0)).g;
-    factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(-2, 1)).g;
-
-    weights *= saturate(factor);
-    #endif
-}
-
-//-----------------------------------------------------------------------------
-// Blending Weight Calculation Pixel Shader (Second Pass)
-
-float4 SMAABlendingWeightCalculationPS(float2 texcoord,
-                                       float2 pixcoord,
-                                       float4 offset[3],
-                                       SMAATexture2D(edgesTex),
-                                       SMAATexture2D(areaTex),
-                                       SMAATexture2D(searchTex),
-                                       float4 subsampleIndices) { // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES.
-    float4 weights = float4(0.0, 0.0, 0.0, 0.0);
-
-    float2 e = SMAASample(edgesTex, texcoord).rg;
-
-    SMAA_BRANCH
-    if (e.g > 0.0) { // Edge at north
-        #if !defined(SMAA_DISABLE_DIAG_DETECTION)
-        // Diagonals have both north and west edges, so searching for them in
-        // one of the boundaries is enough.
-        weights.rg = SMAACalculateDiagWeights(SMAATexturePass2D(edgesTex), SMAATexturePass2D(areaTex), texcoord, e, subsampleIndices);
-
-        // We give priority to diagonals, so if we find a diagonal we skip 
-        // horizontal/vertical processing.
-        SMAA_BRANCH
-        if (weights.r == -weights.g) { // weights.r + weights.g == 0.0
-        #endif
-
-        float2 d;
-
-        // Find the distance to the left:
-        float3 coords;
-        coords.x = SMAASearchXLeft(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].xy, offset[2].x);
-        coords.y = offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET)
-        d.x = coords.x;
-
-        // Now fetch the left crossing edges, two at a time using bilinear
-        // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
-        // discern what value each edge has:
-        float e1 = SMAASampleLevelZero(edgesTex, coords.xy).r;
-
-        // Find the distance to the right:
-        coords.z = SMAASearchXRight(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].zw, offset[2].y);
-        d.y = coords.z;
-
-        // We want the distances to be in pixel units (doing this here allow to
-        // better interleave arithmetic and memory accesses):
-        d = abs(round(mad(SMAA_RT_METRICS.zz, d, -pixcoord.xx)));
-
-        // SMAAArea below needs a sqrt, as the areas texture is compressed
-        // quadratically:
-        float2 sqrt_d = sqrt(d);
-
-        // Fetch the right crossing edges:
-        float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, int2(1, 0)).r;
-
-        // Ok, we know how this pattern looks like, now it is time for getting
-        // the actual area:
-        weights.rg = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.y);
-
-        // Fix corners:
-        coords.y = texcoord.y;
-        SMAADetectHorizontalCornerPattern(SMAATexturePass2D(edgesTex), weights.rg, coords.xyzy, d);
-
-        #if !defined(SMAA_DISABLE_DIAG_DETECTION)
-        } else
-            e.r = 0.0; // Skip vertical processing.
-        #endif
-    }
-
-    SMAA_BRANCH
-    if (e.r > 0.0) { // Edge at west
-        float2 d;
-
-        // Find the distance to the top:
-        float3 coords;
-        coords.y = SMAASearchYUp(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].xy, offset[2].z);
-        coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x;
-        d.x = coords.y;
-
-        // Fetch the top crossing edges:
-        float e1 = SMAASampleLevelZero(edgesTex, coords.xy).g;
-
-        // Find the distance to the bottom:
-        coords.z = SMAASearchYDown(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].zw, offset[2].w);
-        d.y = coords.z;
-
-        // We want the distances to be in pixel units:
-        d = abs(round(mad(SMAA_RT_METRICS.ww, d, -pixcoord.yy)));
-
-        // SMAAArea below needs a sqrt, as the areas texture is compressed 
-        // quadratically:
-        float2 sqrt_d = sqrt(d);
-
-        // Fetch the bottom crossing edges:
-        float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.xz, int2(0, 1)).g;
-
-        // Get the area for this direction:
-        weights.ba = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.x);
-
-        // Fix corners:
-        coords.x = texcoord.x;
-        SMAADetectVerticalCornerPattern(SMAATexturePass2D(edgesTex), weights.ba, coords.xyxz, d);
-    }
-
-    return weights;
-}
-
-//-----------------------------------------------------------------------------
-// Neighborhood Blending Pixel Shader (Third Pass)
-
-float4 SMAANeighborhoodBlendingPS(float2 texcoord,
-                                  float4 offset,
-                                  SMAATexture2D(colorTex),
-                                  SMAATexture2D(blendTex)
-                                  #if SMAA_REPROJECTION
-                                  , SMAATexture2D(velocityTex)
-                                  #endif
-                                  ) {
-    // Fetch the blending weights for current pixel:
-    float4 a;
-    a.x = SMAASample(blendTex, offset.xy).a; // Right
-    a.y = SMAASample(blendTex, offset.zw).g; // Top
-    a.wz = SMAASample(blendTex, texcoord).xz; // Bottom / Left
-
-    // Is there any blending weight with a value greater than 0.0?
-    SMAA_BRANCH
-    if (dot(a, float4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
-        float4 color = SMAASampleLevelZero(colorTex, texcoord);
-
-        #if SMAA_REPROJECTION
-        float2 velocity = SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, texcoord));
-
-        // Pack velocity into the alpha channel:
-        color.a = sqrt(5.0 * length(velocity));
-        #endif
-
-        return color;
-    } else {
-        bool h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical)
-
-        // Calculate the blending offsets:
-        float4 blendingOffset = float4(0.0, a.y, 0.0, a.w);
-        float2 blendingWeight = a.yw;
-        SMAAMovc(bool4(h, h, h, h), blendingOffset, float4(a.x, 0.0, a.z, 0.0));
-        SMAAMovc(bool2(h, h), blendingWeight, a.xz);
-        blendingWeight /= dot(blendingWeight, float2(1.0, 1.0));
-
-        // Calculate the texture coordinates:
-        float4 blendingCoord = mad(blendingOffset, float4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy), texcoord.xyxy);
-
-        // We exploit bilinear filtering to mix current pixel with the chosen
-        // neighbor:
-        float4 color = blendingWeight.x * SMAASampleLevelZero(colorTex, blendingCoord.xy);
-        color += blendingWeight.y * SMAASampleLevelZero(colorTex, blendingCoord.zw);
-
-        #if SMAA_REPROJECTION
-        // Antialias velocity for proper reprojection in a later stage:
-        float2 velocity = blendingWeight.x * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.xy));
-        velocity += blendingWeight.y * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.zw));
-
-        // Pack velocity into the alpha channel:
-        color.a = sqrt(5.0 * length(velocity));
-        #endif
-
-        return color;
-    }
-}
-
-//-----------------------------------------------------------------------------
-// Temporal Resolve Pixel Shader (Optional Pass)
-
-float4 SMAAResolvePS(float2 texcoord,
-                     SMAATexture2D(currentColorTex),
-                     SMAATexture2D(previousColorTex)
-                     #if SMAA_REPROJECTION
-                     , SMAATexture2D(velocityTex)
-                     #endif
-                     ) {
-    #if SMAA_REPROJECTION
-    // Velocity is assumed to be calculated for motion blur, so we need to
-    // inverse it for reprojection:
-    float2 velocity = -SMAA_DECODE_VELOCITY(SMAASamplePoint(velocityTex, texcoord).rg);
-
-    // Fetch current pixel:
-    float4 current = SMAASamplePoint(currentColorTex, texcoord);
-
-    // Reproject current coordinates and fetch previous pixel:
-    float4 previous = SMAASamplePoint(previousColorTex, texcoord + velocity);
-
-    // Attenuate the previous pixel if the velocity is different:
-    float delta = abs(current.a * current.a - previous.a * previous.a) / 5.0;
-    float weight = 0.5 * saturate(1.0 - sqrt(delta) * SMAA_REPROJECTION_WEIGHT_SCALE);
-
-    // Blend the pixels according to the calculated weight:
-    return lerp(current, previous, weight);
-    #else
-    // Just blend the pixels:
-    float4 current = SMAASamplePoint(currentColorTex, texcoord);
-    float4 previous = SMAASamplePoint(previousColorTex, texcoord);
-    return lerp(current, previous, 0.5);
-    #endif
-}
-
-//-----------------------------------------------------------------------------
-// Separate Multisamples Pixel Shader (Optional Pass)
-
-#ifdef SMAALoad
-void SMAASeparatePS(float4 position,
-                    float2 texcoord,
-                    out float4 target0,
-                    out float4 target1,
-                    SMAATexture2DMS2(colorTexMS)) {
-    int2 pos = int2(position.xy);
-    target0 = SMAALoad(colorTexMS, pos, 0);
-    target1 = SMAALoad(colorTexMS, pos, 1);
-}
-#endif
-
-//-----------------------------------------------------------------------------
-#endif // SMAA_INCLUDE_PS

+ 0 - 28
shaders/SmaaEdge.frag.glsl

@@ -1,28 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-#define SMAA_GLSL_4
-#define SMAA_INCLUDE_PS 1
-#define SMAA_INCLUDE_VS 0
-#include "shaders/SMAA.hlsl"
-
-layout(location = 0) out vec2 out_color;
-
-layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_isRt;
-
-layout(location = 0) in vec2 in_uv;
-layout(location = 1) in vec4 in_offset0;
-layout(location = 2) in vec4 in_offset1;
-layout(location = 3) in vec4 in_offset2;
-
-void main()
-{
-	vec4 offsets[3];
-	offsets[0] = in_offset0;
-	offsets[1] = in_offset1;
-	offsets[2] = in_offset2;
-	out_color = SMAAColorEdgeDetectionPS(in_uv, offsets, u_isRt);
-}

+ 0 - 34
shaders/SmaaEdge.vert.glsl

@@ -1,34 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-
-#define SMAA_GLSL_4
-#define SMAA_INCLUDE_PS 0
-#define SMAA_INCLUDE_VS 1
-#include "shaders/SMAA.hlsl"
-
-layout(location = 0) out vec2 out_uv;
-layout(location = 1) out vec4 out_offset0;
-layout(location = 2) out vec4 out_offset1;
-layout(location = 3) out vec4 out_offset2;
-
-void main()
-{
-	const vec2 POSITIONS[3] = vec2[](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
-	vec2 pos = POSITIONS[gl_VertexID];
-	out_uv = pos * 0.5 + 0.5;
-
-	vec4 offsets[3];
-	offsets[0] = vec4(0.0);
-	offsets[1] = vec4(0.0);
-	offsets[2] = vec4(0.0);
-	SMAAEdgeDetectionVS(out_uv, offsets);
-	out_offset0 = offsets[0];
-	out_offset1 = offsets[1];
-	out_offset2 = offsets[2];
-
-	ANKI_WRITE_POSITION(vec4(pos, 0.0, 1.0));
-}

+ 0 - 33
shaders/SmaaWeights.frag.glsl

@@ -1,33 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-
-#define SMAA_GLSL_4
-#define SMAA_INCLUDE_PS 1
-#define SMAA_INCLUDE_VS 0
-#include "shaders/SMAA.hlsl"
-
-layout(location = 0) out vec4 out_color;
-
-layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_edgesTex;
-layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_areaTex;
-layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_searchTex;
-
-layout(location = 0) in vec2 in_uv;
-layout(location = 1) in vec2 in_pixcoord;
-layout(location = 2) in vec4 in_offset0;
-layout(location = 3) in vec4 in_offset1;
-layout(location = 4) in vec4 in_offset2;
-
-void main()
-{
-	vec4 offsets[3];
-	offsets[0] = in_offset0;
-	offsets[1] = in_offset1;
-	offsets[2] = in_offset2;
-	out_color =
-		SMAABlendingWeightCalculationPS(in_uv, in_pixcoord, offsets, u_edgesTex, u_areaTex, u_searchTex, vec4(0.0));
-}

+ 0 - 36
shaders/SmaaWeights.vert.glsl

@@ -1,36 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-
-#define SMAA_GLSL_4
-#define SMAA_INCLUDE_PS 0
-#define SMAA_INCLUDE_VS 1
-#include "shaders/SMAA.hlsl"
-
-layout(location = 0) out vec2 out_uv;
-layout(location = 1) out vec2 out_pixcoord;
-layout(location = 2) out vec4 out_offset0;
-layout(location = 3) out vec4 out_offset1;
-layout(location = 4) out vec4 out_offset2;
-
-void main(void)
-{
-	const vec2 POSITIONS[3] = vec2[](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
-	vec2 pos = POSITIONS[gl_VertexID];
-	out_uv = pos * 0.5 + 0.5;
-
-	vec4 offsets[3];
-	offsets[0] = vec4(0.0);
-	offsets[1] = vec4(0.0);
-	offsets[2] = vec4(0.0);
-	out_pixcoord = vec2(0.0);
-	SMAABlendingWeightCalculationVS(out_uv, out_pixcoord, offsets);
-	out_offset0 = offsets[0];
-	out_offset1 = offsets[1];
-	out_offset2 = offsets[2];
-
-	ANKI_WRITE_POSITION(vec4(pos, 0.0, 1.0));
-}

+ 86 - 0
shaders/Taa.frag.glsl

@@ -0,0 +1,86 @@
+// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include "shaders/Functions.glsl"
+#include "shaders/Pack.glsl"
+#include "shaders/Tonemapping.glsl"
+
+#define YCBCR 1
+const float BLEND_FACTOR = 1.0 / 16.0;
+
+layout(location = 0) in vec2 in_uv;
+
+layout(location = 0) out vec3 out_color;
+
+layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
+layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_inputRt;
+layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_historyRt;
+
+layout(ANKI_UBO_BINDING(0, 0), std140, row_major) uniform u0_
+{
+	mat4 u_prevViewProjMat;
+	mat4 u_invViewProjMat;
+};
+
+#if YCBCR
+#define sample(s, uv) rgbToYCbCr(textureLod(s, uv, 0.0).rgb)
+#define sampleOffset(s, uv, x, y) rgbToYCbCr(textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb)
+#else
+#define sample(s, uv) textureLod(s, uv, 0.0).rgb
+#define sampleOffset(s, uv, x, y) textureLodOffset(s, uv, 0.0, ivec2(x, y)).rgb
+#endif
+
+void main()
+{
+	float depth = textureLod(u_depthRt, in_uv, 0.0).r;
+
+	// Get world position
+	vec4 worldPos4 = u_invViewProjMat * vec4(UV_TO_NDC(in_uv), UV_TO_NDC(depth), 1.0);
+	worldPos4 = worldPos4 / worldPos4.w;
+	vec3 worldPos = worldPos4.xyz;
+
+	// Project pos to get old ndc
+	vec4 oldNdc4 = u_prevViewProjMat * vec4(worldPos, 1.0);
+	vec2 oldNdc = oldNdc4.xy / oldNdc4.w;
+	vec2 oldUv = NDC_TO_UV(oldNdc);
+
+	// Read textures
+	vec3 historyCol = sample(u_historyRt, oldUv);
+	vec3 crntCol = sample(u_inputRt, in_uv);
+
+	// Remove ghosting by clamping the history color to neighbour's AABB
+	vec3 near0 = sampleOffset(u_inputRt, in_uv, 1, 0);
+	vec3 near1 = sampleOffset(u_inputRt, in_uv, 0, 1);
+	vec3 near2 = sampleOffset(u_inputRt, in_uv, -1, 0);
+	vec3 near3 = sampleOffset(u_inputRt, in_uv, 0, -1);
+
+	vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
+	vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
+
+	historyCol = clamp(historyCol, boxMin, boxMax);
+
+// Remove jitter (T. Lottes)
+#if YCBCR
+	float lum0 = crntCol.r;
+	float lum1 = historyCol.r;
+	float maxLum = boxMax.r;
+#else
+	float lum0 = computeLuminance(crntCol);
+	float lum1 = computeLuminance(historyCol);
+	float maxLum = computeLuminance(boxMax);
+#endif
+
+	float diff = abs(lum0 - lum1) / max(lum0, max(lum1, maxLum));
+	diff = 1.0 - diff;
+	diff = diff * diff;
+	float feedback = mix(0.0, BLEND_FACTOR, diff);
+
+// Write result
+#if YCBCR
+	out_color = yCbCrToRgb(mix(historyCol, crntCol, feedback));
+#else
+	out_color = mix(historyCol, crntCol, feedback);
+#endif
+}

+ 1 - 1
src/anki/renderer/Common.h

@@ -36,7 +36,7 @@ class FsUpscale;
 class DownscaleBlur;
 class Volumetric;
 class DepthDownscale;
-class Smaa;
+class Taa;
 
 class RenderingContext;
 class DebugDrawer;

+ 2 - 2
src/anki/renderer/DownscaleBlur.cpp

@@ -5,7 +5,7 @@
 
 #include <anki/renderer/DownscaleBlur.h>
 #include <anki/renderer/Renderer.h>
-#include <anki/renderer/Is.h>
+#include <anki/renderer/Taa.h>
 
 namespace anki
 {
@@ -90,7 +90,7 @@ void DownscaleBlur::run(RenderingContext& ctx)
 {
 	CommandBufferPtr cmdb = ctx.m_commandBuffer;
 
-	cmdb->bindTexture(0, 0, m_r->getIs().getRt());
+	cmdb->bindTexture(0, 0, m_r->getTaa().getRt());
 
 	for(U i = 0; i < m_passes.getSize(); ++i)
 	{

+ 3 - 1
src/anki/renderer/Is.cpp

@@ -27,6 +27,7 @@ public:
 	UVec4 m_tileCount;
 	Mat4 m_invViewProjMat;
 	Mat4 m_prevViewProjMat;
+	Mat4 m_invProjMat;
 };
 
 enum class ShaderVariantBit : U8
@@ -214,8 +215,9 @@ void Is::updateCommonBlock(RenderingContext& ctx)
 
 	blk->m_tileCount = UVec4(m_clusterCounts[0], m_clusterCounts[1], m_clusterCounts[2], m_clusterCount);
 
-	blk->m_invViewProjMat = ctx.m_viewProjMat.getInverse();
+	blk->m_invViewProjMat = ctx.m_viewProjMatJitter.getInverse();
 	blk->m_prevViewProjMat = ctx.m_prevViewProjMat;
+	blk->m_invProjMat = ctx.m_projMatJitter.getInverse();
 }
 
 void Is::setPreRunBarriers(RenderingContext& ctx)

+ 1 - 1
src/anki/renderer/Ms.cpp

@@ -125,7 +125,7 @@ Error Ms::buildCommandBuffers(RenderingContext& ctx, U threadId, U threadCount)
 		// Start drawing
 		ANKI_CHECK(m_r->getSceneDrawer().drawRange(Pass::MS_FS,
 			ctx.m_viewMat,
-			ctx.m_viewProjMat,
+			ctx.m_viewProjMatJitter,
 			cmdb,
 			vis.getBegin(VisibilityGroupType::RENDERABLES_MS) + start,
 			vis.getBegin(VisibilityGroupType::RENDERABLES_MS) + end));

+ 5 - 26
src/anki/renderer/Pps.cpp

@@ -6,12 +6,13 @@
 #include <anki/renderer/Pps.h>
 #include <anki/renderer/Renderer.h>
 #include <anki/renderer/Bloom.h>
+#include <anki/renderer/Taa.h>
 #include <anki/renderer/Sslf.h>
 #include <anki/renderer/Tm.h>
 #include <anki/renderer/Is.h>
 #include <anki/renderer/Ms.h>
 #include <anki/renderer/Dbg.h>
-#include <anki/renderer/Smaa.h>
+#include <anki/renderer/DownscaleBlur.h>
 #include <anki/util/Logger.h>
 #include <anki/misc/ConfigSet.h>
 #include <anki/scene/SceneNode.h>
@@ -107,9 +108,6 @@ Error Pps::run(RenderingContext& ctx)
 				"#define LUT_SIZE %u.0\n"
 				"#define DBG_ENABLED %u\n"
 				"#define DRAW_TO_DEFAULT %u\n"
-				"#define SMAA_ENABLED 1\n"
-				"#define SMAA_RT_METRICS vec4(%f, %f, %f, %f)\n"
-				"#define SMAA_PRESET_%s\n"
 				"#define FB_SIZE vec2(float(%u), float(%u))\n",
 				true,
 				m_sharpenEnabled,
@@ -118,38 +116,19 @@ Error Pps::run(RenderingContext& ctx)
 				LUT_SIZE,
 				dbgEnabled,
 				drawToDefaultFb,
-				1.0 / m_r->getWidth(),
-				1.0 / m_r->getHeight(),
-				F32(m_r->getWidth()),
-				F32(m_r->getHeight()),
-				&m_r->getSmaa().m_qualityPerset[0],
 				m_r->getWidth(),
 				m_r->getHeight()));
 		}
 
-		if(!m_vert)
-		{
-			ANKI_CHECK(m_r->createShaderf("shaders/Pps.vert.glsl",
-				m_vert,
-				"#define SMAA_ENABLED 1\n"
-				"#define SMAA_RT_METRICS vec4(%f, %f, %f, %f)\n"
-				"#define SMAA_PRESET_%s\n",
-				1.0 / m_r->getWidth(),
-				1.0 / m_r->getHeight(),
-				F32(m_r->getWidth()),
-				F32(m_r->getHeight()),
-				&m_r->getSmaa().m_qualityPerset[0]));
-		}
-
-		prog = getGrManager().newInstance<ShaderProgram>(m_vert->getGrShader(), frag->getGrShader());
+		m_r->createDrawQuadShaderProgram(frag->getGrShader(), prog);
 	}
 
 	// Bind stuff
-	cmdb->bindTexture(0, 0, m_r->getIs().getRt());
+	cmdb->bindTextureAndSampler(
+		0, 0, m_r->getTaa().getRt(), (drawToDefaultFb) ? m_r->getNearestSampler() : m_r->getLinearSampler());
 	cmdb->bindTexture(0, 1, m_r->getBloom().m_upscale.m_rt);
 	cmdb->bindTexture(0, 2, m_lut->getGrTexture());
 	cmdb->bindTexture(0, 3, m_blueNoise->getGrTexture());
-	cmdb->bindTexture(0, 4, m_r->getSmaa().m_weights.m_rt);
 	if(dbgEnabled)
 	{
 		cmdb->bindTexture(0, 5, m_r->getDbg().getRt());

+ 0 - 1
src/anki/renderer/Pps.h

@@ -45,7 +45,6 @@ private:
 
 	FramebufferPtr m_fb;
 	Array2d<ShaderResourcePtr, 2, 2> m_frag; ///< One with Dbg and one without
-	ShaderResourcePtr m_vert;
 	Array2d<ShaderProgramPtr, 2, 2> m_prog; ///< With Dbg, Default FB or not
 	TexturePtr m_rt;
 

+ 81 - 9
src/anki/renderer/Renderer.cpp

@@ -24,7 +24,7 @@
 #include <anki/renderer/DownscaleBlur.h>
 #include <anki/renderer/Volumetric.h>
 #include <anki/renderer/DepthDownscale.h>
-#include <anki/renderer/Smaa.h>
+#include <anki/renderer/Taa.h>
 
 #include <cstdarg> // For var args
 
@@ -150,8 +150,8 @@ Error Renderer::initInternal(const ConfigSet& config)
 	m_tm.reset(getAllocator().newInstance<Tm>(this));
 	ANKI_CHECK(m_tm->init(config));
 
-	m_smaa.reset(getAllocator().newInstance<Smaa>(this));
-	ANKI_CHECK(m_smaa->init(config));
+	m_taa.reset(getAllocator().newInstance<Taa>(this));
+	ANKI_CHECK(m_taa->init(config));
 
 	m_bloom.reset(m_alloc.newInstance<Bloom>(this));
 	ANKI_CHECK(m_bloom->init(config));
@@ -162,13 +162,84 @@ Error Renderer::initInternal(const ConfigSet& config)
 	m_dbg.reset(m_alloc.newInstance<Dbg>(this));
 	ANKI_CHECK(m_dbg->init(config));
 
+	SamplerInitInfo sinit;
+	sinit.m_repeat = false;
+	sinit.m_minMagFilter = SamplingFilter::NEAREST;
+	m_nearestSampler = m_gr->newInstance<Sampler>(sinit);
+
+	sinit.m_minMagFilter = SamplingFilter::LINEAR;
+	m_linearSampler = m_gr->newInstance<Sampler>(sinit);
+
+	initJitteredMats();
+
 	return ErrorCode::NONE;
 }
 
+void Renderer::initJitteredMats()
+{
+	static const Array<Vec2, 16> SAMPLE_LOCS_16 = {{Vec2(-8.0, 0.0),
+		Vec2(-6.0, -4.0),
+		Vec2(-3.0, -2.0),
+		Vec2(-2.0, -6.0),
+		Vec2(1.0, -1.0),
+		Vec2(2.0, -5.0),
+		Vec2(6.0, -7.0),
+		Vec2(5.0, -3.0),
+		Vec2(4.0, 1.0),
+		Vec2(7.0, 4.0),
+		Vec2(3.0, 5.0),
+		Vec2(0.0, 7.0),
+		Vec2(-1.0, 3.0),
+		Vec2(-4.0, 6.0),
+		Vec2(-7.0, 8.0),
+		Vec2(-5.0, 2.0)}};
+
+	for(U i = 0; i < 16; ++i)
+	{
+		Vec2 texSize(1.0f / Vec2(m_width, m_height)); // Texel size
+		texSize *= 2.0f; // Move it to NDC
+
+		Vec2 S = SAMPLE_LOCS_16[i] / 8.0f; // In [-1, 1]
+
+		Vec2 subSample = S * texSize; // In [-texSize, texSize]
+		subSample *= 0.5f; // In [-texSize / 2, texSize / 2]
+
+		m_jitteredMats16x[i] = Mat4::getIdentity();
+		m_jitteredMats16x[i].setTranslationPart(Vec4(subSample, 0.0, 1.0));
+	}
+
+	static const Array<Vec2, 8> SAMPLE_LOCS_8 = {{Vec2(-7.0, 1.0),
+		Vec2(-5.0, -5.0),
+		Vec2(-1.0, -3.0),
+		Vec2(3.0, -7.0),
+		Vec2(5.0, -1.0),
+		Vec2(7.0, 7.0),
+		Vec2(1.0, 3.0),
+		Vec2(-3.0, 5.0)}};
+
+	for(U i = 0; i < 8; ++i)
+	{
+		Vec2 texSize(1.0f / Vec2(m_width, m_height)); // Texel size
+		texSize *= 2.0f; // Move it to NDC
+
+		Vec2 S = SAMPLE_LOCS_8[i] / 8.0f; // In [-1, 1]
+
+		Vec2 subSample = S * texSize; // In [-texSize, texSize]
+		subSample *= 0.5f; // In [-texSize / 2, texSize / 2]
+
+		m_jitteredMats8x[i] = Mat4::getIdentity();
+		m_jitteredMats8x[i].setTranslationPart(Vec4(subSample, 0.0, 1.0));
+	}
+}
+
 Error Renderer::render(RenderingContext& ctx)
 {
 	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
 
+	ctx.m_jitterMat = m_jitteredMats16x[m_frameCount & (16 - 1)];
+	ctx.m_projMatJitter = ctx.m_jitterMat * ctx.m_projMat;
+	ctx.m_viewProjMatJitter = ctx.m_projMatJitter * ctx.m_viewMat;
+
 	ctx.m_prevViewProjMat = m_prevViewProjMat;
 	ctx.m_prevCamTransform = m_prevCamTransform;
 
@@ -269,6 +340,13 @@ Error Renderer::render(RenderingContext& ctx)
 		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ_WRITE,
 		TextureUsageBit::SAMPLED_FRAGMENT,
 		TextureSurfaceInfo(0, 0, 0, 0));
+	m_taa->setPreRunBarriers(ctx);
+
+	// Passes
+	m_taa->run(ctx);
+
+	// Barriers
+	m_taa->setPostRunBarriers(ctx);
 	m_downscale->setPreRunBarriers(ctx);
 
 	// Passes
@@ -276,25 +354,19 @@ Error Renderer::render(RenderingContext& ctx)
 
 	// Barriers
 	m_downscale->setPostRunBarriers(ctx);
-	m_smaa->m_edge.setPreRunBarriers(ctx);
 
 	// Passes
 	m_tm->run(ctx);
-	m_smaa->m_edge.run(ctx);
 
 	// Barriers
-	m_smaa->m_edge.setPostRunBarriers(ctx);
 	m_bloom->m_extractExposure.setPreRunBarriers(ctx);
-	m_smaa->m_weights.setPreRunBarriers(ctx);
 
 	// Passes
 	m_bloom->m_extractExposure.run(ctx);
-	m_smaa->m_weights.run(ctx);
 
 	// Barriers
 	m_bloom->m_extractExposure.setPostRunBarriers(ctx);
 	m_bloom->m_upscale.setPreRunBarriers(ctx);
-	m_smaa->m_weights.setPostRunBarriers(ctx);
 
 	// Passes
 	m_bloom->m_upscale.run(ctx);

+ 26 - 3
src/anki/renderer/Renderer.h

@@ -37,6 +37,11 @@ public:
 	Mat4 m_viewMat;
 	Mat4 m_projMat;
 	Mat4 m_viewProjMat;
+
+	Mat4 m_projMatJitter;
+	Mat4 m_viewProjMatJitter;
+	Mat4 m_jitterMat;
+
 	Mat4 m_camTrfMat;
 	F32 m_near;
 	F32 m_far;
@@ -215,9 +220,9 @@ public:
 		return *m_dbg;
 	}
 
-	Smaa& getSmaa()
+	Taa& getTaa()
 	{
-		return *m_smaa;
+		return *m_taa;
 	}
 
 	DownscaleBlur& getDownscaleBlur()
@@ -389,6 +394,16 @@ anki_internal:
 		return 1024;
 	}
 
+	SamplerPtr getNearestSampler() const
+	{
+		return m_nearestSampler;
+	}
+
+	SamplerPtr getLinearSampler() const
+	{
+		return m_linearSampler;
+	}
+
 private:
 	ThreadPool* m_threadpool = nullptr;
 	ResourceManager* m_resources = nullptr;
@@ -410,7 +425,7 @@ private:
 	UniquePtr<Lf> m_lf; ///< Forward shading lens flares.
 	UniquePtr<FsUpscale> m_fsUpscale;
 	UniquePtr<DownscaleBlur> m_downscale;
-	UniquePtr<Smaa> m_smaa;
+	UniquePtr<Taa> m_taa;
 	UniquePtr<Tm> m_tm;
 	UniquePtr<Ssao> m_ssao;
 	UniquePtr<Bloom> m_bloom;
@@ -439,13 +454,21 @@ private:
 	Mat4 m_prevViewProjMat = Mat4::getIdentity();
 	Mat4 m_prevCamTransform = Mat4::getIdentity();
 
+	Array<Mat4, 16> m_jitteredMats16x;
+	Array<Mat4, 8> m_jitteredMats8x;
+
 	TexturePtr m_dummyTex;
 	BufferPtr m_dummyBuff;
 
+	SamplerPtr m_nearestSampler;
+	SamplerPtr m_linearSampler;
+
 	ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);
 
 	ANKI_USE_RESULT Error buildCommandBuffers(RenderingContext& ctx);
 	ANKI_USE_RESULT Error buildCommandBuffersInternal(RenderingContext& ctx, U32 threadId, PtrSize threadCount);
+
+	void initJitteredMats();
 };
 /// @}
 

+ 0 - 272
src/anki/renderer/Smaa.cpp

@@ -1,272 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include <anki/renderer/Smaa.h>
-#include <anki/renderer/Renderer.h>
-#include <anki/renderer/Is.h>
-#include <SMAA/AreaTex.h>
-#include <SMAA/SearchTex.h>
-
-namespace anki
-{
-
-static const PixelFormat EDGE_PIXEL_FORMAT(ComponentFormat::R8G8, TransformFormat::UNORM);
-static const PixelFormat WEIGHTS_PIXEL_FORMAT(ComponentFormat::R8G8B8A8, TransformFormat::UNORM);
-static const PixelFormat STENCIL_PIXEL_FORMAT(ComponentFormat::S8, TransformFormat::UINT);
-
-SmaaEdge::~SmaaEdge()
-{
-}
-
-Error SmaaEdge::init(const ConfigSet& initializer)
-{
-	GrManager& gr = getGrManager();
-
-	// Create shaders
-	StringAuto pps(getAllocator());
-	pps.sprintf("#define SMAA_RT_METRICS vec4(%f, %f, %f, %f)\n"
-				"#define SMAA_PRESET_%s\n",
-		1.0 / m_r->getWidth(),
-		1.0 / m_r->getHeight(),
-		F32(m_r->getWidth()),
-		F32(m_r->getHeight()),
-		&m_r->getSmaa().m_qualityPerset[0]);
-
-	ANKI_CHECK(m_r->createShader("shaders/SmaaEdge.vert.glsl", m_vert, pps.toCString()));
-	ANKI_CHECK(m_r->createShader("shaders/SmaaEdge.frag.glsl", m_frag, pps.toCString()));
-
-	// Create prog
-	m_prog = getGrManager().newInstance<ShaderProgram>(m_vert->getGrShader(), m_frag->getGrShader());
-
-	// Create RT
-	m_rt = m_r->createAndClearRenderTarget(m_r->create2DRenderTargetInitInfo(m_r->getWidth(),
-		m_r->getHeight(),
-		EDGE_PIXEL_FORMAT,
-		TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
-		SamplingFilter::LINEAR));
-
-	// Create FB
-	FramebufferInitInfo fbInit;
-	fbInit.m_colorAttachmentCount = 1;
-	fbInit.m_colorAttachments[0].m_texture = m_rt;
-	fbInit.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::CLEAR;
-	fbInit.m_depthStencilAttachment.m_texture = m_r->getSmaa().m_stencilTex;
-	fbInit.m_depthStencilAttachment.m_stencilLoadOperation = AttachmentLoadOperation::CLEAR;
-	m_fb = gr.newInstance<Framebuffer>(fbInit);
-
-	return ErrorCode::NONE;
-}
-
-void SmaaEdge::setPreRunBarriers(RenderingContext& ctx)
-{
-	ctx.m_commandBuffer->setTextureSurfaceBarrier(
-		m_rt, TextureUsageBit::NONE, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, TextureSurfaceInfo(0, 0, 0, 0));
-
-	ctx.m_commandBuffer->setTextureSurfaceBarrier(m_r->getSmaa().m_stencilTex,
-		TextureUsageBit::NONE,
-		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
-		TextureSurfaceInfo(0, 0, 0, 0));
-}
-
-void SmaaEdge::setPostRunBarriers(RenderingContext& ctx)
-{
-	ctx.m_commandBuffer->setTextureSurfaceBarrier(m_rt,
-		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
-		TextureUsageBit::SAMPLED_FRAGMENT,
-		TextureSurfaceInfo(0, 0, 0, 0));
-
-	ctx.m_commandBuffer->setTextureSurfaceBarrier(m_r->getSmaa().m_stencilTex,
-		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
-		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ,
-		TextureSurfaceInfo(0, 0, 0, 0));
-}
-
-void SmaaEdge::run(RenderingContext& ctx)
-{
-	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
-
-	cmdb->setViewport(0, 0, m_r->getWidth(), m_r->getHeight());
-	cmdb->bindShaderProgram(m_prog);
-	cmdb->bindTexture(0, 0, m_r->getIs().getRt());
-
-	cmdb->setStencilOperations(
-		FaceSelectionBit::FRONT, StencilOperation::KEEP, StencilOperation::KEEP, StencilOperation::REPLACE);
-	cmdb->setStencilCompareMask(FaceSelectionBit::FRONT, 0xF);
-	cmdb->setStencilWriteMask(FaceSelectionBit::FRONT, 0xF);
-	cmdb->setStencilReference(FaceSelectionBit::FRONT, 0xF);
-
-	cmdb->beginRenderPass(m_fb);
-	m_r->drawQuad(cmdb);
-	cmdb->endRenderPass();
-
-	// Restore state
-	cmdb->setStencilOperations(
-		FaceSelectionBit::FRONT, StencilOperation::KEEP, StencilOperation::KEEP, StencilOperation::KEEP);
-}
-
-SmaaWeights::~SmaaWeights()
-{
-}
-
-Error SmaaWeights::init(const ConfigSet& initializer)
-{
-	GrManager& gr = getGrManager();
-
-	// Create shaders
-	StringAuto pps(getAllocator());
-	pps.sprintf("#define SMAA_RT_METRICS vec4(%f, %f, %f, %f)\n"
-				"#define SMAA_PRESET_%s\n",
-		1.0 / m_r->getWidth(),
-		1.0 / m_r->getHeight(),
-		F32(m_r->getWidth()),
-		F32(m_r->getHeight()),
-		&m_r->getSmaa().m_qualityPerset[0]);
-
-	ANKI_CHECK(m_r->createShader("shaders/SmaaWeights.vert.glsl", m_vert, pps.toCString()));
-	ANKI_CHECK(m_r->createShader("shaders/SmaaWeights.frag.glsl", m_frag, pps.toCString()));
-
-	// Create prog
-	m_prog = getGrManager().newInstance<ShaderProgram>(m_vert->getGrShader(), m_frag->getGrShader());
-
-	// Create RT
-	m_rt = m_r->createAndClearRenderTarget(m_r->create2DRenderTargetInitInfo(m_r->getWidth(),
-		m_r->getHeight(),
-		WEIGHTS_PIXEL_FORMAT,
-		TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
-		SamplingFilter::LINEAR));
-
-	// Create FB
-	FramebufferInitInfo fbInit;
-	fbInit.m_colorAttachmentCount = 1;
-	fbInit.m_colorAttachments[0].m_texture = m_rt;
-	fbInit.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::CLEAR;
-	fbInit.m_depthStencilAttachment.m_texture = m_r->getSmaa().m_stencilTex;
-	fbInit.m_depthStencilAttachment.m_stencilLoadOperation = AttachmentLoadOperation::LOAD;
-	fbInit.m_depthStencilAttachment.m_stencilStoreOperation = AttachmentStoreOperation::DONT_CARE;
-	m_fb = gr.newInstance<Framebuffer>(fbInit);
-
-	// Create Area texture
-	CommandBufferInitInfo cmdbinit;
-	cmdbinit.m_flags = CommandBufferFlag::SMALL_BATCH;
-	CommandBufferPtr cmdb = gr.newInstance<CommandBuffer>(cmdbinit);
-
-	{
-		TextureInitInfo texinit;
-		texinit.m_width = AREATEX_WIDTH;
-		texinit.m_height = AREATEX_HEIGHT;
-		texinit.m_format = PixelFormat(ComponentFormat::R8G8, TransformFormat::UNORM);
-		texinit.m_usage = TextureUsageBit::TRANSFER_DESTINATION | TextureUsageBit::SAMPLED_FRAGMENT;
-		texinit.m_usageWhenEncountered = TextureUsageBit::SAMPLED_FRAGMENT;
-		texinit.m_sampling.m_minMagFilter = SamplingFilter::LINEAR;
-		texinit.m_sampling.m_repeat = false;
-		m_areaTex = gr.newInstance<Texture>(texinit);
-
-		StagingGpuMemoryToken token;
-		void* stagingMem = m_r->getStagingGpuMemoryManager().allocateFrame(
-			sizeof(areaTexBytes), StagingGpuMemoryType::TRANSFER, token);
-		memcpy(stagingMem, &areaTexBytes[0], sizeof(areaTexBytes));
-
-		const TextureSurfaceInfo surf(0, 0, 0, 0);
-		cmdb->setTextureSurfaceBarrier(m_areaTex, TextureUsageBit::NONE, TextureUsageBit::TRANSFER_DESTINATION, surf);
-		cmdb->copyBufferToTextureSurface(token.m_buffer, token.m_offset, token.m_range, m_areaTex, surf);
-		cmdb->setTextureSurfaceBarrier(
-			m_areaTex, TextureUsageBit::TRANSFER_DESTINATION, TextureUsageBit::SAMPLED_FRAGMENT, surf);
-	}
-
-	// Create search texture
-	{
-		TextureInitInfo texinit;
-		texinit.m_width = SEARCHTEX_WIDTH;
-		texinit.m_height = SEARCHTEX_HEIGHT;
-		texinit.m_format = PixelFormat(ComponentFormat::R8, TransformFormat::UNORM);
-		texinit.m_usage = TextureUsageBit::TRANSFER_DESTINATION | TextureUsageBit::SAMPLED_FRAGMENT;
-		texinit.m_usageWhenEncountered = TextureUsageBit::SAMPLED_FRAGMENT;
-		texinit.m_sampling.m_minMagFilter = SamplingFilter::LINEAR;
-		texinit.m_sampling.m_repeat = false;
-		m_searchTex = gr.newInstance<Texture>(texinit);
-
-		StagingGpuMemoryToken token;
-		void* stagingMem = m_r->getStagingGpuMemoryManager().allocateFrame(
-			sizeof(searchTexBytes), StagingGpuMemoryType::TRANSFER, token);
-		memcpy(stagingMem, &searchTexBytes[0], sizeof(searchTexBytes));
-
-		const TextureSurfaceInfo surf(0, 0, 0, 0);
-		cmdb->setTextureSurfaceBarrier(m_searchTex, TextureUsageBit::NONE, TextureUsageBit::TRANSFER_DESTINATION, surf);
-		cmdb->copyBufferToTextureSurface(token.m_buffer, token.m_offset, token.m_range, m_searchTex, surf);
-		cmdb->setTextureSurfaceBarrier(
-			m_searchTex, TextureUsageBit::TRANSFER_DESTINATION, TextureUsageBit::SAMPLED_FRAGMENT, surf);
-	}
-	cmdb->flush();
-
-	return ErrorCode::NONE;
-}
-
-void SmaaWeights::setPreRunBarriers(RenderingContext& ctx)
-{
-	ctx.m_commandBuffer->setTextureSurfaceBarrier(
-		m_rt, TextureUsageBit::NONE, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, TextureSurfaceInfo(0, 0, 0, 0));
-}
-
-void SmaaWeights::setPostRunBarriers(RenderingContext& ctx)
-{
-	ctx.m_commandBuffer->setTextureSurfaceBarrier(m_rt,
-		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
-		TextureUsageBit::SAMPLED_FRAGMENT,
-		TextureSurfaceInfo(0, 0, 0, 0));
-}
-
-void SmaaWeights::run(RenderingContext& ctx)
-{
-	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
-
-	cmdb->setViewport(0, 0, m_r->getWidth(), m_r->getHeight());
-	cmdb->bindTexture(0, 0, m_r->getSmaa().m_edge.m_rt);
-	cmdb->bindTexture(0, 1, m_areaTex);
-	cmdb->bindTexture(0, 2, m_searchTex);
-	cmdb->bindShaderProgram(m_prog);
-
-	cmdb->setStencilCompareOperation(FaceSelectionBit::FRONT, CompareOperation::EQUAL);
-	cmdb->setStencilCompareMask(FaceSelectionBit::FRONT, 0xF);
-	cmdb->setStencilWriteMask(FaceSelectionBit::FRONT, 0x0);
-	cmdb->setStencilReference(FaceSelectionBit::FRONT, 0xF);
-
-	cmdb->beginRenderPass(m_fb);
-	m_r->drawQuad(cmdb);
-	cmdb->endRenderPass();
-
-	// Restore state
-	cmdb->setStencilCompareOperation(FaceSelectionBit::FRONT, CompareOperation::ALWAYS);
-}
-
-Error Smaa::init(const ConfigSet& cfg)
-{
-	Error err = initInternal(cfg);
-	if(err)
-	{
-		ANKI_R_LOGE("Failed to initialize SMAA");
-	}
-
-	return err;
-}
-
-Error Smaa::initInternal(const ConfigSet& cfg)
-{
-	m_qualityPerset = "ULTRA";
-
-	ANKI_R_LOGI("Initializing SMAA in %s perset", &m_qualityPerset[0]);
-
-	TextureInitInfo texinit;
-	texinit.m_format = STENCIL_PIXEL_FORMAT;
-	texinit.m_width = m_r->getWidth();
-	texinit.m_height = m_r->getHeight();
-	texinit.m_usage = TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ_WRITE;
-	m_stencilTex = m_r->createAndClearRenderTarget(texinit);
-
-	ANKI_CHECK(m_edge.init(cfg));
-	ANKI_CHECK(m_weights.init(cfg));
-	return ErrorCode::NONE;
-}
-
-} // end namespace anki

+ 0 - 94
src/anki/renderer/Smaa.h

@@ -1,94 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#pragma once
-
-#include <anki/renderer/RenderingPass.h>
-
-namespace anki
-{
-
-/// @addtogroup renderer
-/// @{
-
-class SmaaEdge : public RenderingPass
-{
-anki_internal:
-	TexturePtr m_rt;
-
-	SmaaEdge(Renderer* r)
-		: RenderingPass(r)
-	{
-	}
-
-	~SmaaEdge();
-
-	ANKI_USE_RESULT Error init(const ConfigSet& initializer);
-
-	void setPreRunBarriers(RenderingContext& ctx);
-	void run(RenderingContext& ctx);
-	void setPostRunBarriers(RenderingContext& ctx);
-
-private:
-	FramebufferPtr m_fb;
-	ShaderResourcePtr m_vert;
-	ShaderResourcePtr m_frag;
-	ShaderProgramPtr m_prog;
-};
-
-class SmaaWeights : public RenderingPass
-{
-anki_internal:
-	TexturePtr m_rt;
-
-	SmaaWeights(Renderer* r)
-		: RenderingPass(r)
-	{
-	}
-
-	~SmaaWeights();
-
-	ANKI_USE_RESULT Error init(const ConfigSet& initializer);
-
-	void setPreRunBarriers(RenderingContext& ctx);
-	void run(RenderingContext& ctx);
-	void setPostRunBarriers(RenderingContext& ctx);
-
-private:
-	FramebufferPtr m_fb;
-	ShaderResourcePtr m_vert;
-	ShaderResourcePtr m_frag;
-	ShaderProgramPtr m_prog;
-	TexturePtr m_areaTex;
-	TexturePtr m_searchTex;
-};
-
-class Smaa : public RenderingPass
-{
-anki_internal:
-	SmaaEdge m_edge;
-	SmaaWeights m_weights;
-	CString m_qualityPerset;
-	TexturePtr m_stencilTex;
-
-	Smaa(Renderer* r)
-		: RenderingPass(r)
-		, m_edge(r)
-		, m_weights(r)
-	{
-	}
-
-	~Smaa()
-	{
-	}
-
-	ANKI_USE_RESULT Error init(const ConfigSet& cfg);
-
-private:
-	ANKI_USE_RESULT Error initInternal(const ConfigSet& cfg);
-};
-/// @}
-
-} // end namespace anki

+ 102 - 0
src/anki/renderer/Taa.cpp

@@ -0,0 +1,102 @@
+// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <anki/renderer/Taa.h>
+#include <anki/renderer/Renderer.h>
+#include <anki/renderer/Ms.h>
+#include <anki/renderer/Is.h>
+
+namespace anki
+{
+
+class TaaUniforms
+{
+public:
+	Mat4 m_prevViewProjMat;
+	Mat4 m_invViewProjMat;
+};
+
+Taa::~Taa()
+{
+}
+
+Error Taa::init(const ConfigSet& config)
+{
+	ANKI_R_LOGI("Initializing TAA");
+	Error err = initInternal(config);
+
+	if(err)
+	{
+		ANKI_R_LOGE("Failed to init TAA");
+	}
+
+	return ErrorCode::NONE;
+}
+
+Error Taa::initInternal(const ConfigSet& config)
+{
+	ANKI_CHECK(m_r->getResourceManager().loadResource("shaders/Taa.frag.glsl", m_frag));
+	m_r->createDrawQuadShaderProgram(m_frag->getGrShader(), m_prog);
+
+	for(U i = 0; i < 2; ++i)
+	{
+		m_rts[i] = m_r->createAndClearRenderTarget(m_r->create2DRenderTargetInitInfo(m_r->getWidth(),
+			m_r->getHeight(),
+			IS_COLOR_ATTACHMENT_PIXEL_FORMAT,
+			TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
+			SamplingFilter::LINEAR));
+
+		FramebufferInitInfo fbInit;
+		fbInit.m_colorAttachmentCount = 1;
+		fbInit.m_colorAttachments[0].m_texture = m_rts[i];
+		fbInit.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::DONT_CARE;
+		m_fbs[i] = getGrManager().newInstance<Framebuffer>(fbInit);
+	}
+
+	return ErrorCode::NONE;
+}
+
+void Taa::setPreRunBarriers(RenderingContext& ctx)
+{
+	ctx.m_commandBuffer->setTextureSurfaceBarrier(m_rts[m_r->getFrameCount() & 1],
+		TextureUsageBit::NONE,
+		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
+		TextureSurfaceInfo(0, 0, 0, 0));
+}
+
+void Taa::run(RenderingContext& ctx)
+{
+	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
+
+	cmdb->beginRenderPass(m_fbs[m_r->getFrameCount() & 1]);
+	cmdb->setViewport(0, 0, m_r->getWidth(), m_r->getHeight());
+
+	cmdb->bindShaderProgram(m_prog);
+	cmdb->bindTextureAndSampler(0, 0, m_r->getMs().m_depthRt, m_r->getNearestSampler());
+	cmdb->bindTextureAndSampler(0, 1, m_r->getIs().getRt(), m_r->getNearestSampler());
+	cmdb->bindTextureAndSampler(0, 2, m_rts[(m_r->getFrameCount() + 1) & 1], m_r->getLinearSampler());
+
+	TaaUniforms* unis = allocateAndBindUniforms<TaaUniforms*>(sizeof(TaaUniforms), cmdb, 0, 0);
+	unis->m_prevViewProjMat = ctx.m_jitterMat * ctx.m_prevViewProjMat;
+	unis->m_invViewProjMat = ctx.m_viewProjMatJitter.getInverse();
+
+	m_r->drawQuad(cmdb);
+	cmdb->endRenderPass();
+}
+
+void Taa::setPostRunBarriers(RenderingContext& ctx)
+{
+	ctx.m_commandBuffer->setTextureSurfaceBarrier(m_rts[m_r->getFrameCount() & 1],
+		TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
+		TextureUsageBit::SAMPLED_FRAGMENT,
+		TextureSurfaceInfo(0, 0, 0, 0));
+}
+
+TexturePtr Taa::getRt() const
+{
+	return m_rts[m_r->getFrameCount() & 1];
+}
+
+} // end namespace anki

+ 46 - 0
src/anki/renderer/Taa.h

@@ -0,0 +1,46 @@
+// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include <anki/renderer/RenderingPass.h>
+
+namespace anki
+{
+
+/// @addtogroup renderer
+/// @{
+
+/// Temporal AA resolve.
+class Taa : public RenderingPass
+{
+public:
+	Taa(Renderer* r)
+		: RenderingPass(r)
+	{
+	}
+
+	~Taa();
+
+	TexturePtr getRt() const;
+
+	ANKI_USE_RESULT Error init(const ConfigSet& cfg);
+
+	void setPreRunBarriers(RenderingContext& ctx);
+	void run(RenderingContext& ctx);
+	void setPostRunBarriers(RenderingContext& ctx);
+
+private:
+	Array<TexturePtr, 2> m_rts;
+	Array<FramebufferPtr, 2> m_fbs;
+
+	ShaderResourcePtr m_frag;
+	ShaderProgramPtr m_prog;
+
+	ANKI_USE_RESULT Error initInternal(const ConfigSet& cfg);
+};
+/// @}
+
+} // end namespace anki