uniform sampler2D tex;
uniform float bsize;

// Gaussian Blur
// 512x512 and smaller textures give best results
// 2 passes required
void main() {

	vec2 texcoord = vec2(gl_TexCoord[0]);
	vec4 sum = vec4(0.0);
	float blurSize = 1.0/bsize;

	// 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
	int PixelKernel[13];
	PixelKernel[0] = -6;
	PixelKernel[1] = -5;
	PixelKernel[2] = -4;
	PixelKernel[3] = -3;
	PixelKernel[4] = -2;
	PixelKernel[5] = -1;
	PixelKernel[6] =  0;
	PixelKernel[7] =  1;
	PixelKernel[8] =  2;
	PixelKernel[9] =  3;
	PixelKernel[10] =  4;
	PixelKernel[11] =  5;
	PixelKernel[12] =  6;

	// 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[13];
	BlurWeights[0] = 0.002216;
	BlurWeights[1] = 0.008764;
	BlurWeights[2] = 0.026995;
	BlurWeights[3] = 0.064759;
	BlurWeights[4] = 0.120985;
	BlurWeights[5] = 0.176033;
	BlurWeights[6] = 0.199471;
	BlurWeights[7] = 0.176033;
	BlurWeights[8] = 0.120985;
	BlurWeights[9] = 0.064759;
	BlurWeights[10] = 0.026995;
	BlurWeights[11] = 0.008764;
	BlurWeights[12] = 0.002216;

#ifdef PASS_0
    for (int i = 0; i < 13; i++) {
        sum += texture2D(tex, vec2(clamp(texcoord.x + float(PixelKernel[i]) * blurSize, 0.0, 1.0), texcoord.y)) * BlurWeights[i];
    }
#endif

#ifdef PASS_1
    for (int i = 0; i < 13; i++) {
        sum += texture2D(tex, vec2(texcoord.x, clamp(texcoord.y + float(PixelKernel[i]) * blurSize, 0.0, 1.0))) * BlurWeights[i];
    }
#endif

	gl_FragColor = sum;
}