IonutCava

FragmentShader

Jun 17th, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 35.48 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 FRAG_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. const uint DEPTH_EXP_WARP = 32;
  29. #define USE_HIZ_CULLING
  30. #define MAX_CLIP_PLANES 6
  31. #define MAX_SHADOW_CASTING_LIGHTS 5
  32. #define MAX_LIGHT_TYPES 3
  33. #define BUFFER_GPU_BLOCK 0
  34. #define BUFFER_GPU_COMMANDS 1
  35. #define BUFFER_LIGHT_NORMAL 2
  36. #define BUFFER_LIGHT_SHADOW 3
  37. #define BUFFER_LIGHT_INDICES 4
  38. #define BUFFER_NODE_INFO 5
  39. #define BUFFER_SCENE_DATA 7
  40. #define FORWARD_PLUS_TILE_RES 16
  41. #define MAX_NUM_LIGHTS_PER_TILE 544
  42. #define LIGHT_INDEX_BUFFER_SENTINEL 2147483647
  43. #define MAX_TEXTURE_SLOTS 32
  44. #define MAX_TEXTURE_SLOTS 32
  45. #define MAX_TEXTURE_SLOTS 32
  46. #define TEXTURE_UNIT0 0
  47. #define TEXTURE_UNIT1 1
  48. #define TEXTURE_NORMALMAP 2
  49. #define TEXTURE_OPACITY 3
  50. #define TEXTURE_SPECULAR 4
  51. #define TEXTURE_PROJECTION 5
  52. #define TEXTURE_DEPTH_MAP 6
  53. #define TEXTURE_REFLECTION 7
  54. #define TEXTURE_REFRACTION 8
  55. #define SHADOW_CUBE_MAP_ARRAY 10
  56. #define SHADOW_SINGLE_MAP_ARRAY 11
  57. #define SHADOW_LAYERED_MAP_ARRAY 12
  58.  
  59. in  PerVertexData{
  60.     flat uint dvd_drawID;
  61.     vec2 _texCoord;
  62.     vec4 _vertexW;
  63.     vec4 _vertexWV;
  64.     vec3 _normalWV;
  65.     vec3 _tangentWV;
  66.     vec3 _bitangentWV;
  67. } f_in;
  68. #define VAR f_in
  69.  
  70. #ifndef _NODE_DATA_INPUT_CMN_
  71. #define _NODE_DATA_INPUT_CMN_
  72.  
  73. #if defined(COMPUTE_SHADER)
  74. struct IndirectDrawCommand {
  75.     uint count;
  76.     uint instanceCount;
  77.     uint firstIndex;
  78.     uint baseVertex;
  79.     uint baseInstance;
  80. };
  81.  
  82. layout(binding = BUFFER_GPU_COMMANDS, std430) coherent buffer dvd_GPUCmds
  83. {
  84.     IndirectDrawCommand dvd_drawCommands[MAX_VISIBLE_NODES];
  85. };
  86. #endif
  87.  
  88. layout(binding = BUFFER_GPU_BLOCK, std140) uniform dvd_GPUBlock
  89. {
  90.     mat4  dvd_ProjectionMatrix;
  91.     mat4  dvd_ViewMatrix;
  92.     mat4  dvd_ViewProjectionMatrix;
  93.     vec4  dvd_cameraPosition; ///xyz - position, w - aspect ratio
  94.     vec4  dvd_ViewPort;
  95.     vec4  dvd_ZPlanesCombined; //xy - current, zw - main scene
  96.     vec4  dvd_invScreenDimensions;
  97.     vec4  dvd_renderProperties;
  98.     vec4  dvd_frustumPlanes[6];
  99.     vec4  dvd_clip_plane[MAX_CLIP_PLANES];
  100. };
  101.  
  102. #define dvd_shadowArrayOffset int(dvd_renderProperties.x)
  103. #define dvd_GSInvocationLimit int(dvd_renderProperties.y)
  104. #define dvd_aspectRatio dvd_cameraPosition.w
  105. #define dvd_fieldOfView dvd_renderProperties.z
  106. #define dvd_tanHalfFieldOfView dvd_renderProperties.w
  107.  
  108. layout(binding = BUFFER_SCENE_DATA, std140) uniform dvd_SceneData
  109. {
  110.     vec4   dvd_fogDetails;
  111.     vec4   dvd_windDetails;
  112.     vec4   dvd_shadowingSettings;
  113.     vec4   dvd_otherData;
  114.     vec4   dvd_otherData2;
  115.     uvec4  dvd_lightCountPerType;
  116.     //uint  dvd_lightCountPerType[MAX_LIGHT_TYPES];
  117. };
  118.  
  119. #define dvd_fogColour dvd_fogDetails.xyz
  120. #define dvd_fogDensity dvd_fogDetails.w
  121. #define dvd_time int(dvd_otherData.x)
  122. #define dvd_showDebugInfo int(dvd_otherData.y)
  123.  
  124. int dvd_deltaTime() {
  125.     return int(dvd_otherData2.x);
  126. }
  127.  
  128. bool dvd_shadowsEnabled() {
  129.     return int(dvd_otherData.z) == 1;
  130. }
  131.  
  132. #if defined(VERT_SHADER)
  133.  
  134. invariant gl_Position;
  135. //out float gl_ClipDistance[MAX_CLIP_PLANES];
  136.  
  137. void setClipPlanes(in vec4 worldSpaceVertex) {
  138.    
  139. #if MAX_CLIP_PLANES > 0
  140.     gl_ClipDistance[0] = dot(worldSpaceVertex, dvd_clip_plane[0]);
  141. #   if MAX_CLIP_PLANES > 1
  142.         gl_ClipDistance[1] = dot(worldSpaceVertex, dvd_clip_plane[1]);
  143. #       if MAX_CLIP_PLANES > 2
  144.             gl_ClipDistance[2] = dot(worldSpaceVertex, dvd_clip_plane[2]);
  145. #           if MAX_CLIP_PLANES > 3
  146.                 gl_ClipDistance[3] = dot(worldSpaceVertex, dvd_clip_plane[3]);
  147. #               if MAX_CLIP_PLANES > 4
  148.                     gl_ClipDistance[4] = dot(worldSpaceVertex, dvd_clip_plane[4]);
  149. #                   if MAX_CLIP_PLANES > 5
  150.                         gl_ClipDistance[5] = dot(worldSpaceVertex, dvd_clip_plane[5]);
  151. #                   endif
  152. #               endif
  153. #           endif
  154. #       endif
  155. #   endif
  156. #endif
  157. }
  158. #endif
  159.  
  160. //mix(false, true, condition);
  161.  
  162. #endif //_NODE_DATA_INPUT_CMN_
  163.  
  164. #line 127
  165. #if !defined(HAS_TRANSPARENCY)
  166.  
  167. layout(early_fragment_tests) in;
  168.  
  169. #endif
  170.  
  171.  
  172.  
  173. #ifndef _BRDF_FRAG_
  174. #define _BRDF_FRAG_
  175.  
  176. #ifndef _LIGHTING_DEFAULTS_FRAG_
  177. #define _LIGHTING_DEFAULTS_FRAG_
  178.  
  179. #ifndef _NODE_BUFFERED_INPUT_CMN_
  180. #define _NODE_BUFFERED_INPUT_CMN_
  181.  
  182. struct NodeData {
  183.     mat4 _worldMatrix;
  184.     // [0][0] ... [2][2] = normal matrix
  185.     // [0][3] = bone count
  186.     // [3][0] ... [3][3] = bounding sphere
  187.     mat4 _normalMatrixWV;
  188.     // [0][0] ... [0][3] = albedo
  189.     // [1][0] ... [1][3] = specular
  190.     // [2][0] ... [2][3] = emissive
  191.     // [3][0] = is translucent
  192.     // [3][1] = texture operation
  193.     // [3][2] = texture count
  194.     // [3][3] = parallax factor
  195.     mat4 _colourMatrix;
  196.     // x = selection flag
  197.     // y = is shadow received
  198.     // z = lod level
  199.     // w = reserved
  200.     vec4 _properties;
  201. };
  202.  
  203. layout(binding = BUFFER_NODE_INFO, std430) coherent readonly buffer dvd_MatrixBlock
  204. {
  205.     NodeData dvd_Matrices[MAX_VISIBLE_NODES];
  206. };
  207.  
  208. mat4 dvd_WorldMatrix() {
  209.     return dvd_Matrices[VAR.dvd_drawID]._worldMatrix;
  210. }
  211.  
  212. mat3 dvd_NormalMatrixWV() {
  213.     return mat3(dvd_Matrices[VAR.dvd_drawID]._normalMatrixWV);
  214. }
  215.  
  216. ivec4 dvd_BufferIntegerValues() {
  217.     return ivec4(dvd_Matrices[VAR.dvd_drawID]._properties);
  218. }
  219.  
  220. #if defined(VERT_SHADER)
  221. #define dvd_boneCount     uint(dvd_Matrices[VAR.dvd_drawID]._normalMatrixWV[0][3])
  222. #endif
  223.  
  224. // x - isSelected/isHighlighted; y - isShadowMapped; z - lodLevel, w - occlusion cull data
  225. #define dvd_lodLevel dvd_BufferIntegerValues().z
  226. #define dvd_customData dvd_Matrices[VAR.dvd_drawID]._properties.w
  227.  
  228. #if defined(FRAG_SHADER)
  229. // x - useAlphaTest; y - textureOperation; z - textureCount, w - parallax/relief mapping factor
  230. #define buffer_matProperties dvd_Matrices[VAR.dvd_drawID]._colourMatrix[3]
  231.  
  232. #define dvd_isSelected     (dvd_BufferIntegerValues().x < -0.5)
  233. #define dvd_isHighlighted  (dvd_BufferIntegerValues().x >  0.5)
  234. #define dvd_shadowMapping  (dvd_BufferIntegerValues().y > 0.0)
  235. #define dvd_useAlphaTest   (buffer_matProperties.x > 0.0)
  236. #define dvd_texOperation   uint(buffer_matProperties.y)
  237. #define dvd_textureCount   uint(buffer_matProperties.z)
  238. #define dvd_parallaxFactor buffer_matProperties.w
  239. #define dvd_reliefFactor   dvd_parallaxFactor
  240.  
  241. #endif
  242.  
  243. #endif //_NODE_BUFFERED_INPUT_CMN_
  244.  
  245.  
  246. uniform float projectedTextureMixWeight;
  247. layout(binding = TEXTURE_PROJECTION) uniform sampler2D texDiffuseProjected;
  248.  
  249. #define PRECISION 0.000001
  250.  
  251. float DIST_TO_ZERO(float val) {
  252.     return 1.0 - (step(-PRECISION, val) * (1.0 - step(PRECISION, val)));
  253. }
  254.  
  255. float saturate(float v) { return clamp(v, 0.0, 1.0); }
  256. vec3  saturate(vec3 v)  { return clamp(v, 0.0, 1.0); }
  257. vec4  saturate(vec4 v)  { return clamp(v, 0.0, 1.0); }
  258.  
  259. void projectTexture(in vec3 PoxPosInMap, inout vec4 targetTexture){
  260.     vec4 projectedTex = texture(texDiffuseProjected, vec2(PoxPosInMap.s, 1.0-PoxPosInMap.t));
  261.     targetTexture.xyz = mix(targetTexture.xyz, projectedTex.xyz, projectedTextureMixWeight);
  262. }
  263.  
  264. vec3 applyFogColour(in vec3 colour){
  265.     const float LOG2 = 1.442695;
  266.     float zDepth = gl_FragCoord.z / gl_FragCoord.w;
  267.     return mix(dvd_fogColour, colour, clamp(exp2(-dvd_fogDensity * dvd_fogDensity * zDepth * zDepth * LOG2), 0.0, 1.0));
  268. }
  269.  
  270. vec4 applyFog(in vec4 colour) {
  271.     return vec4(mix(applyFogColour(colour.rgb), colour.rgb, dvd_fogDensity), colour.a);
  272. }
  273.  
  274. float ToLinearDepth(in float depthIn) {
  275. #if defined(USE_SCENE_ZPLANES)
  276.     float n = dvd_ZPlanesCombined.z;
  277.     float f = dvd_ZPlanesCombined.w * 0.5;
  278. #else
  279.     float n = dvd_ZPlanesCombined.x;
  280.     float f = dvd_ZPlanesCombined.y * 0.5;
  281. #endif
  282.  
  283.     return (2 * n) / (f + n - (depthIn) * (f - n));
  284. }
  285.  
  286. float ToLinearDepth(in float depthIn, in mat4 projMatrix) {
  287.     return projMatrix[3][2] / (depthIn - projMatrix[2][2]);
  288. }
  289.  
  290. bool InRangeExclusive(in float value, in float min, in float max) {
  291.     return value > min && value < max;
  292. }
  293.  
  294. vec4 positionFromDepth(in float depth,
  295.                        in mat4 invProjectionMatrix,
  296.                        in vec2 uv) {
  297.  
  298.     vec4 pos = vec4(2.0 * uv.x - 1.0,
  299.                     2.0 * uv.y - 1.0,
  300.                     2.0 * depth - 1.0,
  301.                     1.0);
  302.  
  303.     pos = invProjectionMatrix * pos;
  304.     pos /= pos.w;
  305.  
  306.     return pos;
  307. }
  308.  
  309. float luminance(in vec3 rgb) {
  310.     const vec3 kLum = vec3(0.299, 0.587, 0.114);
  311.     return max(dot(rgb, kLum), 0.0001); // prevent zero result
  312. }
  313.  
  314. const float gamma = 2.2;
  315. const float invGamma = 1.0 / gamma;
  316. const vec3 invGammaVec = vec3(invGamma);
  317. const vec3 gammaVec = vec3(gamma);
  318.  
  319. float ToLinear(float v) { return pow(v, gamma); }
  320. vec3  ToLinear(vec3 v)  { return pow(v, gammaVec); }
  321. vec4  ToLinear(vec4 v)  { return vec4(pow(v.rgb, gammaVec), v.a); }
  322.  
  323. float ToSRGB(float v) { return pow(v, invGamma); }
  324. vec3  ToSRGB(vec3 v)  { return pow(v, invGammaVec); }
  325. vec4  ToSRGB(vec4 v)  { return vec4(pow(v.rgb, invGammaVec), v.a);}
  326.  
  327.  
  328. float Gloss(vec3 bump)
  329. {
  330.     float gloss = 1.0;
  331.  
  332.     /*if (do_toksvig)
  333.     {
  334.         // Compute the "Toksvig Factor"
  335.         float rlen = 1.0 / saturate(length(bump));
  336.         gloss = 1.0 / (1.0 + power*(rlen - 1.0));
  337.     }
  338.  
  339.     if (do_toksmap)
  340.     {
  341.         // Fetch pre-computed "Toksvig Factor"
  342.         // and adjust for specified power
  343.         gloss = texture2D(gloss_map, texcoord).r;
  344.         gloss /= mix(power / baked_power, 1.0, gloss);
  345.     }
  346.     */
  347.     return gloss;
  348. }
  349.  
  350. #endif //_LIGHTING_DEFAULTS_FRAG_
  351.  
  352. #ifndef _LIGHT_INPUT_CMN_
  353. #define _LIGHT_INPUT_CMN_
  354.  
  355. #if defined(COMPUTE_SHADER)
  356. #   define ACCESS
  357. #else
  358. #   define ACCESS readonly
  359. #endif
  360.  
  361. const int LIGHT_DIRECTIONAL = 0;
  362. const int LIGHT_OMNIDIRECTIONAL = 1;
  363. const int LIGHT_SPOT = 2;
  364.  
  365. struct Light {
  366.     /// rgb = colour
  367.     /// w = reserved
  368.     vec4   _colour;
  369.     /// xyz = light positon (or direction for Directional lights)
  370.     /// w = range
  371.     vec4   _positionWV;
  372.     /// xyz = spot direction
  373.     /// w = spot angle  
  374.     vec4   _directionWV;
  375.     /// x = light type: 0.0 - directional, 1.0  - point, 2.0 - spot
  376.     /// y = casts shadows
  377.     /// z = shadow block index
  378.     /// w = if directional light : csm split count else : reserved
  379.     ivec4  _options;
  380. };
  381.  
  382. layout(binding = BUFFER_LIGHT_NORMAL, std430) coherent ACCESS buffer dvd_LightBlock
  383. {
  384.     Light dvd_LightSource[MAX_POSSIBLE_LIGHTS];
  385. };
  386.  
  387. layout(binding = BUFFER_LIGHT_INDICES, std430) coherent ACCESS buffer perTileLightIndexBuffer
  388. {
  389.     uint perTileLightIndices[];
  390. };
  391.  
  392. #if defined(FRAG_SHADER)
  393. struct Shadow {
  394.     uvec4 _arrayOffset;
  395.     mat4  _lightVP[MAX_SPLITS_PER_LIGHT];
  396.     vec4  _lightPosition[MAX_SPLITS_PER_LIGHT];
  397.     vec4  _floatValues[MAX_SPLITS_PER_LIGHT];
  398. };
  399.  
  400. layout(binding = BUFFER_LIGHT_SHADOW, std430) coherent ACCESS buffer dvd_ShadowBlock
  401. {
  402.     Shadow dvd_ShadowSource[MAX_SHADOW_CASTING_LIGHTS];
  403. };
  404.  
  405. #endif
  406.  
  407. #define windowWidth int(dvd_ViewPort.z)
  408. #define windowHeight int(dvd_ViewPort.w)
  409.  
  410. // calculate the number of tiles in the horizontal direction
  411. uint GetNumTilesX()
  412. {
  413.     return uint((windowWidth + FORWARD_PLUS_TILE_RES - 1) / float(FORWARD_PLUS_TILE_RES));
  414. }
  415.  
  416. // calculate the number of tiles in the vertical direction
  417. uint GetNumTilesY()
  418. {
  419.     return uint((windowHeight + FORWARD_PLUS_TILE_RES - 1) / float(FORWARD_PLUS_TILE_RES));
  420. }
  421.  
  422. uint GetTileIndex(in vec2 ScreenPos)
  423. {
  424.     float fTileRes = float(FORWARD_PLUS_TILE_RES);
  425.     return uint(floor(ScreenPos.x / fTileRes) + floor(ScreenPos.y / fTileRes) * GetNumTilesX());
  426. }
  427.  
  428. #endif //_LIGHT_INPUT_CMN_
  429.  
  430. #ifndef _LIGHT_DATA_FRAG_
  431. #define _LIGHT_DATA_FRAG_
  432.  
  433. #define M_PIDIV180 0.01745329251994329576923690768488
  434. // Light direction is NOT normalized
  435. float getLightAttenuation(const in int lightIndex,
  436.                           const in vec3 lightDirection){
  437.     int lightType = dvd_LightSource[lightIndex]._options.x;
  438.     if (lightType == LIGHT_DIRECTIONAL) {
  439.         return 1.0;
  440.     }
  441.  
  442.     float dis = length(lightDirection);
  443.     float r = dvd_LightSource[lightIndex]._positionWV.w;
  444.     float att = clamp(1.0 - (dis*dis) / (r*r), 0.0, 1.0);
  445.     att *= att;
  446.  
  447.     if (lightType == LIGHT_SPOT) {
  448.         vec4 dirIn = dvd_LightSource[lightIndex]._directionWV;
  449.         float cosOutterConeAngle = dvd_LightSource[lightIndex]._colour.w;
  450.         float cosInnerMinusOuterAngle = dirIn.w - cosOutterConeAngle;
  451.         att *= clamp((dot(-normalize(lightDirection), normalize(dirIn.xyz)) - cosOutterConeAngle) /
  452.                      cosInnerMinusOuterAngle, 0.0, 1.0);
  453.     }
  454.     return att;
  455. }
  456.  
  457. //non-normalized light direction!
  458. vec3 getLightDirection(const in int lightIndex) {
  459.     if (dvd_LightSource[lightIndex]._options.x == LIGHT_DIRECTIONAL) {
  460.         return -dvd_LightSource[lightIndex]._positionWV.xyz;
  461.     }
  462.      
  463.     return dvd_LightSource[lightIndex]._positionWV.xyz - VAR._vertexWV.xyz;
  464.  
  465. }
  466.  
  467. #endif
  468.  
  469. #ifndef _MATERIAL_DATA_FRAG_
  470. #define _MATERIAL_DATA_FRAG_
  471.  
  472. #if defined(USE_OPACITY_DIFFUSE) || defined(USE_OPACITY_MAP)
  473.     #define HAS_TRANSPARENCY
  474. #endif
  475.  
  476. #if !defined(SKIP_TEXTURES)
  477. layout(binding = TEXTURE_UNIT0) uniform sampler2D texDiffuse0;
  478. layout(binding = TEXTURE_UNIT1) uniform sampler2D texDiffuse1;
  479. #endif
  480.  
  481. layout(binding = TEXTURE_REFLECTION) uniform samplerCubeArray texEnvironmentCube;
  482.  
  483. //Normal or BumpMap
  484. #if defined(COMPUTE_TBN)
  485. layout(binding = TEXTURE_NORMALMAP) uniform sampler2D texNormalMap;
  486. #endif
  487.  
  488. //Specular and opacity maps are available even for non-textured geometry
  489. #if defined(USE_OPACITY_MAP)
  490. layout(binding = TEXTURE_OPACITY) uniform sampler2D texOpacityMap;
  491. #endif
  492.  
  493. #if defined(USE_SPECULAR_MAP)
  494. layout(binding = TEXTURE_SPECULAR) uniform sampler2D texSpecularMap;
  495. #endif
  496.  
  497.  
  498.  
  499. float Gloss(in vec3 bump, in vec2 texCoord)
  500. {
  501.     #if defined(USE_TOKSVIG)
  502.         // Compute the "Toksvig Factor"
  503.         float rlen = 1.0/saturate(length(bump));
  504.         return 1.0/(1.0 + power*(rlen - 1.0));
  505.     #elif defined(USE_TOKSVIG_MAP)
  506.         float baked_power = 100.0;
  507.         // Fetch pre-computed "Toksvig Factor"
  508.         // and adjust for specified power
  509.         float gloss = texture2D(texSpecularMap, texCoord).r;
  510.         gloss /= mix(power/baked_power, 1.0, gloss);
  511.         return gloss;
  512.     #else
  513.         return 1.0;
  514.     #endif
  515. }
  516.  
  517. #if !defined(SKIP_TEXTURES)
  518. vec4 getTextureColour(in vec2 uv) {
  519.     #define TEX_MODULATE 0
  520.     #define TEX_ADD  1
  521.     #define TEX_SUBTRACT  2
  522.     #define TEX_DIVIDE  3
  523.     #define TEX_SMOOTH_ADD  4
  524.     #define TEX_SIGNED_ADD  5
  525.     #define TEX_DECAL  6
  526.     #define TEX_REPLACE  7
  527.  
  528.     vec4 colour = texture(texDiffuse0, uv);
  529.  
  530.     if (dvd_textureCount == 1) {
  531.         return colour;
  532.     }
  533.  
  534.     vec4 colour2 = texture(texDiffuse1, uv);
  535.  
  536.     // Read from the texture
  537.     switch (dvd_texOperation) {
  538.         default             : colour = vec4(0.7743, 0.3188, 0.5465, 1.0);   break; // <hot pink to easily spot it in a crowd
  539.         case TEX_MODULATE   : colour *= colour2;       break;
  540.         case TEX_REPLACE    : colour  = colour2;       break;
  541.         case TEX_SIGNED_ADD : colour += colour2 - 0.5; break;
  542.         case TEX_DIVIDE     : colour /= colour2;          break;
  543.         case TEX_SUBTRACT   : colour -= colour2;          break;
  544.         case TEX_DECAL      : colour = vec4(mix(colour.rgb, colour2.rgb, colour2.a), colour.a); break;
  545.         case TEX_ADD        : {
  546.             colour.rgb += colour2.rgb;
  547.             colour.a   *= colour2.a;
  548.         }break;
  549.         case TEX_SMOOTH_ADD : {
  550.             colour = (colour + colour2) - (colour * colour2);
  551.         }break;
  552.     }
  553.    
  554.  
  555.     return saturate(colour);
  556. }
  557. #endif
  558.  
  559. vec4 dvd_MatDiffuse;
  560. vec3 dvd_MatSpecular;
  561. vec3 dvd_MatEmissive;
  562. float dvd_MatShininess;
  563.  
  564. void parseMaterial() {
  565.    
  566.     dvd_MatEmissive = dvd_Matrices[VAR.dvd_drawID]._colourMatrix[2].rgb;
  567.     dvd_MatShininess = dvd_Matrices[VAR.dvd_drawID]._colourMatrix[2].w;
  568.  
  569.     #if !defined(USE_CUSTOM_ALBEDO)
  570.         #if defined(SKIP_TEXTURES)
  571.             dvd_MatDiffuse = dvd_Matrices[VAR.dvd_drawID]._colourMatrix[0];
  572.         #else
  573.             dvd_MatDiffuse = getTextureColour(VAR._texCoord);
  574.         #endif
  575.     #endif
  576.  
  577.     #if defined(USE_SPECULAR_MAP)
  578.         dvd_MatSpecular = texture(texSpecularMap, VAR._texCoord).rgb;
  579.     #else
  580.         dvd_MatSpecular = dvd_Matrices[VAR.dvd_drawID]._colourMatrix[1].rgb;
  581.     #endif
  582. }
  583.  
  584. #endif //_MATERIAL_DATA_FRAG_
  585.  
  586. #ifndef _SHADOW_MAPPING_FRAG_
  587. #define _SHADOW_MAPPING_FRAG_
  588.  
  589. layout(binding = SHADOW_SINGLE_MAP_ARRAY)  uniform sampler2DArrayShadow    texDepthMapFromLight;
  590. layout(binding = SHADOW_CUBE_MAP_ARRAY)    uniform samplerCubeArrayShadow  texDepthMapFromLightCube;
  591. layout(binding = SHADOW_LAYERED_MAP_ARRAY) uniform sampler2DArray          texDepthMapFromLightArray;
  592.  
  593.  
  594. #if defined(_DEBUG)
  595. #define DEBUG_SHADOWMAPPING
  596. //#define DEBUG_SHADOWSPLITS
  597. // set this to whatever (current cascade, current depth comparison result, anything)
  598. int _shadowTempInt = -2;
  599. #endif
  600.  
  601. #ifndef _SHADOW_DIRECTIONAL_FRAG_
  602. #define _SHADOW_DIRECTIONAL_FRAG_
  603.  
  604. #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
  605.  
  606. float linstep(float min, float max, float v) {
  607.     return clamp((v - min) / (max - min), 0.0, 1.0);
  608. }
  609.  
  610. // Reduces VSM light bleeding (remove the [0, amount] tail and linearly rescale (amount, 1])
  611. float reduceLightBleeding(float pMax, float amount) {
  612.     return linstep(amount, 1.0f, pMax);
  613. }
  614.  
  615. float chebyshevUpperBound(vec2 moments, float depth, float minVariance) {
  616.     if (depth <= moments.x + dvd_shadowingSettings.x) {
  617.         return 1.0;
  618.     }
  619.  
  620.     float variance = max(moments.y - (moments.x * moments.x), minVariance);
  621.     float d = (depth - moments.x);
  622.     float p_max = variance / (variance + d*d);
  623.     return max(1.0 - p_max, 0.0);
  624. }
  625.  
  626. float applyShadowDirectional(int shadowIndex, int splitCount, in float fragDepth) {
  627.  
  628.     Shadow currentShadowSource  = dvd_ShadowSource[shadowIndex];
  629. #   if !defined(_DEBUG)
  630.       int _shadowTempInt = -2;
  631. #   endif
  632.    
  633.     // find the appropriate depth map to look up in based on the depth of this fragment
  634.     _shadowTempInt = 0;
  635.     for (int i = 0; i < splitCount; ++i) {
  636.         if (fragDepth > currentShadowSource._floatValues[i].x)      {
  637.             _shadowTempInt = i + 1;
  638.         }
  639.     }
  640.    
  641.     // GLOBAL
  642.     const int SplitPowLookup[] = { 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  643.                                    LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
  644.                                    LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)};
  645.     // Ensure that every fragment in the quad choses the same split so that derivatives
  646.     // will be meaningful for proper texture filtering and LOD selection.
  647.     int SplitPow = 1 << _shadowTempInt;
  648.     int SplitX = int(abs(dFdx(SplitPow)));
  649.     int SplitY = int(abs(dFdy(SplitPow)));
  650.     int SplitXY = int(abs(dFdx(SplitY)));
  651.     int SplitMax = max(SplitXY, max(SplitX, SplitY));
  652.     _shadowTempInt = SplitMax > 0 ? SplitPowLookup[SplitMax - 1] : _shadowTempInt;
  653.  
  654.     if (_shadowTempInt < 0 || _shadowTempInt > splitCount) {
  655.         return 1.0;
  656.     }
  657.  
  658.     vec4 sc = currentShadowSource._lightVP[_shadowTempInt] * VAR._vertexW;
  659.     vec3 scPostW = sc.xyz / sc.w;
  660.     if (!(sc.w <= 0.0f || (scPostW.x < 0 || scPostW.y < 0) || (scPostW.x >= 1 || scPostW.y >= 1))){
  661.         float layer = float(_shadowTempInt + currentShadowSource._arrayOffset.x);
  662.  
  663.         vec2 moments = texture(texDepthMapFromLightArray, vec3(scPostW.xy, layer)).rg;
  664.        
  665.         //float shadowBias = DEPTH_EXP_WARP * exp(DEPTH_EXP_WARP * dvd_shadowingSettings.y);
  666.         //float shadowWarpedz1 = exp(scPostW.z * DEPTH_EXP_WARP);
  667.         //return mix(chebyshevUpperBound(moments, shadowWarpedz1, dvd_shadowingSettings.y),
  668.         //             1.0,
  669.         //             clamp(((gl_FragCoord.z + dvd_shadowingSettings.z) - dvd_shadowingSettings.w) / dvd_shadowingSettings.z, 0.0, 1.0));
  670.         return reduceLightBleeding(chebyshevUpperBound(moments, scPostW.z, dvd_shadowingSettings.y), 0.1);
  671.     }
  672.  
  673.     return 1.0;
  674. }
  675.  
  676. #endif //_SHADOW_DIRECTIONAL_FRAG_
  677.  
  678. #ifndef _SHADOW_POINT_FRAG_
  679. #define _SHADOW_POINT_FRAG_
  680.  
  681. float applyShadowPoint(int shadowIndex) {
  682.  
  683.     Shadow currentShadowSource = dvd_ShadowSource[shadowIndex];
  684.  
  685.     // SHADOW MAPS
  686.     vec3 position_ls = currentShadowSource._lightPosition[0].xyz;
  687.     vec3 abs_position = abs(position_ls);
  688.     float fs_z = -max(abs_position.x, max(abs_position.y, abs_position.z));
  689.     vec4 clip = (currentShadowSource._lightVP[0] * VAR._vertexW) * vec4(0.0, 0.0, fs_z, 1.0);
  690.     float depth = (clip.z / clip.w) * 0.5 + 0.5;
  691.     return texture(texDepthMapFromLightCube, vec4(position_ls.xyz, currentShadowSource._arrayOffset.x), depth).r;
  692. }
  693.  
  694. #endif //_SHADOW_POINT_FRAG_
  695.  
  696. #ifndef _SHADOW_SPOT_FRAG_
  697. #define _SHADOW_SPOT_FRAG_
  698.  
  699. float filterFinalShadow(in sampler2DArrayShadow depthMap, in vec4 vPosInDM){
  700.     // Gaussian 3x3 filter
  701.     float vDepthMapColour = texture(depthMap, vPosInDM);
  702.  
  703.     float fShadow = 0.0;
  704.     if((vDepthMapColour+Z_TEST_SIGMA) < vPosInDM.w){
  705. #if GPU_VENDOR == GPU_VENDOR_AMD
  706.         fShadow = vDepthMapColour;
  707. #else
  708.         fShadow = vDepthMapColour * 0.25;
  709.         fShadow += textureOffset(depthMap, vPosInDM, ivec2( -1, -1)) * 0.0625;
  710.         fShadow += textureOffset(depthMap, vPosInDM, ivec2( -1,  0)) * 0.125;
  711.         fShadow += textureOffset(depthMap, vPosInDM, ivec2( -1,  1)) * 0.0625;
  712.         fShadow += textureOffset(depthMap, vPosInDM, ivec2(  0, -1)) * 0.125;
  713.         fShadow += textureOffset(depthMap, vPosInDM, ivec2(  0,  1)) * 0.125;
  714.         fShadow += textureOffset(depthMap, vPosInDM, ivec2(  1, -1)) * 0.0625;
  715.         fShadow += textureOffset(depthMap, vPosInDM, ivec2(  1,  0)) * 0.125;
  716.         fShadow += textureOffset(depthMap, vPosInDM, ivec2(  1,  1)) * 0.0625;
  717. #endif
  718.         fShadow = clamp(fShadow, 0.0, 1.0);
  719.     }else{
  720.         fShadow = 1.0;
  721.     }
  722.  
  723.     return fShadow;
  724. }
  725.  
  726. float applyShadowSpot(int shadowIndex) {
  727.     Shadow currentShadowSource = dvd_ShadowSource[shadowIndex];
  728.  
  729.     vec4 shadow_coord = currentShadowSource._lightVP[0] * VAR._vertexW;
  730.     shadow_coord = shadow_coord / shadow_coord.w;
  731.     shadow_coord = 0.5 + shadow_coord * 0.5;
  732.     shadow_coord.xyw = shadow_coord.xyz;
  733.     shadow_coord.z = currentShadowSource._arrayOffset.x;
  734.  
  735.     return filterFinalShadow(texDepthMapFromLight, shadow_coord);
  736. }
  737.  
  738. #endif //_SHADOW_SPOT_FRAG_
  739.  
  740.  
  741. float getShadowFactor(in int lightIndex, in float fragDepth) {
  742.     Light light = dvd_LightSource[lightIndex];
  743.  
  744.     switch (light._options.x) {
  745.         case LIGHT_DIRECTIONAL     : return applyShadowDirectional(light._options.z, light._options.w, fragDepth);
  746.         //case LIGHT_OMNIDIRECTIONAL : return applyShadowPoint(light._options.z);
  747.         //case LIGHT_SPOT            : return applyShadowSpot(light._options.z);
  748.     }
  749.  
  750.     return 1.0;
  751. }
  752.  
  753. float shadow_loop(){
  754.     if (!dvd_shadowsEnabled()) {
  755.         return 1.0;
  756.     }
  757.  
  758.     float fragDepth = gl_FragCoord.z;
  759.  
  760.     float shadow = 1.0;
  761.     int offset = 0;
  762.     int shadowLights = 0;
  763.     for (int i = 0; i < MAX_LIGHT_TYPES; ++i) {
  764.         for (int j = 0; j < int(dvd_lightCountPerType[i]); ++j) {
  765.             int lightIndex = j + offset;
  766.             if (shadowLights < MAX_SHADOW_CASTING_LIGHTS &&
  767.                 dvd_LightSource[lightIndex]._options.y == 1) {
  768.                 shadow *= getShadowFactor(lightIndex, fragDepth);
  769.                 shadowLights++;
  770.             }
  771.         }
  772.         offset += int(dvd_lightCountPerType[i]);
  773.     }
  774.  
  775.     return saturate(shadow);
  776. }
  777. #endif //_SHADOW_MAPPING_FRAG_
  778.  
  779. #ifndef _PHONG_LIGHTING_FRAG_
  780. #define _PHONG_LIGHTING_FRAG_
  781.  
  782. void Phong(in int lightIndex, in vec3 normalWV, inout vec3 colourInOut) {
  783.     // direction is NOT normalized
  784.     vec3 lightDirection = getLightDirection(lightIndex);
  785.     float att = getLightAttenuation(lightIndex, lightDirection);
  786.  
  787.     vec3 lightColour = dvd_LightSource[lightIndex]._colour.rgb;
  788.  
  789.     float NDotL = max(dot(normalize(lightDirection), normalWV), 0.0);
  790.     colourInOut += clamp(lightColour * dvd_MatDiffuse.rgb * NDotL * att, 0.0, 1.0);
  791.  
  792.     if (NDotL > 0.0) {
  793.         vec3 dvd_ViewDirNorm = normalize(-VAR._vertexWV.xyz);
  794. #if defined(USE_SHADING_BLINN_PHONG)
  795.         vec3 halfDir = normalize(normalize(lightDirection) + dvd_ViewDirNorm);
  796.         float specAngle = max(dot(halfDir, normalWV), 0.0);
  797. #else
  798.         vec3 reflectDir = reflect(normalize(-lightDirection), normalWV);
  799.         float specAngle = max(dot(reflectDir, dvd_ViewDirNorm), 0.0);
  800. #endif
  801.         float shininess = pow(specAngle, dvd_MatShininess) * att;
  802.         colourInOut += clamp(lightColour * dvd_MatSpecular.rgb * shininess, 0.0, 1.0);
  803.     }
  804. }
  805.  
  806.  
  807. #endif //_PHONG_LIGHTING_FRAG_
  808.  
  809.  
  810. vec3 processedNormal = vec3(0.0, 0.0, 1.0);
  811.  
  812. //TEMP PBR
  813. /// Smith GGX Visibility
  814. ///     nDotL: dot-prod of surface normal and light direction
  815. ///     nDotV: dot-prod of surface normal and view direction
  816. ///     roughness: surface roughness
  817. float SmithGGXVisibility(in float nDotL, in float nDotV, in float roughness)
  818. {
  819.     float rough2 = roughness * roughness;
  820.     float gSmithV = nDotV + sqrt(nDotV * (nDotV - nDotV * rough2) + rough2);
  821.     float gSmithL = nDotL + sqrt(nDotL * (nDotL - nDotL * rough2) + rough2);
  822.     return 1.0 / (gSmithV * gSmithL);
  823. }
  824.  
  825.  
  826. float SchlickG1(in float factor, in float rough2)
  827. {
  828.     return 1.0 / (factor * (1.0 - rough2) + rough2);
  829. }
  830.  
  831. /// Schlick approximation of Smith GGX
  832. ///     nDotL: dot product of surface normal and light direction
  833. ///     nDotV: dot product of surface normal and view direction
  834. ///     roughness: surface roughness
  835. float SchlickVisibility(float nDotL, float nDotV, float roughness)
  836. {
  837.     const float rough2 = roughness * roughness;
  838.     return (SchlickG1(nDotL, rough2) * SchlickG1(nDotV, rough2)) * 0.25;
  839. }
  840.  
  841. void PBR(in int lightIndex, in vec3 normalWV, inout vec4 colourInOut) {
  842.     vec3 lightDirection = getLightDirection(lightIndex);
  843.     vec3 lightDir = normalize(lightDirection);
  844.     float roughness = dvd_MatShininess;
  845.  
  846.  
  847.     colourInOut = vec4(1.0);
  848. }
  849.  
  850. //TEMP PBR
  851. void getBRDFFactors(in int lightIndex,
  852.                     in vec3 normalWV,
  853.                     inout vec3 colourInOut)
  854. {
  855. #if defined(USE_SHADING_PHONG) || defined (USE_SHADING_BLINN_PHONG)
  856.     Phong(lightIndex, normalWV, colourInOut);
  857. #elif defined(USE_SHADING_TOON)
  858. #elif defined(USE_SHADING_OREN_NAYAR)
  859. #else //if defined(USE_SHADING_COOK_TORRANCE)
  860.     PBR(lightIndex, normalWV, colourInOut);
  861. #endif
  862. }
  863.  
  864. uint GetNumLightsInThisTile(uint nTileIndex)
  865. {
  866.     uint nNumLightsInThisTile = 0;
  867.     uint nIndex = uint(dvd_otherData.w) * nTileIndex;
  868.     uint nNextLightIndex = perTileLightIndices[nIndex];
  869.     while (nNextLightIndex != LIGHT_INDEX_BUFFER_SENTINEL)
  870.     {
  871.         nIndex++;
  872.         nNextLightIndex = perTileLightIndices[nIndex];
  873.         nNumLightsInThisTile++;
  874.     }
  875.  
  876.     return nNumLightsInThisTile;
  877. }
  878.  
  879. vec4 getPixelColour(const in vec2 texCoord, in vec3 normalWV) {
  880.     //Occlusion culling visibility debug code
  881. #if defined(USE_HIZ_CULLING) && defined(DEBUG_HIZ_CULLING)
  882.     if (dvd_customData > 2.0) {
  883.         return vec4(1.0, 0.0, 0.0, 1.0);
  884.     }
  885. #endif
  886.  
  887.     parseMaterial();
  888.  
  889.     processedNormal = normalWV;
  890.  
  891. #if defined(HAS_TRANSPARENCY)
  892. #   if defined(USE_OPACITY_DIFFUSE)
  893.         float alpha = dvd_MatDiffuse.a;
  894. #   endif
  895. #   if defined(USE_OPACITY_MAP)
  896.         vec4 opacityMap = texture(texOpacityMap, texCoord);
  897.         float alpha = max(min(opacityMap.r, opacityMap.g), min(opacityMap.b, opacityMap.a));
  898. #   endif
  899. #   else
  900.         const float alpha = 1.0;
  901. #   endif
  902.  
  903. #   if defined (USE_DOUBLE_SIDED)
  904.         processedNormal = gl_FrontFacing ? processedNormal : -processedNormal;
  905. #   endif
  906.  
  907. #   if defined(USE_SHADING_FLAT)
  908.         vec3 colour = dvd_MatDiffuse.rgb;
  909. #   else
  910.     vec3 lightColour = vec3(0.0);
  911.     // Apply all lighting contributions
  912.     uint lightIdx;
  913.     // Directional lights
  914.     for (lightIdx = 0; lightIdx < dvd_lightCountPerType[0]; ++lightIdx) {
  915.         getBRDFFactors(int(lightIdx), processedNormal, lightColour);
  916.     }
  917.  
  918.     uint offset = dvd_lightCountPerType[0];
  919.     // Point lights
  920.     uint nIndex = uint(dvd_otherData.w) * GetTileIndex(gl_FragCoord.xy);
  921.     uint nNextLightIndex = perTileLightIndices[nIndex];
  922.     while (nNextLightIndex != LIGHT_INDEX_BUFFER_SENTINEL)
  923.     {
  924.         uint nLightIndex = nNextLightIndex;
  925.         nNextLightIndex = perTileLightIndices[++nIndex];
  926.  
  927.         getBRDFFactors(int(nLightIndex - 1 + offset), processedNormal, lightColour);
  928.     }
  929.  
  930.     offset = dvd_lightCountPerType[1];
  931.     // Spot lights
  932.     // Moves past the first sentinel to get to the spot lights.
  933.     nNextLightIndex = perTileLightIndices[++nIndex];
  934.     while (nNextLightIndex != LIGHT_INDEX_BUFFER_SENTINEL)
  935.     {
  936.         uint nLightIndex = nNextLightIndex;
  937.         nNextLightIndex = perTileLightIndices[++nIndex];
  938.         getBRDFFactors(int(nLightIndex - 1 + offset), processedNormal, lightColour);
  939.     }
  940.    
  941.     vec3 colour = mix(dvd_MatEmissive, lightColour, DIST_TO_ZERO(length(lightColour)));
  942. #endif
  943.  
  944.     float reflectance = saturate(dvd_MatShininess / 255.0);
  945.     if (reflectance > 0.75 && dvd_lodLevel < 1) {
  946.         vec3 reflectDirection = reflect(normalize(VAR._vertexWV.xyz), processedNormal);
  947.         reflectDirection = vec3(inverse(dvd_ViewMatrix) * vec4(reflectDirection, 0.0));
  948.         colour = mix(texture(texEnvironmentCube, vec4(reflectDirection, 0.0)).rgb,
  949.                     colour,
  950.                     vec3(reflectance));
  951.  
  952.     }
  953.  
  954.     colour *= mix(mix(1.0, 2.0, dvd_isHighlighted), 3.0, dvd_isSelected);
  955.     // Apply shadowing
  956.     colour *= mix(1.0, shadow_loop(), dvd_shadowMapping);
  957.  
  958. #if defined(_DEBUG) && defined(DEBUG_SHADOWMAPPING)
  959.     if (dvd_showDebugInfo == 1) {
  960.         switch (_shadowTempInt){
  961. #if defined(DEBUG_SHADOWSPLITS)
  962.             case -1: colour = vec3(1.0); break;
  963.             case  0: colour = vec3(1.0, 0.0, 0.0); break;
  964.             case  1: colour = vec3(0.0, 1.0, 0.0); break;
  965.             case  2: colour = vec3(0.0, 0.0, 1.0); break;
  966.             case  3: colour = vec3(1.0, 0.25, 0.40); break;
  967. #else
  968.             case -1: colour = vec3(1.0); break;
  969.             case  0: colour.r += 0.15; break;
  970.             case  1: colour.g += 0.25; break;
  971.             case  2: colour.b += 0.40; break;
  972.             case  3: colour += vec3(0.15, 0.25, 0.40); break;
  973. #endif
  974.         };
  975.     }
  976. #endif
  977.     return vec4(colour, alpha);
  978. }
  979.  
  980. #endif
  981.  
  982.  
  983.  
  984. #if defined(COMPUTE_TBN)
  985.  
  986. #ifndef _BUMP_MAPPING_FRAG_
  987. #define _BUMP_MAPPING_FRAG_
  988.  
  989. uniform int bumpMapLightID = 0;
  990.  
  991. vec3 getTBNNormal(in vec3 bump) {
  992.     mat3 TBN = mat3(VAR._tangentWV, VAR._bitangentWV, VAR._normalWV);
  993.     return TBN * bump;
  994. }
  995.  
  996. vec3 getBump(in vec2 uv) {
  997.     return normalize(2.0 * texture(texNormalMap, uv).rgb - 1.0);
  998. }
  999.  
  1000. float ReliefMapping_RayIntersection(in vec2 A, in vec2 AB){
  1001.     const int num_steps_lin = 10;
  1002.     const int num_steps_bin = 15;
  1003.     float linear_step = 1.0 / (float(num_steps_lin));
  1004.     //Current depth position
  1005.     float depth = 0.0;
  1006.     //Best match found (starts with last position 1.0)
  1007.     float best_depth = 1.0;
  1008.     float step = linear_step;
  1009.     //Search from front to back for first point inside the object
  1010.     for(int i=0; i<num_steps_lin-1; i++){
  1011.         depth += step;
  1012.         if (depth >= 1.0 - texture(texNormalMap, A+AB*depth).a) {
  1013.             best_depth = depth; //Store best depth
  1014.             i = num_steps_lin-1;
  1015.         }
  1016.     }
  1017.     //The point of intersection is found between (depth) and (depth-step)
  1018.     //so start from (depth - step/2)
  1019.     step = linear_step * 0.5;
  1020.     depth = best_depth - step;
  1021.     // binary search
  1022.     for(int i = 0; i < num_steps_bin; ++i){
  1023.         step *= 0.5;
  1024.         best_depth = depth;
  1025.         if (depth >= 1.0 - texture(texNormalMap, A + AB * depth).a) {
  1026.             depth -= step;
  1027.         }else {
  1028.             depth += step;
  1029.         }
  1030.     }
  1031.     return best_depth;
  1032. }
  1033.  
  1034. vec4 ParallaxMapping(in int bumpMapLightID, in vec2 uv){
  1035.     vec3 lightVecTBN = vec3(0.0);
  1036.     switch (dvd_LightSource[bumpMapLightID]._options.x){
  1037.         case LIGHT_DIRECTIONAL      :
  1038.             lightVecTBN = -normalize(dvd_LightSource[bumpMapLightID]._positionWV.xyz);
  1039.             break;
  1040.         case LIGHT_OMNIDIRECTIONAL  :
  1041.         case LIGHT_SPOT             :
  1042.             lightVecTBN = normalize(-VAR._vertexWV.xyz + dvd_LightSource[bumpMapLightID]._positionWV.xyz);
  1043.             break;
  1044.     };
  1045.  
  1046.     vec3 viewVecTBN = normalize(-VAR._vertexWV.xyz);
  1047.    
  1048.     //Offset, scale and bias
  1049.     vec2 vTexCoord = uv +
  1050.                     ((texture(texNormalMap, uv).a - 0.5) *
  1051.                      dvd_parallaxFactor *
  1052.                      (vec2(viewVecTBN.x, -viewVecTBN.y) /
  1053.                      viewVecTBN.z));
  1054.  
  1055.    
  1056.     return getPixelColour(vTexCoord, getTBNNormal(getBump(vTexCoord)));
  1057. }
  1058.  
  1059. vec4 ReliefMapping(in int _light, in vec2 uv){
  1060.     vec3 viewVecTBN = normalize(-VAR._vertexWV.xyz);
  1061.     //Size and search starting position in texture space
  1062.     vec2 AB = dvd_reliefFactor * vec2(-viewVecTBN.x, viewVecTBN.y)/viewVecTBN.z;
  1063.  
  1064.     float h = ReliefMapping_RayIntersection(uv, AB);
  1065.    
  1066.     vec2 uv_offset = h * AB;
  1067.    
  1068.     vec3 p = VAR._vertexWV.xyz;
  1069.     vec3 v = normalize(p);
  1070.     //Compute light direction
  1071.     p += v*h*viewVecTBN.z;    
  1072.    
  1073.     vec2 planes;
  1074.     planes.x = -dvd_ZPlanesCombined.y / (dvd_ZPlanesCombined.y - dvd_ZPlanesCombined.x);
  1075.     planes.y = -dvd_ZPlanesCombined.y * dvd_ZPlanesCombined.x / (dvd_ZPlanesCombined.y - dvd_ZPlanesCombined.x);
  1076.  
  1077.     gl_FragDepth =((planes.x * p.z + planes.y) / -p.z);
  1078.    
  1079.     return getPixelColour(uv + uv_offset, getTBNNormal(getBump(uv + uv_offset)));
  1080. }
  1081.  
  1082. #endif //_BUMP_MAPPING_FRAG_
  1083.  
  1084. #endif
  1085.  
  1086.  
  1087.  
  1088. layout(location = 0) out vec4 _colourOut;
  1089.  
  1090. layout(location = 1) out vec3 _normalOut;
  1091.  
  1092.  
  1093.  
  1094. //subroutine vec4 MappingRoutineType();
  1095.  
  1096.  
  1097.  
  1098. //layout(location = 0) subroutine uniform MappingRoutineType MappingRoutine;
  1099.  
  1100.  
  1101.  
  1102. //subroutine(MappingRoutineType)
  1103.  
  1104. vec4 mappingFlat(){
  1105.  
  1106.     return getPixelColour(VAR._texCoord, VAR._normalWV);
  1107.  
  1108. }
  1109.  
  1110.  
  1111.  
  1112. #if defined(COMPUTE_TBN)
  1113.  
  1114. //subroutine(MappingRoutineType)
  1115.  
  1116. vec4 mappingNormal(){
  1117.  
  1118.     vec3 bump = getBump(VAR._texCoord);
  1119.  
  1120.     return mix(getPixelColour(VAR._texCoord, getTBNNormal(bump)),
  1121.  
  1122.                mappingFlat(),
  1123.  
  1124.                vec4(dvd_lodLevel > 1));
  1125.  
  1126. }
  1127.  
  1128.  
  1129.  
  1130. //subroutine(MappingRoutineType)
  1131.  
  1132. vec4 mappingRelief(){
  1133.  
  1134.     return mix(ReliefMapping(bumpMapLightID, VAR._texCoord),
  1135.  
  1136.                mappingFlat(),
  1137.  
  1138.                vec4(dvd_lodLevel > 1));
  1139.  
  1140. }
  1141.  
  1142.  
  1143.  
  1144. //subroutine(MappingRoutineType)
  1145.  
  1146. vec4 mappingParallax(){
  1147.  
  1148.     return mix(ParallaxMapping(bumpMapLightID, VAR._texCoord),
  1149.  
  1150.                mappingFlat(),
  1151.  
  1152.                vec4(dvd_lodLevel > 1));
  1153.  
  1154. }
  1155.  
  1156. #endif
  1157.  
  1158.  
  1159.  
  1160. void main (void){
  1161.  
  1162.     //_colourOut = ToSRGB(applyFog(MappingRoutine()));
  1163.  
  1164. #if defined(COMPUTE_TBN)
  1165.  
  1166. #    if defined(USE_PARALLAX_MAPPING)
  1167.  
  1168.     _colourOut = ToSRGB(applyFog(ParallaxMapping()));
  1169.  
  1170. #    elif defined(USE_RELIEF_MAPPING)
  1171.  
  1172.     _colourOut = ToSRGB(applyFog(ReliefMapping()));
  1173.  
  1174. #    else
  1175.  
  1176.     _colourOut = ToSRGB(applyFog(mappingNormal()));
  1177.  
  1178. #    endif
  1179.  
  1180. #else
  1181.  
  1182.     _colourOut = ToSRGB(applyFog(mappingFlat()));
  1183.  
  1184. #endif
  1185.  
  1186.  
  1187.  
  1188.     _normalOut = processedNormal;
  1189.  
  1190. }
Advertisement
Add Comment
Please, Sign In to add comment