/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
   Copyright (C) 2006 guest(r) - guest.r@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;

float3  dt = float3(1,1,1);

/*
   The AdvancedAA shader is well used to:
   - AA 2xscaled gfx. to its 4x absolute size,   
   - AA hi-res "screens" (640x480) to their 2x size or,
   - AA gfx. back to it's original size (looks nice above 640x480, set scaling to 1.0) 
*/

/*
	VERTEX_SHADER
*/

#pragma parameter AA_RESOLUTION_X "AdvancedAA Input Resolution X" 0.0 0.0 1920.0 1.0
#pragma parameter AA_RESOLUTION_Y "AdvancedAA Input Resolution Y" 0.0 0.0 1920.0 1.0

#define AA_RESOLUTION_X_DEF COMPAT_texture_size.x
#define AA_RESOLUTION_Y_DEF COMPAT_texture_size.y
#ifdef PARAMETER_UNIFORM
uniform float AA_RESOLUTION_X;
uniform float AA_RESOLUTION_Y;
#else
#define AA_RESOLUTION_X AA_RESOLUTION_X_DEF
#define AA_RESOLUTION_Y AA_RESOLUTION_Y_DEF
#endif

struct out_vertex
{
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
	float4 position : COMPAT_POS;
	float4 t1 : TEXCOORD1;
	float4 t2 : TEXCOORD2;
	float4 t3 : TEXCOORD3;
	float4 t4 : TEXCOORD4;
	float2 CT : TEXCOORD5;
};

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);

	float2 ps = float2(1.0/((AA_RESOLUTION_X == 0) ? AA_RESOLUTION_X_DEF : AA_RESOLUTION_X), 1.0/((AA_RESOLUTION_Y == 0) ? AA_RESOLUTION_Y_DEF : AA_RESOLUTION_Y));
	float dx = ps.x*0.5;
	float dy = ps.y*0.5;

	// This line fix a bug in ATI cards.
	float2 texCoord1 = texCoord + float2(0.0000001, 0.0000001);

	OUT.CT = texCoord1;
	OUT.t1.xy = texCoord1 + float2(-dx, 0);
	OUT.t2.xy = texCoord1 + float2( dx, 0);
	OUT.t3.xy = texCoord1 + float2( 0,-dy);
	OUT.t4.xy = texCoord1 + float2( 0, dy);
	OUT.t1.zw = texCoord1 + float2(-dx,-dy);
	OUT.t2.zw = texCoord1 + float2(-dx, dy);
	OUT.t3.zw = texCoord1 + float2( dx,-dy);
	OUT.t4.zw = texCoord1 + float2( dx, dy);

	return OUT;
}

/* 
	FRAGMENT SHADER
*/

float4 advanced_aa(float2 texture_size, float2 video_size, float2 output_size, COMPAT_Texture2D(decal), float2 CT, float4 t1, 
	float4 t2, float4 t3, float4 t4)
{
   float3 c00 = COMPAT_SamplePoint(decal, t1.zw).xyz; 
   float3 c10 = COMPAT_SamplePoint(decal, t3.xy).xyz;
   float3 c20 = COMPAT_SamplePoint(decal, t3.zw).xyz;
   float3 c01 = COMPAT_SamplePoint(decal, t1.xy).xyz;
   float3 c11 = COMPAT_SamplePoint(decal, CT).xyz;
   float3 c21 = COMPAT_SamplePoint(decal, t2.xy).xyz;
   float3 c02 = COMPAT_SamplePoint(decal, t2.zw).xyz;
   float3 c12 = COMPAT_SamplePoint(decal, t4.xy).xyz;
   float3 c22 = COMPAT_SamplePoint(decal, t4.zw).xyz;

   float d1=dot(abs(c00-c22),dt)+0.0001;
   float d2=dot(abs(c20-c02),dt)+0.0001;
   float hl=dot(abs(c01-c21),dt)+0.0001;
   float vl=dot(abs(c10-c12),dt)+0.0001;

   float k1=0.5*(hl+vl);
   float k2=0.5*(d1+d2);

   float3 _t1=(hl*(c10+c12)+vl*(c01+c21)+k1*c11)/(2.5*(hl+vl));
   float3 _t2=(d1*(c20+c02)+d2*(c00+c22)+k2*c11)/(2.5*(d1+d2));

   k1=dot(abs(_t1-c11),dt)+0.0001;
   k2=dot(abs(_t2-c11),dt)+0.0001;

   float4 final = float4((k1*_t2+k2*_t1)/(k1+k2),1);
   return final;
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return advanced_aa(COMPAT_texture_size, COMPAT_video_size, COMPAT_output_size, decal, VOUT.CT, VOUT.t1, VOUT.t2, VOUT.t3, VOUT.t4);
}
COMPAT_END