LightShading.ankiprog 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!--
  2. Copyright (C) 2009-2018, 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_Z" type="uint" const="1"/>
  12. <input name="CLUSTER_COUNT" type="uint" const="1"/>
  13. <input name="IR_MIPMAP_COUNT" type="uint" const="1"/>
  14. </inputs>
  15. <shaders>
  16. <shader type="vert">
  17. <source><![CDATA[
  18. #include "shaders/Common.glsl"
  19. layout(location = 0) out vec2 out_uv;
  20. layout(location = 1) out vec2 out_clusterIJ;
  21. out gl_PerVertex
  22. {
  23. vec4 gl_Position;
  24. };
  25. void main()
  26. {
  27. out_uv = vec2(gl_VertexID & 1, gl_VertexID >> 1) * 2.0;
  28. vec2 pos = out_uv * 2.0 - 1.0;
  29. gl_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 5
  43. #define LIGHT_LIGHTS
  44. #define LIGHT_COMMON_UNIS
  45. #include "shaders/ClusterLightCommon.glsl"
  46. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
  47. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
  48. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
  49. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
  50. layout(ANKI_TEX_BINDING(0, 4)) uniform sampler2D u_indirectRt;
  51. layout(location = 0) in vec2 in_uv;
  52. layout(location = 1) in vec2 in_clusterIJ;
  53. layout(location = 0) out vec3 out_color;
  54. const float SUBSURFACE_MIN = 0.05;
  55. // Common code for lighting
  56. #define LIGHTING_COMMON_BRDF() \
  57. vec3 frag2Light = light.posRadius.xyz - worldPos; \
  58. vec3 l = normalize(frag2Light); \
  59. float nol = max(0.0, dot(normal, l)); \
  60. vec3 specC = computeSpecularColorBrdf(viewDir, l, normal, specCol, light.specularColorRadius.rgb, a2, nol); \
  61. vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb); \
  62. float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
  63. float lambert = nol;
  64. void main()
  65. {
  66. float depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
  67. vec2 ndc = UV_TO_NDC(in_uv);
  68. // Get world position
  69. vec4 worldPos4 = u_invViewProjMat * vec4(ndc, depth, 1.0);
  70. vec3 worldPos = worldPos4.xyz / worldPos4.w;
  71. // Decode GBuffer
  72. vec3 normal;
  73. vec3 diffCol;
  74. vec3 specCol;
  75. float roughness;
  76. float subsurface;
  77. float emission;
  78. float metallic;
  79. GbufferInfo gbuffer;
  80. readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
  81. diffCol = gbuffer.diffuse;
  82. specCol = gbuffer.specular;
  83. normal = gbuffer.normal;
  84. roughness = gbuffer.roughness;
  85. metallic = gbuffer.metallic;
  86. subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
  87. emission = gbuffer.emission;
  88. // Get first light index
  89. uint idxOffset;
  90. {
  91. uint k = computeClusterK(u_clustererMagic, worldPos);
  92. uint clusterIdx =
  93. k * (CLUSTER_COUNT_X * CLUSTER_COUNT_Y) + uint(in_clusterIJ.y) * CLUSTER_COUNT_X + uint(in_clusterIJ.x);
  94. idxOffset = u_clusters[clusterIdx];
  95. }
  96. // Skip decals
  97. uint count = u_lightIndices[idxOffset++];
  98. idxOffset += count;
  99. // Ambient and emissive color
  100. vec3 outC = diffCol * emission;
  101. // Don't allow zero a2 because we may end up with division with zero
  102. float a2 = computeRoughnesSquared(roughness);
  103. // Point lights
  104. vec3 viewDir = normalize(u_cameraPos - worldPos);
  105. count = u_lightIndices[idxOffset++];
  106. while(count-- != 0)
  107. {
  108. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  109. LIGHTING_COMMON_BRDF();
  110. if(light.diffuseColorShadowmapId.w >= 0.0)
  111. {
  112. float shadow = computeShadowFactorOmni(frag2Light,
  113. light.specularColorRadius.w,
  114. light.atlasTilesPad2.xy,
  115. light.diffuseColorShadowmapId.w,
  116. u_shadowTex);
  117. lambert *= shadow;
  118. }
  119. outC += (diffC + specC) * (att * max(subsurface, lambert));
  120. }
  121. // Spot lights
  122. count = u_lightIndices[idxOffset++];
  123. while(count-- != 0)
  124. {
  125. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  126. LIGHTING_COMMON_BRDF();
  127. float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
  128. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  129. if(shadowmapLayerIdx >= 0.0)
  130. {
  131. float shadow = computeShadowFactorSpot(
  132. light.texProjectionMat, worldPos, light.specularColorRadius.w, u_shadowTex);
  133. lambert *= shadow;
  134. }
  135. outC += (diffC + specC) * (att * spot * max(subsurface, lambert));
  136. }
  137. // Indirect
  138. outC += textureLod(u_indirectRt, in_uv, 0.0).rgb;
  139. out_color = outC;
  140. #if 0
  141. count = scount;
  142. if(count == 0)
  143. {
  144. out_color = vec3(0.0, 0.0, 0.0);
  145. }
  146. else if(count == 1)
  147. {
  148. out_color = vec3(1.0, 0.0, 0.0);
  149. }
  150. else if(count == 2)
  151. {
  152. out_color = vec3(0.0, 1.0, 0.0);
  153. }
  154. else if(count == 3)
  155. {
  156. out_color = vec3(0.0, 0.0, 1.0);
  157. }
  158. else
  159. {
  160. out_color = vec3(1.0, 1.0, 1.0);
  161. }
  162. #endif
  163. }
  164. ]]></source>
  165. </shader>
  166. </shaders>
  167. </shaderProgram>