Blender  V3.3
noisetex.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
9 
10 /* The following offset functions generate random offsets to be added to texture
11  * coordinates to act as a seed since the noise functions don't have seed values.
12  * A seed value is needed for generating distortion textures and color outputs.
13  * The offset's components are in the range [100, 200], not too high to cause
14  * bad precision and not too small to be noticeable. We use float seed because
15  * OSL only support float hashes.
16  */
17 
19 {
20  return 100.0f + hash_float_to_float(seed) * 100.0f;
21 }
22 
24 {
25  return make_float2(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
26  100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f);
27 }
28 
30 {
31  return make_float3(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
32  100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
33  100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f);
34 }
35 
37 {
38  return make_float4(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
39  100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
40  100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f,
41  100.0f + hash_float2_to_float(make_float2(seed, 3.0f)) * 100.0f);
42 }
43 
45  float detail,
46  float roughness,
47  float distortion,
48  bool color_is_needed,
49  ccl_private float *value,
51 {
52  float p = co;
53  if (distortion != 0.0f) {
54  p += snoise_1d(p + random_float_offset(0.0f)) * distortion;
55  }
56 
57  *value = fractal_noise_1d(p, detail, roughness);
58  if (color_is_needed) {
59  *color = make_float3(*value,
61  fractal_noise_1d(p + random_float_offset(2.0f), detail, roughness));
62  }
63 }
64 
66  float detail,
67  float roughness,
68  float distortion,
69  bool color_is_needed,
70  ccl_private float *value,
72 {
73  float2 p = co;
74  if (distortion != 0.0f) {
75  p += make_float2(snoise_2d(p + random_float2_offset(0.0f)) * distortion,
76  snoise_2d(p + random_float2_offset(1.0f)) * distortion);
77  }
78 
79  *value = fractal_noise_2d(p, detail, roughness);
80  if (color_is_needed) {
81  *color = make_float3(*value,
83  fractal_noise_2d(p + random_float2_offset(3.0f), detail, roughness));
84  }
85 }
86 
88  float detail,
89  float roughness,
90  float distortion,
91  bool color_is_needed,
92  ccl_private float *value,
94 {
95  float3 p = co;
96  if (distortion != 0.0f) {
97  p += make_float3(snoise_3d(p + random_float3_offset(0.0f)) * distortion,
98  snoise_3d(p + random_float3_offset(1.0f)) * distortion,
99  snoise_3d(p + random_float3_offset(2.0f)) * distortion);
100  }
101 
102  *value = fractal_noise_3d(p, detail, roughness);
103  if (color_is_needed) {
104  *color = make_float3(*value,
105  fractal_noise_3d(p + random_float3_offset(3.0f), detail, roughness),
106  fractal_noise_3d(p + random_float3_offset(4.0f), detail, roughness));
107  }
108 }
109 
111  float detail,
112  float roughness,
113  float distortion,
114  bool color_is_needed,
115  ccl_private float *value,
117 {
118  float4 p = co;
119  if (distortion != 0.0f) {
120  p += make_float4(snoise_4d(p + random_float4_offset(0.0f)) * distortion,
121  snoise_4d(p + random_float4_offset(1.0f)) * distortion,
122  snoise_4d(p + random_float4_offset(2.0f)) * distortion,
123  snoise_4d(p + random_float4_offset(3.0f)) * distortion);
124  }
125 
126  *value = fractal_noise_4d(p, detail, roughness);
127  if (color_is_needed) {
128  *color = make_float3(*value,
129  fractal_noise_4d(p + random_float4_offset(4.0f), detail, roughness),
130  fractal_noise_4d(p + random_float4_offset(5.0f), detail, roughness));
131  }
132 }
133 
136  ccl_private float *stack,
137  uint dimensions,
138  uint offsets1,
139  uint offsets2,
140  int offset)
141 {
142  uint vector_stack_offset, w_stack_offset, scale_stack_offset;
143  uint detail_stack_offset, roughness_stack_offset, distortion_stack_offset;
144  uint value_stack_offset, color_stack_offset;
145 
147  offsets1, &vector_stack_offset, &w_stack_offset, &scale_stack_offset, &detail_stack_offset);
148  svm_unpack_node_uchar4(offsets2,
149  &roughness_stack_offset,
150  &distortion_stack_offset,
151  &value_stack_offset,
152  &color_stack_offset);
153 
154  uint4 defaults1 = read_node(kg, &offset);
155  uint4 defaults2 = read_node(kg, &offset);
156 
157  float3 vector = stack_load_float3(stack, vector_stack_offset);
158  float w = stack_load_float_default(stack, w_stack_offset, defaults1.x);
159  float scale = stack_load_float_default(stack, scale_stack_offset, defaults1.y);
160  float detail = stack_load_float_default(stack, detail_stack_offset, defaults1.z);
161  float roughness = stack_load_float_default(stack, roughness_stack_offset, defaults1.w);
162  float distortion = stack_load_float_default(stack, distortion_stack_offset, defaults2.x);
163 
164  vector *= scale;
165  w *= scale;
166 
167  float value;
168  float3 color;
169  switch (dimensions) {
170  case 1:
172  w, detail, roughness, distortion, stack_valid(color_stack_offset), &value, &color);
173  break;
174  case 2:
176  detail,
177  roughness,
178  distortion,
179  stack_valid(color_stack_offset),
180  &value,
181  &color);
182  break;
183  case 3:
185  vector, detail, roughness, distortion, stack_valid(color_stack_offset), &value, &color);
186  break;
187  case 4:
189  detail,
190  roughness,
191  distortion,
192  stack_valid(color_stack_offset),
193  &value,
194  &color);
195  break;
196  default:
197  kernel_assert(0);
198  }
199 
200  if (stack_valid(value_stack_offset)) {
201  stack_store_float(stack, value_stack_offset, value);
202  }
203  if (stack_valid(color_stack_offset)) {
204  stack_store_float3(stack, color_stack_offset, color);
205  }
206  return offset;
207 }
208 
unsigned int uint
Definition: BLI_sys_types.h:67
float float4[4]
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
static unsigned long seed
Definition: btSoftBody.h:39
#define kernel_assert(cond)
Definition: cpu/compat.h:34
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define ccl_device_noinline
Definition: cuda/compat.h:40
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
const KernelGlobalsCPU *ccl_restrict KernelGlobals
ccl_device_noinline float fractal_noise_4d(float4 p, float octaves, float roughness)
Definition: fractal_noise.h:98
ccl_device_noinline float fractal_noise_2d(float2 p, float octaves, float roughness)
Definition: fractal_noise.h:40
ccl_device_noinline float fractal_noise_3d(float3 p, float octaves, float roughness)
Definition: fractal_noise.h:69
CCL_NAMESPACE_BEGIN ccl_device_noinline float fractal_noise_1d(float p, float octaves, float roughness)
Definition: fractal_noise.h:11
ccl_device_inline float hash_float2_to_float(float2 k)
Definition: hash.h:144
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
ccl_device_inline void stack_store_float3(ccl_private float *stack, uint a, float3 f)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(ccl_private float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *offset)
ccl_device_inline float stack_load_float_default(ccl_private float *stack, uint a, uint value)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(uint a)
ShaderData
Definition: kernel/types.h:925
#define make_float2(x, y)
Definition: metal/compat.h:203
#define make_float4(x, y, z, w)
Definition: metal/compat.h:205
#define make_float3(x, y, z)
Definition: metal/compat.h:204
float hash_float_to_float(float k)
Definition: noise.cc:178
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
ccl_device_inline float snoise_1d(float p)
Definition: noise.h:680
ccl_device_inline float snoise_2d(float2 p)
Definition: noise.h:690
ccl_device_inline float snoise_3d(float3 p)
Definition: noise.h:700
ccl_device_inline float snoise_4d(float4 p)
Definition: noise.h:710
ccl_device_inline float4 random_float4_offset(float seed)
Definition: noisetex.h:36
CCL_NAMESPACE_BEGIN ccl_device_inline float random_float_offset(float seed)
Definition: noisetex.h:18
ccl_device_noinline int svm_node_tex_noise(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint dimensions, uint offsets1, uint offsets2, int offset)
Definition: noisetex.h:134
ccl_device void noise_texture_3d(float3 co, float detail, float roughness, float distortion, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition: noisetex.h:87
ccl_device_inline float2 random_float2_offset(float seed)
Definition: noisetex.h:23
ccl_device_inline float3 random_float3_offset(float seed)
Definition: noisetex.h:29
ccl_device void noise_texture_4d(float4 co, float detail, float roughness, float distortion, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition: noisetex.h:110
ccl_device void noise_texture_2d(float2 co, float detail, float roughness, float distortion, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition: noisetex.h:65
ccl_device void noise_texture_1d(float co, float detail, float roughness, float distortion, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition: noisetex.h:44
uint x
Definition: types_uint4.h:15
uint y
Definition: types_uint4.h:15
uint z
Definition: types_uint4.h:15
uint w
Definition: types_uint4.h:15