/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
   bicubic-fast Shader

   Copyright (C) 2011-2015 Hyllian - sergiogdb@gmail.com

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.

*/

#include "../../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

const static float4x4 invX = float4x4(-1.0/6.0,  0.5, -1.0/3.0, 0.0,
                                           0.5, -1.0,     -0.5, 1.0,
                                          -0.5,  0.5,      1.0, 0.0,
                                       1.0/6.0,  0.0, -1.0/6.0, 0.0);


// Can't init statically from variable, even though it's const ...
//const static float4x4 invY = transpose(invX);
const static float4x4 invY = float4x4(-1.0/6.0,  0.5, -0.5,  1.0/6.0,
                                           0.5, -1.0,  0.5,      0.0,
                                      -1.0/3.0, -0.5,  1.0, -1.0/6.0,
                                           0.0,  1.0,  0.0,      0.0);


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;
	float4 t7       : TEXCOORD7;
	float2 t8;
#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
	float2 ps = float2(1.0/COMPAT_texture_size.x, 1.0/COMPAT_texture_size.y);
	float dx = ps.x;
	float dy = ps.y;

	// This line fix a bug in ATI cards.
	float2 tex = texCoord + float2(0.0000001, 0.0000001);

	OUT.position = mul(modelViewProj, position);
	OUT.texCoord = tex;
	OUT.t1 = float4(tex,tex) + float4(   -dx,    -dy,    0.0,    -dy);
	OUT.t2 = float4(tex,tex) + float4(    dx,    -dy, 2.0*dx,    -dy);
	OUT.t3 = float4(tex,tex) + float4(   -dx,    0.0,     dx,    0.0); 
	OUT.t4 = float4(tex,tex) + float4(2.0*dx,    0.0,    -dx,     dy);
	OUT.t5 = float4(tex,tex) + float4(   0.0,     dy,     dx,     dy);
	OUT.t6 = float4(tex,tex) + float4(2.0*dx,     dy,    -dx, 2.0*dy);
	OUT.t7 = float4(tex,tex) + float4(   0.0, 2.0*dy,     dx, 2.0*dy);
	OUT.t8 = tex             + float2(2.0*dx, 2.0*dy);

	return OUT;
}

float4 bicubic_fast(float2 texture_size, float2 video_size, float2 output_size, COMPAT_Texture2D(decal), float2 texCoord,
	float4 t1, float4 t2, float4 t3, float4 t4, float4 t5, float4 t6, float4 t7, float2 t8)
{

  float2 fp = frac(texCoord*texture_size);

  float3 c00 = COMPAT_SamplePoint(decal, t1.xy).xyz;
  float3 c01 = COMPAT_SamplePoint(decal, t1.zw).xyz;
  float3 c02 = COMPAT_SamplePoint(decal, t2.xy).xyz;
  float3 c03 = COMPAT_SamplePoint(decal, t2.zw).xyz;
  float3 c10 = COMPAT_SamplePoint(decal, t3.xy).xyz;
  float3 c11 = COMPAT_SamplePoint(decal, texCoord).xyz;
  float3 c12 = COMPAT_SamplePoint(decal, t3.zw).xyz;
  float3 c13 = COMPAT_SamplePoint(decal, t4.xy).xyz;
  float3 c20 = COMPAT_SamplePoint(decal, t4.zw).xyz;
  float3 c21 = COMPAT_SamplePoint(decal, t5.xy).xyz;
  float3 c22 = COMPAT_SamplePoint(decal, t5.zw).xyz;
  float3 c23 = COMPAT_SamplePoint(decal, t6.xy).xyz;
  float3 c30 = COMPAT_SamplePoint(decal, t6.zw).xyz;
  float3 c31 = COMPAT_SamplePoint(decal, t7.xy).xyz;
  float3 c32 = COMPAT_SamplePoint(decal, t7.zw).xyz;
  float3 c33 = COMPAT_SamplePoint(decal, t8.xy).xyz;


  float4x4   red_matrix = float4x4(c00.x, c01.x, c02.x, c03.x,
                                   c10.x, c11.x, c12.x, c13.x,
                                   c20.x, c21.x, c22.x, c23.x,
                                   c30.x, c31.x, c32.x, c33.x);

  float4x4 green_matrix = float4x4(c00.y, c01.y, c02.y, c03.y,
                                   c10.y, c11.y, c12.y, c13.y,
                                   c20.y, c21.y, c22.y, c23.y,
                                   c30.y, c31.y, c32.y, c33.y);

  float4x4  blue_matrix = float4x4(c00.z, c01.z, c02.z, c03.z,
                                   c10.z, c11.z, c12.z, c13.z,
                                   c20.z, c21.z, c22.z, c23.z,
                                   c30.z, c31.z, c32.z, c33.z);


  float4x1 invX_Px = mul(invX, float4x1(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
  float1x4 Py_invY = mul(float1x4(fp.y*fp.y*fp.y, fp.y*fp.y, fp.y, 1.0), invY);


  float red   = float(mul(Py_invY, mul(  red_matrix, invX_Px)));
  float green = float(mul(Py_invY, mul(green_matrix, invX_Px)));
  float blue  = float(mul(Py_invY, mul( blue_matrix, invX_Px)));

  return float4(red, green, blue, 1.0);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return bicubic_fast(COMPAT_texture_size, COMPAT_video_size, COMPAT_output_size, decal, VOUT.texCoord,
		VOUT.t1, VOUT.t2, VOUT.t3, VOUT.t4, VOUT.t5, VOUT.t6, VOUT.t7, VOUT.t8);
}
COMPAT_END