#version 450
 
struct PerFrameUniforms
{
    mat4 gViewMat4;
    mat4 gViewProjectionMat4;
    mat4 gCameraMat4;
    mat4 gCameraNoHeadTrackingMat4;
    mat4 gPrevViewProjectionMat4;
    mat4 gThisToPrevViewProjectionMat4;
    mat4 gInverseViewMat4;
    mat4 gInverseProjectionMat4;
    mat4 gInverseProjectionSCMat4;
    mat4 gInverseViewProjectionMat4;
    vec4 gInverseWorldUpMatVec4[3];
    vec3 gViewPositionVec3;
    float gfTime;
    vec3 gVREyeInfoVec3;
    float gfPrevTime;
    vec4 gClipPlanesVec4;
    vec4 gMBlurSettingsVec4;
    vec3 gDeJitterVec3;
    int giFrameIndex;
    vec4 gTaaSettingsVec4;
    vec4 gTessSettingsVec4;
    vec4 gShellsSettingsVec4;
    vec4 gFrameBufferSizeVec4;
    vec4 gFoVValuesVec4;
    vec4 gShadowSizeVec4;
    vec3 gShadowProjScaleVec3;
    int giDisableAmbientAllowed;
    vec4 gShadowFadeParamVec4;
};

struct CustomPerMeshUniforms
{
    vec4 gMaterialParamsVec4;
    vec4 gMaterialSFXVec4;
    vec4 gMaterialSFXColVec4;
    vec4 gaPlanetPositionsVec4[6];
    vec4 gaPlanetColoursVec4[6];
    vec4 gSkyColourVec4;
    vec4 gHorizonColourVec4;
    vec4 gSunColourVec4;
    vec4 gScatteringParamsVec4;
    vec4 gSunPositionVec4;
    vec4 gSkyUpperParamsVec4;
    vec4 gSkyUpperColourVec4;
    vec4 gSkySolarColourVec4;
    vec4 gSkyGradientSpeedVec4;
    vec4 gFogParamsVec4;
    vec4 gFogColourVec4;
    vec4 gHeightFogParamsVec4;
    vec4 gHeightFogColourVec4;
    vec4 gWaterFogVec4;
    vec4 gWaterFogColourFarVec4;
    vec4 gWaterFogColourNearVec4;
    vec4 gSpaceHorizonColourVec4;
    vec4 gFogFadeHeightsVec4;
    vec4 gFogFadeHeights2Vec4;
    vec4 gFogFadeHeights3Vec4;
    vec4 gSpaceSkyColourVec4;
    vec4 gSpaceScatteringParamsVec4;
    vec4 gSpaceSkyColour1Vec4;
    vec4 gSpaceSkyColour2Vec4;
    vec4 gSpaceSkyColour3Vec4;
    vec4 gSpaceFogColourVec4;
    vec4 gSpaceFogColour2Vec4;
    vec4 gSpaceFogParamsVec4;
    vec4 gLightShaftParamsVec4;
    vec4 gLightTopColourVec4;
    vec4 gHueOverlayParamsVec4;
    vec4 gSaturationOverlayParamsVec4;
    vec4 gValueOverlayParamsVec4;
};

struct CommonPerMeshUniforms
{
    vec4 gPlanetPositionVec4;
    mat4 gWorldMat4;
    mat4 gWorldNormalMat4;
    float fdFadeValueDummy;
    vec4 gLightPositionVec4;
    vec4 gLightDirectionVec4;
    vec4 gLightOriginVec4;
    vec4 gScanParamsPosVec4;
    vec4 gScanParamsCfg1Vec4;
    vec4 gScanParamsCfg2Vec4;
    vec4 gScanParamsCfg3Vec4;
    vec4 gScanColourVec4;
    mat4 gaShadowMat4[3];
    vec4 gLightColourVec4;
    vec4 gLightCustomParamsVec4;
    mat4 gWorldMotionMat4;
    mat4 gInverseModelMat4Dummy;
    vec4 gSpotlightPositionVec4;
    vec4 gSpotlightDirectionVec4;
    vec4 gSpotlightUpVec4;
};

