float2 g_Viewport;
float g_TextureHeight;
Texture2D g_MRT0;


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 Tex  : TEXCOORD0;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VBLUR_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);
    output.Tex = input.Tex;

    return output;
}


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

float4 VBLUR_PS( PS_INPUT input ) : SV_Target
{
	float4 total = g_MRT0.Sample(samLinearClamp, input.Tex);
	total += 0.895 * g_MRT0.Sample(samLinearClamp, input.Tex + float2(0.0, 1.0 / g_TextureHeight));
	total += 0.895 * g_MRT0.Sample(samLinearClamp, input.Tex + float2(0.0, -1.0 / g_TextureHeight));
	total += 0.641 * g_MRT0.Sample(samLinearClamp, input.Tex + float2(0.0, 2.0 / g_TextureHeight));
	total += 0.641 * g_MRT0.Sample(samLinearClamp, input.Tex + float2(0.0, -2.0 / g_TextureHeight));
	total += 0.368 * g_MRT0.Sample(samLinearClamp, input.Tex + float2(0.0, 3.0 / g_TextureHeight));
	total += 0.368 * g_MRT0.Sample(samLinearClamp, input.Tex + float2(0.0, -3.0 / g_TextureHeight));
	return total / 4.808;
}

/*
float4 VBLUR_PS( PS_INPUT input ) : SV_Target
{
	const float fStepSize = 1.0;
	const float fFilterWidth = 7.0;
	
    float fStartOffset = (fFilterWidth - 1.0f) / 2.0;
    float2 fTexelOffset = float2(0.0, fStepSize / g_TextureHeight);
    
    float2 fTexStart = input.Tex - (fStartOffset * fTexelOffset);
    float4 output = (float4)0;
    
    for (int i = 0; i < fFilterWidth; ++i)
        output += g_MRT0.Sample(samLinear, float2(fTexStart + fTexelOffset * i));
    
    return output / fFilterWidth;
}
*/

/*
float4 VBLUR_PS( PS_INPUT input ) : SV_Target
{
	const float blurSize = 1.0 / g_Viewport.y;
	float4 sum = (float4)0;
 
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y - 4.0*blurSize)) * 0.05;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y - 3.0*blurSize)) * 0.09;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y - 2.0*blurSize)) * 0.12;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y - blurSize)) * 0.15;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y)) * 0.16;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y + blurSize)) * 0.15;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y + 2.0*blurSize)) * 0.12;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y + 3.0*blurSize)) * 0.09;
	sum += g_MRT0.Sample(samLinearClamp, float2(input.Tex.x, input.Tex.y + 4.0*blurSize)) * 0.05;
 
	return sum;
}
*/
/*
float4 blur(float2 uv, float r, float4 center, float depth, inout float totalWeight)
{
    float4 sample = g_MRT0.Sample(samPoint, uv);
    float sampleDepth = sample.r;

    float diff = sampleDepth - depth;
    float weight = exp(-0.1 * r*r - 16.0 * diff*diff);
    totalWeight += weight;

    return weight * sample;
}


float4 VBLUR_PS( PS_INPUT input ) : SV_Target
{
	float4 sum = (float4)0;
	
   	float4 center = g_MRT0.Sample(samPoint, input.Tex);
	float depth = center.r;
   
	float totalWeight = 0.0;
    
	for (float r = -3.0; r <= 3.0; ++r) {
    	float2 uv = input.Tex + float2(0.0, r / g_Viewport.y);
    	sum += blur(uv, r, center, depth, totalWeight);	
	}

   	sum /= totalWeight;
   	
   	return sum;
}
*/

//--------------------------------------------------------------------------------------
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, VBLUR_VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, VBLUR_PS() ) );
    }
}