ForwardShadingCommonFrag.glsl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2009-2018, 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(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_gbufferDepthRt;
  11. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler3D u_lightVol;
  12. #define LIGHT_SET 0
  13. #define LIGHT_UBO_BINDING 0
  14. #define LIGHT_SS_BINDING 0
  15. #define LIGHT_TEX_BINDING 2
  16. #define LIGHT_LIGHTS
  17. #define LIGHT_COMMON_UNIS
  18. #include <shaders/ClusteredShadingCommon.glsl>
  19. #define anki_u_time u_time
  20. #define RENDERER_SIZE (u_rendererSize)
  21. layout(location = 0) out Vec4 out_color;
  22. void writeGBuffer(in Vec4 color)
  23. {
  24. out_color = Vec4(color.rgb, color.a);
  25. }
  26. Vec4 readAnimatedTextureRgba(sampler2DArray tex, F32 period, Vec2 uv, F32 time)
  27. {
  28. F32 layerCount = F32(textureSize(tex, 0).z);
  29. F32 layer = mod(time * layerCount / period, layerCount);
  30. return texture(tex, Vec3(uv, layer));
  31. }
  32. // Iterate the clusters to compute the light color
  33. Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
  34. {
  35. diffCol = diffuseLambert(diffCol);
  36. Vec3 outColor = Vec3(0.0);
  37. // Find the cluster and then the light counts
  38. U32 clusterIdx = computeClusterIndex(
  39. u_clustererMagic, gl_FragCoord.xy / RENDERER_SIZE, worldPos, u_clusterCountX, u_clusterCountY);
  40. U32 idxOffset = u_clusters[clusterIdx];
  41. // Point lights
  42. U32 idx;
  43. ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
  44. {
  45. PointLight light = u_pointLights[idx];
  46. Vec3 diffC = diffCol * light.m_diffuseColorTileSize.rgb;
  47. Vec3 frag2Light = light.m_posRadius.xyz - worldPos;
  48. F32 att = computeAttenuationFactor(light.m_posRadius.w, frag2Light);
  49. #if LOD > 1
  50. const F32 shadow = 1.0;
  51. #else
  52. F32 shadow = 1.0;
  53. if(light.m_diffuseColorTileSize.w >= 0.0)
  54. {
  55. shadow = computeShadowFactorOmni(
  56. frag2Light, light.m_radiusPad1.x, light.m_atlasTiles, light.m_diffuseColorTileSize.w, u_shadowTex);
  57. }
  58. #endif
  59. outColor += diffC * (att * shadow);
  60. }
  61. // Spot lights
  62. ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
  63. {
  64. SpotLight light = u_spotLights[idx];
  65. Vec3 diffC = diffCol * light.m_diffuseColorShadowmapId.rgb;
  66. Vec3 frag2Light = light.m_posRadius.xyz - worldPos;
  67. F32 att = computeAttenuationFactor(light.m_posRadius.w, frag2Light);
  68. Vec3 l = normalize(frag2Light);
  69. F32 spot =
  70. computeSpotFactor(l, light.m_outerCosInnerCos.x, light.m_outerCosInnerCos.y, light.m_lightDirRadius.xyz);
  71. #if LOD > 1
  72. const F32 shadow = 1.0;
  73. #else
  74. F32 shadow = 1.0;
  75. F32 shadowmapLayerIdx = light.m_diffuseColorShadowmapId.w;
  76. if(shadowmapLayerIdx >= 0.0)
  77. {
  78. shadow = computeShadowFactorSpot(light.m_texProjectionMat, worldPos, light.m_lightDirRadius.w, u_shadowTex);
  79. }
  80. #endif
  81. outColor += diffC * (att * spot * shadow);
  82. }
  83. return outColor;
  84. }
  85. // Just read the light color from the vol texture
  86. Vec3 computeLightColorLow(Vec3 diffCol, Vec3 worldPos)
  87. {
  88. Vec2 uv = gl_FragCoord.xy / RENDERER_SIZE;
  89. Vec3 uv3d = computeClustererVolumeTextureUvs(u_clustererMagic, uv, worldPos, u_lightVolumeLastCluster + 1u);
  90. Vec3 light = textureLod(u_lightVol, uv3d, 0.0).rgb;
  91. return diffuseLambert(diffCol) * light;
  92. }
  93. void particleAlpha(Vec4 color, Vec4 scaleColor, Vec4 biasColor)
  94. {
  95. writeGBuffer(color * scaleColor + biasColor);
  96. }
  97. void fog(Vec3 color, F32 fogAlphaScale, F32 fogDistanceOfMaxThikness, F32 zVSpace)
  98. {
  99. const Vec2 screenSize = 1.0 / RENDERER_SIZE;
  100. Vec2 texCoords = gl_FragCoord.xy * screenSize;
  101. F32 depth = texture(u_gbufferDepthRt, texCoords).r;
  102. F32 zFeatherFactor;
  103. Vec4 fragPosVspace4 = u_invProjMat * Vec4(Vec3(UV_TO_NDC(texCoords), depth), 1.0);
  104. F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
  105. F32 diff = max(0.0, zVSpace - sceneZVspace);
  106. zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
  107. writeGBuffer(Vec4(color, zFeatherFactor * fogAlphaScale));
  108. }