out vec4 fragOut0;

in vec4 fragTexCoord;

uniform sampler2D tex;

const float TapSize = 1.0;

layout (std140) uniform genericData {
	float texSize;
	int level;
};

// Gaussian Blur
// 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 = textureLod(tex, fragTexCoord.xy, float(level)) * BlurWeights[0];
#ifdef PASS_0
	for (int i = 1; i < 6; i++) {
		sum += textureLod(tex, vec2(clamp(fragTexCoord.x - float(i) * (texSize) * TapSize, 0.0, 1.0), fragTexCoord.y), float(level)) * BlurWeights[i];
		sum += textureLod(tex, vec2(clamp(fragTexCoord.x + float(i) * (texSize) * TapSize, 0.0, 1.0), fragTexCoord.y), float(level)) * BlurWeights[i];
	}
#endif
#ifdef PASS_1
	for (int i = 1; i < 6; i++) {
		sum += textureLod(tex, vec2(fragTexCoord.x, clamp(fragTexCoord.y - float(i) * (texSize) * TapSize, 0.0, 1.0)), float(level)) * BlurWeights[i];
		sum += textureLod(tex, vec2(fragTexCoord.x, clamp(fragTexCoord.y + float(i) * (texSize) * TapSize, 0.0, 1.0)), float(level)) * BlurWeights[i];
	}
#endif
	fragOut0 = sum;
}
