#ifdef s_shadow2D
float poisson15shadow(vec3 pos, float sigma, float cdepth, float diff) {
    float shadow    = 0.0;
    vec2 dither     = taaShadowDither();

    for (int i = 0; i<15; i++) {
        vec2 coord = pos.xy + poisson60o[i*4]*dither*sigma;
        shadow += shadow2DLod(shadowtex1, vec3(coord, pos.z+dither.x*sigma*0.03), 1).x;
    }
    return shadow/15;
}
float poisson20shadow(vec3 pos, float sigma, float cdepth, float diff) {
    float shadow    = 0.0;
    vec2 dither     = taaShadowDither();

    for (int i = 0; i<20; i++) {
        vec2 coord = pos.xy + poisson60o[i*3]*dither*sigma;
        shadow += shadow2DLod(shadowtex1, vec3(coord, pos.z+dither.x*sigma*0.03), 1).x;
    }
    return shadow/20;
}
float poisson30shadow(vec3 pos, float sigma, float cdepth, float diff) {
    float shadow    = 0.0;
    vec2 dither     = taaShadowDither();

    for (int i = 0; i<30; i++) {
        vec2 coord = pos.xy + poisson60o[i*2]*dither*sigma;
        shadow += shadow2DLod(shadowtex1, vec3(coord, pos.z+dither.x*sigma*0.03), 1).x;
    }
    return shadow/30;
}
#else
float poisson15shadow(vec3 pos, float sigma, float cdepth, float diff) {
    float shadow    = 0.0;
    vec2 dither     = taaShadowDither();

    for (int i = 0; i<15; i++) {
        vec2 coord = pos.xy + poisson60o[i*4]*dither*sigma;
        shadow += clamp(cdepth-textureLod(shadowtex1, coord, 2).x*256.0, 0.0, diff)/diff;
    }
    return 1.0-(shadow/15);
}
float poisson20shadow(vec3 pos, float sigma, float cdepth, float diff) {
    float shadow    = 0.0;
    vec2 dither     = taaShadowDither();

    for (int i = 0; i<20; i++) {
        vec2 coord = pos.xy + poisson60o[i*3]*dither*sigma;
        shadow += clamp(cdepth-textureLod(shadowtex1, coord, 2).x*256.0, 0.0, diff)/diff;
    }
    return 1.0-(shadow/20);
}
float poisson30shadow(vec3 pos, float sigma, float cdepth, float diff) {
    float shadow    = 0.0;
    vec2 dither     = taaShadowDither();

    for (int i = 0; i<30; i++) {
        vec2 coord = pos.xy + poisson60o[i*2]*dither*sigma;
        shadow += clamp(cdepth-textureLod(shadowtex1, coord, 2).x*256.0, 0.0, diff)/diff;
    }
    return 1.0-(shadow/30);
}
#endif


float getFakeGI(in vec3 pos, in float distortion, in float cdepth) {
    float softShadow    = poisson20shadow(pos, 0.01*distortion, cdepth, 1.0);

    #ifndef s_shadow2D
        softShadow *= 0.8;
    #endif

    float lambert   = dot(scene.normal, vec.light);
        lambert     = saturate(lambert*1.75);

        #ifndef gbufferWater
            lambert     = mix(lambert, 1.0, mat.foliage*0.7);
        #endif

    return pow2(softShadow)*lambert*0.45;
}