Blender V5.3
multi_function_common.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_math_vector.hh"
11
12#include "FN_init.hh"
15
16#include <numeric>
17
19
24class PowFunction : public MultiFunction {
25 private:
26 static inline const MultiFunction *pow_generic = nullptr;
27 static inline const MultiFunction *pow_2 = nullptr;
28 static inline const MultiFunction *pow_3 = nullptr;
29
30 public:
32 {
33 static Signature signature = []() {
34 pow_2 = &registry::lookup("float ** 2"_ustr);
35 pow_3 = &registry::lookup("float ** 3"_ustr);
36 static auto pow_generic_fn = build::SI2_SO<float, float, float>(
37 "pow generic",
38 [](const float a, const float b) { return safe_powf(a, b); },
40 pow_generic = &pow_generic_fn;
41
43 SignatureBuilder builder("float ** float", signature);
44 builder.single_input<float>("Base");
45 builder.single_input<float>("Exponent");
46 builder.single_output<float>("Result");
47 return signature;
48 }();
50 }
51
52 void call(const IndexMask &mask, Params params, Context context) const override
53 {
54 /* Use GVArray here to avoid unnecessary conversions to typed virtual arrays. */
55 const GVArray &base = params.readonly_single_input(0, "Base");
56 const GVArray &exponent = params.readonly_single_input(1, "Exponent");
57 MutableSpan<float> result = params.uninitialized_single_output<float>(2, "Result");
58
59 if (exponent.is_single()) {
60 float exponent_single;
61 exponent.get_internal_single(&exponent_single);
62 const int exponent_int = int(exponent_single);
63 /* Handle some exponents without invoking the general powf function. */
64 if (float(exponent_int) == exponent_single) {
65 switch (exponent_int) {
66 case 0: {
68 return;
69 }
70 case 1: {
72 return;
73 }
74 case 2: {
75 ParamsBuilder sub_params{*pow_2, &mask};
76 sub_params.add_readonly_single_input(base);
78 pow_2->call(mask, sub_params, context);
79 return;
80 }
81 case 3: {
82 ParamsBuilder sub_params{*pow_3, &mask};
83 sub_params.add_readonly_single_input(base);
85 pow_3->call(mask, sub_params, context);
86 return;
87 }
88 default: {
89 break;
90 }
91 }
92 }
93 }
94 pow_generic->call(mask, params, context);
95 }
96};
97
99 private:
100 static inline const MultiFunction *multiply = nullptr;
101 static inline const MultiFunction *divide_generic = nullptr;
102
103 public:
105 {
106 static Signature signature = []() {
107 multiply = &registry::lookup("float * float"_ustr);
108 static auto divide_generic_fn = build::SI2_SO<float, float, float>(
109 "float / float",
110 [](float a, float b) { return safe_divide(a, b); },
112 divide_generic = &divide_generic_fn;
113
115 SignatureBuilder builder("float / float", signature);
116 builder.single_input<float>("A");
117 builder.single_input<float>("B");
118 builder.single_output<float>("Result");
119 return signature;
120 }();
121 this->set_signature(&signature);
122 }
123
124 void call(const IndexMask &mask, mf::Params params, mf::Context context) const override
125 {
126 const GVArray &a = params.readonly_single_input(0, "A");
127 const GVArray &b = params.readonly_single_input(1, "B");
128 MutableSpan<float> result = params.uninitialized_single_output<float>(2, "Result");
129
130 if (b.is_single()) {
131 float divisor;
132 b.get_internal_single(&divisor);
133 if (divisor == 0.0f) {
134 /* We define the output to be 0 for division by zero. Same as #safe_divide. */
136 return;
137 }
138 if (divisor == 1.0f) {
139 /* If the divisor is 1 the result is the dividend. */
141 return;
142 }
143 if (is_inverse_exact(divisor)) {
144 /* Use multiplication by the inverse which is more efficient than division. */
145 const float inverse = 1.0f / divisor;
146 ParamsBuilder sub_params{*multiply, &mask};
147 sub_params.add_readonly_single_input(a);
150 multiply->call(mask, sub_params, context);
151 return;
152 }
153 }
154 if (a.is_single()) {
155 float dividend;
156 a.get_internal_single(&dividend);
157 if (dividend == 0.0f) {
158 /* If the dividend is zero the result is always zero regardless of the divisor. */
160 return;
161 }
162 }
163 /* General case. */
164 divide_generic->call(mask, params, context);
165 }
166
167 static bool is_inverse_exact(float x)
168 {
169 BLI_assert(x != 0.0f);
170 x = fabsf(x);
171 int exp;
172 /* Check that x is a power of two. */
173 const float fraction = frexpf(x, &exp);
174 return fraction == 0.5f;
175 }
176};
177
179{
180 static constexpr auto exec_fast = build::exec_presets::AllSpanOrSingle();
181
184 "float ** 2", [](const float a) { return a * a; }, exec_fast);
185 });
188 "float ** 3", [](const float a) { return a * a * a; }, exec_fast);
189 });
191 return build::SI1_SO<float, float>("exp(float)", [](const float a) { return expf(a); });
192 });
195 "sqrt(float)", [](const float a) { return safe_sqrtf(a); }, exec_fast);
196 });
199 "inverse_sqrt(float)", [](const float a) { return safe_inverse_sqrtf(a); }, exec_fast);
200 });
203 "abs(float)", [](const float a) { return fabsf(a); }, exec_fast);
204 });
207 "radians(float)", [](const float a) { return float(DEG2RAD(a)); }, exec_fast);
208 });
211 "degrees(float)", [](const float a) { return float(RAD2DEG(a)); }, exec_fast);
212 });
215 "sign(float)", [](const float a) { return compatible_signf(a); }, exec_fast);
216 });
219 "round(float)", [](const float a) { return floorf(a + 0.5f); }, exec_fast);
220 });
223 "floor(float)", [](const float a) { return floorf(a); }, exec_fast);
224 });
227 "ceil(float)", [](const float a) { return ceilf(a); }, exec_fast);
228 });
231 "frac(float)", [](const float a) { return a - floorf(a); }, exec_fast);
232 });
235 "trunc(float)", [](const float a) { return a >= 0.0f ? floorf(a) : ceilf(a); }, exec_fast);
236 });
238 return build::SI1_SO<float, float>("sin(float)", [](const float a) { return sinf(a); });
239 });
241 return build::SI1_SO<float, float>("cos(float)", [](const float a) { return cosf(a); });
242 });
244 return build::SI1_SO<float, float>("tan(float)", [](const float a) { return tanf(a); });
245 });
247 return build::SI1_SO<float, float>("sinh(float)", [](const float a) { return sinhf(a); });
248 });
250 return build::SI1_SO<float, float>("cosh(float)", [](const float a) { return coshf(a); });
251 });
253 return build::SI1_SO<float, float>("tanh(float)", [](const float a) { return tanhf(a); });
254 });
256 return build::SI1_SO<float, float>("asin(float)", [](const float a) { return safe_asinf(a); });
257 });
259 return build::SI1_SO<float, float>("acos(float)", [](const float a) { return safe_acosf(a); });
260 });
262 return build::SI1_SO<float, float>("atan(float)", [](const float a) { return atanf(a); });
263 });
266 "float + float", [](const float a, const float b) { return a + b; }, exec_fast);
267 });
270 "float - float", [](const float a, const float b) { return a - b; }, exec_fast);
271 });
274 "float * float", [](const float a, const float b) { return a * b; }, exec_fast);
275 });
276 registry::add_new_cb([] { return DivideFunction(); });
277 registry::add_new_cb([] { return PowFunction(); });
280 "log(float, float)",
281 [](const float a, const float b) { return safe_logf(a, b); },
282 exec_fast);
283 });
286 "min(float, float)",
287 [](const float a, const float b) { return std::min(a, b); },
288 exec_fast);
289 });
292 "max(float, float)",
293 [](const float a, const float b) { return std::max(a, b); },
294 exec_fast);
295 });
298 "float(float < float)",
299 [](const float a, const float b) { return float(a < b); },
300 exec_fast);
301 });
304 "float(float > float)",
305 [](const float a, const float b) { return float(a > b); },
306 exec_fast);
307 });
310 "float % float", [](const float a, const float b) { return safe_modf(a, b); }, exec_fast);
311 });
314 "floor_mod(float, float)",
315 [](const float a, const float b) { return safe_floored_modf(a, b); },
316 exec_fast);
317 });
320 "snap(float, float)",
321 [](const float a, const float b) { return floorf(safe_divide(a, b)) * b; },
322 exec_fast);
323 });
326 "atan2(float, float)",
327 [](const float a, const float b) { return atan2f(a, b); },
328 exec_fast);
329 });
332 "pingpong(float, float)",
333 [](const float a, const float b) { return pingpongf(a, b); },
334 exec_fast);
335 });
338 "float * float + float",
339 [](const float a, const float b, const float c) { return a * b + c; },
340 exec_fast);
341 });
344 "compare(float, float, float)",
345 [](const float a, const float b, const float c) {
346 return ((a == b) || (fabsf(a - b) <= fmaxf(c, FLT_EPSILON))) ? 1.0f : 0.0f;
347 },
348 exec_fast);
349 });
352 "smooth_min(float, float, float)",
353 [](const float a, const float b, const float c) { return smoothminf(a, b, c); },
354 exec_fast);
355 });
358 "smooth_max(float, float, float)",
359 [](const float a, const float b, const float c) { return -smoothminf(-a, -b, c); },
360 exec_fast);
361 });
364 "wrap(float, float, float)",
365 [](const float a, const float b, const float c) { return wrapf(a, b, c); },
366 exec_fast);
367 });
370 "float3 + float3", [](const float3 &a, const float3 &b) { return a + b; }, exec_fast);
371 });
374 "float3 - float3", [](const float3 &a, const float3 &b) { return a - b; }, exec_fast);
375 });
378 "float3 * float3", [](const float3 &a, const float3 &b) { return a * b; }, exec_fast);
379 });
382 "float3 / float3",
383 [](const float3 &a, const float3 &b) { return math::safe_divide(a, b); },
384 exec_fast);
385 });
388 "cross_product(float3, float3)",
389 [](const float3 &a, const float3 &b) { return math::cross_high_precision(a, b); },
390 exec_fast);
391 });
394 "project(float3, float3)",
395 [](const float3 &a, const float3 &b) { return math::project(a, b); },
396 exec_fast);
397 });
400 "reflect(float3, float3)",
401 [](const float3 &a, const float3 &b) { return math::reflect(a, math::normalize(b)); },
402 exec_fast);
403 });
406 "snap(float3, float3)",
407 [](const float3 &a, const float3 &b) { return math::floor(math::safe_divide(a, b)) * b; },
408 exec_fast);
409 });
412 "float3 % float3", [](const float3 &a, const float3 &b) { return math::safe_mod(a, b); });
413 });
416 "min(float3, float3)",
417 [](const float3 &a, const float3 &b) { return math::min(a, b); },
418 exec_fast);
419 });
422 "max(float3, float3)",
423 [](const float3 &a, const float3 &b) { return math::max(a, b); },
424 exec_fast);
425 });
427 return build::SI2_SO<float3, float3, float3>("float3 ** float3", [](float3 a, float3 b) {
428 return float3(safe_powf(a.x, b.x), safe_powf(a.y, b.y), safe_powf(a.z, b.z));
429 });
430 });
433 "dot_product(float3, float3)",
434 [](const float3 &a, const float3 &b) { return math::dot(a, b); },
435 exec_fast);
436 });
439 "distance(float3, float3)",
440 [](const float3 &a, const float3 &b) { return math::distance(a, b); },
441 exec_fast);
442 });
445 "float3 * float3 + float3",
446 [](const float3 &a, const float3 &b, const float3 &c) { return a * b + c; },
447 exec_fast);
448 });
451 "wrap(float3, float3, float3)", [](const float3 &a, const float3 &b, const float3 &c) {
452 return float3(wrapf(a.x, b.x, c.x), wrapf(a.y, b.y, c.y), wrapf(a.z, b.z, c.z));
453 });
454 });
457 "faceforward(float3, float3, float3)",
458 [](const float3 &a, const float3 &b, const float3 &c) {
459 return math::faceforward(a, b, c);
460 },
461 exec_fast);
462 });
465 "refract(float3, float3, float)", [](const float3 &a, const float3 &b, float c) {
466 return math::refract(a, math::normalize(b), c);
467 });
468 });
471 "length(float3)", [](const float3 &a) { return math::length(a); }, exec_fast);
472 });
475 "float3 * float", [](const float3 &a, float b) { return a * b; }, exec_fast);
476 });
479 "normalize(float3)", [](const float3 &a) { return math::normalize(a); }, exec_fast);
480 });
483 "round(float3)", [](const float3 &a) { return math::floor(a + 0.5f); }, exec_fast);
484 });
486 return build::SI1_SO<float3, float3>("floor(float3)",
487 [](const float3 &a) { return math::floor(a); });
488 });
490 return build::SI1_SO<float3, float3>("ceil(float3)",
491 [](const float3 &a) { return math::ceil(a); });
492 });
495 "frac(float3)", [](const float3 &a) { return math::fract(a); }, exec_fast);
496 });
499 "abs(float3)", [](const float3 &a) { return math::abs(a); }, exec_fast);
500 });
503 "sign(float3)", [](const float3 &a) { return math::sign(a); }, exec_fast);
504 });
507 "sin(float3)", [](const float3 &a) { return float3(sinf(a.x), sinf(a.y), sinf(a.z)); });
508 });
511 "cos(float3)", [](const float3 &a) { return float3(cosf(a.x), cosf(a.y), cosf(a.z)); });
512 });
515 "tan(float3)", [](const float3 &a) { return float3(tanf(a.x), tanf(a.y), tanf(a.z)); });
516 });
518 return mf::build::SI2_SO<int, int, int>(
519 "int + int", [](int a, int b) { return a + b; }, exec_fast);
520 });
522 return mf::build::SI2_SO<int, int, int>(
523 "int - int", [](int a, int b) { return a - b; }, exec_fast);
524 });
526 return mf::build::SI2_SO<int, int, int>(
527 "int * int", [](int a, int b) { return a * b; }, exec_fast);
528 });
530 return mf::build::SI2_SO<int, int, int>(
531 "int / int", [](int a, int b) { return math::safe_divide(a, b); }, exec_fast);
532 });
534 return mf::build::SI2_SO<int, int, int>(
535 "floor(int, int)",
536 [](int a, int b) { return (b != 0) ? divide_floor_i(a, b) : 0; },
537 exec_fast);
538 });
540 return mf::build::SI2_SO<int, int, int>(
541 "divide_ceil(int, int)",
542 [](int a, int b) { return (b != 0) ? -divide_floor_i(a, -b) : 0; },
543 exec_fast);
544 });
546 return mf::build::SI2_SO<int, int, int>(
547 "divide_round(int, int)",
548 [](int a, int b) {
549 /* Derived from `divide_round_i` but fixed to be safe and handle negative inputs. */
550 const int c = math::abs(b);
551 return (a >= 0) ? math::safe_divide((2 * a + c), (2 * c)) * math::sign(b) :
552 -math::safe_divide((2 * -a + c), (2 * c)) * math::sign(b);
553 },
554 exec_fast);
555 });
557 return mf::build::SI2_SO<int, int, int>(
558 "int ** int",
559 [](int base, int exponent) {
560 if (exponent < 0) {
561 if (base == 1 || base == -1) {
562 return (base < 0 && (exponent & 1) != 0) ? -1 : 1;
563 }
564 return 0;
565 }
566 return pow_i(base, exponent);
567 },
568 exec_fast);
569 });
571 return mf::build::SI3_SO<int, int, int, int>(
572 "int * int + int", [](int a, int b, int c) { return a * b + c; }, exec_fast);
573 });
575 return mf::build::SI2_SO<int, int, int>(
576 "mod_periodic(int, int)",
577 [](int a, int b) { return b != 0 ? math::mod_periodic(a, b) : 0; },
578 exec_fast);
579 });
581 return mf::build::SI2_SO<int, int, int>(
582 "int % int", [](int a, int b) { return b != 0 ? a % b : 0; }, exec_fast);
583 });
585 return mf::build::SI1_SO<int, int>("abs(int)", [](int a) { return math::abs(a); }, exec_fast);
586 });
588 return mf::build::SI1_SO<int, int>(
589 "sign(int)", [](int a) { return math::sign(a); }, exec_fast);
590 });
592 return mf::build::SI2_SO<int, int, int>(
593 "min(int, int)", [](int a, int b) { return math::min(a, b); }, exec_fast);
594 });
596 return mf::build::SI2_SO<int, int, int>(
597 "max(int, int)", [](int a, int b) { return math::max(a, b); }, exec_fast);
598 });
600 return mf::build::SI2_SO<int, int, int>("gcd(int, int)",
601 [](int a, int b) { return std::gcd(a, b); });
602 });
604 return mf::build::SI2_SO<int, int, int>("lcm(int, int)",
605 [](int a, int b) { return std::lcm(a, b); });
606 });
608 [] { return mf::build::SI1_SO<int, int>("-int", [](int a) { return -a; }, exec_fast); });
610 return mf::build::SI2_SO<bool, bool, bool>(
611 "bool && bool", [](bool a, bool b) { return a && b; }, exec_fast);
612 });
614 return mf::build::SI2_SO<bool, bool, bool>(
615 "bool || bool", [](bool a, bool b) { return a || b; }, exec_fast);
616 });
618 [] { return mf::build::SI1_SO<bool, bool>("!bool", [](bool a) { return !a; }, exec_fast); });
620 return mf::build::SI2_SO<bool, bool, bool>(
621 "!(bool && bool)", [](bool a, bool b) { return !(a && b); }, exec_fast);
622 });
624 return mf::build::SI2_SO<bool, bool, bool>(
625 "!(bool || bool)", [](bool a, bool b) { return !(a || b); }, exec_fast);
626 });
628 return mf::build::SI2_SO<bool, bool, bool>(
629 "bool == bool", [](bool a, bool b) { return a == b; }, exec_fast);
630 });
632 return mf::build::SI2_SO<bool, bool, bool>(
633 "bool != bool", [](bool a, bool b) { return a != b; }, exec_fast);
634 });
636 return mf::build::SI2_SO<bool, bool, bool>(
637 "!bool || bool", [](bool a, bool b) { return !a || b; }, exec_fast);
638 });
640 return mf::build::SI2_SO<bool, bool, bool>(
641 "bool && !bool", [](bool a, bool b) { return a && !b; }, exec_fast);
642 });
644 return mf::build::SI2_SO<int, int, int>(
645 "int & int", [](int a, int b) { return a & b; }, exec_fast);
646 });
648 return mf::build::SI2_SO<int, int, int>(
649 "int | int", [](int a, int b) { return a | b; }, exec_fast);
650 });
652 return mf::build::SI2_SO<int, int, int>(
653 "int ^ int", [](int a, int b) { return a ^ b; }, exec_fast);
654 });
656 [] { return build::SI1_SO<int, int>("~int", [](int a) { return ~a; }, exec_fast); });
659 "shift(int, int)",
660 [](int a, int b) {
661 const uint32_t value = a;
662 const int shift = math::clamp(b, -32, 32);
663 const uint64_t wide_value = uint64_t(value) << 16;
664 const uint64_t wide_result = shift > 0 ? wide_value << shift : wide_value >> -shift;
665 return uint32_t(wide_result >> 16);
666 },
667 exec_fast);
668 });
671 "rotate(int, int)",
672 [](int a, int b) {
673 const uint32_t value = a;
674 const int shift = math::mod_periodic(b, 32);
675 const uint64_t wide_value = uint64_t(value) | (uint64_t(value) << 32);
676 const uint64_t double_result = (wide_value << shift);
677 return uint32_t((double_result | (double_result >> 32)) & ((uint64_t(1) << 33) - 1));
678 },
679 exec_fast);
680 });
682 return mf::build::SI2_SO<float3, int, float>(
683 "float3[int]",
684 [](const float3 &vec, const int index) {
685 if (index >= 0 && index <= 2) {
686 return vec[index];
687 }
688 return 0.0f;
689 },
690 exec_fast);
691 });
692}
693
695{
696 /* Make sure the functions are only registered once even if called multiple times. */
697 [[maybe_unused]] static bool registered = []() {
699 return true;
700 }();
701}
702
703} // namespace blender::fn::multi_function
#define DEG2RAD(_deg)
#define RAD2DEG(_rad)
void materialize_to_uninitialized(void *dst) const
void get_internal_single(void *r_value) const
void call(const IndexMask &mask, mf::Params params, mf::Context context) const override
void set_signature(const Signature *signature)
void add_uninitialized_single_output(T *value, StringRef expected_name="")
void add_readonly_single_input(const T *value, StringRef expected_name="")
void add_readonly_single_input_value(T value, StringRef expected_name="")
MutableSpan< T > uninitialized_single_output(int param_index, StringRef name="")
VArray< T > readonly_single_input(int param_index, StringRef name="")
void call(const IndexMask &mask, Params params, Context context) const override
void single_output(const char *name, const ParamFlag flag=ParamFlag::None)
#define expf(x)
#define tanhf(x)
#define sinhf(x)
#define coshf(x)
#define FLT_EPSILON
MatBase< C, R > inverse(MatBase< C, R >) RET
#define exp
auto SI2_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI3_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI1_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
const MultiFunction & lookup(UString id)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T clamp(const T &a, const T &min, const T &max)
T safe_divide(const T &a, const T &b)
T sign(const T &a)
T floor(const T &a)
VecBase< T, Size > project(const VecBase< T, Size > &p, const VecBase< T, Size > &v_proj)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
VecBase< float, 3 > cross_high_precision(const VecBase< float, 3 > &a, const VecBase< float, 3 > &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T safe_mod(const T &a, const T &b)
T fract(const T &a)
VecBase< T, Size > refract(const VecBase< T, Size > &incident, const VecBase< T, Size > &normal, const T &eta)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, Size > faceforward(const VecBase< T, Size > &vector, const VecBase< T, Size > &incident, const VecBase< T, Size > &reference)
T ceil(const T &a)
T mod_periodic(const T &a, const T &b)
VecBase< T, Size > reflect(const VecBase< T, Size > &incident, const VecBase< T, Size > &normal)
T max(const T &a, const T &b)
T abs(const T &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
MINLINE float safe_asinf(float a)
MINLINE float wrapf(float value, float max, float min)
MINLINE float safe_sqrtf(float a)
MINLINE float safe_acosf(float a)
unsigned int uint32_t
Definition BKE_text.h:81
MINLINE float pingpongf(float value, float scale)
MINLINE float compatible_signf(float f)
MINLINE float smoothminf(float a, float b, float c)
MINLINE float safe_powf(float base, float exponent)
int pow_i(int base, int exp)
Definition math_base.cc:15
MINLINE float safe_floored_modf(float a, float b)
VecBase< float, 3 > float3
MINLINE float safe_divide(float a, float b)
MINLINE int divide_floor_i(int a, int b)
MINLINE float safe_inverse_sqrtf(float a)
MINLINE float safe_logf(float a, float base)
MINLINE float safe_modf(float a, float b)
#define floorf
#define fabsf
#define atanf
#define fmaxf
#define sinf
#define tanf
#define cosf
#define ceilf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float