/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*

   Hyllian's xBR MultiLevel4 Shader - Pass1
   
   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.

*/

#define round(X) floor((X)+0.5)
#define TEX(dx,dy) tex2D(decal, VAR.texCoord+float2((dx),(dy))*VAR.t1).rgb

const static float cf2             = 2.0;
const static float cf3             = 4.0;
const static float cf4             = 4.0;
const static float4 eq_thresholdold= float4(15.0, 15.0, 15.0, 15.0);
const static float4 eq_threshold   = float4( 2.0,  2.0,  2.0,  2.0);
const static float4 eq_threshold3  = float4(25.0, 25.0, 25.0, 25.0);
const static float3 Yuv_weight     = float3(4.0, 1.0, 2.0);
const static float3x3 yuv          = float3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
const static float3x3 yuvT         = float3x3(0.299, -0.169,  0.499,
                                              0.587, -0.331, -0.418,
                                              0.114,  0.499, -0.0813); 
const static float3x3 yuv_weighted = float3x3(Yuv_weight.x*yuv[0], Yuv_weight.y*yuv[1], Yuv_weight.z*yuv[2]);
const static float4 maximo         = float4(255.0f, 255.0f, 255.0f, 255.0f);


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

bool4 rd(float4 A, float4 B, float4 C, float4 D)
{
    return ((df(C,D)/(df(A,B)+0.000000001)) > 2.0);
}

bool4 id(float4 A, float4 B, float4 C, float4 D)
{
    return (df(C,D) > df(A,B));
}


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

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

bool4 eq3(float4 A, float4 B)
{
	return (df(A, B) < eq_threshold3);
}

