#pragma parameter SB_RED_THRESHOLD "Smart Blur Red Threshold" 0.2 0.0 0.6 0.01
#pragma parameter SB_GREEN_THRESHOLD "Smart Blur Green Threshold" 0.2 0.0 0.6 0.01
#pragma parameter SB_BLUE_THRESHOLD "Smart Blur Blue Threshold" 0.2 0.0 0.6 0.01
#ifdef PARAMETER_UNIFORM
uniform float SB_RED_THRESHOLD;
uniform float SB_GREEN_THRESHOLD;
uniform float SB_BLUE_THRESHOLD;
#else
#define SB_RED_THRESHOLD 0.2
#define SB_GREEN_THRESHOLD 0.2
#define SB_BLUE_THRESHOLD 0.2
#endif
// END PARAMETERS //

/* COMPATIBILITY
   - HLSL compilers
   - Cg   compilers
*/

/*
   Hyllian Smart-Blur Shader
  
   Copyright (C) 2011-2016 Hyllian - sergiogdb@gmail.com

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.

*/

// Below the thresholds, blur is applied for each color channel.
// Threshold is the max color differency among the eight pixel neighbors from central pixel.

bool eq(float3 c1, float3 c2) {
    float3 df = abs(c1 - c2);
    return (df.r < SB_RED_THRESHOLD) && (df.g < SB_GREEN_THRESHOLD) && (df.b < SB_BLUE_THRESHOLD);
}


struct input
{
    float2 video_size;
    float2 texture_size;
    float2 output_size;
    float  frame_count;
    float  frame_direction;
    float  frame_rotation;
};

/*       
           A  B  C
           D  E  F 
           G  H  I
*/


struct out_vertex {
	float2 texCoord : TEXCOORD0;
	float4 t1       : TEXCOORD1;
	float4 t2       : TEXCOORD2;
	float4 t3       : TEXCOORD3;
};

/*    VERTEX_SHADER    */
out_vertex main_vertex
(
    float4 position    : POSITION,
    out float4 oPosition : POSITION,
    float2 tex      : TEXCOORD0,

    uniform float4x4 modelViewProj,
    uniform input IN
)
{
    float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
    float dx = ps.x;
    float dy = ps.y;

    oPosition = mul(modelViewProj, position);

    out_vertex OUT = {
        tex,
        tex.xxxy + float4(    -dx,    0.0,     dx,    -dy), //  A B C
        tex.xxxy + float4(    -dx,    0.0,     dx,    0.0), //  D E F
        tex.xxxy + float4(    -dx,    0.0,     dx,     dy)  //  G H I
    };


    return OUT;
}


float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
{
    float3   A = tex2D(s_p, VAR.t1.xw).xyz;
    float3   B = tex2D(s_p, VAR.t1.yw).xyz;
    float3   C = tex2D(s_p, VAR.t1.zw).xyz;
    float3   D = tex2D(s_p, VAR.t2.xw).xyz;
    float3   E = tex2D(s_p, VAR.t2.yw).xyz;
    float3   F = tex2D(s_p, VAR.t2.zw).xyz;
    float3   G = tex2D(s_p, VAR.t3.xw).xyz;
    float3   H = tex2D(s_p, VAR.t3.yw).xyz;
    float3   I = tex2D(s_p, VAR.t3.zw).xyz;

    float3 sum = 0.0;

    if (eq(E,F) && eq(E,H) && eq(E,I) && eq(E,B) && eq(E,C) && eq(E,A) && eq(E,D) && eq(E,G))
    {
        sum = (E+A+C+D+F+G+I+B+H)/9.0;
        E = sum;
    }

    return float4(E, 1.0);
}

