/*#############################################
#    _   ___ _____ ___    _   _    _____  __  #
#   /_\ / __|_   _| _ \  /_\ | |  | __\ \/ /  #
#  / _ \\__ \ | | |   / / _ \| |__| _| >  <   #
# /_/ \_\___/ |_| |_|_\/_/ \_\____|___/_/\_\  #
#											  #
#############################################*/

//Settings//////////////////Settings///////////////////Settings///////////////////Settings//

#include "/lib/util/functions.glsl"

#include "/settings/globalSettings.glsl"

//Varying////////////////////Varying////////////////////Varying////////////////////Varying//
varying vec2 texCoord;

//Vertex Shader////////////Vertex Shader////////////Vertex Shader////////////Vertex Shader//
#ifdef VERTEX

	//Program//
	void main(){
		texCoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;

		gl_Position=ftransform();
	}

#endif

//Fragment Shader//////////Fragment Shader/////////Fragment Shader/////////Fragment Shader//
#ifdef FRAGMENT

	//Uniforms//
	uniform float viewWidth, viewHeight, aspectRatio;
	uniform int frameCounter;
	uniform sampler2D colortex0;
	uniform sampler2D noisetex;

	//Optifine Constants//
	const bool colortex0MipmapEnabled = true;

	//Common Variables//
	float ph = 0.8 / min(360.0, viewHeight);
	float pw = ph / aspectRatio;
	vec2 view = vec2(1.0 / viewWidth, 1.0 / viewHeight);

	const float weight[6] = float[6](0.0556, 0.1667, 0.2777, 0.2777, 0.1667, 0.0556);

	//Common Functions//

	vec3 BloomTile(float lod, vec2 coord, vec2 offset) {
	vec3 bloom = vec3(0.0), temp = vec3(0.0);
	float scale = exp2(lod);
	coord = (coord - offset) * scale;
	vec2 padding = vec2(0.5) + 2.0 * view * scale;

	if (abs(coord.x - 0.5) < padding.x && abs(coord.y - 0.5) < padding.y) {
		for(int i = 0; i < 6; i++) {
			for(int j = 0; j < 6; j++) {
				float wg = weight[i] * weight[j];
				vec2 pixelOffset = vec2((float(i) - 2.5) * pw, (float(j) - 2.5) * ph);
				vec2 sampleCoord = coord + pixelOffset * scale;
				bloom += texture2D(colortex0, sampleCoord).rgb * wg;
			}
		}
	}

	return pow(bloom / 32.0, vec3(0.25));
	}

	#include "/lib/util/dither.glsl"

	//Program//
	void main(){

		vec2 bloomCoord = texCoord * viewHeight * 0.8 / min(360.0, viewHeight);
		vec3 blur =  BloomTile(1.0, bloomCoord, vec2(0.0      , 0.0   ));
			 blur += BloomTile(2.0, bloomCoord, vec2(0.50     , 0.0   ) + vec2( 4.0, 0.0) * view);
			 blur += BloomTile(3.0, bloomCoord, vec2(0.50     , 0.25  ) + vec2( 4.0, 4.0) * view);
			 blur += BloomTile(4.0, bloomCoord, vec2(0.625    , 0.25  ) + vec2( 8.0, 4.0) * view);
			 blur += BloomTile(5.0, bloomCoord, vec2(0.6875   , 0.25  ) + vec2(12.0, 4.0) * view);
			 blur += BloomTile(6.0, bloomCoord, vec2(0.625    , 0.3125) + vec2( 8.0, 8.0) * view);
			 blur += BloomTile(7.0, bloomCoord, vec2(0.640625 , 0.3125) + vec2(12.0, 8.0) * view);

		blur = clamp(blur + (Bayer8(gl_FragCoord.xy) - 0.5) / 384.0, vec3(0.0), vec3(1.0));

		/* RENDERTARGETS: 1 */

		#ifdef BLOOM
			gl_FragData[0] = vec4(blur, 1.0);
		#else
			gl_FragData[0] = vec4(1.0);
		#endif
	}

#endif