#pragma parameter XBR_SCALE "xBR Blend Level" 4.0 1.0 5.0 1.0
#ifdef PARAMETER_UNIFORM
uniform float XBR_SCALE;
#else
#define XBR_SCALE 4.0
#endif
// END PARAMETERS //

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*
   Hyllian's xBR LV2 Accuracy - pass1 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 round(X) floor((X)+0.5)

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


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

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

float4 remapFrom01(float4 v, float4 low, float4 high)
{
	return round(lerp(low, high, v));
}

float4 unpack_info(float i)
{
	float4 info;
	info.w = round(modf(i/2.0f, i));
	info.z = round(modf(i/2.0f, i));
	info.y = round(modf(i/2.0f, i));
	info.x = i;

	return info;
}

struct prev
{
	uniform float2 video_size;
	uniform float2 texture_size;
	uniform sampler2D texture;
};


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

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

   	uniform float4x4 modelViewProj,
	uniform input IN,

	out out_vertex co
)
{
	co.position = mul(modelViewProj, position);
	co.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

	co.texCoord = texCoord;
	co.t1 = texCoord.xyyy + float4(   0, -dy,  0, dy);  //  B
	co.t2 = texCoord.xxxy + float4( -dx,   0, dx,  0);  //D E F
                                                            //  H
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex co, uniform sampler2D decal : TEXUNIT0, prev PASSPREV2, uniform input IN) : COLOR
{

	bool4 px; // px = pixel, edr = edge detection rule
	float4 edri, edr, edr_left, edr_up;
	float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
	float3 res1, res2;

	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(co.texCoord*IN.texture_size);

	float3 B  = tex2D(PASSPREV2.texture, co.t1.xy).rgb;
	float3 D  = tex2D(PASSPREV2.texture, co.t2.xw).rgb;
	float3 E  = tex2D(PASSPREV2.texture, co.t2.yw).rgb;
	float3 F  = tex2D(PASSPREV2.texture, co.t2.zw).rgb;
	float3 H  = tex2D(PASSPREV2.texture, co.t1.xw).rgb;


	float4x3 b  = float4x3(B, D, H, F);
	float4x3 e  = float4x3(E, E, E, E);
	float4x3 d  = float4x3(D, H, F, B);
	float4x3 f  = float4x3(F, B, D, H);
	float4x3 h  = float4x3(H, F, B, D);

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

	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 info  = tex2D(decal, co.texCoord);

	float4 i = remapFrom01(info, float4(0.0f, 0.0f, 0.0f, 0.0f), float4(255.0f, 255.0f, 255.0f, 255.0f));

	edr      = unpack_info(i.x);
	edr_left = unpack_info(i.y);
	edr_up   = unpack_info(i.z);
	edri     = unpack_info(i.w);

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

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

	float4 maximos = max(max(fx30, fx60), max(fx45, fx45i));

	res1 = E;
	res1 = lerp(res1, lerp(H, F, px.x), maximos.x);
	res1 = lerp(res1, lerp(B, D, px.z), maximos.z);
	
	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(df1(E, res1), df1(E, res2)));

	return float4(res, 1.0);
}


