#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 CommonPerMeshUniforms
{
    vec4 gPlanetPositionVec4;
    mat4 gWorldMat4;
    mat4 gWorldNormalMat4;
    float gfFadeValue;
    vec4 gLightColourVec4;
    vec4 gLightCustomParamsVec4;
    mat4 gWorldMotionMat4;
    mat4 gInverseModelMat4;
};

struct CustomPerMaterialUniforms
{
    vec4 gImposterDataVec4;
    vec4 gTileBlendScalesVec4;
    vec4 gHueOverlayParamsVec4;
    vec4 gSaturationOverlayParamsVec4;
    vec4 gValueOverlayParamsVec4;
    vec4 gTerrainColour1Vec4;
    vec4 gTerrainColour2Vec4;
    vec4 gMaterialParamsVec4;
    vec4 gMaterialColourVec4;
    vec4 gMaterialSFXVec4;
    vec4 gMaterialSFXColVec4;
    vec4 gSunPositionVec4;
};

struct CustomPerMeshUniforms
{
    vec4 gUVScrollStepVec4;
    vec4 gCustomParams01Vec4;
    vec4 gCustomParams02Vec4;
    vec4 gObjectColourVec4;
};

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


// 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;
} _502;

layout(set = 1, binding = 2) uniform sampler2D gFadeNoiseMap;
layout(set = 1, binding = 3) uniform sampler2D gBufferMap;
layout(set = 1, binding = 0) uniform sampler2D gDiffuseMap;
layout(set = 1, binding = 1) uniform sampler2D gMasksMap;

layout(location = 0) in VertexBlock
{
    vec4 mTexCoordsVec4;
    vec4 mWorldPositionVec3_mfSpare;
    vec3 mTangentSpaceNormalVec3;
    vec4 mScreenSpacePositionVec4;
    flat vec3 mfFadeValueForInstance_mfLodIndex_mfShearMotionLength;
} In;

layout(location = 0) out vec4 out_color0;
layout(location = 1) out vec4 out_color1;
layout(location = 2) out vec4 out_color2;
layout(location = 3) out vec4 out_color3;

vec4 _1292;

void main()
{
    if (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x < 1.0)
    {
        vec3 _900 = floor(In.mWorldPositionVec3_mfSpare.xyz);
        vec3 _902 = fract(In.mWorldPositionVec3_mfSpare.xyz);
        vec4 _922 = textureLod(gFadeNoiseMap, (_900.xy + (vec2(37.0, 17.0) * _900.z)) + ((_902 * _902) * (vec3(3.0) - (_902 * 2.0))).xy, 0.0);
        float _923 = _922.x;
        if (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x < 0.0)
        {
            if ((1.0 - _923) > (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x + 2.0))
            {
                discard;
            }
        }
        else
        {
            if (_923 > In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x)
            {
                discard;
            }
        }
    }
    vec2 _964 = ((In.mScreenSpacePositionVec4.xy / vec2(In.mScreenSpacePositionVec4.w)) * 0.5) + vec2(0.5);
    vec2 _1067 = (_964 * 2.0) - vec2(1.0);
    vec4 _1256 = vec4(_1067.x, _1067.y, _1292.z, _1292.w);
    _1256.z = (((_502.lUniforms.mpPerFrame.gClipPlanesVec4.x * _502.lUniforms.mpPerFrame.gClipPlanesVec4.y) / (texture(gBufferMap, vec2(_964.x, 1.0 - _964.y)).x * _502.lUniforms.mpPerFrame.gClipPlanesVec4.y)) - _502.lUniforms.mpPerFrame.gClipPlanesVec4.x) / (_502.lUniforms.mpPerFrame.gClipPlanesVec4.y - _502.lUniforms.mpPerFrame.gClipPlanesVec4.x);

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_1256.x -= vk3d_params.stereo.x * (1 - vk3d_params.stereo.y * _1256.z * 10); 
    vec4 _1258 = _1256;
    _1258.w = 1.0;
    vec4 _1077 = _502.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _1258;
    vec3 _993 = (_502.lUniforms.mpCommonPerMesh.gInverseModelMat4 * vec4((_1077.xyz / vec3(_1077.w)).xyz + _502.lUniforms.mpPerFrame.gViewPositionVec3, 1.0)).xyz + vec3(0.5);
    float _996 = 1.0 - _993.y;
    vec3 _1262 = _993;
    _1262.y = _996;
    float _999 = _993.x;
    bool _1000 = _999 < 0.0;
    bool _1007;
    if (!_1000)
    {
        _1007 = _999 >= 1.0;
    }
    else
    {
        _1007 = _1000;
    }
    bool _1014;
    if (!_1007)
    {
        _1014 = _996 < 0.0;
    }
    else
    {
        _1014 = _1007;
    }
    bool _1021;
    if (!_1014)
    {
        _1021 = _996 >= 1.0;
    }
    else
    {
        _1021 = _1014;
    }
    bool _1028;
    if (!_1021)
    {
        _1028 = _993.z < 0.0;
    }
    else
    {
        _1028 = _1021;
    }
    bool _1035;
    if (!_1028)
    {
        _1035 = _993.z >= 1.0;
    }
    else
    {
        _1035 = _1028;
    }
    if (_1035)
    {
        discard;
    }
    vec4 _624 = texture(gDiffuseMap, _1262.xy);
    vec4 _630 = texture(gMasksMap, _1262.xy);
    float _632 = _624.w;
    if (_632 < 0.449999988079071044921875)
    {
        discard;
    }
    vec4 _1277 = _1292;
    _1277.x = 0.0;
    vec4 _1279 = _1277;
    _1279.y = sqrt(((1.0 - _630.y) * _502.lUniforms.mpCustomPerMaterial.gMaterialParamsVec4.x) * 0.5);
    vec4 _1281 = _1279;
    _1281.z = clamp(_502.lUniforms.mpCustomPerMaterial.gMaterialParamsVec4.z, 0.0, 1.0);
    vec4 _1283 = _1281;
    _1283.w = 0.0;
    out_color0 = vec4(_624.xyz, smoothstep(0.449999988079071044921875, 0.800000011920928955078125, _632));
    out_color1 = vec4(0.0);
    out_color2 = vec4(0.0);
    out_color3 = _1283;
}


