/*
   Hyllian's 4xBR v3.5 + CRT-caligari Shader
   
   Copyright (C) 2011 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.

*/

const static float coef           = 2.0;
const static float3 dtt           = float3(65536,255,1);
const static half y_weight        = 48.0;
const static half u_weight        = 7.0;
const static half v_weight        = 6.0;
const static half3x3 yuv          = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);

//const static half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);


        // Constants used with gamma correction.
        #define InputGamma 2.4
        #define OutputGamma 2.2

        #define GAMMA_IN(color)     pow(color, float3(InputGamma))
        #define GAMMA_OUT(color)    pow(color, float3(1.0 / OutputGamma))

	#define TEX2D(coords)	GAMMA_IN( tex2D(decal, coords).xyz )

        // 0.5 = the spot stays inside the original pixel
        // 1.0 = the spot bleeds up to the center of next pixel
	#define SPOT_HEIGHT 0.5

        // Used to counteract the desaturation effect of weighting.
        #define COLOR_BOOST 1.45

        // Macro for weights computing
        #define WEIGHT(w) \
       		if(w>1.0) w=1.0; \
       		w = 1.0 - w * w; \
       		w = w * w;\


float3 RGBtoYUV(half3x3 mat_color)
{
	float a= abs(mul(yuv_weighted, mat_color[0]));
	float b= abs(mul(yuv_weighted, mat_color[1]));
	float c= abs(mul(yuv_weighted, mat_color[2]));

	return float3(a, b, c);
}

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


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



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


struct out_vertex {
	half4 position : POSITION;
	half4 color    : COLOR;
	float2 texCoord : TEXCOORD0;
	half4 t1 : TEXCOORD1;
};

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

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

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

	half2 ps = half2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
	half dx = ps.x;
	half dy = ps.y;

	OUT.texCoord = texCoord;
	OUT.t1.xy = half2(  0,-dy); // B
	OUT.t1.zw = half2(-dx,  0); // D

	return OUT;
}