struct UniformBuffer
{
    PerFrameUniforms mpPerFrame;
    CustomPerMeshUniforms mpCustomPerMesh;
    CommonPerMeshUniforms mpCommonPerMesh;
};


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

layout(set = 0, binding = 0, std140) uniform lUniforms_BLK
{
    UniformBuffer lUniforms;
} _908;

layout(set = 1, binding = 0) uniform sampler2D gBufferMap;
layout(set = 1, binding = 1) uniform sampler3D gLightVolNoise3D;

layout(location = 0) in VertexBlock
{
    vec4 mScreenSpacePositionVec4;
} In;

layout(location = 0) out vec4 out_color0;

vec4 _2427;
vec3 _2494;

void main()
{
    vec2 _886 = In.mScreenSpacePositionVec4.xy / vec2(In.mScreenSpacePositionVec4.w);
    float _1510 = texture(gBufferMap, _886).x * _908.lUniforms.mpPerFrame.gClipPlanesVec4.y;
    vec2 _1520 = (vec2(_886.x, 1.0 - _886.y) * 2.0) - vec2(1.0);
    vec4 _1522 = vec4(_1520.x, _1520.y, _2427.z, _2427.w);
    float _1563 = _908.lUniforms.mpPerFrame.gClipPlanesVec4.x * _908.lUniforms.mpPerFrame.gClipPlanesVec4.y;
    float _1570 = _908.lUniforms.mpPerFrame.gClipPlanesVec4.y - _908.lUniforms.mpPerFrame.gClipPlanesVec4.x;
    vec4 _2382 = _1522;
    _2382.z = ((_1563 / _1510) - _908.lUniforms.mpPerFrame.gClipPlanesVec4.x) / _1570;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_2382.x -= vk3d_params.stereo.x * (1 - vk3d_params.stereo.y * _2382.z * 10); 
    vec4 _2384 = _2382;
    _2384.w = 1.0;
    vec4 _1530 = _908.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _2384;
    vec3 _1536 = _1530.xyz / vec3(_1530.w);
    vec3 _1546 = _1536.xyz;
    float _1589 = _1563 / (_908.lUniforms.mpPerFrame.gClipPlanesVec4.x + (gl_FragCoord.z * _1570));
    vec4 _2393 = _1522;
    _2393.z = ((_1563 / _1589) - _908.lUniforms.mpPerFrame.gClipPlanesVec4.x) / _1570;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_2393.x -= vk3d_params.stereo.x * (1 - vk3d_params.stereo.y * _2393.z * 10); 
    vec4 _2395 = _2393;
    _2395.w = 1.0;
    vec4 _1609 = _908.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _2395;
    vec3 _1615 = _1609.xyz / vec3(_1609.w);
    vec3 _977 = normalize(-_1615.xyz);
    float _983 = _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w * 1.0499999523162841796875;
    float _989 = _908.lUniforms.mpCommonPerMesh.gLightPositionVec4.w * 0.9900000095367431640625;
    float _998 = (abs(sin(acos(_983))) / _983) * _989;
    int _1010 = int(_908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.w);
    vec4 _2429;
    for (;;)
    {
        if (_1589 < _1510)
        {
            _2429 = vec4(_1615, 1.0);
            break;
        }
        bool _2428;
        for (;;)
        {
            vec3 _1698 = _1546 - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz;
            if (dot(_1698, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) > (_908.lUniforms.mpCommonPerMesh.gLightPositionVec4.w * 0.99989998340606689453125))
            {
                _2428 = false;
                break;
            }
            if (dot(normalize(_1698), _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) <= (_908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w * 0.997499942779541015625))
            {
                _2428 = false;
                break;
            }
            _2428 = true;
            break;
        }
        _2429 = vec4(_1536, _2428 ? 2.0 : 0.0);
        break;
    }
    if (_2429.w < 1.0)
    {
        discard;
    }
    float _1734;
    bool _2430;
    for (;;)
    {
        vec3 _1730 = -_908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz;
        _1734 = _908.lUniforms.mpCommonPerMesh.gLightPositionVec4.w * 0.99989998340606689453125;
        if (dot(_1730, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) > _1734)
        {
            _2430 = false;
            break;
        }
        if (dot(normalize(_1730), _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) <= (_908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w * 0.997499942779541015625))
        {
            _2430 = false;
            break;
        }
        _2430 = true;
        break;
    }
    vec3 _2478;
    if (!_2430)
    {
        vec4 _2437;
        for (;;)
        {
            float _1901;
            vec4 _2434;
            for (;;)
            {
                float _1885 = _983 * _983;
                vec3 _1898 = _2429.xyz - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz;
                _1901 = dot(_977, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz);
                if (abs(_1901 - _983) < 1.0000000133514319600180897396058e-10)
                {
                    _2434 = vec4(_2429.xyz, 1.0);
                    break;
                }
                float _1922 = (_1901 * _1901) - _1885;
                float _1928 = dot(_1898, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz);
                float _1935 = (_1901 * _1928) - (dot(_977, _1898) * _1885);
                float _1936 = 2.0 * _1935;
                float _1957 = (_1936 * _1936) - ((4.0 * _1922) * ((_1928 * _1928) - (dot(_1898, _1898) * _1885)));
                if (_1957 < 0.0)
                {
                    _2434 = vec4(0.0);
                    break;
                }
                float _1964 = sqrt(_1957);
                float _1966 = _1935 * (-2.0);
                float _1970 = 2.0 * _1922;
                float _1971 = (_1966 - _1964) / _1970;
                float _1978 = (_1966 + _1964) / _1970;
                vec3 _1989 = _2429.xyz + (_977 * max(_1971, _1978));
                vec3 _2433;
                if (dot(_1989 - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) <= 0.0)
                {
                    _2433 = _2429.xyz + (_977 * min(_1971, _1978));
                }
                else
                {
                    _2433 = _1989;
                }
                if (dot(_2433 - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) <= 0.0)
                {
                    _2434 = vec4(0.0);
                    break;
                }
                _2434 = vec4(_2433, 1.0);
                break;
            }
            vec4 _2435;
            for (;;)
            {
                vec3 _2047 = _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz + (_908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz * _989);
                vec3 _2064 = _2429.xyz + (_977 * (dot(_2047 - _2429.xyz, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) / _1901));
                vec3 _2067 = _2064 - _2047;
                if (dot(_2067, _2067) < (_998 * _998))
                {
                    _2435 = vec4(_2064, 1.0);
                    break;
                }
                _2435 = vec4(0.0);
                break;
            }
            bool _1798 = _2435.w >= 1.0;
            bool _1801 = _2434.w >= 1.0;
            bool _1811;
            if (_1801)
            {
                bool _2436;
                for (;;)
                {
                    vec3 _2096 = _2434.xyz - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz;
                    if (dot(_2096, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) > _1734)
                    {
                        _2436 = false;
                        break;
                    }
                    if (dot(normalize(_2096), _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) <= (_908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w * 0.997499942779541015625))
                    {
                        _2436 = false;
                        break;
                    }
                    _2436 = true;
                    break;
                }
                _1811 = _2436;
            }
            else
            {
                _1811 = _1801;
            }
            bool _1813 = !_1811;
            bool _1815 = !_1798;
            if (_1813 && _1815)
            {
                _2437 = vec4(0.0);
                break;
            }
            if (_1811 && _1815)
            {
                _2437 = _2434;
                break;
            }
            if (_1813 && _1798)
            {
                _2437 = _2435;
                break;
            }
            if (dot(_2435.xyz, _2435.xyz) < dot(_2434.xyz, _2434.xyz))
            {
                _2437 = _2435;
                break;
            }
            _2437 = _2434;
            break;
        }
        if (_2437.w < 1.0)
        {
            discard;
        }
        vec4 _2439;
        for (;;)
        {
            if (length(_2437.xyz) < _1510)
            {
                _2439 = vec4(_2437.xyz, 1.0);
                break;
            }
            bool _2438;
            for (;;)
            {
                vec3 _2165 = _1546 - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz;
                if (dot(_2165, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) > _1734)
                {
                    _2438 = false;
                    break;
                }
                if (dot(normalize(_2165), _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) <= (_908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w * 0.997499942779541015625))
                {
                    _2438 = false;
                    break;
                }
                _2438 = true;
                break;
            }
            _2439 = vec4(_1536, _2438 ? 2.0 : 0.0);
            break;
        }
        vec3 _2481;
        if (_2439.w >= 1.0)
        {
            _2481 = _2439.xyz;
        }
        else
        {
            _2481 = _2437.xyz;
        }
        _2478 = _2481;
    }
    else
    {
        _2478 = vec3(0.0);
    }
    float _1112 = _908.lUniforms.mpCommonPerMesh.gLightColourVec4.w * _908.lUniforms.mpCommonPerMesh.gLightCustomParamsVec4.x;
    uvec2 _2193 = uvec2(_886 * _908.lUniforms.mpPerFrame.gFrameBufferSizeVec4.xy) & uvec2(3u);
    mat4 _2189 = mat4(vec4(0.0588235296308994293212890625, 0.529411792755126953125, 0.17647059261798858642578125, 0.64705884456634521484375), vec4(0.7647058963775634765625, 0.2941176593303680419921875, 0.88235294818878173828125, 0.4117647111415863037109375), vec4(0.23529411852359771728515625, 0.705882370471954345703125, 0.117647059261798858642578125, 0.588235318660736083984375), vec4(0.941176474094390869140625, 0.4705882370471954345703125, 0.823529422283172607421875, 0.3529411852359771728515625));
    float _2226 = length(_2429.xyz - _2478) * 0.0625;
    float _2487;
    vec3 _2488;
    _2488 = _2494;
    _2487 = 0.0;
    vec3 _2277;
    float _2297;
    float _2512;
    for (int _2486 = 0; _2486 < 16; _2488 = _2277, _2487 += (_2512 * _2297), _2486++)
    {
        vec3 _2247 = (_2429.xyz + (_977 * (_2226 * (float(_2486) + _2189[_2193.x][_2193.y])))) - _908.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz;
        vec3 _2405 = _2488;
        _2405.x = dot(_2247, cross(_908.lUniforms.mpCommonPerMesh.gSpotlightUpVec4.xyz, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz)) / _998;
        vec3 _2407 = _2405;
        _2407.y = dot(_2247, _908.lUniforms.mpCommonPerMesh.gSpotlightUpVec4.xyz) / _998;
        vec3 _2412 = _2407;
        _2412.z = mod((dot(_2247, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz) / _989) - (_908.lUniforms.mpPerFrame.gfTime * 0.015625), 1.0);
        _2277 = _2412 * 2.0;
        vec4 _2280 = textureLod(gLightVolNoise3D, _2277, 0.0);
        float _2281 = _2280.x;
        float _2294 = dot(_977, _908.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz);
        _2297 = ((0.06684507429599761962890625 / pow(1.15999996662139892578125 - (0.800000011920928955078125 * _2294), 1.5)) + ((1.0 + (_2294 * _2294)) * 0.07500000298023223876953125)) * (_2281 * ((((_2281 * _2281) * _2281) * _2281) * 4.0));
        float _2356 = dot(_2247, _2247);
        if (_1010 == 2)
        {
            _2512 = _1112 / max(1.0, _2356);
        }
        else
        {
            float _2513;
            if (_1010 == 1)
            {
                _2513 = _1112 * min(inversesqrt(_2356), 1.0);
            }
            else
            {
                _2513 = _1112;
            }
            _2512 = _2513;
        }
    }
    float _2313 = _2487 * _2226;
    out_color0 = vec4(_2313, _2313, _2313, 1.0);
}