float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 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;
	float2 t1;
};

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

	//      A3 B3 C3
	//      A1 B1 C1
	//A2 A0  A  B  C C4 C6
	//D2 D0  D  E  F F4 F6
	//G2 G0  G  H  I I4 I6
	//      G5 H5 I5
	//      G7 H7 I7

	OUT.texCoord = texCoord;
	OUT.t1       = float2(dx, dy);  // F  H

	return OUT;
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{
	bool4 edr0, edr, edr_left, edr_up, edr3_left, edr3_up, edr4_left, edr4_up; // edr = edge detection rule
	bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
	bool4 interp_restriction_lv3_left, interp_restriction_lv3_up;
	bool4 interp_restriction_lv4_left, interp_restriction_lv4_up;

	float3 A3=TEX(-1,-3);	float3 B3=TEX( 0,-3);	float3 C3=TEX( 1,-3);	
	float3 A1=TEX(-1,-2);	float3 B1=TEX( 0,-2);	float3 C1=TEX( 1,-2);
	float3 A2=TEX(-3,-1);	float3 A0=TEX(-2,-1);	float3 A =TEX(-1,-1);
	float3 B =TEX( 0,-1);	float3 C =TEX( 1,-1);	float3 C4=TEX( 2,-1);	float3 C6=TEX( 3,-1);
	float3 D2=TEX(-3, 0);	float3 D0=TEX(-2, 0);	float3 D =TEX(-1, 0);	float3 E =TEX( 0, 0);
	float3 F =TEX( 1, 0);	float3 F4=TEX( 2, 0);	float3 F6=TEX( 3, 0);
	float3 G2=TEX(-3, 1);	float3 G0=TEX(-2, 1);	float3 G =TEX(-1, 1);	float3 H =TEX( 0, 1);
	float3 I =TEX( 1, 1);	float3 I4=TEX( 2, 1);	float3 I6=TEX( 3, 1);
	float3 G5=TEX(-1, 2);	float3 H5=TEX( 0, 2);	float3 I5=TEX( 1, 2);
	float3 G7=TEX(-1, 3);	float3 H7=TEX( 0, 3);	float3 I7=TEX( 1, 3);

	float4x3 bdhf = mul( float4x3(B, D, H, F), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 b = mul( bdhf,  Yuv_weight);

	bdhf = mul( float4x3(C, A, G, I), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 c = mul( bdhf,  Yuv_weight);

	bdhf = mul( float4x3(E, E, E, E), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 e = mul( bdhf,  Yuv_weight);

	float4 d = b.yzwx;
	float4 f = b.wxyz;
	float4 g = c.zwxy;
	float4 h = b.zwxy;
	float4 i = c.wxyz;

	bdhf = mul( float4x3(I4, C1, A0, G5), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 i4 = mul( bdhf,  Yuv_weight);

	bdhf = mul( float4x3(I5, C4, A1, G0), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 i5 = mul( bdhf,  Yuv_weight);

	bdhf = mul( float4x3(H5, F4, B1, D0), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 h5 = mul( bdhf,  Yuv_weight);

	float4 f4 = h5.yzwx;

	float4 c1 = i4.yzwx;
	float4 g0 = i5.wxyz;
	float4 b1 = h5.zwxy;
	float4 d0 = h5.wxyz;

	bdhf = mul( float4x3(I6, C3, A2, G7), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 i6 = mul( bdhf,  Yuv_weight);

	bdhf = mul( float4x3(I7, C6, A3, G2), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 i7 = mul( bdhf,  Yuv_weight);

	bdhf = mul( float4x3(H7, F6, B3, D2), yuvT );
	bdhf = float4x3(abs(bdhf[0]), abs(bdhf[1]), abs(bdhf[2]), abs(bdhf[3]));
	float4 h7 = mul( bdhf,  Yuv_weight);

	float4 f6 = h7.yzwx;

	float4 c3 = i6.yzwx;
	float4 g2 = i7.wxyz;
	float4 b3 = h7.zwxy;
	float4 d2 = h7.wxyz;


	interp_restriction_lv1      = (e!=f  &&  e!=h);
	interp_restriction_lv2_left = (e!=g  &&  d!=g  && (eq(e, d) || eq(h, g)));
	interp_restriction_lv2_up   = (e!=c  &&  b!=c  && (eq(e, b) || eq(f, c)));
	interp_restriction_lv3_left = (e!=g0 && d0!=g0 && (eq(d,d0) || eq(g,g0)));
	interp_restriction_lv3_up   = (e!=c1 && b1!=c1 && (eq(b,b1) || eq(c,c1)));
	interp_restriction_lv4_left = (e!=g2 && d2!=g2 && (eq(d0,d2) || eq(g0,g2)));
	interp_restriction_lv4_up   = (e!=c3 && b3!=c3 && (eq(b1,b3) || eq(c1,c3)));

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

	edr0      = (wd1 <= wd2) && interp_restriction_lv1;
	edr       = (wd1 <  wd2) && interp_restriction_lv1 && ( !eq(f,b) && !id(f,c,f,b) || !eq(h,d) && !id(h,g,h,d) || eq(e,g) || eq(e,c) );
	edr_left  = ((cf2*df(f,g))  <= df(h,c))   && interp_restriction_lv2_left && edr;
	edr_up    = (df(f,g)  >= (cf2*df(h,c)))   && interp_restriction_lv2_up   && edr;
	edr3_left = ((cf3*df(f,g0)) <= df(h,c1)) && interp_restriction_lv3_left && edr_left;
	edr3_up   = (df(f,g0) >= (cf3*df(h,c1))) && interp_restriction_lv3_up   && edr_up;
	edr4_left = ((cf4*df(f,g2)) <= df(h,c3)) && interp_restriction_lv4_left && edr3_left;
	edr4_up   = (df(f,g2) >= (cf4*df(h,c3))) && interp_restriction_lv4_up   && edr3_up;


	float4 info;


info= (edr4_left && !edr4_up) ? float4(8.0) : ((edr4_up && !edr4_left) ? float4(7.0) : ((edr3_left && !edr3_up) ? float4(6.0) : ((edr3_up && !edr3_left) ? float4(5.0) : ((edr_left && !edr_up) ? float4(4.0) : ((edr_up && !edr_left) ? float4(3.0) : (edr ? float4(2.0) : (edr0 ? float4(1.0) : float4(0.0))))))));

	return float4(remapTo01(info, maximo));

}

