FsCommonFrag.glsl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (C) 2009-2016, 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_lightingUniforms.rendererSizeTimePad1.z
  20. #define RENDERER_SIZE (u_lightingUniforms.rendererSizeTimePad1.xy * 0.5)
  21. layout(location = 0) in vec3 in_vertPosViewSpace;
  22. layout(location = 1) flat in float in_alpha;
  23. layout(location = 0) out vec4 out_color;
  24. #if PASS == COLOR
  25. #define texture_DEFINED
  26. #endif
  27. #define getAlpha_DEFINED
  28. float getAlpha()
  29. {
  30. return in_alpha;
  31. }
  32. #define getPointCoord_DEFINED
  33. #define getPointCoord() gl_PointCoord
  34. #if PASS == COLOR
  35. #define writeGBuffer_DEFINED
  36. void writeGBuffer(in vec4 color)
  37. {
  38. out_color = color;
  39. }
  40. #endif
  41. #if PASS == COLOR
  42. #define particleAlpha_DEFINED
  43. void particleAlpha(in sampler2D tex, in float alpha)
  44. {
  45. vec4 color = texture(tex, gl_PointCoord);
  46. color.a *= alpha;
  47. writeGBuffer(color);
  48. }
  49. #endif
  50. #if PASS == COLOR
  51. #define particleSoftTextureAlpha_DEFINED
  52. void particleSoftTextureAlpha(in sampler2D depthMap, in sampler2D tex, in float alpha)
  53. {
  54. vec2 screenSize = 1.0 / RENDERER_SIZE;
  55. float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
  56. float delta = depth - gl_FragCoord.z;
  57. float softalpha = clamp(delta * 50.0, 0.0, 1.0);
  58. vec4 color = texture(tex, gl_PointCoord);
  59. color.a *= alpha;
  60. // color.a *= softalpha;
  61. writeGBuffer(color);
  62. }
  63. #endif
  64. #if PASS == COLOR
  65. #define particleTextureAlpha_DEFINED
  66. void particleTextureAlpha(in sampler2D tex, vec4 mulColor, vec4 addColor, in float alpha)
  67. {
  68. vec4 color = texture(tex, gl_PointCoord) * mulColor + addColor;
  69. color.a *= alpha;
  70. writeGBuffer(color);
  71. }
  72. #endif
  73. #if PASS == COLOR
  74. #define particleSoftColorAlpha_DEFINED
  75. void particleSoftColorAlpha(in sampler2D depthMap, in vec3 icolor, in float alpha)
  76. {
  77. vec2 screenSize = 1.0 / RENDERER_SIZE;
  78. float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
  79. float delta = depth - gl_FragCoord.z;
  80. float softalpha = clamp(delta * 50.0, 0.0, 1.0);
  81. vec2 pix = (1.0 - abs(gl_PointCoord * 2.0 - 1.0));
  82. float roundFactor = pix.x * pix.y;
  83. vec4 color;
  84. color.rgb = icolor;
  85. color.a = alpha * softalpha * roundFactor;
  86. writeGBuffer(color);
  87. }
  88. #endif
  89. #if PASS == COLOR
  90. #define computeLightColor_DEFINED
  91. vec3 computeLightColor(vec3 diffCol)
  92. {
  93. vec3 outColor = vec3(0.0);
  94. // Compute frag pos in view space
  95. vec3 fragPos;
  96. {
  97. float depth = gl_FragCoord.z;
  98. fragPos.z = u_lightingUniforms.projectionParams.z / (u_lightingUniforms.projectionParams.w + depth);
  99. vec2 screenSize = 1.0 / RENDERER_SIZE;
  100. vec2 ndc = gl_FragCoord.xy * screenSize * 2.0 - 1.0;
  101. fragPos.xy = ndc * u_lightingUniforms.projectionParams.xy * fragPos.z;
  102. }
  103. // Find the cluster and then the light counts
  104. uint clusterIdx = computeClusterIndexUsingCustomFragCoord(u_lightingUniforms.nearFarClustererMagicPad1.x,
  105. u_lightingUniforms.nearFarClustererMagicPad1.z,
  106. fragPos.z,
  107. u_lightingUniforms.tileCount.x,
  108. u_lightingUniforms.tileCount.y,
  109. gl_FragCoord.xy * 2.0);
  110. uint idxOffset = u_clusters[clusterIdx];
  111. // Skip decals
  112. uint count = u_lightIndices[idxOffset];
  113. idxOffset += count + 1;
  114. // Point lights
  115. count = u_lightIndices[idxOffset++];
  116. while(count-- != 0)
  117. {
  118. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  119. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  120. vec3 frag2Light = light.posRadius.xyz - fragPos;
  121. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  122. #if LOD > 1
  123. const float shadow = 1.0;
  124. #else
  125. float shadow = 1.0;
  126. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  127. if(light.diffuseColorShadowmapId.w < 128.0)
  128. {
  129. shadow = computeShadowFactorOmni(
  130. frag2Light, shadowmapLayerIdx, -1.0 / light.posRadius.w, u_lightingUniforms.viewMat, u_omniMapArr);
  131. }
  132. #endif
  133. outColor += diffC * (att * shadow);
  134. }
  135. // Spot lights
  136. count = u_lightIndices[idxOffset++];
  137. while(count-- != 0)
  138. {
  139. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  140. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  141. vec3 frag2Light = light.posRadius.xyz - fragPos;
  142. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  143. vec3 l = normalize(frag2Light);
  144. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  145. #if LOD > 1
  146. const float shadow = 1.0;
  147. #else
  148. float shadow = 1.0;
  149. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  150. if(shadowmapLayerIdx < 128.0)
  151. {
  152. shadow = computeShadowFactorSpot(light.texProjectionMat, fragPos, shadowmapLayerIdx, 1, u_spotMapArr);
  153. }
  154. #endif
  155. outColor += diffC * (att * spot * shadow);
  156. }
  157. return outColor;
  158. }
  159. #endif
  160. #if PASS == COLOR
  161. #define particleTextureAlphaLight_DEFINED
  162. void particleTextureAlphaLight(in sampler2D tex, in float alpha)
  163. {
  164. vec4 color = texture(tex, gl_PointCoord);
  165. color.a *= alpha;
  166. color.rgb = computeLightColor(color.rgb);
  167. writeGBuffer(color);
  168. }
  169. #endif
  170. #if PASS == COLOR
  171. #define particleAnimatedTextureAlphaLight_DEFINED
  172. void particleAnimatedTextureAlphaLight(sampler2DArray tex, float alpha, float layerCount, float period)
  173. {
  174. vec4 color = readAnimatedTextureRgba(tex, layerCount, period, gl_PointCoord, anki_u_time);
  175. color.rgb = computeLightColor(color.rgb);
  176. color.a *= alpha;
  177. writeGBuffer(color);
  178. }
  179. #endif
  180. #if PASS == COLOR
  181. #define fog_DEFINED
  182. void fog(in sampler2D depthMap, in vec3 color, in float fogScale)
  183. {
  184. const vec2 screenSize = 1.0 / RENDERER_SIZE;
  185. vec2 texCoords = gl_FragCoord.xy * screenSize;
  186. float depth = texture(depthMap, texCoords).r;
  187. float diff;
  188. if(depth < 1.0)
  189. {
  190. float zNear = u_lightingUniforms.nearFarClustererMagicPad1.x;
  191. float zFar = u_lightingUniforms.nearFarClustererMagicPad1.y;
  192. vec2 linearDepths = (2.0 * zNear) / (zFar + zNear - vec2(depth, gl_FragCoord.z) * (zFar - zNear));
  193. diff = linearDepths.x - linearDepths.y;
  194. }
  195. else
  196. {
  197. // The depth buffer is cleared at this place. Set the diff to zero to avoid weird pop ups
  198. diff = 0.0;
  199. }
  200. writeGBuffer(vec4(color, diff * fogScale));
  201. }
  202. #endif
  203. #endif