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

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/

#pragma parameter display_gamma "Display Gamma" 2.2 0.0 10.0 0.1
#pragma parameter target_gamma "Target Gamma" 2.2 0.0 10.0 0.1
#pragma parameter sat "Saturation" 1.0 0.0 3.0 0.01
#pragma parameter lum "Luminance" 1.0 0.0 5.0 0.01
#pragma parameter cntrst "Contrast" 1.0 0.0 2.0 0.01
#pragma parameter r "Red" 1.0 0.0 2.0 0.01
#pragma parameter g "Green" 1.0 0.0 2.0 0.01
#pragma parameter b "Blue" 1.0 0.0 2.0 0.01
#pragma parameter rg "Red-Green Tint" 0.0 0.0 1.0 0.005
#pragma parameter rb "Red-Blue Tint" 0.0 0.0 1.0 0.005
#pragma parameter gr "Green-Red Tint" 0.0 0.0 1.0 0.005
#pragma parameter gb "Green-Blue Tint" 0.0 0.0 1.0 0.005
#pragma parameter br "Blue-Red Tint" 0.0 0.0 1.0 0.005
#pragma parameter bg "Blue-Green Tint" 0.0 0.0 1.0 0.005
#pragma parameter blr "Black-Red Tint" 0.0 0.0 1.0 0.005
#pragma parameter blg "Black-Green Tint" 0.0 0.0 1.0 0.005
#pragma parameter blb "Black-Blue Tint" 0.0 0.0 1.0 0.005
#ifdef PARAMETER_UNIFORM
uniform float display_gamma;
uniform float target_gamma;
uniform float sat;
uniform float lum;
uniform float cntrst;
uniform float blr;
uniform float blg;
uniform float blb;
uniform float r;
uniform float g;
uniform float b;
uniform float rg;
uniform float rb;
uniform float gr;
uniform float gb;
uniform float br;
uniform float bg;
#else
#define display_gamma 2.2
#define target_gamma 2.2
#define sat 1.0
#define lum 1.0
#define cntrst 1.0
#define blr 0.0
#define blg 0.0
#define blb 0.0
#define r 1.0
#define g 1.0
#define b 1.0
#define rg 0.0
#define rb 0.0
#define gr 0.0
#define gb 0.0
#define br 0.0
#define bg 0.0
#endif

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

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

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 color_mangler(float2 texCoord, COMPAT_Texture2D(decal))
{
	float4 screen = pow(COMPAT_Sample(decal, texCoord), float4(target_gamma, target_gamma, target_gamma, target_gamma)).rgba; //sample image in linear colorspace
	float4 avglum = float4(0.5, 0.5, 0.5, 0.5);
	screen = lerp(screen, avglum, (1.0 - cntrst));

	//					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.,	1.0}; //alpha channel; these numbers do nothing for our purposes.
			  
	float4x4 adjust = {	(1.0 - sat) * 0.3086 + sat,	(1.0 - sat) * 0.6094,		(1.0 - sat) * 0.0820,		0.0,
						(1.0 - sat) * 0.3086,		(1.0 - sat) * 0.6094 + sat, (1.0 - sat) * 0.0820,		0.0,
						(1.0 - sat) * 0.3086,		(1.0 - sat) * 0.6094,		(1.0 - sat) * 0.0820 + sat,	0.0,
						 1.0,						 1.0,						 1.0, 						1.0};

	color = mul(color, adjust);
	screen = saturate(screen * lum);
	screen = mul(color, screen);
	return pow(screen, float4(1.0 / display_gamma, 1.0 / display_gamma, 1.0 / display_gamma, 1.0 / display_gamma));
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return color_mangler(VOUT.texCoord, decal);
}
COMPAT_END