TraditionalDeferredShading.ankiprog 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2009-2020, 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 <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 <shaders/Pack.glsl>
  40. #include <shaders/LightFunctions.glsl>
  41. #include <shaders/glsl_cpp_common/TraditionalDeferredShading.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. readGBuffer(u_msRt0, u_msRt1, u_msRt2, u_msSampler, uvToRead, 0.0, gbuffer);
  80. gbuffer.m_subsurface = max(gbuffer.m_subsurface, SUBSURFACE_MIN * 8.0);
  81. const Vec4 worldPos4 = u_unis.m_invViewProjMat * Vec4(UV_TO_NDC(uvToWrite), depth, 1.0);
  82. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  83. // Compute diff
  84. const Vec3 diffC = diffuseLambert(gbuffer.m_diffuse);
  85. // Compute spec
  86. const Vec3 viewDir = normalize(u_unis.m_camPos - worldPos);
  87. #if LIGHT_TYPE == DIR_LIGHT_TYPE
  88. const Vec3 l = -u_unis.m_lightDir;
  89. #else
  90. const Vec3 frag2Light = u_unis.m_position - worldPos;
  91. const Vec3 l = normalize(frag2Light);
  92. const F32 nol = max(0.0, dot(gbuffer.m_normal, l));
  93. #endif
  94. #if SPECULAR == 1
  95. const Vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l);
  96. #else
  97. const Vec3 specC = Vec3(0.0);
  98. #endif
  99. // Compute factors
  100. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  101. const F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
  102. const F32 lambert = nol;
  103. const F32 factor = att * max(lambert, gbuffer.m_subsurface);
  104. #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
  105. const F32 att = computeAttenuationFactor(u_unis.m_oneOverSquareRadius, frag2Light);
  106. const F32 lambert = nol;
  107. const F32 spot = computeSpotFactor(l, u_unis.m_outerCos, u_unis.m_innerCos, u_unis.m_lightDir);
  108. const F32 factor = att * spot * max(lambert, gbuffer.m_subsurface);
  109. #else
  110. const F32 linearDepth = linearizeDepth(depth, u_unis.m_near, u_unis.m_far);
  111. F32 shadowFactor;
  112. if(linearDepth * (u_unis.m_far - u_unis.m_near) < u_unis.m_effectiveShadowDistance)
  113. {
  114. // Acceptable distance
  115. shadowFactor = computeShadowFactorDirLight(u_unis.m_lightMatrix, worldPos, u_shadowMap, u_shadowMapSampler);
  116. }
  117. else
  118. {
  119. shadowFactor = 1.0;
  120. }
  121. const F32 lambert = dot(l, gbuffer.m_normal);
  122. const F32 factor = shadowFactor * max(gbuffer.m_subsurface, lambert);
  123. #endif
  124. out_color = gbuffer.m_diffuse * gbuffer.m_emission;
  125. out_color += (specC + diffC) * u_unis.m_diffuseColor * factor;
  126. }
  127. #pragma anki end