//Volumetric lighting CS 2.
#version 450
#extension GL_EXT_nonuniform_qualifier : require
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;

struct LightData
{
    vec4 posAndRadiusWS;
    vec4 colorAndIntensity;
    vec4 lightDirectionAndFalloffExp;
    vec4 lightTangent;
    uvec4 lightTypeAndFlags;
    vec4 spotLightAngles;
    vec4 sphereAndShadowParams;
};

struct ExtendedLightData
{
    mat4 boxInverseWorldMatrix;
    mat4 viewProjectionMatrix;
    uvec4 params0;
    vec4 projectorTransform;
};

struct FlrInput
{
    uvec3 cellCoord;
};

struct SH9Compressed
{
    uvec4 data0[2];
    uvec4 data1;
};

struct FogVolumeData
{
    mat4 inverseWorldMatrix;
    vec4 albedoAndExtinction;
    vec4 fogVolumeTypeAndFalloffExponent;
    vec4 noiseTransformScale;
    vec4 noiseTransformOffset;
    uvec4 params0;
};

struct SH9Compressed_1
{
    uvec4 data0[2];
    uvec4 data1;
};

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
layout(set = 0, binding = 110, std140) uniform Vk3DParams
{
    vec4 stereo;
    vec4 custom_params;
} vk3d_params;

layout(set = 0, binding = 8, std140) uniform PerInstance
{
    mat4 invViewProjMatrix;
    mat4 invProjMatrix;
    mat4 prevViewProjMatrix;
    vec4 jitterOffsets;
    vec2 nearFar;
    vec4 nearFarWidthHeight;
    vec4 cameraPosWS;
    vec4 dirLightDirWS;
    vec4 dirLightColorAndIntens;
    uvec4 dirLightProjectorParams;
    vec4 dirLightProjectorTransform;
    mat4 dirLightProjectorViewProjMatrix;
    SH9Compressed skyModelSH;
    vec4 frameParameters0;
    vec4 volumetricsParameters0;
    uvec4 volumetricsParameters1;
    vec4 volumetricsParameters2;
    mat4 pssmViewProjMatrices[5];
    mat4 localLightViewProjMatrices[16];
    vec4 shadowParameters0;
    vec4 lightingWeights0;
    vec4 irradianceGridWorldPosition;
    vec4 irradianceGridHalfExtents;
    uvec4 irradianceGridResolution;
    vec4 tempParams;
} _758;

layout(set = 0, binding = 4, std430) buffer extendedLightBuffer
{
    ExtendedLightData _extendedLightBuffer[];
} _1050;

layout(set = 0, binding = 7, std430) buffer resourceIndexBuffer
{
    uint _resourceIndexBuffer[];
} _1535;

layout(set = 0, binding = 5, std430) buffer fogVolumeBuffer
{
    FogVolumeData _fogVolumeBuffer[];
} _1579;

layout(set = 0, binding = 3, std430) buffer lightBuffer
{
    LightData _lightBuffer[];
} _1832;

layout(set = 0, binding = 11, std430) buffer irradianceGridBuffer
{
    SH9Compressed_1 _irradianceGridBuffer[];
} _2020;

layout(set = 0, binding = 102, std430) buffer DebugBuffer
{
    uint debugBuffer[];
} _2025;

layout(set = 2, binding = 0) uniform texture2D globalTextures[];
layout(set = 2, binding = 0) uniform texture3D global3DTextures[];
layout(set = 1, binding = 1) uniform sampler linearRepeatSampler;
layout(set = 0, binding = 2) uniform sampler2DArrayShadow shadowBufferTex;
layout(set = 0, binding = 1) uniform sampler3D prevOutputTex;
layout(set = 0, binding = 10) uniform sampler3D prevLightingTex;
layout(set = 0, binding = 0, r11f_g11f_b10f) uniform writeonly image3D outputTex;
layout(set = 0, binding = 9, r11f_g11f_b10f) uniform writeonly image3D lightingTex;
layout(set = 2, binding = 0) uniform textureCube globalCubeTextures[];
layout(set = 1, binding = 0) uniform sampler linearClampSampler;
layout(set = 1, binding = 2) uniform sampler nearestClampSampler;
layout(set = 1, binding = 3) uniform sampler nearestRepeatSampler;
layout(set = 1, binding = 4) uniform sampler shadowSampler;

bool _1153;

vec3 cellCenterToNDC(vec3 cellCenter, vec4 volumetricsParams0)
{
    return (cellCenter * volumetricsParams0.xyz) + vec3(-1.0, -1.0, 0.0);
}

float linearDepthToDevice(float depthLinear, float near, float far)
{
    return -(((depthLinear + far) * near) / (depthLinear * (far - near)));
}

float volumeZToNativeDepth(float z, float depthExp, float depthRange, vec2 nearFar)
{
    float depthLinear = -((pow(abs(z), depthExp) * depthRange) + nearFar.x);
    float param = depthLinear;
    float param_1 = nearFar.x;
    float param_2 = nearFar.y;
    return linearDepthToDevice(param, param_1, param_2);
}

float deviceDepthToLinear(float deviceDepth, float near, float far)
{
    return -((far * near) / ((deviceDepth * (far - near)) + near));
}

