/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/

/*
   Hyllian's 5xBRetro Shader
   
   Copyright (C) 2012 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.

*/

#include "../../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
	float4 t1       : TEXCOORD1;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

out_vertex main_vertex(COMPAT_IN_VERTEX)
{
	out_vertex OUT;
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
#else
	OUT.Color = color;
#endif
	OUT.position = mul(modelViewProj, position);
	OUT.texCoord = texCoord * 1.0001;
	float2 ps = float2(1.0/COMPAT_texture_size.x, 1.0/COMPAT_texture_size.y);
	float dx = ps.x;
	float dy = ps.y;
	OUT.t1.xy = float2(  0,-dy); // B
	OUT.t1.zw = float2(-dx,  0); // D
	
	return OUT;
}

const static float style          = 0.75;  // Use 0.5 to get xBR LVL1 and 0.75 to get retro style.
const static float coef           = 2.0;
const static float3 dtt           = float3(65536,255,1);
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]);

float RGBtoYUV(float3 color)
{
	return dot(color, yuv_weighted[0]);
}

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

bool eq(float A, float B)
{
	return (df(A, B) < 15.0);
}

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

/*
    xBR LVL1 works over the pixels below:

        |B |C |
     |D |E |F |F4|
     |G |H |I |I4|
        |H5|I5|

    Consider E as the central pixel. xBR LVL1 needs only to look at 12 texture pixels.
*/

/*    FRAGMENT SHADER    */
float4 xBR_retro(float2 texture_size, float4 t1, float2 texCoord, COMPAT_Texture2D(decal))
{
	bool edr, px; // px = pixel, edr = edge detection rule
	bool interp_restriction_lv1;
	bool nc; // new_color
	bool fx; // inequations of straight lines.

	float2 pos  = frac(texCoord*texture_size)-float2(0.5, 0.5); // pos = pixel position
	float2 dir = sign(pos); // dir = pixel direction

	float2 g1 = dir*t1.xy;
	float2 g2 = dir*t1.zw;

	float3 B = COMPAT_SamplePoint(decal, texCoord +g1   ).xyz;
	float3 C = COMPAT_SamplePoint(decal, texCoord +g1-g2).xyz;
	float3 D = COMPAT_SamplePoint(decal, texCoord    +g2).xyz;
	float3 E = COMPAT_SamplePoint(decal, texCoord       ).xyz;
	float3 F = COMPAT_SamplePoint(decal, texCoord    -g2).xyz;
	float3 G = COMPAT_SamplePoint(decal, texCoord -g1+g2).xyz;
	float3 H = COMPAT_SamplePoint(decal, texCoord -g1   ).xyz;
	float3 I = COMPAT_SamplePoint(decal, texCoord -g1-g2).xyz;

	float3 F4 = COMPAT_SamplePoint(decal, texCoord    -2.0*g2   ).xyz;
	float3 I4 = COMPAT_SamplePoint(decal, texCoord -g1-2.0*g2   ).xyz;
	float3 H5 = COMPAT_SamplePoint(decal, texCoord -2.0*g1      ).xyz;
	float3 I5 = COMPAT_SamplePoint(decal, texCoord -2.0*g1-g2   ).xyz;

	float b = RGBtoYUV( B );
	float c = RGBtoYUV( C );
	float d = RGBtoYUV( D );
	float e = RGBtoYUV( E );
	float f = RGBtoYUV( F );
	float g = RGBtoYUV( G );
	float h = RGBtoYUV( H );
	float i = RGBtoYUV( I );

	float i4 = RGBtoYUV( I4 );
	float i5 = RGBtoYUV( I5 );
	float h5 = RGBtoYUV( H5 );
	float f4 = RGBtoYUV( F4 );

	fx = ( dot(dir,pos) > style );

	interp_restriction_lv1 = ((e!=f) && (e!=h));

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

	nc = ( edr && fx );

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

	float3 res = nc ? px ? F : H : E;

	return float4(res, 1.0);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return xBR_retro(COMPAT_texture_size, VOUT.t1, VOUT.texCoord, decal);
}
COMPAT_END