-
Notifications
You must be signed in to change notification settings - Fork 18
/
hash.glsl
133 lines (112 loc) · 3.53 KB
/
hash.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
uint ihash1D(uint q)
{
// hash by Hugo Elias, Integer Hash - I, 2017
q = q * 747796405u + 2891336453u;
q = (q << 13u) ^ q;
return q * (q * q * 15731u + 789221u) + 1376312589u;
}
uvec2 ihash1D(uvec2 q)
{
// hash by Hugo Elias, Integer Hash - I, 2017
q = q * 747796405u + 2891336453u;
q = (q << 13u) ^ q;
return q * (q * q * 15731u + 789221u) + 1376312589u;
}
uvec4 ihash1D(uvec4 q)
{
// hash by Hugo Elias, Integer Hash - I, 2017
q = q * 747796405u + 2891336453u;
q = (q << 13u) ^ q;
return q * (q * q * 15731u + 789221u) + 1376312589u;
}
// @return Value of the noise, range: [0, 1]
float hash1D(float x)
{
// based on: pcg by Mark Jarzynski: http://www.jcgt.org/published/0009/03/02/
uint state = uint(x * 8192.0) * 747796405u + 2891336453u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return float((word >> 22u) ^ word) * (1.0 / float(0xffffffffu));;
}
// @return Value of the noise, range: [0, 1]
float hash1D(vec2 x)
{
// hash by Inigo Quilez, Integer Hash - III, 2017
uvec2 q = uvec2(x * 8192.0);
q = 1103515245u * ((q >> 1u) ^ q.yx);
uint n = 1103515245u * (q.x ^ (q.y >> 3u));
return float(n) * (1.0 / float(0xffffffffu));
}
// @return Value of the noise, range: [0, 1]
float hash1D(vec3 x)
{
// based on: pcg3 by Mark Jarzynski: http://www.jcgt.org/published/0009/03/02/
uvec3 v = uvec3(x * 8192.0) * 1664525u + 1013904223u;
v += v.yzx * v.zxy;
v ^= v >> 16u;
return float(v.x + v.y * v.z) * (1.0 / float(0xffffffffu));
}
// @return Value of the noise, range: [0, 1]
vec2 hash2D(vec2 x)
{
// based on: Inigo Quilez, Integer Hash - III, 2017
uvec4 q = uvec2(x * 8192.0).xyyx + uvec2(0u, 3115245u).xxyy;
q = 1103515245u * ((q >> 1u) ^ q.yxwz);
uvec2 n = 1103515245u * (q.xz ^ (q.yw >> 3u));
return vec2(n) * (1.0 / float(0xffffffffu));
}
// @return Value of the noise, range: [0, 1]
vec3 hash3D(vec2 x)
{
// based on: pcg3 by Mark Jarzynski: http://www.jcgt.org/published/0009/03/02/
uvec3 v = uvec3(x.xyx * 8192.0) * 1664525u + 1013904223u;
v += v.yzx * v.zxy;
v ^= v >> 16u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
return vec3(v) * (1.0 / float(0xffffffffu));
}
// @return Value of the noise, range: [0, 1]
vec3 hash3D(vec3 x)
{
// based on: pcg3 by Mark Jarzynski: http://www.jcgt.org/published/0009/03/02/
uvec3 v = uvec3(x * 8192.0) * 1664525u + 1013904223u;
v += v.yzx * v.zxy;
v ^= v >> 16u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
return vec3(v) * (1.0 / float(0xffffffffu));
}
// @return Value of the noise, range: [0, 1]
vec4 hash4D(vec2 x)
{
// based on: pcg4 by Mark Jarzynski: http://www.jcgt.org/published/0009/03/02/
uvec4 v = uvec4(x.xyyx * 8192.0) * 1664525u + 1013904223u;
v += v.yzxy * v.wxyz;
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
v.x += v.y * v.w;
v.w += v.y * v.z;
v ^= v >> 16u;
return vec4(v ^ (v >> 16u)) * (1.0 / float(0xffffffffu));
}
// @return Value of the noise, range: [0, 1]
vec4 hash4D(vec4 x)
{
// based on: pcg4 by Mark Jarzynski: http://www.jcgt.org/published/0009/03/02/
uvec4 v = uvec4(x * 8192.0) * 1664525u + 1013904223u;
v += v.yzxy * v.wxyz;
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
v ^= v >> 16u;
return vec4(v ^ (v >> 16u)) * (1.0 / float(0xffffffffu));
}