FsCommonFrag.glsl 7.8 KB

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