Volumetric.frag.glsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "shaders/Common.glsl"
  6. #include "shaders/Functions.glsl"
  7. #include "shaders/Clusterer.glsl"
  8. #define LIGHT_TEX_BINDING 3
  9. #define LIGHT_UBO_BINDING 0
  10. #define LIGHT_SS_BINDING 0
  11. #define LIGHT_SET 0
  12. #include "shaders/ClusterLightCommon.glsl"
  13. layout(location = 0) in vec2 in_uv;
  14. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msDepthRt;
  15. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2DArray u_noiseTex;
  16. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_historyRt;
  17. layout(std140, ANKI_UBO_BINDING(0, 3), row_major) uniform ubo0_
  18. {
  19. vec4 u_linearizeNoiseTexOffsetLayer;
  20. vec4 u_fogParticleColorPad1;
  21. mat4 u_prevViewProjMatMulInvViewProjMat;
  22. };
  23. #define u_linearize readFirstInvocationARB(u_linearizeNoiseTexOffsetLayer.xy)
  24. #define u_noiseYOffset readFirstInvocationARB(u_linearizeNoiseTexOffsetLayer.z)
  25. #define u_noiseLayer readFirstInvocationARB(u_linearizeNoiseTexOffsetLayer.w)
  26. #define u_fogParticleColor readFirstInvocationARB(u_fogParticleColorPad1.rgb)
  27. layout(location = 0) out vec3 out_color;
  28. #define ENABLE_SHADOWS 1
  29. const uint MAX_SAMPLES_PER_CLUSTER = 4u;
  30. const float DIST_BETWEEN_SAMPLES = 0.25;
  31. // Return the diffuse color without taking into account the diffuse term of the particles.
  32. vec3 computeLightColor(vec3 fragPos, uint plightCount, uint plightIdx, uint slightCount, uint slightIdx)
  33. {
  34. vec3 outColor = vec3(0.0);
  35. // Point lights
  36. while(plightCount-- != 0)
  37. {
  38. PointLight light = u_pointLights[u_lightIndices[plightIdx++]];
  39. vec3 frag2Light = light.posRadius.xyz - fragPos;
  40. float factor = computeAttenuationFactor(light.posRadius.w, frag2Light);
  41. #if ENABLE_SHADOWS
  42. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  43. if(light.diffuseColorShadowmapId.w >= 0.0)
  44. {
  45. factor *= computeShadowFactorOmni(
  46. frag2Light, shadowmapLayerIdx, -1.0 / light.posRadius.w, u_invViewRotation, u_omniMapArr);
  47. }
  48. #endif
  49. outColor += light.diffuseColorShadowmapId.rgb * factor;
  50. }
  51. // Spot lights
  52. while(slightCount-- != 0)
  53. {
  54. SpotLight light = u_spotLights[u_lightIndices[slightIdx++]];
  55. vec3 frag2Light = light.posRadius.xyz - fragPos;
  56. float factor = computeAttenuationFactor(light.posRadius.w, frag2Light);
  57. vec3 l = normalize(frag2Light);
  58. factor *= computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  59. #if ENABLE_SHADOWS
  60. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  61. if(shadowmapLayerIdx >= 0.0)
  62. {
  63. factor *= computeShadowFactorSpot(light.texProjectionMat, fragPos, shadowmapLayerIdx, 1, u_spotMapArr);
  64. }
  65. #endif
  66. outColor += light.diffuseColorShadowmapId.rgb * factor;
  67. }
  68. return outColor;
  69. }
  70. void main()
  71. {
  72. float depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
  73. vec3 ndc = UV_TO_NDC(vec3(in_uv, depth));
  74. vec4 v4 = u_prevViewProjMatMulInvViewProjMat * vec4(ndc, 1.0);
  75. vec2 oldUv = NDC_TO_UV(v4.xy / v4.w);
  76. vec3 history = textureLod(u_historyRt, oldUv, 0.0).rgb;
  77. vec3 farPos;
  78. farPos.z = u_unprojectionParams.z / (u_unprojectionParams.w + depth);
  79. farPos.xy = ndc.xy * u_unprojectionParams.xy * farPos.z;
  80. vec3 viewDir = normalize(farPos);
  81. uint i = uint(in_uv.x * float(CLUSTER_COUNT.x));
  82. uint j = uint(in_uv.y * float(CLUSTER_COUNT.y));
  83. uint ij = j * CLUSTER_COUNT.x + i;
  84. vec3 noiseTexUv = vec3(vec2(FB_SIZE) / vec2(NOISE_MAP_SIZE) * in_uv + vec2(0.0, u_noiseYOffset), u_noiseLayer);
  85. float randFactor = clamp(texture(u_noiseTex, noiseTexUv).r, EPSILON, 1.0 - EPSILON);
  86. float kNear = -u_near;
  87. vec3 newCol = vec3(0.0);
  88. for(uint k = 0u; k < CLUSTER_COUNT.z; ++k)
  89. {
  90. float kFar = computeClusterFar(k, u_near, u_clustererMagic);
  91. //
  92. // Compute sample count
  93. //
  94. float diff = kNear - kFar;
  95. float samplesf = clamp(diff / DIST_BETWEEN_SAMPLES, 1.0, float(MAX_SAMPLES_PER_CLUSTER));
  96. float dist = 1.0 / samplesf;
  97. float start = dist * randFactor;
  98. //
  99. // Find index ranges
  100. //
  101. uint clusterIdx = k * (CLUSTER_COUNT.x * CLUSTER_COUNT.y) + ij;
  102. uint idxOffset = u_clusters[clusterIdx];
  103. // Skip decals
  104. uint count = u_lightIndices[idxOffset];
  105. idxOffset += count + 1;
  106. uint plightCount = u_lightIndices[idxOffset++];
  107. uint plightIdx = idxOffset;
  108. idxOffset += plightCount;
  109. uint slightCount = u_lightIndices[idxOffset++];
  110. uint slightIdx = idxOffset;
  111. for(float factor = start; factor <= 1.0; factor += dist)
  112. {
  113. float zMedian = mix(kNear, kFar, factor);
  114. if(zMedian < farPos.z)
  115. {
  116. k = CLUSTER_COUNT.z; // Break the outer loop
  117. break;
  118. }
  119. vec3 fragPos = viewDir * (zMedian / viewDir.z);
  120. newCol += computeLightColor(fragPos, plightCount, plightIdx, slightCount, slightIdx);
  121. }
  122. kNear = kFar;
  123. }
  124. newCol *= u_fogParticleColor;
  125. history = max(history, newCol);
  126. out_color = mix(history, newCol, 1.0 / 16.0);
  127. }