//=============================================================================
//// Shader uses position and texture
//=============================================================================
SamplerState samPoint
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Mirror;
    AddressV = Mirror;
};

Texture2D gTexture;

/// Create Depth Stencil State (ENABLE DEPTH WRITING)
/// Create Rasterizer State (Backface culling) 


//IN/OUT STRUCTS
//--------------
struct VS_INPUT
{
    float3 Position : POSITION;
	float2 TexCoord : TEXCOORD0;

};

struct PS_INPUT
{
    float4 Position : SV_POSITION;
	float2 TexCoord : TEXCOORD1;
};

DepthStencilState EnableDepth
{
	DepthEnable = TRUE;
	DepthWriteMask = ALL;
};

RasterizerState BackfaceCulling
{
	CullMode = BACK;
};


//VERTEX SHADER
//-------------
PS_INPUT VS(VS_INPUT input)
{
	PS_INPUT output = (PS_INPUT)0;
	// Set the Position
	output.Position = float4(input.Position, 1.0f);
	// Set the TexCoord
	output.TexCoord = input.TexCoord;
	return output;
}


//PIXEL SHADER
//------------
float4 PS(PS_INPUT input): SV_Target
{
	// Step 1: find the dimensions of the texture (the texture has a method for that)
	int width;
	int height;
	gTexture.GetDimensions(width, height);
	// Step 2: calculate dx and dy	
	float dx = 1.0f / width;
	float dy = 1.0f / height;
	// Step 3: Create a double for loop (5 iterations each)
	float3 color = gTexture.Sample(samPoint, input.TexCoord);
	for(int x = -2; x <= 2; ++x)
	{
		for(int y = -2; y <= 2; ++y)
		{
			color += gTexture.Sample(samPoint, input.TexCoord + float2(x * dx, y * dy));
		}
	}
	//Inside the loop, calculate the offset in each direction. Make sure not to take every pixel but move by 2 pixels each time
	//Do a texture lookup using your previously calculated uv coordinates + the offset, and add to the final color
	// Step 4: Divide the final color by the number of passes (in this case 5*5)
	// Step 5: return the final color

	return float4(color / 25, 1.0f);
}


//TECHNIQUE
//---------
technique11 Blur
{
    pass P0
    {
		SetRasterizerState(BackfaceCulling);
		SetDepthStencilState(EnableDepth, 0);
		
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}