#version 420
#extension GL_ARB_texture_gather : enable
#extension GL_ARB_separate_shader_objects : enable
// shader be99d80628d31127 //AA PS
// Used for: Another vertical blur

uniform vec2 uf_fragCoordScale;

const float hazeFactor = 0.1;

const float gamma = $gamma; // 1.0 is neutral  Botw is already colour graded at this stage
const float exposure = $exposure; // 1.0 is neutral
const float vibrance = $vibrance;  // 0.0 is neutral  
const float crushContrast = $crushContrast; // 0.0 is neutral. Use small increments, loss of shadow detail 
const float contrastCurve = $contrastCurve;


vec3 RGB_Lift = vec3($redShadows, $greenShadows , $blueSadows);		// [0.000 to 2.000] Adjust shadows for Red, Green and Blue.
vec3 RGB_Gamma = vec3($redMid ,$greenMid, $blueMid);				// [0.000 to 2.000] Adjust midtones for Red, Green and Blue
vec3 RGB_Gain = vec3($redHilight, $greenHilight, $blueHilight);		// [0.000 to 2.000] Adjust highlights for Red, Green and Blue
//lumasharpen
const float sharp_mix = $sharp_mix;
const float sharp_strength = 2.0; 
const float sharp_clamp = 0.75;
const float offset_bias = 1.0;
float Sigmoid (float x) {

	return 1.0 / (1.0 + (exp(-(x - 0.5) * 5.5))); 
}


#define px (1.0/1280.0*uf_fragCoordScale.x)
#define py (1.0/720.0*uf_fragCoordScale.y)
#define CoefLuma vec3(0.2126, 0.7152, 0.0722)  

float lumasharping(sampler2D tex, vec2 pos) {
	vec4 colorInput = texture(tex, pos);

	vec3 ori = colorInput.rgb;

	// -- Combining the strength and luma multipliers --
	vec3 sharp_strength_luma = (CoefLuma * sharp_strength);

	// -- Gaussian filter --
	//   [ .25, .50, .25]     [ 1 , 2 , 1 ]
	//   [ .50,   1, .50]  =  [ 2 , 4 , 2 ]
	//   [ .25, .50, .25]     [ 1 , 2 , 1 ]

	vec3 blur_ori = texture(tex, pos + vec2(px, -py) * 0.5 * offset_bias).rgb; // South East
	blur_ori += texture(tex, pos + vec2(-px, -py) * 0.5 * offset_bias).rgb;  // South West
	blur_ori += texture(tex, pos + vec2(px, py) * 0.5 * offset_bias).rgb; // North East
	blur_ori += texture(tex, pos + vec2(-px, py) * 0.5 * offset_bias).rgb; // North West

	blur_ori *= 0.25;  // ( /= 4) Divide by the number of texture fetches

	// -- Calculate the sharpening --
	vec3 sharp = ori - blur_ori;  //Subtracting the blurred image from the original image

	// -- Adjust strength of the sharpening and clamp it--
	vec4 sharp_strength_luma_clamp = vec4(sharp_strength_luma * (0.5 / sharp_clamp), 0.5); //Roll part of the clamp into the dot

	float sharp_luma = clamp((dot(vec4(sharp, 1.0), sharp_strength_luma_clamp)), 0.0, 1.0); //Calculate the luma, adjust the strength, scale up and clamp
	sharp_luma = (sharp_clamp * 2.0) * sharp_luma - sharp_clamp; //scale down

	return sharp_luma;
}

vec3 LiftGammaGainPass(vec3 colorInput)
{ 	//reshade BSD https://reshade.me ,  Alexkiri port
	vec3 color = colorInput;
	color = color * (1.5 - 0.5 * RGB_Lift) + 0.5 * RGB_Lift - 0.5;
	color = clamp(color, 0.0, 1.0); 
	color *= RGB_Gain;	
	color = pow(color, 1.0 / RGB_Gamma);
	return clamp(color, 0.0, 1.0);
}

vec3 contrasty(vec3 colour){
	vec3 fColour = (colour.xyz);
	//fColour = LiftGammaGainPass(fColour);
	
	fColour = clamp(exposure * fColour, 0.0, 1.0);
	fColour = pow(fColour, vec3(1.0 / gamma));
	float luminance = fColour.r*0.299 + fColour.g*0.587 + fColour.b*0.114;
	float mn = min(min(fColour.r, fColour.g), fColour.b);
	float mx = max(max(fColour.r, fColour.g), fColour.b);
	float sat = (1.0 - (mx - mn)) * (1.0 - mx) * luminance * 5.0;
	vec3 lightness = vec3((mn + mx) / 2.0);
	fColour = LiftGammaGainPass(fColour);
	// vibrance
	fColour = mix(fColour, mix(fColour, lightness, -vibrance), sat);
	fColour = max(vec3(0.0), fColour - vec3(crushContrast));
	return fColour;
}

