DeferredShading.ankiprog 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!--
  2. Copyright (C) 2009-2017, 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. ANKI_WRITE_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 projectionParams;
  45. vec4 posRadius; // xyz: Light pos in eye space. w: The -1/radius
  46. vec4 diffuseColorPad1; // xyz: diff color
  47. vec4 specularColorPad1; // xyz: spec color
  48. };
  49. // Spot light
  50. struct SpotLight
  51. {
  52. vec4 projectionParams;
  53. vec4 posRadius; // xyz: Light pos in eye space. w: The -1/radius
  54. vec4 diffuseColorOuterCos; // xyz: diff color, w: outer cosine of spot
  55. vec4 specularColorInnerCos; // xyz: spec color, w: inner cosine of spot
  56. vec4 lightDirPad1;
  57. };
  58. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
  59. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
  60. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
  61. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
  62. layout(ANKI_UBO_BINDING(0, 1)) uniform u1_
  63. {
  64. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  65. PointLight u_light;
  66. #elif LIGHT_TYPE == SPOT_LIGHT_TYPE
  67. SpotLight u_light;
  68. #else
  69. #error See file
  70. #endif
  71. };
  72. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  73. #define u_ldiff u_light.diffuseColorPad1.xyz
  74. #define u_lspec u_light.specularColorPad1.xyz
  75. #else
  76. #define u_ldiff u_light.diffuseColorOuterCos.xyz
  77. #define u_lspec u_light.specularColorInnerCos.xyz
  78. #endif
  79. vec3 readPosition(in vec2 uv)
  80. {
  81. vec3 fragPosVspace;
  82. float depth = texture(u_msDepthRt, uv).r;
  83. fragPosVspace.z = u_light.projectionParams.z / (u_light.projectionParams.w + depth);
  84. fragPosVspace.xy = (2.0 * uv - 1.0) * u_light.projectionParams.xy * fragPosVspace.z;
  85. return fragPosVspace;
  86. }
  87. void main()
  88. {
  89. // Read G-buffer
  90. vec2 uv = vec2(gl_FragCoord.xy) / vec2(FB_SIZE.x, FB_SIZE.y);
  91. GbufferInfo gbuffer;
  92. readGBuffer(u_msRt0, u_msRt1, u_msRt2, uv, 0.0, gbuffer);
  93. float a2 = pow(gbuffer.roughness, 2.0);
  94. // Calculate the light color
  95. vec3 fragPos = readPosition(uv);
  96. vec3 viewDir = normalize(-fragPos);
  97. vec3 frag2Light = u_light.posRadius.xyz - fragPos;
  98. vec3 l = normalize(frag2Light);
  99. float nol = max(0.0, dot(gbuffer.normal, l));
  100. vec3 specC = computeSpecularColorBrdf(viewDir, l, gbuffer.normal, gbuffer.specular, u_lspec, a2, nol);
  101. vec3 diffC = computeDiffuseColor(gbuffer.diffuse, u_ldiff);
  102. float att = computeAttenuationFactor(u_light.posRadius.w, frag2Light);
  103. float lambert = nol;
  104. #if LIGHT_TYPE == POINT_LIGHT_TYPE
  105. out_color = (specC + diffC) * (att * max(lambert, gbuffer.subsurface));
  106. #else
  107. float spot =
  108. computeSpotFactor(l, u_light.diffuseColorOuterCos.w, u_light.specularColorInnerCos.w, u_light.lightDirPad1.xyz);
  109. out_color = (diffC + specC) * (att * spot * max(lambert, gbuffer.subsurface));
  110. #endif
  111. }
  112. ]]></source>
  113. </shader>
  114. </shaders>
  115. </shaderProgram>