Clusterer.glsl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2009-2017, 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. uint computeClusterK(float near, float clustererMagic, float zVSpace)
  10. {
  11. float fz = sqrt((zVSpace + near) * clustererMagic);
  12. uint z = uint(fz);
  13. return z;
  14. }
  15. uint computeClusterKSafe(float near, float far, float clustererMagic, float zVSpace)
  16. {
  17. float z = clamp(zVSpace, -far, -near);
  18. float kf = sqrt((z + near) * clustererMagic);
  19. return uint(kf);
  20. }
  21. // Compute cluster index
  22. uint computeClusterIndex(
  23. vec2 uv, float near, float clustererMagic, float zVSpace, uint clusterCountX, uint clusterCountY)
  24. {
  25. uvec2 xy = uvec2(uv * vec2(clusterCountX, clusterCountY));
  26. return computeClusterK(near, clustererMagic, zVSpace) * (clusterCountX * clusterCountY) + xy.y * clusterCountX
  27. + xy.x;
  28. }
  29. // Compute the Z of the near plane given a cluster idx
  30. float computeClusterNear(uint k, float near, float clustererMagic)
  31. {
  32. return 1.0 / clustererMagic * pow(float(k), 2.0) - near;
  33. }
  34. float computeClusterFar(uint k, float near, float clustererMagic)
  35. {
  36. return 1.0 / clustererMagic * pow(float(k + 1u), 2.0) - near;
  37. }
  38. #endif