#extension GL_ARB_gpu_shader5 : enable
// data1

#include "/lib/projections.glsl"
#include "/lib/taa/res_params.glsl"
#include "/lib/shadow_param.glsl"
#include "/lib/uniforms.glsl"

flat varying vec3 ambientUp;
flat varying vec3 ambientDown;
flat varying vec3 ambientLeft;
flat varying vec3 ambientRight;
flat varying vec3 ambientB;
flat varying vec3 ambientF;
flat varying float ambientA;
varying vec2 texcoord;
uniform float near;
uniform float far;
uniform float nightblendShadows;
uniform mat4 shadowProjectionInverse;

uniform float frameTimeCounter;

uniform float darknessLightFactor;
uniform sampler2D depthtex0;
uniform sampler2D depthtex2;

uniform sampler2D colortex9; // data0
uniform sampler2D colortex8; // data0

uniform sampler2D colortex10; // data0
uniform sampler2D colortex11; // data0
uniform sampler2D colortex12; // data0
uniform sampler2D colortex13; // data0

uniform sampler2D colortex0; // albedo
#include "/lib/color_transforms.glsl"
#include "/lib/light.glsl"

#ifdef SSAO
#include "/lib/ssao.glsl"
#endif

vec2 tapLocation(int sampleNumber, float spinAngle, int nb, float nbRot)
{
    float alpha = float(sampleNumber + spinAngle / 6.28) / nb;
    float angle = alpha * (nbRot * 6.28) + spinAngle;

    float ssR = alpha * alpha;
    float sin_v, cos_v;

    sin_v = sin(angle);
    cos_v = cos(angle);

    return vec2(cos_v, sin_v) * ssR;
}

#include "/lib/sspt_lib.glsl"
vec3 adjustSaturation(vec3 color, float userSaturation)
{
    float saturation = (userSaturation + 100.0) / 100.0;        // Normalize to 0 - 2
    float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722)); // Luminance in linear space
    return mix(vec3(luminance), color, saturation);             // Adjust saturation linearly
}

void main()
{
    /* RENDERTARGETS: 13 */
    vec3 color;

    float z = texture(depthtex0, texcoord).x;

    if (z >= 1)
    {
        return;
    }
    vec3 albedo = texelFetch(colortex0, ivec2(gl_FragCoord.xy), 0).rgb;

    vec4 normalTex = texelFetch(colortex11, ivec2(gl_FragCoord.xy), 0);
    vec3 normal = (decodeNormal(normalTex.rg));
    vec4 materials = texture(colortex12, texcoord);
    vec2 lightmap = decodeVec2(normalTex.b);

    bool hand = texelFetch(depthtex2, ivec2(gl_FragCoord.xy), 0).x > z;

    float emissive = materials.y;

#ifdef TAA
    vec2 tempOffset = offsets[framemod8] * texelSize * 0.5;
    vec3 fragpos = toScreenSpace(vec3(texcoord - tempOffset, z));
#else
    vec3 fragpos = toScreenSpace(vec3(texcoord, z));
#endif

    vec3 sspt =
        diffuseFunc(lightmap.y, normal, fragpos, (emissive * albedo * 32.0) + (albedo));

    gl_FragData[0].rgb = clamp(sspt, 0, 100);
}