float nativeDepthToVolumeZ(float depth, float invDepthExp, float depthRange, vec2 nearFar)
{
    float param = depth;
    float param_1 = nearFar.x;
    float param_2 = nearFar.y;
    float depthLinear = -deviceDepthToLinear(param, param_1, param_2);
    return pow(clamp((depthLinear - nearFar.x) / depthRange, 0.0, 1.0) + 9.9999999392252902907785028219223e-09, invDepthExp);
}

vec3 cellCenterSSToUVW(inout vec3 cellCenter, float invDepthExp, float depthRange, vec2 nearFar)
{
    float param = cellCenter.z;
    float param_1 = invDepthExp;
    float param_2 = depthRange;
    vec2 param_3 = nearFar;
    cellCenter.z = nativeDepthToVolumeZ(param, param_1, param_2, param_3);
    return cellCenter;
}

float square(float x)
{
    return x * x;
}

uint calcGridDepthIndex(float depthVS)
{
    return uint((24.0 * log(depthVS)) * 0.0819264352321624755859375);
}

float calcGridDepthSlice(uint depthSliceIdx)
{
    return pow(200000.0, float(depthSliceIdx) * 0.0416666679084300994873046875);
}

uvec3 clampGridPos(uvec3 gridPos)
{
    return clamp(gridPos, uvec3(0u), uvec3(15u, 7u, 23u));
}

uvec3 calcGridPosForViewPos(vec3 posVS, vec2 nearFar, vec4 nearFarWidthHeight)
{
    float param = -posVS.z;
    uint depthIdx = calcGridDepthIndex(param);
    uint param_1 = depthIdx + 1u;
    float gridEndDepth = calcGridDepthSlice(param_1);
    float rayPos = (gridEndDepth - nearFar.x) / (nearFar.y - nearFar.x);
    vec2 gridHalfWidthHeight = mix(nearFarWidthHeight.xy, nearFarWidthHeight.zw, vec2(rayPos)) * 0.5;
    vec2 localPos = posVS.xy / gridHalfWidthHeight;
    uvec3 gridPos = uvec3(uvec2(((localPos * 0.5) + vec2(0.5)) * vec2(16.0, 8.0)), depthIdx);
    uvec3 param_2 = gridPos;
    return clampGridPos(param_2);
}

uint calcClusterIndex(uvec3 gridPos)
{
    return uint(dot(vec3(gridPos), vec3(390.0, 6240.0, 49920.0))) / 2u;
}

vec4 flrSampleLevelBindless3D(sampler samplerState, vec3 uvw, float lod, uint textureIdx)
{
    return textureLod(sampler3D(global3DTextures[nonuniformEXT(textureIdx)], samplerState), uvw, lod);
}

vec4 calcPosLS(vec3 posWS, mat4 shadowViewProjMatrix)
{
    vec4 posLS = shadowViewProjMatrix * vec4(posWS, 1.0);
    vec3 _298 = posLS.xyz / posLS.www;
    posLS = vec4(_298.x, _298.y, _298.z, posLS.w);
    vec2 _306 = (posLS.xy * 0.5) + vec2(0.5);
    posLS = vec4(_306.x, _306.y, posLS.z, posLS.w);
    return posLS;
}

uint findBestFittingSplit(vec3 posWS, inout vec4 posLS)
{
    for (uint i = 0u; i < uint(_758.shadowParameters0.x); i++)
    {
        vec3 param = posWS;
        mat4 param_1 = _758.pssmViewProjMatrices[i];
        posLS = calcPosLS(param, param_1);
        bool _777 = all(lessThanEqual(posLS.xyz, vec3(1.0)));
        bool _785;
        if (_777)
        {
            _785 = all(greaterThanEqual(posLS.xyz, vec3(0.0)));
        }
        else
        {
            _785 = _777;
        }
        if (_785)
        {
            return i;
        }
    }
    return 4294967295u;
}

vec2 calcBiasedShadowUV(vec3 posWS, vec3 normalVtx, vec3 L, mat4 projMatrix, float factor)
{
    float weight = max(1.0 - max(dot(normalVtx, L), 0.0), 0.1500000059604644775390625);
    vec3 offsetWS = (normalVtx * weight) * factor;
    vec3 offsetPosWS = posWS + offsetWS;
    vec4 offsetPosLS = projMatrix * vec4(offsetPosWS, 1.0);
    vec2 offsetUV = ((offsetPosLS.xy / vec2(offsetPosLS.w)) * 0.5) + vec2(0.5);
    return offsetUV;
}

float pcf(vec3 shadowPos, uint shadowMapIdx, sampler2DArrayShadow shadowBufferTex_1)
{
    vec4 _326 = vec4(vec3(shadowPos.xy, float(shadowMapIdx)), shadowPos.z);
    return textureGrad(shadowBufferTex_1, vec4(_326.xyz, _326.w), vec2(0.0), vec2(0.0));
}

