#pragma parameter XBR_SCALE "xBR Scale" 3.0 1.0 5.0 1.0
#pragma parameter XBR_Y_WEIGHT "Y Weight" 48.0 0.0 100.0 1.0
#pragma parameter XBR_EQ_THRESHOLD "Eq Threshold" 15.0 0.0 50.0 1.0
#pragma parameter XBR_LV2_COEFFICIENT "Lv2 Coefficient" 2.0 1.0 3.0 0.1
#ifdef PARAMETER_UNIFORM
uniform float XBR_SCALE;
uniform float XBR_Y_WEIGHT;
uniform float XBR_EQ_THRESHOLD;
uniform float XBR_LV2_COEFFICIENT;
#else
#define XBR_SCALE 3.0
#define XBR_Y_WEIGHT 48.0
#define XBR_EQ_THRESHOLD 15.0
#define XBR_LV2_COEFFICIENT 2.0
#endif
// END PARAMETERS //

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/


/*
   Hyllian's xBR-lv2-lq Shader
   
   Copyright (C) 2011/2015 Hyllian/Jararaca - sergiogdb@gmail.com

   Copyright (C) 2011-2015 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.


   Incorporates some of the ideas from SABR shader. Thanks to Joshua Street.
*/


#include "../../compat_includes.inc"

uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

// Uncomment just one of the three params below to choose the corner detection
//#define CORNER_A
#define CORNER_C

#ifdef CORNER_C
  #define SMOOTH_TIPS
#endif

const static float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
const static float4 Bo = float4( 1.0,  1.0, -1.0,-1.0 );
const static float4 Co = float4( 1.5,  0.5, -0.5, 0.5 );
const static float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
const static float4 Bx = float4( 0.5,  2.0, -0.5,-2.0 );
const static float4 Cx = float4( 1.0,  1.0, -0.5, 0.0 );
const static float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
const static float4 By = float4( 2.0,  0.5, -2.0,-0.5 );
const static float4 Cy = float4( 2.0,  0.0, -1.0, 0.5 );
const static float4 Ci = float4(0.25, 0.25, 0.25, 0.25);

const static float3 Y          = float3(0.2126, 0.7152, 0.0722);

float4 df(float4 A, float4 B)
{
	return float4(abs(A-B));
}

float c_df(float3 c1, float3 c2) 
{
        float3 df = abs(c1 - c2);
        return df.r + df.g + df.b;
}

bool4 eq(float4 A, float4 B)
{
	return (df(A, B) < float4(XBR_EQ_THRESHOLD, XBR_EQ_THRESHOLD, XBR_EQ_THRESHOLD, XBR_EQ_THRESHOLD));
}

float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
{
	return (df(c,d) + df(e,f) + 3.0*df(g,h));
//	return (1.0*df(a,b) + 2.0*df(c,d) + 2.0*df(e,f) + 4.0*df(g,h));
}

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

	OUT.position = mul(modelViewProj, position);

	float2 ps = float2(1.0/COMPAT_texture_size.x, 1.0/COMPAT_texture_size.y);
	float dx = ps.x;
	float dy = ps.y;

	//    A1 B1 C1
	// A0  A  B  C C4
	// D0  D  E  F F4
	// G0  G  H  I I4
	//    G5 H5 I5

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

	return OUT;
}


