//=============================================================================
//// Shader uses position and texture
//=============================================================================
SamplerState samLin
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Mirror;
    AddressV = Mirror;
};

Texture2D gTexture;

/// Create Depth Stencil State (ENABLE DEPTH WRITING)
DepthStencilState stencilState
{
    DepthEnable = true;
};
/// Create Rasterizer State (Backface culling) 
RasterizerState backCulling
{
    CullMode = BACK;
};

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

};

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


//VERTEX SHADER
//-------------
PS_INPUT VS(VS_INPUT input)
{
	PS_INPUT output = (PS_INPUT)0;
    output.Position = float4(input.Position.xy, 0, 1);
    output.TexCoord = input.TexCoord;
	
	return output;
}


//PIXEL SHADER
//------------
float4 PS(PS_INPUT input): SV_Target
{
    int stride = 1;
	// Step 1: find the dimensions of the texture (the texture has a method for that)
    float width, height;
    gTexture.GetDimensions(width, height);
	// Step 2: calculate dx and dy (UV space for 1 pixel)
    float dx = 1 / width;
    float dy = 1 / height;
	// Step 3: Create a double for loop (5 iterations each)
	//		   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
    float4 sum = 0;
    for (int i = -2; i <= 2; ++i)
    {
        for (int j = -2; j <= 2; ++j)
        {
            float x = input.TexCoord.x + dx * i * stride;
            float y = input.TexCoord.y + dy * j * stride;
            sum += gTexture.Sample(samLin, float2(x, y));
        }
    }
	// Step 4: Divide the final color by the number of passes (in this case 5*5)
    sum /= 25;
	// Step 5: return the final color
    return float4(sum.xyz, 1);

}


//TECHNIQUE
//---------
technique11 Blur
{
    pass P0
    {
		// Set states...
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_4_0, PS()));

        SetRasterizerState(backCulling);
        SetDepthStencilState(stencilState, 0);
    }
}