FsCommonFrag.glsl 6.4 KB

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