/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*
   Hyllian's 2xBR level 3 pass1 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.
*/

const static float2x4 sym_vectors  = float2x4(1,  1,   -1, -1,    1, -1,   -1,  1);


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


float3 remapFrom01(float3 v, float3 low, float3 high)
{
	return round(lerp(low, high, v));
}

float4 unpack_info(float i)
{
	float4 info;
	info.x = round(modf(i/2.0f, i));
	info.y = round(modf(i/2.0f, i));
	info.z = round(modf(i/2.0f, i));
	info.w = i;

	return info;
}

struct orig
{
	float2 tex_coord;
	uniform float2 texture_size;
	uniform sampler2D texture;
};


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;
	float4 t1       : TEXCOORD1;
	float2 orig_tex;
};

/*    VERTEX_SHADER    */
void main_vertex
(
	float4 position	: POSITION,
	float4 color	: COLOR,
	float2 texCoord : TEXCOORD0,

   	uniform float4x4 modelViewProj,
	orig ORIG,
	out out_vertex co
)
{
	co.position = mul(modelViewProj, position);
	co.color = color;

	float2 ps = float2(1.0/ORIG.texture_size.x, 1.0/ORIG.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

	co.texCoord = texCoord;
	co.orig_tex = ORIG.tex_coord;
	co.t1 = float4(dx, 0, 0, dy);  // F  H
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex co, uniform sampler2D decal : TEXUNIT0, orig ORIG, uniform input IN) : COLOR
{
	float2 px; // px = pixel to blend
	float pxr, pxd, pct, edr3_nrl, edr3_ndu;
	float pc[10] = {0.0, 1.0/8.0, 1.0/6.0, 1.0/4.0, 3.0/8.0, 1.0/2.0, 5.0/8.0, 3.0/4.0, 5.0/6.0, 7.0/8.0};

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

	float2 g1 = dir*( saturate(-dir.y*dir.x)*co.t1.zw + saturate( dir.y*dir.x)*co.t1.xy);
	float2 g2 = dir*( saturate( dir.y*dir.x)*co.t1.zw + saturate(-dir.y*dir.x)*co.t1.xy);

	float3 F   = tex2D(ORIG.texture, co.orig_tex +g1).xyz;
	float3 B   = tex2D(ORIG.texture, co.orig_tex -g2).xyz;
	float3 D   = tex2D(ORIG.texture, co.orig_tex -g1).xyz;
	float3 H   = tex2D(ORIG.texture, co.orig_tex +g2).xyz;
	float3 E   = tex2D(ORIG.texture, co.orig_tex    ).xyz;

	float3 F4  = tex2D(ORIG.texture, co.orig_tex +2.0*g1).xyz;
	float3 I   = tex2D(ORIG.texture, co.orig_tex  +g1+g2).xyz;
	float3 H5  = tex2D(ORIG.texture, co.orig_tex +2.0*g2).xyz;

	float4 icomp    = round(saturate(mul(dir, sym_vectors))); // choose info component
	float  info     = remapFrom01(dot(tex2D(decal, co.texCoord   ), icomp), 255.0f); // retrieve 1st pass info
	float  info_nr  = remapFrom01(dot(tex2D(decal, co.texCoord+g1), icomp), 255.0f); // 1st pass info from right neighbor
	float  info_nd  = remapFrom01(dot(tex2D(decal, co.texCoord+g2), icomp), 255.0f); // 1st pass info from down neighbor

	modf(info/2.0f, info); // discard info
	modf(info/2.0f, info); // discard info
	px.x = round(modf(info/2.0f, info));
	px.y = round(modf(info/2.0f, info));

	float4 flags = unpack_info(info); // retrieve 1st pass flags

	edr3_nrl = round(modf(info_nr/2.0f, info_nr));
	modf(info_nr/2.0f, info_nr); // discard info_nr
	modf(info_nr/2.0f, info_nr); // discard info_nr
	pxr      = round(modf(info_nr/2.0f, info_nr));

	modf(info_nd/2.0f, info_nd); // discard info_nd
	edr3_ndu = round(modf(info_nd/2.0f, info_nd));
	modf(info_nd/2.0f, info_nd); // discard info_nd
	pxd      = round(modf(info_nd/2.0f, info_nd));

	float aux = round(dot(float4(8.0, 4.0, 2.0, 1.0), flags));

	if (aux >= 5.0)
	{
		pct = (aux==5.0 ? pc[5] : (aux==6.0 ? pc[6] : (aux==7.0 ? pc[7] : (aux==8.0 ? pc[8] : pc[9]))));
	}
	else
	{
		pct = (aux==0.0 ? pc[0] : (aux==1.0 ? pc[1] : (aux==2.0 ? pc[2] : (aux==3.0 ? pc[3] : pc[4]))));
	}

	float3 color;

	if (edr3_nrl)
	{
		color = lerp(E, lerp(I, F4, pxr), pc[2]); // interpolate if there's edge
	}
	else if (edr3_ndu)
	{
		color = lerp(E, lerp(I, H5, pxd), pc[2]); // interpolate if there's edge
	}
	else
	{
		color = lerp(E, lerp(lerp(H, F, px.y), lerp(D, B, px.y), px.x), pct); // interpolate if there's edge
	}

	return float4(color, 1.0);
}



