#version 400 compatibility

/*
====================================================================================================

    Copyright (C) 2020 RRe36

    All Rights Reserved unless otherwise explicitly stated.


    By downloading this you have agreed to the license and terms of use.
    These can be found inside the included license-file
    or here: https://rre36.com/copyright-license

    Violating these terms may be penalized with actions according to the Digital Millennium
    Copyright Act (DMCA), the Information Society Directive and/or similar laws
    depending on your country.

====================================================================================================
*/

/*DRAWBUFFERS:356*/
layout(location = 0) out vec4 lightmapData;
layout(location = 1) out vec3 skyboxData;
layout(location = 2) out vec3 filterNormals;

#include "/lib/head.glsl"
#include "/lib/util/encoders.glsl"
#include "/lib/util/colorspace.glsl"

// SKYBOX PRECOMPUTATION FOR LATER PASSES
// AO AND LIGHTMAP FILTER SETUP

const int noiseTextureResolution = 256;

in vec2 coord;

flat in vec3 airIlluminance;
flat in mat2x3 airIllumMod;

uniform sampler2D colortex0;
uniform sampler2D colortex1;
uniform sampler2D depthtex1;

uniform sampler2D noisetex;

uniform int frameCounter;

uniform float aspectRatio;
uniform float eyeAltitude;
uniform float far, near;

uniform vec2 viewSize, pixelSize;
uniform vec2 taaOffset;

uniform vec3 upvec, upvecView;
uniform vec3 sunvec, sunvecView;
uniform vec3 moonvec, moonvecView;

uniform vec4 daytime;

uniform mat4 gbufferModelView, gbufferModelViewInverse;
uniform mat4 gbufferProjection, gbufferProjectionInverse;


/* ------ includes ------ */
#define FUTIL_LINDEPTH
#define FUTIL_D3X3
#include "/lib/fUtil.glsl"

#include "/lib/util/transforms.glsl"

#include "/lib/atmos/phase.glsl"
#include "/lib/atmos/air/atmosphere.glsl"
#include "/lib/atmos/project.glsl"

/* ------ ambient occlusion and gi ------ */

#include "/lib/frag/bluenoise.glsl"

#include "/lib/light/ao.glsl"

void main() {
    vec4 sceneColor  = stex(colortex0);
    float sceneDepth = stex(depthtex1).x;

    vec3 viewPos    = screenToViewSpace(vec3(coord, sceneDepth));
    vec3 viewDir    = normalize(viewPos);
    vec3 scenePos   = viewToSceneSpace(viewPos);
    vec3 worldDir   = normalize(scenePos);

    lightmapData    = vec4(0.0);
    skyboxData      = vec3(0.0);
    filterNormals   = vec3(0.0);

    #ifdef lightmapSmoothingEnabled
    if (landMask(sceneDepth)) {
        vec4 tex1           = stex(colortex1);
        lightmapData.xyz    = vec3(decode2x8(tex1.z), sceneColor.a);
        filterNormals       = decodeNormal(tex1.xy) * 0.5 + 0.5;
    }
    #endif
    
    if(!(coord.y >= exp2(-SKY_RENDER_LOD) || coord.x >= exp2(-SKY_RENDER_LOD))) {
        vec3 sceneDir   = unprojectSky(coord);
            skyboxData  = atmosphericScattering(sceneDir, mat2x3(sunvec, moonvec), airIlluminance, airIllumMod);
    }

    #ifdef airTransmittanceHQ
        vec2 transmittanceCoord = coord-vec2(0.0, exp2(-SKY_RENDER_LOD) + pixelSize.y);

        if(!(transmittanceCoord.y >= exp2(-SKY_RENDER_LOD) || transmittanceCoord.x >= exp2(-SKY_RENDER_LOD)) && (transmittanceCoord.y>=0.0)) {
            vec3 sceneDir   = unprojectSky(transmittanceCoord);
            skyboxData  = getAirTransmittance(vec3(0.0, planetRad * mix(0.9997, 1.0, saturate(sqrt(max0(sunvec.y * tau)))) + eyeAltitude, 0.0), sceneDir);
        }
    #endif

    #if (defined ambientOcclusionToggle && !defined rtaoEnabled) 
        vec2 ao_coord   = (coord)*2.0;

        if (clamp(ao_coord, -0.003, 1.003) == ao_coord) {
            vec2 coord  = ao_coord;
            float d     = depthMax3x3(depthtex1, ao_coord, pixelSize*sqrt(2.0));

            if (landMask(d)) {
                float sceneDepth = stex(depthtex1).x;
                vec4 tex1       = stex(colortex1);

                vec3 viewpos    = screenToViewSpace(vec3(coord, sceneDepth));

                vec3 sceneNormal = decodeNormal(tex1.xy);
                vec3 viewnormal = normalize(mat3(gbufferModelView) * sceneNormal);

                float ao    = calculate_dbao(depthtex1, sceneDepth, coord, ditherBluenoise());

                lightmapData.w  = ao;
            } else {
                lightmapData.w  = 1.0;
            }
        }
    #endif

    lightmapData    = clamp16F(lightmapData);
    skyboxData      = clamp16F(skyboxData);
    filterNormals   = clamp16F(filterNormals);
}