#include "/lib/config.glsl"

#if MC_VERSION < 11604
    const bool colortex0Clear = false;
    const bool colortex1Clear = false;
    const bool colortex2Clear = false;
    const bool colortex3Clear = false;
    const bool gaux1Clear = false;
    const bool gaux2Clear = false;
    const bool gaux3Clear = false;
    const bool gaux4Clear = false;
#endif

/* Uniforms */

uniform sampler2D colortex1;

#if AA_TYPE > 0
    uniform sampler2D colortex3;  // TAA past averages
    #if defined BLOOM && defined FANTASY_LIFE_SYSTEM && FANTASY_FIREFLIES > 0
        uniform sampler2D gaux1;  // Bloom RGB + firefly reactive mask in alpha
    #endif
    uniform mat4 gbufferProjectionInverse;
    uniform mat4 gbufferProjection;
    uniform mat4 gbufferModelViewInverse;
    uniform vec3 cameraPosition;
    uniform vec3 previousCameraPosition;
    uniform mat4 gbufferPreviousProjection;
    uniform mat4 gbufferPreviousModelView;
    uniform sampler2D depthtex1;
#endif

/* Ins / Outs */

varying vec2 texcoord;

/* Utility functions */

#if AA_TYPE > 0
    #include "/lib/projection_utils.glsl"
#endif

#define FRAGMENT
#include "/lib/downscale.glsl"

#if AA_TYPE > 0
    #include "/lib/luma.glsl"
    #include "/lib/color_conversion.glsl"
    #include "/lib/fast_taa.glsl"
#endif

// MAIN FUNCTION ------------------

void main() {
    if (fragment_cull()) discard;
    vec4 block_color = texture2DLod(colortex1, texcoord, 0);

    // Precalc past position and velocity
    #if AA_TYPE > 0
        // Retrojection of previous frame
        float z_depth = texture2DLod(depthtex1, texcoord, 0).r;
        vec2 texcoord_past;
        vec3 curr_view_pos;
        vec3 curr_feet_player_pos;
        vec3 prev_feet_player_pos;
        vec3 prev_view_pos;
        vec2 final_pos;

        if(z_depth < 0.56) {
            texcoord_past = texcoord;
        } else {
            curr_view_pos =
                vec3(vec2(gbufferProjectionInverse[0].x, gbufferProjectionInverse[1].y) * (texcoord * 2.0 - 1.0) + gbufferProjectionInverse[3].xy, gbufferProjectionInverse[3].z);
            curr_view_pos /= (gbufferProjectionInverse[2].w * (z_depth * 2.0 - 1.0) + gbufferProjectionInverse[3].w);
            curr_feet_player_pos = mat3(gbufferModelViewInverse) * curr_view_pos + gbufferModelViewInverse[3].xyz;

            prev_feet_player_pos =
                z_depth > 0.56 ? curr_feet_player_pos + cameraPosition - previousCameraPosition : curr_feet_player_pos;
            prev_view_pos = mat3(gbufferPreviousModelView) * prev_feet_player_pos + gbufferPreviousModelView[3].xyz;
            final_pos =
                vec2(gbufferPreviousProjection[0].x, gbufferPreviousProjection[1].y) * prev_view_pos.xy + gbufferPreviousProjection[3].xy;
            texcoord_past = (final_pos / -prev_view_pos.z) * 0.5 + 0.5;
        }

    #endif

    #if AA_TYPE > 0
        float fireflyReactive = 0.0;
        #if defined BLOOM && defined FANTASY_LIFE_SYSTEM && FANTASY_FIREFLIES > 0
            fireflyReactive = smoothstep(
                0.006, 0.16,
                texture2DLod(gaux1, texcoord, 0.0).a);
        #endif

        #ifdef DOF
            block_color = fast_taa_depth(
                block_color, texcoord_past, fireflyReactive);
        #else
            block_color.rgb = fast_taa(
                block_color.rgb, texcoord_past, fireflyReactive);
        #endif

        block_color = clamp(block_color, vec4(0.0), vec4(vec3(50.0), 1.0));
        /* DRAWBUFFERS:13 */
        gl_FragData[0] = block_color;  // colortex1
        gl_FragData[1] = block_color;  // To TAA averages
    #else
        block_color = clamp(block_color, vec4(0.0), vec4(vec3(50.0), 1.0));
        /* DRAWBUFFERS:1 */
        gl_FragData[0] = block_color;  // colortex1
    #endif
}
