
// Vk3DVision (http://3dsurroundgaming.com/) - Original Shader
#version 450
#extension GL_EXT_nonuniform_qualifier : require
#extension GL_ARB_shader_draw_parameters : require
#extension GL_ARB_shader_ballot : require

out float gl_ClipDistance[1];

invariant gl_Position;

struct S_MATRICES
{
    mat4 _mModelViewProj;
    mat4 _mModelViewProjPrevious;
    mat4 _mModelView;
    mat4 _mInvModelView;
};

struct unaligned_float3
{
    float x;
    float y;
    float z;
};

struct S_TBN
{
    int _iNormal;
    int _iTangent;
};

struct S_PASS_MATRICES
{
    mat4 _mViewJitteredProj;
    mat4 _mViewProj;
    mat4 _mView;
    mat4 _mInvView;
    mat4 _mJitteredProj;
};

struct S_EYE_DATA
{
    S_PASS_MATRICES _mCurrentFrameMatrices[2];
    S_PASS_MATRICES _mPreviousFrameMatrices[2];
    mat4 _mJitteredProjection[2];
    mat4 _mNoJitterProjection[2];
    mat4 _mInvViewJitteredProj[2];
    vec4 _vWorldSpaceCameraVector[2];
    vec4 _vWorldSpaceCameraPosition[2];
    vec4 _vFrameBufferScale;
    vec4 _vFrameBufferViewportSize;
    vec4 _vFrameBufferOneOnViewportSize;
    vec4 _vViewportSize;
    vec4 _vFogSubCrop;
    uint _uiAlignment0;
    uint _uiAlignment1;
    float _vCameraJitterDeltaX;
    float _vCameraJitterDeltaY;
    vec4 _vfRcpFVolumetricFogDepthTileSize;
};

struct S_CASCADED_SHADOW
{
    mat4 _mCascadeMatrix[2][8];
    mat4 _mViewSpaceToStaticShadowMapSpace[2];
    vec4 _vfCascadeShadowAttenuation;
    vec4 _vfShadowCascadeTextureTexelSize;
    vec4 _vfStaticShadowBlurRadius;
    vec4 _vfCascadeLinearZProjFactors[8];
    vec4 _vfCascadeParams[8];
};

struct S_CLUSTER_LIGHT_SETUP
{
    S_CASCADED_SHADOW _CascadedShadow;
    vec4 _vfSkinUVParamsAndNormalBias;
    vec4 _vfShadowTextureTexelSize;
    vec4 _vfDummy_SkinFresnelF0AndF90;
    uint _uiCascadedUsed;
    float _fSSSWidth;
    float _fOffsetBias;
    float _fDummy;
};

struct S_CLUSTER_INFO
{
    mat4 _mViewToClusterView[2];
    mat4 _mClusterProjMatrix;
    mat4 _mClusterInvProjMatrix;
    vec4 _vfLeftEyeToCombinedEyeVsDeltaXZ;
    uint _uiFinestMipWidth;
    uint _uiFinestMipHeight;
    uint _uiFinestMipDepth;
    uint _uiFinestMipWidthHeight;
    float _fZSliceGeomFactor;
    float _fRcpLogZSliceGeomFactor;
    float _fDummy0;
    float _fDummy1;
    float _fNearP;
    float _fInvNearP;
    float _fFarP;
    float _fClusterCoincidentWithCamera;
};

