#version 450
//decals
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;
} _634;

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

void main()
{
    if (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x < 1.0)
    {
        vec3 _1064 = floor(In.mWorldPositionVec3_mfSpare.xyz);
        vec3 _1066 = fract(In.mWorldPositionVec3_mfSpare.xyz);
        vec4 _1086 = textureLod(gFadeNoiseMap, (_1064.xy + (vec2(37.0, 17.0) * _1064.z)) + ((_1066 * _1066) * (vec3(3.0) - (_1066 * 2.0))).xy, 0.0);
        float _1087 = _1086.x;
        if (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x < 0.0)
        {
            if ((1.0 - _1087) > (In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x + 2.0))
            {
                discard;
            }
        }
        else
        {
            if (_1087 > In.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x)
            {
                discard;
            }
        }
    }
    vec2 _1128 = ((In.mScreenSpacePositionVec4.xy / vec2(In.mScreenSpacePositionVec4.w)) * 0.5) + vec2(0.5);
    vec2 _1231 = (_1128 * 2.0) - vec2(1.0);
    vec4 _1537 = vec4(_1231.x, _1231.y, _1587.z, _1587.w);
    _1537.z = (((_634.lUniforms.mpPerFrame.gClipPlanesVec4.x * _634.lUniforms.mpPerFrame.gClipPlanesVec4.y) / (texture(gBufferMap, vec2(_1128.x, 1.0 - _1128.y)).x * _634.lUniforms.mpPerFrame.gClipPlanesVec4.y)) - _634.lUniforms.mpPerFrame.gClipPlanesVec4.x) / (_634.lUniforms.mpPerFrame.gClipPlanesVec4.y - _634.lUniforms.mpPerFrame.gClipPlanesVec4.x);
    
    _1537.x-=vk3d_params.stereo.x*(1-vk3d_params.stereo.y*_1537.z*10);
    
    vec4 _1539 = _1537;
    _1539.w = 1.0;
    vec4 _1241 = _634.lUniforms.mpPerFrame.gInverseViewProjectionMat4 * _1539;
    vec3 _1253 = (_1241.xyz / vec3(_1241.w)).xyz + _634.lUniforms.mpPerFrame.gViewPositionVec3;
    vec3 _1257 = _1253.xyz;
    vec3 _1157 = (_634.lUniforms.mpCommonPerMesh.gInverseModelMat4 * vec4(_1253, 1.0)).xyz + vec3(0.5);
    float _1160 = 1.0 - _1157.y;
    vec3 _1543 = _1157;
    _1543.y = _1160;
    float _1163 = _1157.x;
    bool _1164 = _1163 < 0.0;
    bool _1171;
    if (!_1164)
    {
        _1171 = _1163 >= 1.0;
    }
    else
    {
        _1171 = _1164;
    }
    bool _1178;
    if (!_1171)
    {
        _1178 = _1160 < 0.0;
    }
    else
    {
        _1178 = _1171;
    }
    bool _1185;
    if (!_1178)
    {
        _1185 = _1160 >= 1.0;
    }
    else
    {
        _1185 = _1178;
    }
    bool _1192;
    if (!_1185)
    {
        _1192 = _1157.z < 0.0;
    }
    else
    {
        _1192 = _1185;
    }
    bool _1199;
    if (!_1192)
    {
        _1199 = _1157.z >= 1.0;
    }
    else
    {
        _1199 = _1192;
    }
    if (_1199)
    {
        discard;
    }
    vec4 _756 = texture(gDiffuseMap, _1543.xy);
    float _762 = _756.w * _634.lUniforms.mpCustomPerMaterial.gMaterialColourVec4.w;
    if (_762 < 9.9999997473787516355514526367188e-05)
    {
        discard;
    }
    vec4 _1287 = (texture(gNormalMap, _1543.xy) * 1.9921875) - vec4(1.0);
    float _1289 = _1287.x;
    float _1291 = _1287.y;
    vec3 _1329 = normalize(cross(-dFdy(_1257), dFdx(_1257)));
    vec3 _1334 = -normalize(_634.lUniforms.mpPerFrame.gViewPositionVec3 - _1257);
    vec2 _1340 = dFdx(_1543.xy);
    vec2 _1342 = dFdy(_1543.xy);
    vec3 _1345 = cross(dFdy(_1334), _1329);
    vec3 _1348 = cross(_1329, dFdx(_1334));
    vec3 _1357 = (_1345 * _1340.x) + (_1348 * _1342.x);
    vec3 _1366 = (_1345 * _1340.y) + (_1348 * _1342.y);
    float _1374 = inversesqrt(max(dot(_1357, _1357), dot(_1366, _1366)));
    vec4 _1569 = _1587;
    _1569.x = 0.0;
    vec4 _1571 = _1569;
    _1571.y = sqrt(_634.lUniforms.mpCustomPerMaterial.gMaterialParamsVec4.x * 0.5);
    vec4 _1573 = _1571;
    _1573.z = clamp(_634.lUniforms.mpCustomPerMaterial.gMaterialParamsVec4.z, 0.0, 1.0);
    vec4 _1575 = _1573;
    _1575.w = 0.0;
    out_color0 = vec4(_756.xyz, _762);
    out_color1 = vec4(0.0);
    out_color2 = vec4((normalize(mat3(_1357 * _1374, _1366 * _1374, -_1329) * vec3(_1289, _1291, sqrt(max((1.0 - (_1289 * _1289)) - (_1291 * _1291), 0.0)))) * 0.5) + vec3(0.5), _762);
    out_color3 = _1575;
}


