Clusterer.glsl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Contains functions for the clusterer
  6. #ifndef ANKI_SHADERS_CLUSTERER_GLSL
  7. #define ANKI_SHADERS_CLUSTERER_GLSL
  8. #include "shaders/Common.glsl"
  9. // Compute the cluster index using the tile index.
  10. uint computeClusterIndexUsingTileIdx(
  11. float near, float clustererMagic, float zVSpace, uint tileIdx, uint tileCountX, uint tileCountY)
  12. {
  13. float fk = sqrt((zVSpace + near) * clustererMagic);
  14. uint k = uint(fk);
  15. return tileIdx + k * (tileCountX * tileCountY);
  16. }
  17. // Compute the cluster index using the a custom gl_FragCoord.xy.
  18. uint computeClusterIndexUsingCustomFragCoord(
  19. float near, float clustererMagic, float zVSpace, uint tileCountX, uint tileCountY, vec2 fragCoord)
  20. {
  21. // Compute tile idx
  22. uvec2 f = uvec2(fragCoord) >> 6;
  23. uint tileIdx = f.y * tileCountX + f.x;
  24. return computeClusterIndexUsingTileIdx(near, clustererMagic, zVSpace, tileIdx, tileCountX, tileCountY);
  25. }
  26. // Compute the cluster index using the gl_FragCoord.xy.
  27. uint computeClusterIndexUsingFragCoord(
  28. float near, float clustererMagic, float zVSpace, uint tileCountX, uint tileCountY)
  29. {
  30. return computeClusterIndexUsingCustomFragCoord(
  31. near, clustererMagic, zVSpace, tileCountX, tileCountY, gl_FragCoord.xy);
  32. }
  33. // Compute the Z of the near plane given a cluster idx
  34. float computeClusterNear(uint k, float near, float clustererMagic)
  35. {
  36. return 1.0 / clustererMagic * pow(float(k), 2.0) - near;
  37. }
  38. float computeClusterFar(uint k, float near, float clustererMagic)
  39. {
  40. return 1.0 / clustererMagic * pow(float(k + 1u), 2.0) - near;
  41. }
  42. uint computeClusterKFromZViewSpace(float zViewSpace, float near, float far, float clustererMagic)
  43. {
  44. float z = clamp(zViewSpace, -far, -near);
  45. z = -z;
  46. float kf = sqrt((z - near) * -clustererMagic);
  47. return uint(kf);
  48. }
  49. #endif