struct S_PASS
{
    S_EYE_DATA _EyeData;
    vec4 _avFrustumCorners[4];
    vec4 _vTime;
    vec4 _cFogColor;
    vec4 _vFogSetup;
    vec4 _cSunColor;
    vec4 _vSunDir;
    vec4 _vProjectionSetup;
    vec4 _vProjectionCoeff;
    vec4 _vOutputColor;
    vec4 _vfVsClippingPlane[2];
    S_CLUSTER_LIGHT_SETUP _LightSetup;
    S_CLUSTER_INFO _ClusterInfo;
    float _fIBLIntensity;
    float _fGIIntensity;
    float _fAmbiantOcclusionDirectIntensity;
    float _fAmbiantOcclusionIndirectIntensity;
    float _fNormalSign;
    int _iFogType;
    float _fSelfOcclusionDebugRatio;
    float _fEmissive;
    int _iEnableSkinSSSSS;
    int _iUseHairOpaquePrepass;
    float _fLocalSceneExposure;
    uint _uiDebugMode;
    uint _sFlags;
    float _fTAADropsSharpness;
    uint _uNeoScaleX;
    uint _uNeoScaleY;
    uint _uiDirectOverdrawFilter;
    float _fCoderTest0;
    float _fCoderTest1;
    float _fCoderTest2;
    uint _uiBakeOutput;
    float _fDefaultAmbientIntensity;
    float _fDefaultLightingIntensity;
    uint _uiOutputColor;
    float _fAARotation;
    float _fClampDirectSpecular;
    float _fNearPlane;
    float _fFarPlane;
    float _fNDFFilteringScreenSpaceVariance;
    float _fNDFFilteringThreshold;
    float _fNDFFilteringScreenSpaceVariance_EyeShader;
    float _fNDFFilteringThreshold_EyeShader;
    float _fSurfaceGradientTextureFiltering;
    float _fZero;
    uint _uDepthClamp;
    uint _uiLightDebugMode;
    vec4 _vfFragCoordToDepthTexFactor;
    vec4 _vfFragCoordToDepthTexFactor_HalfRes;
    uint _uHdr;
    uint _uiDrawMayaSwatch;
    float _fEyeRoughnessBias;
    float _fDummy1;
};

struct S_INDICES
{
    uint _uiObjectIndex;
    uint _uiMaterialIndex;
    uint _uiMaterialInstanceIndex;
    uint _uiLastFrameMaterialIndex;
    uint _uiLastFrameMaterialInstanceIndex;
    uint _uiPositionsBufferIndex;
    uint _uiPrevPositionsBufferIndex;
    uint _uiNormalsBufferIndex;
    uint _uiPositionOffset;
    uint _uiPrevPositionOffset;
    uint _uiNormalOffset;
    uint _uiMatricesIndex;
};

struct S_MAIN_DEFAULT
{
    S_INDICES _Indices;
};

struct S_MAIN_DEFAULT_ARRAY
{
    S_MAIN_DEFAULT _aMain[32];
};

struct S_SPEEDTREE_MESH_OBJECT
{
    mat4 _mModel;
    mat4 _mModelLast;
    mat4 _mInvModel;
    mat4 _mInvModelLast;
    vec4 _vDiversityOffset;
    uint _uiSceneZoneIndex;
    uint _uiNeighborSceneZoneIndex;
    uint _uiDisplayFlags;
    uint _uiPackedLocalExposureCompensation;
    vec4 _vPortalDirAndDistance;
    int _iPickingID[2];
    uint _uiPadding[2];
    vec4 _vTreeRotation;
    vec4 _vWindGlobal;
    vec4 _vWindVector;
    vec4 _vWindBranchAdherences;
    vec4 _vWindBranch;
    vec4 _vWindBranchTwitch;
    vec4 _vWindBranchWhipTurbulences;
    vec4 _vWindFrondRipple;
    vec4 _vWindLeaf1Ripple;
    vec4 _vWindLeaf1Tumble;
    vec4 _vWindLeaf1Twitch;
    vec4 _vWindLeaf2Ripple;
    vec4 _vWindLeaf2Tumble;
    vec4 _vWindLeaf2Twitch;
    vec4 _vAnchorAllTime;
    vec4 _vRollingBranch;
    vec4 _vRollingLeafOffset;
    vec4 _vRollingNoise;
    uint _uiLOD;
    uint _uiSmooth;
    float _fLocalExposureCompensation;
    float _fLocalExposureCompensationEV100;
    vec4 _vWindGlobalLast;
    vec4 _vWindBranchLast;
    vec4 _vWindFrondRippleLast;
    vec4 _vWindLeaf1RippleLast;
    vec4 _vWindLeaf1TumbleLast;
    vec4 _vWindLeaf1TwitchLast;
    vec4 _vWindLeaf2RippleLast;
    vec4 _vWindLeaf2TumbleLast;
    vec4 _vAnchorAllTimeLast;
};

