/*
   GBC Color Correction Shader
   A shader that replicates the LCD dynamics from a Game Boy Color
   Color values are derived from Gambatte's color correction implementation.

   Based on Color Mangler
   Author: hunterk
   License: Public domain
*/

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

#pragma parameter darken_screen "Darken Screen" 0.0 -0.25 2.0 0.05
#ifdef PARAMETER_UNIFORM
uniform float darken_screen;
#else
#define darken_screen 0.0
#endif

#define target_gamma 2.2
#define display_gamma 2.2
#define blr 0.0
#define blg 0.0
#define blb 0.0
#define r 0.78824
#define g 0.72941
#define b 0.82
#define rg 0.025
#define rb 0.12039
#define gr 0.12157
#define gb 0.12157
#define br 0.0
#define bg 0.275000

struct out_vertex
{
     float4 position : COMPAT_POS;
     float2 texCoord : TEXCOORD;
#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
     OUT.position = mul(modelViewProj, position);
     OUT.texCoord = texCoord;

     return OUT;
}

float4 gbc_color(float2 texCoord : TEXCOORD)
{
     float4 screen = pow(COMPAT_SamplePoint(decal, texCoord), target_gamma + darken_screen).rgba;
     //                r    g     b    black
     float4x4 color = {r,   gr,   br,  blr,   //red channel
                       rg,  g,    bg,  blg,   //green channel
                       rb,  gb,   b,   blb,   //blue channel
                       0.0, 0.0,  0.0, 1.0};  //alpha channel; these numbers do nothing for our purposes.
                  
     screen = mul(color, screen);
     return pow(screen, 1.0 / display_gamma);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
     return gbc_color(VOUT.texCoord);
}
COMPAT_END
