/*********************************************************************NVMH3****

******************************************************************************/

float4x4 worldViewProj : WorldViewProjection;
float4x4 Proj : Projection;

float3 campos : CAMERAPOS;

float  BloomFactor;
float  Exposure;
float  MiddleGray;
float  LumWhiteSquared;
float  AdaptedTonemapWeight;
float  HDRBlend;

texture diffuseTexture : DiffuseTexture;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};


texture BlurTexture : BlurTexture;
sampler BlurTextureSampler = sampler_state 
{
    texture = <BlurTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};


texture LuminanceTexture : LuminanceTexture;
sampler LuminanceTextureSampler = sampler_state 
{
   texture = <LuminanceTexture>;
   AddressU  = Clamp;
   AddressV  = Clamp;
   MinFilter = POINT;
   MagFilter = POINT;
   MipFilter = POINT;
};

struct vertexInput {
    float4 position				: POSITION;
    float2 texCoordDiffuse		: TEXCOORD0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
};


vertexOutput VS_Screen(vertexInput IN) 
{
    vertexOutput OUT;
    OUT.hPosition = mul( IN.position, Proj );    
    OUT.texCoordDiffuse = IN.texCoordDiffuse;
    return OUT;
}


//-----------------------------------
// The following is from the DX9 sample, HDR pipeline
float4 PS_Adaptive( vertexOutput IN): COLOR
{	
	float4 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
	float4 blurred = tex2D( BlurTextureSampler, IN.texCoordDiffuse );  
		
	// Read the luminance value, target the centre of the texture
    // which will map to the only pixel in it!
	float AdaptedLum = tex2D( LuminanceTextureSampler, float2( 0.5f, 0.5f ) ).y;
	//float AdaptedMax = tex2D( LuminanceTextureSampler, float2( 0.5f, 0.5f ) ).x;
	
	float4 outcolor = sampled;
	
	//float fLumSq = lerp( 0.7f*0.7f, AdaptedMax*AdaptedMax, AdaptedTonemapWeight );
	
	AdaptedLum = lerp( 0.7f, AdaptedLum, AdaptedTonemapWeight );
	
	outcolor.rgb *= MiddleGray / (AdaptedLum + 0.00001f);
	
	//outcolor.rgb *= (1.0f + outcolor / fLumSq);
	outcolor.rgb *= 1.0f + (outcolor / 8.0f);
    outcolor.rgb *= 1.0f + outcolor;
	outcolor.rgb /= 1.0f + outcolor;
	
	outcolor += (Exposure/12.0f) * blurred;
	
	outcolor = lerp( sampled, outcolor, HDRBlend );
	
	//outcolor = (Exposure/12.0f) * blurred;
    return outcolor;
}



//-----------------------------------
// The following is from the DX9 sample, HDR pipeline
float4 PS_Static( vertexOutput IN): COLOR
{	
	float4 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
	float4 blurred = tex2D( BlurTextureSampler, IN.texCoordDiffuse );  
		
	// Read the luminance value, target the centre of the texture
    // which will map to the only pixel in it!
	//float AdaptedLum = tex2D( LuminanceTextureSampler, float2( 0.5f, 0.5f ) ).y;
	//float AdaptedMax = tex2D( LuminanceTextureSampler, float2( 0.5f, 0.5f ) ).x;
	
	float4 outcolor = sampled;
		
	//AdaptedLum = lerp( 0.7f, AdaptedLum, AdaptedTonemapWeight );
	float AdaptedLum = 0.7f;

	//float fLumSq = AdaptedLum*AdaptedLum;

	outcolor.rgb *= MiddleGray / (AdaptedLum + 0.00001f);
	//outcolor.rgb *= (1.0f + outcolor / fLumSq);
	outcolor.rgb *= (1.0f + outcolor / 8.0f);
    outcolor.rgb *= (1.0f + outcolor);
	outcolor.rgb /= (1.0f + outcolor);
	
	outcolor += (Exposure/12.0f) * blurred;

	outcolor = lerp( sampled, outcolor, HDRBlend );

	//outcolor = (Exposure/12.0f) * blurred;;
    return outcolor;
}



//-----------------------------------
technique adaptiveTonemap
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_Adaptive();
		cullmode = none;
    }
}

technique staticTonemap
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_Static();
		cullmode = none;
    }
}

