#version 420
#extension GL_ARB_texture_gather : enable
// shader d936195db0dd8e7d //crossfade exposure
//Xenoblade FX
//Version 0.01Beta
//Shader Dumped from 1.01
//Shader Edits by Jamie
//

//ToneMapping
#define Bleach 0.3         //Default is 0.0
#define exposure 1.25      //Default is 1.0
#define defog 0.000	       //Default is 0.0  //How much of the overall color you want removed form the values of FogColor.
#define FogColor vec3(1.0, 1.0, 1.0)    //Color you want to Add or Remove  0.25 would add .25 percent of that color 1.25 would remove .25 percent of the color."

//VibrancePass
#define Vibrance 0.15      //"Intelligently saturates (or desaturates if you use negative values) the pixels depending on their original saturation.";
#define VibranceRGBBalance vec3(1.0, 1.0, 1.0) //"A per channel multiplier to the Vibrance strength so you can give more boost to certain colors over others.";

//Lift Gamma Gain
#define RGB_Lift  vec3(1.05, 1.05, 1.05)  //[0.000 to 2.000] Adjust shadows for Red, Green and Blue.
#define RGB_Gamma vec3(0.70, 0.70, 0.70)  //[0.000 to 2.000] Adjust midtones for Red, Green and Blue
#define RGB_Gain  vec3(1.05, 1.05, 1.05)  //[0.000 to 2.000] Adjust highlights for Red, Green and Blue
//Note that a value of 1.0 is a neutral setting that leave the color unchanged.

//Curves 
#define Contrast 0.50      //[-1.0, 1.0]  The amount of contrast you want

//LumaShapening
#define sharp_strength 0.25   //[0.10 to 3.00] Strength of the sharpening Default is 0.65
#define sharp_clamp    0.085  //[0.000 to 1.000] Limits maximum amount of sharpening a pixel recieves - Default is 0.035
//Advanced sharpening settings
#define offset_bias 1.0       //[0.0 to 6.0] Offset bias adjusts the radius of the sampling pattern.

//Fake High Dynamic Range.
#define HDRPower 1.70  // 0.0 to  8.0  "Raising this seems to make the effect stronger and also darker , Default 1.30."
#define radius1 0.793  // 0.0 to  8.0  "Default 0.793 , will affect FX."
#define radius2 0.87   // 0.0 to  8.0  "Default 0.87 , will affect FX."

//###########################################################
//Do not edit under this line.
uniform ivec4 uf_remappedPS[1];
layout(binding = 0) uniform sampler2D textureUnitPS0;// Tex0 addr 0xf470a000 res 1280x720x1 dim 1 tm: 4 format 0816 compSel: 0 1 2 5 mipView: 0x0 (num 0x1) sliceView: 0x0 (num 0x1) Sampler0 ClampX/Y/Z: 2 2 2 border: 0
layout(binding = 1) uniform sampler3D textureUnitPS1;// Tex1 addr 0x26032000 res 16x16x16 dim 2 tm: 7 format 001a compSel: 0 1 2 3 mipView: 0x0 (num 0x1) sliceView: 0x0 (num 0x10) Sampler1 ClampX/Y/Z: 2 2 2 border: 0
layout(binding = 2) uniform sampler3D textureUnitPS2;// Tex2 addr 0x2603b000 res 16x16x16 dim 2 tm: 7 format 001a compSel: 0 1 2 3 mipView: 0x0 (num 0x1) sliceView: 0x0 (num 0x10) Sampler2 ClampX/Y/Z: 2 2 2 border: 0
layout(location = 0) in vec4 passParameterSem0;
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 min(a*b,min(abs(a)*3.40282347E+38F,abs(b)*3.40282347E+38F)); }
//----------------------------------------------------------------------------
//ToneMapping

