#pragma parameter SHARPNESS "CRT - Sharpness Hack" 1.0 1.0 5.0 1.0
#pragma parameter CRT_ANTI_RINGING "CRT - Anti-Ringing" 0.8 0.0 1.0 0.1
#pragma parameter InputGamma "CRT - Input gamma" 2.5 0.0 5.0 0.1
#ifdef PARAMETER_UNIFORM
uniform float SHARPNESS;
uniform float CRT_ANTI_RINGING;
uniform float InputGamma;
#else
#define SHARPNESS 1.0
#define CRT_ANTI_RINGING 0.8 
#define InputGamma 2.5
#endif
// END PARAMETERS //

/* COMPATIBILITY
   - HLSL compilers
   - Cg   compilers
*/

/*
   Hyllian's CRT Shader - pass0
  
   Copyright (C) 2011-2016 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.

*/

#define GAMMA_IN(color)     pow(color, float3(InputGamma, InputGamma, InputGamma))

// Horizontal cubic filter.

// Some known filters use these values:

//    B = 0.0, C = 0.0  =>  Hermite cubic filter.
//    B = 1.0, C = 0.0  =>  Cubic B-Spline filter.
//    B = 0.0, C = 0.5  =>  Catmull-Rom Spline filter. This is the default used in this shader.
//    B = C = 1.0/3.0   =>  Mitchell-Netravali cubic filter.
//    B = 0.3782, C = 0.3109  =>  Robidoux filter.
//    B = 0.2620, C = 0.3690  =>  Robidoux Sharp filter.
//    B = 0.36, C = 0.28  =>  My best config for ringing elimination in pixel art (Hyllian).


// For more info, see: http://www.imagemagick.org/Usage/img_diagrams/cubic_survey.gif

// Change these params to configure the horizontal filter.
const static float  B =  0.0; 
const static float  C =  0.5;  

const static float4x4 invX = float4x4(            (-B - 6.0*C)/6.0,         (3.0*B + 12.0*C)/6.0,     (-3.0*B - 6.0*C)/6.0,             B/6.0,
                                        (12.0 - 9.0*B - 6.0*C)/6.0, (-18.0 + 12.0*B + 6.0*C)/6.0,                      0.0, (6.0 - 2.0*B)/6.0,
                                       -(12.0 - 9.0*B - 6.0*C)/6.0, (18.0 - 15.0*B - 12.0*C)/6.0,      (3.0*B + 6.0*C)/6.0,             B/6.0,
                                                   (B + 6.0*C)/6.0,                           -C,                      0.0,               0.0);


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


struct out_vertex {
    float2 texCoord : TEXCOORD0;
};

/*    VERTEX_SHADER    */
out_vertex main_vertex
(
    float4 position      : POSITION,
    out float4 oPosition : POSITION,
    float2 texCoord      : TEXCOORD0,

    uniform float4x4 modelViewProj,
    uniform input IN
)
{
    oPosition = mul(modelViewProj, position);

    out_vertex OUT = {
        texCoord
    };

    return OUT;
}


float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
{
    float2 TextureSize = float2(SHARPNESS*IN.texture_size.x, IN.texture_size.y);

    float3 color;
    float2 dx = float2(1.0/TextureSize.x, 0.0);
    float2 dy = float2(0.0, 1.0/TextureSize.y);
    float2 pix_coord = VAR.texCoord*TextureSize+float2(-0.5,0.0);

    float2 tc = (floor(pix_coord)+float2(0.5,0.0))/TextureSize;

    float2 fp = frac(pix_coord);

    float3 c10 = GAMMA_IN(tex2D(s_p, tc     - dx).xyz);
    float3 c11 = GAMMA_IN(tex2D(s_p, tc         ).xyz);
    float3 c12 = GAMMA_IN(tex2D(s_p, tc     + dx).xyz);
    float3 c13 = GAMMA_IN(tex2D(s_p, tc + 2.0*dx).xyz);

    //  Get min/max samples
    float3 min_sample = min(c11,c12);
    float3 max_sample = max(c11,c12);

    float4x3 color_matrix = float4x3(c10, c11, c12, c13);

    float4 invX_Px  = mul(invX, float4(fp.x*fp.x*fp.x, fp.x*fp.x, fp.x, 1.0));
    color           = mul(invX_Px, color_matrix);

    // Anti-ringing
    float3 aux = color;
    color = clamp(color, min_sample, max_sample);
    color = lerp(aux, color, CRT_ANTI_RINGING);

    return float4(color, 1.0);
}

