/*
   Hyllian's 5xBR v3.5 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 half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);

float4 RGBtoYUV(half4x3 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]));
	float d= abs(mul(yuv_weighted, mat_color[3]));

	return float4(a, b, c, d);
}

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

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
{
	half2 video_size;
	float2 texture_size;
	half2 output_size;
};

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

out_vertex main_vertex
(
	half4 position	: POSITION,
	float2 texCoord : TEXCOORD0,

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

	OUT.position = mul(modelViewProj, position);

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

	OUT.texCoord = texCoord;
	OUT.t1.xy = half2( dx,  0); // F
	OUT.t1.zw = half2(  0, dy); // H

	return OUT;
}

half4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{
	bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
	bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
	bool4 nc; // new_color
	bool4 fx, fx_left, fx_up; // inequations of straight lines.

	float2 fp = frac(VAR.texCoord * IN.texture_size);

	half2 dx = VAR.t1.xy;
	half2 dy = VAR.t1.zw;

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

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

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

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

	float4 Ao = float4(1.0, -1.0, -1.0, 1.0);
	float4 Bo = float4(1.0,  1.0, -1.0,-1.0);
	float4 Co = float4(1.5,  0.5, -0.5, 0.5);
	float4 Ax = float4(1.0, -1.0, -1.0, 1.0);
	float4 Bx = float4(0.5,  2.0, -0.5,-2.0);
	float4 Cx = float4(1.0,  1.0, -0.5, 0.0);
	float4 Ay = float4(1.0, -1.0, -1.0, 1.0);
	float4 By = float4(2.0,  0.5, -2.0,-0.5);
	float4 Cy = float4(2.0,  0.0, -1.0, 0.5);

	// These inequations define the line below which interpolation occurs.
	fx      = (Ao * fp.y + Bo * fp.x > Co); 
	fx_left = (Ax * fp.y + Bx * fp.x > Cx);
	fx_up   = (Ay * fp.y + By * fp.x > Cy);

	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;

	nc = (edr && (fx || edr_left && fx_left || edr_up && fx_up));

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

	half3 res = nc.x ? px.x ? F : H : nc.y ? px.y ? B : F : nc.z ? px.z ? D : B : nc.w ? px.w ? H : D : E;

	return half4(res, 1.0);
}

