Blender V5.3
map_range.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
8#include "kernel/svm/util.h"
9
11
12/* Map Range Node */
13
14ccl_device_inline float smootherstep(const float edge0, const float edge1, float x)
15{
16 x = clamp(safe_divide((x - edge0), (edge1 - edge0)), 0.0f, 1.0f);
17 return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f);
18}
19
22{
23 const float value = stack_load(stack, node.value);
24 const float from_min = stack_load(stack, node.from_min);
25 const float from_max = stack_load(stack, node.from_max);
26 const float to_min = stack_load(stack, node.to_min);
27 const float to_max = stack_load(stack, node.to_max);
28 const float steps = stack_load(stack, node.steps);
29
30 float result;
31
32 if (from_max != from_min) {
33 float factor = value;
34 switch (node.range_type) {
35 default:
37 factor = (value - from_min) / (from_max - from_min);
38 break;
40 factor = (value - from_min) / (from_max - from_min);
41 factor = (steps > 0.0f) ? floorf(factor * (steps + 1.0f)) / steps : 0.0f;
42 break;
43 }
45 factor = (from_min > from_max) ? 1.0f - smoothstep(from_max, from_min, factor) :
46 smoothstep(from_min, from_max, factor);
47 break;
48 }
50 factor = (from_min > from_max) ? 1.0f - smootherstep(from_max, from_min, factor) :
51 smootherstep(from_min, from_max, factor);
52 break;
53 }
54 }
55 result = to_min + factor * (to_max - to_min);
56 }
57 else {
58 result = 0.0f;
59 }
60 stack_store_float(stack, node.result_offset, result);
61}
62
64 ccl_private float *ccl_restrict stack,
66{
67 const float3 value = stack_load(stack, node.value);
68 const float3 from_min = stack_load(stack, node.from_min);
69 const float3 from_max = stack_load(stack, node.from_max);
70 const float3 to_min = stack_load(stack, node.to_min);
71 const float3 to_max = stack_load(stack, node.to_max);
72 const float3 steps = stack_load(stack, node.steps);
73
74 const int use_clamp = (node.range_type == NODE_MAP_RANGE_SMOOTHSTEP ||
75 node.range_type == NODE_MAP_RANGE_SMOOTHERSTEP) ?
76 0 :
77 node.use_clamp;
78 float3 result;
79 float3 factor = value;
80 switch (node.range_type) {
81 default:
83 factor = safe_divide((value - from_min), (from_max - from_min));
84 break;
86 factor = safe_divide((value - from_min), (from_max - from_min));
87 factor = make_float3((steps.x > 0.0f) ? floorf(factor.x * (steps.x + 1.0f)) / steps.x : 0.0f,
88 (steps.y > 0.0f) ? floorf(factor.y * (steps.y + 1.0f)) / steps.y : 0.0f,
89 (steps.z > 0.0f) ? floorf(factor.z * (steps.z + 1.0f)) / steps.z :
90 0.0f);
91 break;
92 }
94 factor = safe_divide((value - from_min), (from_max - from_min));
95 factor = clamp(factor, zero_float3(), one_float3());
96 factor = (make_float3(3.0f, 3.0f, 3.0f) - 2.0f * factor) * (factor * factor);
97 break;
98 }
100 factor = safe_divide((value - from_min), (from_max - from_min));
101 factor = clamp(factor, zero_float3(), one_float3());
102 factor = factor * factor * factor * (factor * (factor * 6.0f - 15.0f) + 10.0f);
103 break;
104 }
105 }
106 result = to_min + factor * (to_max - to_min);
107 if (use_clamp > 0) {
108 result.x = (to_min.x > to_max.x) ? clamp(result.x, to_max.x, to_min.x) :
109 clamp(result.x, to_min.x, to_max.x);
110 result.y = (to_min.y > to_max.y) ? clamp(result.y, to_max.y, to_min.y) :
111 clamp(result.y, to_min.y, to_max.y);
112 result.z = (to_min.z > to_max.z) ? clamp(result.z, to_max.z, to_min.z) :
113 clamp(result.z, to_min.z, to_max.z);
114 }
115
116 stack_store_float3(stack, node.result_offset, result);
117}
118
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
#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)
T smoothstep(T, T, T) RET
constexpr T clamp(T, U, U) RET
float smootherstep(float edge0, float edge1, float x)
float safe_divide(float a, float b)
@ NODE_MAP_RANGE_STEPPED
@ NODE_MAP_RANGE_SMOOTHERSTEP
@ NODE_MAP_RANGE_SMOOTHSTEP
@ NODE_MAP_RANGE_LINEAR
ccl_device_noinline void svm_node_map_range(ccl_private float *ccl_restrict stack, const ccl_global SVMNodeMapRange &ccl_restrict node)
Definition map_range.h:20
ccl_device_noinline void svm_node_vector_map_range(ccl_private float *ccl_restrict stack, const ccl_global SVMNodeVectorMapRange &ccl_restrict node)
Definition map_range.h:63
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define floorf
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136