layout(set = 1, binding = 24, std430) readonly buffer stc_g_rbMatrices_Layout
{
    S_MATRICES g_rbMatrices[];
} _459;

layout(set = 1, binding = 39, std430) readonly buffer g_rbVertices_Layout
{
    unaligned_float3 g_rbVertices[];
} g_rbVertices_Buffer[];

layout(set = 1, binding = 40, std430) readonly buffer g_rbTBNs_Layout
{
    S_TBN g_rbTBNs[];
} g_rbTBNs_Buffer[];

layout(set = 2, binding = 53, std140) uniform dyn_CONSTANT_BUFFER_BLOCK_CB_S_PASS_BINDING
{
    S_PASS g_cbPass;
} _3070;

layout(set = 0, binding = 52, std140) uniform dyn_CONSTANT_BUFFER_BLOCK_CB_S_MAIN_ARRAY_BINDING
{
    S_MAIN_DEFAULT_ARRAY g_cbMainArray;
} _3454;

layout(set = 1, binding = 33, std430) readonly buffer g_rbObjects_Layout
{
    S_SPEEDTREE_MESH_OBJECT g_rbObjects[];
} g_rbObjects_Buffer[];

layout(location = 8) in vec2 in_uv0;
layout(location = 10) in vec4 in_lodPosition;
layout(location = 11) in vec4 in_windBranchData;
layout(location = 12) in vec4 in_windExtraDataFlag;
layout(location = 13) in vec4 in_LeafAnchorHint;
layout(location = 8) out vec2 interp_uv0;
layout(location = 21) out vec3 interp_vfVsPosition;
layout(location = 14) out vec3 interp_vfVsTangent;
layout(location = 15) out vec3 interp_vfVsBinormal;
layout(location = 13) out vec3 interp_vfVsNormal;
layout(location = 23) out float interp_instanceID;

