Is.frag.glsl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. vec3 fragPos;
  94. fragPos.z = u_lightingUniforms.projectionParams.z / (u_lightingUniforms.projectionParams.w + depth);
  95. fragPos.xy = ndc * u_lightingUniforms.projectionParams.xy * fragPos.z;
  96. vec3 viewDir = normalize(-fragPos);
  97. // Decode GBuffer
  98. vec3 normal;
  99. vec3 diffCol;
  100. vec3 specCol;
  101. float roughness;
  102. float subsurface;
  103. float emission;
  104. float metallic;
  105. GbufferInfo gbuffer;
  106. readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
  107. diffCol = gbuffer.diffuse;
  108. specCol = gbuffer.specular;
  109. normal = gbuffer.normal;
  110. roughness = gbuffer.roughness;
  111. metallic = gbuffer.metallic;
  112. subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
  113. emission = gbuffer.emission;
  114. // Get SSAO
  115. vec3 worldPos;
  116. {
  117. vec4 worldPos4 = u_lightingUniforms.invViewProjMat * vec4(ndc, UV_TO_NDC(depth), 1.0);
  118. worldPos4 = worldPos4 / worldPos4.w;
  119. worldPos = worldPos4.xyz;
  120. // Project to get old ndc
  121. vec4 oldNdc4 = u_lightingUniforms.prevViewProjMat * worldPos4;
  122. vec2 oldNdc = oldNdc4.xy / oldNdc4.w;
  123. vec2 oldUv = NDC_TO_UV(oldNdc);
  124. float ssao = texture(u_ssaoTex, oldUv).r;
  125. diffCol *= ssao;
  126. }
  127. // Get counts and offsets
  128. uint clusterIdx =
  129. computeClusterK(
  130. u_lightingUniforms.nearFarClustererMagicPad1.x, u_lightingUniforms.nearFarClustererMagicPad1.z, fragPos.z)
  131. * (CLUSTER_COUNT_X * CLUSTER_COUNT_Y)
  132. + uint(in_clusterIJ.y) * CLUSTER_COUNT_X + uint(in_clusterIJ.x);
  133. uint idxOffset = u_clusters[clusterIdx];
  134. // Shadowpass sample count
  135. uint shadowSampleCount = computeShadowSampleCount(SHADOW_SAMPLE_COUNT, fragPos.z);
  136. // Decals
  137. uint count = u_lightIndices[idxOffset++];
  138. while(count-- != 0)
  139. {
  140. Decal decal = u_decals[u_lightIndices[idxOffset++]];
  141. appendDecalColors(decal, fragPos, diffCol, roughness);
  142. }
  143. float a2 = pow(roughness, 2.0);
  144. // Ambient and emissive color
  145. out_color = diffCol * emission;
  146. // Point lights
  147. count = u_lightIndices[idxOffset++];
  148. while(count-- != 0)
  149. {
  150. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  151. LIGHTING_COMMON_BRDF();
  152. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  153. if(light.diffuseColorShadowmapId.w >= 0.0)
  154. {
  155. float shadow = computeShadowFactorOmni(frag2Light,
  156. shadowmapLayerIdx,
  157. light.specularColorRadius.w,
  158. u_lightingUniforms.invViewRotation,
  159. u_omniMapArr);
  160. lambert *= shadow;
  161. }
  162. out_color += (specC + diffC) * (att * max(subsurface, lambert));
  163. }
  164. // Spot lights
  165. count = u_lightIndices[idxOffset++];
  166. while(count-- != 0)
  167. {
  168. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  169. LIGHTING_COMMON_BRDF();
  170. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  171. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  172. if(shadowmapLayerIdx >= 0.0)
  173. {
  174. float shadow = computeShadowFactorSpot(
  175. light.texProjectionMat, fragPos, shadowmapLayerIdx, shadowSampleCount, u_spotMapArr);
  176. lambert *= shadow;
  177. }
  178. out_color += (diffC + specC) * (att * spot * max(subsurface, lambert));
  179. }
  180. #if INDIRECT_ENABLED
  181. vec3 eye = -viewDir;
  182. vec3 worldEye = u_lightingUniforms.invViewRotation * eye;
  183. vec3 worldNormal = u_lightingUniforms.invViewRotation * normal;
  184. vec3 worldR = reflect(worldEye, worldNormal);
  185. float reflLod = float(IR_MIPMAP_COUNT) * roughness;
  186. float ndotv = dot(normal, viewDir);
  187. vec2 envBRDF = texture(u_integrationLut, vec2(roughness, ndotv)).xy;
  188. vec3 specIndirectTerm = specCol * envBRDF.x + envBRDF.y;
  189. vec3 specIndirect, diffIndirect;
  190. readIndirect(idxOffset, worldPos, worldR, worldNormal, reflLod, specIndirect, diffIndirect);
  191. out_color += specIndirect * specIndirectTerm + diffIndirect * diffCol;
  192. #endif
  193. #if 0
  194. count = scount;
  195. if(count == 0)
  196. {
  197. out_color = vec3(0.0, 0.0, 0.0);
  198. }
  199. else if(count == 1)
  200. {
  201. out_color = vec3(1.0, 0.0, 0.0);
  202. }
  203. else if(count == 2)
  204. {
  205. out_color = vec3(0.0, 1.0, 0.0);
  206. }
  207. else if(count == 3)
  208. {
  209. out_color = vec3(0.0, 0.0, 1.0);
  210. }
  211. else
  212. {
  213. out_color = vec3(1.0, 1.0, 1.0);
  214. }
  215. #endif
  216. }