Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Random.hlsl 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef UNITY_RANDOM_INCLUDED
  2. #define UNITY_RANDOM_INCLUDED
  3. float Hash(uint s)
  4. {
  5. s = s ^ 2747636419u;
  6. s = s * 2654435769u;
  7. s = s ^ (s >> 16);
  8. s = s * 2654435769u;
  9. s = s ^ (s >> 16);
  10. s = s * 2654435769u;
  11. return float(s) * rcp(4294967296.0); // 2^-32
  12. }
  13. // A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
  14. uint JenkinsHash(uint x)
  15. {
  16. x += (x << 10u);
  17. x ^= (x >> 6u);
  18. x += (x << 3u);
  19. x ^= (x >> 11u);
  20. x += (x << 15u);
  21. return x;
  22. }
  23. // Compound versions of the hashing algorithm.
  24. uint JenkinsHash(uint2 v)
  25. {
  26. return JenkinsHash(v.x ^ JenkinsHash(v.y));
  27. }
  28. uint JenkinsHash(uint3 v)
  29. {
  30. return JenkinsHash(v.x ^ JenkinsHash(v.yz));
  31. }
  32. uint JenkinsHash(uint4 v)
  33. {
  34. return JenkinsHash(v.x ^ JenkinsHash(v.yzw));
  35. }
  36. // Construct a float with half-open range [0, 1) using low 23 bits.
  37. // All zeros yields 0, all ones yields the next smallest representable value below 1.
  38. float ConstructFloat(int m) {
  39. const int ieeeMantissa = 0x007FFFFF; // Binary FP32 mantissa bitmask
  40. const int ieeeOne = 0x3F800000; // 1.0 in FP32 IEEE
  41. m &= ieeeMantissa; // Keep only mantissa bits (fractional part)
  42. m |= ieeeOne; // Add fractional part to 1.0
  43. float f = asfloat(m); // Range [1, 2)
  44. return f - 1; // Range [0, 1)
  45. }
  46. float ConstructFloat(uint m)
  47. {
  48. return ConstructFloat(asint(m));
  49. }
  50. // Pseudo-random value in half-open range [0, 1). The distribution is reasonably uniform.
  51. // Ref: https://stackoverflow.com/a/17479300
  52. float GenerateHashedRandomFloat(uint x)
  53. {
  54. return ConstructFloat(JenkinsHash(x));
  55. }
  56. float GenerateHashedRandomFloat(uint2 v)
  57. {
  58. return ConstructFloat(JenkinsHash(v));
  59. }
  60. float GenerateHashedRandomFloat(uint3 v)
  61. {
  62. return ConstructFloat(JenkinsHash(v));
  63. }
  64. float GenerateHashedRandomFloat(uint4 v)
  65. {
  66. return ConstructFloat(JenkinsHash(v));
  67. }
  68. float2 InitRandom(float2 input)
  69. {
  70. float2 r;
  71. r.x = Hash(uint(input.x * UINT_MAX));
  72. r.y = Hash(uint(input.y * UINT_MAX));
  73. return r;
  74. }
  75. //From Next Generation Post Processing in Call of Duty: Advanced Warfare [Jimenez 2014]
  76. // http://advances.realtimerendering.com/s2014/index.html
  77. float InterleavedGradientNoise(float2 pixCoord, int frameCount)
  78. {
  79. const float3 magic = float3(0.06711056f, 0.00583715f, 52.9829189f);
  80. float2 frameMagicScale = float2(2.083f, 4.867f);
  81. pixCoord += frameCount * frameMagicScale;
  82. return frac(magic.z * frac(dot(pixCoord, magic.xy)));
  83. }
  84. // 32-bit Xorshift random number generator
  85. uint XorShift(inout uint rngState)
  86. {
  87. rngState ^= rngState << 13;
  88. rngState ^= rngState >> 17;
  89. rngState ^= rngState << 5;
  90. return rngState;
  91. }
  92. #endif // UNITY_RANDOM_INCLUDED