IonutCava

VertexShader

Jun 17th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.27 KB | None | 0 0
  1. #version 450 core
  2. /*Copyright 2009-2016 DIVIDE-Studio*/
  3. #extension GL_ARB_shader_draw_parameters : require
  4. #extension GL_ARB_gpu_shader5 : require
  5. #define GPU_VENDOR_AMD 1
  6. #define GPU_VENDOR_NVIDIA 0
  7. #define GPU_VENDOR_INTEL 2
  8. #define GPU_VENDOR_OTHER 3
  9. #define GPU_VENDOR 0
  10. #define _DEBUG
  11. #define VERT_SHADER
  12. #define SKIP_TEXTURES
  13. #define USE_DOUBLE_SIDED
  14. #define USE_SHADING_PHONG
  15.  
  16. //#pragma optionNV(fastmath on)
  17. //#pragma optionNV(fastprecision on)
  18. //#pragma optionNV(inline all)
  19. //#pragma optionNV(ifcvt none)
  20. //#pragma optionNV(strict on)
  21. //#pragma optionNV(unroll all)
  22. #define USE_HIZ_CULLING
  23. const uint MAX_SPLITS_PER_LIGHT = 6;
  24. const uint MAX_POSSIBLE_LIGHTS = 1024;
  25. const int MAX_VISIBLE_NODES = 1024;
  26. const float Z_TEST_SIGMA = 0.0001;
  27. const float ALPHA_DISCARD_THRESHOLD = 0.05;
  28. #define USE_HIZ_CULLING
  29. #define MAX_CLIP_PLANES 6
  30. #define MAX_SHADOW_CASTING_LIGHTS 5
  31. #define MAX_LIGHT_TYPES 3
  32. #define BUFFER_GPU_BLOCK 0
  33. #define BUFFER_GPU_COMMANDS 1
  34. #define BUFFER_LIGHT_NORMAL 2
  35. #define BUFFER_LIGHT_SHADOW 3
  36. #define BUFFER_LIGHT_INDICES 4
  37. #define BUFFER_NODE_INFO 5
  38. #define BUFFER_SCENE_DATA 7
  39. #define FORWARD_PLUS_TILE_RES 16
  40. #define MAX_NUM_LIGHTS_PER_TILE 544
  41. #define LIGHT_INDEX_BUFFER_SENTINEL 2147483647
  42. #define TEXTURE_UNIT0 0
  43. #define TEXTURE_DEPTH_MAP 6
  44. invariant gl_Position;
  45. const uint MAX_BONE_COUNT_PER_NODE = 256;
  46. #define BUFFER_BONE_TRANSFORMS 6
  47. #define DIRECTIONAL_LIGHT_DISTANCE_FACTOR 500
  48. layout(location = 0) in vec3 inVertexData;
  49. layout(location = 1) in vec4 inColourData;
  50. layout(location = 2) in float inNormalData;
  51. layout(location = 3) in vec2 inTexCoordData;
  52. layout(location = 4) in float inTangentData;
  53. layout(location = 5) in vec4 inBoneWeightData;
  54. layout(location = 6) in uvec4 inBoneIndiceData;
  55. layout(location = 7) in uint inLineWidthData;
  56.  
  57. out PerVertexData{
  58.     flat uint dvd_drawID;
  59.     vec2 _texCoord;
  60.     vec4 _vertexW;
  61.     vec4 _vertexWV;
  62.     vec3 _normalWV;
  63.     vec3 _tangentWV;
  64.     vec3 _bitangentWV;
  65. } v_out;
  66. #define VAR v_out
  67.  
  68. #ifndef _NODE_DATA_INPUT_CMN_
  69. #define _NODE_DATA_INPUT_CMN_
  70.  
  71. #if defined(COMPUTE_SHADER)
  72. struct IndirectDrawCommand {
  73.     uint count;
  74.     uint instanceCount;
  75.     uint firstIndex;
  76.     uint baseVertex;
  77.     uint baseInstance;
  78. };
  79.  
  80. layout(binding = BUFFER_GPU_COMMANDS, std430) coherent buffer dvd_GPUCmds
  81. {
  82.     IndirectDrawCommand dvd_drawCommands[MAX_VISIBLE_NODES];
  83. };
  84. #endif
  85.  
  86. layout(binding = BUFFER_GPU_BLOCK, std140) uniform dvd_GPUBlock
  87. {
  88.     mat4  dvd_ProjectionMatrix;
  89.     mat4  dvd_ViewMatrix;
  90.     mat4  dvd_ViewProjectionMatrix;
  91.     vec4  dvd_cameraPosition; ///xyz - position, w - aspect ratio
  92.     vec4  dvd_ViewPort;
  93.     vec4  dvd_ZPlanesCombined; //xy - current, zw - main scene
  94.     vec4  dvd_invScreenDimensions;
  95.     vec4  dvd_renderProperties;
  96.     vec4  dvd_frustumPlanes[6];
  97.     vec4  dvd_clip_plane[MAX_CLIP_PLANES];
  98. };
  99.  
  100. #define dvd_shadowArrayOffset int(dvd_renderProperties.x)
  101. #define dvd_GSInvocationLimit int(dvd_renderProperties.y)
  102. #define dvd_aspectRatio dvd_cameraPosition.w
  103. #define dvd_fieldOfView dvd_renderProperties.z
  104. #define dvd_tanHalfFieldOfView dvd_renderProperties.w
  105.  
  106. layout(binding = BUFFER_SCENE_DATA, std140) uniform dvd_SceneData
  107. {
  108.     vec4   dvd_fogDetails;
  109.     vec4   dvd_windDetails;
  110.     vec4   dvd_shadowingSettings;
  111.     vec4   dvd_otherData;
  112.     vec4   dvd_otherData2;
  113.     uvec4  dvd_lightCountPerType;
  114.     //uint  dvd_lightCountPerType[MAX_LIGHT_TYPES];
  115. };
  116.  
  117. #define dvd_fogColour dvd_fogDetails.xyz
  118. #define dvd_fogDensity dvd_fogDetails.w
  119. #define dvd_time int(dvd_otherData.x)
  120. #define dvd_showDebugInfo int(dvd_otherData.y)
  121.  
  122. int dvd_deltaTime() {
  123.     return int(dvd_otherData2.x);
  124. }
  125.  
  126. bool dvd_shadowsEnabled() {
  127.     return int(dvd_otherData.z) == 1;
  128. }
  129.  
  130. #if defined(VERT_SHADER)
  131.  
  132. invariant gl_Position;
  133. //out float gl_ClipDistance[MAX_CLIP_PLANES];
  134.  
  135. void setClipPlanes(in vec4 worldSpaceVertex) {
  136.    
  137. #if MAX_CLIP_PLANES > 0
  138.     gl_ClipDistance[0] = dot(worldSpaceVertex, dvd_clip_plane[0]);
  139. #   if MAX_CLIP_PLANES > 1
  140.         gl_ClipDistance[1] = dot(worldSpaceVertex, dvd_clip_plane[1]);
  141. #       if MAX_CLIP_PLANES > 2
  142.             gl_ClipDistance[2] = dot(worldSpaceVertex, dvd_clip_plane[2]);
  143. #           if MAX_CLIP_PLANES > 3
  144.                 gl_ClipDistance[3] = dot(worldSpaceVertex, dvd_clip_plane[3]);
  145. #               if MAX_CLIP_PLANES > 4
  146.                     gl_ClipDistance[4] = dot(worldSpaceVertex, dvd_clip_plane[4]);
  147. #                   if MAX_CLIP_PLANES > 5
  148.                         gl_ClipDistance[5] = dot(worldSpaceVertex, dvd_clip_plane[5]);
  149. #                   endif
  150. #               endif
  151. #           endif
  152. #       endif
  153. #   endif
  154. #endif
  155. }
  156. #endif
  157.  
  158. //mix(false, true, condition);
  159.  
  160. #endif //_NODE_DATA_INPUT_CMN_
  161.  
  162. #line 125
  163.  
  164.  
  165. #ifndef _VB_INPUT_DATA_VERT_
  166. #define _VB_INPUT_DATA_VERT_
  167.  
  168. invariant gl_Position;
  169.  
  170. #ifndef _NODE_BUFFERED_INPUT_CMN_
  171. #define _NODE_BUFFERED_INPUT_CMN_
  172.  
  173. struct NodeData {
  174.     mat4 _worldMatrix;
  175.     // [0][0] ... [2][2] = normal matrix
  176.     // [0][3] = bone count
  177.     // [3][0] ... [3][3] = bounding sphere
  178.     mat4 _normalMatrixWV;
  179.     // [0][0] ... [0][3] = albedo
  180.     // [1][0] ... [1][3] = specular
  181.     // [2][0] ... [2][3] = emissive
  182.     // [3][0] = is translucent
  183.     // [3][1] = texture operation
  184.     // [3][2] = texture count
  185.     // [3][3] = parallax factor
  186.     mat4 _colourMatrix;
  187.     // x = selection flag
  188.     // y = is shadow received
  189.     // z = lod level
  190.     // w = reserved
  191.     vec4 _properties;
  192. };
  193.  
  194. layout(binding = BUFFER_NODE_INFO, std430) coherent readonly buffer dvd_MatrixBlock
  195. {
  196.     NodeData dvd_Matrices[MAX_VISIBLE_NODES];
  197. };
  198.  
  199. mat4 dvd_WorldMatrix() {
  200.     return dvd_Matrices[VAR.dvd_drawID]._worldMatrix;
  201. }
  202.  
  203. mat3 dvd_NormalMatrixWV() {
  204.     return mat3(dvd_Matrices[VAR.dvd_drawID]._normalMatrixWV);
  205. }
  206.  
  207. ivec4 dvd_BufferIntegerValues() {
  208.     return ivec4(dvd_Matrices[VAR.dvd_drawID]._properties);
  209. }
  210.  
  211. #if defined(VERT_SHADER)
  212. #define dvd_boneCount     uint(dvd_Matrices[VAR.dvd_drawID]._normalMatrixWV[0][3])
  213. #endif
  214.  
  215. // x - isSelected/isHighlighted; y - isShadowMapped; z - lodLevel, w - occlusion cull data
  216. #define dvd_lodLevel dvd_BufferIntegerValues().z
  217. #define dvd_customData dvd_Matrices[VAR.dvd_drawID]._properties.w
  218.  
  219. #if defined(FRAG_SHADER)
  220. // x - useAlphaTest; y - textureOperation; z - textureCount, w - parallax/relief mapping factor
  221. #define buffer_matProperties dvd_Matrices[VAR.dvd_drawID]._colourMatrix[3]
  222.  
  223. #define dvd_isSelected     (dvd_BufferIntegerValues().x < -0.5)
  224. #define dvd_isHighlighted  (dvd_BufferIntegerValues().x >  0.5)
  225. #define dvd_shadowMapping  (dvd_BufferIntegerValues().y > 0.0)
  226. #define dvd_useAlphaTest   (buffer_matProperties.x > 0.0)
  227. #define dvd_texOperation   uint(buffer_matProperties.y)
  228. #define dvd_textureCount   uint(buffer_matProperties.z)
  229. #define dvd_parallaxFactor buffer_matProperties.w
  230. #define dvd_reliefFactor   dvd_parallaxFactor
  231.  
  232. #endif
  233.  
  234. #endif //_NODE_BUFFERED_INPUT_CMN_
  235.  
  236.  
  237. #if defined(USE_GPU_SKINNING)
  238. #ifndef _BONE_TRANSFORM_VERT_
  239. #define _BONE_TRANSFORM_VERT_
  240.  
  241. layout(binding = BUFFER_BONE_TRANSFORMS, std430) coherent readonly buffer dvd_BoneTransforms
  242. {
  243.     mat4 boneTransforms[MAX_BONE_COUNT_PER_NODE];
  244. };
  245.  
  246. #if defined(COMPUTE_TBN)
  247. void applyBoneTransforms(inout vec4 position, inout vec3 normal, inout vec3 tangnet, in int lod){
  248. #else
  249. void applyBoneTransforms(inout vec4 position, inout vec3 normal, in int lod){
  250. #endif
  251.     if (dvd_boneCount == 0) {
  252.         return;
  253.     }
  254.  
  255.     mat4 transformMatrix[4] = mat4[](inBoneWeightData.x * boneTransforms[inBoneIndiceData.x],
  256.                                      inBoneWeightData.y * boneTransforms[inBoneIndiceData.y],
  257.                                      inBoneWeightData.z * boneTransforms[inBoneIndiceData.z],
  258.                                      inBoneWeightData.w * boneTransforms[inBoneIndiceData.w]);
  259.  
  260.     position = (transformMatrix[0] * position + transformMatrix[1] * position +
  261.                 transformMatrix[2] * position + transformMatrix[3] * position);
  262.    
  263.     if (lod >= 2) {
  264.         return;
  265.     }
  266.  
  267.     vec4 tempVec = vec4(normal, 0.0);
  268.     normal = vec4(transformMatrix[0] * tempVec + transformMatrix[1] * tempVec +
  269.                   transformMatrix[2] * tempVec + transformMatrix[3] * tempVec).xyz;
  270.  
  271. #if defined(COMPUTE_TBN)
  272.     tempVec = vec4(tangnet, 0.0);
  273.     tangnet = vec4(transformMatrix[0] * tempVec + transformMatrix[1] * tempVec +
  274.                    transformMatrix[2] * tempVec + transformMatrix[3] * tempVec).xyz;
  275. #endif
  276.    
  277. }
  278.  
  279. #endif //_BONE_TRANSFORM_VERT_
  280.  
  281. #endif
  282.  
  283. vec4   dvd_Vertex;
  284. vec4   dvd_Colour;
  285. vec3   dvd_Normal;
  286. vec3   dvd_Tangent;
  287.  
  288. vec3 UNPACK_FLOAT(in float value) {
  289.     return (fract(vec3(1.0, 256.0, 65536.0) * value)* 2.0) - 1.0;
  290. }
  291.  
  292. void computeData(){
  293.     VAR.dvd_drawID  = gl_BaseInstanceARB;
  294.     dvd_Vertex  = vec4(inVertexData, 1.0);
  295.     //Occlusion culling visibility debug code
  296. #if defined(USE_HIZ_CULLING) && defined(DEBUG_HIZ_CULLING)
  297.     if (dvd_customData > 2.0) {
  298.         dvd_Vertex.xyz *= 5;
  299.     }
  300. #endif
  301.     dvd_Normal  = UNPACK_FLOAT(inNormalData);
  302.     dvd_Colour   = inColourData;
  303.     dvd_Tangent = UNPACK_FLOAT(inTangentData);
  304.  
  305. #   if defined(USE_GPU_SKINNING)
  306. #       if defined(COMPUTE_TBN)
  307.             applyBoneTransforms(dvd_Vertex, dvd_Normal, dvd_Tangent, dvd_lodLevel);
  308. #       else
  309.             applyBoneTransforms(dvd_Vertex, dvd_Normal, dvd_lodLevel);
  310. #       endif
  311. #   endif
  312.  
  313.     VAR._texCoord = inTexCoordData;
  314.     VAR._vertexW  = dvd_WorldMatrix() * dvd_Vertex;
  315.  
  316.     setClipPlanes(VAR._vertexW);
  317. }
  318.  
  319. #endif //_VB_INPUT_DATA_VERT_
  320.  
  321. #ifndef _LIGHTING_DEFAULTS_VERT_
  322. #define _LIGHTING_DEFAULTS_VERT_
  323.  
  324. #ifndef _LIGHT_INPUT_CMN_
  325. #define _LIGHT_INPUT_CMN_
  326.  
  327. #if defined(COMPUTE_SHADER)
  328. #   define ACCESS
  329. #else
  330. #   define ACCESS readonly
  331. #endif
  332.  
  333. const int LIGHT_DIRECTIONAL = 0;
  334. const int LIGHT_OMNIDIRECTIONAL = 1;
  335. const int LIGHT_SPOT = 2;
  336.  
  337. struct Light {
  338.     /// rgb = colour
  339.     /// w = reserved
  340.     vec4   _colour;
  341.     /// xyz = light positon (or direction for Directional lights)
  342.     /// w = range
  343.     vec4   _positionWV;
  344.     /// xyz = spot direction
  345.     /// w = spot angle  
  346.     vec4   _directionWV;
  347.     /// x = light type: 0.0 - directional, 1.0  - point, 2.0 - spot
  348.     /// y = casts shadows
  349.     /// z = shadow block index
  350.     /// w = if directional light : csm split count else : reserved
  351.     ivec4  _options;
  352. };
  353.  
  354. layout(binding = BUFFER_LIGHT_NORMAL, std430) coherent ACCESS buffer dvd_LightBlock
  355. {
  356.     Light dvd_LightSource[MAX_POSSIBLE_LIGHTS];
  357. };
  358.  
  359. layout(binding = BUFFER_LIGHT_INDICES, std430) coherent ACCESS buffer perTileLightIndexBuffer
  360. {
  361.     uint perTileLightIndices[];
  362. };
  363.  
  364. #if defined(FRAG_SHADER)
  365. struct Shadow {
  366.     uvec4 _arrayOffset;
  367.     mat4  _lightVP[MAX_SPLITS_PER_LIGHT];
  368.     vec4  _lightPosition[MAX_SPLITS_PER_LIGHT];
  369.     vec4  _floatValues[MAX_SPLITS_PER_LIGHT];
  370. };
  371.  
  372. layout(binding = BUFFER_LIGHT_SHADOW, std430) coherent ACCESS buffer dvd_ShadowBlock
  373. {
  374.     Shadow dvd_ShadowSource[MAX_SHADOW_CASTING_LIGHTS];
  375. };
  376.  
  377. #endif
  378.  
  379. #define windowWidth int(dvd_ViewPort.z)
  380. #define windowHeight int(dvd_ViewPort.w)
  381.  
  382. // calculate the number of tiles in the horizontal direction
  383. uint GetNumTilesX()
  384. {
  385.     return uint((windowWidth + FORWARD_PLUS_TILE_RES - 1) / float(FORWARD_PLUS_TILE_RES));
  386. }
  387.  
  388. // calculate the number of tiles in the vertical direction
  389. uint GetNumTilesY()
  390. {
  391.     return uint((windowHeight + FORWARD_PLUS_TILE_RES - 1) / float(FORWARD_PLUS_TILE_RES));
  392. }
  393.  
  394. uint GetTileIndex(in vec2 ScreenPos)
  395. {
  396.     float fTileRes = float(FORWARD_PLUS_TILE_RES);
  397.     return uint(floor(ScreenPos.x / fTileRes) + floor(ScreenPos.y / fTileRes) * GetNumTilesX());
  398. }
  399.  
  400. #endif //_LIGHT_INPUT_CMN_
  401.  
  402.  
  403. void computeLightVectors(){
  404.     VAR._vertexWV = dvd_ViewMatrix * VAR._vertexW;
  405.     VAR._normalWV = normalize(dvd_NormalMatrixWV() * dvd_Normal);
  406. #if defined(COMPUTE_TBN)
  407.     VAR._tangentWV = normalize(dvd_NormalMatrixWV() * dvd_Tangent);
  408.     VAR._bitangentWV = normalize(cross(VAR._normalWV, VAR._tangentWV));
  409. #endif
  410. }
  411.  
  412. #endif //_LIGHTING_DEFAULTS_VERT_
  413.  
  414.  
  415.  
  416. #if defined(ADD_FOLIAGE)
  417.  
  418. #ifndef _FOLIAGE_VERT_
  419. #define _FOLIAGE_VERT_
  420.  
  421. uniform vec3  scale;
  422. uniform float grassScale;
  423. uniform float lod_metric;
  424.  
  425. void computeFoliageMovementTree(inout vec4 vertex) {
  426.  
  427.     float move_speed = (float(int(VAR._vertexW.y * VAR._vertexW.z) % 50)/50.0 + 0.5);
  428.     float timeTree = dvd_time * 0.001 * dvd_windDetails.w * move_speed; //to seconds
  429.     float amplituted = pow(vertex.y, 2.0);
  430.     vertex.x += 0.01 * amplituted * cos(timeTree + VAR._vertexW.x) *dvd_windDetails.x;
  431.     vertex.z += 0.05 *scale.y* amplituted * cos(timeTree + VAR._vertexW.z) *dvd_windDetails.z;
  432. }
  433.  
  434. void computeFoliageMovementGrass(inout vec4 vertex) {
  435.     float timeGrass = dvd_windDetails.w * dvd_time * 0.001; //to seconds
  436.     float cosX = cos(vertex.x);
  437.     float sinX = sin(vertex.x);
  438.     float halfScale = 0.5*grassScale;
  439.     vertex.x += (halfScale*cos(timeGrass) * cosX * sinX)*dvd_windDetails.x;
  440.     vertex.z += (halfScale*sin(timeGrass) * cosX * sinX)*dvd_windDetails.z;
  441. }
  442.  
  443. #endif //_FOLIAGE_VERT_
  444.  
  445. #endif
  446.  
  447. void main(void){
  448.  
  449.  
  450.  
  451.     computeData();
  452.  
  453.  
  454.  
  455. #if defined(ADD_FOLIAGE) && defined(IS_TREE)
  456.  
  457.     computeFoliageMovementTree(dvd_Vertex);
  458.  
  459. #endif
  460.  
  461.    
  462.  
  463.     computeLightVectors();
  464.  
  465.  
  466.  
  467.     //Compute the final vert position
  468.  
  469.     gl_Position = dvd_ViewProjectionMatrix * VAR._vertexW;
  470.  
  471. }
Add Comment
Please, Sign In to add comment