2
0

Is.ankiprog 8.0 KB

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