💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.2 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_NOISE_HASH_HPP
  20. #define ANTKEEPER_MATH_NOISE_HASH_HPP
  21. #include "math/vector.hpp"
  22. #include <cstdint>
  23. namespace math {
  24. namespace noise {
  25. /// Hash functions.
  26. namespace hash {
  27. /**
  28. * PCG3D hash.
  29. *
  30. * @see Mark Jarzynski and Marc Olano, Hash Functions for GPU Rendering, Journal of Computer Graphics Techniques (JCGT), vol. 9, no. 3, 21-38, 2020.
  31. */
  32. /// @{
  33. template <class T>
  34. math::vector<std::uint32_t, 3> pcg3d_3(const math::vector<T, 3>& x)
  35. {
  36. math::vector<std::uint32_t, 3> u = math::vector<std::uint32_t, 3>(x) * 1664525U + 1013904223U;
  37. u[0] += u[1] * u[2];
  38. u[1] += u[2] * u[0];
  39. u[2] += u[0] * u[1];
  40. u[0] ^= u[0] >> 16U;
  41. u[1] ^= u[1] >> 16U;
  42. u[2] ^= u[2] >> 16U;
  43. u[0] += u[1] * u[2];
  44. u[1] += u[2] * u[0];
  45. u[2] += u[0] * u[1];
  46. return u;
  47. }
  48. template <class T>
  49. math::vector<std::uint32_t, 3> pcg3d_3(const math::vector<T, 2>& x)
  50. {
  51. return pcg3d_3<T>({x[0], x[1], T{1}});
  52. }
  53. template <class T>
  54. math::vector<std::uint32_t, 3> pcg3d_3(const math::vector<T, 1>& x)
  55. {
  56. return pcg3d_3<T>({x[0], T{1}, T{1}});
  57. }
  58. template <class T>
  59. math::vector<std::uint32_t, 3> pcg3d_3(T x)
  60. {
  61. return pcg3d_3<T>({x, T{1}, T{1}});
  62. }
  63. template <class T>
  64. std::uint32_t pcg3d_1(const math::vector<T, 3>& x)
  65. {
  66. return pcg3d_3<T>(x)[0];
  67. }
  68. template <class T>
  69. std::uint32_t pcg3d_1(const math::vector<T, 2>& x)
  70. {
  71. return pcg3d_3<T>({x[0], x[1], T{1}})[0];
  72. }
  73. template <class T>
  74. std::uint32_t pcg3d_1(const math::vector<T, 1>& x)
  75. {
  76. return pcg3d_3<T>({x[0], T{1}, T{1}})[0];
  77. }
  78. template <class T>
  79. std::uint32_t pcg3d_1(T x)
  80. {
  81. return pcg3d_3<T>({x, T{1}, T{1}})[0];
  82. }
  83. /// @}
  84. /**
  85. * PCG4D hash.
  86. *
  87. * @see Mark Jarzynski and Marc Olano, Hash Functions for GPU Rendering, Journal of Computer Graphics Techniques (JCGT), vol. 9, no. 3, 21-38, 2020.
  88. */
  89. /// @{
  90. template <class T>
  91. math::vector<std::uint32_t, 4> pcg4d_4(const math::vector<T, 4>& x)
  92. {
  93. math::vector<std::uint32_t, 4> u = math::vector<std::uint32_t, 4>(x) * 1664525U + 1013904223U;
  94. u[0] += u[1] * u[3];
  95. u[1] += u[2] * u[0];
  96. u[2] += u[0] * u[1];
  97. u[3] += u[1] * u[2];
  98. u[0] ^= u[0] >> 16U;
  99. u[1] ^= u[1] >> 16U;
  100. u[2] ^= u[2] >> 16U;
  101. u[3] ^= u[3] >> 16U;
  102. u[0] += u[1] * u[3];
  103. u[1] += u[2] * u[0];
  104. u[2] += u[0] * u[1];
  105. u[3] += u[1] * u[2];
  106. return u;
  107. }
  108. template <class T>
  109. std::uint32_t pcg4d_1(const math::vector<T, 4>& x)
  110. {
  111. return pcg4d_4<T>(x)[0];
  112. }
  113. /// @}
  114. } // namespace hash
  115. } // namespace noise
  116. } // namespace math
  117. #endif // ANTKEEPER_MATH_NOISE_HASH_HPP