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

layout(set = 1, binding = 1) uniform sampler2D gBuffer1Map;
layout(set = 1, binding = 9) uniform sampler2DArray gLightCookiesMap;
layout(set = 1, binding = 0) uniform sampler2D gBufferMap;
layout(set = 1, binding = 2) uniform sampler2D gBuffer2Map;
layout(set = 1, binding = 3) uniform sampler2D gBuffer3Map;
layout(set = 1, binding = 4) uniform sampler2D gBuffer4Map;

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

layout(location = 0) out vec4 out_color0;

vec4 _2185;

void main()
{
    vec2 _647 = In.mScreenSpacePositionVec4.xy / vec2(In.mScreenSpacePositionVec4.w);
    vec2 _1604 = (vec2(_647.x, 1.0 - _647.y) * 2.0) - vec2(1.0);
    vec4 _2133 = vec4(_1604.x, _1604.y, _2185.z, _2185.w);
    _2133.z = (((_668.lUniforms.mpPerFrame.gClipPlanesVec4.x * _668.lUniforms.mpPerFrame.gClipPlanesVec4.y) / (texture(gBuffer1Map, _647).x * _668.lUniforms.mpPerFrame.gClipPlanesVec4.y)) - _668.lUniforms.mpPerFrame.gClipPlanesVec4.x) / (_668.lUniforms.mpPerFrame.gClipPlanesVec4.y - _668.lUniforms.mpPerFrame.gClipPlanesVec4.x);

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_2133.x -= vk3d_params.stereo.x;
vec4 _2135a = _2133;
_2135a.w = 1.0;
vec4 _1614a = _668.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _2135a;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
vec3 _1630a = (_1614a.xyz / vec3(_1614a.w)).xyz;
vec3 _751spec = _668.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz - _1630a;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_2133.x += vk3d_params.stereo.x * vk3d_params.stereo.y * _2133.z * 10;
    vec4 _2135 = _2133;
    _2135.w = 1.0;
    vec4 _1614 = _668.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _2135;
    vec3 _1630 = (_1614.xyz / vec3(_1614.w)).xyz;
    vec3 _728 = _668.lUniforms.mpCommonPerMesh.gLightColourVec4.xyz * _668.lUniforms.mpCommonPerMesh.gLightColourVec4.w;
    vec3 _751 = _668.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.xyz - _1630;
    float _2188;
    if (_668.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.w == 2.0)
    {
        float _765 = _668.lUniforms.mpCommonPerMesh.gLightColourVec4.w / max(1.0, dot(_751, _751));
        if (_765 <= 0.052631579339504241943359375)
        {
            discard;
        }
        _2188 = _765;
    }
    else
    {
        float _2189;
        if (_668.lUniforms.mpCommonPerMesh.gSpotlightPositionVec4.w == 1.0)
        {
            
// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
float _790 = min(inversesqrt(dot(_751spec, _751spec)), 1.0) * _668.lUniforms.mpCommonPerMesh.gLightColourVec4.w;
            if (_790 <= 0.052631579339504241943359375)
            {
                discard;
            }
            _2189 = _790;
        }
        else
        {
            _2189 = _668.lUniforms.mpCommonPerMesh.gLightColourVec4.w;
        }
        _2188 = _2189;
    }
    vec3 _804 = normalize(_751);
    float _2192;
    vec3 _2198;
    if (_668.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w > (-2.0))
    {
        vec3 _818 = -_804;
        float _829 = _2188 * clamp((dot(_668.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz, _818) - _668.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w) * 5.0, 0.0, 1.0);
        if (_829 <= 0.052631579339504241943359375)
        {
            discard;
        }
        vec3 _2199;
        if (_668.lUniforms.mpCommonPerMesh.gSpotlightUpVec4.w >= 0.0)
        {
            _2199 = _728 * texture(gLightCookiesMap, vec3((vec2(dot(_818, cross(_668.lUniforms.mpCommonPerMesh.gSpotlightUpVec4.xyz, _668.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.xyz)), -dot(_818, _668.lUniforms.mpCommonPerMesh.gSpotlightUpVec4.xyz)) / vec2(abs(sin(acos(_668.lUniforms.mpCommonPerMesh.gSpotlightDirectionVec4.w))) * 2.0)) + vec2(0.5), _668.lUniforms.mpCommonPerMesh.gSpotlightUpVec4.w)).x;
        }
        else
        {
            _2199 = _728;
        }
        _2198 = _2199;
        _2192 = _829;
    }
    else
    {
        _2198 = _728;
        _2192 = _2188;
    }
    float _902 = max((_2192 - 0.0500000007450580596923828125) * 1.0526316165924072265625, 0.0);
    vec4 _928 = texture(gBufferMap, _647);
    vec4 _933 = texture(gBuffer2Map, _647);
    vec4 _938 = texture(gBuffer3Map, _647);
    float _1678 = _928.w;
    float _1682 = (_1678 * _1678) * 4.0;
    vec3 _1862 = (_933.xyz * 2.0) - vec3(1.0);
    int _1689 = int(_938.x * 255.0);
    float _1691 = _938.y;
    float _1694 = _1691 * _1691;
    float _1697 = _938.z;
    vec3 _991 = _928.xyz * mat3(vec3(0.7767217159271240234375, 0.1715466976165771484375, 0.0518387891352176666259765625), vec3(0.0330350138247013092041015625, 0.9576499462127685546875, 0.00929595343768596649169921875), vec3(0.01708533428609371185302734375, 0.072395719587802886962890625, 0.9103014469146728515625));
    vec3 _2237;
    if ((_1689 & 8) != 0)
    {
        _2237 = _991 * _902;
    }
    else
    {
        
// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
vec3 _1905 = normalize(_668.lUniforms.mpPerFrame.gViewPositionVec3 - (_1630a + _668.lUniforms.mpPerFrame.gViewPositionVec3));
        vec3 _1919 = _991 * _1697;
        vec4 _2018 = (vec4(-1.0, -0.0274999998509883880615234375, -0.572000026702880859375, 0.02199999988079071044921875) * (_1694 * 2.0)) + vec4(1.0, 0.0425000004470348358154296875, 1.03999996185302734375, -0.039999999105930328369140625);
        float _2020 = _2018.x;
        vec2 _2038 = (vec2(-1.03999996185302734375, 1.03999996185302734375) * ((min(_2020 * _2020, exp2((-9.27999973297119140625) * clamp(dot(_1862, _1905), 0.0, 1.0))) * _2020) + _2018.y)) + _2018.zw;
        vec2 _2074 = (vec2(0.721347510814666748046875, 0.25) * exp2((_1694 * (-13.77773761749267578125)) + 6.888868808746337890625)) + vec2(0.3967411220073699951171875, 0.75);
        float _2078 = _2074.x;
        _2237 = min(vec3(1.14999997615814208984375), mix(vec3(0.0), ((_2198 * mat3(vec3(0.7767217159271240234375, 0.1715466976165771484375, 0.0518387891352176666259765625), vec3(0.0330350138247013092041015625, 0.9576499462127685546875, 0.00929595343768596649169921875), vec3(0.01708533428609371185302734375, 0.072395719587802886962890625, 0.9103014469146728515625))) * (clamp(dot(_1862, _804), 0.0, 1.0) + ((pow(clamp(dot(_1905, -_804), 0.0, 1.0), 4.0) + (((_1689 & 128) != 0) ? 0.25 : 0.0)) * _938.w))) * ((_991 - _1919) + ((((vec3(0.039999999105930328369140625 - (0.039999999105930328369140625 * _1697)) + _1919) * _2038.x) + vec3(_2038.y)) * (_2074.y * exp2((_2078 * clamp(dot(reflect(-_1905, _1862), _804), 0.0, 1.0)) - _2078)))), vec3(mix(0.61000001430511474609375, 1.0, texture(gBuffer4Map, _647).x))) * _902);
    }
    vec3 _2238;
    if ((_1689 & 64) != 0)
    {
        _2238 = _2237 + (_991 * mix(_1682, _1682 * sqrt(_1682), clamp(_1682 - 1.0, 0.0, 1.0)));
    }
    else
    {
        _2238 = _2237;
    }
    out_color0 = vec4(_2238, 1.0);
}


