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

******************************************************************************/
//#include "..\mrt.fx"

float4x4 worldViewProj : WorldViewProjection;
float4x4 Proj : Projection;
float3 campos : CAMERAPOS;

#define MRT_FORCE_BLOOM             2.0f
#define MRT_FORCE_BLOOM_NO_FOG      0.0f


float  ImageWidth;
float  ImageHeight;
float  BrightThreshold;
float  BrightOffset;

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

texture DistanceTexture: DistanceTexture;
sampler DistanceTextureSampler = sampler_state 
{
    texture = <DistanceTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = 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;
}


float4 PS_LDR_MRT( vertexOutput IN): COLOR
{	
	float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		  
	float4 cameraToPixel = tex2D( DistanceTextureSampler, IN.texCoordDiffuse );
	
	//if( cameraToPixel.w == MRT_FORCE_BLOOM_NO_FOG ||  sampled.x > BrightThreshold || sampled.y > BrightThreshold || sampled.z > BrightThreshold )
	if( sampled.x > BrightThreshold || sampled.y > BrightThreshold || sampled.z > BrightThreshold )
	{	
	    return float4( sampled + BrightOffset, 1 );
	}
    else
		return float4( 0,0,0, 1 );
		
}

float4 PS_LDR_NoMRT( vertexOutput IN): COLOR
{	
	float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		  
	if( sampled.x > BrightThreshold || sampled.y > BrightThreshold || sampled.z > BrightThreshold )
	{	
	    return float4( sampled + BrightOffset, 1 );
	}
    else
		return float4( 0,0,0, 1 );
		
}


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

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