//GLOBAL SHINNY REFLECTIONS - FIXED
// Vk3DVision (http://3dsurroundgaming.com/) - 
#version 450
#extension GL_EXT_nonuniform_qualifier : require
#extension GL_KHR_shader_subgroup_arithmetic : require

struct S_CLUSTER_CELL
{
    uint LightCount;
    uint LightFirstIndex;
};

struct S_IBL
{
    mat4 _mVsToLs[2];
    mat4 _mLsToWs;
    vec4 _vfExtentSceneZone;
    vec4 _vfParameters;
    vec4 _vfLsBoxMin;
    vec4 _vfLsBoxMax;
    uint _uUVSpecularEnable;
    float _fUVSpecularBlend;
    uint _uiSpecular0TextureIndex;
    uint _uiSpecular1TextureIndex;
};

struct S_IBL_ARRAY
{
    S_IBL _aArray[128];
};

struct S_IBL_PASS
{
    mat4 _mInvProjMatrix[2];
    vec4 _vfProjSetup;
    vec4 _vfViewportTranslationScaling;
    ivec4 _viNoiseOffset;
    uint _uEyeIndex;
    uint _uEyeIndexAlignment;
    uint _uDebugDisplay;
    float _fFarPlaneZ;
};

struct S_CASCADED_SHADOW
{
    mat4 _mCascadeMatrix[2][8];
    mat4 _mViewSpaceToStaticShadowMapSpace[2];
    vec4 _vfCascadeShadowAttenuation;
    vec4 _vfShadowCascadeTextureTexelSize;
    vec4 _vfStaticShadowBlurRadius;
    vec4 _vfCascadeLinearZProjFactors[8];
    vec4 _vfCascadeParams[8];
};

struct S_CLUSTER_LIGHT_SETUP
{
    S_CASCADED_SHADOW _CascadedShadow;
    vec4 _vfSkinUVParamsAndNormalBias;
    vec4 _vfShadowTextureTexelSize;
    vec4 _vfDummy_SkinFresnelF0AndF90;
    uint _uiCascadedUsed;
    float _fSSSWidth;
    float _fOffsetBias;
    float _fDummy;
};

struct S_CLUSTER_INFO
{
    mat4 _mViewToClusterView[2];
    mat4 _mClusterProjMatrix;
    mat4 _mClusterInvProjMatrix;
    vec4 _vfLeftEyeToCombinedEyeVsDeltaXZ;
    uint _uiFinestMipWidth;
    uint _uiFinestMipHeight;
    uint _uiFinestMipDepth;
    uint _uiFinestMipWidthHeight;
    float _fZSliceGeomFactor;
    float _fRcpLogZSliceGeomFactor;
    float _fDummy0;
    float _fDummy1;
    float _fNearP;
    float _fInvNearP;
    float _fFarP;
    float _fClusterCoincidentWithCamera;
};

struct S_CLUSTER_LIGHT_SETUP_INFO
{
    S_CLUSTER_LIGHT_SETUP _LightSetup;
    S_CLUSTER_INFO _ClusterInfo;
};

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

layout(set = 0, binding = 24, std430) readonly buffer stc_g_rbClusterCells_Layout
{
    S_CLUSTER_CELL g_rbClusterCells[];
} _481;

layout(set = 0, binding = 25, std430) readonly buffer stc_g_rbClusterIndices_Layout
{
    uint g_rbClusterIndices[];
} _509;

layout(set = 0, binding = 54, std140) uniform dyn_CONSTANT_BUFFER_BLOCK_2
{
    S_IBL_ARRAY g_cbIBLArray;
} _538;

layout(set = 0, binding = 53, std140) uniform dyn_CONSTANT_BUFFER_BLOCK_1
{
    S_IBL_PASS g_cbIBLPass;
} _836;

layout(set = 0, binding = 52, std140) uniform dyn_CONSTANT_BUFFER_BLOCK_0
{
    S_CLUSTER_LIGHT_SETUP_INFO g_cbClusterLightSetupInfo;
} _989;

