#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;
};

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;
} _696;

layout(set = 1, binding = 1) uniform sampler2DShadow gShadowMap;
layout(set = 1, binding = 0) uniform sampler2D gBufferMap;

layout(location = 0) in VertexBlock
{
    vec2 mTexCoordsVec2;
} In;

layout(location = 0) out vec4 out_color0;

vec4 _1922;

void main()
{
    float _2008;
    if (_696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.y != 0.0)
    {
        vec2 _1222 = (vec2(In.mTexCoordsVec2.x, 1.0 - In.mTexCoordsVec2.y) * 2.0) - vec2(1.0);
        vec4 _1861 = vec4(_1222.x, _1222.y, _1922.z, _1922.w);

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_1861.x-=vk3d_params.stereo.x*(1-vk3d_params.stereo.y/textureLod(gBufferMap, In.mTexCoordsVec2, 0.0).x/_696.lUniforms.mpPerFrame.gClipPlanesVec4.y);
        _1861.z = 0.0;
        vec4 _1863 = _1861;
        _1863.w = 1.0;
        vec4 _1229 = _696.lUniforms.mpPerFrame.gInverseProjectionMat4 * _1863;
        mat4 _1866 = _696.lUniforms.mpPerFrame.gInverseViewMat4;
        _1866[3] = vec4(0.0, 0.0, 0.0, 1.0);
        vec3 _1251 = (_1866 * ((_1229 / vec4(abs(_1229.z))) * (textureLod(gBufferMap, In.mTexCoordsVec2, 0.0).x * _696.lUniforms.mpPerFrame.gClipPlanesVec4.y))).xyz;
        float _1123 = length(_1251);
        vec3 _1127 = _1251 / vec3(_1123);
        vec3 _1132 = _1127 * (_1123 * 0.0625);
        uvec2 _1265 = uvec2(In.mTexCoordsVec2 * _696.lUniforms.mpPerFrame.gFrameBufferSizeVec4.xy) & uvec2(3u);
        mat4 _1261 = 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 _1279 = 1.0 - _696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.x;
        float _1295 = (_1279 * _1279) / (12.56637096405029296875 * pow((1.0 + (_696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.x * _696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.x)) - ((2.0 * _696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.x) * dot(_1127, _696.lUniforms.mpCustomPerMesh.gSunPositionVec4.xyz)), 1.5));
        float _2009;
        if (_1295 > 0.0030000000260770320892333984375)
        {
            vec3 _1924;
            _1924 = (_696.lUniforms.mpPerFrame.gViewPositionVec3 + (_1132 * _1261[_1265.x][_1265.y])) + _1132;
            float _1177;
            float _1179;
            vec3 _1182;
            float _2006;
            float _2007;
            int _1923 = 1;
            float _1953 = 0.0;
            float _1970 = 0.0;
            for (;;)
            {
                if (_1923 < 15)
                {
                    float _1939;
                    for (;;)
                    {
                        vec4 _1326 = vec4(_1924, 1.0);
                        vec4 _1327 = _696.lUniforms.mpCommonPerMesh.gaShadowMat4[0] * _1326;
                        float _1330 = _1327.x;
                        float _1332 = _1327.y;
                        float _1336 = 1.0 - _696.lUniforms.mpPerFrame.gShadowSizeVec4.w;
                        bool _1337 = max(_1330, _1332) < _1336;
                        bool _1348;
                        if (_1337)
                        {
                            _1348 = min(_1330, _1332) > _696.lUniforms.mpPerFrame.gShadowSizeVec4.w;
                        }
                        else
                        {
                            _1348 = _1337;
                        }
                        bool _1354;
                        if (_1348)
                        {
                            _1354 = _1327.z < 1.0;
                        }
                        else
                        {
                            _1354 = _1348;
                        }
                        bool _1360;
                        if (_1354)
                        {
                            _1360 = _1327.z >= 0.0;
                        }
                        else
                        {
                            _1360 = _1354;
                        }
                        vec3 _1930;
                        if (!_1360)
                        {
                            vec4 _1371 = _696.lUniforms.mpCommonPerMesh.gaShadowMat4[1] * _1326;
                            float _1374 = _1371.x;
                            float _1376 = _1371.y;
                            bool _1381 = max(_1374, _1376) < _1336;
                            bool _1392;
                            if (_1381)
                            {
                                _1392 = min(_1374, _1376) > _696.lUniforms.mpPerFrame.gShadowSizeVec4.w;
                            }
                            else
                            {
                                _1392 = _1381;
                            }
                            bool _1398;
                            if (_1392)
                            {
                                _1398 = _1371.z < 1.0;
                            }
                            else
                            {
                                _1398 = _1392;
                            }
                            bool _1404;
                            if (_1398)
                            {
                                _1404 = _1371.z >= 0.0;
                            }
                            else
                            {
                                _1404 = _1398;
                            }
                            if (!_1404)
                            {
                                vec4 _1420 = _696.lUniforms.mpCommonPerMesh.gaShadowMat4[2] * _1326;
                                float _1423 = _1420.x;
                                float _1425 = _1420.y;
                                bool _1430 = max(_1423, _1425) < _1336;
                                bool _1441;
                                if (_1430)
                                {
                                    _1441 = min(_1423, _1425) > _696.lUniforms.mpPerFrame.gShadowSizeVec4.w;
                                }
                                else
                                {
                                    _1441 = _1430;
                                }
                                bool _1447;
                                if (_1441)
                                {
                                    _1447 = _1420.z < 1.0;
                                }
                                else
                                {
                                    _1447 = _1441;
                                }
                                bool _1453;
                                if (_1447)
                                {
                                    _1453 = _1420.z >= 0.0;
                                }
                                else
                                {
                                    _1453 = _1447;
                                }
                                if (!_1453)
                                {
                                    _1939 = 1.0;
                                    break;
                                }
                                else
                                {
                                    vec3 _1897 = _1420.xyz;
                                    _1897.x = (_1423 + 2.0) * 0.3333333432674407958984375;
                                    float _1496 = 1.0 - _1420.z;
                                    vec3 _1900 = _1897;
                                    _1900.z = _1496;
                                    _1939 = texture(gShadowMap, vec3(_1900.xy, _1496));
                                    break;
                                }
                            }
                            vec3 _1885 = _1371.xyz;
                            _1885.x = _1374 + 1.0;
                            _1930 = _1885;
                        }
                        else
                        {
                            _1930 = _1327.xyz;
                        }
                        vec3 _1905 = _1930;
                        _1905.x = _1930.x * 0.3333333432674407958984375;
                        float _1589 = 1.0 - _1930.z;
                        vec3 _1908 = _1905;
                        _1908.z = _1589;
                        vec2 _1615 = _1908.xy + vec2(_696.lUniforms.mpPerFrame.gShadowSizeVec4.z * (-0.16666667163372039794921875), 0.0);
                        vec2 _1629 = _1908.xy + vec2(_696.lUniforms.mpPerFrame.gShadowSizeVec4.z * 0.16666667163372039794921875, 0.0);
                        vec2 _1643 = _1908.xy + vec2(0.0, _696.lUniforms.mpPerFrame.gShadowSizeVec4.w * (-0.5));
                        vec2 _1657 = _1908.xy + vec2(0.0, 0.5 * _696.lUniforms.mpPerFrame.gShadowSizeVec4.w);
                        _1939 = ((((texture(gShadowMap, vec3(_1908.xy, _1589)) + texture(gShadowMap, vec3(vec3(_1615.x, _1615.y, _1908.z).xy, _1589))) + texture(gShadowMap, vec3(vec3(_1629.x, _1629.y, _1908.z).xy, _1589))) + texture(gShadowMap, vec3(vec3(_1643.x, _1643.y, _1908.z).xy, _1589))) + texture(gShadowMap, vec3(vec3(_1657.x, _1657.y, _1908.z).xy, _1589))) * 0.20000000298023223876953125;
                        break;
                    }
                    _1177 = _1953 + (_1295 * _1939);
                    _1179 = _1970 + 1.0;
                    _1182 = _1924 + _1132;
                    vec3 _1186 = _696.lUniforms.mpPerFrame.gViewPositionVec3 - _1182;
                    if (dot(_1186, _1186) > _696.lUniforms.mpPerFrame.gShadowFadeParamVec4.w)
                    {
                        _2007 = _1177;
                        _2006 = _1179;
                        break;
                    }
                    _1970 = _1179;
                    _1953 = _1177;
                    _1924 = _1182;
                    _1923++;
                    continue;
                }
                else
                {
                    _2007 = _1953;
                    _2006 = _1970;
                    break;
                }
            }
            _2009 = smoothstep(_696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.z, _696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.w, clamp(_2007 / max(9.9999997473787516355514526367188e-05, _2006), 0.0, 1.0) * _696.lUniforms.mpCustomPerMesh.gLightShaftParamsVec4.y);
        }
        else
        {
            _2009 = 0.0;
        }
        _2008 = _2009;
    }
    else
    {
        _2008 = 0.0;
    }
    out_color0 = vec4(_2008);
}


