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

/*
   Copyright (C) 2010 Team XBMC
   http://www.xbmc.org
   Copyright (C) 2011 Stefanos A.
   http://www.opentk.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, 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 XBMC; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
http://www.gnu.org/copyleft/gpl.html

    From this forum post:
        http://board.byuu.org/viewtopic.php?p=33488#p33488

*/

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

#define FIX(c) max(abs(c), 1e-5);

const float PI = 3.1415926535897932384626433832795;

float4 weight4(float x)
{         
   float4 sample = FIX(PI * float4(1.0 + x, x, 1.0 - x, 2.0 - x));

   // Lanczos2. Note: we normalize below, so no point in multiplying by radius (2.0)
   float4 ret = /*2.0 **/ sin(sample) * sin(sample / 2.0) / (sample * sample);

   // Normalize
   return ret / dot(ret, float4(1.0, 1.0, 1.0, 1.0));
}         

float3 pixel(float xpos, float ypos, COMPAT_Texture2D(s_p))
{
   return COMPAT_SamplePoint(s_p, float2(xpos, ypos)).rgb;
}

float3 line_run(float ypos, float4 xpos, float4 linetaps, COMPAT_Texture2D(s_p))
{         
   return mul(linetaps, float4x3(
            pixel(xpos.x, ypos, s_p),
            pixel(xpos.y, ypos, s_p),
            pixel(xpos.z, ypos, s_p),
            pixel(xpos.w, ypos, s_p)));
}

float4 lanczos4(float2 texture_size, float2 texCoord, COMPAT_Texture2D(decal))
{
   float2 stepxy = 1.0 / texture_size.xy;
   float2 pos = texCoord.xy + stepxy * 0.5;
   float2 f = frac(pos / stepxy);

   float2 xystart = (-1.5 - f) * stepxy + pos;
   float4 xpos = float4(
         xystart.x,
         xystart.x + stepxy.x,
         xystart.x + stepxy.x * 2.0,
         xystart.x + stepxy.x * 3.0);

   float4 linetaps   = weight4(f.x);
   float4 columntaps = weight4(f.y);   

   // final sum and weight normalization
   return float4(mul(columntaps, float4x3(
               line_run(xystart.y                 , xpos, linetaps, decal),
               line_run(xystart.y + stepxy.y      , xpos, linetaps, decal),
               line_run(xystart.y + stepxy.y * 2.0, xpos, linetaps, decal),
               line_run(xystart.y + stepxy.y * 3.0, xpos, linetaps, decal))),1.0);
}

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