#version 410 compatibility
#define SMAA_PIXEL_SIZE vec2(1.0 / 512.0, 1.0 / 512.0)

#define SMAA_THRESHOLD 0.05
#define SMAA_MAX_SEARCH_STEPS 32
#define SMAA_MAX_SEARCH_STEPS_DIAG 16
#define SMAA_CORNER_ROUNDING 25


#ifndef SMAA_DEPTH_THRESHOLD
#define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD)
#endif


#ifndef SMAA_REPROJECTION
#define SMAA_REPROJECTION 0
#endif

#define SMAA_REPROJECTION_WEIGHT_SCALE 30.0


#ifndef SMAA_AREATEX_MAX_DISTANCE
#define SMAA_AREATEX_MAX_DISTANCE 16
#endif
#ifndef SMAA_AREATEX_MAX_DISTANCE_DIAG
#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
#endif
#define SMAA_AREATEX_PIXEL_SIZE (1.0 / vec2(160.0, 560.0))
#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)


/* --- Define section is over ---- */

vec4 SMAANeighborhoodBlendingPS(vec2 texcoord,
                                  vec4 offset[2],
                                  sampler2D colorTex,
                                  sampler2D blendTex) {
    // Fetch the blending weights for current pixel:
    vec4 a;
    a.xz = texture(blendTex, texcoord).xz;
    a.y = texture(blendTex, offset[1].zw).g;
    a.w = texture(blendTex, offset[1].xy).a;

    // Is there any blending weight with a value greater than 0.0?
    //SMAA_BRANCH
    if (dot(a, vec4(1.0, 1.0, 1.0, 1.0)) < 1e-5)
        return textureLod(colorTex, texcoord, 0.0);
    else {
        vec4 color = vec4(0.0, 0.0, 0.0, 0.0);

        // Up to 4 lines can be crossing a pixel (one through each edge). We
        // favor blending by choosing the line with the maximum weight for each
        // direction:
        vec2 offset;
        offset.x = a.a > a.b? a.a : -a.b; // left vs. right 
        offset.y = a.g > a.r? a.g : -a.r; // top vs. bottom

        // Then we go in the direction that has the maximum weight:
        if (abs(offset.x) > abs(offset.y)) // horizontal vs. vertical
            offset.y = 0.0;
        else
            offset.x = 0.0;

        #if SMAA_REPROJECTION == 1
        // Fetch the opposite color and lerp by hand:
        vec4 C = textureLod(colorTex, texcoord, 0.0);
        texcoord += sign(offset) * SMAA_PIXEL_SIZE;
        vec4 Cop = textureLod(colorTex, texcoord, 0.0);
        float s = abs(offset.x) > abs(offset.y)? abs(offset.x) : abs(offset.y);

        // Unpack the velocity values:
        C.a *= C.a;
        Cop.a *= Cop.a;

        // Lerp the colors:
        vec4 Caa = mix(C, Cop, s);

        // Unpack velocity and return the resulting value:
        Caa.a = sqrt(Caa.a);
        return Caa;
        #else
        // Fetch the opposite color and lerp by hand:
        vec4 C = textureLod(colorTex, texcoord, 0.0);
        texcoord += sign(offset) * SMAA_PIXEL_SIZE;
        vec4 Cop = textureLod(colorTex, texcoord, 0.0);
        float s = abs(offset.x) > abs(offset.y)? abs(offset.x) : abs(offset.y);
        return mix(C, Cop, s);
        #endif
    }
}

/* ------------- Header is over -------------- */

uniform sampler2D albedo_tex;
uniform sampler2D blend_tex;
in vec2 texcoord;
in vec4 offset[2];
in vec4 dummy2;
void main()
{
  gl_FragColor = SMAANeighborhoodBlendingPS(texcoord, offset, albedo_tex, blend_tex);
}