/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*
   Hyllian's 2xBR level 3 pass0 Shader
   
   Copyright (C) 2011/2013 Hyllian/Jararaca - sergiogdb@gmail.com

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


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

const static float coef            = 2.0;
const static float cf              = 4.0;
const static float4 eq_threshold   = float4(15.0, 15.0, 15.0, 15.0);
const static float y_weight        = 48.0;
const static float u_weight        = 7.0;
const static float v_weight        = 6.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 yuv_weighted = float3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
const static float4 bin1           = float4( 1.0f,  2.0f,  4.0f,   8.0f);
const static float4 bin2           = float4(16.0f, 32.0f, 64.0f, 128.0f);
const static float4 maximo         = float4(255.0f, 255.0f, 255.0f, 255.0f);

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

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

float4 remapFrom01(float4 v, float4 high)
{
	return (high*v + float4(0.5));
}

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

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


/*
0   0 0 0 0  0     0/24
1   0 0 0 1  1/8   3/24 0001   1
2   0 0 1 0  1/6   4/24 0010   2
3   0 0 1 1  1/4   6/24 0011   3

4   0 1 0 0  3/8   9/24 0100   4
5   0 1 0 1  1/2  12/24 0101   5
6   0 1 1 0  5/8  15/24 1100  12 
7   0 1 1 1  3/4  18/24 1011  11

8   1 0 0 0  5/6  20/24 1010  10
9   1 0 0 1  7/8  21/24 1001   9
*/


#define FILTRO(EDR, LEFT, UP, LEFT3, UP3,    PX0, PX3, PX1,    PCT0, PCT3, PCT1,    PX, PW, PY)\
	if (LEFT && (!UP))\
	{\
		PX0  = bool2(false,   PX);\
		PX3  = bool2(   PY, true);\
		if (LEFT3)\
		{\
			PCT0 = bool4( true, false, false, false);\
			PCT3 = bool4(false,  true, false,  true);\
		}\
		else \
		{\
			PCT0 = bool4(false,  true,  true,  true);\
			PCT3 = bool4(false, false,  true,  true);\
		}\
	}\
	else if (UP && (!LEFT))\
	{\
		PX0  = bool2(false,    PX);\
		PX1  = bool2(  !PW, false);\
		if (UP3)\
		{\
			PCT0 = bool4( true, false, false, false);\
			PCT1 = bool4(false,  true, false,  true);\
		}\
		else \
		{\
			PCT0 = bool4(false,  true,  true,  true);\
			PCT1 = bool4(false, false,  true,  true);\
		}\
	}\
	else if (EDR)\
	{\
		LEFT = UP = LEFT3 = UP3 = false;\
		PX0  = bool2(false, PX);\
		PCT0 = bool4(false,  true, false,  true);\
	}\




struct input
{
	float2 video_size;
	float2 texture_size;
	float2 output_size;
};


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 edr, edr_left, edr_up, edr3_left, edr3_up, px; // px = pixel, 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;
	float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines.
	float3 res1, res2, pix1, pix2;
	bool2 px0, px1, px2, px3;
	bool4 pct0, pct1, pct2, pct3;


	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), yuv_weighted[0] );
	float4 c = mul( float4x3(C, A, G, I), yuv_weighted[0] );
	float4 e = mul( float4x3(E, E, E, E), yuv_weighted[0] );
	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), yuv_weighted[0] );
	float4 i5 = mul( float4x3(I5, C4, A1, G0), yuv_weighted[0] );
	float4 h5 = mul( float4x3(H5, F4, B1, D0), yuv_weighted[0] );
	float4 f4 = h5.yzwx;

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


	interp_restriction_lv1      = (e!=f  &&  e!=h &&  ( !eq(f,b) && !eq(h,d) || eq(e,i) && !eq(f,i4) && !eq(h,i5) || eq(e,g) || eq(e,c) ) );
	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)));

	edr       = (weighted_distance( e, c, g, i, h5, f4, h, f) < weighted_distance( h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;

	edr_left  = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left && edr;
	edr_up    = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up   && edr;
	edr3_left = ((cf*df(f,g0)) <= df(h,c1)) && interp_restriction_lv3_left && edr_left;
	edr3_up   = (df(f,g0) >= (cf*df(h,c1))) && interp_restriction_lv3_up   && edr_up;

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

	pct0 = pct1 = pct2 = pct3 = bool4(false, false, false, false);

	FILTRO(edr.x, edr_left.x, edr_up.x, edr3_left.x, edr3_up.x, px0, px3, px1, pct0, pct3, pct1, px.x, px.w, px.y);
	FILTRO(edr.y, edr_left.y, edr_up.y, edr3_left.y, edr3_up.y, px1, px0, px2, pct1, pct0, pct2, px.y, px.x, px.z);
	FILTRO(edr.z, edr_left.z, edr_up.z, edr3_left.z, edr3_up.z, px2, px1, px3, pct2, pct1, pct3, px.z, px.y, px.w);
	FILTRO(edr.w, edr_left.w, edr_up.w, edr3_left.w, edr3_up.w, px3, px2, px0, pct3, pct2, pct0, px.w, px.z, px.x);

	float4 info = mul(
                          bin1, float4x4(
                                          edr3_left,
                                          edr3_up,
                                          px0.x,  px1.x,  px2.x,  px3.x,
                                          px0.y,  px1.y,  px2.y,  px3.y
                                        )
                         );

	info +=       mul(bin2, float4x4(
                                         pct0.x, pct1.x, pct2.x, pct3.x,
                                         pct0.y, pct1.y, pct2.y, pct3.y,
                                         pct0.z, pct1.z, pct2.z, pct3.z,
                                         pct0.w, pct1.w, pct2.w, pct3.w
                                        )
                         );


	return float4(remapTo01(info, maximo));
}

