#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 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 CustomPerMaterialUniforms
{
    vec4 gImposterDataVec4;
    vec4 gTileBlendScalesVec4;
    vec4 gHueOverlayParamsVec4;
    vec4 gSaturationOverlayParamsVec4;
    vec4 gValueOverlayParamsVec4;
    vec4 gTerrainColour1Vec4;
    vec4 gTerrainColour2Vec4;
    vec4 gWaveOneAmplitudeAndPosOffsetV4;
    vec4 gWaveOneFrequencyVec4;
    vec4 gWaveOneFallOffAndSpeedVec4;
    vec4 gWaveTwoAmplitudeVec4;
    vec4 gWaveTwoFrequencyVec4;
    vec4 gWaveTwoFallOffAndSpeedVec4;
    vec4 gMaterialParamsVec4;
    vec4 gMaterialColourVec4;
    vec4 gMaterialSFXVec4;
    vec4 gMaterialSFXColVec4;
    vec4 gSunPositionVec4;
    vec4 gAverageColour1Vec4;
    vec4 gAverageColour2Vec4;
    vec4 gRecolour1Vec4;
    vec4 gRecolour2Vec4;
    vec4 gSkyColourVec4;
    vec4 gHorizonColourVec4;
    vec4 gSunColourVec4;
    vec4 gWaterFogColourNearVec4;
    vec4 gWaterFogColourFarVec4;
    vec4 gWaterFogVec4;
    vec4 gHeightFogParamsVec4;
    vec4 gHeightFogColourVec4;
    vec4 gSpaceHorizonColourVec4;
    vec4 gFogColourVec4;
    vec4 gFogParamsVec4;
    vec4 gScatteringParamsVec4;
    vec4 gFogFadeHeightsVec4;
    vec4 gSpaceScatteringParamsVec4;
    vec4 gSkyUpperColourVec4;
    vec4 gLightTopColourVec4;
};

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

layout(location = 0) in vec4 mkLocalPositionVec4;
layout(location = 7) in vec4 mkLocalNormalVec4;
layout(location = 3) in vec4 mkTangentVec4;
layout(location = 0) out VertexBlock
{
    vec4 mTexCoordsVec4;
    vec4 mWorldPositionVec3_mfSpare;
    flat mat3 mUpMatrixMat3;
    vec3 mTangentMatRow1Vec3;
    vec3 mTangentMatRow2Vec3;
    vec3 mTangentMatRow3Vec3;
    vec4 mPrevScreenPosition;
    vec4 mScreenSpacePositionVec4;
    flat vec3 mfFadeValueForInstance_mfLodIndex_mfShearMotionLength;
} Out;

layout(location = 1) in vec4 mkTexCoordsVec4;
layout(location = 8) in vec4 mkColourVec4;

vec4 _3324;
mat3 _3341;