/*    FRAGMENT SHADER    */
float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	bool4 edri, edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
	bool4 interp_restriction_lv0, interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
	float4 fx, fx_left, fx_up; // inequations of straight lines.

	float4 delta         = float4(1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE);
	float4 deltaL        = float4(0.5/XBR_SCALE, 1.0/XBR_SCALE, 0.5/XBR_SCALE, 1.0/XBR_SCALE);
	float4 deltaU        = deltaL.yxwz;

	float2 fp = frac(VOUT.texCoord*COMPAT_texture_size);

	float3 A  = COMPAT_SamplePoint(decal, VOUT.t1.xw).rgb;
	float3 B  = COMPAT_SamplePoint(decal, VOUT.t1.yw).rgb;
	float3 C  = COMPAT_SamplePoint(decal, VOUT.t1.zw).rgb;

	float3 D  = COMPAT_SamplePoint(decal, VOUT.t2.xw).rgb;
	float3 E  = COMPAT_SamplePoint(decal, VOUT.t2.yw).rgb;
	float3 F  = COMPAT_SamplePoint(decal, VOUT.t2.zw).rgb;

	float3 G  = COMPAT_SamplePoint(decal, VOUT.t3.xw).rgb;
	float3 H  = COMPAT_SamplePoint(decal, VOUT.t3.yw).rgb;
	float3 I  = COMPAT_SamplePoint(decal, VOUT.t3.zw).rgb;


	float4 b = mul( float4x3(B, D, H, F), XBR_Y_WEIGHT*Y );
	float4 c = mul( float4x3(C, A, G, I), XBR_Y_WEIGHT*Y );
	float4 e = mul( float4x3(E, E, E, E), XBR_Y_WEIGHT*Y );
	float4 a = c.yzwx;
	float4 d = b.yzwx;
	float4 f = b.wxyz;
	float4 g = c.zwxy;
	float4 h = b.zwxy;
	float4 i = c.wxyz;

	// These inequations define the line below which interpolation occurs.
	fx      = (Ao*fp.y+Bo*fp.x); 
	fx_left = (Ax*fp.y+Bx*fp.x);
	fx_up   = (Ay*fp.y+By*fp.x);

        interp_restriction_lv1 = interp_restriction_lv0 = ((e!=f) && (e!=h));

#ifndef CORNER_A
	interp_restriction_lv1 = ( interp_restriction_lv0  && ( !eq(f,b) && !eq(f,c) || !eq(h,d) && !eq(h,g) || eq(e,g) || eq(e,c)) );
#endif

	interp_restriction_lv2_left = ((e!=g) && (d!=g));
	interp_restriction_lv2_up   = ((e!=c) && (b!=c));

	float4 fx45i = saturate((fx      + delta  -Co - Ci)/(2*delta ));
	float4 fx45  = saturate((fx      + delta  -Co     )/(2*delta ));
	float4 fx30  = saturate((fx_left + deltaL -Cx     )/(2*deltaL));
	float4 fx60  = saturate((fx_up   + deltaU -Cy     )/(2*deltaU));

	float4 wd1 = weighted_distance( d, b, g, e, e, c, h, f);
	float4 wd2 = weighted_distance( a, e, b, f, d, h, e, i);

        edri     = (wd1 <= wd2) && interp_restriction_lv0;
	edr      = (wd1 <  wd2) && interp_restriction_lv1;
	edr_left = ((XBR_LV2_COEFFICIENT*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left && edr;
	edr_up   = (df(f,g) >= (XBR_LV2_COEFFICIENT*df(h,c))) && interp_restriction_lv2_up && edr;

	fx45  = edr*fx45;
	fx30  = edr_left*fx30;
	fx60  = edr_up*fx60;
	fx45i = edri*fx45i;

	px = (df(e,f) <= df(e,h));

#ifdef SMOOTH_TIPS
	float4 maximos = max(max(fx30, fx60), max(fx45, fx45i));
#else
	float4 maximos = max(max(fx30, fx60), fx45);
#endif

	float3 res1 = E;
	res1 = lerp(res1, lerp(H, F, px.x), maximos.x);
	res1 = lerp(res1, lerp(B, D, px.z), maximos.z);
	
	float3 res2 = E;
	res2 = lerp(res2, lerp(F, B, px.y), maximos.y);
	res2 = lerp(res2, lerp(D, H, px.w), maximos.w);
	
	float3 res = lerp(res1, res2, step(c_df(E, res1), c_df(E, res2)));

	return float4(res, 1.0);
}

COMPAT_END