#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.

*/

#include "../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

// 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);
}

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

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
	float4 t1       : TEXCOORD1;
	float4 t2       : TEXCOORD2;
	float4 t3       : TEXCOORD3;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

/*    VERTEX_SHADER    */
out_vertex main_vertex(COMPAT_IN_VERTEX)
{
	out_vertex OUT;
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
#else
	OUT.Color = color;
#endif
    float2 ps = float2(1.0/COMPAT_texture_size.x, 1.0/COMPAT_texture_size.y);
    float dx = ps.x;
    float dy = ps.y;

    OUT.position = mul(modelViewProj, position);

    OUT.texCoord = texCoord;
    OUT.t1 = texCoord.xxxy + float4(    -dx,    0.0,     dx,    -dy); //  A B C
    OUT.t2 = texCoord.xxxy + float4(    -dx,    0.0,     dx,    0.0); //  D E F
    OUT.t3 = texCoord.xxxy + float4(    -dx,    0.0,     dx,     dy);  //  G H I
	
    return OUT;
}

float4 smart_blur(float2 texture_size, float2 video_size, float2 output_size, COMPAT_Texture2D(decal),
	float4 t1, float4 t2, float4 t3)
{
    float3   A = COMPAT_SamplePoint(decal, t1.xw).xyz;
    float3   B = COMPAT_SamplePoint(decal, t1.yw).xyz;
    float3   C = COMPAT_SamplePoint(decal, t1.zw).xyz;
    float3   D = COMPAT_SamplePoint(decal, t2.xw).xyz;
    float3   E = COMPAT_SamplePoint(decal, t2.yw).xyz;
    float3   F = COMPAT_SamplePoint(decal, t2.zw).xyz;
    float3   G = COMPAT_SamplePoint(decal, t3.xw).xyz;
    float3   H = COMPAT_SamplePoint(decal, t3.yw).xyz;
    float3   I = COMPAT_SamplePoint(decal, 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);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return smart_blur(COMPAT_texture_size, COMPAT_video_size, COMPAT_output_size, decal,
		VOUT.t1, VOUT.t2, VOUT.t3);
}
COMPAT_END