DeferredShading.ankiprog 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. };
  47. // Spot light
  48. struct SpotLight
  49. {
  50. vec4 posRadius; // xyz: Light pos in world space. w: The -1/radius
  51. vec4 diffuseColorOuterCos; // xyz: diff color, w: outer cosine of spot
  52. vec4 lightDirInnerCos; // xyz: light dir, w: inner cosine of spot
  53. };
  54. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
  55. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
  56. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
  57. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
  58. layout(ANKI_UBO_BINDING(0, 1), row_major) uniform u1_
  59. {
  60. vec4 u_inputTexUvScaleAndOffset; // Use this to get the correct face UVs
  61. mat4 u_invViewProjMat;
  62. vec4 u_camPosPad1;
  63. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  64. PointLight u_light;
  65. #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
  66. SpotLight u_light;
  67. #else
  68. # error See file
  69. #endif
  70. };
  71. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  72. # define u_ldiff u_light.diffuseColorPad1.xyz
  73. #else
  74. # define u_ldiff u_light.diffuseColorOuterCos.xyz
  75. # define u_lightDir u_light.lightDirInnerCos.xyz
  76. # define u_outerCos u_light.diffuseColorOuterCos.w
  77. # define u_innerCos u_light.lightDirInnerCos.w
  78. # define u_lightDir u_light.lightDirInnerCos.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. vec4 worldPos4 = u_invViewProjMat * vec4(UV_TO_NDC(uv), depth, 1.0);
  96. vec3 worldPos = worldPos4.xyz / worldPos4.w;
  97. // Calculate the light color
  98. vec3 viewDir = normalize(u_camPos - worldPos);
  99. vec3 frag2Light = u_light.posRadius.xyz - worldPos;
  100. vec3 l = normalize(frag2Light);
  101. float nol = max(0.0, dot(gbuffer.normal, l));
  102. vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l);
  103. vec3 diffC = diffuseLambert(gbuffer.diffuse);
  104. float att = computeAttenuationFactor(u_light.posRadius.w, frag2Light);
  105. float lambert = nol;
  106. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  107. out_color = (specC + diffC) * u_ldiff * (att * max(lambert, gbuffer.subsurface));
  108. #else
  109. float spot = computeSpotFactor(l, u_outerCos, u_innerCos, u_lightDir);
  110. out_color = (diffC + specC) * u_ldiff * (att * spot * max(lambert, gbuffer.subsurface));
  111. #endif
  112. }
  113. ]]></source>
  114. </shader>
  115. </shaders>
  116. </shaderProgram>