ForwardShadingCommonFrag.glsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2009-2020, 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 <shaders/Common.glsl>
  8. #include <shaders/Functions.glsl>
  9. // Global resources
  10. layout(set = 0, binding = 0) uniform sampler u_linearAnyClampSampler;
  11. layout(set = 0, binding = 1) uniform texture2D u_gbufferDepthRt;
  12. layout(set = 0, binding = 2) uniform texture3D u_lightVol;
  13. #define LIGHT_SET 0
  14. #define LIGHT_COMMON_UNIS_BINDING 3
  15. #define LIGHT_LIGHTS_BINDING 4
  16. #define LIGHT_CLUSTERS_BINDING 7
  17. #include <shaders/ClusteredShadingCommon.glsl>
  18. #define anki_u_time u_time
  19. #define RENDERER_SIZE (u_rendererSize)
  20. layout(location = 0) out Vec4 out_color;
  21. void writeGBuffer(Vec4 color)
  22. {
  23. out_color = Vec4(color.rgb, color.a);
  24. }
  25. Vec4 readAnimatedTextureRgba(texture2DArray tex, sampler sampl, F32 period, Vec2 uv, F32 time)
  26. {
  27. const F32 layerCount = F32(textureSize(tex, 0).z);
  28. const F32 layer = mod(time * layerCount / period, layerCount);
  29. return texture(tex, sampl, Vec3(uv, layer));
  30. }
  31. // Iterate the clusters to compute the light color
  32. Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
  33. {
  34. diffCol = diffuseLambert(diffCol);
  35. Vec3 outColor = Vec3(0.0);
  36. // Find the cluster and then the light counts
  37. const U32 clusterIdx = computeClusterIndex(u_clustererMagic, gl_FragCoord.xy / RENDERER_SIZE, worldPos,
  38. u_clusterCountX, u_clusterCountY);
  39. U32 idxOffset = u_clusters[clusterIdx];
  40. // Point lights
  41. U32 idx;
  42. ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
  43. {
  44. const PointLight light = u_pointLights[idx];
  45. const Vec3 diffC = diffCol * light.m_diffuseColor;
  46. const Vec3 frag2Light = light.m_position - worldPos;
  47. const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
  48. #if LOD > 1
  49. const F32 shadow = 1.0;
  50. #else
  51. F32 shadow = 1.0;
  52. if(light.m_shadowAtlasTileScale >= 0.0)
  53. {
  54. shadow = computeShadowFactorPointLight(light, frag2Light, u_shadowTex, u_linearAnyClampSampler);
  55. }
  56. #endif
  57. outColor += diffC * (att * shadow);
  58. }
  59. // Spot lights
  60. ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
  61. {
  62. const SpotLight light = u_spotLights[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_dir);
  68. #if LOD > 1
  69. const F32 shadow = 1.0;
  70. #else
  71. F32 shadow = 1.0;
  72. const F32 shadowmapLayerIdx = light.m_shadowmapId;
  73. if(shadowmapLayerIdx >= 0.0)
  74. {
  75. shadow = computeShadowFactorSpotLight(light, worldPos, u_shadowTex, u_linearAnyClampSampler);
  76. }
  77. #endif
  78. outColor += diffC * (att * spot * shadow);
  79. }
  80. return outColor;
  81. }
  82. // Just read the light color from the vol texture
  83. Vec3 computeLightColorLow(Vec3 diffCol, Vec3 worldPos)
  84. {
  85. const Vec2 uv = gl_FragCoord.xy / RENDERER_SIZE;
  86. const Vec3 uv3d = computeClustererVolumeTextureUvs(u_clustererMagic, uv, worldPos, u_lightVolumeLastCluster + 1u);
  87. const Vec3 light = textureLod(u_lightVol, u_linearAnyClampSampler, uv3d, 0.0).rgb;
  88. return diffuseLambert(diffCol) * light;
  89. }
  90. void particleAlpha(Vec4 color, Vec4 scaleColor, Vec4 biasColor)
  91. {
  92. writeGBuffer(color * scaleColor + biasColor);
  93. }
  94. void fog(Vec3 color, F32 fogAlphaScale, F32 fogDistanceOfMaxThikness, F32 zVSpace)
  95. {
  96. const Vec2 screenSize = 1.0 / RENDERER_SIZE;
  97. const Vec2 texCoords = gl_FragCoord.xy * screenSize;
  98. const F32 depth = textureLod(u_gbufferDepthRt, u_linearAnyClampSampler, texCoords, 0.0).r;
  99. F32 zFeatherFactor;
  100. const Vec4 fragPosVspace4 = u_invProjMat * Vec4(Vec3(UV_TO_NDC(texCoords), depth), 1.0);
  101. const F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
  102. const F32 diff = max(0.0, zVSpace - sceneZVspace);
  103. zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
  104. writeGBuffer(Vec4(color, zFeatherFactor * fogAlphaScale));
  105. }