Is.frag.glsl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #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(location = 0) in vec2 in_texCoord;
  22. layout(location = 1) flat in int in_instanceId;
  23. layout(location = 2) in vec2 in_projectionParams;
  24. layout(location = 0) out vec3 out_color;
  25. const uint TILE_COUNT = TILE_COUNT_X * TILE_COUNT_Y;
  26. const float SUBSURFACE_MIN = 0.05;
  27. // Return frag pos in view space
  28. vec3 getFragPosVSpace()
  29. {
  30. float depth = texture(u_msDepthRt, in_texCoord, 0.0).r;
  31. vec3 fragPos;
  32. fragPos.z = u_lightingUniforms.projectionParams.z / (u_lightingUniforms.projectionParams.w + depth);
  33. fragPos.xy = in_projectionParams * fragPos.z;
  34. return fragPos;
  35. }
  36. // Common code for lighting
  37. #define LIGHTING_COMMON_BRDF() \
  38. vec3 frag2Light = light.posRadius.xyz - fragPos; \
  39. vec3 l = normalize(frag2Light); \
  40. float nol = max(0.0, dot(normal, l)); \
  41. vec3 specC = computeSpecularColorBrdf(viewDir, l, normal, specCol, light.specularColorTexId.rgb, a2, nol); \
  42. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb); \
  43. float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
  44. float lambert = nol;
  45. void debugIncorrectColor(inout vec3 c)
  46. {
  47. if(isnan(c.x) || isnan(c.y) || isnan(c.z) || isinf(c.x) || isinf(c.y) || isinf(c.z))
  48. {
  49. c = vec3(1.0, 0.0, 1.0);
  50. }
  51. }
  52. // Compute the colors of a decal.
  53. void appendDecalColors(in Decal decal, in vec3 fragPos, inout vec3 diffuseColor, inout float roughness)
  54. {
  55. vec4 texCoords4 = decal.texProjectionMat * vec4(fragPos, 1.0);
  56. vec2 texCoords2 = texCoords4.xy / texCoords4.w;
  57. // Clamp the tex coords. Expect a border in the texture atlas
  58. texCoords2 = clamp(texCoords2, 0.0, 1.0);
  59. vec2 diffUv = texCoords2 * decal.diffUv.zw + decal.diffUv.xy;
  60. vec4 dcol = texture(u_diffDecalTex, diffUv);
  61. diffuseColor = mix(diffuseColor, dcol.rgb, dcol.a * decal.blendFactors[0]);
  62. // Roughness
  63. vec2 roughnessUv = texCoords2 * decal.normRoughnessUv.zw + decal.normRoughnessUv.xy;
  64. float r = texture(u_normalRoughnessDecalTex, roughnessUv).w;
  65. roughness = mix(roughness, r, dcol.a * decal.blendFactors[1]);
  66. }
  67. void readIndirect(in uint idxOffset,
  68. in vec3 posVSpace,
  69. in vec3 r,
  70. in vec3 n,
  71. in float lod,
  72. out vec3 specIndirect,
  73. out vec3 diffIndirect)
  74. {
  75. specIndirect = vec3(0.0);
  76. diffIndirect = vec3(0.0);
  77. // Check proxy
  78. uint count = u_lightIndices[idxOffset++];
  79. while(count-- != 0)
  80. {
  81. ReflectionProbe probe = u_reflectionProbes[u_lightIndices[idxOffset++]];
  82. float R2 = probe.positionRadiusSq.w;
  83. vec3 center = probe.positionRadiusSq.xyz;
  84. // Get distance from the center of the probe
  85. vec3 f = posVSpace - center;
  86. // Cubemap UV in view space
  87. vec3 uv = computeCubemapVecAccurate(r, R2, f, u_lightingUniforms.invViewRotation);
  88. // Read!
  89. float cubemapIndex = probe.cubemapIndexPad3.x;
  90. vec3 c = textureLod(u_reflectionsTex, vec4(uv, cubemapIndex), lod).rgb;
  91. // Combine (lerp) with previous color
  92. float d = dot(f, f);
  93. float factor = d / R2;
  94. factor = min(factor, 1.0);
  95. specIndirect = mix(c, specIndirect, factor);
  96. // Same as: specIndirect = c * (1.0 - factor) + specIndirect * factor
  97. // Do the same for diffuse
  98. uv = computeCubemapVecCheap(n, R2, f, u_lightingUniforms.invViewRotation);
  99. vec3 id = texture(u_irradianceTex, vec4(uv, cubemapIndex)).rgb;
  100. diffIndirect = mix(id, diffIndirect, factor);
  101. }
  102. }
  103. void main()
  104. {
  105. // Get frag pos in view space
  106. vec3 fragPos = getFragPosVSpace();
  107. vec3 viewDir = normalize(-fragPos);
  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_texCoord, 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 counts and offsets
  126. uint clusterIdx = computeClusterIndexUsingTileIdx(u_lightingUniforms.nearFarClustererMagicPad1.x,
  127. u_lightingUniforms.nearFarClustererMagicPad1.z,
  128. fragPos.z,
  129. in_instanceId,
  130. TILE_COUNT_X,
  131. TILE_COUNT_Y);
  132. uint idxOffset = u_clusters[clusterIdx];
  133. uint idx;
  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 shadow = 1.0;
  153. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  154. if(light.diffuseColorShadowmapId.w < 128.0)
  155. {
  156. shadow = computeShadowFactorOmni(
  157. frag2Light, shadowmapLayerIdx, 1.0 / sqrt(light.posRadius.w), u_lightingUniforms.viewMat, u_omniMapArr);
  158. }
  159. out_color += (specC + diffC) * (att * max(subsurface, lambert * shadow));
  160. }
  161. // Spot lights
  162. count = u_lightIndices[idxOffset++];
  163. while(count-- != 0)
  164. {
  165. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  166. LIGHTING_COMMON_BRDF();
  167. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  168. float shadow = 1.0;
  169. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  170. if(shadowmapLayerIdx < 128.0)
  171. {
  172. shadow = computeShadowFactorSpot(
  173. light.texProjectionMat, fragPos, shadowmapLayerIdx, shadowSampleCount, u_spotMapArr);
  174. }
  175. out_color += (diffC + specC) * (att * spot * max(subsurface, lambert * shadow));
  176. }
  177. #if INDIRECT_ENABLED
  178. vec3 eye = -viewDir;
  179. vec3 r = reflect(eye, normal);
  180. float reflLod = float(IR_MIPMAP_COUNT) * roughness;
  181. vec3 specIndirect, diffIndirect;
  182. readIndirect(idxOffset, fragPos, r, normal, reflLod, specIndirect, diffIndirect);
  183. diffIndirect *= diffCol;
  184. // Finalize the indirect specular
  185. float ndotv = dot(normal, viewDir);
  186. vec2 envBRDF = texture(u_integrationLut, vec2(roughness, ndotv)).xy;
  187. specIndirect = specIndirect * (specCol * envBRDF.x + envBRDF.y);
  188. out_color += specIndirect + diffIndirect;
  189. #endif
  190. #if 0
  191. count = scount;
  192. if(count == 0)
  193. {
  194. out_color = vec3(0.0, 0.0, 0.0);
  195. }
  196. else if(count == 1)
  197. {
  198. out_color = vec3(1.0, 0.0, 0.0);
  199. }
  200. else if(count == 2)
  201. {
  202. out_color = vec3(0.0, 1.0, 0.0);
  203. }
  204. else if(count == 3)
  205. {
  206. out_color = vec3(0.0, 0.0, 1.0);
  207. }
  208. else
  209. {
  210. out_color = vec3(1.0, 1.0, 1.0);
  211. }
  212. #endif
  213. }