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

/*
   Author: Themaister
   License: Public domain
*/

#pragma parameter QUAD_INTERP_RESOLUTION_X "QuadInterp Input Resolution X" 0.0 0.0 1920.0 1.0
#pragma parameter QUAD_INTERP_RESOLUTION_Y "QuadInterp Input Resolution Y" 0.0 0.0 1920.0 1.0
#pragma parameter QUAD_INTERP_SHARPNESS     "QuadInterp Sharpness"           2.01 0.0 10.0 0.01

#define QUAD_INTERP_RESOLUTION_X_DEF texture_size.x
#define QUAD_INTERP_RESOLUTION_Y_DEF texture_size.y
#ifdef PARAMETER_UNIFORM
uniform float QUAD_INTERP_RESOLUTION_X;
uniform float QUAD_INTERP_RESOLUTION_Y;
uniform float QUAD_INTERP_SHARPNESS;
#else
#define QUAD_INTERP_RESOLUTION_X QUAD_INTERP_RESOLUTION_X_DEF
#define QUAD_INTERP_RESOLUTION_Y QUAD_INTERP_RESOLUTION_Y_DEF
#define QUAD_INTERP_SHARPNESS 2.01
#endif

#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;
}

float3 quad_inter(float3 x0, float3 x1, float3 x2, float x)
{
   float3 poly[3];
   poly[2] =  0.5 * x0 - x1 + 0.5*x2;
   poly[1] = -1.5 * x0 + 2.0 * x1 - 0.5*x2;
   poly[0] = x0;
   return poly[2] * x * x + poly[1] * x + poly[0];
}

float4 quad_interp(float2 texture_size, float2 texCoord, COMPAT_Texture2D(decal))
{
   /* messy I know but we need to make it possible to have it default to input resolution x/y in case RESOLUTION_X is 0.0 */
	float2 texsize = float2(1.0/((QUAD_INTERP_RESOLUTION_X == 0) ? QUAD_INTERP_RESOLUTION_X_DEF : QUAD_INTERP_RESOLUTION_X), 1.0/((QUAD_INTERP_RESOLUTION_Y == 0) ? QUAD_INTERP_RESOLUTION_Y_DEF : QUAD_INTERP_RESOLUTION_Y));
   float dx = float(pow(QUAD_INTERP_SHARPNESS * texsize.x, -1.0));
   float dy = float(pow(QUAD_INTERP_SHARPNESS * texsize.y, -1.0));

   float3 c00 = COMPAT_Sample(decal, texCoord + float2(-dx, -dy)).xyz;
   float3 c01 = COMPAT_Sample(decal, texCoord + float2(-dx, 0)).xyz;
   float3 c02 = COMPAT_Sample(decal, texCoord + float2(-dx, dy)).xyz;
   float3 c10 = COMPAT_Sample(decal, texCoord + float2(0, -dy)).xyz;
   float3 c11 = COMPAT_Sample(decal, texCoord + float2(0, 0)).xyz;
   float3 c12 = COMPAT_Sample(decal, texCoord + float2(0, dy)).xyz;
   float3 c20 = COMPAT_Sample(decal, texCoord + float2(dx, -dy)).xyz;
   float3 c21 = COMPAT_Sample(decal, texCoord + float2(dx, 0)).xyz;
   float3 c22 = COMPAT_Sample(decal, texCoord + float2(dx, dy)).xyz;

   float frac_amt_x = frac(texCoord.x * texsize.x);
   float frac_amt_y = frac(texCoord.y * texsize.y);
   float3 loval = quad_inter(c00, c10, c20, frac_amt_x + 0.5);
   float3 midval = quad_inter(c01, c11, c21, frac_amt_x + 0.5);
   float3 hival = quad_inter(c02, c12, c22, frac_amt_x + 0.5);
   float3 res = quad_inter(loval, midval, hival, frac_amt_y + 0.5);

// Bilinear!
//   float3 first = lerp(c00, c20, frac(texCoord.x * texsize.x + 0.5));
//   float3 second = lerp(c02, c22, frac(texCoord.x * texsize.x + 0.5));
//   float3 res = lerp(first, second, frac(texCoord.y * texsize.y + 0.5));
//   return float4(res, 1.0);


   return float4(res, 1.0);
}

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