Blender V5.3
noisetex.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
9#include "kernel/svm/util.h"
10
12
13/* The following offset functions generate random offsets to be added to texture
14 * coordinates to act as a seed since the noise functions don't have seed values.
15 * A seed value is needed for generating distortion textures and color outputs.
16 * The offset's components are in the range [100, 200], not too high to cause
17 * bad precision and not too small to be noticeable. We use float seed because
18 * OSL only support float hashes.
19 */
20
22{
23 return 100.0f + hash_float_to_float(seed) * 100.0f;
24}
25
27{
28 return make_float2(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
29 100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f);
30}
31
33{
34 return make_float3(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
35 100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
36 100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f);
37}
38
40{
41 return make_float4(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
42 100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
43 100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f,
44 100.0f + hash_float2_to_float(make_float2(seed, 3.0f)) * 100.0f);
45}
46
47template<typename T>
49 const float detail,
50 const float roughness,
51 const float lacunarity,
52 const float offset,
53 const float gain,
54 const int type,
55 bool normalize)
56{
57 switch ((NodeNoiseType)type) {
59 return noise_multi_fractal(p, detail, roughness, lacunarity);
60 }
61 case NODE_NOISE_FBM: {
62 return noise_fbm(p, detail, roughness, lacunarity, normalize);
63 }
65 return noise_hybrid_multi_fractal(p, detail, roughness, lacunarity, offset, gain);
66 }
68 return noise_ridged_multi_fractal(p, detail, roughness, lacunarity, offset, gain);
69 }
71 return noise_hetero_terrain(p, detail, roughness, lacunarity, offset);
72 }
73 default: {
75 return 0.0;
76 }
77 }
78}
79
80ccl_device void noise_texture_1d(const float co,
81 const float detail,
82 const float roughness,
83 const float lacunarity,
84 const float offset,
85 const float gain,
86 const float distortion,
87 const int type,
88 bool normalize,
89 bool color_is_needed,
90 ccl_private float *value,
91 ccl_private float3 *color)
92{
93 float p = co;
94 if (distortion != 0.0f) {
95 p += snoise_1d(p + random_float_offset(0.0f)) * distortion;
96 }
97
98 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
99 if (color_is_needed) {
100 *color = make_float3(*value,
102 detail,
103 roughness,
104 lacunarity,
105 offset,
106 gain,
107 type,
108 normalize),
110 detail,
111 roughness,
112 lacunarity,
113 offset,
114 gain,
115 type,
116 normalize));
117 }
118}
119
121 const float detail,
122 const float roughness,
123 const float lacunarity,
124 const float offset,
125 const float gain,
126 const float distortion,
127 const int type,
128 const bool normalize,
129 const bool color_is_needed,
130 ccl_private float *value,
131 ccl_private float3 *color)
132{
133 float2 p = co;
134 if (distortion != 0.0f) {
135 p += make_float2(snoise_2d(p + random_float2_offset(0.0f)) * distortion,
136 snoise_2d(p + random_float2_offset(1.0f)) * distortion);
137 }
138
139 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
140 if (color_is_needed) {
141 *color = make_float3(*value,
143 detail,
144 roughness,
145 lacunarity,
146 offset,
147 gain,
148 type,
149 normalize),
151 detail,
152 roughness,
153 lacunarity,
154 offset,
155 gain,
156 type,
157 normalize));
158 }
159}
160
162 const float detail,
163 const float roughness,
164 const float lacunarity,
165 const float offset,
166 const float gain,
167 const float distortion,
168 const int type,
169 const bool normalize,
170 const bool color_is_needed,
171 ccl_private float *value,
172 ccl_private float3 *color)
173{
174 float3 p = co;
175 if (distortion != 0.0f) {
176 p += make_float3(snoise_3d(p + random_float3_offset(0.0f)) * distortion,
177 snoise_3d(p + random_float3_offset(1.0f)) * distortion,
178 snoise_3d(p + random_float3_offset(2.0f)) * distortion);
179 }
180
181 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
182 if (color_is_needed) {
183 *color = make_float3(*value,
185 detail,
186 roughness,
187 lacunarity,
188 offset,
189 gain,
190 type,
191 normalize),
193 detail,
194 roughness,
195 lacunarity,
196 offset,
197 gain,
198 type,
199 normalize));
200 }
201}
202
204 const float detail,
205 const float roughness,
206 const float lacunarity,
207 const float offset,
208 const float gain,
209 const float distortion,
210 const int type,
211 const bool normalize,
212 const bool color_is_needed,
213 ccl_private float *value,
214 ccl_private float3 *color)
215{
216 float4 p = co;
217 if (distortion != 0.0f) {
218 p += make_float4(snoise_4d(p + random_float4_offset(0.0f)) * distortion,
219 snoise_4d(p + random_float4_offset(1.0f)) * distortion,
220 snoise_4d(p + random_float4_offset(2.0f)) * distortion,
221 snoise_4d(p + random_float4_offset(3.0f)) * distortion);
222 }
223
224 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
225 if (color_is_needed) {
226 *color = make_float3(*value,
228 detail,
229 roughness,
230 lacunarity,
231 offset,
232 gain,
233 type,
234 normalize),
236 detail,
237 roughness,
238 lacunarity,
239 offset,
240 gain,
241 type,
242 normalize));
243 }
244}
245
248{
249 float3 vector = stack_load_float3(stack, node.vector);
250 float w = stack_load(stack, node.w);
251 const float scale = stack_load(stack, node.scale);
252 float detail = stack_load(stack, node.detail);
253 float roughness = stack_load(stack, node.roughness);
254 const float lacunarity = stack_load(stack, node.lacunarity);
255 const float offset = stack_load(stack, node.offset);
256 const float gain = stack_load(stack, node.gain);
257 const float distortion = stack_load(stack, node.distortion);
258
259 detail = clamp(detail, 0.0f, 15.0f);
260 roughness = fmaxf(roughness, 0.0f);
261
262 vector *= scale;
263 w *= scale;
264
265 float value;
266 float3 color;
267 switch (node.dimensions) {
268 case 1:
270 detail,
271 roughness,
272 lacunarity,
273 offset,
274 gain,
275 distortion,
276 node.noise_type,
277 node.normalize,
278 stack_valid(node.color_offset),
279 &value,
280 &color);
281 break;
282 case 2:
284 detail,
285 roughness,
286 lacunarity,
287 offset,
288 gain,
289 distortion,
290 node.noise_type,
291 node.normalize,
292 stack_valid(node.color_offset),
293 &value,
294 &color);
295 break;
296 case 3:
298 detail,
299 roughness,
300 lacunarity,
301 offset,
302 gain,
303 distortion,
304 node.noise_type,
305 node.normalize,
306 stack_valid(node.color_offset),
307 &value,
308 &color);
309 break;
310 case 4:
312 detail,
313 roughness,
314 lacunarity,
315 offset,
316 gain,
317 distortion,
318 node.noise_type,
319 node.normalize,
320 stack_valid(node.color_offset),
321 &value,
322 &color);
323 break;
324 default:
325 kernel_assert(0);
326 }
327
328 if (stack_valid(node.value_offset)) {
329 stack_store_float(stack, node.value_offset, value);
330 }
331 if (stack_valid(node.color_offset)) {
332 stack_store_float3(stack, node.color_offset, color);
333 }
334}
335
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline bool stack_valid(const uint a)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
#define kernel_assert(cond)
#define ccl_restrict
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float hash_float_to_float(float k)
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float noise_fbm(T co, float detail, float roughness, float lacunarity, float, float, bool normalize)
float noise_multi_fractal(T co, float detail, float roughness, float lacunarity, float, float, bool)
float noise_hybrid_multi_fractal(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool)
float noise_ridged_multi_fractal(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool)
float noise_hetero_terrain(T co, float detail, float roughness, float lacunarity, float offset, float, bool)
float random_float_offset(float seed)
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline float hash_float2_to_float(const float2 k)
Definition hash.h:222
NodeNoiseType
@ NODE_NOISE_FBM
@ NODE_NOISE_HYBRID_MULTIFRACTAL
@ NODE_NOISE_RIDGED_MULTIFRACTAL
@ NODE_NOISE_HETERO_TERRAIN
@ NODE_NOISE_MULTIFRACTAL
#define T
ccl_device_inline float snoise_1d(float p)
Definition noise.h:691
ccl_device_inline float snoise_2d(float2 p)
Definition noise.h:707
ccl_device_inline float snoise_3d(float3 p)
Definition noise.h:725
ccl_device_inline float snoise_4d(float4 p)
Definition noise.h:743
ccl_device_noinline void svm_node_tex_noise(ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexNoise &ccl_restrict node)
Definition noisetex.h:246
ccl_device_inline float2 random_float2_offset(const float seed)
Definition noisetex.h:26
ccl_device void noise_texture_2d(const float2 co, const float detail, const float roughness, const float lacunarity, const float offset, const float gain, const float distortion, const int type, const bool normalize, const bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:120
ccl_device void noise_texture_4d(const float4 co, const float detail, const float roughness, const float lacunarity, const float offset, const float gain, const float distortion, const int type, const bool normalize, const bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:203
ccl_device void noise_texture_1d(const float co, const float detail, const float roughness, const float lacunarity, const float offset, const float gain, const float distortion, const int type, bool normalize, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:80
ccl_device void noise_texture_3d(const float3 co, const float detail, const float roughness, const float lacunarity, const float offset, const float gain, const float distortion, const int type, const bool normalize, const bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:161
ccl_device float noise_select(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain, const int type, bool normalize)
Definition noisetex.h:48
ccl_device_inline float4 random_float4_offset(const float seed)
Definition noisetex.h:39
ccl_device_inline float3 random_float3_offset(const float seed)
Definition noisetex.h:32
#define ccl_device
#define fmaxf
#define make_float2
#define make_float4