Is.frag.glsl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include "shaders/Pack.glsl"
  6. #include "shaders/Clusterer.glsl"
  7. #include "shaders/Functions.glsl"
  8. #define LIGHT_SET 0
  9. #define LIGHT_SS_BINDING 0
  10. #define LIGHT_UBO_BINDING 0
  11. #define LIGHT_TEX_BINDING 4
  12. #define LIGHT_INDIRECT
  13. #define LIGHT_DECALS
  14. #include "shaders/ClusterLightCommon.glsl"
  15. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
  16. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
  17. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
  18. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
  19. layout(ANKI_TEX_BINDING(1, 0)) uniform sampler2D u_diffDecalTex;
  20. layout(ANKI_TEX_BINDING(1, 1)) uniform sampler2D u_normalRoughnessDecalTex;
  21. layout(ANKI_TEX_BINDING(1, 2)) uniform sampler2D u_ssaoTex;
  22. layout(location = 0) in vec2 in_uv;
  23. layout(location = 1) in vec2 in_clusterIJ;
  24. layout(location = 0) out vec3 out_color;
  25. const float SUBSURFACE_MIN = 0.05;
  26. // Common code for lighting
  27. #define LIGHTING_COMMON_BRDF() \
  28. vec3 frag2Light = light.posRadius.xyz - fragPos; \
  29. vec3 l = normalize(frag2Light); \
  30. float nol = max(0.0, dot(normal, l)); \
  31. vec3 specC = computeSpecularColorBrdf(viewDir, l, normal, specCol, light.specularColorRadius.rgb, a2, nol); \
  32. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb); \
  33. float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
  34. float lambert = nol;
  35. void debugIncorrectColor(inout vec3 c)
  36. {
  37. if(isnan(c.x) || isnan(c.y) || isnan(c.z) || isinf(c.x) || isinf(c.y) || isinf(c.z))
  38. {
  39. c = vec3(1.0, 0.0, 1.0);
  40. }
  41. }
  42. // Compute the colors of a decal.
  43. void appendDecalColors(in Decal decal, in vec3 fragPos, inout vec3 diffuseColor, inout float roughness)
  44. {
  45. vec4 texCoords4 = decal.texProjectionMat * vec4(fragPos, 1.0);
  46. vec2 texCoords2 = texCoords4.xy / texCoords4.w;
  47. // Clamp the tex coords. Expect a border in the texture atlas
  48. texCoords2 = clamp(texCoords2, 0.0, 1.0);
  49. vec2 diffUv = texCoords2 * decal.diffUv.zw + decal.diffUv.xy;
  50. vec4 dcol = texture(u_diffDecalTex, diffUv);
  51. diffuseColor = mix(diffuseColor, dcol.rgb, dcol.a * decal.blendFactors[0]);
  52. // Roughness
  53. vec2 roughnessUv = texCoords2 * decal.normRoughnessUv.zw + decal.normRoughnessUv.xy;
  54. float r = texture(u_normalRoughnessDecalTex, roughnessUv).w;
  55. roughness = mix(roughness, r, dcol.a * decal.blendFactors[1]);
  56. }
  57. void readIndirect(
  58. in uint idxOffset, in vec3 pos, in vec3 r, in vec3 n, in float lod, out vec3 specIndirect, out vec3 diffIndirect)
  59. {
  60. specIndirect = vec3(0.0);
  61. diffIndirect = vec3(0.0);
  62. // Check proxy
  63. uint count = u_lightIndices[idxOffset++];
  64. while(count-- != 0)
  65. {
  66. ReflectionProbe probe = u_reflectionProbes[u_lightIndices[idxOffset++]];
  67. float R2 = probe.positionRadiusSq.w;
  68. vec3 center = probe.positionRadiusSq.xyz;
  69. // Get distance from the center of the probe
  70. vec3 f = pos - center;
  71. // Cubemap UV in view space
  72. vec3 uv = computeCubemapVecAccurate(r, R2, f);
  73. // Read!
  74. float cubemapIndex = probe.cubemapIndexPad3.x;
  75. vec3 c = textureLod(u_reflectionsTex, vec4(uv, cubemapIndex), lod).rgb;
  76. // Combine (lerp) with previous color
  77. float d = dot(f, f);
  78. float factor = d / R2;
  79. factor = min(factor, 1.0);
  80. specIndirect = mix(c, specIndirect, factor);
  81. // Same as: specIndirect = c * (1.0 - factor) + specIndirect * factor
  82. // Do the same for diffuse
  83. uv = computeCubemapVecCheap(n, R2, f);
  84. vec3 id = texture(u_irradianceTex, vec4(uv, cubemapIndex)).rgb;
  85. diffIndirect = mix(id, diffIndirect, factor);
  86. }
  87. }
  88. void main()
  89. {
  90. float depth = texture(u_msDepthRt, in_uv, 0.0).r;
  91. vec2 ndc = UV_TO_NDC(in_uv);
  92. // Get frag pos in view space
  93. vec4 fragPos4 = u_invProjMat * vec4(ndc, UV_TO_NDC(depth), 1.0);
  94. vec3 fragPos = fragPos4.xyz / fragPos4.w;
  95. vec3 viewDir = normalize(-fragPos);
  96. // Get world position
  97. vec3 worldPos;
  98. vec2 oldUv;
  99. {
  100. vec4 worldPos4 = u_invViewProjMat * vec4(ndc, UV_TO_NDC(depth), 1.0);
  101. worldPos4 = worldPos4 / worldPos4.w;
  102. worldPos = worldPos4.xyz;
  103. // Project to get old ndc
  104. vec4 oldNdc4 = u_prevViewProjMat * vec4(worldPos, 1.0);
  105. vec2 oldNdc = oldNdc4.xy / oldNdc4.w;
  106. oldUv = NDC_TO_UV(oldNdc);
  107. }
  108. // Decode GBuffer
  109. vec3 normal;
  110. vec3 diffCol;
  111. vec3 specCol;
  112. float roughness;
  113. float subsurface;
  114. float emission;
  115. float metallic;
  116. GbufferInfo gbuffer;
  117. readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
  118. diffCol = gbuffer.diffuse;
  119. specCol = gbuffer.specular;
  120. normal = gbuffer.normal;
  121. roughness = gbuffer.roughness;
  122. metallic = gbuffer.metallic;
  123. subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
  124. emission = gbuffer.emission;
  125. // Get SSAO
  126. float ssao = texture(u_ssaoTex, oldUv).r;
  127. diffCol *= ssao;
  128. // Get counts and offsets
  129. uint clusterIdx = computeClusterK(u_near, u_clustererMagic, fragPos.z) * (CLUSTER_COUNT_X * CLUSTER_COUNT_Y)
  130. + uint(in_clusterIJ.y) * CLUSTER_COUNT_X + uint(in_clusterIJ.x);
  131. uint idxOffset = u_clusters[clusterIdx];
  132. // Shadowpass sample count
  133. uint shadowSampleCount = computeShadowSampleCount(SHADOW_SAMPLE_COUNT, fragPos.z);
  134. // Decals
  135. #if 1
  136. uint count = u_lightIndices[idxOffset++];
  137. while(count-- != 0)
  138. {
  139. Decal decal = u_decals[u_lightIndices[idxOffset++]];
  140. appendDecalColors(decal, fragPos, diffCol, roughness);
  141. }
  142. #else
  143. uint count = u_lightIndices[idxOffset];
  144. idxOffset += count;
  145. #endif
  146. // Don't allow zero a2 because we may end up with division with zero
  147. float a2 = roughness * 0.9 + 0.1;
  148. a2 *= a2;
  149. // Ambient and emissive color
  150. vec3 outC = diffCol * emission;
  151. // Point lights
  152. #if PERMUTATION & 1
  153. count = u_lightIndices[idxOffset++];
  154. while(count-- != 0)
  155. {
  156. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  157. LIGHTING_COMMON_BRDF();
  158. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  159. if(light.diffuseColorShadowmapId.w >= 0.0)
  160. {
  161. float shadow = computeShadowFactorOmni(
  162. frag2Light, shadowmapLayerIdx, light.specularColorRadius.w, u_invViewRotation, u_omniMapArr);
  163. lambert *= shadow;
  164. }
  165. outC += (specC + diffC) * (att * max(subsurface, lambert));
  166. }
  167. #else
  168. count = u_lightIndices[idxOffset];
  169. idxOffset += count + 1;
  170. #endif
  171. // Spot lights
  172. #if PERMUTATION & 2
  173. count = u_lightIndices[idxOffset++];
  174. while(count-- != 0)
  175. {
  176. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  177. LIGHTING_COMMON_BRDF();
  178. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  179. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  180. if(shadowmapLayerIdx >= 0.0)
  181. {
  182. float shadow = computeShadowFactorSpot(
  183. light.texProjectionMat, fragPos, shadowmapLayerIdx, shadowSampleCount, u_spotMapArr);
  184. lambert *= shadow;
  185. }
  186. outC += (diffC + specC) * (att * spot * max(subsurface, lambert));
  187. }
  188. #else
  189. count = u_lightIndices[idxOffset];
  190. idxOffset += count + 1;
  191. #endif
  192. #if INDIRECT_ENABLED
  193. vec3 eye = -viewDir;
  194. vec3 worldEye = u_invViewRotation * eye;
  195. vec3 worldNormal = u_invViewRotation * normal;
  196. vec3 worldR = reflect(worldEye, worldNormal);
  197. float reflLod = float(IR_MIPMAP_COUNT) * roughness;
  198. float ndotv = dot(normal, viewDir);
  199. vec2 envBRDF = texture(u_integrationLut, vec2(roughness, ndotv)).xy;
  200. vec3 specIndirectTerm = specCol * envBRDF.x + envBRDF.y;
  201. vec3 specIndirect, diffIndirect;
  202. readIndirect(idxOffset, worldPos, worldR, worldNormal, reflLod, specIndirect, diffIndirect);
  203. outC += specIndirect * specIndirectTerm + diffIndirect * diffCol;
  204. #endif
  205. out_color = outC;
  206. #if 0
  207. out_color = vec3(((PERMUTATION & 2) != 0) ? 1.0 : 0.0, ((PERMUTATION & 1) != 0) ? 1.0 : 0.0, 0.0);
  208. #endif
  209. #if 0
  210. count = scount;
  211. if(count == 0)
  212. {
  213. out_color = vec3(0.0, 0.0, 0.0);
  214. }
  215. else if(count == 1)
  216. {
  217. out_color = vec3(1.0, 0.0, 0.0);
  218. }
  219. else if(count == 2)
  220. {
  221. out_color = vec3(0.0, 1.0, 0.0);
  222. }
  223. else if(count == 3)
  224. {
  225. out_color = vec3(0.0, 0.0, 1.0);
  226. }
  227. else
  228. {
  229. out_color = vec3(1.0, 1.0, 1.0);
  230. }
  231. #endif
  232. }