float calcDirShadowAttenuation(inout vec3 posWS, vec3 normalVtx, vec3 L, float shadowFactor)
{
    float shadowAttenuation = 1.0;
    posWS += (L * 20.0);
    vec3 param = posWS;
    vec4 param_1;
    uint _930 = findBestFittingSplit(param, param_1);
    vec4 posLS = param_1;
    uint shadowMapIdx = _930;
    if (shadowMapIdx != 4294967295u)
    {
        vec3 param_2 = posWS;
        vec3 param_3 = normalVtx;
        vec3 param_4 = L;
        mat4 param_5 = _758.pssmViewProjMatrices[shadowMapIdx];
        float param_6 = 10.0 + ((10.0 * float(shadowMapIdx)) * 1.0);
        vec2 _953 = calcBiasedShadowUV(param_2, param_3, param_4, param_5, param_6);
        posLS = vec4(_953.x, _953.y, posLS.z, posLS.w);
        vec3 param_7 = posLS.xyz;
        uint param_8 = shadowMapIdx;
        shadowAttenuation = pcf(param_7, param_8, shadowBufferTex);
    }
    return mix(1.0, shadowAttenuation, shadowFactor);
}

vec4 flrSampleLevelBindless(sampler samplerState, vec2 uv, float lod, uint textureIdx)
{
    return textureLod(sampler2D(globalTextures[nonuniformEXT(textureIdx)], samplerState), uv, lod);
}

vec3 calcDirProjectorAttenuation(vec3 posWS)
{
    vec3 att = vec3(1.0);
    uint projImageId = _758.dirLightProjectorParams.x;
    if (projImageId != 4294967295u)
    {
        vec3 posLS = (_758.dirLightProjectorViewProjMatrix * vec4(posWS, 1.0)).xyw;
        vec2 _822 = posLS.xy / vec2(posLS.z);
        posLS = vec3(_822.x, _822.y, posLS.z);
        vec2 _832 = posLS.xy * _758.dirLightProjectorTransform.xy;
        posLS = vec3(_832.x, _832.y, posLS.z);
        vec2 _848 = posLS.xy + ((_758.dirLightProjectorTransform.zw * _758.frameParameters0.x) * _758.dirLightProjectorTransform.xy);
        posLS = vec3(_848.x, _848.y, posLS.z);
        vec2 _855 = (posLS.xy * 0.5) + vec2(0.5);
        posLS = vec3(_855.x, _855.y, posLS.z);
        float projectorFactor = uintBitsToFloat(_758.dirLightProjectorParams.y);
        vec2 param = posLS.xy;
        float param_1 = 0.0;
        uint param_2 = projImageId;
        att *= max(mix(vec3(1.0), flrSampleLevelBindless(linearRepeatSampler, param, param_1, param_2).xyz, vec3(projectorFactor)), vec3(0.0));
    }
    return att;
}

bool isCustomPointLight(LightData ld)
{
    if (ld.lightTypeAndFlags.w == 3718345796u)
    {
        return true;
    }
    else
    {
        return false;
    }
}

vec3 calcLocalLightShadowAttenuation(LightData ld, inout vec3 posWS, vec3 shadowBiasNormal)
{
    vec3 L = normalize(ld.posAndRadiusWS.xyz - posWS);
    posWS += (L * 20.0);
    uint localShadowBufferIdx = floatBitsToUint(ld.sphereAndShadowParams.z);
    float shadowAtt = 1.0;
    if (localShadowBufferIdx != 255u)
    {
        uint shadowBufferIdx = floatBitsToUint(ld.sphereAndShadowParams.w);
        vec3 param = posWS;
        mat4 param_1 = _758.localLightViewProjMatrices[localShadowBufferIdx];
        vec3 posLS = calcPosLS(param, param_1).xyz;
        vec3 param_2 = posWS;
        vec3 param_3 = shadowBiasNormal;
        vec3 param_4 = L;
        mat4 param_5 = _758.localLightViewProjMatrices[localShadowBufferIdx];
        float param_6 = 2.0;
        vec2 _1015 = calcBiasedShadowUV(param_2, param_3, param_4, param_5, param_6);
        posLS = vec3(_1015.x, _1015.y, posLS.z);
        vec3 param_7 = posLS;
        uint param_8 = shadowBufferIdx;
        shadowAtt = pcf(param_7, param_8, shadowBufferTex);
    }
    float shadowFactor = ld.lightTangent.w;
    vec3 coloredAtt = mix(vec3(1.0), vec3(shadowAtt), vec3(shadowFactor));
    uint flags = (ld.lightTypeAndFlags.x >> 16u) & 65535u;
    if ((flags & 4u) > 0u)
    {
        uint _1058 = ld.lightTypeAndFlags.y + (floatBitsToUint(_758.frameParameters0.w) * 128u);
        ExtendedLightData eld;
        eld.boxInverseWorldMatrix = _1050._extendedLightBuffer[_1058].boxInverseWorldMatrix;
        eld.viewProjectionMatrix = _1050._extendedLightBuffer[_1058].viewProjectionMatrix;
        eld.params0 = _1050._extendedLightBuffer[_1058].params0;
        eld.projectorTransform = _1050._extendedLightBuffer[_1058].projectorTransform;
        vec3 posLS_1 = (eld.viewProjectionMatrix * vec4(posWS, 1.0)).xyw;
        vec2 _1086 = posLS_1.xy / vec2(posLS_1.z);
        posLS_1 = vec3(_1086.x, _1086.y, posLS_1.z);
        vec2 _1094 = posLS_1.xy * eld.projectorTransform.xy;
        posLS_1 = vec3(_1094.x, _1094.y, posLS_1.z);
        vec2 _1109 = posLS_1.xy + ((eld.projectorTransform.zw * _758.frameParameters0.x) * eld.projectorTransform.xy);
        posLS_1 = vec3(_1109.x, _1109.y, posLS_1.z);
        vec2 _1116 = (posLS_1.xy * 0.5) + vec2(0.5);
        posLS_1 = vec3(_1116.x, _1116.y, posLS_1.z);
        float projectorFactor = uintBitsToFloat(eld.params0.y);
        vec2 param_9 = posLS_1.xy;
        float param_10 = 0.0;
        uint param_11 = eld.params0.x;
        vec3 projAtt = mix(vec3(1.0), flrSampleLevelBindless(linearRepeatSampler, param_9, param_10, param_11).xyz, vec3(projectorFactor));
        coloredAtt *= projAtt;
    }
    return coloredAtt;
}

