Is.frag.glsl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. uint count = u_lightIndices[idxOffset++];
  136. while(count-- != 0)
  137. {
  138. Decal decal = u_decals[u_lightIndices[idxOffset++]];
  139. appendDecalColors(decal, fragPos, diffCol, roughness);
  140. }
  141. // Don't allow zero a2 because we may end up with division with zero
  142. float a2 = roughness * 0.9 + 0.1;
  143. a2 *= a2;
  144. // Ambient and emissive color
  145. vec3 outC = diffCol * emission;
  146. // Lights
  147. count = u_lightIndices[idxOffset++];
  148. while(count-- != 0)
  149. {
  150. Light light = u_lights[u_lightIndices[idxOffset++]];
  151. LIGHTING_COMMON_BRDF();
  152. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  153. if(isSpotLight(light))
  154. {
  155. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  156. att *= spot;
  157. if(shadowmapLayerIdx >= 0.0)
  158. {
  159. float shadow = computeShadowFactorSpot(
  160. light.texProjectionMat, fragPos, shadowmapLayerIdx, shadowSampleCount, u_spotMapArr);
  161. lambert *= shadow;
  162. }
  163. }
  164. else
  165. {
  166. if(light.diffuseColorShadowmapId.w >= 0.0)
  167. {
  168. float shadow = computeShadowFactorOmni(
  169. frag2Light, shadowmapLayerIdx, light.specularColorRadius.w, u_invViewRotation, u_omniMapArr);
  170. lambert *= shadow;
  171. }
  172. }
  173. outC += (specC + diffC) * (att * max(subsurface, lambert));
  174. }
  175. #if INDIRECT_ENABLED
  176. vec3 eye = -viewDir;
  177. vec3 worldEye = u_invViewRotation * eye;
  178. vec3 worldNormal = u_invViewRotation * normal;
  179. vec3 worldR = reflect(worldEye, worldNormal);
  180. float reflLod = float(IR_MIPMAP_COUNT) * roughness;
  181. float ndotv = dot(normal, viewDir);
  182. vec2 envBRDF = texture(u_integrationLut, vec2(roughness, ndotv)).xy;
  183. vec3 specIndirectTerm = specCol * envBRDF.x + envBRDF.y;
  184. vec3 specIndirect, diffIndirect;
  185. readIndirect(idxOffset, worldPos, worldR, worldNormal, reflLod, specIndirect, diffIndirect);
  186. outC += specIndirect * specIndirectTerm + diffIndirect * diffCol;
  187. #endif
  188. out_color = outC;
  189. #if 0
  190. count = scount;
  191. if(count == 0)
  192. {
  193. out_color = vec3(0.0, 0.0, 0.0);
  194. }
  195. else if(count == 1)
  196. {
  197. out_color = vec3(1.0, 0.0, 0.0);
  198. }
  199. else if(count == 2)
  200. {
  201. out_color = vec3(0.0, 1.0, 0.0);
  202. }
  203. else if(count == 3)
  204. {
  205. out_color = vec3(0.0, 0.0, 1.0);
  206. }
  207. else
  208. {
  209. out_color = vec3(1.0, 1.0, 1.0);
  210. }
  211. #endif
  212. }