#version 460 compatibility
#define COMP 1
#extension GL_KHR_shader_subgroup_basic : enable
#extension GL_KHR_shader_subgroup_shuffle : enable
#extension GL_KHR_shader_subgroup_ballot : enable
/*
    References:
        [WHY23] Whyte, Killian. "Soft Clamp Function". 2023.
            https:

        You can find full license texts in /licenses
*/
// This file is generated by options.main.kts. Do not edit this file manually.
#define SETTING_SHADOW_MAP_RESOLUTION 2048//[1024 2048 3072 4096]
const float shadowDistance = 512.0;//[64.0 128.0 192.0 256.0 384.0 512.0]
const float sunPathRotation = -20.0;//[-90.0 -89.0 -88.0 -87.0 -86.0 -85.0 -84.0 -83.0 -82.0 -81.0 -80.0 -79.0 -78.0 -77.0 -76.0 -75.0 -74.0 -73.0 -72.0 -71.0 -70.0 -69.0 -68.0 -67.0 -66.0 -65.0 -64.0 -63.0 -62.0 -61.0 -60.0 -59.0 -58.0 -57.0 -56.0 -55.0 -54.0 -53.0 -52.0 -51.0 -50.0 -49.0 -48.0 -47.0 -46.0 -45.0 -44.0 -43.0 -42.0 -41.0 -40.0 -39.0 -38.0 -37.0 -36.0 -35.0 -34.0 -33.0 -32.0 -31.0 -30.0 -29.0 -28.0 -27.0 -26.0 -25.0 -24.0 -23.0 -22.0 -21.0 -20.0 -19.0 -18.0 -17.0 -16.0 -15.0 -14.0 -13.0 -12.0 -11.0 -10.0 -9.0 -8.0 -7.0 -6.0 -5.0 -4.0 -3.0 -2.0 -1.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 29.0 30.0 31.0 32.0 33.0 34.0 35.0 36.0 37.0 38.0 39.0 40.0 41.0 42.0 43.0 44.0 45.0 46.0 47.0 48.0 49.0 50.0 51.0 52.0 53.0 54.0 55.0 56.0 57.0 58.0 59.0 60.0 61.0 62.0 63.0 64.0 65.0 66.0 67.0 68.0 69.0 70.0 71.0 72.0 73.0 74.0 75.0 76.0 77.0 78.0 79.0 80.0 81.0 82.0 83.0 84.0 85.0 86.0 87.0 88.0 89.0 90.0]
//#define SETTING_DEBUG_AE
#ifdef SETTING_DEBUG_AE
#endif
#if SETTING_SHADOW_MAP_RESOLUTION == 1024
#define SHADOW_MAP_SIZE_D16 64
const int shadowMapResolution = 1024;
#elif SETTING_SHADOW_MAP_RESOLUTION == 2048
#define SHADOW_MAP_SIZE_D16 128
const int shadowMapResolution = 2048;
#elif SETTING_SHADOW_MAP_RESOLUTION == 3072
#define SHADOW_MAP_SIZE_D16 192
const int shadowMapResolution = 3072;
#elif SETTING_SHADOW_MAP_RESOLUTION == 4096
#define SHADOW_MAP_SIZE_D16 256
const int shadowMapResolution = 4096;
#endif
uniform float near;
uniform float far;
uniform mat4 gbufferPreviousModelView;
uniform mat4 textureMatrix = mat4(1.0);
#ifdef DISTANT_HORIZONS
uniform float dhNearPlane;
uniform float dhFarPlane;
float farPlane = max(dhFarPlane, far);
float nearPlane = min(dhNearPlane, near);
#else
float farPlane = far;
float nearPlane = near;
#endif
uniform vec2 uval_mainImageSize;
uniform int uval_mainImageSizeIX;
uniform int uval_mainImageSizeIY;
ivec2 uval_mainImageSizeI = ivec2(uval_mainImageSizeIX, uval_mainImageSizeIY);
#ifndef GLOBAL_DATA_MODIFIER
#define GLOBAL_DATA_MODIFIER restrict readonly buffer
#endif
struct AEData {
    vec4 lastScreenAvgLum;
    vec4 screenAvgLum;
    vec4 expValues;
    vec2 hsPercents;
    uint shadowCount;
    uint highlightCount;
    uint weightSum;
    #ifdef SETTING_DEBUG_AE
    uint lumHistogram[256];
    uint lumHistogramMaxBinCount;
    float finalAvgLum;
    #endif
};
struct CameraData {
    uint frustumPlaneCount;
    vec4 frustumPlanes[6];
};
mat4 gbufferPrevModelView = gbufferPreviousModelView;
layout(std430, binding = 0) GLOBAL_DATA_MODIFIER GlobalData {
    uvec4 global_dispatchSize1;
    uvec4 global_dispatchSize2;
    uvec4 global_dispatchSize3;
    uvec4 global_dispatchSize4;
    mat4 gbufferPrevModelViewInverse;
    mat4 global_shadowRotationMatrix;
    mat4 global_shadowRotationMatrixInverse;
    mat4 global_taaJitterMat;
    mat4 global_camProj;
    mat4 global_camProjInverse;
    mat4 global_prevCamProj;
    mat4 global_prevCamProjInverse;
    mat4 global_shadowProjPrev;
    mat4 global_shadowProjInversePrev;
    mat4 global_shadowProj;
    mat4 global_shadowProjInverse;
    mat4 global_shadowView;
    mat4 global_shadowViewInverse;
    vec3 global_prevCameraDelta;
    vec2 global_prevTaaJitter;
    vec2 global_taaJitter;
    CameraData global_cameraData;
    vec2 global_mipmapSizes[16];
    vec2 global_mipmapSizesRcp[16];
    ivec4 global_mipmapTiles3[16];
    ivec2 global_mipmapSizesI[16];
    ivec2 global_mipmapSizePrefixesPadded[16];
    ivec2 global_mipmapSizePrefixes[16];
    ivec4 global_hizTiles[16];
    ivec4 global_mipmapTileCeil[16];
    ivec4 global_mipmapTileCeilPadded[16];
    ivec3 global_shadowAABBMinPrev;
    ivec3 global_shadowAABBMaxPrev;
    ivec3 global_shadowAABBMin;
    ivec3 global_shadowAABBMax;
    vec3 global_shadowAABBMinHistory;
    vec3 global_shadowAABBMaxHistory;
    ivec3 global_shadowAABBMinNew;
    ivec3 global_shadowAABBMaxNew;
    float global_focusDistance;
    int global_lastWorldTime;
    float global_historyResetFactor;
    vec4 global_motionFactor;
    vec4 global_taaResetFactor;
    float global_turbidity;
    AEData global_aeData;
    uint global_atomicCounters[16];
};
layout(std430, binding = 1) GLOBAL_DATA_MODIFIER IndirectComputeData {
    uint indirectComputeData[];
};
const vec2 SHADOW_MAP_SIZE = vec2(float(shadowMapResolution), 1.0 / float(shadowMapResolution));
uniform sampler2D usam_r32f;
#define saturate(x) clamp(x, 0.0, 1.0)
ivec2 _textile_texelToTexel(ivec2 texelPos, ivec2 tileOffset, ivec2 tileSize) {
    return clamp(texelPos, ivec2(0), tileSize - 1) + tileOffset;
}
vec2 _textile_uvToUV(vec2 uv, vec2 tileOffsetF, vec2 tileSizeF, vec2 atlastSizeRcp) {
    vec2 textureTexelPos = clamp(uv * tileSizeF, vec2(0.5), tileSizeF - 0.5) + tileOffsetF;
    return saturate(textureTexelPos * atlastSizeRcp);
}
vec2 _textile_uvToGatherUV(vec2 uv, vec2 tileOffsetF, vec2 tileSizeF, vec2 atlastSizeRcp) {
    vec2 textureTexelPos = clamp(uv * tileSizeF, vec2(1.0), tileSizeF - 1.0) + tileOffsetF;
    return saturate(textureTexelPos * atlastSizeRcp);
}
vec2 _textile_texelToGatherUV(vec2 texelPos, vec2 tileOffsetF, vec2 tileSizeF, vec2 atlastSizeRcp) {
    vec2 textureTexelPos = clamp(texelPos, vec2(1.0), tileSizeF - 1.0) + tileOffsetF;
    return saturate(textureTexelPos * atlastSizeRcp);
}
#undef saturate
#define _shadesmith_R32F_ATLAS_SIZE (uval_mainImageSize * vec2(2, 2))
#define _shadesmith_R32F_ATLAS_SIZE_RCP (vec2(1.0) / _shadesmith_R32F_ATLAS_SIZE)
#define _shadesmith_R32F_2_OFFSET_F (uval_mainImageSize * vec2(1, 0))
#define _shadesmith_R32F_2_SIZE_F uval_mainImageSize
#define _shadesmith_R32F_2_TEXEL_TO_GATHER_UV(texelPos) _textile_texelToGatherUV(texelPos, _shadesmith_R32F_2_OFFSET_F, _shadesmith_R32F_2_SIZE_F, _shadesmith_R32F_ATLAS_SIZE_RCP)
#define transient_initialSampleRayCount_gatherTexel(x, c) textureGather(usam_r32f, _shadesmith_R32F_2_TEXEL_TO_GATHER_UV(x), c)
#if defined MC_GL_AMD_gpu_shader_half_float
#extension GL_AMD_gpu_shader_half_float : enable
#elif defined MC_GL_NV_gpu_shader5
#extension GL_NV_gpu_shader5 : enable
#else
#define NO_HALF
#define hf
#define float16_t float
#define f16vec2 vec2
#define f16vec3 vec3
#define f16vec4 vec4
#define f16mat2 mat2
#define f16mat3 mat3
#define f16mat4 mat4
#endif
#define PI 3.14159265358979323846
#define PI_2 (2.0 * PI)
#define PI_HALF (0.5 * PI)
#define PI_QUARTER (0.25 * PI)
#define RCP_PI (1.0 / PI)
#define RCP_PI_2 (1.0 / PI_2)
#define RCP_PI_HALF (1.0 / PI_HALF)
#define GOLDEN_RATIO 1.618033988749
#define GOLDEN_ANGLE 2.39996322972865332
#define SPHERE_SOLID_ANGLE (4.0 * PI)
#define FLT_MAX uintBitsToFloat(0x7F7FFFFF)
#define FLT_MIN uintBitsToFloat(0x00800000)
#define FLT_TRUE_MIN uintBitsToFloat(0x00000001)
#define FLT_POS_INF uintBitsToFloat(0x7F800000)
#define FLT_NEG_INF uintBitsToFloat(0xFF800000)
#define FP16_MAX 65504.0
#define INT32_MAX 2147483647
#define INT32_MIN -2147483648
#define rcp(x) (1.0 / (x))
#define saturate(x) clamp(x, 0.0, 1.0)
layout(std430, binding = 5) buffer RayDataIndices {
    uint ssbo_rayDataIndices[];
};
layout(local_size_x = 16, local_size_y = 16) in;
const vec2 workGroupsRender = vec2(0.5, 0.5);
shared uint temp[2][1024];
bool checkBinRayCount(uvec2 binIdx);
void main() {
    uint clusterIdx = gl_WorkGroupID.x + gl_WorkGroupID.y * gl_NumWorkGroups.x;
    uvec2 numClusters = (uvec2(uval_mainImageSizeI) + 31u) >> 5u;
    uint totalClusters = numClusters.x * numClusters.y;
    uvec2 binId = uvec2(clusterIdx % numClusters.x, clusterIdx / numClusters.x);
    if (checkBinRayCount(binId)) {
        if (clusterIdx < totalClusters) {
            uint clusterIndexBase = clusterIdx * 1024u + gl_LocalInvocationIndex;
            uvec4 blockIdx = uvec4(0, 256u, 512u, 768u) + gl_LocalInvocationIndex;
            uvec4 val;
            val.x = ssbo_rayDataIndices[clusterIndexBase];
            val.y = ssbo_rayDataIndices[clusterIndexBase + 256u];
            val.z = ssbo_rayDataIndices[clusterIndexBase + 512u];
            val.w = ssbo_rayDataIndices[clusterIndexBase + 768u];
            for (uint size = 2u; size <= gl_SubgroupSize; size <<= 1u) {
                for (uint stride = size >> 1u; stride > 0u; stride >>= 1u) {
                    bool isLower = gl_SubgroupInvocationID < (gl_SubgroupInvocationID ^ stride);
                    uvec4 pair = subgroupShuffleXor(val, stride);
                    bool asc = !bool(gl_SubgroupInvocationID & size);
                    bool cond = (asc != bool(gl_SubgroupID & 1u)) == isLower;
                    val = mix(max(val, pair), min(val, pair), bvec4(cond));
                }
            }
            temp[0][blockIdx.x] = val.x;
            temp[0][blockIdx.y] = val.y;
            temp[0][blockIdx.z] = val.z;
            temp[0][blockIdx.w] = val.w;
            barrier();
            uint readBuf = 0u;
            for (uint k = gl_SubgroupSize * 2u; k <= 1024u; k <<= 1u) {
                for (uint j = k >> 1u; j >= gl_SubgroupSize; j >>= 1u) {
                    uint writeBuf = readBuf ^ 1u;
                    uvec4 pair = blockIdx ^ j;
                    uvec4 a;
                    a.x = temp[readBuf][blockIdx.x];
                    a.y = temp[readBuf][blockIdx.y];
                    a.z = temp[readBuf][blockIdx.z];
                    a.w = temp[readBuf][blockIdx.w];
                    uvec4 b;
                    b.x = temp[readBuf][pair.x];
                    b.y = temp[readBuf][pair.y];
                    b.z = temp[readBuf][pair.z];
                    b.w = temp[readBuf][pair.w];
                    bvec4 cond = equal(not(bvec4(blockIdx & k)), lessThan(blockIdx, pair));
                    uvec4 result = mix(max(a, b), min(a, b), cond);
                    temp[writeBuf][blockIdx.x] = result.x;
                    temp[writeBuf][blockIdx.y] = result.y;
                    temp[writeBuf][blockIdx.z] = result.z;
                    temp[writeBuf][blockIdx.w] = result.w;
                    readBuf = writeBuf;
                    barrier();
                }
                uvec4 val;
                val.x = temp[readBuf][blockIdx.x];
                val.y = temp[readBuf][blockIdx.y];
                val.z = temp[readBuf][blockIdx.z];
                val.w = temp[readBuf][blockIdx.w];
                bvec4 ascs = not(bvec4(blockIdx & k));
                for (uint j = gl_SubgroupSize >> 1u; j > 0u; j >>= 1u) {
                    bool isLower = gl_SubgroupInvocationID < (gl_SubgroupInvocationID ^ j);
                    uvec4 pair = subgroupShuffleXor(val, j);
                    bvec4 cond = equal(ascs, bvec4(isLower));
                    val = mix(max(val, pair), min(val, pair), cond);
                }
                temp[readBuf][blockIdx.x] = val.x;
                temp[readBuf][blockIdx.y] = val.y;
                temp[readBuf][blockIdx.z] = val.z;
                temp[readBuf][blockIdx.w] = val.w;
                barrier();
            }
            ssbo_rayDataIndices[clusterIndexBase] = temp[readBuf][blockIdx.x];
            ssbo_rayDataIndices[clusterIndexBase + 256u] = temp[readBuf][blockIdx.y];
            ssbo_rayDataIndices[clusterIndexBase + 512u] = temp[readBuf][blockIdx.z];
            ssbo_rayDataIndices[clusterIndexBase + 768u] = temp[readBuf][blockIdx.w];
        }
    }
}
bool checkBinRayCount(uvec2 binIdx) {
    vec2 gatherTexel = vec2(binIdx) * 2.0 + 1.0;
    vec4 rayCounts = transient_initialSampleRayCount_gatherTexel(gatherTexel, 0);
    return any(greaterThan(rayCounts, vec4(0.5)));
}