/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*
   Hyllian's 2xBR level 1 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 float coef           = 2.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 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));
}

float2 unpack_info(float i)
{
	float2 info;
	info.x = round(modf(i/2.0f, i));
	info.y = 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
{
	float px, edr; // px = pixel to blend, edr = edge detection rule

	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*co.t1.xy;
	float2 g2  = dir*co.t1.zw;

	float3 F   = 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;

	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
	float2 flags = unpack_info(info); // retrieve 1st pass flags

	edr = flags.x;
	px  = flags.y;

	float3 color = lerp(E, lerp(H, F, px), edr*0.5); // interpolate if there's edge

	return float4(color, 1.0);
}

