LightShading.ankiprog 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l); \
  60. vec3 diffC = diffuseLambert(gbuffer.diffuse); \
  61. float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
  62. float lambert = max(0.0, dot(gbuffer.normal, l));
  63. void main()
  64. {
  65. float depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
  66. vec2 ndc = UV_TO_NDC(in_uv);
  67. // Get world position
  68. vec4 worldPos4 = u_invViewProjMat * vec4(ndc, depth, 1.0);
  69. vec3 worldPos = worldPos4.xyz / worldPos4.w;
  70. // Get first light index
  71. uint idxOffset;
  72. {
  73. uint k = computeClusterK(u_clustererMagic, worldPos);
  74. uint clusterIdx =
  75. k * (CLUSTER_COUNT_X * CLUSTER_COUNT_Y) + uint(in_clusterIJ.y) * CLUSTER_COUNT_X + uint(in_clusterIJ.x);
  76. idxOffset = u_clusters[clusterIdx];
  77. }
  78. // Decode GBuffer
  79. GbufferInfo gbuffer;
  80. readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
  81. gbuffer.subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
  82. // Ambient and emissive color
  83. out_color = gbuffer.diffuse * gbuffer.emission;
  84. // Indirect
  85. out_color += textureLod(u_indirectRt, in_uv, 0.0).rgb;
  86. return;
  87. // Skip decals
  88. uint count = u_lightIndices[idxOffset];
  89. idxOffset += count + 1u;
  90. // Point lights
  91. vec3 viewDir = normalize(u_cameraPos - worldPos);
  92. count = u_lightIndices[idxOffset++];
  93. uint idxOffsetEnd = idxOffset + count;
  94. ANKI_LOOP while(idxOffset < idxOffsetEnd)
  95. {
  96. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  97. LIGHTING_COMMON_BRDF();
  98. if(light.diffuseColorTileSize.w >= 0.0)
  99. {
  100. float shadow = computeShadowFactorOmni(frag2Light,
  101. light.radiusPad1.x,
  102. light.atlasTiles,
  103. light.diffuseColorTileSize.w,
  104. u_shadowTex);
  105. lambert *= shadow;
  106. }
  107. out_color += (diffC + specC) * light.diffuseColorTileSize.rgb * (att * max(gbuffer.subsurface, lambert));
  108. }
  109. // Spot lights
  110. count = u_lightIndices[idxOffset++];
  111. idxOffsetEnd = idxOffset + count;
  112. ANKI_LOOP while(idxOffset < idxOffsetEnd)
  113. {
  114. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  115. LIGHTING_COMMON_BRDF();
  116. float spot = computeSpotFactor(
  117. l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDirRadius.xyz);
  118. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  119. if(shadowmapLayerIdx >= 0.0)
  120. {
  121. float shadow = computeShadowFactorSpot(
  122. light.texProjectionMat, worldPos, light.lightDirRadius.w, u_shadowTex);
  123. lambert *= shadow;
  124. }
  125. out_color +=
  126. (diffC + specC) * light.diffuseColorShadowmapId.rgb * (att * spot * max(gbuffer.subsurface, lambert));
  127. }
  128. #if 0
  129. count = scount;
  130. if(count == 0)
  131. {
  132. out_color = vec3(0.0, 0.0, 0.0);
  133. }
  134. else if(count == 1)
  135. {
  136. out_color = vec3(1.0, 0.0, 0.0);
  137. }
  138. else if(count == 2)
  139. {
  140. out_color = vec3(0.0, 1.0, 0.0);
  141. }
  142. else if(count == 3)
  143. {
  144. out_color = vec3(0.0, 0.0, 1.0);
  145. }
  146. else
  147. {
  148. out_color = vec3(1.0, 1.0, 1.0);
  149. }
  150. #endif
  151. }
  152. ]]></source>
  153. </shader>
  154. </shaders>
  155. </shaderProgram>