vec3 TonemapPass(vec3 inputColor) {
	vec3 color = inputColor;
	color = clamp(color - defog * FogColor * 2.55, 0.0, 1.0); // defog
	color *= exposure / (1.0 + color / exposure);

	const vec3 coefLuma = vec3(0.2126, 0.7152, 0.0722);
	float lum = dot(coefLuma, color);

	float L = clamp(10.0 * (lum - 0.45), 0.0, 1.0);
	vec3 A2 = Bleach * color;

	vec3 result1 = 2.0f * color * lum;
	vec3 result2 = 1.0f - 2.0f * (1.0f - lum) * (1.0f - color);

	vec3 newColor = mix(result1, result2, L);
	vec3 mixRGB = A2 * newColor;
	color += ((1.0f - A2) * mixRGB);

	vec3 middlegray = vec3(dot(color, vec3(1.0 / 3.0)));
	vec3 diffcolor = color - middlegray;
	
	float sat = 0.0;
	color = (color + diffcolor * sat) / (1 + (diffcolor * sat)); // saturation

	return color;
}

//LumaShapening

#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;
}

//Fake High Dynamic Range.

vec3 HDRPass(sampler2D tex, vec2 pos) {
	vec3 color = texture(tex, pos).rgb;

	vec3 bloom_sum1 = texture(tex, pos + vec2(1.5, -1.5) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(-1.5, -1.5) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(1.5, 1.5) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(-1.5, 1.5) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(0.0, -2.5) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(0.0, 2.5) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(-2.5, 0.0) * radius1 * vec2(px, py)).rgb;
	bloom_sum1 += texture(tex, pos + vec2(2.5, 0.0) * radius1 * vec2(px, py)).rgb;

	bloom_sum1 *= 0.005;

	vec3 bloom_sum2 = texture(tex, pos + vec2(1.5, -1.5) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(-1.5, -1.5) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(1.5, 1.5) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(-1.5, 1.5) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(0.0, -2.5) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(0.0, 2.5) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(-2.5, 0.0) * radius2 * vec2(px, py)).rgb;
	bloom_sum2 += texture(tex, pos + vec2(2.5, 0.0) * radius2 * vec2(px, py)).rgb;

	bloom_sum2 *= 0.010;

	float dist = radius2 - radius1;
	vec3 HDR = (color + (bloom_sum2 - bloom_sum1)) * dist;

	vec3 blend = HDR + color;
	color = pow(abs(blend), vec3(abs(HDRPower))) + HDR;

	return color;
}

//Lift Gamma Gain

vec3 LiftGammaGainPass(vec3 colorInput)
{
	// -- Get input --
	vec3 color = colorInput;

	// -- Lift --

	color = color * (1.5 - 0.5 * RGB_Lift) + 0.5 * RGB_Lift - 0.5;
	color = clamp(color, 0.0, 1.0); //isn't strictly necessary, but doesn't cost performance.

	// -- Gain --
	color *= RGB_Gain;

	// -- Gamma --
	color = pow(color, 1.0 / RGB_Gamma); //Gamma

	// -- Return output --
	return clamp(color, 0.0, 1.0);
}

//VibrancePass
vec3 VibrancePass(vec3 color) {
  
	const vec3 coefLuma = vec3(0.2126, 0.7152, 0.0722);
	float luma = dot(coefLuma, color);

	float max_color = max(color.r, max(color.g, color.b)); // Find the strongest color
	float min_color = min(color.r, min(color.g, color.b)); // Find the weakest color

	float color_saturation = max_color - min_color; // The difference between the two is the saturation

	// Extrapolate between luma and original by 1 + (1-saturation) - current
	vec3 coeffVibrance = VibranceRGBBalance * Vibrance;
	
	color = mix(vec3(luma), color, 1.0 + (coeffVibrance * (1.0 - (sign(coeffVibrance) * color_saturation))));
	
	return color;
}

//Curves

vec3 CurvesPass(vec3 inputColor) {
	vec3 colorInput = inputColor;
	float Contrast_blend = Contrast * 2.0; //I multiply by two to give it a strength closer to the other curves.
	vec3 x = colorInput.rgb; //if the curve should be applied to both Luma and Chroma
	x = x * (x * (1.5 - x) + 0.5); //horner form - fastest version
	vec3 color = x;  //if the curve should be applied to both Luma and Chroma
	colorInput.rgb = mix(colorInput.rgb, color, Contrast_blend); //Blend by Contrast
	return colorInput;
}

