float2 g_Viewport;

Texture2D g_MRT0;

const float3 luminanceFilter = { 0.2989, 0.5866, 0.1145 };


SamplerState samPoint
{
    Filter = MIN_MAG_MIP_POINT;
    
    AddressU = Wrap;
    AddressV = Wrap;
};

SamplerState samLinear
{
    Filter = MIN_MAG_MIP_LINEAR;
    
    AddressU = Wrap;
    AddressV = Wrap;
};

SamplerState samLinearClamp
{
    Filter = MIN_MAG_MIP_LINEAR;
    
    AddressU = Clamp;
    AddressV = Clamp;
};


SamplerState samAniso
{
    Filter = ANISOTROPIC;
    MaxAnisotropy = 16;
    
    AddressU = Wrap;
    AddressV = Wrap;
};

SamplerState samAnisoClamp
{
    Filter = ANISOTROPIC;
    MaxAnisotropy = 16;
    
    AddressU = Clamp;
    AddressV = Clamp;
};

BlendState AlphaBlending
{
    BlendEnable[0] = TRUE;
    SrcBlend = ONE;
    DestBlend = ONE;
    BlendOp = ADD;
    RenderTargetWriteMask[0] = 0x0F;
};

BlendState DisableBlend 
{ 
	BlendEnable[0] = FALSE; 
};

DepthStencilState DepthEnabling 
{ 
	DepthEnable = TRUE; 
};

DepthStencilState DepthDisabling
{
	DepthEnable = FALSE;
};

RasterizerState DisableCulling
{
	CullMode = NONE;
};


struct VS_INPUT
{
    float4 Pos  : POSITION;
    float3 Norm : NORMAL;
    float3 Tang : TANGENT; 
    float2 Tex  : TEXCOORD;
};

struct PS_INPUT
{
    float4 Pos  : SV_Position;
    float2 Tex0  : TEXCOORD0;
    float2 Tex1  : TEXCOORD1;
    float2 Tex2  : TEXCOORD2;
    float2 Tex3  : TEXCOORD3;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT BRIGHT_PASS_VS( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;

    output.Pos = float4(2.0*input.Pos.x-1.0, 1.0-2.0*input.Pos.y, 0.0, 1.0);
    
    float2 texelSize = float2(0.25 / g_Viewport.x, 0.25 / g_Viewport.y);
    
    output.Tex0 = input.Tex + float2(0.0, 0.0) * texelSize;
	output.Tex1 = input.Tex + float2(0.0, 2.0) * texelSize;
	output.Tex2 = input.Tex + float2(2.0, 2.0) * texelSize;
	output.Tex3 = input.Tex + float2(2.0, 0.0) * texelSize;

    return output;
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------

float4 BRIGHT_PASS_PS( PS_INPUT input ) : SV_Target
{
	float4 sample = (float4)0;
	
	sample += g_MRT0.SampleLevel(samLinear, input.Tex0, 0);
	sample += g_MRT0.SampleLevel(samLinear, input.Tex1, 0);
	sample += g_MRT0.SampleLevel(samLinear, input.Tex2, 0);
	sample += g_MRT0.SampleLevel(samLinear, input.Tex3, 0);
		
	sample *= 0.25;
	
	float luminance = dot(sample.rgb, luminanceFilter);
	
	if (luminance < 1.0)
		sample.rgb = 0;
		
	return sample;
}


//--------------------------------------------------------------------------------------
technique10 Render
{
    pass P0
    {
		SetRasterizerState( DisableCulling );
		SetBlendState( DisableBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
		SetDepthStencilState( DepthDisabling, 0 );
		
        SetVertexShader( CompileShader( vs_4_0, BRIGHT_PASS_VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, BRIGHT_PASS_PS() ) );
    }
}