////////////////////////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "../../../types.txt"

////////////////////////////////////////////////////////////////////////////////////////////////////
// Structures
////////////////////////////////////////////////////////////////////////////////////////////////////

struct PS_INPUT
{
#if MIX_SM_HIGH
	float4 pos : SV_POSITION;
#endif //MIX_SM_HIGH
	float2 tex : TEXCOORD0;
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// Grobal values
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH

	cbuffer cbInput : register( b0 )
	{
		float4 g_Param0;
		float4 g_Param1;
	};

	Texture2D    g_ColorTexture : register( t0 );
	SamplerState g_ColorSampler : register( s0 );

#else //MIX_SM_HIGH

	float4 g_Param0 : register( c0 );
	float4 g_Param1 : register( c1 );

	sampler g_ColorSampler : register( s0 );

#endif //MIX_SM_HIGH

////////////////////////////////////////////////////////////////////////////////////////////////////
// Macros
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH
	#define TEX_COLOR( uv ) g_ColorTexture.Sample( g_ColorSampler, uv )
#else //MIX_SM_HIGH
	#define TEX_COLOR( uv ) tex2D( g_ColorSampler, uv )
#endif //MIX_SM_HIGH

#define FH_EXP_BIAS g_Param0.x
#define FH_A g_Param0.y
#define FH_B g_Param0.z
#define FH_C g_Param0.w
#define FH_D g_Param1.x
#define FH_E g_Param1.y
#define FH_F g_Param1.z
#define FH_W g_Param1.w

//static const float FH_EXP_BIAS = 2.0; //ExposureBias
//static const float FH_A = 0.22; //ShoulderStrength
//static const float FH_B = 0.30; //LinearStrength
//static const float FH_C = 0.10; //LinearAngle
//static const float FH_D = 0.20; //ToeStrength
//static const float FH_E = 0.01; //ToeNumerator
//static const float FH_F = 0.30; //ToeDenominator
//static const float FH_W = 11.2; //LinearWhitePointValue

////////////////////////////////////////////////////////////////////////////////////////////////////
// Functions - John Hable
////////////////////////////////////////////////////////////////////////////////////////////////////

float3 Tonemap( float3 x )
{
	return ( ( x * ( FH_A * x + FH_C * FH_B ) + FH_D * FH_E ) / ( x * ( FH_A * x + FH_B ) + FH_D * FH_F ) ) - FH_E / FH_F;
}

float4 main( PS_INPUT input ) : MSV_TARGET0
{
	float4 output = TEX_COLOR( input.tex );

	float3 current = Tonemap( FH_EXP_BIAS * output.rgb );
	float3 whiteScale = rcp( Tonemap( FH_W ) );

	output.rgb = current * whiteScale;
//	output.rgb = pow( abs( output.rgb ), 1.0 / 2.2 );

	return output;
}
