Ei kuvausta
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.

Hashes.hlsl 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef _SAMPLING_HASHES_HLSL_
  2. #define _SAMPLING_HASHES_HLSL_
  3. // Low bias hash from https://github.com/skeeto/hash-prospector
  4. uint LowBiasHash32(uint x, uint seed = 0)
  5. {
  6. x += seed;
  7. x ^= x >> 16;
  8. x *= 0x21f0aaad;
  9. x ^= x >> 15;
  10. x *= 0xd35a2d97;
  11. x ^= x >> 15;
  12. return x;
  13. }
  14. // Murmur Hash from https://github.com/aappleby/smhasher/wiki/MurmurHash3
  15. uint MurmurAdd(uint hash, uint item)
  16. {
  17. item *= 0xcc9e2d51;
  18. item = (item << 15) | (item >> 17);
  19. item *= 0x1b873593;
  20. hash ^= item;
  21. hash = (hash << 13) | (hash >> 19);
  22. hash = hash * 5 + 0xe6546b64;
  23. return hash;
  24. }
  25. uint MurmurFinalize(uint hash)
  26. {
  27. hash ^= hash >> 16;
  28. hash *= 0x85ebca6b;
  29. hash ^= hash >> 13;
  30. hash *= 0xc2b2ae35;
  31. hash ^= hash >> 16;
  32. return hash;
  33. }
  34. uint MurmurHash(uint x, uint seed = 0)
  35. {
  36. uint h = seed;
  37. h = MurmurAdd(h, x);
  38. return MurmurFinalize(h);
  39. }
  40. uint MurmurHash(uint2 x, uint seed = 0)
  41. {
  42. uint h = seed;
  43. h = MurmurAdd(h, x.x);
  44. h = MurmurAdd(h, x.y);
  45. return MurmurFinalize(h);
  46. }
  47. uint MurmurHash(uint3 x, uint seed = 0)
  48. {
  49. uint h = seed;
  50. h = MurmurAdd(h, x.x);
  51. h = MurmurAdd(h, x.y);
  52. h = MurmurAdd(h, x.z);
  53. return MurmurFinalize(h);
  54. }
  55. uint MurmurHash(uint4 x, uint seed = 0)
  56. {
  57. uint h = seed;
  58. h = MurmurAdd(h, x.x);
  59. h = MurmurAdd(h, x.y);
  60. h = MurmurAdd(h, x.z);
  61. h = MurmurAdd(h, x.w);
  62. return MurmurFinalize(h);
  63. }
  64. uint XorShift32(uint rngState)
  65. {
  66. rngState ^= rngState << 13;
  67. rngState ^= rngState >> 17;
  68. rngState ^= rngState << 5;
  69. return rngState;
  70. }
  71. // From PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.
  72. // and "Hash Functions for GPU Rendering" paper
  73. uint Pcg(uint v)
  74. {
  75. uint state = v * 747796405u + 2891336453u;
  76. uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
  77. return (word >> 22u) ^ word;
  78. }
  79. uint2 Pcg2d(uint2 v)
  80. {
  81. v = v * 1664525u + 1013904223u;
  82. v.x += v.y * 1664525u;
  83. v.y += v.x * 1664525u;
  84. v = v ^ (v >> 16u);
  85. v.x += v.y * 1664525u;
  86. v.y += v.x * 1664525u;
  87. v = v ^ (v >> 16u);
  88. return v;
  89. }
  90. uint3 Pcg3d(uint3 v)
  91. {
  92. v = v * 1664525u + 1013904223u;
  93. v.x += v.y * v.z;
  94. v.y += v.z * v.x;
  95. v.z += v.x * v.y;
  96. v ^= v >> 16u;
  97. v.x += v.y * v.z;
  98. v.y += v.z * v.x;
  99. v.z += v.x * v.y;
  100. return v;
  101. }
  102. uint4 Pcg4d(uint4 v)
  103. {
  104. v = v * 1664525u + 1013904223u;
  105. v.x += v.y * v.w;
  106. v.y += v.z * v.x;
  107. v.z += v.x * v.y;
  108. v.w += v.y * v.z;
  109. v = v ^ (v >> 16u);
  110. v.x += v.y * v.w;
  111. v.y += v.z * v.x;
  112. v.z += v.x * v.y;
  113. v.w += v.y * v.z;
  114. return v;
  115. }
  116. #endif // _SAMPLING_HASHES_HLSL_