layout(set = 0, binding = 4) uniform samplerCube g_rbCubeTextures[];
layout(set = 0, binding = 5) uniform usampler2D g_tsNormalRoughness;
layout(set = 0, binding = 6) uniform sampler2D g_tsDepth;
layout(set = 0, binding = 7) uniform sampler2D g_tsGISpecular;
layout(set = 0, binding = 8) uniform sampler2D g_tsTAAFlags;
layout(set = 0, binding = 9) uniform sampler2D g_tsSceneZoneID;

layout(location = 8) in vec2 CsFrag;
layout(location = 0) out uint OutSpecularColor;

vec3 _2403;

void main()
{
    mat4 _2015[2];
    for (;;)
    {
        vec2 _1298 = (((CsFrag * 0.5) + vec2(0.5)) * _836.g_cbIBLPass._vfViewportTranslationScaling.zw) + _836.g_cbIBLPass._vfViewportTranslationScaling.xy;
        vec4 _871 = texture(g_tsGISpecular, _1298);
        OutSpecularColor = 0u;
        bool _876 = _871.x > 0.0;
        bool _883;
        if (!_876)
        {
            _883 = _871.y > 0.0;
        }
        else
        {
            _883 = _876;
        }
        bool _890;
        if (!_883)
        {
            _890 = _871.z > 0.0;
        }
        else
        {
            _890 = _883;
        }
        bool _897;
        if (_890)
        {
            _897 = _836.g_cbIBLPass._uDebugDisplay != 1u;
        }
        else
        {
            _897 = _890;
        }
        if (_897)
        {
            if ((int(texture(g_tsTAAFlags, _1298).x * 255.0) & 128) == 0)
            {
                break;
            }
            float _949 = _871.w;
            ivec2 _955 = ivec2(gl_FragCoord.xy);
            uvec4 _1329 = texelFetch(g_tsNormalRoughness, _955, 0);
            uint _1330 = _1329.x;
            vec3 _1363 = vec3((vec2(float((_1330 >> uint(0)) & 32767u) * 3.0518509447574615478515625e-05, float((_1330 >> uint(15)) & 32767u) * 3.0518509447574615478515625e-05) * 3.55539989471435546875) - vec2(1.777699947357177734375), 1.0);
            float _1367 = 2.0 / dot(_1363, _1363);
            vec2 _1371 = _1363.xy * _1367;
            vec3 _2332 = vec3(_1371.x, _1371.y, _2403.z);
            _2332.z = _1367 - 1.0;

			// CHANGE this to VEC4 instead of VEC3
            vec4 _975 = (_836.g_cbIBLPass._mInvProjMatrix[_836.g_cbIBLPass._uEyeIndex] * vec4(CsFrag, 1.0, 1.0)).xyzw * ((_836.g_cbIBLPass._vfProjSetup.y / (texture(g_tsDepth, _1298).x + _836.g_cbIBLPass._vfProjSetup.x)) * _836.g_cbIBLPass._fFarPlaneZ);
            vec4 _1389 = vec4(_975.xyz, 1.0);
            vec4 _1390 = _989.g_cbClusterLightSetupInfo._ClusterInfo._mClusterProjMatrix * _975;
            vec3 _1396 = _1390.xyz / vec3(_1390.w);
            
            // HMM why the FACTOR?! The depth is not updated with the FOV? We need the FOV here
            float t = _989.g_cbClusterLightSetupInfo._ClusterInfo._mClusterProjMatrix[1][1];
            const float PI = 3.1415926538;
            const float rad2deg = 180 / PI;
            float fov = atan(1.0f /t) *2.0 * rad2deg / 62; 
            
            // Add the FOV into account always!
            _975.x -= vk3d_params.stereo.x * (_1390.w - vk3d_params.stereo.y) * fov; 

			// REDO the original work
            _1389 = vec4(_975.xyz, 1.0);
            _1390 = _989.g_cbClusterLightSetupInfo._ClusterInfo._mClusterProjMatrix * _1389;
            _1396 = _1390.xyz / vec3(_1390.w);

            vec3 _1085 = normalize(-_975.xyz);
            uint _1113 = uint((texelFetch(g_tsSceneZoneID, _955, 0).x * 255.0) + 0.100000001490116119384765625);
            int _2675;
            vec3 _2681;
            float _2689;
            vec3 _2714;
            uint _1487;
            for (;;)
            {
                uint _1482 = (uint((clamp(int(float(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipWidth) * ((_1396.x * 0.5) + 0.5)), 0, int(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipWidth - 1u)) + (clamp(int(float(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipHeight) * ((_1396.y * 0.5) + 0.5)), 0, int(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipHeight - 1u)) * int(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipWidth))) + (clamp(int(log((-_975.z) * _989.g_cbClusterLightSetupInfo._ClusterInfo._fInvNearP) * _989.g_cbClusterLightSetupInfo._ClusterInfo._fRcpLogZSliceGeomFactor), 0, int(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipDepth - 1u)) * int(_989.g_cbClusterLightSetupInfo._ClusterInfo._uiFinestMipWidthHeight))) * 3u) + 1u;
                _1487 = _481.g_rbClusterCells[_1482].LightFirstIndex;
                _2689 = 1.0;
                _2681 = vec3(0.0);
                _2675 = int(_481.g_rbClusterCells[_1482].LightCount) - 1;
                bool _2700;
                vec3 _2710;
                vec3 _2715;
                int _2774;
                vec3 _2781;
                float _2787;
                for (;;)
                {
                    if (_2675 >= 0)
                    {
                        int _1505 = int(_1487) + _2675;
                        uint _1509 = subgroupMax(_509.g_rbClusterIndices[_1505]);
                        if (_509.g_rbClusterIndices[_1505] == _1509)
                        {
                            _2015[0] = _538.g_cbIBLArray._aArray[_1509]._mVsToLs[0];
                            _2015[1] = _538.g_cbIBLArray._aArray[_1509]._mVsToLs[1];
                            uint _1546 = uint(_538.g_cbIBLArray._aArray[_1509]._vfExtentSceneZone.w);
                            bool _1552 = (_1113 >= 32u) || (_1546 == 0u);
                            bool _1561;
                            if (!_1552)
                            {
                                _1561 = (uint(1 << int(_1113)) & _1546) != 0u;
                            }
                            else
                            {
                                _1561 = _1552;
                            }
                            vec3 _2782;
                            float _2788;
                            if (_1561)
                            {
                                vec3 _1583 = (_2015[_836.g_cbIBLPass._uEyeIndex] * _1389).xyz;
                                vec3 _1592 = smoothstep(vec3(_538.g_cbIBLArray._aArray[_1509]._vfParameters.x), vec3(1.0), abs(_1583 / _538.g_cbIBLArray._aArray[_1509]._vfExtentSceneZone.xyz));
                                float _1603 = ((1.0 - _1592.x) * (1.0 - _1592.z)) * (1.0 - _1592.y);
                                vec3 _2783;
                                float _2789;
                                if (_1603 > 0.0)
                                {
                                    vec3 _1626 = mat3(_2015[_836.g_cbIBLPass._uEyeIndex][0].xyz, _2015[_836.g_cbIBLPass._uEyeIndex][1].xyz, _2015[_836.g_cbIBLPass._uEyeIndex][2].xyz) * normalize((_2332 * (2.0 * clamp(dot(_2332, _1085), 0.0, 1.0))) - _1085);
                                    vec3 _1628 = normalize(_1626);
                                    vec3 _1750 = max((_538.g_cbIBLArray._aArray[_1509]._vfLsBoxMax.xyz - _1583) / _1628, (_538.g_cbIBLArray._aArray[_1509]._vfLsBoxMin.xyz - _1583) / _1628);
                                    vec3 _1648 = mix(normalize(_1583 + (_1628 * min(min(_1750.x, _1750.y), _1750.z))), _1626, vec3(smoothstep(0.20000000298023223876953125, 0.60000002384185791015625, _949)));
                                    vec3 _2676;
                                    if ((_538.g_cbIBLArray._aArray[_1509]._vfParameters.z > 0.0) == false)
                                    {
                                        _2676 = mat3(_538.g_cbIBLArray._aArray[_1509]._mLsToWs[0].xyz, _538.g_cbIBLArray._aArray[_1509]._mLsToWs[1].xyz, _538.g_cbIBLArray._aArray[_1509]._mLsToWs[2].xyz) * _1648;
                                    }
                                    else
                                    {
                                        _2676 = _1648;
                                    }
                                    vec3 _2368 = _2676;
                                    _2368.y = -_2676.y;
                                    float _1672 = mix(0.0, _538.g_cbIBLArray._aArray[_1509]._vfParameters.y, _949);
                                    vec3 _1680 = textureLod(g_rbCubeTextures[_538.g_cbIBLArray._aArray[_1509]._uiSpecular0TextureIndex], _2368, _1672).xyz;
                                    vec3 _2677;
                                    if (_538.g_cbIBLArray._aArray[_1509]._fUVSpecularBlend != 0.0)
                                    {
                                        _2677 = mix(_1680, textureLod(g_rbCubeTextures[_538.g_cbIBLArray._aArray[_1509]._uiSpecular1TextureIndex], _2368, _1672).xyz, vec3(_538.g_cbIBLArray._aArray[_1509]._fUVSpecularBlend));
                                    }
                                    else
                                    {
                                        _2677 = _1680;
                                    }
                                    vec3 _1798 = _2681 + ((_2677 * _538.g_cbIBLArray._aArray[_1509]._vfParameters.w) * (_2689 * _1603));
                                    float _1805 = _2689 * (1.0 - _1603);
                                    if (_1805 < 9.9999997473787516355514526367188e-05)
                                    {
                                        _2715 = _1798;
                                        _2710 = _1798;
                                        _2700 = true;
                                        break;
                                    }
                                    _2789 = _1805;
                                    _2783 = _1798;
                                }
                                else
                                {
                                    _2789 = _2689;
                                    _2783 = _2681;
                                }
                                _2788 = _2789;
                                _2782 = _2783;
                            }
                            else
                            {
                                _2788 = _2689;
                                _2782 = _2681;
                            }
                            _2787 = _2788;
                            _2781 = _2782;
                            _2774 = _2675 - 1;
                        }
                        else
                        {
                            _2787 = _2689;
                            _2781 = _2681;
                            _2774 = _2675;
                        }
                        _2689 = _2787;
                        _2681 = _2781;
                        _2675 = _2774;
                        continue;
                    }
                    else
                    {
                        _2715 = _2403;
                        _2710 = _2681;
                        _2700 = false;
                        break;
                    }
                }
                if (_2700)
                {
                    _2714 = _2715;
                    break;
                }
                _2714 = _2710;
                break;
            }
            vec3 _1824 = max(vec3(0.0), _2714);
            float _1826 = _1824.x;
            float _1828 = _1824.y;
            float _1831 = _1824.z;
            int _1837 = max(931135488, (floatBitsToInt(max(max(_1826, _1828), _1831)) & 2139095040));
            float _1842 = uintBitsToFloat(uint(_1837 + 125829120));
            OutSpecularColor = uint((((floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_1826) & (-32768))) + _1842) 
            | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_1828) & (-32768))) + _1842) << 9)) & 262143) 
            | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_1831) & (-32768))) + _1842) << 18)) 
            | ((_1837 + (-931135488)) << 4));
            //OutSpecularColor = 0;
            
        }
        break;
    }
}