/*    FRAGMENT SHADER    */
half4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{
	bool3 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
	bool3 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
	half3 E10, E11, E14, E15;
	half3 P[3];

	float2 fp = frac(VAR.texCoord * IN.texture_size);
	half2 st = step(0.5,fp);

	half2 g1 = VAR.t1.xy*(st.x + st.y - 1.0) + VAR.t1.zw*(st.x - st.y    );
	half2 g2 = VAR.t1.xy*(st.y - st.x    ) + VAR.t1.zw*(st.x + st.y - 1.0);

	half3 A = tex2D(decal, VAR.texCoord +g1+g2).xyz;
	half3 B = tex2D(decal, VAR.texCoord +g1   ).xyz;
	half3 C = tex2D(decal, VAR.texCoord +g1-g2).xyz;
	half3 D = tex2D(decal, VAR.texCoord    +g2).xyz;
	half3 E = tex2D(decal, VAR.texCoord       ).xyz;
	half3 F = tex2D(decal, VAR.texCoord    -g2).xyz;
	half3 G = tex2D(decal, VAR.texCoord -g1+g2).xyz;
	half3 H = tex2D(decal, VAR.texCoord -g1   ).xyz;
	half3 I = tex2D(decal, VAR.texCoord -g1-g2).xyz;

	half3  C1 = tex2D(decal,VAR.texCoord +2.0*g1-g2   ).xyz;
	half3  G0 = tex2D(decal,VAR.texCoord -g1+2.0*g2   ).xyz;
	half3  C4 = tex2D(decal,VAR.texCoord +g1-2.0*g2   ).xyz;
	half3  F4 = tex2D(decal,VAR.texCoord    -2.0*g2   ).xyz;
	half3  I4 = tex2D(decal,VAR.texCoord -g1-2.0*g2   ).xyz;
	half3  G5 = tex2D(decal,VAR.texCoord -2.0*g1+g2   ).xyz;
	half3  H5 = tex2D(decal,VAR.texCoord -2.0*g1      ).xyz;
	half3  I5 = tex2D(decal,VAR.texCoord -2.0*g1-g2   ).xyz;
	half3  B1 = tex2D(decal,VAR.texCoord +2.0*g1      ).xyz;
	half3  D0 = tex2D(decal,VAR.texCoord +2.0*g2      ).xyz;
	half3  A1 = tex2D(decal,VAR.texCoord +2.0*g1+g2   ).xyz;
	half3  A0 = tex2D(decal,VAR.texCoord +2.0*g2+g1   ).xyz;

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

	float3 a1 = RGBtoYUV( half3x3(A1, G0, C4) );
	float3 c1 = RGBtoYUV( half3x3(C1, A0, I4) );
	float3 a0 = RGBtoYUV( half3x3(A0, G5, C1) );
	float3 g0 = RGBtoYUV( half3x3(G0, I5, A1) );
	float3 c4 = RGBtoYUV( half3x3(C4, A1, I5) );
	float3 i4 = RGBtoYUV( half3x3(I4, C1, G5) );
	float3 g5 = RGBtoYUV( half3x3(G5, I4, A0) );
	float3 i5 = RGBtoYUV( half3x3(I5, C4, G0) );
	float3 b1 = RGBtoYUV( half3x3(B1, D0, F4) );
	float3 d0 = RGBtoYUV( half3x3(D0, H5, B1) );
	float3 h5 = RGBtoYUV( half3x3(H5, F4, D0) );
	float3 f4 = RGBtoYUV( half3x3(F4, B1, H5) );

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

	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_up   = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;

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

	E10 = E11 = E14 = E15 = E;

	P[0] = px.x ? F : H;
	P[1] = px.y ? B : F;
	P[2] = px.z ? H : D;

	if (edr.x && edr_left.x && edr_up.x)
	{
		E10 = lerp(E10 , P[0],  0.333333);
		E11 = E14 = E15 = P[0];
	}
	else if (edr.x && edr_left.x)
	{
		E10  = lerp(E10 , P[0],  0.25);
		E11  = lerp(E11 , P[0],  0.75);
		E14 = E15 = P[0];
	}
	else if (edr.x && edr_up.x)
	{
		E10  = lerp(E10 , P[0],  0.25);
		E14  = lerp(E14 , P[0],  0.75);
		E11 = E15 = P[0];
	}
	else if (edr.y && edr_left.y && edr.z && edr_up.z)
	{
		E11  = lerp(E11 , P[1],  0.75);
		E14  = lerp(E14 , P[2],  0.75);
		E15  = lerp(E15 , P[1],  0.25);
		E15  = lerp(E15 , P[2],  0.25);
	}
	else if (edr.y && edr_left.y)
	{
		E11  = lerp(E11 , P[1],  0.75);
		E15  = lerp(E15 , P[1],  0.25);
	}
	else if (edr.z && edr_up.z)
	{
		E14  = lerp(E14 , P[2],  0.75);
		E15  = lerp(E15 , P[2],  0.25);
	}
	else if (edr.x)
	{
		E11  = lerp(E11 , P[0],  0.5);
		E14  = lerp(E14 , P[0],  0.5);
		E15 = P[0];
	}


	half3 res = (fp.x < 0.50) ? ((fp.x < 0.25) ? ((fp.y < 0.25) ? E15: (fp.y < 0.50) ? E11: (fp.y < 0.75) ? E14: E15) : ((fp.y < 0.25) ? E14: (fp.y < 0.50) ? E  : (fp.y < 0.75) ? E  : E11)) : ((fp.x < 0.75) ? ((fp.y < 0.25) ? E11: (fp.y < 0.50) ? E  : (fp.y < 0.75) ? E   : E14) : ((fp.y < 0.25) ? E15: (fp.y < 0.50) ? E14: (fp.y < 0.75) ? E11 : E15));


// CRT-caligari - only vertical blend

            float3 color = GAMMA_IN(res);

            float dy = fp.y - 0.5;
            float v_weight_00 = dy / SPOT_HEIGHT;
            WEIGHT(v_weight_00);
            color *= float3( v_weight_00 );

	    // get closest vertical neighbour to blend
 	    float2 coords10;
            if (dy>0.0) {
            	coords10 = -VAR.t1.xy;
                dy = 1.0 - dy;
            } else {
                coords10 = VAR.t1.xy;
                dy = 1.0 + dy;
            }
	    float3 colorNB = TEX2D( VAR.texCoord + coords10 );

            float v_weight_10 = dy / SPOT_HEIGHT;
            WEIGHT( v_weight_10 );

            color += colorNB * float3( v_weight_10 );

            color *= float3( COLOR_BOOST );


	return half4(clamp( GAMMA_OUT(color), 0.0, 1.0 ), 1.0);

}

