LightShading.ankiprog 7.8 KB

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