float calcDist2(vec3 lightDistVec, float biasSqr)
{
    return dot(lightDistVec, lightDistVec) + biasSqr;
}

float calcInverseSqrRadiusCutoff(float dist2, float radius)
{
    float rcA = 1.0 / radius;
    float param = rcA;
    float rcB = square(param) * dist2;
    float param_1 = rcB;
    float rcC = clamp(1.0 - square(param_1), 0.0, 1.0);
    float param_2 = rcC;
    return square(param_2);
}

float calcInverseSqrFalloff(inout float dist2)
{
    dist2 *= 9.9999997473787516355514526367188e-05;
    return 1.0 / (dist2 + 1.0);
}

float calculateAreaLightFalloff(vec3 lightDistVec, LightData ld, inout float NdL, vec3 N, float biasSqr)
{
    vec3 param = lightDistVec;
    float param_1 = biasSqr;
    float dist2 = calcDist2(param, param_1);
    float param_2 = dist2;
    float param_3 = ld.posAndRadiusWS.w;
    float radialCutoff = calcInverseSqrRadiusCutoff(param_2, param_3);
    float radialFalloff = 1.0;
    if (ld.sphereAndShadowParams.y > 9.9999999392252902907785028219223e-09)
    {
        vec3 L0 = ld.lightTangent.xyz * ld.sphereAndShadowParams.y;
        vec3 L1 = lightDistVec - (L0 * 0.5);
        vec3 L2 = lightDistVec + (L0 * 0.5);
        float L1Sqr = dot(L1, L1) + biasSqr;
        float L2Sqr = dot(L2, L2) + biasSqr;
        float L1RSqr = inversesqrt(L1Sqr);
        float L2RSqr = inversesqrt(L2Sqr);
        float L1Len = L1Sqr * L1RSqr;
        float L2Len = L2Sqr * L2RSqr;
        radialFalloff = 10000.0 / (((L1Len * L2Len) + dot(L1, L2)) * 0.5);
        NdL = clamp(0.5 * ((dot(N, L1) * L1RSqr) + (dot(N, L2) * L2RSqr)), 0.0, 1.0);
    }
    else
    {
        float param_4 = dist2;
        float _462 = calcInverseSqrFalloff(param_4);
        radialFalloff = _462;
    }
    return radialCutoff * radialFalloff;
}

float calcExponentialFalloff(LightData ld, vec3 lightDistVec)
{
    vec3 nlv = lightDistVec / vec3(ld.posAndRadiusWS.w + 0.001000000047497451305389404296875);
    float d2 = dot(nlv, nlv);
    return pow(abs(1.0 - clamp(d2, 0.0, 1.0)), ld.lightDirectionAndFalloffExp.w);
}

float calcSpotLightAttenuation(LightData ld, vec3 L)
{
    float att = clamp((dot(L, -ld.lightDirectionAndFalloffExp.xyz) - ld.spotLightAngles.x) * ld.spotLightAngles.y, 0.0, 1.0);
    float param = att;
    return square(param);
}

float calcExponentialFalloff(LightData ld, float _distance)
{
    float d2 = _distance * _distance;
    return pow(abs(1.0 - clamp(d2, 0.0, 1.0)), ld.lightDirectionAndFalloffExp.w);
}

float calcBoxLightAttenuation(vec3 posWS, LightData ld, ExtendedLightData eld)
{
    vec3 posBS = (eld.boxInverseWorldMatrix * vec4(posWS, 1.0)).xyz;
    float ret = 0.0;
    bool _539 = all(lessThanEqual(posBS, vec3(1.0)));
    bool _547;
    if (_539)
    {
        _547 = all(greaterThanEqual(posBS, vec3(-1.0)));
    }
    else
    {
        _547 = _539;
    }
    if (_547)
    {
        vec2 a = vec2(1.0) - (clamp(abs(posBS.xz) - vec2(0.800000011920928955078125), vec2(0.0), vec2(1.0)) / vec2(0.20000000298023223876953125));
        ret = min(a.x, a.y);
        LightData param = ld;
        float param_1 = (posBS.y * 0.5) + 0.5;
        ret *= calcExponentialFalloff(param, param_1);
    }
    return ret;
}

