/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
    cgwg's CRT shader

    Copyright (C) 2010-2011 cgwg, Themaister

    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.

    (cgwg gave their consent to have their code distributed under the GPL in
    this message:

        http://board.byuu.org/viewtopic.php?p=26075#p26075

        "Feel free to distribute my shaders under the GPL. After all, the
        barrel distortion code was taken from the Curvature shader, which is
        under the GPL."
    )
*/

struct tex_coords
{
   float2 c01; 
   float2 c11; 
   float2 c21;
   float2 c31;
   float2 c02;
   float2 c12; 
   float2 c22;
   float2 c32; 
   float mod_factor;
   float2 ratio_scale;
};

struct input
{
   float2 video_size;
   float2 texture_size;
   float2 output_size;
   float  frame_count;
   float  frame_direction;
   float frame_rotation;
};

void main_vertex
(
   float4 position : POSITION,
   out float4 oPosition : POSITION,
   uniform float4x4 modelViewProj,

   float4 color : COLOR,
   out float4 oColor : COLOR,

   float2 tex : TEXCOORD,

   uniform input IN,
   out tex_coords coords : TEXCOORD1
)
{
   oPosition = mul(modelViewProj, position);
   oColor = color;

   float2 delta = 1.0 / IN.texture_size;
   float dx = delta.x;
   float dy = delta.y;

   coords.c01 = tex + float2(-dx, 0.0);
   coords.c11 = tex + float2(0.0, 0.0);
   coords.c21 = tex + float2(dx, 0.0);
   coords.c31 = tex + float2(2.0 * dx, 0.0);
   coords.c02 = tex + float2(-dx, dy);
   coords.c12 = tex + float2(0.0, dy);
   coords.c22 = tex + float2(dx, dy);
   coords.c32 = tex + float2(2.0 * dx, dy);
   coords.mod_factor = tex.x * IN.output_size.x * IN.texture_size.x / IN.video_size.x;
   coords.ratio_scale = tex * IN.texture_size;
}

#define TEX2D(c) tex2D(s0 ,(c))
#define PI 3.141592653589
#define gamma 1.5

float4 main_fragment(in tex_coords co : TEXCOORD1, uniform input IN, uniform sampler2D s0 : TEXUNIT0) : COLOR
{
   float2 uv_ratio = frac(co.ratio_scale);
   float3 col, col2;

   float4x3 texes0 = float4x3(TEX2D(co.c01).xyz, TEX2D(co.c11).xyz, TEX2D(co.c21).xyz, TEX2D(co.c31).xyz);
   float4x3 texes1 = float4x3(TEX2D(co.c02).xyz, TEX2D(co.c12).xyz, TEX2D(co.c22).xyz, TEX2D(co.c32).xyz);

   float4 coeffs = float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x) + 0.005;
   coeffs = sin(PI * coeffs) * sin(0.5 * PI * coeffs) / (coeffs * coeffs);
   coeffs = coeffs / dot(coeffs, float(1.0));

   float3 weights = float3(3.33 * uv_ratio.y, 3.33 * uv_ratio.y, 3.33 * uv_ratio.y);
   float3 weights2 = float3(uv_ratio.y * -3.33 + 3.33, uv_ratio.y * -3.33 + 3.33, uv_ratio.y * -3.33 + 3.33);

   col = saturate(mul(coeffs, texes0));
   col2 = saturate(mul(coeffs, texes1));

   float3 wid = 2.0 * pow(col, float3(4.0, 4.0, 4.0)) + 2.0;
   float3 wid2 = 2.0 * pow(col2, float3(4.0, 4.0, 4.0)) + 2.0;

   col = pow(col, float3(gamma, gamma, gamma));
   col2 = pow(col2, float3(gamma, gamma, gamma));

   float3 sqrt1 = rsqrt(0.5 * wid);
   float3 sqrt2 = rsqrt(0.5 * wid2);

   float3 pow_mul1 = weights * sqrt1;
   float3 pow_mul2 = weights2 * sqrt2;

   float3 div1 = 0.1320 * wid + 0.392;
   float3 div2 = 0.1320 * wid2 + 0.392;

   float3 pow1 = -pow(pow_mul1, wid);
   float3 pow2 = -pow(pow_mul2, wid2);

   weights = exp(pow1) / div1;
   weights2 = exp(pow2) / div2;

   float3 multi = col * weights + col2 * weights2;
   float3 mcol = lerp(float3(1.0, 0.7, 1.0), float3(0.7, 1.0, 0.7), floor(fmod(co.mod_factor, 2.0)));

   return float4(pow(mcol * multi, float3(0.454545, 0.454545, 0.454545)), 1.0);
}
