Blender  V2.93
svm_noisetex.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 /* The following offset functions generate random offsets to be added to texture
20  * coordinates to act as a seed since the noise functions don't have seed values.
21  * A seed value is needed for generating distortion textures and color outputs.
22  * The offset's components are in the range [100, 200], not too high to cause
23  * bad precision and not to small to be noticeable. We use float seed because
24  * OSL only support float hashes.
25  */
26 
28 {
29  return 100.0f + hash_float_to_float(seed) * 100.0f;
30 }
31 
33 {
34  return make_float2(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 }
37 
39 {
40  return make_float3(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
41  100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
42  100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f);
43 }
44 
46 {
47  return make_float4(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
48  100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
49  100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f,
50  100.0f + hash_float2_to_float(make_float2(seed, 3.0f)) * 100.0f);
51 }
52 
54  float detail,
55  float roughness,
56  float distortion,
57  bool color_is_needed,
58  float *value,
59  float3 *color)
60 {
61  float p = co;
62  if (distortion != 0.0f) {
63  p += snoise_1d(p + random_float_offset(0.0f)) * distortion;
64  }
65 
66  *value = fractal_noise_1d(p, detail, roughness);
67  if (color_is_needed) {
68  *color = make_float3(*value,
70  fractal_noise_1d(p + random_float_offset(2.0f), detail, roughness));
71  }
72 }
73 
75  float detail,
76  float roughness,
77  float distortion,
78  bool color_is_needed,
79  float *value,
80  float3 *color)
81 {
82  float2 p = co;
83  if (distortion != 0.0f) {
84  p += make_float2(snoise_2d(p + random_float2_offset(0.0f)) * distortion,
85  snoise_2d(p + random_float2_offset(1.0f)) * distortion);
86  }
87 
88  *value = fractal_noise_2d(p, detail, roughness);
89  if (color_is_needed) {
90  *color = make_float3(*value,
92  fractal_noise_2d(p + random_float2_offset(3.0f), detail, roughness));
93  }
94 }
95 
97  float detail,
98  float roughness,
99  float distortion,
100  bool color_is_needed,
101  float *value,
102  float3 *color)
103 {
104  float3 p = co;
105  if (distortion != 0.0f) {
106  p += make_float3(snoise_3d(p + random_float3_offset(0.0f)) * distortion,
107  snoise_3d(p + random_float3_offset(1.0f)) * distortion,
108  snoise_3d(p + random_float3_offset(2.0f)) * distortion);
109  }
110 
111  *value = fractal_noise_3d(p, detail, roughness);
112  if (color_is_needed) {
113  *color = make_float3(*value,
114  fractal_noise_3d(p + random_float3_offset(3.0f), detail, roughness),
115  fractal_noise_3d(p + random_float3_offset(4.0f), detail, roughness));
116  }
117 }
118 
120  float detail,
121  float roughness,
122  float distortion,
123  bool color_is_needed,
124  float *value,
125  float3 *color)
126 {
127  float4 p = co;
128  if (distortion != 0.0f) {
129  p += make_float4(snoise_4d(p + random_float4_offset(0.0f)) * distortion,
130  snoise_4d(p + random_float4_offset(1.0f)) * distortion,
131  snoise_4d(p + random_float4_offset(2.0f)) * distortion,
132  snoise_4d(p + random_float4_offset(3.0f)) * distortion);
133  }
134 
135  *value = fractal_noise_4d(p, detail, roughness);
136  if (color_is_needed) {
137  *color = make_float3(*value,
138  fractal_noise_4d(p + random_float4_offset(4.0f), detail, roughness),
139  fractal_noise_4d(p + random_float4_offset(5.0f), detail, roughness));
140  }
141 }
142 
143 ccl_device void svm_node_tex_noise(KernelGlobals *kg,
144  ShaderData *sd,
145  float *stack,
146  uint dimensions,
147  uint offsets1,
148  uint offsets2,
149  int *offset)
150 {
151  uint vector_stack_offset, w_stack_offset, scale_stack_offset;
152  uint detail_stack_offset, roughness_stack_offset, distortion_stack_offset;
153  uint value_stack_offset, color_stack_offset;
154 
156  offsets1, &vector_stack_offset, &w_stack_offset, &scale_stack_offset, &detail_stack_offset);
157  svm_unpack_node_uchar4(offsets2,
158  &roughness_stack_offset,
159  &distortion_stack_offset,
160  &value_stack_offset,
161  &color_stack_offset);
162 
163  uint4 defaults1 = read_node(kg, offset);
164  uint4 defaults2 = read_node(kg, offset);
165 
166  float3 vector = stack_load_float3(stack, vector_stack_offset);
167  float w = stack_load_float_default(stack, w_stack_offset, defaults1.x);
168  float scale = stack_load_float_default(stack, scale_stack_offset, defaults1.y);
169  float detail = stack_load_float_default(stack, detail_stack_offset, defaults1.z);
170  float roughness = stack_load_float_default(stack, roughness_stack_offset, defaults1.w);
171  float distortion = stack_load_float_default(stack, distortion_stack_offset, defaults2.x);
172 
173  vector *= scale;
174  w *= scale;
175 
176  float value;
177  float3 color;
178  switch (dimensions) {
179  case 1:
181  w, detail, roughness, distortion, stack_valid(color_stack_offset), &value, &color);
182  break;
183  case 2:
185  detail,
186  roughness,
187  distortion,
188  stack_valid(color_stack_offset),
189  &value,
190  &color);
191  break;
192  case 3:
194  vector, detail, roughness, distortion, stack_valid(color_stack_offset), &value, &color);
195  break;
196  case 4:
198  detail,
199  roughness,
200  distortion,
201  stack_valid(color_stack_offset),
202  &value,
203  &color);
204  break;
205  default:
206  kernel_assert(0);
207  }
208 
209  if (stack_valid(value_stack_offset)) {
210  stack_store_float(stack, value_stack_offset, value);
211  }
212  if (stack_valid(color_stack_offset)) {
213  stack_store_float3(stack, color_stack_offset, color);
214  }
215 }
216 
unsigned int uint
Definition: BLI_sys_types.h:83
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
static unsigned long seed
Definition: btSoftBody.h:39
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals *kg, int *offset)
ccl_device_inline float stack_load_float_default(float *stack, uint a, uint value)
ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
ccl_device_inline void stack_store_float(float *stack, uint a, float f)
ccl_device_inline bool stack_valid(uint a)
#define kernel_assert(cond)
#define ccl_device
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define make_float2(x, y)
#define make_float4(x, y, z, w)
#define make_float3(x, y, z)
ShaderData
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
float hash_float_to_float(float k)
Definition: node_hash.h:9
ccl_device_noinline float fractal_noise_4d(float4 p, float octaves, float roughness)
ccl_device_noinline float fractal_noise_2d(float2 p, float octaves, float roughness)
ccl_device_noinline float fractal_noise_3d(float3 p, float octaves, float roughness)
CCL_NAMESPACE_BEGIN ccl_device_noinline float fractal_noise_1d(float p, float octaves, float roughness)
ccl_device_inline float snoise_1d(float p)
Definition: svm_noise.h:702
ccl_device_inline float snoise_2d(float2 p)
Definition: svm_noise.h:712
ccl_device_inline float snoise_3d(float3 p)
Definition: svm_noise.h:722
ccl_device_inline float snoise_4d(float4 p)
Definition: svm_noise.h:732
ccl_device_inline float4 random_float4_offset(float seed)
Definition: svm_noisetex.h:45
ccl_device void noise_texture_3d(float3 co, float detail, float roughness, float distortion, bool color_is_needed, float *value, float3 *color)
Definition: svm_noisetex.h:96
CCL_NAMESPACE_BEGIN ccl_device_inline float random_float_offset(float seed)
Definition: svm_noisetex.h:27
ccl_device_inline float2 random_float2_offset(float seed)
Definition: svm_noisetex.h:32
ccl_device void noise_texture_4d(float4 co, float detail, float roughness, float distortion, bool color_is_needed, float *value, float3 *color)
Definition: svm_noisetex.h:119
ccl_device_inline float3 random_float3_offset(float seed)
Definition: svm_noisetex.h:38
ccl_device void svm_node_tex_noise(KernelGlobals *kg, ShaderData *sd, float *stack, uint dimensions, uint offsets1, uint offsets2, int *offset)
Definition: svm_noisetex.h:143
ccl_device void noise_texture_2d(float2 co, float detail, float roughness, float distortion, bool color_is_needed, float *value, float3 *color)
Definition: svm_noisetex.h:74
ccl_device void noise_texture_1d(float co, float detail, float roughness, float distortion, bool color_is_needed, float *value, float3 *color)
Definition: svm_noisetex.h:53
ccl_device_inline float hash_float2_to_float(float2 k)
Definition: util_hash.h:157