SpaceFillingCurves.hlsl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef UNITY_SPACE_FILLING_CURVES_INCLUDED
  2. #define UNITY_SPACE_FILLING_CURVES_INCLUDED
  3. // "Insert" a 0 bit after each of the 16 low bits of x.
  4. // Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  5. uint Part1By1(uint x)
  6. {
  7. x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
  8. x = (x ^ (x << 8)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
  9. x = (x ^ (x << 4)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
  10. x = (x ^ (x << 2)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
  11. x = (x ^ (x << 1)) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
  12. return x;
  13. }
  14. // "Insert" two 0 bits after each of the 10 low bits of x.
  15. // Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  16. uint Part1By2(uint x)
  17. {
  18. x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
  19. x = (x ^ (x << 16)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
  20. x = (x ^ (x << 8)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
  21. x = (x ^ (x << 4)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
  22. x = (x ^ (x << 2)) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
  23. return x;
  24. }
  25. // Inverse of Part1By1 - "delete" all odd-indexed bits.
  26. // Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  27. uint Compact1By1(uint x)
  28. {
  29. x &= 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
  30. x = (x ^ (x >> 1)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
  31. x = (x ^ (x >> 2)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
  32. x = (x ^ (x >> 4)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
  33. x = (x ^ (x >> 8)) & 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
  34. return x;
  35. }
  36. // Inverse of Part1By2 - "delete" all bits not at positions divisible by 3.
  37. // Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  38. uint Compact1By2(uint x)
  39. {
  40. x &= 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
  41. x = (x ^ (x >> 2)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
  42. x = (x ^ (x >> 4)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
  43. x = (x ^ (x >> 8)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
  44. x = (x ^ (x >> 16)) & 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
  45. return x;
  46. }
  47. uint EncodeMorton2D(uint2 coord)
  48. {
  49. return (Part1By1(coord.y) << 1) + Part1By1(coord.x);
  50. }
  51. uint EncodeMorton3D(uint3 coord)
  52. {
  53. return (Part1By2(coord.z) << 2) + (Part1By2(coord.y) << 1) + Part1By2(coord.x);
  54. }
  55. uint2 DecodeMorton2D(uint code)
  56. {
  57. return uint2(Compact1By1(code >> 0), Compact1By1(code >> 1));
  58. }
  59. uint3 DecodeMorton3D(uint code)
  60. {
  61. return uint3(Compact1By2(code >> 0), Compact1By2(code >> 1), Compact1By2(code >> 2));
  62. }
  63. uint InterleaveQuad(uint2 quad)
  64. {
  65. return quad.x + 2 * quad.y;
  66. }
  67. uint2 DeinterleaveQuad(uint code)
  68. {
  69. return uint2(code & 1, (code >> 1) & 1);
  70. }
  71. #endif // UNITY_SPACE_FILLING_CURVES_INCLUDED