void main()
{
    Out.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.y = 0.0;
    Out.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.x = _1002.lUniforms.mpCommonPerMesh.gfFadeValue;
    vec3 _1039 = _1002.lUniforms.mpCommonPerMesh.gWorldMat4[3].xyz - _1002.lUniforms.mpCommonPerMesh.gPlanetPositionVec4.xyz;
    float _1053 = _1002.lUniforms.mpPerFrame.gfTime + (_1002.lUniforms.mpCustomPerMaterial.gWaveOneAmplitudeAndPosOffsetV4.w * ((_1039.x + _1039.y) + _1039.z));
    float _3278;
    if (dot(_1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.xyz, _1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.xyz) > 0.0)
    {
        float _3264;
        if (_1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.x > 0.0)
        {
            _3264 = 1.0 / _1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.x;
        }
        else
        {
            _3264 = 0.0;
        }
        float _3265;
        if (_1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.y > 0.0)
        {
            _3265 = 1.0 / _1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.y;
        }
        else
        {
            _3265 = 0.0;
        }
        float _3266;
        if (_1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.z > 0.0)
        {
            _3266 = 1.0 / _1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.z;
        }
        else
        {
            _3266 = 0.0;
        }
        _3278 = dot(vec3(_3264, _3265, _3266), mkLocalPositionVec4.xyz);
    }
    else
    {
        _3278 = 1.0;
    }
    float _3282;
    if (dot(_1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.xyz, _1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.xyz) > 0.0)
    {
        float _3271;
        if (_1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.x > 0.0)
        {
            _3271 = 1.0 / _1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.x;
        }
        else
        {
            _3271 = 0.0;
        }
        float _3272;
        if (_1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.y > 0.0)
        {
            _3272 = 1.0 / _1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.y;
        }
        else
        {
            _3272 = 0.0;
        }
        float _3273;
        if (_1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.z > 0.0)
        {
            _3273 = 1.0 / _1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.z;
        }
        else
        {
            _3273 = 0.0;
        }
        _3282 = dot(vec3(_3271, _3272, _3273), mkLocalPositionVec4.xyz);
    }
    else
    {
        _3282 = 1.0;
    }
    vec3 _1822 = mkLocalPositionVec4.xyz + (((vec3(sin(dot(_1002.lUniforms.mpCustomPerMaterial.gWaveOneFrequencyVec4.xyz, mkLocalPositionVec4.xyz) + (_1053 * _1002.lUniforms.mpCustomPerMaterial.gWaveOneFallOffAndSpeedVec4.w))) * _1002.lUniforms.mpCustomPerMaterial.gWaveOneAmplitudeAndPosOffsetV4.xyz) * _3278) + ((vec3(sin(dot(_1002.lUniforms.mpCustomPerMaterial.gWaveTwoFrequencyVec4.xyz, mkLocalPositionVec4.xyz) + (_1053 * _1002.lUniforms.mpCustomPerMaterial.gWaveTwoFallOffAndSpeedVec4.w))) * _1002.lUniforms.mpCustomPerMaterial.gWaveTwoAmplitudeVec4.xyz) * _3282));
    float _2589 = mkTexCoordsVec4.x - 0.5;
    float _2592 = 0.5 - mkTexCoordsVec4.y;
    vec4 _2611 = _1002.lUniforms.mpCommonPerMesh.gWorldMat4 * vec4(_1822.xy - vec2(_2589, _2592), _1822.z, 1.0);
    mat4 _3184 = _1002.lUniforms.mpCommonPerMesh.gWorldMat4;
    _3184[3] = _2611;
    vec3 _2661 = vec3(_1002.lUniforms.mpCustomPerMesh.gCustomParams01Vec4.x);
    vec3 _2664 = normalize(mix(_1002.lUniforms.mpCommonPerMesh.gWorldMat4[1].xyz, _1002.lUniforms.mpPerFrame.gCameraNoHeadTrackingMat4[1].xyz, _2661));
    vec3 _2677 = normalize(mix(normalize(_1002.lUniforms.mpPerFrame.gCameraNoHeadTrackingMat4[3].xyz - _2611.xyz), _1002.lUniforms.mpPerFrame.gCameraNoHeadTrackingMat4[2].xyz, vec3((_1002.lUniforms.mpCustomPerMesh.gCustomParams01Vec4.x * _1002.lUniforms.mpCustomPerMesh.gCustomParams01Vec4.x) * _1002.lUniforms.mpCustomPerMesh.gCustomParams01Vec4.x)));
    mat3 _3196 = _3341;
    _3196[2] = _2677;
    mat3 _3199 = _3196;
    _3199[0] = normalize(mix(cross(_2664, _2677), _1002.lUniforms.mpPerFrame.gCameraNoHeadTrackingMat4[0].xyz, _2661));
    mat3 _3201 = _3199;
    _3201[1] = _2664;
    vec3 _2694 = _3201 * vec4(_2589, _2592, 0.0, 1.0).xyz;
    vec4 _3203 = vec4(_2694.x, _2694.y, _2694.z, _3324.w);
    _3203.w = 1.0;
    mat4 _2740 = inverse(_3184);
    mat4 _3208 = _2740;
    _3208[0] = normalize(_2740[0]);
    mat4 _3211 = _3208;
    _3211[1] = normalize(_2740[1]);
    mat4 _3214 = _3211;
    _3214[2] = normalize(_2740[2]);
    mat4 _3216 = _3214;
    _3216[3] = vec4(0.0, 0.0, 0.0, 1.0);
    vec4 _2554 = _3184 * vec4((_3216 * _3203).xyz, 1.0);
    Out.mWorldPositionVec3_mfSpare = vec4(0.0);
    Out.mWorldPositionVec3_mfSpare = vec4(_2554.x, _2554.y, _2554.z, Out.mWorldPositionVec3_mfSpare.w);
    Out.mWorldPositionVec3_mfSpare = vec4(_2554.x, _2554.y, _2554.z, Out.mWorldPositionVec3_mfSpare.w);
    Out.mTangentMatRow1Vec3 = normalize(_1002.lUniforms.mpPerFrame.gCameraMat4[0].xyz);
    Out.mTangentMatRow2Vec3 = normalize(-_1002.lUniforms.mpPerFrame.gCameraMat4[1].xyz);
    Out.mTangentMatRow3Vec3 = normalize(_1002.lUniforms.mpPerFrame.gCameraMat4[2].xyz);
    mat3 _3257 = _3341;
    _3257[0] = _1002.lUniforms.mpPerFrame.gInverseWorldUpMatVec4[0].xyz;
    mat3 _3259 = _3257;
    _3259[1] = _1002.lUniforms.mpPerFrame.gInverseWorldUpMatVec4[1].xyz;
    mat3 _3261 = _3259;
    _3261[2] = _1002.lUniforms.mpPerFrame.gInverseWorldUpMatVec4[2].xyz;
    Out.mUpMatrixMat3 = _3261;
    Out.mTexCoordsVec4 = vec4(mkTexCoordsVec4.x, mkTexCoordsVec4.y, mkTexCoordsVec4.x, mkTexCoordsVec4.y);
    vec4 _2970 = _1002.lUniforms.mpPerFrame.gViewProjectionMat4 * _2554;
    
    _2970.x += vk3d_params.stereo.x * (_2970.w - vk3d_params.stereo.y); 
    
    Out.mScreenSpacePositionVec4 = _2970;
    Out.mPrevScreenPosition = _1002.lUniforms.mpPerFrame.gPrevViewProjectionMat4 * _2554;
    Out.mfFadeValueForInstance_mfLodIndex_mfShearMotionLength.z = 0.0;
    gl_Position = _2970;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
//gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.stereo.y); 
}