uniform ivec4 uf_remappedPS[4];
layout(binding = 0) uniform sampler2D textureUnitPS0;// Tex0 addr 0xf5800800 res 1280x720x1 dim 1 tm: 4 format 0019 compSel: 0 1 2 3 mipView: 0x0 (num 0x1) sliceView: 0x0 (num 0x1) Sampler0 ClampX/Y/Z: 2 2 2 border: 1
layout(binding = 1) uniform sampler2D textureUnitPS1;// Tex1 addr 0xf4341000 res 1280x720x1 dim 1 tm: 4 format 0001 compSel: 0 4 4 5 mipView: 0x0 (num 0x1) sliceView: 0x0 (num 0x1) Sampler1 ClampX/Y/Z: 2 2 2 border: 1
layout(location = 0) in vec4 passParameterSem2;
layout(location = 0) out vec4 passPixelColor0;
//uniform vec2 uf_fragCoordScale;
int clampFI32(int v)
{
if( v == 0x7FFFFFFF )
	return floatBitsToInt(1.0);
else if( v == 0xFFFFFFFF )
	return floatBitsToInt(0.0);
return floatBitsToInt(clamp(intBitsToFloat(v), 0.0, 1.0));
}
float mul_nonIEEE(float a, float b){return mix(0.0, a*b, (a != 0.0) && (b != 0.0));}
void main()
{
vec4 R0f = vec4(0.0);
vec4 R1f = vec4(0.0);
vec4 R2f = vec4(0.0);
vec4 R3f = vec4(0.0);
vec4 R4f = vec4(0.0);
vec4 R5f = vec4(0.0);
vec4 R123f = vec4(0.0);
vec4 R126f = vec4(0.0);
vec4 R127f = vec4(0.0);
float backupReg0f, backupReg1f, backupReg2f, backupReg3f, backupReg4f;
vec4 PV0f = vec4(0.0), PV1f = vec4(0.0);
float PS0f = 0.0, PS1f = 0.0;
vec4 tempf = vec4(0.0);
float tempResultf;
int tempResulti;
ivec4 ARi = ivec4(0);
bool predResult = true;
bool activeMaskStack[2];
bool activeMaskStackC[3];
activeMaskStack[0] = false;
activeMaskStackC[0] = false;
activeMaskStackC[1] = false;
activeMaskStack[0] = true;
activeMaskStackC[0] = true;
activeMaskStackC[1] = true;
vec3 cubeMapSTM;
int cubeMapFaceId;
float scaler =  uf_fragCoordScale.x;
R0f = passParameterSem2;
if( activeMaskStackC[1] == true ) {
R4f.xyzw = (texture(textureUnitPS0, R0f.xy).xyzw);
R2f.xyzw = (textureGather(textureUnitPS1, R0f.xy).wzxy);
}
if( activeMaskStackC[1] == true ) {
activeMaskStack[1] = activeMaskStack[0];
activeMaskStackC[2] = activeMaskStackC[1];
// 0
PV0f.x = min(R2f.z, R2f.x);
PV0f.y = max(R2f.z, R2f.x);
PV0f.z = mul_nonIEEE(R4f.x, intBitsToFloat(uf_remappedPS[0].x));
PV0f.w = min(R2f.w, R2f.y);
PS0f = max(R2f.w, R2f.y);
// 1
PV1f.x = min(PV0f.x, PV0f.w);
R123f.y = (mul_nonIEEE(R4f.y,intBitsToFloat(uf_remappedPS[0].y)) + PV0f.z);
PV1f.y = R123f.y;
R127f.z = R2f.z + -(R2f.y);
PV1f.z = R127f.z;
PV1f.w = max(PV0f.y, PS0f);
R126f.z = R2f.w + -(R2f.x);
PS1f = R126f.z;
// 2
PV0f.x = mul_nonIEEE(PV1f.w, intBitsToFloat(uf_remappedPS[1].x));
PV0f.y = max(PV1f.y, PV1f.w);
PV0f.z = min(PV1f.y, PV1f.x);
R3f.x = PV1f.z + PS1f;
PS0f = R3f.x;
// 3
R1f.x = max(PV0f.x, intBitsToFloat(uf_remappedPS[1].y));
R3f.y = -(PV0f.z) + PV0f.y;
R1f.y = R127f.z + -(R126f.z);
PS1f = R1f.y;
// 4
predResult = (R3f.y > R1f.x);
activeMaskStack[1] = predResult;
activeMaskStackC[2] = predResult == true && activeMaskStackC[1] == true;
}
else {
activeMaskStack[1] = false;
activeMaskStackC[2] = false;
}
if( activeMaskStackC[2] == true ) {
// 0
backupReg0f = R2f.y;
R1f.x = max(R3f.x, -(R3f.x));
PV0f.x = R1f.x;
R2f.y = backupReg0f + R2f.x;
PV0f.y = R2f.y;
R0f.z = intBitsToFloat(uf_remappedPS[2].z) * 0.25;
R0f.w = max(R1f.y, -(R1f.y));
PV0f.w = R0f.w;
R2f.x = -(intBitsToFloat(uf_remappedPS[3].x));
PS0f = R2f.x;
// 1
R3f.y = R2f.z + PV0f.y;
PV1f.y = R3f.y;
R2f.y = min(PV0f.x, PV0f.w);
PS1f = R2f.y;
// 2
R3f.y = R2f.w + PV1f.y;
PV0f.y = R3f.y;
R1f.z = intBitsToFloat(uf_remappedPS[3].x);
R0f.w = intBitsToFloat(uf_remappedPS[3].y);
R5f.y = -(intBitsToFloat(uf_remappedPS[3].y));
PS0f = R5f.y;
// 3
backupReg0f = R0f.z;
R0f.z = (mul_nonIEEE(backupReg0f,PV0f.y) + intBitsToFloat(uf_remappedPS[2].w));
PV1f.z = R0f.z;
// 4
backupReg0f = R2f.y;
R2f.y = max(PV1f.z, backupReg0f);
PV0f.y = R2f.y;
// 5
R2f.y = 1.0 / PV0f.y;
PS1f = R2f.y;
// 6
backupReg0f = R1f.y;
R1f.x = mul_nonIEEE(R3f.x, PS1f);
PV0f.x = R1f.x;
R1f.y = mul_nonIEEE(backupReg0f, PS1f);
PV0f.y = R1f.y;
// 7
R1f.x = max(PV0f.x, -(intBitsToFloat(uf_remappedPS[2].y)));
PV1f.x = R1f.x;
R1f.y = max(PV0f.y, -(intBitsToFloat(uf_remappedPS[2].y)));
PV1f.y = R1f.y;
// 8
R1f.x = min(PV1f.x, intBitsToFloat(uf_remappedPS[2].y));
PV0f.x = R1f.x;
R1f.y = min(PV1f.y, intBitsToFloat(uf_remappedPS[2].y));
PV0f.y = R1f.y;
// 9
backupReg0f = R0f.x;
backupReg1f = R0f.y;
backupReg0f = R0f.x;
backupReg2f = R0f.w;
backupReg1f = R0f.y;
R0f.x = (mul_nonIEEE(PV0f.x,R2f.x) *scaler + backupReg0f);
R0f.y = (mul_nonIEEE(PV0f.y,R5f.y) *scaler + backupReg1f);
R0f.z = (mul_nonIEEE(PV0f.x,R1f.z) *scaler + backupReg0f);
R0f.w = (mul_nonIEEE(PV0f.y,backupReg2f) *scaler + backupReg1f);
}
if( activeMaskStackC[2] == true ) {
R1f.xyzw = (texture(textureUnitPS0, R0f.zw).xyzw);
R0f.xyzw = (texture(textureUnitPS0, R0f.xy).xyzw);
}
if( activeMaskStackC[2] == true ) {
// 0
backupReg0f = R0f.y;
backupReg1f = R0f.x;
PV0f.x = R0f.w + R1f.w;
PV0f.x /= 2.0;
PV0f.y = R0f.z + R1f.z;
PV0f.y /= 2.0;
PV0f.z = backupReg0f + R1f.y;
PV0f.z /= 2.0;
PV0f.w = backupReg1f + R1f.x;
PV0f.w /= 2.0;
// 1
PV1f.x = -(R4f.w) + PV0f.x;
PV1f.y = -(R4f.z) + PV0f.y;
PV1f.z = -(R4f.y) + PV0f.z;
PV1f.w = -(R4f.x) + PV0f.w;
// 2
backupReg0f = R4f.x;
backupReg1f = R4f.y;
backupReg2f = R4f.z;
backupReg3f = R4f.w;
R4f.x = (PV1f.w * intBitsToFloat(0x3f4ccccd) + backupReg0f);
R4f.y = (PV1f.z * intBitsToFloat(0x3f4ccccd) + backupReg1f);
R4f.z = (PV1f.y * intBitsToFloat(0x3f4ccccd) + backupReg2f);
R4f.w = (PV1f.x * intBitsToFloat(0x3f4ccccd) + backupReg3f);
}
activeMaskStackC[1] = activeMaskStack[0] == true && activeMaskStackC[0] == true;
// export
R4f.xyz = contrasty(R4f.xyz);
R4f.xyz = mix(R4f.xyz, smoothstep(0.0, 1.0, R4f.xyz), contrastCurve);
float smask = lumasharping(textureUnitPS0, passParameterSem2.xy);
vec3 temp3 = R4f.xyz;
R4f.xyz = mix(R4f.xyz, (temp3.xyz += (smask)), sharp_mix);

passPixelColor0 = vec4(R4f.x, R4f.y, R4f.z, R4f.w);
}
