#pragma parameter XBR_EQ_THRESHOLD "Eq Threshold" 4.0 0.0 5.0 0.2
#pragma parameter XBR_LV2_COEFFICIENT "Lv2 Coefficient" 2.0 1.0 3.0 0.1
#ifdef PARAMETER_UNIFORM
uniform float XBR_EQ_THRESHOLD;
uniform float XBR_LV2_COEFFICIENT;
#else
#define XBR_EQ_THRESHOLD 4.0
#define XBR_LV2_COEFFICIENT 2.0
#endif
// END PARAMETERS //

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*
   Hyllian's xBR LV2 Accuracy - pass0 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.

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

#define XBR_RED_COEF 17.0
#define XBR_GREEN_COEF 20.0
#define XBR_BLUE_COEF 3.0

#define coef  XBR_LV2_COEFFICIENT

const static float3 dtt = float3(65536,256,1);

float4 remapTo01(float4 v, float4 low, float4 high)
{
	return saturate((v - low)/(high-low));
}

float df1(float3 A, float3 B)
{
	float rmean = (A.r+B.r)/2.0;
	float3 diff = A - B;
	float3 K = float3(XBR_RED_COEF+rmean, XBR_GREEN_COEF, XBR_BLUE_COEF-rmean);

	return sqrt(dot(K*diff, diff));
}

float4 df(float4x3 A, float4x3 B)
{
	return float4(df1(A[0],B[0]), df1(A[1],B[1]), df1(A[2],B[2]), df1(A[3],B[3]));
}

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

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


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


struct out_vertex {
	float4 position : POSITION;
	float4 color    : COLOR;
	float2 texCoord : TEXCOORD0;
	float4 t1       : TEXCOORD1;
	float4 t2       : TEXCOORD2;
	float4 t3       : TEXCOORD3;
	float4 t4       : TEXCOORD4;
	float4 t5       : TEXCOORD5;
	float4 t6       : TEXCOORD6;
	float4 t7       : TEXCOORD7;
};

/*    VERTEX_SHADER    */
out_vertex main_vertex
(
	float4 position	: POSITION,
	float4 color	: COLOR,
	float2 texCoord : TEXCOORD0,

   	uniform float4x4 modelViewProj,
	uniform input IN
)
{
	out_vertex OUT;

	OUT.position = mul(modelViewProj, position);
	OUT.color = color;

	float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.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,-2.0*dy); // A1 B1 C1
	OUT.t2 = texCoord.xxxy + float4( -dx, 0, dx,    -dy); //  A  B  C
	OUT.t3 = texCoord.xxxy + float4( -dx, 0, dx,      0); //  D  E  F
	OUT.t4 = texCoord.xxxy + float4( -dx, 0, dx,     dy); //  G  H  I
	OUT.t5 = texCoord.xxxy + float4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
	OUT.t6 = texCoord.xyyy + float4(-2.0*dx,-dy, 0,  dy); // A0 D0 G0
	OUT.t7 = texCoord.xyyy + float4( 2.0*dx,-dy, 0,  dy); // C4 F4 I4

	return OUT;
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{
	bool4 edri, edr, edr_left, edr_up;
	bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;

	float3 A1 = tex2D(decal, VAR.t1.xw).rgb;
	float3 B1 = tex2D(decal, VAR.t1.yw).rgb;
	float3 C1 = tex2D(decal, VAR.t1.zw).rgb;

	float3 A  = tex2D(decal, VAR.t2.xw).rgb;
	float3 B  = tex2D(decal, VAR.t2.yw).rgb;
	float3 C  = tex2D(decal, VAR.t2.zw).rgb;

	float3 D  = tex2D(decal, VAR.t3.xw).rgb;
	float3 E  = tex2D(decal, VAR.t3.yw).rgb;
	float3 F  = tex2D(decal, VAR.t3.zw).rgb;

	float3 G  = tex2D(decal, VAR.t4.xw).rgb;
	float3 H  = tex2D(decal, VAR.t4.yw).rgb;
	float3 I  = tex2D(decal, VAR.t4.zw).rgb;

	float3 G5 = tex2D(decal, VAR.t5.xw).rgb;
	float3 H5 = tex2D(decal, VAR.t5.yw).rgb;
	float3 I5 = tex2D(decal, VAR.t5.zw).rgb;

	float3 A0 = tex2D(decal, VAR.t6.xy).rgb;
	float3 D0 = tex2D(decal, VAR.t6.xz).rgb;
	float3 G0 = tex2D(decal, VAR.t6.xw).rgb;

	float3 C4 = tex2D(decal, VAR.t7.xy).rgb;
	float3 F4 = tex2D(decal, VAR.t7.xz).rgb;
	float3 I4 = tex2D(decal, VAR.t7.xw).rgb;

	float4 b_ = mul( float4x3(B, D, H, F), dtt );
	float4 c_ = mul( float4x3(C, A, G, I), dtt );
	float4 e_ = mul( float4x3(E, E, E, E), dtt );
	float4 d_ = b_.yzwx;
	float4 f_ = b_.wxyz;
	float4 g_ = c_.zwxy;
	float4 h_ = b_.zwxy;
	float4 i_ = c_.wxyz;

	float4 i4_ = mul( float4x3(I4, C1, A0, G5), dtt );
	float4 i5_ = mul( float4x3(I5, C4, A1, G0), dtt );
	float4 h5_ = mul( float4x3(H5, F4, B1, D0), dtt );
	float4 f4_ = h5_.yzwx;

	float4x3 b  = float4x3(B, D, H, F);
	float4x3 c  = float4x3(C, A, G, I);
	float4x3 e  = float4x3(E, E, E, E);
	float4x3 d  = float4x3(D, H, F, B);
	float4x3 f  = float4x3(F, B, D, H);
	float4x3 g  = float4x3(G, I, C, A);
	float4x3 h  = float4x3(H, F, B, D);
	float4x3 i  = float4x3(I, C, A, G);

	float4x3 i4  = float4x3(I4, C1, A0, G5);
	float4x3 i5  = float4x3(I5, C4, A1, G0);
	float4x3 h5  = float4x3(H5, F4, B1, D0);
	float4x3 f4  = float4x3(F4, B1, D0, H5);

	interp_restriction_lv1      = ((e_!=f_) && (e_!=h_));
	interp_restriction_lv2_left = ((e_!=g_) && (d_!=g_));
	interp_restriction_lv2_up   = ((e_!=c_) && (b_!=c_));

	float4 wd1 = weighted_distance( e, c, g, i, h5, f4, h, f);
	float4 wd2 = weighted_distance( h, d, i5, f, i4, b, e, i);

	edri     = (wd1 <= wd2) && interp_restriction_lv1;
	edr      = (wd1 <  wd2) && (!edri.yzwx || !edri.wxyz) && interp_restriction_lv1;
	edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left && edr && (!edri.yzwx && eq(e,c));
	edr_up   = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up   && edr && (!edri.wxyz && eq(e,g));

	float4 info;

	info.x = dot(float4(edr     ), float4(8.0f, 4.0f, 2.0f, 1.0f));
	info.y = dot(float4(edr_left), float4(8.0f, 4.0f, 2.0f, 1.0f));
	info.z = dot(float4(edr_up  ), float4(8.0f, 4.0f, 2.0f, 1.0f));
	info.w = dot(float4(edri    ), float4(8.0f, 4.0f, 2.0f, 1.0f));

	return remapTo01(info, float4(0.0f, 0.0f, 0.0f, 0.0f), float4(255.0f, 255.0f, 255.0f, 255.0f));
}