void calculateDiffuseLocalLightLighting(LightData ld, vec3 posWS, vec3 shadowBiasNormal, inout vec3 outLighting, inout vec3 factor, float attBiasSqr)
{
    LightData param = ld;
    if (isCustomPointLight(param))
    {
        return;
    }
    uint lightType = ld.lightTypeAndFlags.x & 65535u;
    if ((lightType == 2u) || (lightType == 1u))
    {
        LightData param_1 = ld;
        vec3 param_2 = posWS;
        vec3 param_3 = shadowBiasNormal;
        vec3 _1177 = calcLocalLightShadowAttenuation(param_1, param_2, param_3);
        factor *= _1177;
    }
    if (lightType != 2u)
    {
        vec3 lightDistVec = ld.posAndRadiusWS.xyz - posWS;
        float dist = length(lightDistVec);
        vec3 L = lightDistVec / vec3(dist);
        float att = 1.0;
        if ((((ld.lightTypeAndFlags.x >> 16u) & 65535u) & 1u) == 0u)
        {
            vec3 param_4 = lightDistVec;
            LightData param_5 = ld;
            float NdL;
            float param_6 = NdL;
            vec3 param_7 = vec3(0.0, 1.0, 0.0);
            float param_8 = attBiasSqr;
            float _1218 = calculateAreaLightFalloff(param_4, param_5, param_6, param_7, param_8);
            NdL = param_6;
            att = _1218;
        }
        else
        {
            LightData param_9 = ld;
            vec3 param_10 = lightDistVec;
            att = calcExponentialFalloff(param_9, param_10);
        }
        if (lightType == 1u)
        {
            LightData param_11 = ld;
            vec3 param_12 = L;
            att *= calcSpotLightAttenuation(param_11, param_12);
            att *= clamp(max(dist - ld.spotLightAngles.w, 0.0) / ld.spotLightAngles.w, 0.0, 1.0);
        }
        vec3 lighting = ((ld.colorAndIntensity.xyz * att) * ld.colorAndIntensity.w) / vec3(3.1415927410125732421875);
        outLighting += (lighting * factor);
    }
    else
    {
        uint _1273 = ld.lightTypeAndFlags.y + (floatBitsToUint(_758.frameParameters0.w) * 128u);
        ExtendedLightData eld;
        eld.boxInverseWorldMatrix = _1050._extendedLightBuffer[_1273].boxInverseWorldMatrix;
        eld.viewProjectionMatrix = _1050._extendedLightBuffer[_1273].viewProjectionMatrix;
        eld.params0 = _1050._extendedLightBuffer[_1273].params0;
        eld.projectorTransform = _1050._extendedLightBuffer[_1273].projectorTransform;
        vec3 param_13 = posWS;
        LightData param_14 = ld;
        ExtendedLightData param_15 = eld;
        float att_1 = calcBoxLightAttenuation(param_13, param_14, param_15);
        if (att_1 > 9.9999999392252902907785028219223e-09)
        {
            outLighting += ((((ld.colorAndIntensity.xyz * att_1) * ld.colorAndIntensity.w) * factor) / vec3(3.1415927410125732421875));
        }
    }
}

float luminance(vec3 x)
{
    return dot(x, vec3(0.2125999927520751953125, 0.715200006961822509765625, 0.072200000286102294921875));
}

