varying float blurSize;

uniform sampler2D tex;

#define BLUR_SIZE_DIV 3.0

// Gaussian Blur
// 512x512 and smaller textures give best results
// 2 passes required
void main()
{
	// Echelon9 - Due to Apple not implementing array constructors in OS X's 
	// GLSL implementation we need to setup the arrays this way as a workaround
	float BlurWeights[6];

	BlurWeights[5] = 0.0402;
	BlurWeights[4] = 0.0623;
	BlurWeights[3] = 0.0877;
	BlurWeights[2] = 0.1120;
	BlurWeights[1] = 0.1297;
	BlurWeights[0] = 0.1362;

	vec4 sum = texture2D(tex, gl_TexCoord[0].xy) * BlurWeights[0];

#ifdef PASS_0
	for (int i = 1; i < 6; i++) {
		sum += texture2D(tex, vec2(clamp(gl_TexCoord[0].x - float(i) * (blurSize/BLUR_SIZE_DIV), 0.0, 1.0), gl_TexCoord[0].y)) * BlurWeights[i];
		sum += texture2D(tex, vec2(clamp(gl_TexCoord[0].x + float(i) * (blurSize/BLUR_SIZE_DIV), 0.0, 1.0), gl_TexCoord[0].y)) * BlurWeights[i];
	}
#endif

#ifdef PASS_1
	for (int i = 1; i < 6; i++) {
		sum += texture2D(tex, vec2(gl_TexCoord[0].x, clamp(gl_TexCoord[0].y - float(i) * (blurSize/BLUR_SIZE_DIV), 0.0, 1.0))) * BlurWeights[i];
		sum += texture2D(tex, vec2(gl_TexCoord[0].x, clamp(gl_TexCoord[0].y + float(i) * (blurSize/BLUR_SIZE_DIV), 0.0, 1.0))) * BlurWeights[i];
	}
#endif

	gl_FragColor = sum;
}
