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


// shader 5f422bf63e25be7f: desaturates colors (shadow world only)
// desaturates the image by shifting colors towards the red channel
// used in shadow world only, as far as tested
// grading values always seem to be: 1.0, 1.0, 1.0, ~0.33


#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)
	
	layout(set = 1, binding = 1) uniform ufBlock
	{
		uniform ivec4 uf_remappedPS[1]; // grading
	};
	
#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)
	
	uniform ivec4 uf_remappedPS[1]; // grading
	
#endif


TEXTURE_LAYOUT(0, 1, 0) uniform sampler2D textureUnitPS0; // frame
layout(location = 0) in vec4 passParameterSem1; // pixel coordinate
layout(location = 0) out vec4 passPixelColor0; // output. w is alpha, overlayed


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


void main()
{
	// get texel color
	vec2 coord = passParameterSem1.xy;
	vec3 texel = texture(textureUnitPS0, coord).xyz;

	// get grading
	vec4 grading = vec4(0.0);
	grading.x = intBitsToFloat(uf_remappedPS[0].x); // 1.0
	grading.y = intBitsToFloat(uf_remappedPS[0].y); // 1.0
	grading.z = intBitsToFloat(uf_remappedPS[0].z); // 1.0
	grading.w = intBitsToFloat(uf_remappedPS[0].w); // alpha, ~0.33

	// shift towards red channel
	vec4 color = vec4(0.0);
	color.x = texel.x * grading.x; // red * 1.0
	color.y = texel.x * grading.y; // red * 1.0
	color.z = texel.x * grading.z; // red * 1.0
	color.w = mixf(0.0, grading.w, $shadow_world_desaturation); // apply custom strength

	// export
	passPixelColor0 = vec4(color.x, color.y, color.z, color.w);


	// test current grading colors
	//passPixelColor0 = vec4(grading.x, grading.y, grading.z, 1.0);

	// test current grading alpha
	//passPixelColor0 = vec4(grading.w, grading.w, grading.w, 1.0);
}
