Blender  V2.93
svm_map_range.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 /* Map Range Node */
20 
21 ccl_device_inline float smootherstep(float edge0, float edge1, float x)
22 {
23  x = clamp(safe_divide((x - edge0), (edge1 - edge0)), 0.0f, 1.0f);
24  return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f);
25 }
26 
27 ccl_device void svm_node_map_range(KernelGlobals *kg,
28  ShaderData *sd,
29  float *stack,
30  uint value_stack_offset,
31  uint parameters_stack_offsets,
32  uint results_stack_offsets,
33  int *offset)
34 {
35  uint from_min_stack_offset, from_max_stack_offset, to_min_stack_offset, to_max_stack_offset;
36  uint type_stack_offset, steps_stack_offset, result_stack_offset;
37  svm_unpack_node_uchar4(parameters_stack_offsets,
38  &from_min_stack_offset,
39  &from_max_stack_offset,
40  &to_min_stack_offset,
41  &to_max_stack_offset);
43  results_stack_offsets, &type_stack_offset, &steps_stack_offset, &result_stack_offset);
44 
45  uint4 defaults = read_node(kg, offset);
46  uint4 defaults2 = read_node(kg, offset);
47 
48  float value = stack_load_float(stack, value_stack_offset);
49  float from_min = stack_load_float_default(stack, from_min_stack_offset, defaults.x);
50  float from_max = stack_load_float_default(stack, from_max_stack_offset, defaults.y);
51  float to_min = stack_load_float_default(stack, to_min_stack_offset, defaults.z);
52  float to_max = stack_load_float_default(stack, to_max_stack_offset, defaults.w);
53  float steps = stack_load_float_default(stack, steps_stack_offset, defaults2.x);
54 
55  float result;
56 
57  if (from_max != from_min) {
58  float factor = value;
59  switch (type_stack_offset) {
60  default:
62  factor = (value - from_min) / (from_max - from_min);
63  break;
65  factor = (value - from_min) / (from_max - from_min);
66  factor = (steps > 0.0f) ? floorf(factor * (steps + 1.0f)) / steps : 0.0f;
67  break;
68  }
70  factor = (from_min > from_max) ? 1.0f - smoothstep(from_max, from_min, factor) :
71  smoothstep(from_min, from_max, factor);
72  break;
73  }
75  factor = (from_min > from_max) ? 1.0f - smootherstep(from_max, from_min, factor) :
76  smootherstep(from_min, from_max, factor);
77  break;
78  }
79  }
80  result = to_min + factor * (to_max - to_min);
81  }
82  else {
83  result = 0.0f;
84  }
85  stack_store_float(stack, result_stack_offset, result);
86 }
87 
MINLINE float safe_divide(float a, float b)
unsigned int uint
Definition: BLI_sys_types.h:83
@ NODE_MAP_RANGE_STEPPED
@ NODE_MAP_RANGE_SMOOTHERSTEP
@ NODE_MAP_RANGE_SMOOTHSTEP
@ NODE_MAP_RANGE_LINEAR
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 float stack_load_float(float *stack, uint a)
ccl_device_forceinline void svm_unpack_node_uchar3(uint i, uint *x, uint *y, uint *z)
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)
#define ccl_device
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define floorf(x)
ShaderData
static const int steps
Definition: sky_nishita.cpp:28
CCL_NAMESPACE_BEGIN ccl_device_inline float smootherstep(float edge0, float edge1, float x)
Definition: svm_map_range.h:21
ccl_device void svm_node_map_range(KernelGlobals *kg, ShaderData *sd, float *stack, uint value_stack_offset, uint parameters_stack_offsets, uint results_stack_offsets, int *offset)
Definition: svm_map_range.h:27
ccl_device_inline float smoothstep(float edge0, float edge1, float x)
Definition: util_math.h:298
ccl_device_inline int clamp(int a, int mn, int mx)
Definition: util_math.h:283