Blender V5.3
math_functions.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
12
13namespace blender::nodes {
14
15static const mf::MultiFunction *get_base_multi_function(const bNode &node)
16{
17 const int mode = node.custom1;
19 if (!info) {
20 return nullptr;
21 }
23}
24
25class ClampWrapperFunction : public mf::MultiFunction {
26 private:
27 const mf::MultiFunction &fn_;
28
29 public:
30 ClampWrapperFunction(const mf::MultiFunction &fn) : fn_(fn)
31 {
32 this->set_signature(&fn.signature());
33 }
34
35 void call(const IndexMask &mask, mf::Params params, mf::Context context) const override
36 {
37 fn_.call(mask, params, context);
38
39 /* Assumes the output parameter is the last one. */
40 const int output_param_index = this->param_amount() - 1;
41 /* This has actually been initialized in the call above. */
42 MutableSpan<float> results = params.uninitialized_single_output<float>(output_param_index);
43
44 mask.foreach_index_optimized<int>([&](const int i) {
45 float &value = results[i];
46 CLAMP(value, 0.0f, 1.0f);
47 });
48 }
49
50 void hash_unique(UniqueHashBytes &hash) const override
51 {
52 static constexpr int8_t id = 0;
53 hash.add(&id);
54 fn_.hash_unique(hash);
55 }
56};
57
59{
60 const mf::MultiFunction *base_function = get_base_multi_function(builder.node());
61
62 const bool clamp_output = builder.node().custom2 != 0;
63 if (clamp_output) {
65 }
66 else {
67 builder.set_matching_fn(base_function);
68 }
69}
70
72{
73#define RETURN_OPERATION_INFO(title_case_name, shader_name, multi_function_name) \
74 { \
75 static const FloatMathOperationInfo info{title_case_name, shader_name, multi_function_name}; \
76 return &info; \
77 } \
78 ((void)0)
79
80 switch (operation) {
81 case NODE_MATH_ADD:
82 RETURN_OPERATION_INFO("Add", "math_add", "float + float"_ustr);
84 RETURN_OPERATION_INFO("Subtract", "math_subtract", "float - float"_ustr);
86 RETURN_OPERATION_INFO("Multiply", "math_multiply", "float * float"_ustr);
88 RETURN_OPERATION_INFO("Divide", "math_divide", "float / float"_ustr);
89 case NODE_MATH_SINE:
90 RETURN_OPERATION_INFO("Sine", "math_sine", "sin(float)"_ustr);
92 RETURN_OPERATION_INFO("Cosine", "math_cosine", "cos(float)"_ustr);
94 RETURN_OPERATION_INFO("Tangent", "math_tangent", "tan(float)"_ustr);
96 RETURN_OPERATION_INFO("Arc Sine", "math_arcsine", "asin(float)"_ustr);
98 RETURN_OPERATION_INFO("Arc Cosine", "math_arccosine", "acos(float)"_ustr);
100 RETURN_OPERATION_INFO("Arc Tangent", "math_arctangent", "atan(float)"_ustr);
101 case NODE_MATH_POWER:
102 RETURN_OPERATION_INFO("Power", "math_power", "float ** float"_ustr);
104 RETURN_OPERATION_INFO("Logarithm", "math_logarithm", "log(float, float)"_ustr);
106 RETURN_OPERATION_INFO("Minimum", "math_minimum", "min(float, float)"_ustr);
108 RETURN_OPERATION_INFO("Maximum", "math_maximum", "max(float, float)"_ustr);
109 case NODE_MATH_ROUND:
110 RETURN_OPERATION_INFO("Round", "math_round", "round(float)"_ustr);
112 RETURN_OPERATION_INFO("Less Than", "math_less_than", "float(float < float)"_ustr);
114 RETURN_OPERATION_INFO("Greater Than", "math_greater_than", "float(float > float)"_ustr);
115 case NODE_MATH_MODULO:
116 RETURN_OPERATION_INFO("Modulo", "math_modulo", "float % float"_ustr);
119 "Floored Modulo", "math_floored_modulo", "floor_mod(float, float)"_ustr);
121 RETURN_OPERATION_INFO("Absolute", "math_absolute", "abs(float)"_ustr);
123 RETURN_OPERATION_INFO("Arc Tangent 2", "math_arctan2", "atan2(float, float)"_ustr);
124 case NODE_MATH_FLOOR:
125 RETURN_OPERATION_INFO("Floor", "math_floor", "floor(float)"_ustr);
126 case NODE_MATH_CEIL:
127 RETURN_OPERATION_INFO("Ceil", "math_ceil", "ceil(float)"_ustr);
129 RETURN_OPERATION_INFO("Fraction", "math_fraction", "frac(float)"_ustr);
130 case NODE_MATH_SQRT:
131 RETURN_OPERATION_INFO("Sqrt", "math_sqrt", "sqrt(float)"_ustr);
133 RETURN_OPERATION_INFO("Inverse Sqrt", "math_inversesqrt", "inverse_sqrt(float)"_ustr);
134 case NODE_MATH_SIGN:
135 RETURN_OPERATION_INFO("Sign", "math_sign", "sign(float)"_ustr);
137 RETURN_OPERATION_INFO("Exponent", "math_exponent", "exp(float)"_ustr);
139 RETURN_OPERATION_INFO("Radians", "math_radians", "radians(float)"_ustr);
141 RETURN_OPERATION_INFO("Degrees", "math_degrees", "degrees(float)"_ustr);
142 case NODE_MATH_SINH:
143 RETURN_OPERATION_INFO("Hyperbolic Sine", "math_sinh", "sinh(float)"_ustr);
144 case NODE_MATH_COSH:
145 RETURN_OPERATION_INFO("Hyperbolic Cosine", "math_cosh", "cosh(float)"_ustr);
146 case NODE_MATH_TANH:
147 RETURN_OPERATION_INFO("Hyperbolic Tangent", "math_tanh", "tanh(float)"_ustr);
148 case NODE_MATH_TRUNC:
149 RETURN_OPERATION_INFO("Truncate", "math_trunc", "trunc(float)"_ustr);
150 case NODE_MATH_SNAP:
151 RETURN_OPERATION_INFO("Snap", "math_snap", "snap(float, float)"_ustr);
152 case NODE_MATH_WRAP:
153 RETURN_OPERATION_INFO("Wrap", "math_wrap", "wrap(float, float, float)"_ustr);
155 RETURN_OPERATION_INFO("Compare", "math_compare", "compare(float, float, float)"_ustr);
157 RETURN_OPERATION_INFO("Multiply Add", "math_multiply_add", "float * float + float"_ustr);
159 RETURN_OPERATION_INFO("Ping Pong", "math_pingpong", "pingpong(float, float)"_ustr);
162 "Smooth Min", "math_smoothmin", "smooth_min(float, float, float)"_ustr);
165 "Smooth Max", "math_smoothmax", "smooth_max(float, float, float)"_ustr);
166 }
167
168#undef RETURN_OPERATION_INFO
169
170 return nullptr;
171}
172
174{
175
176#define RETURN_OPERATION_INFO(title_case_name, shader_name) \
177 { \
178 static const FloatMathOperationInfo info{title_case_name, shader_name}; \
179 return &info; \
180 } \
181 ((void)0)
182
183 switch (operation) {
185 RETURN_OPERATION_INFO("Less Than", "math_less_than");
187 RETURN_OPERATION_INFO("Less Than or Equal", "math_less_equal");
189 RETURN_OPERATION_INFO("Greater Than", "math_greater_than");
191 RETURN_OPERATION_INFO("Greater Than or Equal", "math_greater_equal");
193 RETURN_OPERATION_INFO("Equal", "math_equal");
195 RETURN_OPERATION_INFO("Not Equal", "math_not_equal");
196 }
197
198#undef RETURN_OPERATION_INFO
199
200 return nullptr;
201}
202
204{
205
206#define RETURN_OPERATION_INFO(title_case_name, shader_name, multi_function_name) \
207 { \
208 static const FloatMathOperationInfo info{title_case_name, shader_name, multi_function_name}; \
209 return &info; \
210 } \
211 ((void)0)
212
213 switch (operation) {
215 RETURN_OPERATION_INFO("Add", "vector_math_add", "float3 + float3"_ustr);
217 RETURN_OPERATION_INFO("Subtract", "vector_math_subtract", "float3 - float3"_ustr);
219 RETURN_OPERATION_INFO("Multiply", "vector_math_multiply", "float3 * float3"_ustr);
221 RETURN_OPERATION_INFO("Divide", "vector_math_divide", "float3 / float3"_ustr);
224 "Cross Product", "vector_math_cross", "cross_product(float3, float3)"_ustr);
226 RETURN_OPERATION_INFO("Project", "vector_math_project", "project(float3, float3)"_ustr);
228 RETURN_OPERATION_INFO("Reflect", "vector_math_reflect", "reflect(float3, float3)"_ustr);
230 RETURN_OPERATION_INFO("Dot Product", "vector_math_dot", "dot_product(float3, float3)"_ustr);
232 RETURN_OPERATION_INFO("Distance", "vector_math_distance", "distance(float3, float3)"_ustr);
234 RETURN_OPERATION_INFO("Length", "vector_math_length", "length(float3)"_ustr);
236 RETURN_OPERATION_INFO("Scale", "vector_math_scale", "float3 * float"_ustr);
238 RETURN_OPERATION_INFO("Normalize", "vector_math_normalize", "normalize(float3)"_ustr);
240 RETURN_OPERATION_INFO("Snap", "vector_math_snap", "snap(float3, float3)"_ustr);
242 RETURN_OPERATION_INFO("Round", "vector_math_round", "round(float3)"_ustr);
244 RETURN_OPERATION_INFO("Floor", "vector_math_floor", "floor(float3)"_ustr);
246 RETURN_OPERATION_INFO("Ceiling", "vector_math_ceil", "ceil(float3)"_ustr);
248 RETURN_OPERATION_INFO("Modulo", "vector_math_modulo", "float3 % float3"_ustr);
250 RETURN_OPERATION_INFO("Fraction", "vector_math_fraction", "frac(float3)"_ustr);
252 RETURN_OPERATION_INFO("Absolute", "vector_math_absolute", "abs(float3)"_ustr);
254 RETURN_OPERATION_INFO("Minimum", "vector_math_minimum", "min(float3, float3)"_ustr);
256 RETURN_OPERATION_INFO("Maximum", "vector_math_maximum", "max(float3, float3)"_ustr);
258 RETURN_OPERATION_INFO("Wrap", "vector_math_wrap", "wrap(float3, float3, float3)"_ustr);
260 RETURN_OPERATION_INFO("Sine", "vector_math_sine", "sin(float3)"_ustr);
262 RETURN_OPERATION_INFO("Cosine", "vector_math_cosine", "cos(float3)"_ustr);
264 RETURN_OPERATION_INFO("Tangent", "vector_math_tangent", "tan(float3)"_ustr);
267 "Refract", "vector_math_refract", "refract(float3, float3, float)"_ustr);
270 "Faceforward", "vector_math_faceforward", "faceforward(float3, float3, float3)"_ustr);
273 "Multiply Add", "vector_math_multiply_add", "float3 * float3 + float3"_ustr);
275 RETURN_OPERATION_INFO("Power", "vector_math_power", "float3 ** float3"_ustr);
277 RETURN_OPERATION_INFO("Sign", "vector_math_sign", "sign(float3)"_ustr);
278 }
279
280#undef RETURN_OPERATION_INFO
281
282 return nullptr;
283}
284
285} // namespace blender::nodes
#define CLAMP(a, b, c)
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context context) const override
ClampWrapperFunction(const mf::MultiFunction &fn)
void hash_unique(UniqueHashBytes &hash) const override
void set_matching_fn(const mf::MultiFunction *fn)
#define RETURN_OPERATION_INFO(title_case_name, shader_name, multi_function_name)
const MultiFunction & lookup(UString id)
void node_math_build_multi_function(NodeMultiFunctionBuilder &builder)
const FloatMathOperationInfo * get_float3_math_operation_info(const int operation)
static const mf::MultiFunction * get_base_multi_function(const bNode &node)
const FloatMathOperationInfo * get_float_compare_operation_info(const int operation)
const FloatMathOperationInfo * get_float_math_operation_info(const int operation)
@ NODE_MATH_SMOOTH_MAX
@ NODE_MATH_LESS_THAN
@ NODE_MATH_GREATER_THAN
@ NODE_MATH_LOGARITHM
@ NODE_MATH_SMOOTH_MIN
@ NODE_MATH_FLOORED_MODULO
@ NODE_MATH_ARCCOSINE
@ NODE_MATH_ARCTANGENT
@ NODE_MATH_MULTIPLY_ADD
@ NODE_COMPARE_NOT_EQUAL
@ NODE_COMPARE_LESS_EQUAL
@ NODE_COMPARE_GREATER_EQUAL
@ NODE_COMPARE_GREATER_THAN
@ NODE_COMPARE_LESS_THAN
@ NODE_VECTOR_MATH_COSINE
@ NODE_VECTOR_MATH_SINE
@ NODE_VECTOR_MATH_MAXIMUM
@ NODE_VECTOR_MATH_FLOOR
@ NODE_VECTOR_MATH_MINIMUM
@ NODE_VECTOR_MATH_MULTIPLY_ADD
@ NODE_VECTOR_MATH_REFLECT
@ NODE_VECTOR_MATH_ROUND
@ NODE_VECTOR_MATH_TANGENT
@ NODE_VECTOR_MATH_PROJECT
@ NODE_VECTOR_MATH_SCALE
@ NODE_VECTOR_MATH_POWER
@ NODE_VECTOR_MATH_CROSS_PRODUCT
@ NODE_VECTOR_MATH_DIVIDE
@ NODE_VECTOR_MATH_NORMALIZE
@ NODE_VECTOR_MATH_SUBTRACT
@ NODE_VECTOR_MATH_DOT_PRODUCT
@ NODE_VECTOR_MATH_SNAP
@ NODE_VECTOR_MATH_DISTANCE
@ NODE_VECTOR_MATH_LENGTH
@ NODE_VECTOR_MATH_ADD
@ NODE_VECTOR_MATH_MODULO
@ NODE_VECTOR_MATH_FRACTION
@ NODE_VECTOR_MATH_CEIL
@ NODE_VECTOR_MATH_SIGN
@ NODE_VECTOR_MATH_WRAP
@ NODE_VECTOR_MATH_FACEFORWARD
@ NODE_VECTOR_MATH_REFRACT
@ NODE_VECTOR_MATH_MULTIPLY
@ NODE_VECTOR_MATH_ABSOLUTE
signed char int8_t
Definition BKE_text.h:76
#define hash
Definition noise_c.cc:156