DeferredShading.ankiprog 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. <!-- Classic deferred lighting shader -->
  8. <shaderProgram>
  9. <mutators>
  10. <mutator name="LIGHT_TYPE" values="0 1"/>
  11. </mutators>
  12. <shaders>
  13. <shader type="vert">
  14. <source><![CDATA[
  15. #include "shaders/Common.glsl"
  16. layout(location = 0) in vec3 in_position;
  17. out gl_PerVertex
  18. {
  19. vec4 gl_Position;
  20. };
  21. layout(ANKI_UBO_BINDING(0, 0), row_major) uniform u0_
  22. {
  23. mat4 u_mvp;
  24. };
  25. void main()
  26. {
  27. gl_Position = u_mvp * vec4(in_position, 1.0);
  28. }
  29. ]]></source>
  30. </shader>
  31. <shader type="frag">
  32. <inputs>
  33. <input name="FB_SIZE" type="uvec2" const="1" />
  34. </inputs>
  35. <source><![CDATA[
  36. #include "shaders/Pack.glsl"
  37. #include "shaders/LightFunctions.glsl"
  38. #define POINT_LIGHT_TYPE 0
  39. #define SPOT_LIGHT_TYPE 1
  40. layout(location = 0) out vec3 out_color;
  41. // Point light
  42. struct PointLight
  43. {
  44. vec4 posRadius; // xyz: Light pos in world space. w: The -1/radius
  45. vec4 diffuseColorPad1; // xyz: diff color
  46. vec4 specularColorPad1; // xyz: spec color
  47. };
  48. // Spot light
  49. struct SpotLight
  50. {
  51. vec4 posRadius; // xyz: Light pos in world space. w: The -1/radius
  52. vec4 diffuseColorOuterCos; // xyz: diff color, w: outer cosine of spot
  53. vec4 specularColorInnerCos; // xyz: spec color, w: inner cosine of spot
  54. vec4 lightDirPad1;
  55. };
  56. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
  57. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
  58. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
  59. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
  60. layout(ANKI_UBO_BINDING(0, 1), row_major) uniform u1_
  61. {
  62. vec4 u_inputTexUvScaleAndOffset; // Use this to get the correct face UVs
  63. mat4 u_invViewProjMat;
  64. vec4 u_camPosPad1;
  65. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  66. PointLight u_light;
  67. #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
  68. SpotLight u_light;
  69. #else
  70. #error See file
  71. #endif
  72. };
  73. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  74. #define u_ldiff u_light.diffuseColorPad1.xyz
  75. #define u_lspec u_light.specularColorPad1.xyz
  76. #else
  77. #define u_ldiff u_light.diffuseColorOuterCos.xyz
  78. #define u_lspec u_light.specularColorInnerCos.xyz
  79. #endif
  80. #define u_camPos u_camPosPad1.xyz
  81. void main()
  82. {
  83. // Compute UV coordinates
  84. vec2 uv = vec2(gl_FragCoord.xy) / vec2(FB_SIZE.x, FB_SIZE.y);
  85. vec2 uvToRead = fma(uv, u_inputTexUvScaleAndOffset.xy, u_inputTexUvScaleAndOffset.zw);
  86. // Do manual depth test
  87. float depth = texture(u_msDepthRt, uvToRead).r;
  88. if(gl_FragCoord.z < depth)
  89. {
  90. discard;
  91. }
  92. // Decode and process gbuffer
  93. GbufferInfo gbuffer;
  94. readGBuffer(u_msRt0, u_msRt1, u_msRt2, uvToRead, 0.0, gbuffer);
  95. float a2 = pow(gbuffer.roughness, 2.0);
  96. vec4 worldPos4 = u_invViewProjMat * vec4(UV_TO_NDC(uv), depth, 1.0);
  97. vec3 worldPos = worldPos4.xyz / worldPos4.w;
  98. // Calculate the light color
  99. vec3 viewDir = normalize(u_camPos - worldPos);
  100. vec3 frag2Light = u_light.posRadius.xyz - worldPos;
  101. vec3 l = normalize(frag2Light);
  102. float nol = max(0.0, dot(gbuffer.normal, l));
  103. vec3 specC = computeSpecularColorBrdf(viewDir, l, gbuffer.normal, gbuffer.specular, u_lspec, a2, nol);
  104. vec3 diffC = computeDiffuseColor(gbuffer.diffuse, u_ldiff);
  105. float att = computeAttenuationFactor(u_light.posRadius.w, frag2Light);
  106. float lambert = nol;
  107. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  108. out_color = (specC + diffC) * (att * max(lambert, gbuffer.subsurface));
  109. #else
  110. float spot =
  111. computeSpotFactor(l, u_light.diffuseColorOuterCos.w, u_light.specularColorInnerCos.w, u_light.lightDirPad1.xyz);
  112. out_color = (diffC + specC) * (att * spot * max(lambert, gbuffer.subsurface));
  113. #endif
  114. }
  115. ]]></source>
  116. </shader>
  117. </shaders>
  118. </shaderProgram>