Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 450 core
- /*Copyright 2009-2016 DIVIDE-Studio*/
- #extension GL_ARB_shader_draw_parameters : require
- #extension GL_ARB_gpu_shader5 : require
- #define GPU_VENDOR_AMD 1
- #define GPU_VENDOR_NVIDIA 0
- #define GPU_VENDOR_INTEL 2
- #define GPU_VENDOR_OTHER 3
- #define GPU_VENDOR 0
- #define _DEBUG
- #define VERT_SHADER
- #define SKIP_TEXTURES
- #define USE_DOUBLE_SIDED
- #define USE_SHADING_PHONG
- //#pragma optionNV(fastmath on)
- //#pragma optionNV(fastprecision on)
- //#pragma optionNV(inline all)
- //#pragma optionNV(ifcvt none)
- //#pragma optionNV(strict on)
- //#pragma optionNV(unroll all)
- #define USE_HIZ_CULLING
- const uint MAX_SPLITS_PER_LIGHT = 6;
- const uint MAX_POSSIBLE_LIGHTS = 1024;
- const int MAX_VISIBLE_NODES = 1024;
- const float Z_TEST_SIGMA = 0.0001;
- const float ALPHA_DISCARD_THRESHOLD = 0.05;
- #define USE_HIZ_CULLING
- #define MAX_CLIP_PLANES 6
- #define MAX_SHADOW_CASTING_LIGHTS 5
- #define MAX_LIGHT_TYPES 3
- #define BUFFER_GPU_BLOCK 0
- #define BUFFER_GPU_COMMANDS 1
- #define BUFFER_LIGHT_NORMAL 2
- #define BUFFER_LIGHT_SHADOW 3
- #define BUFFER_LIGHT_INDICES 4
- #define BUFFER_NODE_INFO 5
- #define BUFFER_SCENE_DATA 7
- #define FORWARD_PLUS_TILE_RES 16
- #define MAX_NUM_LIGHTS_PER_TILE 544
- #define LIGHT_INDEX_BUFFER_SENTINEL 2147483647
- #define TEXTURE_UNIT0 0
- #define TEXTURE_DEPTH_MAP 6
- invariant gl_Position;
- const uint MAX_BONE_COUNT_PER_NODE = 256;
- #define BUFFER_BONE_TRANSFORMS 6
- #define DIRECTIONAL_LIGHT_DISTANCE_FACTOR 500
- layout(location = 0) in vec3 inVertexData;
- layout(location = 1) in vec4 inColourData;
- layout(location = 2) in float inNormalData;
- layout(location = 3) in vec2 inTexCoordData;
- layout(location = 4) in float inTangentData;
- layout(location = 5) in vec4 inBoneWeightData;
- layout(location = 6) in uvec4 inBoneIndiceData;
- layout(location = 7) in uint inLineWidthData;
- out PerVertexData{
- flat uint dvd_drawID;
- vec2 _texCoord;
- vec4 _vertexW;
- vec4 _vertexWV;
- vec3 _normalWV;
- vec3 _tangentWV;
- vec3 _bitangentWV;
- } v_out;
- #define VAR v_out
- #ifndef _NODE_DATA_INPUT_CMN_
- #define _NODE_DATA_INPUT_CMN_
- #if defined(COMPUTE_SHADER)
- struct IndirectDrawCommand {
- uint count;
- uint instanceCount;
- uint firstIndex;
- uint baseVertex;
- uint baseInstance;
- };
- layout(binding = BUFFER_GPU_COMMANDS, std430) coherent buffer dvd_GPUCmds
- {
- IndirectDrawCommand dvd_drawCommands[MAX_VISIBLE_NODES];
- };
- #endif
- layout(binding = BUFFER_GPU_BLOCK, std140) uniform dvd_GPUBlock
- {
- mat4 dvd_ProjectionMatrix;
- mat4 dvd_ViewMatrix;
- mat4 dvd_ViewProjectionMatrix;
- vec4 dvd_cameraPosition; ///xyz - position, w - aspect ratio
- vec4 dvd_ViewPort;
- vec4 dvd_ZPlanesCombined; //xy - current, zw - main scene
- vec4 dvd_invScreenDimensions;
- vec4 dvd_renderProperties;
- vec4 dvd_frustumPlanes[6];
- vec4 dvd_clip_plane[MAX_CLIP_PLANES];
- };
- #define dvd_shadowArrayOffset int(dvd_renderProperties.x)
- #define dvd_GSInvocationLimit int(dvd_renderProperties.y)
- #define dvd_aspectRatio dvd_cameraPosition.w
- #define dvd_fieldOfView dvd_renderProperties.z
- #define dvd_tanHalfFieldOfView dvd_renderProperties.w
- layout(binding = BUFFER_SCENE_DATA, std140) uniform dvd_SceneData
- {
- vec4 dvd_fogDetails;
- vec4 dvd_windDetails;
- vec4 dvd_shadowingSettings;
- vec4 dvd_otherData;
- vec4 dvd_otherData2;
- uvec4 dvd_lightCountPerType;
- //uint dvd_lightCountPerType[MAX_LIGHT_TYPES];
- };
- #define dvd_fogColour dvd_fogDetails.xyz
- #define dvd_fogDensity dvd_fogDetails.w
- #define dvd_time int(dvd_otherData.x)
- #define dvd_showDebugInfo int(dvd_otherData.y)
- int dvd_deltaTime() {
- return int(dvd_otherData2.x);
- }
- bool dvd_shadowsEnabled() {
- return int(dvd_otherData.z) == 1;
- }
- #if defined(VERT_SHADER)
- invariant gl_Position;
- //out float gl_ClipDistance[MAX_CLIP_PLANES];
- void setClipPlanes(in vec4 worldSpaceVertex) {
- #if MAX_CLIP_PLANES > 0
- gl_ClipDistance[0] = dot(worldSpaceVertex, dvd_clip_plane[0]);
- # if MAX_CLIP_PLANES > 1
- gl_ClipDistance[1] = dot(worldSpaceVertex, dvd_clip_plane[1]);
- # if MAX_CLIP_PLANES > 2
- gl_ClipDistance[2] = dot(worldSpaceVertex, dvd_clip_plane[2]);
- # if MAX_CLIP_PLANES > 3
- gl_ClipDistance[3] = dot(worldSpaceVertex, dvd_clip_plane[3]);
- # if MAX_CLIP_PLANES > 4
- gl_ClipDistance[4] = dot(worldSpaceVertex, dvd_clip_plane[4]);
- # if MAX_CLIP_PLANES > 5
- gl_ClipDistance[5] = dot(worldSpaceVertex, dvd_clip_plane[5]);
- # endif
- # endif
- # endif
- # endif
- # endif
- #endif
- }
- #endif
- //mix(false, true, condition);
- #endif //_NODE_DATA_INPUT_CMN_
- #line 125
- #ifndef _VB_INPUT_DATA_VERT_
- #define _VB_INPUT_DATA_VERT_
- invariant gl_Position;
- #ifndef _NODE_BUFFERED_INPUT_CMN_
- #define _NODE_BUFFERED_INPUT_CMN_
- struct NodeData {
- mat4 _worldMatrix;
- // [0][0] ... [2][2] = normal matrix
- // [0][3] = bone count
- // [3][0] ... [3][3] = bounding sphere
- mat4 _normalMatrixWV;
- // [0][0] ... [0][3] = albedo
- // [1][0] ... [1][3] = specular
- // [2][0] ... [2][3] = emissive
- // [3][0] = is translucent
- // [3][1] = texture operation
- // [3][2] = texture count
- // [3][3] = parallax factor
- mat4 _colourMatrix;
- // x = selection flag
- // y = is shadow received
- // z = lod level
- // w = reserved
- vec4 _properties;
- };
- layout(binding = BUFFER_NODE_INFO, std430) coherent readonly buffer dvd_MatrixBlock
- {
- NodeData dvd_Matrices[MAX_VISIBLE_NODES];
- };
- mat4 dvd_WorldMatrix() {
- return dvd_Matrices[VAR.dvd_drawID]._worldMatrix;
- }
- mat3 dvd_NormalMatrixWV() {
- return mat3(dvd_Matrices[VAR.dvd_drawID]._normalMatrixWV);
- }
- ivec4 dvd_BufferIntegerValues() {
- return ivec4(dvd_Matrices[VAR.dvd_drawID]._properties);
- }
- #if defined(VERT_SHADER)
- #define dvd_boneCount uint(dvd_Matrices[VAR.dvd_drawID]._normalMatrixWV[0][3])
- #endif
- // x - isSelected/isHighlighted; y - isShadowMapped; z - lodLevel, w - occlusion cull data
- #define dvd_lodLevel dvd_BufferIntegerValues().z
- #define dvd_customData dvd_Matrices[VAR.dvd_drawID]._properties.w
- #if defined(FRAG_SHADER)
- // x - useAlphaTest; y - textureOperation; z - textureCount, w - parallax/relief mapping factor
- #define buffer_matProperties dvd_Matrices[VAR.dvd_drawID]._colourMatrix[3]
- #define dvd_isSelected (dvd_BufferIntegerValues().x < -0.5)
- #define dvd_isHighlighted (dvd_BufferIntegerValues().x > 0.5)
- #define dvd_shadowMapping (dvd_BufferIntegerValues().y > 0.0)
- #define dvd_useAlphaTest (buffer_matProperties.x > 0.0)
- #define dvd_texOperation uint(buffer_matProperties.y)
- #define dvd_textureCount uint(buffer_matProperties.z)
- #define dvd_parallaxFactor buffer_matProperties.w
- #define dvd_reliefFactor dvd_parallaxFactor
- #endif
- #endif //_NODE_BUFFERED_INPUT_CMN_
- #if defined(USE_GPU_SKINNING)
- #ifndef _BONE_TRANSFORM_VERT_
- #define _BONE_TRANSFORM_VERT_
- layout(binding = BUFFER_BONE_TRANSFORMS, std430) coherent readonly buffer dvd_BoneTransforms
- {
- mat4 boneTransforms[MAX_BONE_COUNT_PER_NODE];
- };
- #if defined(COMPUTE_TBN)
- void applyBoneTransforms(inout vec4 position, inout vec3 normal, inout vec3 tangnet, in int lod){
- #else
- void applyBoneTransforms(inout vec4 position, inout vec3 normal, in int lod){
- #endif
- if (dvd_boneCount == 0) {
- return;
- }
- mat4 transformMatrix[4] = mat4[](inBoneWeightData.x * boneTransforms[inBoneIndiceData.x],
- inBoneWeightData.y * boneTransforms[inBoneIndiceData.y],
- inBoneWeightData.z * boneTransforms[inBoneIndiceData.z],
- inBoneWeightData.w * boneTransforms[inBoneIndiceData.w]);
- position = (transformMatrix[0] * position + transformMatrix[1] * position +
- transformMatrix[2] * position + transformMatrix[3] * position);
- if (lod >= 2) {
- return;
- }
- vec4 tempVec = vec4(normal, 0.0);
- normal = vec4(transformMatrix[0] * tempVec + transformMatrix[1] * tempVec +
- transformMatrix[2] * tempVec + transformMatrix[3] * tempVec).xyz;
- #if defined(COMPUTE_TBN)
- tempVec = vec4(tangnet, 0.0);
- tangnet = vec4(transformMatrix[0] * tempVec + transformMatrix[1] * tempVec +
- transformMatrix[2] * tempVec + transformMatrix[3] * tempVec).xyz;
- #endif
- }
- #endif //_BONE_TRANSFORM_VERT_
- #endif
- vec4 dvd_Vertex;
- vec4 dvd_Colour;
- vec3 dvd_Normal;
- vec3 dvd_Tangent;
- vec3 UNPACK_FLOAT(in float value) {
- return (fract(vec3(1.0, 256.0, 65536.0) * value)* 2.0) - 1.0;
- }
- void computeData(){
- VAR.dvd_drawID = gl_BaseInstanceARB;
- dvd_Vertex = vec4(inVertexData, 1.0);
- //Occlusion culling visibility debug code
- #if defined(USE_HIZ_CULLING) && defined(DEBUG_HIZ_CULLING)
- if (dvd_customData > 2.0) {
- dvd_Vertex.xyz *= 5;
- }
- #endif
- dvd_Normal = UNPACK_FLOAT(inNormalData);
- dvd_Colour = inColourData;
- dvd_Tangent = UNPACK_FLOAT(inTangentData);
- # if defined(USE_GPU_SKINNING)
- # if defined(COMPUTE_TBN)
- applyBoneTransforms(dvd_Vertex, dvd_Normal, dvd_Tangent, dvd_lodLevel);
- # else
- applyBoneTransforms(dvd_Vertex, dvd_Normal, dvd_lodLevel);
- # endif
- # endif
- VAR._texCoord = inTexCoordData;
- VAR._vertexW = dvd_WorldMatrix() * dvd_Vertex;
- setClipPlanes(VAR._vertexW);
- }
- #endif //_VB_INPUT_DATA_VERT_
- #ifndef _LIGHTING_DEFAULTS_VERT_
- #define _LIGHTING_DEFAULTS_VERT_
- #ifndef _LIGHT_INPUT_CMN_
- #define _LIGHT_INPUT_CMN_
- #if defined(COMPUTE_SHADER)
- # define ACCESS
- #else
- # define ACCESS readonly
- #endif
- const int LIGHT_DIRECTIONAL = 0;
- const int LIGHT_OMNIDIRECTIONAL = 1;
- const int LIGHT_SPOT = 2;
- struct Light {
- /// rgb = colour
- /// w = reserved
- vec4 _colour;
- /// xyz = light positon (or direction for Directional lights)
- /// w = range
- vec4 _positionWV;
- /// xyz = spot direction
- /// w = spot angle
- vec4 _directionWV;
- /// x = light type: 0.0 - directional, 1.0 - point, 2.0 - spot
- /// y = casts shadows
- /// z = shadow block index
- /// w = if directional light : csm split count else : reserved
- ivec4 _options;
- };
- layout(binding = BUFFER_LIGHT_NORMAL, std430) coherent ACCESS buffer dvd_LightBlock
- {
- Light dvd_LightSource[MAX_POSSIBLE_LIGHTS];
- };
- layout(binding = BUFFER_LIGHT_INDICES, std430) coherent ACCESS buffer perTileLightIndexBuffer
- {
- uint perTileLightIndices[];
- };
- #if defined(FRAG_SHADER)
- struct Shadow {
- uvec4 _arrayOffset;
- mat4 _lightVP[MAX_SPLITS_PER_LIGHT];
- vec4 _lightPosition[MAX_SPLITS_PER_LIGHT];
- vec4 _floatValues[MAX_SPLITS_PER_LIGHT];
- };
- layout(binding = BUFFER_LIGHT_SHADOW, std430) coherent ACCESS buffer dvd_ShadowBlock
- {
- Shadow dvd_ShadowSource[MAX_SHADOW_CASTING_LIGHTS];
- };
- #endif
- #define windowWidth int(dvd_ViewPort.z)
- #define windowHeight int(dvd_ViewPort.w)
- // calculate the number of tiles in the horizontal direction
- uint GetNumTilesX()
- {
- return uint((windowWidth + FORWARD_PLUS_TILE_RES - 1) / float(FORWARD_PLUS_TILE_RES));
- }
- // calculate the number of tiles in the vertical direction
- uint GetNumTilesY()
- {
- return uint((windowHeight + FORWARD_PLUS_TILE_RES - 1) / float(FORWARD_PLUS_TILE_RES));
- }
- uint GetTileIndex(in vec2 ScreenPos)
- {
- float fTileRes = float(FORWARD_PLUS_TILE_RES);
- return uint(floor(ScreenPos.x / fTileRes) + floor(ScreenPos.y / fTileRes) * GetNumTilesX());
- }
- #endif //_LIGHT_INPUT_CMN_
- void computeLightVectors(){
- VAR._vertexWV = dvd_ViewMatrix * VAR._vertexW;
- VAR._normalWV = normalize(dvd_NormalMatrixWV() * dvd_Normal);
- #if defined(COMPUTE_TBN)
- VAR._tangentWV = normalize(dvd_NormalMatrixWV() * dvd_Tangent);
- VAR._bitangentWV = normalize(cross(VAR._normalWV, VAR._tangentWV));
- #endif
- }
- #endif //_LIGHTING_DEFAULTS_VERT_
- #if defined(ADD_FOLIAGE)
- #ifndef _FOLIAGE_VERT_
- #define _FOLIAGE_VERT_
- uniform vec3 scale;
- uniform float grassScale;
- uniform float lod_metric;
- void computeFoliageMovementTree(inout vec4 vertex) {
- float move_speed = (float(int(VAR._vertexW.y * VAR._vertexW.z) % 50)/50.0 + 0.5);
- float timeTree = dvd_time * 0.001 * dvd_windDetails.w * move_speed; //to seconds
- float amplituted = pow(vertex.y, 2.0);
- vertex.x += 0.01 * amplituted * cos(timeTree + VAR._vertexW.x) *dvd_windDetails.x;
- vertex.z += 0.05 *scale.y* amplituted * cos(timeTree + VAR._vertexW.z) *dvd_windDetails.z;
- }
- void computeFoliageMovementGrass(inout vec4 vertex) {
- float timeGrass = dvd_windDetails.w * dvd_time * 0.001; //to seconds
- float cosX = cos(vertex.x);
- float sinX = sin(vertex.x);
- float halfScale = 0.5*grassScale;
- vertex.x += (halfScale*cos(timeGrass) * cosX * sinX)*dvd_windDetails.x;
- vertex.z += (halfScale*sin(timeGrass) * cosX * sinX)*dvd_windDetails.z;
- }
- #endif //_FOLIAGE_VERT_
- #endif
- void main(void){
- computeData();
- #if defined(ADD_FOLIAGE) && defined(IS_TREE)
- computeFoliageMovementTree(dvd_Vertex);
- #endif
- computeLightVectors();
- //Compute the final vert position
- gl_Position = dvd_ViewProjectionMatrix * VAR._vertexW;
- }
Add Comment
Please, Sign In to add comment