#pragma parameter AAOFFSET "Filter width" 1.0 0.25 2.0 0.125 
#ifdef PARAMETER_UNIFORM
uniform float AAOFFSET;
#else
#define AAOFFSET  1.0
#endif
// END PARAMETERS // 


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

/*
   Copyright (C) 2016 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;

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
#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;
	
	return OUT;
}

float4 two_xsal_lvl2_xbr(float2 texture_size, float2 texCoord, COMPAT_Texture2D(decal))
{
   float2 texsize = texture_size;
   float dx = AAOFFSET/texsize.x;
   float dy = AAOFFSET/texsize.y;
   float3 dt = float3(1.0, 1.0, 1.0);
 
   float4 yx = float4( dx, dy, -dx, -dy);
   float4 xh = yx*float4(3.0,1.0,3.0,1.0);
   float4 yv = yx*float4(1.0,3.0,1.0,3.0);

   float3 c11 = COMPAT_SamplePoint(decal, texCoord        ).xyz; 	
   float3 s00 = COMPAT_SamplePoint(decal, texCoord + yx.zw).xyz; 
   float3 s20 = COMPAT_SamplePoint(decal, texCoord + yx.xw).xyz; 
   float3 s22 = COMPAT_SamplePoint(decal, texCoord + yx.xy).xyz; 
   float3 s02 = COMPAT_SamplePoint(decal, texCoord + yx.zy).xyz;
   float3 h00 = COMPAT_SamplePoint(decal, texCoord + xh.zw).xyz; 
   float3 h20 = COMPAT_SamplePoint(decal, texCoord + xh.xw).xyz; 
   float3 h22 = COMPAT_SamplePoint(decal, texCoord + xh.xy).xyz; 
   float3 h02 = COMPAT_SamplePoint(decal, texCoord + xh.zy).xyz;
   float3 v00 = COMPAT_SamplePoint(decal, texCoord + yv.zw).xyz; 
   float3 v20 = COMPAT_SamplePoint(decal, texCoord + yv.xw).xyz; 
   float3 v22 = COMPAT_SamplePoint(decal, texCoord + yv.xy).xyz; 
   float3 v02 = COMPAT_SamplePoint(decal, texCoord + yv.zy).xyz;	 
   
   float m1=1.0/(dot(abs(s00-s22),dt)+0.00001);
   float m2=1.0/(dot(abs(s02-s20),dt)+0.00001);
   float h1=1.0/(dot(abs(s00-h22),dt)+0.00001);
   float h2=1.0/(dot(abs(s02-h20),dt)+0.00001);
   float h3=1.0/(dot(abs(h00-s22),dt)+0.00001);
   float h4=1.0/(dot(abs(h02-s20),dt)+0.00001);
   float v1=1.0/(dot(abs(s00-v22),dt)+0.00001);
   float v2=1.0/(dot(abs(s02-v20),dt)+0.00001);
   float v3=1.0/(dot(abs(v00-s22),dt)+0.00001);
   float v4=1.0/(dot(abs(v02-s20),dt)+0.00001);

   float3 t1 = 0.5*(m1*(s00+s22)+m2*(s02+s20))/(m1+m2);
   float3 t2 = 0.5*(h1*(s00+h22)+h2*(s02+h20)+h3*(h00+s22)+h4*(h02+s20))/(h1+h2+h3+h4);
   float3 t3 = 0.5*(v1*(s00+v22)+v2*(s02+v20)+v3*(v00+s22)+v4*(v02+s20))/(v1+v2+v3+v4);

   float k1 = 1.0/(dot(abs(t1-c11),dt)+0.00001);
   float k2 = 1.0/(dot(abs(t2-c11),dt)+0.00001);
   float k3 = 1.0/(dot(abs(t3-c11),dt)+0.00001);

   return float4((k1*t1 + k2*t2 + k3*t3)/(k1+k2+k3),1.0);
}

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