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

/*
   4xGLSLHqFilter shader
   
   Copyright (C) 2005 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;

/* Default Vertex shader */
struct out_vertex {
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
	float4 t1 : TEXCOORD1;
	float4 t2 : TEXCOORD2;
	float4 t3 : TEXCOORD3;
	float4 t4 : TEXCOORD4;
	float4 t5 : TEXCOORD5;
	float4 t6 : TEXCOORD6;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

/*    VERTEX_SHADER    */
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;

	float x = 0.5 * (1.0 / COMPAT_texture_size.x);
	float y = 0.5 * (1.0 / COMPAT_texture_size.y);
	float2 dg1 = float2( x, y);
	float2 dg2 = float2(-x, y);
	float2 sd1 = dg1*0.5;
	float2 sd2 = dg2*0.5;
	float2 ddx = float2(x,0.0);
	float2 ddy = float2(0.0,y);

	OUT.t1 = float4(texCoord-sd1,texCoord-ddy);
	OUT.t2 = float4(texCoord-sd2,texCoord+ddx);
	OUT.t3 = float4(texCoord+sd1,texCoord+ddy);
	OUT.t4 = float4(texCoord+sd2,texCoord-ddx);
	OUT.t5 = float4(texCoord-dg1,texCoord-dg2);
	OUT.t6 = float4(texCoord+dg1,texCoord+dg2);

	return OUT;
}

const static float3 dt = float3(1.0, 1.0, 1.0);
const float mx = 1.0;      // start smoothing wt.
const float k = -1.10;      // wt. decrease factor
const float max_w = 0.75;    // max filter weigth
const float min_w = 0.03;    // min filter weigth
const float lum_add = 0.33;  // effects smoothing

float4 FourxScaleHQ(float4 t1, float4 t2, float4 t3, float4 t4, float4 t5, float4 t6, float2 texCoord, COMPAT_Texture2D(decal))
 {

   float3 c  = COMPAT_SamplePoint(decal, texCoord).xyz;
   float3 i1 = COMPAT_SamplePoint(decal, t1.xy).xyz; 
   float3 i2 = COMPAT_SamplePoint(decal, t2.xy).xyz; 
   float3 i3 = COMPAT_SamplePoint(decal, t3.xy).xyz; 
   float3 i4 = COMPAT_SamplePoint(decal, t4.xy).xyz; 
   float3 o1 = COMPAT_SamplePoint(decal, t5.xy).xyz; 
   float3 o3 = COMPAT_SamplePoint(decal, t6.xy).xyz; 
   float3 o2 = COMPAT_SamplePoint(decal, t5.zw).xyz;
   float3 o4 = COMPAT_SamplePoint(decal, t6.zw).xyz;
   float3 s1 = COMPAT_SamplePoint(decal, t1.zw).xyz; 
   float3 s2 = COMPAT_SamplePoint(decal, t2.zw).xyz; 
   float3 s3 = COMPAT_SamplePoint(decal, t3.zw).xyz; 
   float3 s4 = COMPAT_SamplePoint(decal, t4.zw).xyz;  

   float ko1=dot(abs(o1-c),dt);
   float ko2=dot(abs(o2-c),dt);
   float ko3=dot(abs(o3-c),dt);
   float ko4=dot(abs(o4-c),dt);

   float k1=min(dot(abs(i1-i3),dt),max(ko1,ko3));
   float k2=min(dot(abs(i2-i4),dt),max(ko2,ko4));

   float w1 = k2; if(ko3<ko1) w1*=ko3/ko1;
   float w2 = k1; if(ko4<ko2) w2*=ko4/ko2;
   float w3 = k2; if(ko1<ko3) w3*=ko1/ko3;
   float w4 = k1; if(ko2<ko4) w4*=ko2/ko4;

   c=(w1*o1+w2*o2+w3*o3+w4*o4+0.001*c)/(w1+w2+w3+w4+0.001);
   w1 = k*dot(abs(i1-c)+abs(i3-c),dt)/(0.125*dot(i1+i3,dt)+lum_add);
   w2 = k*dot(abs(i2-c)+abs(i4-c),dt)/(0.125*dot(i2+i4,dt)+lum_add);
   w3 = k*dot(abs(s1-c)+abs(s3-c),dt)/(0.125*dot(s1+s3,dt)+lum_add);
   w4 = k*dot(abs(s2-c)+abs(s4-c),dt)/(0.125*dot(s2+s4,dt)+lum_add);

   w1 = clamp(w1+mx,min_w,max_w); 
   w2 = clamp(w2+mx,min_w,max_w);
   w3 = clamp(w3+mx,min_w,max_w); 
   w4 = clamp(w4+mx,min_w,max_w);

   return float4((w1*(i1+i3)+w2*(i2+i4)+w3*(s1+s3)+w4*(s2+s4)+c)/(2.0*(w1+w2+w3+w4)+1.0), 1.0);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return FourxScaleHQ(VOUT.t1, VOUT.t2, VOUT.t3, VOUT.t4, VOUT.t5, VOUT.t6, VOUT.texCoord, decal);
}
COMPAT_END