FsCommonFrag.glsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifndef ANKI_SHADERS_FS_COMMON_FRAG_GLSL
  6. #define ANKI_SHADERS_FS_COMMON_FRAG_GLSL
  7. // Common code for all fragment shaders of BS
  8. #include "shaders/Common.glsl"
  9. #include "shaders/MsFsCommon.glsl"
  10. #include "shaders/Functions.glsl"
  11. #include "shaders/Clusterer.glsl"
  12. // Global resources
  13. layout(ANKI_TEX_BINDING(1, 0)) uniform sampler2D anki_msDepthRt;
  14. #define LIGHT_SET 1
  15. #define LIGHT_UBO_BINDING 0
  16. #define LIGHT_SS_BINDING 0
  17. #define LIGHT_TEX_BINDING 1
  18. #include "shaders/ClusterLightCommon.glsl"
  19. #define anki_u_time u_time
  20. #define RENDERER_SIZE (u_lightingUniforms.rendererSizeTimePad1.xy * 0.5)
  21. layout(location = 0) flat in float in_alpha;
  22. layout(location = 1) in vec2 in_uv;
  23. layout(location = 2) in vec3 in_posViewSpace;
  24. layout(location = 0) out vec4 out_color;
  25. #if PASS == COLOR
  26. #define texture_DEFINED
  27. #endif
  28. #define getAlpha_DEFINED
  29. float getAlpha()
  30. {
  31. return in_alpha;
  32. }
  33. #define getPointCoord_DEFINED
  34. #define getPointCoord() gl_PointCoord
  35. #if PASS == COLOR
  36. #define writeGBuffer_DEFINED
  37. void writeGBuffer(in vec4 color)
  38. {
  39. out_color = vec4(color.rgb, 1.0 - color.a);
  40. }
  41. #endif
  42. #if PASS == COLOR
  43. #define particleAlpha_DEFINED
  44. void particleAlpha(in sampler2D tex, in float alpha)
  45. {
  46. vec4 color = texture(tex, gl_PointCoord);
  47. color.a *= alpha;
  48. writeGBuffer(color);
  49. }
  50. #endif
  51. #if PASS == COLOR
  52. #define particleSoftTextureAlpha_DEFINED
  53. void particleSoftTextureAlpha(in sampler2D depthMap, in sampler2D tex, in float alpha)
  54. {
  55. vec2 screenSize = 1.0 / RENDERER_SIZE;
  56. float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
  57. float delta = depth - gl_FragCoord.z;
  58. float softalpha = clamp(delta * 50.0, 0.0, 1.0);
  59. vec4 color = texture(tex, gl_PointCoord);
  60. color.a *= alpha;
  61. // color.a *= softalpha;
  62. writeGBuffer(color);
  63. }
  64. #endif
  65. #if PASS == COLOR
  66. #define particleTextureAlpha_DEFINED
  67. void particleTextureAlpha(in sampler2D tex, vec4 mulColor, vec4 addColor, in float alpha)
  68. {
  69. vec4 color = texture(tex, in_uv) * mulColor + addColor;
  70. color.a *= alpha;
  71. writeGBuffer(color);
  72. }
  73. #endif
  74. #if PASS == COLOR
  75. #define particleSoftColorAlpha_DEFINED
  76. void particleSoftColorAlpha(in sampler2D depthMap, in vec3 icolor, in float alpha)
  77. {
  78. vec2 screenSize = 1.0 / RENDERER_SIZE;
  79. float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
  80. float delta = depth - gl_FragCoord.z;
  81. float softalpha = clamp(delta * 50.0, 0.0, 1.0);
  82. vec2 pix = (1.0 - abs(gl_PointCoord * 2.0 - 1.0));
  83. float roundFactor = pix.x * pix.y;
  84. vec4 color;
  85. color.rgb = icolor;
  86. color.a = alpha * softalpha * roundFactor;
  87. writeGBuffer(color);
  88. }
  89. #endif
  90. #if PASS == COLOR
  91. #define computeLightColor_DEFINED
  92. vec3 computeLightColor(vec3 diffCol)
  93. {
  94. vec3 outColor = vec3(0.0);
  95. // Compute frag pos in view space
  96. vec3 fragPos = in_posViewSpace;
  97. // Find the cluster and then the light counts
  98. uint clusterIdx = computeClusterIndex(
  99. gl_FragCoord.xy / RENDERER_SIZE, u_near, u_clustererMagic, fragPos.z, u_clusterCountX, u_clusterCountY);
  100. uint idxOffset = u_clusters[clusterIdx];
  101. // Skip decals
  102. uint count = u_lightIndices[idxOffset];
  103. idxOffset += count + 1;
  104. // Lights
  105. count = u_lightIndices[idxOffset++];
  106. while(count-- != 0)
  107. {
  108. Light light = u_lights[u_lightIndices[idxOffset++]];
  109. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  110. vec3 frag2Light = light.posRadius.xyz - fragPos;
  111. float factor = computeAttenuationFactor(light.posRadius.w, frag2Light);
  112. #if LOD > 1
  113. if(isSpotLight(light))
  114. {
  115. vec3 l = normalize(frag2Light);
  116. factor *= computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  117. }
  118. #else
  119. if(isSpotLight(light))
  120. {
  121. vec3 l = normalize(frag2Light);
  122. factor *= computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  123. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  124. if(shadowmapLayerIdx >= 0.0)
  125. {
  126. factor *= computeShadowFactorSpot(light.texProjectionMat, fragPos, shadowmapLayerIdx, 1, u_spotMapArr);
  127. }
  128. }
  129. else
  130. {
  131. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  132. if(light.diffuseColorShadowmapId.w >= 0.0)
  133. {
  134. factor *= computeShadowFactorOmni(
  135. frag2Light, shadowmapLayerIdx, light.specularColorRadius.w, u_invViewRotation, u_omniMapArr);
  136. }
  137. }
  138. #endif
  139. outColor += diffC * factor;
  140. }
  141. return outColor;
  142. }
  143. #endif
  144. #if PASS == COLOR
  145. #define particleTextureAlphaLight_DEFINED
  146. void particleTextureAlphaLight(in sampler2D tex, in float alpha)
  147. {
  148. vec4 color = texture(tex, in_uv);
  149. color.a *= alpha;
  150. color.rgb = computeLightColor(color.rgb);
  151. writeGBuffer(color);
  152. }
  153. #endif
  154. #if PASS == COLOR
  155. #define particleAnimatedTextureAlphaLight_DEFINED
  156. void particleAnimatedTextureAlphaLight(sampler2DArray tex, float alpha, float layerCount, float period)
  157. {
  158. vec4 color = readAnimatedTextureRgba(tex, layerCount, period, in_uv, anki_u_time);
  159. color.rgb = computeLightColor(color.rgb);
  160. color.a *= alpha;
  161. writeGBuffer(color);
  162. }
  163. #endif
  164. #if PASS == COLOR
  165. #define fog_DEFINED
  166. void fog(in sampler2D depthMap, in vec3 color, in float fogScale)
  167. {
  168. const vec2 screenSize = 1.0 / RENDERER_SIZE;
  169. vec2 texCoords = gl_FragCoord.xy * screenSize;
  170. float depth = texture(depthMap, texCoords).r;
  171. float diff;
  172. if(depth < 1.0)
  173. {
  174. float zNear = u_near;
  175. float zFar = u_far;
  176. vec2 linearDepths = (2.0 * zNear) / (zFar + zNear - vec2(depth, gl_FragCoord.z) * (zFar - zNear));
  177. diff = linearDepths.x - linearDepths.y;
  178. }
  179. else
  180. {
  181. // The depth buffer is cleared at this place. Set the diff to zero to avoid weird pop ups
  182. diff = 0.0;
  183. }
  184. writeGBuffer(vec4(color, diff * fogScale));
  185. }
  186. #endif
  187. #endif