FsCommonFrag.glsl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // All rights reserved.
  2. // Code licensed under the BSD License.
  3. // http://www.anki3d.org/LICENSE
  4. // Common code for all fragment shaders of BS
  5. #include "shaders/Common.glsl"
  6. #include "shaders/MsFsCommon.glsl"
  7. #include "shaders/LinearDepth.glsl"
  8. #include "shaders/Clusterer.glsl"
  9. // Global resources
  10. layout(TEX_BINDING(1, 0)) uniform sampler2D anki_msDepthRt;
  11. #define LIGHT_SET 1
  12. #define LIGHT_SS_BINDING 0
  13. #define LIGHT_TEX_BINDING 1
  14. #include "shaders/LightResources.glsl"
  15. #undef LIGHT_SET
  16. #undef LIGHT_SS_BINDING
  17. #undef LIGHT_TEX_BINDING
  18. #define anki_u_time u_lightingUniforms.rendererSizeTimePad1.z
  19. #define RENDERER_SIZE (u_lightingUniforms.rendererSizeTimePad1.xy * 0.5)
  20. layout(location = 0) in vec3 in_vertPosViewSpace;
  21. layout(location = 1) flat in float in_alpha;
  22. layout(location = 0) out vec4 out_color;
  23. #include "shaders/LightFunctions.glsl"
  24. //==============================================================================
  25. #if PASS == COLOR
  26. #define texture_DEFINED
  27. #endif
  28. //==============================================================================
  29. #define getAlpha_DEFINED
  30. float getAlpha()
  31. {
  32. return in_alpha;
  33. }
  34. //==============================================================================
  35. #define getPointCoord_DEFINED
  36. #define getPointCoord() gl_PointCoord
  37. //==============================================================================
  38. #if PASS == COLOR
  39. #define writeGBuffer_DEFINED
  40. void writeGBuffer(in vec4 color)
  41. {
  42. out_color = color;
  43. }
  44. #endif
  45. //==============================================================================
  46. #if PASS == COLOR
  47. #define particleAlpha_DEFINED
  48. void particleAlpha(in sampler2D tex, in float alpha)
  49. {
  50. vec4 color = texture(tex, gl_PointCoord);
  51. color.a *= alpha;
  52. writeGBuffer(color);
  53. }
  54. #endif
  55. //==============================================================================
  56. #if PASS == COLOR
  57. #define particleSoftTextureAlpha_DEFINED
  58. void particleSoftTextureAlpha(
  59. in sampler2D depthMap, in sampler2D tex, in float alpha)
  60. {
  61. vec2 screenSize = 1.0 / RENDERER_SIZE;
  62. float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
  63. float delta = depth - gl_FragCoord.z;
  64. float softalpha = clamp(delta * 50.0, 0.0, 1.0);
  65. vec4 color = texture(tex, gl_PointCoord);
  66. color.a *= alpha;
  67. // color.a *= softalpha;
  68. writeGBuffer(color);
  69. }
  70. #endif
  71. //==============================================================================
  72. #if PASS == COLOR
  73. #define particleTextureAlpha_DEFINED
  74. void particleTextureAlpha(in sampler2D tex, in float alpha)
  75. {
  76. vec4 color = texture(tex, gl_PointCoord);
  77. color.a *= alpha;
  78. writeGBuffer(color);
  79. }
  80. #endif
  81. //==============================================================================
  82. #if PASS == COLOR
  83. #define particleSoftColorAlpha_DEFINED
  84. void particleSoftColorAlpha(
  85. in sampler2D depthMap, in vec3 icolor, in float alpha)
  86. {
  87. vec2 screenSize = 1.0 / RENDERER_SIZE;
  88. float depth = texture(depthMap, gl_FragCoord.xy * screenSize).r;
  89. float delta = depth - gl_FragCoord.z;
  90. float softalpha = clamp(delta * 50.0, 0.0, 1.0);
  91. vec2 pix = (1.0 - abs(gl_PointCoord * 2.0 - 1.0));
  92. float roundFactor = pix.x * pix.y;
  93. vec4 color;
  94. color.rgb = icolor;
  95. color.a = alpha * softalpha * roundFactor;
  96. writeGBuffer(color);
  97. }
  98. #endif
  99. //==============================================================================
  100. #if PASS == COLOR
  101. #define computeLightColor_DEFINED
  102. vec3 computeLightColor(vec3 diffCol)
  103. {
  104. vec3 outColor = diffCol * u_lightingUniforms.sceneAmbientColor.rgb;
  105. // Compute frag pos in view space
  106. vec3 fragPos;
  107. {
  108. float depth = gl_FragCoord.z;
  109. fragPos.z = u_lightingUniforms.projectionParams.z
  110. / (u_lightingUniforms.projectionParams.w + depth);
  111. vec2 screenSize = 1.0 / RENDERER_SIZE;
  112. vec2 ndc = gl_FragCoord.xy * screenSize * 2.0 - 1.0;
  113. fragPos.xy = ndc * u_lightingUniforms.projectionParams.xy * fragPos.z;
  114. }
  115. // Find the cluster and then the light counts
  116. uint lightOffset;
  117. uint pointLightsCount;
  118. uint spotLightsCount;
  119. {
  120. uint clusterIdx = computeClusterIndexUsingFragCoord(
  121. u_lightingUniforms.nearFarClustererMagicPad1.x,
  122. u_lightingUniforms.nearFarClustererMagicPad1.z,
  123. fragPos.z,
  124. u_lightingUniforms.tileCountPad1.x,
  125. u_lightingUniforms.tileCountPad1.y);
  126. uint cluster = u_clusters[clusterIdx];
  127. lightOffset = cluster >> 16u;
  128. pointLightsCount = (cluster >> 8u) & 0xFFu;
  129. spotLightsCount = cluster & 0xFFu;
  130. }
  131. // Point lights
  132. for(uint i = 0U; i < pointLightsCount; ++i)
  133. {
  134. uint lightId = u_lightIndices[lightOffset++];
  135. PointLight light = u_pointLights[lightId];
  136. vec3 diffC =
  137. computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  138. vec3 frag2Light = light.posRadius.xyz - fragPos;
  139. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  140. #if LOD > 1
  141. const float shadow = 1.0;
  142. #else
  143. float shadow = 1.0;
  144. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  145. if(light.diffuseColorShadowmapId.w < 128.0)
  146. {
  147. shadow = computeShadowFactorOmni(
  148. frag2Light, shadowmapLayerIdx, -1.0 / light.posRadius.w);
  149. }
  150. #endif
  151. outColor += diffC * (att * shadow);
  152. }
  153. // Spot lights
  154. for(uint i = 0U; i < spotLightsCount; ++i)
  155. {
  156. uint lightId = u_lightIndices[lightOffset++];
  157. SpotLight light = u_spotLights[lightId];
  158. vec3 diffC =
  159. computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb);
  160. vec3 frag2Light = light.posRadius.xyz - fragPos;
  161. float att = computeAttenuationFactor(light.posRadius.w, frag2Light);
  162. vec3 l = normalize(frag2Light);
  163. float spot = computeSpotFactor(l,
  164. light.outerCosInnerCos.x,
  165. light.outerCosInnerCos.y,
  166. light.lightDir.xyz);
  167. #if LOD > 1
  168. const float shadow = 1.0;
  169. #else
  170. float shadow = 1.0;
  171. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  172. if(shadowmapLayerIdx < 128.0)
  173. {
  174. shadow = computeShadowFactorSpot(
  175. light.texProjectionMat, fragPos, shadowmapLayerIdx, 1);
  176. }
  177. #endif
  178. outColor += diffC * (att * spot * shadow);
  179. }
  180. return outColor;
  181. }
  182. #endif
  183. //==============================================================================
  184. #if PASS == COLOR
  185. #define particleTextureAlphaLight_DEFINED
  186. void particleTextureAlphaLight(in sampler2D tex, in float alpha)
  187. {
  188. vec4 color = texture(tex, gl_PointCoord);
  189. color.a *= alpha;
  190. vec3 lightColor = computeLightColor(color.rgb);
  191. writeGBuffer(vec4(lightColor, color.a));
  192. }
  193. #endif
  194. //==============================================================================
  195. #if PASS == COLOR
  196. #define particleAnimatedTextureAlphaLight_DEFINED
  197. void particleAnimatedTextureAlphaLight(
  198. sampler2DArray tex, float alpha, float layerCount, float period)
  199. {
  200. vec4 color = readAnimatedTextureRgba(
  201. tex, layerCount, period, gl_PointCoord, anki_u_time);
  202. color.a *= alpha;
  203. vec3 lightColor = computeLightColor(color.rgb);
  204. writeGBuffer(vec4(lightColor, color.a));
  205. }
  206. #endif
  207. //==============================================================================
  208. #if PASS == COLOR
  209. #define fog_DEFINED
  210. void fog(in sampler2D depthMap, in vec3 color, in float fogScale)
  211. {
  212. const vec2 screenSize = 1.0 / RENDERER_SIZE;
  213. vec2 texCoords = gl_FragCoord.xy * screenSize;
  214. float depth = texture(depthMap, texCoords).r;
  215. float diff;
  216. if(depth < 1.0)
  217. {
  218. float zNear = u_lightingUniforms.nearFarClustererMagicPad1.x;
  219. float zFar = u_lightingUniforms.nearFarClustererMagicPad1.y;
  220. vec2 linearDepths = (2.0 * zNear)
  221. / (zFar + zNear - vec2(depth, gl_FragCoord.z) * (zFar - zNear));
  222. diff = linearDepths.x - linearDepths.y;
  223. }
  224. else
  225. {
  226. // The depth buffer is cleared at this place. Set the diff to zero to
  227. // avoid weird pop ups
  228. diff = 0.0;
  229. }
  230. writeGBuffer(vec4(color, diff * fogScale));
  231. }
  232. #endif