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

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 gNormalMap;

layout(location = 0) in VertexBlock
{
    vec4 mTexCoordsVec4;
    vec4 mWorldPositionVec3_mfSpare;
    vec3 mTangentMatRow1Vec3;
    vec3 mTangentMatRow2Vec3;
    vec3 mTangentMatRow3Vec3;
    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 _1577;

void main()
{
    if (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x < 1.0)
    {
        vec3 _1059 = floor(In.mWorldPositionVec3_mfSpare.xyz);
        vec3 _1061 = fract(In.mWorldPositionVec3_mfSpare.xyz);
        vec4 _1081 = textureLod(gFadeNoiseMap, (_1059.xy + (vec2(37.0, 17.0) * _1059.z)) + ((_1061 * _1061) * (vec3(3.0) - (_1061 * 2.0))).xy, 0.0);
        float _1082 = _1081.x;
        if (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x < 0.0)
        {
            if ((1.0 - _1082) > (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x + 2.0))
            {
                discard;
            }
        }
        else
        {
            if (_1082 > In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x)
            {
                discard;
            }
        }
    }
    vec2 _1123 = ((In.mScreenSpacePositionVec4.xy / vec2(In.mScreenSpacePositionVec4.w)) * 0.5) + vec2(0.5);
    vec2 _1226 = (_1123 * 2.0) - vec2(1.0);
    vec4 _1528 = vec4(_1226.x, _1226.y, _1577.z, _1577.w);
    _1528.z = (((_629.lUniforms.mpPerFrame.gClipPlanesVec4.x * _629.lUniforms.mpPerFrame.gClipPlanesVec4.y) / (texture(gBufferMap, vec2(_1123.x, 1.0 - _1123.y)).x * _629.lUniforms.mpPerFrame.gClipPlanesVec4.y)) - _629.lUniforms.mpPerFrame.gClipPlanesVec4.x) / (_629.lUniforms.mpPerFrame.gClipPlanesVec4.y - _629.lUniforms.mpPerFrame.gClipPlanesVec4.x);

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
_1528.x -= vk3d_params.stereo.x * (1 - vk3d_params.stereo.y * _1528.z * 10); 
    vec4 _1530 = _1528;
    _1530.w = 1.0;
    vec4 _1236 = _629.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _1530;
    vec3 _1248 = (_1236.xyz / vec3(_1236.w)).xyz + _629.lUniforms.mpPerFrame.gViewPositionVec3;
    vec3 _1252 = _1248.xyz;
    vec3 _1152 = (_629.lUniforms.mpCommonPerMesh.gInverseModelMat4 * vec4(_1248, 1.0)).xyz + vec3(0.5);
    float _1155 = 1.0 - _1152.y;
    vec3 _1534 = _1152;
    _1534.y = _1155;
    float _1158 = _1152.x;
    bool _1159 = _1158 < 0.0;
    bool _1166;
    if (!_1159)
    {
        _1166 = _1158 >= 1.0;
    }
    else
    {
        _1166 = _1159;
    }
    bool _1173;
    if (!_1166)
    {
        _1173 = _1155 < 0.0;
    }
    else
    {
        _1173 = _1166;
    }
    bool _1180;
    if (!_1173)
    {
        _1180 = _1155 >= 1.0;
    }
    else
    {
        _1180 = _1173;
    }
    bool _1187;
    if (!_1180)
    {
        _1187 = _1152.z < 0.0;
    }
    else
    {
        _1187 = _1180;
    }
    bool _1194;
    if (!_1187)
    {
        _1194 = _1152.z >= 1.0;
    }
    else
    {
        _1194 = _1187;
    }
    if (_1194)
    {
        discard;
    }
    vec4 _751 = texture(gDiffuseMap, _1534.xy);
    float _757 = _751.w * _629.lUniforms.mpCustomPerMaterial.gMaterialColourVec4.w;
    if (_757 < 9.9999997473787516355514526367188e-05)
    {
        discard;
    }
    vec4 _1282 = (texture(gNormalMap, _1534.xy) * 1.9921875) - vec4(1.0);
    float _1284 = _1282.x;
    float _1286 = _1282.y;
    vec3 _1324 = normalize(cross(-dFdy(_1252), dFdx(_1252)));
    vec3 _1329 = -normalize(_629.lUniforms.mpPerFrame.gViewPositionVec3 - _1252);
    vec2 _1335 = dFdx(_1534.xy);
    vec2 _1337 = dFdy(_1534.xy);
    vec3 _1340 = cross(dFdy(_1329), _1324);
    vec3 _1343 = cross(_1324, dFdx(_1329));
    vec3 _1352 = (_1340 * _1335.x) + (_1343 * _1337.x);
    vec3 _1361 = (_1340 * _1335.y) + (_1343 * _1337.y);
    float _1369 = inversesqrt(max(dot(_1352, _1352), dot(_1361, _1361)));
    vec4 _1560 = _1577;
    _1560.x = 0.0;
    vec4 _1562 = _1560;
    _1562.y = sqrt(_629.lUniforms.mpCustomPerMaterial.gMaterialParamsVec4.x * 0.5);
    vec4 _1564 = _1562;
    _1564.z = clamp(_629.lUniforms.mpCustomPerMaterial.gMaterialParamsVec4.z, 0.0, 1.0);
    vec4 _1566 = _1564;
    _1566.w = 0.0;
    out_color0 = vec4(_751.xyz, _757);
    out_color1 = vec4(0.0);
    out_color2 = vec4((normalize(mat3(_1352 * _1369, _1361 * _1369, -_1324) * vec3(_1284, _1286, sqrt(max((1.0 - (_1284 * _1284)) - (_1286 * _1286), 0.0)))) * 0.5) + vec3(0.5), _757);
    out_color3 = _1566;
}


