ForwardShadingCommon.glsl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2009-2022, 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. #include <AnKi/Shaders/Common.glsl>
  7. #include <AnKi/Shaders/Functions.glsl>
  8. #include <AnKi/Shaders/Include/ModelTypes.h>
  9. #include <AnKi/Shaders/Include/MaterialTypes.h>
  10. #include <AnKi/Shaders/Include/GpuSceneTypes.h>
  11. //
  12. // Vert
  13. //
  14. #if defined(ANKI_VERTEX_SHADER)
  15. layout(location = VERTEX_ATTRIBUTE_ID_POSITION) in Vec3 in_position;
  16. #endif
  17. //
  18. // Frag
  19. //
  20. #if defined(ANKI_FRAGMENT_SHADER)
  21. // Global resources
  22. layout(set = MATERIAL_SET_GLOBAL,
  23. binding = MATERIAL_BINDING_LINEAR_CLAMP_SAMPLER) uniform sampler u_linearAnyClampSampler;
  24. layout(set = MATERIAL_SET_GLOBAL, binding = MATERIAL_BINDING_DEPTH_RT) uniform texture2D u_gbufferDepthRt;
  25. layout(set = MATERIAL_SET_GLOBAL, binding = MATERIAL_BINDING_LIGHT_VOLUME) uniform ANKI_RP texture3D u_lightVol;
  26. # define CLUSTERED_SHADING_SET MATERIAL_SET_GLOBAL
  27. # define CLUSTERED_SHADING_UNIFORMS_BINDING MATERIAL_BINDING_CLUSTER_SHADING_UNIFORMS
  28. # define CLUSTERED_SHADING_LIGHTS_BINDING MATERIAL_BINDING_CLUSTER_SHADING_LIGHTS
  29. # define CLUSTERED_SHADING_CLUSTERS_BINDING MATERIAL_BINDING_CLUSTERS
  30. # include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  31. layout(location = 0) out Vec4 out_color;
  32. void packGBuffer(Vec4 color)
  33. {
  34. out_color = Vec4(color.rgb, color.a);
  35. }
  36. ANKI_RP Vec4 readAnimatedTextureRgba(ANKI_RP texture2DArray tex, sampler sampl, F32 period, Vec2 uv, F32 time)
  37. {
  38. const F32 layerCount = F32(textureSize(tex, 0).z);
  39. const F32 layer = mod(time * layerCount / period, layerCount);
  40. return texture(tex, sampl, Vec3(uv, layer));
  41. }
  42. // Iterate the clusters to compute the light color
  43. Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos)
  44. {
  45. diffCol = diffuseLobe(diffCol);
  46. Vec3 outColor = Vec3(0.0);
  47. // Find the cluster and then the light counts
  48. Cluster cluster = getClusterFragCoord(gl_FragCoord.xyz);
  49. // Point lights
  50. ANKI_LOOP while(cluster.m_pointLightsMask != ExtendedClusterObjectMask(0))
  51. {
  52. const I32 idx = findLSB2(cluster.m_pointLightsMask);
  53. cluster.m_pointLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  54. const PointLight light = u_pointLights2[idx];
  55. const Vec3 diffC = diffCol * light.m_diffuseColor;
  56. const Vec3 frag2Light = light.m_position - worldPos;
  57. const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
  58. # if LOD > 1
  59. const F32 shadow = 1.0;
  60. # else
  61. F32 shadow = 1.0;
  62. if(light.m_shadowAtlasTileScale >= 0.0)
  63. {
  64. shadow = computeShadowFactorPointLight(light, frag2Light, u_shadowAtlasTex, u_linearAnyClampSampler);
  65. }
  66. # endif
  67. outColor += diffC * (att * shadow);
  68. }
  69. // Spot lights
  70. ANKI_LOOP while(cluster.m_spotLightsMask != ExtendedClusterObjectMask(0))
  71. {
  72. const I32 idx = findLSB2(cluster.m_spotLightsMask);
  73. cluster.m_spotLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  74. const SpotLight light = u_spotLights[idx];
  75. const Vec3 diffC = diffCol * light.m_diffuseColor;
  76. const Vec3 frag2Light = light.m_position - worldPos;
  77. const F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light);
  78. const Vec3 l = normalize(frag2Light);
  79. const F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_direction);
  80. # if LOD > 1
  81. const F32 shadow = 1.0;
  82. # else
  83. F32 shadow = 1.0;
  84. ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
  85. {
  86. shadow = computeShadowFactorSpotLight(light, worldPos, u_shadowAtlasTex, u_linearAnyClampSampler);
  87. }
  88. # endif
  89. outColor += diffC * (att * spot * shadow);
  90. }
  91. return outColor;
  92. }
  93. // Just read the light color from the vol texture
  94. ANKI_RP Vec3 computeLightColorLow(ANKI_RP Vec3 diffCol, ANKI_RP Vec3 worldPos)
  95. {
  96. const Vec2 uv = gl_FragCoord.xy / u_clusteredShading.m_renderingSize;
  97. const F32 linearDepth = linearizeDepth(gl_FragCoord.z, u_clusteredShading.m_near, u_clusteredShading.m_far);
  98. const Vec3 uvw =
  99. Vec3(uv, linearDepth
  100. * (F32(u_clusteredShading.m_zSplitCount) / F32(u_clusteredShading.m_lightVolumeLastZSplit + 1u)));
  101. const ANKI_RP Vec3 light = textureLod(u_lightVol, u_linearAnyClampSampler, uvw, 0.0).rgb;
  102. return diffuseLobe(diffCol) * light;
  103. }
  104. void particleAlpha(ANKI_RP Vec4 color, ANKI_RP Vec4 scaleColor, ANKI_RP Vec4 biasColor)
  105. {
  106. packGBuffer(color * scaleColor + biasColor);
  107. }
  108. void fog(ANKI_RP Vec3 color, ANKI_RP F32 fogAlphaScale, ANKI_RP F32 fogDistanceOfMaxThikness, F32 zVSpace)
  109. {
  110. const Vec2 screenSize = 1.0 / u_clusteredShading.m_renderingSize;
  111. const Vec2 texCoords = gl_FragCoord.xy * screenSize;
  112. const F32 depth = textureLod(u_gbufferDepthRt, u_linearAnyClampSampler, texCoords, 0.0).r;
  113. F32 zFeatherFactor;
  114. const Vec4 fragPosVspace4 =
  115. u_clusteredShading.m_matrices.m_invertedProjectionJitter * Vec4(Vec3(UV_TO_NDC(texCoords), depth), 1.0);
  116. const F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
  117. const F32 diff = max(0.0, zVSpace - sceneZVspace);
  118. zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
  119. packGBuffer(Vec4(color, zFeatherFactor * fogAlphaScale));
  120. }
  121. #endif // defined(ANKI_FRAGMENT_SHADER)