/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
   ShadX's Natural Vision Shader

   Ported and tweaked by Hyllian - 2016
   parameterized by Sp00kyFox

*/


#pragma parameter GIN	"NaturalVision Gamma In"	2.2 0.0 10.0 0.05
#pragma parameter GOUT	"NaturalVision Gamma Out"	2.2 0.0 10.0 0.05
#pragma parameter Y	"NaturalVision Luminance"	1.1 0.0 10.0 0.01
#pragma parameter I	"NaturalVision Orange-Cyan"	1.1 0.0 10.0 0.01
#pragma parameter Q	"NaturalVision Magenta-Green"	1.1 0.0 10.0 0.01

#ifdef PARAMETER_UNIFORM
	uniform float GIN, GOUT, Y, I, Q;
#else
	#define GIN	2.2
	#define GOUT	2.2
	#define Y	1.1
	#define I	1.1
	#define Q	1.1	
#endif


const static float3x3 RGBtoYIQ = float3x3(0.299,     0.587,     0.114,
					  0.595716, -0.274453, -0.321263,
					  0.211456, -0.522591,  0.311135);

const static float3x3 YIQtoRGB = float3x3(1,  0.95629572,  0.62102442,
					  1, -0.27212210, -0.64738060,
					  1, -1.10698902,  1.70461500);

const static float3 YIQ_lo = float3(0, -0.595716, -0.522591);
const static float3 YIQ_hi = float3(1,  0.595716,  0.522591);

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 texCoord1 : TEXCOORD0,

   	uniform float4x4 modelViewProj,
	uniform input IN
)
{

   oPosition = mul(modelViewProj, position);

	// This line fix a bug in ATI cards.
	float2 tex = texCoord1 + float2(0.0000001, 0.0000001);

	out_vertex OUT = { 
		tex
	};

	return OUT;
}


float3 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
{
	float3 c = tex2D(s_p, VAR.texCoord).xyz;

	c = pow(c, GIN);
	c = mul(RGBtoYIQ, c);
	c = float3(pow(c.x,Y), c.y*I, c.z*Q);
	c = clamp(c, YIQ_lo, YIQ_hi);
	c = mul(YIQtoRGB, c);
	c = pow(c, 1.0/GOUT);

	return c;
}
