Clusterer.glsl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (C) 2009-2018, 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. // See the documentation of the Clusterer class.
  10. struct ClustererMagicValues
  11. {
  12. vec4 val0;
  13. vec4 val1;
  14. };
  15. uint computeClusterK(ClustererMagicValues magic, vec3 worldPos)
  16. {
  17. float fz = sqrt(dot(magic.val0.xyz, worldPos) - magic.val0.w);
  18. uint z = uint(fz);
  19. return z;
  20. }
  21. // Compute cluster index
  22. uint computeClusterIndex(ClustererMagicValues magic, vec2 uv, vec3 worldPos, uint clusterCountX, uint clusterCountY)
  23. {
  24. uvec2 xy = uvec2(uv * vec2(clusterCountX, clusterCountY));
  25. return computeClusterK(magic, worldPos) * (clusterCountX * clusterCountY) + xy.y * clusterCountX + xy.x;
  26. }
  27. // Compute the Z of the near plane given a cluster idx
  28. float computeClusterNear(ClustererMagicValues magic, uint k)
  29. {
  30. float fk = float(k);
  31. return magic.val1.x * fk * fk + magic.val1.y;
  32. }
  33. float computeClusterFar(ClustererMagicValues magic, uint k)
  34. {
  35. return computeClusterNear(magic, k + 1u);
  36. }
  37. #endif