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

/*
	Fragment shader based on "Improved texture interpolation" by Iñigo Quílez
	Original description: http://www.iquilezles.org/www/articles/texture/texture.htm

*/

#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 quilez(float2 texture_size, float2 texCoord, COMPAT_Texture2D(decal))
{
	float2 p = texCoord.xy;

	p = p * texture_size + float2(0.5, 0.5);

	float2 i = floor(p);
	float2 f = p - i;
	f = f * f * f * (f * (f * 6.0 - float2(15.0, 15.0)) + float2(10.0, 10.0));
	p = i + f;

	p = (p - float2(0.5, 0.5)) / texture_size;

	// final sum and weight normalization
	return COMPAT_Sample(decal, p);
}

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