Blender V5.3
gpu_shader_common_hash.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9/* ***** Jenkins Lookup3 Hash Functions ***** */
10
11/* Source: http://burtleburtle.net/bob/c/lookup3.c */
12
13[[force_inline]] void hash_mix(uint &a, uint &b, uint &c)
14{
15 a -= c;
16 a ^= (((c) << (4)) | ((c) >> (32 - (4))));
17 c += b;
18 b -= a;
19 b ^= (((a) << (6)) | ((a) >> (32 - (6))));
20 a += c;
21 c -= b;
22 c ^= (((b) << (8)) | ((b) >> (32 - (8))));
23 b += a;
24 a -= c;
25 a ^= (((c) << (16)) | ((c) >> (32 - (16))));
26 c += b;
27 b -= a;
28 b ^= (((a) << (19)) | ((a) >> (32 - (19))));
29 a += c;
30 c -= b;
31 c ^= (((b) << (4)) | ((b) >> (32 - (4))));
32 b += a;
33}
34
35[[force_inline]] void hash_final(uint &a, uint &b, uint &c)
36{
37 c ^= b;
38 c -= (((b) << (14)) | ((b) >> (32 - (14))));
39 a ^= c;
40 a -= (((c) << (11)) | ((c) >> (32 - (11))));
41 b ^= a;
42 b -= (((a) << (25)) | ((a) >> (32 - (25))));
43 c ^= b;
44 c -= (((b) << (16)) | ((b) >> (32 - (16))));
45 a ^= c;
46 a -= (((c) << (4)) | ((c) >> (32 - (4))));
47 b ^= a;
48 b -= (((a) << (14)) | ((a) >> (32 - (14))));
49 c ^= b;
50 c -= (((b) << (24)) | ((b) >> (32 - (24))));
51}
52
53uint hash_uint(uint kx)
54{
55 uint a, b, c;
56 a = b = c = 0xdeadbeefu + (1u << 2u) + 13u;
57
58 a += kx;
59 hash_final(a, b, c);
60
61 return c;
62}
63
64uint hash_uint2(uint kx, uint ky)
65{
66 uint a, b, c;
67 a = b = c = 0xdeadbeefu + (2u << 2u) + 13u;
68
69 b += ky;
70 a += kx;
71 hash_final(a, b, c);
72
73 return c;
74}
75
76uint hash_uint3(uint kx, uint ky, uint kz)
77{
78 uint a, b, c;
79 a = b = c = 0xdeadbeefu + (3u << 2u) + 13u;
80
81 c += kz;
82 b += ky;
83 a += kx;
84 hash_final(a, b, c);
85
86 return c;
87}
88
89uint hash_uint4(uint kx, uint ky, uint kz, uint kw)
90{
91 uint a, b, c;
92 a = b = c = 0xdeadbeefu + (4u << 2u) + 13u;
93
94 a += kx;
95 b += ky;
96 c += kz;
97 hash_mix(a, b, c);
98
99 a += kw;
100 hash_final(a, b, c);
101
102 return c;
103}
104
105uint hash_int(int kx)
106{
107 return hash_uint(uint(kx));
108}
109
110uint hash_int2(int kx, int ky)
111{
112 return hash_uint2(uint(kx), uint(ky));
113}
114
115uint hash_int3(int kx, int ky, int kz)
116{
117 return hash_uint3(uint(kx), uint(ky), uint(kz));
118}
119
120uint hash_int4(int kx, int ky, int kz, int kw)
121{
122 return hash_uint4(uint(kx), uint(ky), uint(kz), uint(kw));
123}
124
125/* PCG 2D, 3D and 4D hash functions,
126 * from "Hash Functions for GPU Rendering" JCGT 2020
127 * https://jcgt.org/published/0009/03/02/
128 *
129 * Slightly modified to only use signed integers,
130 * so that they can also be implemented in OSL. */
131
133{
134 v = v * 1664525 + 1013904223;
135 v.x += v.y * 1664525;
136 v.y += v.x * 1664525;
137 v = v ^ (v >> 16);
138 v.x += v.y * 1664525;
139 v.y += v.x * 1664525;
140 return v;
141}
142
144{
145 v = v * 1664525 + 1013904223;
146 v.x += v.y * v.z;
147 v.y += v.z * v.x;
148 v.z += v.x * v.y;
149 v = v ^ (v >> 16);
150 v.x += v.y * v.z;
151 v.y += v.z * v.x;
152 v.z += v.x * v.y;
153 return v;
154}
155
157{
158 v = v * 1664525 + 1013904223;
159 v.x += v.y * v.w;
160 v.y += v.z * v.x;
161 v.z += v.x * v.y;
162 v.w += v.y * v.z;
163 v = v ^ (v >> 16);
164 v.x += v.y * v.w;
165 v.y += v.z * v.x;
166 v.z += v.x * v.y;
167 v.w += v.y * v.z;
168 return v;
169}
170
171/* Hashing uint or uint[234] into a float in the range [0, 1]. */
172
173float hash_uint_to_float(uint kx)
174{
175 return float(hash_uint(kx)) / float(0xFFFFFFFFu);
176}
177
178float hash_uint2_to_float(uint kx, uint ky)
179{
180 return float(hash_uint2(kx, ky)) / float(0xFFFFFFFFu);
181}
182
183float hash_uint3_to_float(uint kx, uint ky, uint kz)
184{
185 return float(hash_uint3(kx, ky, kz)) / float(0xFFFFFFFFu);
186}
187
188float hash_uint4_to_float(uint kx, uint ky, uint kz, uint kw)
189{
190 return float(hash_uint4(kx, ky, kz, kw)) / float(0xFFFFFFFFu);
191}
192
193/* Hashing float or vec[234] into a float in the range [0, 1]. */
194
196{
198}
199
204
209
215
216/* Hashing vec[234] into vec[234] of components in the range [0, 1]. */
217
222
229
231{
232 return float4(hash_vec4_to_float(k.xyzw),
233 hash_vec4_to_float(k.wxyz),
234 hash_vec4_to_float(k.zwxy),
235 hash_vec4_to_float(k.yzwx));
236}
237
238/* Hashing a number of integers into floats in [0..1] range. */
239
241{
242 int2 h = hash_pcg2d_i(k);
243 return float2(h & 0x7fffffff) * (1.0 / float(0x7fffffff));
244}
245
247{
248 int3 h = hash_pcg3d_i(k);
249 return float3(h & 0x7fffffff) * (1.0 / float(0x7fffffff));
250}
251
253{
254 int4 h = hash_pcg4d_i(k);
255 return float4(h & 0x7fffffff) * (1.0 / float(0x7fffffff));
256}
257
259{
260 return hash_int3_to_vec3(int3(k.x, k.y, 0));
261}
262
264{
265 return hash_int4_to_vec4(k).xyz;
266}
267
268/* Hashing float or vec[234] into vec3 of components in range [0, 1]. */
269
271{
272 return float3(hash_float_to_float(k),
273 hash_vec2_to_float(float2(k, 1.0f)),
274 hash_vec2_to_float(float2(k, 2.0f)));
275}
276
283
285{
286 return float3(
288}
289
290/* Hashing float or vec[234] into vec2 of components in range [0, 1]. */
291
293{
295}
296
301
306
307/* Other Hash Functions */
308
309float integer_noise(int n)
310{
311 /* Integer bit-shifts for these calculations can cause precision problems on macOS.
312 * Using uint resolves these issues. */
313 uint nn;
314 nn = (uint(n) + 1013u) & 0x7fffffffu;
315 nn = (nn >> 13u) ^ nn;
316 nn = (uint(nn * (nn * nn * 60493u + 19990303u)) + 1376312589u) & 0x7fffffffu;
317 return 0.5f * (float(nn) / 1073741824.0f);
318}
319
320float wang_hash_noise(uint s)
321{
322 s = (s ^ 61u) ^ (s >> 16u);
323 s *= 9u;
324 s = s ^ (s >> 4u);
325 s *= 0x27d4eb2du;
326 s = s ^ (s >> 15u);
327
328 return fract(float(s) / 4294967296.0f);
329}
float3 hash_int2_to_vec3(int2 k)
uint hash_int2(int kx, int ky)
float3 hash_vec4_to_vec3(float4 k)
int4 hash_pcg4d_i(int4 v)
uint hash_int3(int kx, int ky, int kz)
uint hash_uint(uint kx)
uint hash_int(int kx)
float integer_noise(int n)
float hash_uint_to_float(uint kx)
uint hash_int4(int kx, int ky, int kz, int kw)
uint hash_uint4(uint kx, uint ky, uint kz, uint kw)
float3 hash_float_to_vec3(float k)
uint hash_uint3(uint kx, uint ky, uint kz)
uint hash_uint2(uint kx, uint ky)
float4 hash_vec4_to_vec4(float4 k)
float hash_uint3_to_float(uint kx, uint ky, uint kz)
float2 hash_float_to_vec2(float k)
float2 hash_int2_to_vec2(int2 k)
float4 hash_int4_to_vec4(int4 k)
float hash_vec4_to_float(float4 k)
float2 hash_vec3_to_vec2(float3 k)
void hash_final(uint &a, uint &b, uint &c)
float3 hash_int3_to_vec3(int3 k)
int2 hash_pcg2d_i(int2 v)
float hash_vec3_to_float(float3 k)
float hash_uint2_to_float(uint kx, uint ky)
float wang_hash_noise(uint s)
float3 hash_vec2_to_vec3(float2 k)
float hash_float_to_float(float k)
float3 hash_vec3_to_vec3(float3 k)
float2 hash_vec4_to_vec2(float4 k)
int3 hash_pcg3d_i(int3 v)
float hash_uint4_to_float(uint kx, uint ky, uint kz, uint kw)
void hash_mix(uint &a, uint &b, uint &c)
float3 hash_int4_to_vec3(int4 k)
float2 hash_vec2_to_vec2(float2 k)
float hash_vec2_to_float(float2 k)
#define force_inline
T fract(T) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225