#version 430
#extension GL_ARB_texture_gather : enable
#extension GL_ARB_separate_shader_objects : enable


// shader 49865bd2e62efda1: tints the bloom mask and applies it to the frame


#ifdef VULKAN
	#define ATTR_LAYOUT(__vkSet, __location) layout(set = __vkSet, location = __location)
	#define UNIFORM_BUFFER_LAYOUT(__glLocation, __vkSet, __vkLocation) layout(set = __vkSet, binding = __vkLocation, std140)
	#define TEXTURE_LAYOUT(__glLocation, __vkSet, __vkLocation) layout(set = __vkSet, binding = __vkLocation)
	#define GET_FRAGCOORD() vec4(gl_FragCoord.xy*uf_fragCoordScale.xy,gl_FragCoord.z, 1.0/gl_FragCoord.w)
	
#else
	#define ATTR_LAYOUT(__vkSet, __location) layout(location = __location)
	#define UNIFORM_BUFFER_LAYOUT(__glLocation, __vkSet, __vkLocation) layout(binding = __glLocation, std140) 
	#define TEXTURE_LAYOUT(__glLocation, __vkSet, __vkLocation) layout(binding = __glLocation)
	#define GET_FRAGCOORD() vec4(gl_FragCoord.xy*uf_fragCoordScale,gl_FragCoord.zw)
	
#endif

#ifdef VULKAN
	layout(set = 1, binding = 1) uniform ufBlock
	{
		uniform ivec4 uf_remappedPS[1]; // holds area specific bloom tint color
		uniform vec4 uf_fragCoordScale;
	};
	
#else
	uniform ivec4 uf_remappedPS[1]; // holds area specific bloom tint color
	uniform vec2 uf_fragCoordScale;
	
#endif

TEXTURE_LAYOUT(0, 1, 0) uniform sampler2D textureUnitPS0; // bloom mask created in 6e2f31b2b2fcab1f
layout(location = 0) in vec4 passParameterSem0; // pixel coord
layout(location = 0) out vec4 passPixelColor0; // pixel color, alpha = blend of original pixel -> used to dampen non-bloom areas in shadow world


// more compatible interpolation
float mixf(float x, float y, float a)
{
	return x * (1.0 - a) + y * a;
}


void main()
{
	// get pixel coord
	vec2 coord = passParameterSem0.xy;
	
	// get bloom mask
	vec3 mask = texture(textureUnitPS0, coord).xyz;
	
	// get area specific color tint
	vec4 tint = vec4(0.0);
	tint.x = intBitsToFloat(uf_remappedPS[0].x);
	tint.y = intBitsToFloat(uf_remappedPS[0].y);
	tint.z = intBitsToFloat(uf_remappedPS[0].z);
	tint.w = intBitsToFloat(uf_remappedPS[0].w);
	
	// get luminance of tint: removes color, keeps intended brightness
	float tintLuminance = dot(tint.xyz, vec3(0.299, 0.587, 0.114)); // percieved approximation
	
	// apply custom tint color
	tint.x = mixf(tintLuminance, tint.x, $bloom_tint_strength);
	tint.y = mixf(tintLuminance, tint.y, $bloom_tint_strength);
	tint.z = mixf(tintLuminance, tint.z, $bloom_tint_strength);
	
	// apply tint on mask
	vec4 outColor = vec4(0.0);
	outColor.x = mask.x * tint.x;
	outColor.y = mask.y * tint.y;
	outColor.z = mask.z * tint.z;
	
	// custom brightness reduction, only in shadow world below 1.0
	outColor.w = mixf(1.0, tint.w, $shadow_world_darkening);
	
	// export
	passPixelColor0 = vec4(outColor.x, outColor.y, outColor.z, outColor.w);
}