TraditionalDeferredShading.ankiprog 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Classic deferred lighting shader
  6. #pragma anki mutator LIGHT_TYPE 0 1 2
  7. #pragma anki mutator SPECULAR 0 1
  8. #define POINT_LIGHT_TYPE 0
  9. #define SPOT_LIGHT_TYPE 1
  10. #define DIR_LIGHT_TYPE 2
  11. // VERT
  12. #pragma anki start vert
  13. #include <AnKi/Shaders/Common.glsl>
  14. out gl_PerVertex
  15. {
  16. Vec4 gl_Position;
  17. };
  18. #if LIGHT_TYPE == DIR_LIGHT_TYPE
  19. void main()
  20. {
  21. Vec2 uv = Vec2(gl_VertexID & 1, gl_VertexID >> 1) * 2.0;
  22. Vec2 pos = uv * 2.0 - 1.0;
  23. gl_Position = Vec4(pos, 0.0, 1.0);
  24. }
  25. #else
  26. layout(location = 0) in Vec3 in_position;
  27. layout(set = 0, binding = 0, row_major) uniform u0_
  28. {
  29. Mat4 u_mvp;
  30. };
  31. void main()
  32. {
  33. gl_Position = u_mvp * Vec4(in_position, 1.0);
  34. }
  35. #endif
  36. #pragma anki end
  37. // FRAG
  38. #pragma anki start frag
  39. #include <AnKi/Shaders/PackFunctions.glsl>
  40. #include <AnKi/Shaders/LightFunctions.glsl>
  41. #include <AnKi/Shaders/Include/TraditionalDeferredShadingTypes.h>
  42. layout(location = 0) out Vec3 out_color;
  43. layout(set = 0, binding = 1, row_major) uniform u1_
  44. {
  45. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  46. DeferredPointLightUniforms u_unis;
  47. #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
  48. DeferredSpotLightUniforms u_unis;
  49. #elif LIGHT_TYPE == DIR_LIGHT_TYPE
  50. DeferredDirectionalLightUniforms u_unis;
  51. #else
  52. # error See file
  53. #endif
  54. };
  55. layout(set = 0, binding = 2) uniform sampler u_msSampler;
  56. layout(set = 0, binding = 3) uniform texture2D u_msRt0;
  57. layout(set = 0, binding = 4) uniform texture2D u_msRt1;
  58. layout(set = 0, binding = 5) uniform texture2D u_msRt2;
  59. layout(set = 0, binding = 6) uniform texture2D u_msDepthRt;
  60. #if LIGHT_TYPE == DIR_LIGHT_TYPE
  61. layout(set = 0, binding = 7) uniform samplerShadow u_shadowMapSampler;
  62. layout(set = 0, binding = 8) uniform texture2D u_shadowMap;
  63. #endif
  64. void main()
  65. {
  66. // Compute UV coordinates
  67. const Vec2 uvToRead = fma(Vec2(gl_FragCoord.xy), u_unis.m_inputTexUvScale, u_unis.m_inputTexUvBias);
  68. const Vec2 uvToWrite = fma(Vec2(gl_FragCoord.xy), u_unis.m_fbUvScale, u_unis.m_fbUvBias);
  69. const F32 depth = textureLod(u_msDepthRt, u_msSampler, uvToRead, 0.0).r;
  70. #if LIGHT_TYPE != DIR_LIGHT_TYPE
  71. // Do manual depth test
  72. if(gl_FragCoord.z < depth)
  73. {
  74. discard;
  75. }
  76. #endif
  77. // Decode and process gbuffer
  78. GbufferInfo gbuffer;
  79. unpackGBufferNoVelocity(textureLod(u_msRt0, u_msSampler, uvToRead, 0.0),
  80. textureLod(u_msRt1, u_msSampler, uvToRead, 0.0),
  81. textureLod(u_msRt2, u_msSampler, uvToRead, 0.0), gbuffer);
  82. gbuffer.m_subsurface = max(gbuffer.m_subsurface, SUBSURFACE_MIN * 8.0);
  83. const Vec4 worldPos4 = u_unis.m_invViewProjMat * Vec4(UV_TO_NDC(uvToWrite), depth, 1.0);
  84. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  85. // Compute diff
  86. const Vec3 diffC = diffuseLobe(gbuffer.m_diffuse);
  87. // Compute spec
  88. const Vec3 viewDir = normalize(u_unis.m_camPos - worldPos);
  89. #if LIGHT_TYPE == DIR_LIGHT_TYPE
  90. const Vec3 l = -u_unis.m_lightDir;
  91. #else
  92. const Vec3 frag2Light = u_unis.m_position - worldPos;
  93. const Vec3 l = normalize(frag2Light);
  94. const F32 nol = max(0.0, dot(gbuffer.m_normal, l));
  95. #endif
  96. #if SPECULAR == 1
  97. const Vec3 specC = specularIsotropicLobe(gbuffer, viewDir, l);
  98. #else
  99. const Vec3 specC = Vec3(0.0);
  100. #endif
  101. // Compute factors
  102. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  103. const F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
  104. const F32 lambert = nol;
  105. const F32 factor = att * max(lambert, gbuffer.m_subsurface);
  106. #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
  107. const F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
  108. const F32 lambert = nol;
  109. const F32 spot = computeSpotFactor(l, u_unis.m_outerCos, u_unis.m_innerCos, u_unis.m_lightDir);
  110. const F32 factor = att * spot * max(lambert, gbuffer.m_subsurface);
  111. #else
  112. const F32 linearDepth = linearizeDepth(depth, u_unis.m_near, u_unis.m_far);
  113. F32 shadowFactor;
  114. if(linearDepth * (u_unis.m_far - u_unis.m_near) < u_unis.m_effectiveShadowDistance)
  115. {
  116. // Acceptable distance
  117. shadowFactor = computeShadowFactorDirLight(u_unis.m_lightMatrix, worldPos, u_shadowMap, u_shadowMapSampler);
  118. }
  119. else
  120. {
  121. shadowFactor = 1.0;
  122. }
  123. const F32 lambert = dot(l, gbuffer.m_normal);
  124. const F32 factor = shadowFactor * max(gbuffer.m_subsurface, lambert);
  125. #endif
  126. out_color = gbuffer.m_emission;
  127. out_color += (specC + diffC) * u_unis.m_diffuseColor * factor;
  128. }
  129. #pragma anki end