void flrMain(FlrInput flrInput)
{
    vec3 cellCenter = vec3(flrInput.cellCoord) + vec3(0.5);
    
    
    
    float reprojWeight = 0.97500002384185791015625;
    vec3 param = cellCenter;
    vec4 param_1 = _758.volumetricsParameters0;
    
    vec3 cellCenterNDCbak = cellCenterToNDC(param, param_1);
    
    
    //parama.x-= vk3d_params.stereo.x*240;
    //parama.x+= vk3d_params.stereo.x*vk3d_params.stereo.y/(cellCenterNDCbak.z*12);
    
    
    vec3 cellCenterNDCb = cellCenterToNDC(param, param_1);
    float cellCenterVolumeZb = cellCenterNDCb.z;
    float param_2b = cellCenterVolumeZb;
    float param_3b = _758.volumetricsParameters2.x;
    float param_4b = _758.volumetricsParameters2.z;
    vec2 param_5b = _758.nearFar;
    cellCenterNDCb.z = volumeZToNativeDepth(param_2b, param_3b, param_4b, param_5b);
    
    vec3 parama = param;
    
    //Fix here.
    parama.x+= vk3d_params.stereo.x*vk3d_params.stereo.y*cellCenterNDCb.z*48;
    //parama.x+= vk3d_params.stereo.x*vk3d_params.stereo.y*cellCenterNDCb.z*_758.nearFarWidthHeight.x*6/_758.nearFar.x;
    
    vec3 cellCenterNDCa = cellCenterToNDC(parama, param_1);
    float cellCenterVolumeZa = cellCenterNDCa.z;
    float param_2a = cellCenterVolumeZa;
    float param_3a = _758.volumetricsParameters2.x;
    float param_4a = _758.volumetricsParameters2.z;
    vec2 param_5a = _758.nearFar;
    cellCenterNDCa.z = volumeZToNativeDepth(param_2a, param_3a, param_4a, param_5a);
    
    cellCenterNDCbak.z = volumeZToNativeDepth(cellCenterNDCbak.z, param_3a, param_4a, param_5a);
    
    vec4 reprojResulta = vec4(0.0);
    vec4 reprojResultUnscaleda = vec4(0.0);
    vec4 cellCenterWSa = _758.invViewProjMatrix * vec4(cellCenterNDCa, 1.0);
    vec3 _1361a = cellCenterWSa.xyz / vec3(cellCenterWSa.w);
    cellCenterWSa = vec4(_1361a.x, _1361a.y, _1361a.z, cellCenterWSa.w);
    vec4 prevCellCenterSSa = _758.prevViewProjMatrix * vec4(cellCenterWSa.xyz, 1.0);
    vec3 _1378a = prevCellCenterSSa.xyz / prevCellCenterSSa.www;
    prevCellCenterSSa = vec4(_1378a.x, _1378a.y, _1378a.z, prevCellCenterSSa.w);
    vec2 _1385a = (prevCellCenterSSa.xy * 0.5) + vec2(0.5);
    prevCellCenterSSa = vec4(_1385a.x, _1385a.y, prevCellCenterSSa.z, prevCellCenterSSa.w);
    bool _1391a = any(lessThan(prevCellCenterSSa.xyz, vec3(0.0)));
    bool _1399a;
    if (!_1391a)
    {
        _1399a = any(greaterThan(prevCellCenterSSa.xyz, vec3(1.0)));
    }
    else
    {
        _1399a = _1391a;
    }
    vec3 param_6a = prevCellCenterSSa.xyz;
    
    vec3 cellCenterNDCc = vec3(cellCenterNDCbak.xy, cellCenterVolumeZa) + _758.jitterOffsets.xyz;
    
    //param.x-= vk3d_params.stereo.x*120.0;
    //param.x+= vk3d_params.stereo.x*param_4a/640;
    //Fix here.
    param.x+= vk3d_params.stereo.x*vk3d_params.stereo.y*cellCenterNDCbak.z*24.0;
    //param.x+= vk3d_params.stereo.x*vk3d_params.stereo.y*cellCenterNDCbak.z*_758.nearFarWidthHeight.x*3/_758.nearFar.x;
    
    vec3 cellCenterNDC = cellCenterToNDC(param, param_1);
    float cellCenterVolumeZ = cellCenterNDC.z;
    float param_2 = cellCenterVolumeZ;
    float param_3 = _758.volumetricsParameters2.x;
    float param_4 = _758.volumetricsParameters2.z;
    vec2 param_5 = _758.nearFar;
    cellCenterNDC.z = volumeZToNativeDepth(param_2, param_3, param_4, param_5);
    vec4 reprojResult = vec4(0.0);
    vec4 reprojResultUnscaled = vec4(0.0);
    vec4 cellCenterWS = _758.invViewProjMatrix * vec4(cellCenterNDC, 1.0);
    vec3 _1361 = cellCenterWS.xyz / vec3(cellCenterWS.w);
    cellCenterWS = vec4(_1361.x, _1361.y, _1361.z, cellCenterWS.w);
    vec4 prevCellCenterSS = _758.prevViewProjMatrix * vec4(cellCenterWS.xyz, 1.0);
    vec3 _1378 = prevCellCenterSS.xyz / prevCellCenterSS.www;
    prevCellCenterSS = vec4(_1378.x, _1378.y, _1378.z, prevCellCenterSS.w);
    vec2 _1385 = (prevCellCenterSS.xy * 0.5) + vec2(0.5);
    prevCellCenterSS = vec4(_1385.x, _1385.y, prevCellCenterSS.z, prevCellCenterSS.w);
    bool _1391 = any(lessThan(prevCellCenterSS.xyz, vec3(0.0)));
    bool _1399;
    if (!_1391)
    {
        _1399 = any(greaterThan(prevCellCenterSS.xyz, vec3(1.0)));
    }
    else
    {
        _1399 = _1391;
    }
    reprojWeight *= (_1399 ? 0.5 : 1.0);
    if (vk3d_params.custom_params.w==0.0) {
        prevCellCenterSS = prevCellCenterSSa;
    }
    vec3 param_6 = prevCellCenterSS.xyz;
    float param_7 = _758.volumetricsParameters2.y;
    float param_8 = _758.volumetricsParameters2.z;
    vec2 param_9 = _758.nearFar;
    if (vk3d_params.custom_params.w!=0.0) {
        param_6a = param_6;
    }
    vec3 _1418 = cellCenterSSToUVW(param_6, param_7, param_8, param_9);
    reprojResult = textureLod(prevOutputTex, _1418, 0.0);
    vec3 param_10 = prevCellCenterSS.xyz;
    float param_11 = _758.volumetricsParameters2.y;
    float param_12 = _758.volumetricsParameters2.z;
    vec2 param_13 = _758.nearFar;
    vec3 _1434 = cellCenterSSToUVW(param_10, param_11, param_12, param_13);
    reprojResultUnscaled = textureLod(prevLightingTex, _1434, 0.0);
    cellCenterNDC = vec3(cellCenterNDC.xy, cellCenterVolumeZ) + _758.jitterOffsets.xyz;
    cellCenterVolumeZ = cellCenterNDC.z;
    float param_14 = cellCenterVolumeZ;
    float param_15 = _758.volumetricsParameters2.x;
    float param_16 = _758.volumetricsParameters2.z;
    vec2 param_17 = _758.nearFar;
    cellCenterNDC.z = volumeZToNativeDepth(param_14, param_15, param_16, param_17);
    vec4 cellCenterWS_1 = _758.invViewProjMatrix * vec4(cellCenterNDC, 1.0);
    vec3 _1474 = cellCenterWS_1.xyz / cellCenterWS_1.www;
    cellCenterWS_1 = vec4(_1474.x, _1474.y, _1474.z, cellCenterWS_1.w);
    vec4 cellCenterVS = _758.invProjMatrix * vec4(cellCenterNDC, 1.0);
    vec3 _1490 = cellCenterVS.xyz / cellCenterVS.www;
    cellCenterVS = vec4(_1490.x, _1490.y, _1490.z, cellCenterVS.w);
    float param_18 = 80.0;
    float attenuationBiasSqr = square(param_18);
    vec3 rayWS = normalize(cellCenterWS_1.xyz - _758.cameraPosWS.xyz);
    vec3 param_19 = cellCenterVS.xyz;
    vec2 param_20 = _758.nearFar;
    vec4 param_21 = _758.nearFarWidthHeight;
    uvec3 clusterGridPos = calcGridPosForViewPos(param_19, param_20, param_21);
    uvec3 param_22 = clusterGridPos;
    uint clusterIdx = calcClusterIndex(param_22);
    float diffuseFactor = _758.lightingWeights0.x * 1.0;
    float irradFactor = _758.lightingWeights0.z * 1.0;
    float extinction = 0.0;
    vec3 albedo = vec3(0.0);
    uint fogVolumeCount = (_1535._resourceIndexBuffer[clusterIdx + (floatBitsToUint(_758.frameParameters0.w) * 599040u)] >> uint(16)) & 65535u;
    float weight = 0.0;
    FogVolumeData fv;
    for (uint i = 0u; i < fogVolumeCount; i++)
    {
        uint fogVolumeIndex = (_1535._resourceIndexBuffer[((clusterIdx + (i * 3u)) + 3u) + (floatBitsToUint(_758.frameParameters0.w) * 599040u)] >> uint(16)) & 65535u;
        uint _1586 = fogVolumeIndex + (floatBitsToUint(_758.frameParameters0.w) * 32u);
        fv.inverseWorldMatrix = _1579._fogVolumeBuffer[_1586].inverseWorldMatrix;
        fv.albedoAndExtinction = _1579._fogVolumeBuffer[_1586].albedoAndExtinction;
        fv.fogVolumeTypeAndFalloffExponent = _1579._fogVolumeBuffer[_1586].fogVolumeTypeAndFalloffExponent;
        fv.noiseTransformScale = _1579._fogVolumeBuffer[_1586].noiseTransformScale;
        fv.noiseTransformOffset = _1579._fogVolumeBuffer[_1586].noiseTransformOffset;
        fv.params0 = _1579._fogVolumeBuffer[_1586].params0;
        vec3 posFogVolume = (fv.inverseWorldMatrix * vec4(cellCenterWS_1.xyz, 1.0)).xyz;
        float dist = clamp(length(posFogVolume), 0.0, 1.0);
        bool _1619 = all(lessThanEqual(posFogVolume, vec3(1.0)));
        bool _1625;
        if (_1619)
        {
            _1625 = all(greaterThanEqual(posFogVolume, vec3(-1.0)));
        }
        else
        {
            _1625 = _1619;
        }
        if (_1625)
        {
            float falloffExponent = fv.fogVolumeTypeAndFalloffExponent.y;
            float fade = 1.0;
            if (fv.fogVolumeTypeAndFalloffExponent.x == 0.0)
            {
                vec3 boxFade = pow(abs(vec3(1.0) - abs(posFogVolume)), vec3(falloffExponent, falloffExponent, falloffExponent));
                fade = min(boxFade.x, min(boxFade.y, boxFade.z));
            }
            else
            {
                if (fv.fogVolumeTypeAndFalloffExponent.x == 1.0)
                {
                    fade = pow(1.0 - dist, falloffExponent);
                }
                else
                {
                    if (fv.fogVolumeTypeAndFalloffExponent.x == 2.0)
                    {
                        float hemisphere = dot(vec3(0.0, 1.0, 0.0), posFogVolume);
                        fade = float(hemisphere >= 0.0);
                        fade *= pow(1.0 - dist, falloffExponent);
                    }
                }
            }
            if (fv.params0.x != 4294967295u)
            {
                vec3 noiseUV = (cellCenterWS_1.xyz * fv.noiseTransformScale.xyz) + (fv.noiseTransformOffset.xyz * _758.frameParameters0.x);
                vec3 param_23 = noiseUV;
                float param_24 = 0.0;
                uint param_25 = fv.params0.x;
                fv.albedoAndExtinction.w *= clamp(flrSampleLevelBindless3D(linearRepeatSampler, param_23, param_24, param_25).x + fv.noiseTransformOffset.w, fv.noiseTransformScale.w, 1.0);
            }
            fv.albedoAndExtinction *= fade;
            extinction += fv.albedoAndExtinction.w;
            albedo += (fv.albedoAndExtinction.xyz * fv.albedoAndExtinction.w);
            weight += fade;
        }
    }
    albedo /= vec3((weight > 9.9999999392252902907785028219223e-09) ? weight : 1.0);
    vec3 lighting = vec3(0.0);
    vec3 shadowBiasNormal = -_758.dirLightDirWS.xyz;
    float shadowFactor = _758.dirLightDirWS.w;
    vec3 param_26 = cellCenterWS_1.xyz;
    vec3 param_27 = shadowBiasNormal;
    vec3 param_28 = _758.dirLightDirWS.xyz;
    float param_29 = shadowFactor;
    float _1772 = calcDirShadowAttenuation(param_26, param_27, param_28, param_29);
    vec3 param_30 = cellCenterWS_1.xyz;
    vec3 attenuation = vec3(_1772) * calcDirProjectorAttenuation(param_30);
    lighting += (((attenuation * _758.dirLightColorAndIntens.xyz) * _758.dirLightColorAndIntens.w) * diffuseFactor);
    vec3 lightingUnscaled = lighting;
    uint lightCount = _1535._resourceIndexBuffer[clusterIdx + (floatBitsToUint(_758.frameParameters0.w) * 599040u)] & 65535u;
    LightData ld;
    for (uint i_1 = 0u; i_1 < lightCount; i_1++)
    {
        uint lightIndex = _1535._resourceIndexBuffer[((clusterIdx + (i_1 * 3u)) + 3u) + (floatBitsToUint(_758.frameParameters0.w) * 599040u)] & 65535u;
        uint _1838 = lightIndex + (floatBitsToUint(_758.frameParameters0.w) * 128u);
        ld.posAndRadiusWS = _1832._lightBuffer[_1838].posAndRadiusWS;
        ld.colorAndIntensity = _1832._lightBuffer[_1838].colorAndIntensity;
        ld.lightDirectionAndFalloffExp = _1832._lightBuffer[_1838].lightDirectionAndFalloffExp;
        ld.lightTangent = _1832._lightBuffer[_1838].lightTangent;
        ld.lightTypeAndFlags = _1832._lightBuffer[_1838].lightTypeAndFlags;
        ld.spotLightAngles = _1832._lightBuffer[_1838].spotLightAngles;
        ld.sphereAndShadowParams = _1832._lightBuffer[_1838].sphereAndShadowParams;
        float factor = ld.spotLightAngles.z * diffuseFactor;
        vec3 lightLighting = vec3(0.0);
        LightData param_31 = ld;
        vec3 param_32 = cellCenterWS_1.xyz;
        vec3 param_33 = shadowBiasNormal;
        vec3 param_34 = lightLighting;
        vec3 param_35 = vec3(1.0);
        float param_36 = attenuationBiasSqr;
        calculateDiffuseLocalLightLighting(param_31, param_32, param_33, param_34, param_35, param_36);
        lightLighting = param_34;
        lighting += (lightLighting * factor);
        lightingUnscaled += lightLighting;
    }
    vec4 result = vec4(albedo * lighting, extinction);
    vec4 resultUnscaled = vec4(lightingUnscaled, 0.0);
    float minReprojWeight = max(reprojWeight - 0.25, 0.0);
    vec3 param_37 = result.xyz;
    float lum0 = luminance(param_37);
    vec3 param_38 = reprojResult.xyz;
    float lum1 = luminance(param_38);
    float weightFactor = 1.0 - (abs(lum0 - lum1) / max(lum0, max(lum1, 0.20000000298023223876953125)));
    float param_39 = weightFactor;
    reprojWeight = max(reprojWeight * square(param_39), minReprojWeight);
    float temporalWeight = mix(1.0, reprojWeight, _758.tempParams.x);
    if (temporalWeight < 1.0 && vk3d_params.custom_params.w==0.0)
    {
        result = mix(result, reprojResult, vec4(reprojWeight));
        resultUnscaled = mix(resultUnscaled, reprojResultUnscaled, vec4(reprojWeight));
    }
    result = clamp(result, vec4(0.0), vec4(1000.0));
    resultUnscaled = min(resultUnscaled, vec4(65000.0));
    if (all(lessThan(flrInput.cellCoord, _758.volumetricsParameters1.xyz)))
    {
        imageStore(outputTex, ivec3(flrInput.cellCoord), vec4(result));
        imageStore(lightingTex, ivec3(flrInput.cellCoord), vec4(resultUnscaled));
    }
}

void main()
{
    FlrInput flrInput;
    flrInput.cellCoord = gl_GlobalInvocationID;
    FlrInput param = flrInput;
    flrMain(param);
}
















