FsCommonFrag.glsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // Point lights
  105. count = u_lightIndices[idxOffset++];
  106. while(count-- != 0)
  107. {
  108. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  109. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  110. vec3 frag2Light = light.posRadius.xyz - fragPos;
  111. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  112. #if LOD > 1
  113. const float shadow = 1.0;
  114. #else
  115. float shadow = 1.0;
  116. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  117. if(light.diffuseColorShadowmapId.w >= 0.0)
  118. {
  119. shadow = computeShadowFactorOmni(
  120. frag2Light, shadowmapLayerIdx, light.specularColorRadius.w, u_invViewRotation, u_omniMapArr);
  121. }
  122. #endif
  123. outColor += diffC * (att * shadow);
  124. }
  125. // Spot lights
  126. count = u_lightIndices[idxOffset++];
  127. while(count-- != 0)
  128. {
  129. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  130. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  131. vec3 frag2Light = light.posRadius.xyz - fragPos;
  132. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  133. vec3 l = normalize(frag2Light);
  134. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  135. #if LOD > 1
  136. const float shadow = 1.0;
  137. #else
  138. float shadow = 1.0;
  139. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  140. if(shadowmapLayerIdx >= 0.0)
  141. {
  142. shadow = computeShadowFactorSpot(light.texProjectionMat, fragPos, shadowmapLayerIdx, 1, u_spotMapArr);
  143. }
  144. #endif
  145. outColor += diffC * (att * spot * shadow);
  146. }
  147. return outColor;
  148. }
  149. #endif
  150. #if PASS == COLOR
  151. #define particleTextureAlphaLight_DEFINED
  152. void particleTextureAlphaLight(in sampler2D tex, in float alpha)
  153. {
  154. vec4 color = texture(tex, in_uv);
  155. color.a *= alpha;
  156. color.rgb = computeLightColor(color.rgb);
  157. writeGBuffer(color);
  158. }
  159. #endif
  160. #if PASS == COLOR
  161. #define particleAnimatedTextureAlphaLight_DEFINED
  162. void particleAnimatedTextureAlphaLight(sampler2DArray tex, float alpha, float layerCount, float period)
  163. {
  164. vec4 color = readAnimatedTextureRgba(tex, layerCount, period, in_uv, anki_u_time);
  165. color.rgb = computeLightColor(color.rgb);
  166. color.a *= alpha;
  167. writeGBuffer(color);
  168. }
  169. #endif
  170. #if PASS == COLOR
  171. #define fog_DEFINED
  172. void fog(in sampler2D depthMap, in vec3 color, in float fogScale)
  173. {
  174. const vec2 screenSize = 1.0 / RENDERER_SIZE;
  175. vec2 texCoords = gl_FragCoord.xy * screenSize;
  176. float depth = texture(depthMap, texCoords).r;
  177. float diff;
  178. if(depth < 1.0)
  179. {
  180. float zNear = u_near;
  181. float zFar = u_far;
  182. vec2 linearDepths = (2.0 * zNear) / (zFar + zNear - vec2(depth, gl_FragCoord.z) * (zFar - zNear));
  183. diff = linearDepths.x - linearDepths.y;
  184. }
  185. else
  186. {
  187. // The depth buffer is cleared at this place. Set the diff to zero to avoid weird pop ups
  188. diff = 0.0;
  189. }
  190. writeGBuffer(vec4(color, diff * fogScale));
  191. }
  192. #endif
  193. #endif