#version 330 core

// To be used with fsquad vertex shader!
in FSQuadOutput
{
	vec2 p; //position [-1,1]
	vec2 t; //tex coords [0,1]
} inData;

out vec4 fragColor;

//previous eye/intermediate buffer
uniform sampler2D texPrevious;
//current slice (only used in FTB)
uniform sampler2D texSlice;
uniform bool bFTB;

//circle of confusion extents in texture space
uniform vec2 vCoC;

//the DOF blur shader
void main(void)
{
	vec4 blurred = vec4(0.0f,0.0f,0.0f,0.0f);

	//sample texture N times with offset
	blurred+=texture(texPrevious,inData.t + vCoC);
	blurred+=texture(texPrevious,inData.t + vec2(vCoC.x,-vCoC.y));
	blurred+=texture(texPrevious,inData.t + vec2(-vCoC.x,vCoC.y));
	blurred+=texture(texPrevious,inData.t - vCoC);
	blurred = blurred / 4.0f;

	if(bFTB)
	{
		//we have to perform manual blending
		vec4 slice = texture(texSlice, inData.t);
		float outAlpha = slice.a * (1.0 - blurred.a);
		fragColor = vec4( blurred.rgb + slice.rgb * outAlpha, blurred.a + (1-blurred.a)*slice.a);
	}
	else
	{
		//blending with current slice (which is already in the draw buffer) is performed by GPU
		fragColor = blurred;
	}
}