// ================ //
// ================ //
// CUSTOM SETTINGS  //
// ================ //
// ================ //

// Levels
float BlackPoint = 0.01;
float WhitePoint = 1.00;
float Gamma      = 0.88;
float Saturation = 0.97;
float Exposure       = 1.00;
float Contrast       = 1.00;
float ContrastPivot  = 0.18;

// Tint
float TintStrength   = 1.00;

// Red
float MatR_R = 1.00;
float MatR_G = 0.00;
float MatR_B = 0.00;
// Green
float MatG_R = 0.02;
float MatG_G = 1.01;
float MatG_B = 0.00;
// Blue
float MatB_R = 0.01;
float MatB_G = 0.02;
float MatB_B = 1.03;

// ================ //
// ================ //


int Blend[5] : BLENDSTATE;
int BaseMinTextureFilter : BASEMINTEXTUREFILTER;
int BaseMagTextureFilter : BASEMAGTEXTUREFILTER;
texture diffusemap : DIFFUSETEX;
texture gainmap : GAINMAP; 
texture rampmap : RAMPMAP;
float FilterBlend : FILTERBLEND;
float4 ScreenOffset : SCREENOFFSET;

sampler samp =
sampler_state
{
    Texture = <diffusemap>;
    AddressU = 1;
    AddressV = 1;
    MipFilter = 0;
    MinFilter = 2;
    MagFilter = 2;
};
sampler samp_gainmap =
sampler_state
{
    Texture = <gainmap>;
    AddressU = 3;
    AddressV = 3;
    MipFilter = 0;
    MinFilter = 2;
    MagFilter = 2;
};
sampler samp_rampmap =
sampler_state
{
    Texture = <rampmap>; 
    AddressU = 3;
    AddressV = 3;
    MipFilter = 0;
    MinFilter = 2;
    MagFilter = 2;
};

float4 PixelShader1(): POSITION;
float4 VertexShader2(): POSITION;
float4 PixelShader8(): POSITION;
float4 VertexShader9(): POSITION;
float4 PixelShader15(): POSITION;
float4 VertexShader16(): POSITION;
float4 PixelShader22(): POSITION;
float4 VertexShader23(): POSITION;
float4 PixelShader29(): POSITION;
float4 VertexShader30(): POSITION;

struct VS_OUTPUT {
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};

VS_OUTPUT VS_Levels(float4 Pos : POSITION, float2 Tex : TEXCOORD0)
{
    VS_OUTPUT Out;
    Out.Pos = Pos;
    Out.Pos.xy += ScreenOffset.xy; 
    Out.Tex = Tex;
    return Out;
}
float4 PS_Levels(float2 Tex : TEXCOORD0) : COLOR0
{
    float4 Color = tex2D(samp, Tex);
    Color.rgb = max(0.0, Color.rgb - BlackPoint);
    Color.rgb = Color.rgb / max(0.001, (WhitePoint - BlackPoint));
    Color.rgb = pow(abs(Color.rgb), 1.0 / Gamma);
    Color.rgb *= Exposure;
    Color.rgb = (Color.rgb - ContrastPivot) * Contrast + ContrastPivot;
    float lum = Color.r * 0.299 + Color.g * 0.587 + Color.b * 0.114;
    Color.rgb = lerp(lum.xxx, Color.rgb, Saturation);
    float3 matColor;
    matColor.r = Color.r * MatR_R + Color.g * MatR_G + Color.b * MatR_B;
    matColor.g = Color.r * MatG_R + Color.g * MatG_G + Color.b * MatG_B;
    matColor.b = Color.r * MatB_R + Color.g * MatB_G + Color.b * MatB_B;
    Color.rgb = lerp(Color.rgb, matColor, TintStrength);
    return Color;
}


technique filter <int shader = 1;>
{
    pass p0
    {
        FogEnable = 0;
        ColorWriteEnable = 15;
        AlphaTestEnable = <Blend[0]>; 
        AlphaRef = <Blend[1]>;
        AlphaBlendEnable = <Blend[2]>;
        SrcBlend = <Blend[3]>;
        DestBlend = <Blend[4]>;
        CullMode = 1;
        ZEnable = 0;
        ZWriteEnable = 0;
        
        VertexShader = compile vs_2_0 VS_Levels(); 
        PixelShader = compile ps_2_0 PS_Levels();
    }
    pass p1
    {
        FogEnable = 0;
        ColorWriteEnable = 15;
        AlphaTestEnable = <Blend[0]>;
        AlphaRef = <Blend[1]>;
        AlphaBlendEnable = <Blend[2]>;
        SrcBlend = <Blend[3]>;
        DestBlend = <Blend[4]>;
        CullMode = 1;
        ZEnable = 0;
        ZWriteEnable = 0;
        VertexShader = compile vs_2_0 VS_Levels(); 
        PixelShader = compile ps_2_0 PS_Levels();
    }
    pass p2
    {
        FogEnable = 0;
        ColorWriteEnable = 15;
        AlphaTestEnable = <Blend[0]>;
        AlphaRef = <Blend[1]>;
        AlphaBlendEnable = <Blend[2]>;
        SrcBlend = <Blend[3]>;
        DestBlend = <Blend[4]>;
        CullMode = 1;
        ZEnable = 0;
        ZWriteEnable = 0;
        VertexShader = compile vs_2_0 VS_Levels(); 
        PixelShader = compile ps_2_0 PS_Levels();
    }
}

technique filter <int shader = 1;>
{
    pass p0
    {
        FogEnable = 0;
        ColorWriteEnable = 15;
        AlphaTestEnable = <Blend[0]>; 
        AlphaRef = <Blend[1]>;
        AlphaBlendEnable = <Blend[2]>;
        SrcBlend = <Blend[3]>;
        DestBlend = <Blend[4]>;
        CullMode = 1;
        ZEnable = 0;
        ZWriteEnable = 0;
        VertexShader = compile vs_2_0 VS_Levels(); 
        PixelShader = compile ps_2_0 PS_Levels();
    }
    pass p1
    {
        FogEnable = 0;
        ColorWriteEnable = 15;
        AlphaTestEnable = <Blend[0]>;
        AlphaRef = <Blend[1]>;
        AlphaBlendEnable = <Blend[2]>;
        SrcBlend = <Blend[3]>;
        DestBlend = <Blend[4]>;
        CullMode = 1;
        ZEnable = 0;
        ZWriteEnable = 0;
        VertexShader = compile vs_2_0 VS_Levels(); 
        PixelShader = compile ps_2_0 PS_Levels();
    }
}