ForwardShadingCommonFrag.glsl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. // Common code for all fragment shaders of FS
  7. #include <AnKi/Shaders/Common.glsl>
  8. #include <AnKi/Shaders/Functions.glsl>
  9. #include <AnKi/Shaders/Include/ModelTypes.h>
  10. // Global resources
  11. layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
  12. layout(set = 0, binding = 1) uniform texture2D u_gbufferDepthRt;
  13. layout(set = 0, binding = 2) uniform texture3D u_lightVol;
  14. #define CLUSTERED_SHADING_SET 0
  15. #define CLUSTERED_SHADING_UNIFORMS_BINDING 3
  16. #define CLUSTERED_SHADING_LIGHTS_BINDING 4
  17. #define CLUSTERED_SHADING_CLUSTERS_BINDING 7
  18. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  19. layout(location = 0) out Vec4 out_color;
  20. void writeGBuffer(Vec4 color)
  21. {
  22. out_color = Vec4(color.rgb, color.a);
  23. }
  24. Vec4 readAnimatedTextureRgba(texture2DArray tex, sampler sampl, F32 period, Vec2 uv, F32 time)
  25. {
  26. const F32 layerCount = F32(textureSize(tex, 0).z);
  27. const F32 layer = mod(time * layerCount / period, layerCount);
  28. return texture(tex, sampl, Vec3(uv, layer));
  29. }
  30. // Iterate the clusters to compute the light color
  31. Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
  32. {
  33. diffCol = diffuseLambert(diffCol);
  34. Vec3 outColor = Vec3(0.0);
  35. // Find the cluster and then the light counts
  36. Cluster cluster = getClusterFragCoord(gl_FragCoord.xyz);
  37. // Point lights
  38. ANKI_LOOP while(cluster.m_pointLightsMask != ExtendedClusterObjectMask(0))
  39. {
  40. const I32 idx = findLSB2(cluster.m_pointLightsMask);
  41. cluster.m_pointLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  42. const PointLight light = u_pointLights2[idx];
  43. const Vec3 diffC = diffCol * light.m_diffuseColor;
  44. const Vec3 frag2Light = light.m_position - worldPos;
  45. const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
  46. #if LOD > 1
  47. const F32 shadow = 1.0;
  48. #else
  49. F32 shadow = 1.0;
  50. if(light.m_shadowAtlasTileScale >= 0.0)
  51. {
  52. shadow = computeShadowFactorPointLight(light, frag2Light, u_shadowAtlasTex, u_linearAnyClampSampler);
  53. }
  54. #endif
  55. outColor += diffC * (att * shadow);
  56. }
  57. // Spot lights
  58. ANKI_LOOP while(cluster.m_spotLightsMask != ExtendedClusterObjectMask(0))
  59. {
  60. const I32 idx = findLSB2(cluster.m_spotLightsMask);
  61. cluster.m_spotLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  62. const SpotLight light = u_spotLights2[idx];
  63. const Vec3 diffC = diffCol * light.m_diffuseColor;
  64. const Vec3 frag2Light = light.m_position - worldPos;
  65. const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
  66. const Vec3 l = normalize(frag2Light);
  67. const F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_direction);
  68. #if LOD > 1
  69. const F32 shadow = 1.0;
  70. #else
  71. F32 shadow = 1.0;
  72. ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
  73. {
  74. shadow = computeShadowFactorSpotLight(light, worldPos, u_shadowAtlasTex, u_linearAnyClampSampler);
  75. }
  76. #endif
  77. outColor += diffC * (att * spot * shadow);
  78. }
  79. return outColor;
  80. }
  81. // Just read the light color from the vol texture
  82. Vec3 computeLightColorLow(Vec3 diffCol, Vec3 worldPos)
  83. {
  84. const Vec2 uv = gl_FragCoord.xy / u_clusteredShading.m_renderingSize;
  85. const F32 linearDepth = linearizeDepth(gl_FragCoord.z, u_clusteredShading.m_near, u_clusteredShading.m_far);
  86. const Vec3 uvw =
  87. Vec3(uv, linearDepth
  88. * (F32(u_clusteredShading.m_zSplitCount) / F32(u_clusteredShading.m_lightVolumeLastZSplit + 1u)));
  89. const Vec3 light = textureLod(u_lightVol, u_linearAnyClampSampler, uvw, 0.0).rgb;
  90. return diffuseLambert(diffCol) * light;
  91. }
  92. void particleAlpha(Vec4 color, Vec4 scaleColor, Vec4 biasColor)
  93. {
  94. writeGBuffer(color * scaleColor + biasColor);
  95. }
  96. void fog(Vec3 color, F32 fogAlphaScale, F32 fogDistanceOfMaxThikness, F32 zVSpace)
  97. {
  98. const Vec2 screenSize = 1.0 / u_clusteredShading.m_renderingSize;
  99. const Vec2 texCoords = gl_FragCoord.xy * screenSize;
  100. const F32 depth = textureLod(u_gbufferDepthRt, u_linearAnyClampSampler, texCoords, 0.0).r;
  101. F32 zFeatherFactor;
  102. const Vec4 fragPosVspace4 =
  103. u_clusteredShading.m_matrices.m_invertedProjectionJitter * Vec4(Vec3(UV_TO_NDC(texCoords), depth), 1.0);
  104. const F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
  105. const F32 diff = max(0.0, zVSpace - sceneZVspace);
  106. zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
  107. writeGBuffer(Vec4(color, zFeatherFactor * fogAlphaScale));
  108. }