LightShading.ankiprog 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Skip decals
  87. uint count = u_lightIndices[idxOffset];
  88. idxOffset += count + 1u;
  89. // Point lights
  90. vec3 viewDir = normalize(u_cameraPos - worldPos);
  91. count = u_lightIndices[idxOffset++];
  92. uint idxOffsetEnd = idxOffset + count;
  93. ANKI_LOOP while(idxOffset < idxOffsetEnd)
  94. {
  95. PointLight light = u_pointLights[u_lightIndices[idxOffset++]];
  96. LIGHTING_COMMON_BRDF();
  97. if(light.diffuseColorTileSize.w >= 0.0)
  98. {
  99. float shadow = computeShadowFactorOmni(frag2Light,
  100. light.radiusPad1.x,
  101. light.atlasTiles,
  102. light.diffuseColorTileSize.w,
  103. u_shadowTex);
  104. lambert *= shadow;
  105. }
  106. out_color += (diffC + specC) * light.diffuseColorTileSize.rgb * (att * max(gbuffer.subsurface, lambert));
  107. }
  108. // Spot lights
  109. count = u_lightIndices[idxOffset++];
  110. idxOffsetEnd = idxOffset + count;
  111. ANKI_LOOP while(idxOffset < idxOffsetEnd)
  112. {
  113. SpotLight light = u_spotLights[u_lightIndices[idxOffset++]];
  114. LIGHTING_COMMON_BRDF();
  115. float spot = computeSpotFactor(
  116. l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDirRadius.xyz);
  117. float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
  118. if(shadowmapLayerIdx >= 0.0)
  119. {
  120. float shadow = computeShadowFactorSpot(
  121. light.texProjectionMat, worldPos, light.lightDirRadius.w, u_shadowTex);
  122. lambert *= shadow;
  123. }
  124. out_color +=
  125. (diffC + specC) * light.diffuseColorShadowmapId.rgb * (att * spot * max(gbuffer.subsurface, lambert));
  126. }
  127. #if 0
  128. count = scount;
  129. if(count == 0)
  130. {
  131. out_color = vec3(0.0, 0.0, 0.0);
  132. }
  133. else if(count == 1)
  134. {
  135. out_color = vec3(1.0, 0.0, 0.0);
  136. }
  137. else if(count == 2)
  138. {
  139. out_color = vec3(0.0, 1.0, 0.0);
  140. }
  141. else if(count == 3)
  142. {
  143. out_color = vec3(0.0, 0.0, 1.0);
  144. }
  145. else
  146. {
  147. out_color = vec3(1.0, 1.0, 1.0);
  148. }
  149. #endif
  150. }
  151. ]]></source>
  152. </shader>
  153. </shaders>
  154. </shaderProgram>