void main()
{
    int _3444 = gl_InstanceIndex + gl_DrawIDARB;
    int _3445 = readFirstInvocationARB(_3444);
    uint _5707 = uint(gl_VertexIndex);
    uint _5710 = _5707 + _3454.g_cbMainArray._aMain[_3445]._Indices._uiPositionOffset;
    vec4 _5723 = vec4(g_rbVertices_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiPositionsBufferIndex].g_rbVertices[_5710].x, g_rbVertices_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiPositionsBufferIndex].g_rbVertices[_5710].y, g_rbVertices_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiPositionsBufferIndex].g_rbVertices[_5710].z, 1.0);
    uint _5730 = _5707 + _3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalOffset;
    int _5759 = g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iNormal & 1023;
    int _5762 = (g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iNormal >> 10) & 1023;
    int _5765 = (g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iNormal >> 20) & 1023;
    int _11790;
    if (_5759 > 512)
    {
        _11790 = _5759 | (-1024);
    }
    else
    {
        _11790 = _5759;
    }
    int _11792;
    if (_5762 > 512)
    {
        _11792 = _5762 | (-1024);
    }
    else
    {
        _11792 = _5762;
    }
    int _11794;
    if (_5765 > 512)
    {
        _11794 = _5765 | (-1024);
    }
    else
    {
        _11794 = _5765;
    }
    vec3 _5818 = vec3(float(_11790), float(_11792), float(_11794)) * vec3(0.001956947147846221923828125);
    int _5827 = g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iTangent & 1023;
    int _5830 = (g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iTangent >> 10) & 1023;
    int _5833 = (g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iTangent >> 20) & 1023;
    int _11796;
    if (_5827 > 512)
    {
        _11796 = _5827 | (-1024);
    }
    else
    {
        _11796 = _5827;
    }
    int _11798;
    if (_5830 > 512)
    {
        _11798 = _5830 | (-1024);
    }
    else
    {
        _11798 = _5830;
    }
    int _11800;
    if (_5833 > 512)
    {
        _11800 = _5833 | (-1024);
    }
    else
    {
        _11800 = _5833;
    }
    float _5891 = float((g_rbTBNs_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiNormalsBufferIndex].g_rbTBNs[_5730]._iTangent >> 30) != 0);
    vec3 _5908 = _5723.xyz;
    vec3 _6022 = vec4(vec3(float(_11796), float(_11798), float(_11800)) * vec3(0.001956947147846221923828125), _5891).xyz;
    vec3 _6239 = vec3(g_rbObjects_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiObjectIndex].g_rbObjects[0]._vTreeRotation.z);
    vec3 _6240 = mix(in_lodPosition.xyz, _5908, _6239);
    float _6699 = _6240.x;
    float _6700 = _6240.y;
    float _6701 = _6240.z;
    vec4 _6702 = vec4(_6699, _6700, _6701, 1.0);
    vec4 _6703 = g_rbObjects_Buffer[_3454.g_cbMainArray._aMain[_3445]._Indices._uiObjectIndex].g_rbObjects[0]._mModel * _6702;
    vec3 _6704 = _6703.xyz;
    vec3 _6811 = _6704 - _3070.g_cbPass._EyeData._vWorldSpaceCameraPosition[0].xyz;
    float _6812 = length(_6811);
    float _6821 = _6812 - 80.0;
    float _6825 = _6821 * 0.0500000007450580596923828125;
    float _6826 = clamp(_6825, 0.0, 1.0);
    float _6829 = _6826 * (-1.0);
    vec3 _6831 = vec3(0.0, _6829, 0.0);
    vec3 _6843 = _6704 + _6831;
    float _6879 = _6843.x;
    float _6880 = _6843.y;
    float _6881 = _6843.z;
    vec4 _6882 = vec4(_6879, _6880, _6881, 1.0);
    vec4 _6883 = _3070.g_cbPass._EyeData._mCurrentFrameMatrices[0]._mView * _6882;
    vec4 _6899 = _3070.g_cbPass._EyeData._mCurrentFrameMatrices[0]._mViewJitteredProj * _6882;
    vec4 _11809;
    if (_3070.g_cbPass._uDepthClamp == 4294967295u)
    {
        float _6954 = _6899.w;
        vec4 _11810;
        if ((_6899.z / _6954) > 1.0)
        {
            vec4 _11811;
            if (_6883.z > _3070.g_cbPass._fNearPlane)
            {
                vec4 _11777 = _6899;
                _11777.z = _6954;
                _11811 = _11777;
            }
            else
            {
                _11811 = _6899;
            }
            _11810 = _11811;
        }
        else
        {
            _11810 = _6899;
        }
        _11809 = _11810;
    }
    else
    {
        _11809 = _6899;
    }
    interp_instanceID = float(_3444) + 0.5;
    float _5590 = _6883.x;
    float _5591 = _6883.y;
    float _5592 = _6883.z;
    vec4 _5593 = vec4(_5590, _5591, _5592, 1.0);
    float _5594 = dot(_3070.g_cbPass._vfVsClippingPlane[0], _5593);
    float _5595 = -_5594;
    gl_ClipDistance[0] = _5595;
    interp_uv0 = in_uv0;
    interp_vfVsPosition = _6883.xyz;
    interp_vfVsTangent = mat3(_459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[0].xyz, _459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[1].xyz, _459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[2].xyz) * _6022;
    interp_vfVsBinormal = mat3(_459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[0].xyz, _459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[1].xyz, _459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[2].xyz) * (cross(_5818, _6022) * ((_5891 != 0.0) ? (-1.0) : 1.0));
    interp_vfVsNormal = mat3(_459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[0].xyz, _459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[1].xyz, _459.g_rbMatrices[_3454.g_cbMainArray._aMain[_3445]._Indices._uiMatricesIndex]._mModelView[2].xyz) * _5818;
    gl_Position = _11809;
}

