DeferredShading.ankiprog 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 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 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. vec4 u_inputTexUvScaleAndOffset; // Use this to get the correct face UVs
  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. void main()
  81. {
  82. // Compute UV coordinates
  83. vec2 uv = vec2(gl_FragCoord.xy) / vec2(FB_SIZE.x, FB_SIZE.y);
  84. vec2 uvToRead = fma(uv, u_inputTexUvScaleAndOffset.xy, u_inputTexUvScaleAndOffset.zw);
  85. // Do manual depth test
  86. float depth = texture(u_msDepthRt, uvToRead).r;
  87. if(gl_FragCoord.z < depth)
  88. {
  89. discard;
  90. }
  91. GbufferInfo gbuffer;
  92. readGBuffer(u_msRt0, u_msRt1, u_msRt2, uvToRead, 0.0, gbuffer);
  93. float a2 = pow(gbuffer.roughness, 2.0);
  94. // Calculate the light color
  95. vec3 fragPos;
  96. fragPos.z = u_light.projectionParams.z / (u_light.projectionParams.w + depth);
  97. fragPos.xy = UV_TO_NDC(uv) * u_light.projectionParams.xy * fragPos.z;
  98. vec3 viewDir = normalize(-fragPos);
  99. vec3 frag2Light = u_light.posRadius.xyz - fragPos;
  100. vec3 l = normalize(frag2Light);
  101. float nol = max(0.0, dot(gbuffer.normal, l));
  102. vec3 specC = computeSpecularColorBrdf(viewDir, l, gbuffer.normal, gbuffer.specular, u_lspec, a2, nol);
  103. vec3 diffC = computeDiffuseColor(gbuffer.diffuse, u_ldiff);
  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) * (att * max(lambert, gbuffer.subsurface));
  108. #else
  109. float spot =
  110. computeSpotFactor(l, u_light.diffuseColorOuterCos.w, u_light.specularColorInnerCos.w, u_light.lightDirPad1.xyz);
  111. out_color = (diffC + specC) * (att * spot * max(lambert, gbuffer.subsurface));
  112. #endif
  113. }
  114. ]]></source>
  115. </shader>
  116. </shaders>
  117. </shaderProgram>