Blender V5.3
gpu_shader_material_noise.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
9
10[[force_inline]] void floor_frac(float x, int &x_int, float &x_fract)
11{
12 float x_floor = floor(x);
13 x_int = int(x_floor);
14 x_fract = x - x_floor;
15}
16
17/* Bilinear Interpolation:
18 *
19 * v2 v3
20 * @ + + + + @ y
21 * + + ^
22 * + + |
23 * + + |
24 * @ + + + + @ @------> x
25 * v0 v1
26 */
27float bi_mix(float v0, float v1, float v2, float v3, float x, float y)
28{
29 float x1 = 1.0f - x;
30 return (1.0f - y) * (v0 * x1 + v1 * x) + y * (v2 * x1 + v3 * x);
31}
32
33/* Trilinear Interpolation:
34 *
35 * v6 v7
36 * @ + + + + + + @
37 * +\ +\
38 * + \ + \
39 * + \ + \
40 * + \ v4 + \ v5
41 * + @ + + + +++ + @ z
42 * + + + + y ^
43 * v2 @ + +++ + + + @ v3 + \ |
44 * \ + \ + \ |
45 * \ + \ + \|
46 * \ + \ + +---------> x
47 * \+ \+
48 * @ + + + + + + @
49 * v0 v1
50 */
51float tri_mix(float v0,
52 float v1,
53 float v2,
54 float v3,
55 float v4,
56 float v5,
57 float v6,
58 float v7,
59 float x,
60 float y,
61 float z)
62{
63 float x1 = 1.0f - x;
64 float y1 = 1.0f - y;
65 float z1 = 1.0f - z;
66 return z1 * (y1 * (v0 * x1 + v1 * x) + y * (v2 * x1 + v3 * x)) +
67 z * (y1 * (v4 * x1 + v5 * x) + y * (v6 * x1 + v7 * x));
68}
69
70float quad_mix(float v0,
71 float v1,
72 float v2,
73 float v3,
74 float v4,
75 float v5,
76 float v6,
77 float v7,
78 float v8,
79 float v9,
80 float v10,
81 float v11,
82 float v12,
83 float v13,
84 float v14,
85 float v15,
86 float x,
87 float y,
88 float z,
89 float w)
90{
91 return mix(tri_mix(v0, v1, v2, v3, v4, v5, v6, v7, x, y, z),
92 tri_mix(v8, v9, v10, v11, v12, v13, v14, v15, x, y, z),
93 w);
94}
95
96float fade(float t)
97{
98 return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
99}
100
101float negate_if(float value, uint condition)
102{
103 return (condition != 0u) ? -value : value;
104}
105
106float noise_grad(uint hash, float x)
107{
108 uint h = hash & 15u;
109 float g = 1u + (h & 7u);
110 return negate_if(g, h & 8u) * x;
111}
112
113float noise_grad(uint hash, float x, float y)
114{
115 uint h = hash & 7u;
116 float u = h < 4u ? x : y;
117 float v = 2.0f * (h < 4u ? y : x);
118 return negate_if(u, h & 1u) + negate_if(v, h & 2u);
119}
120
121float noise_grad(uint hash, float x, float y, float z)
122{
123 uint h = hash & 15u;
124 float u = h < 8u ? x : y;
125 float vt = ((h == 12u) || (h == 14u)) ? x : z;
126 float v = h < 4u ? y : vt;
127 return negate_if(u, h & 1u) + negate_if(v, h & 2u);
128}
129
130float noise_grad(uint hash, float x, float y, float z, float w)
131{
132 uint h = hash & 31u;
133 float u = h < 24u ? x : y;
134 float v = h < 16u ? y : z;
135 float s = h < 8u ? z : w;
136 return negate_if(u, h & 1u) + negate_if(v, h & 2u) + negate_if(s, h & 4u);
137}
138
139float noise_perlin(float x)
140{
141 int X;
142 float fx;
143
144 floor_frac(x, X, fx);
145
146 float u = fade(fx);
147
148 float r = mix(noise_grad(hash_int(X), fx), noise_grad(hash_int(X + 1), fx - 1.0f), u);
149
150 return r;
151}
152
154{
155 int X, Y;
156 float fx, fy;
157
158 floor_frac(vec.x, X, fx);
159 floor_frac(vec.y, Y, fy);
160
161 float u = fade(fx);
162 float v = fade(fy);
163
164 float r = bi_mix(noise_grad(hash_int2(X, Y), fx, fy),
165 noise_grad(hash_int2(X + 1, Y), fx - 1.0f, fy),
166 noise_grad(hash_int2(X, Y + 1), fx, fy - 1.0f),
167 noise_grad(hash_int2(X + 1, Y + 1), fx - 1.0f, fy - 1.0f),
168 u,
169 v);
170
171 return r;
172}
173
175{
176 int X, Y, Z;
177 float fx, fy, fz;
178
179 floor_frac(vec.x, X, fx);
180 floor_frac(vec.y, Y, fy);
181 floor_frac(vec.z, Z, fz);
182
183 float u = fade(fx);
184 float v = fade(fy);
185 float w = fade(fz);
186
187 float r = tri_mix(noise_grad(hash_int3(X, Y, Z), fx, fy, fz),
188 noise_grad(hash_int3(X + 1, Y, Z), fx - 1, fy, fz),
189 noise_grad(hash_int3(X, Y + 1, Z), fx, fy - 1, fz),
190 noise_grad(hash_int3(X + 1, Y + 1, Z), fx - 1, fy - 1, fz),
191 noise_grad(hash_int3(X, Y, Z + 1), fx, fy, fz - 1),
192 noise_grad(hash_int3(X + 1, Y, Z + 1), fx - 1, fy, fz - 1),
193 noise_grad(hash_int3(X, Y + 1, Z + 1), fx, fy - 1, fz - 1),
194 noise_grad(hash_int3(X + 1, Y + 1, Z + 1), fx - 1, fy - 1, fz - 1),
195 u,
196 v,
197 w);
198
199 return r;
200}
201
203{
204 int X, Y, Z, W;
205 float fx, fy, fz, fw;
206
207 floor_frac(vec.x, X, fx);
208 floor_frac(vec.y, Y, fy);
209 floor_frac(vec.z, Z, fz);
210 floor_frac(vec.w, W, fw);
211
212 float u = fade(fx);
213 float v = fade(fy);
214 float t = fade(fz);
215 float s = fade(fw);
216
217 float r = quad_mix(
218 noise_grad(hash_int4(X, Y, Z, W), fx, fy, fz, fw),
219 noise_grad(hash_int4(X + 1, Y, Z, W), fx - 1.0f, fy, fz, fw),
220 noise_grad(hash_int4(X, Y + 1, Z, W), fx, fy - 1.0f, fz, fw),
221 noise_grad(hash_int4(X + 1, Y + 1, Z, W), fx - 1.0f, fy - 1.0f, fz, fw),
222 noise_grad(hash_int4(X, Y, Z + 1, W), fx, fy, fz - 1.0f, fw),
223 noise_grad(hash_int4(X + 1, Y, Z + 1, W), fx - 1.0f, fy, fz - 1.0f, fw),
224 noise_grad(hash_int4(X, Y + 1, Z + 1, W), fx, fy - 1.0f, fz - 1.0f, fw),
225 noise_grad(hash_int4(X + 1, Y + 1, Z + 1, W), fx - 1.0f, fy - 1.0f, fz - 1.0f, fw),
226 noise_grad(hash_int4(X, Y, Z, W + 1), fx, fy, fz, fw - 1.0f),
227 noise_grad(hash_int4(X + 1, Y, Z, W + 1), fx - 1.0f, fy, fz, fw - 1.0f),
228 noise_grad(hash_int4(X, Y + 1, Z, W + 1), fx, fy - 1.0f, fz, fw - 1.0f),
229 noise_grad(hash_int4(X + 1, Y + 1, Z, W + 1), fx - 1.0f, fy - 1.0f, fz, fw - 1.0f),
230 noise_grad(hash_int4(X, Y, Z + 1, W + 1), fx, fy, fz - 1.0f, fw - 1.0f),
231 noise_grad(hash_int4(X + 1, Y, Z + 1, W + 1), fx - 1.0f, fy, fz - 1.0f, fw - 1.0f),
232 noise_grad(hash_int4(X, Y + 1, Z + 1, W + 1), fx, fy - 1.0f, fz - 1.0f, fw - 1.0f),
234 hash_int4(X + 1, Y + 1, Z + 1, W + 1), fx - 1.0f, fy - 1.0f, fz - 1.0f, fw - 1.0f),
235 u,
236 v,
237 t,
238 s);
239
240 return r;
241}
242
243/* Remap the output of noise to a predictable range [-1, 1].
244 * The scale values were computed experimentally by the OSL developers.
245 */
246float noise_scale1(float result)
247{
248 return 0.2500f * result;
249}
250
251float noise_scale2(float result)
252{
253 return 0.6616f * result;
254}
255
256float noise_scale3(float result)
257{
258 return 0.9820f * result;
259}
260
261float noise_scale4(float result)
262{
263 return 0.8344f * result;
264}
265
266/* Safe Signed And Unsigned Noise */
267
268float snoise(float p)
269{
270 float precision_correction = 0.5f * float(abs(p) >= 1000000.0f);
271 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
272 * representation issues. */
273 p = compatible_mod(p, 100000.0f) + precision_correction;
274
275 return noise_scale1(noise_perlin(p));
276}
277
278float noise(float p)
279{
280 return 0.5f * snoise(p) + 0.5f;
281}
282
284{
285 float2 precision_correction = 0.5f * float2(float(abs(p.x) >= 1000000.0f),
286 float(abs(p.y) >= 1000000.0f));
287 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
288 * representation issues. This causes discontinuities every 100000.0f, however at such scales
289 * this usually shouldn't be noticeable. */
290 p = compatible_mod(p, 100000.0f) + precision_correction;
291
292 return noise_scale2(noise_perlin(p));
293}
294
295float noise(float2 p)
296{
297 return 0.5f * snoise(p) + 0.5f;
298}
299
301{
302 float3 precision_correction = 0.5f * float3(float(abs(p.x) >= 1000000.0f),
303 float(abs(p.y) >= 1000000.0f),
304 float(abs(p.z) >= 1000000.0f));
305 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
306 * representation issues. This causes discontinuities every 100000.0f, however at such scales
307 * this usually shouldn't be noticeable. */
308 p = compatible_mod(p, 100000.0f) + precision_correction;
309
310 return noise_scale3(noise_perlin(p));
311}
312
313float noise(float3 p)
314{
315 return 0.5f * snoise(p) + 0.5f;
316}
317
319{
320 float4 precision_correction = 0.5f * float4(float(abs(p.x) >= 1000000.0f),
321 float(abs(p.y) >= 1000000.0f),
322 float(abs(p.z) >= 1000000.0f),
323 float(abs(p.w) >= 1000000.0f));
324 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
325 * representation issues. This causes discontinuities every 100000.0f, however at such scales
326 * this usually shouldn't be noticeable. */
327 p = compatible_mod(p, 100000.0f) + precision_correction;
328
329 return noise_scale4(noise_perlin(p));
330}
331
332float noise(float4 p)
333{
334 return 0.5f * snoise(p) + 0.5f;
335}
#define Z
#define Y
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
uint hash_int2(int kx, int ky)
uint hash_int3(int kx, int ky, int kz)
uint hash_int(int kx)
uint hash_int4(int kx, int ky, int kz, int kw)
#define condition(cond)
#define force_inline
#define abs
#define floor
float noise_scale4(float result)
float noise_grad(uint hash, float x)
float noise(float p)
float quad_mix(float v0, float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8, float v9, float v10, float v11, float v12, float v13, float v14, float v15, float x, float y, float z, float w)
void floor_frac(float x, int &x_int, float &x_fract)
float noise_scale3(float result)
float tri_mix(float v0, float v1, float v2, float v3, float v4, float v5, float v6, float v7, float x, float y, float z)
float noise_perlin(float x)
float bi_mix(float v0, float v1, float v2, float v3, float x, float y)
float fade(float t)
float negate_if(float value, uint condition)
float snoise(float p)
float noise_scale2(float result)
float noise_scale1(float result)
float compatible_mod(float a, float b)
#define X(TYPE)
#define hash
Definition noise_c.cc:156
#define mix
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