void main()
{
vec4 R0f = vec4(0.0);
vec4 R1f = vec4(0.0);
vec4 R2f = 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;
vec3 cubeMapSTM;
int cubeMapFaceId;
R0f = passParameterSem0;
R0f.xyz = HDRPass(textureUnitPS0, passParameterSem0.xy);
float smask = lumasharping(textureUnitPS0, passParameterSem0.xy);
R0f.xyz += vec3(smask);
//R0f.xyz = (texture(textureUnitPS0, R0f.xy).xyz);
// -- Original shader code
// 0
backupReg0f = R0f.x;
PV0f.x = backupReg0f * intBitsToFloat(uf_remappedPS[0].x);
PV0f.x = clamp(PV0f.x, 0.0, 1.0);
R127f.z = R0f.z * intBitsToFloat(uf_remappedPS[0].x);
R127f.z = clamp(R127f.z, 0.0, 1.0);
R127f.w = R0f.y * intBitsToFloat(uf_remappedPS[0].x);
R127f.w = clamp(R127f.w, 0.0, 1.0);
R2f.w = 1.0;
PS0f = R2f.w;
// 1
tempResultf = log2(PV0f.x);
if( isinf(tempResultf) == true ) tempResultf = -3.40282347E+38F;
PS1f = tempResultf;
// 2
R127f.x = PS1f * intBitsToFloat(0x3ee8ba2e);
tempResultf = log2(R127f.w);
if( isinf(tempResultf) == true ) tempResultf = -3.40282347E+38F;
PS0f = tempResultf;
// 3
R127f.y = PS0f * intBitsToFloat(0x3ee8ba2e);
tempResultf = log2(R127f.z);
if( isinf(tempResultf) == true ) tempResultf = -3.40282347E+38F;
PS1f = tempResultf;
// 4
R127f.w = PS1f * intBitsToFloat(0x3ee8ba2e);
PS0f = exp2(R127f.x);
// 5
R0f.x = (PS0f * intBitsToFloat(uf_remappedPS[0].z) + intBitsToFloat(uf_remappedPS[0].w));
PS1f = exp2(R127f.y);
// 6
R0f.y = (PS1f * intBitsToFloat(uf_remappedPS[0].z) + intBitsToFloat(uf_remappedPS[0].w));
PS0f = exp2(R127f.w);
// 7
R0f.z = (PS0f * intBitsToFloat(uf_remappedPS[0].z) + intBitsToFloat(uf_remappedPS[0].w));
R1f.xyz = (texture(textureUnitPS1, vec3(R0f.x,R0f.y,R0f.z)).xyz);
R0f.xyz = (texture(textureUnitPS2, vec3(R0f.x,R0f.y,R0f.z)).xyz);
// 0
backupReg0f = R0f.y;
backupReg1f = R0f.x;
PV0f.x = R1f.z + -(R0f.z);
PV0f.y = R1f.y + -(backupReg0f);
PV0f.z = R1f.x + -(backupReg1f);
// 1
R2f.x = (PV0f.z * intBitsToFloat(uf_remappedPS[0].y) + R0f.x);
R2f.y = (PV0f.y * intBitsToFloat(uf_remappedPS[0].y) + R0f.y);
R2f.z = (PV0f.x * intBitsToFloat(uf_remappedPS[0].y) + R0f.z);
// -- End original shader code

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

	vec3 color = texture(textureUnitPS0, passParameterSem0.xy).xyz;
    color = TonemapPass(color);
    color = CurvesPass(color);
	color = LiftGammaGainPass(color);
	color = VibrancePass(color);
    passPixelColor0 = vec4(color, passParameterSem0.w);
//passPixelColor0 = vec4(R1f.x, R1f.y, R1f.z, R1f.w);
//passPixelColor0 = vec4(R2f.x, R2f.y, R2f.z, R2f.w);
}
