Blender V5.3
FN_multi_function_builder.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
12
13#include "FN_multi_function.hh"
14
15namespace blender {
16
18
23namespace exec_presets {
24
32
38struct Simple {
39 static constexpr bool use_devirtualization = false;
41};
42
50 static constexpr bool use_devirtualization = false;
52};
53
61 static constexpr bool use_devirtualization = true;
63
64 template<typename... ParamTags, typename... LoadedParams, size_t... I>
66 std::index_sequence<I...> /*indices*/,
67 const std::tuple<LoadedParams...> &loaded_params) const
68 {
69 return std::make_tuple([&]() {
70 using ParamTag = ParamTags;
71 using T = typename ParamTag::base_type;
73 const GVArrayImpl &varray_impl = *std::get<I>(loaded_params);
74 return GVArrayDevirtualizer<T, true, true>{varray_impl};
75 }
76 else if constexpr (ELEM(ParamTag::category,
79 {
80 T *ptr = std::get<I>(loaded_params);
81 return BasicDevirtualizer<T *>{ptr};
82 }
83 }()...);
84 }
85};
86
92template<size_t... Indices> struct SomeSpanOrSingle {
93 static constexpr bool use_devirtualization = true;
95
96 template<typename... ParamTags, typename... LoadedParams, size_t... I>
98 std::index_sequence<I...> /*indices*/,
99 const std::tuple<LoadedParams...> &loaded_params) const
100 {
101 return std::make_tuple([&]() {
102 using ParamTag = ParamTags;
103 using T = typename ParamTag::base_type;
104
106 constexpr bool UseSpan = ValueSequence<size_t, Indices...>::template contains<I>();
107 const GVArrayImpl &varray_impl = *std::get<I>(loaded_params);
108 return GVArrayDevirtualizer<T, true, UseSpan>{varray_impl};
109 }
110 else if constexpr (ELEM(ParamTag::category,
113 {
114 T *ptr = std::get<I>(loaded_params);
115 return BasicDevirtualizer<T *>{ptr};
116 }
117 }()...);
118 }
119};
120
121} // namespace exec_presets
122
123namespace detail {
124
130template<typename MaskT, typename... Args, typename ElementFn>
131/* Perform additional optimizations on this loop because it is a very hot loop. For example, the
132 * math node in geometry nodes is processed here. */
133#if (defined(__GNUC__) && !defined(__clang__))
134[[gnu::optimize("-funroll-loops")]] [[gnu::optimize("O3")]]
135#endif
136inline void execute_array(const ElementFn &element_fn,
137 MaskT mask,
138 /* Use restrict to tell the compiler that pointer inputs do not alias
139 * each other. This is important for some compiler optimizations. */
140 Args &&__restrict... args)
141{
142 if constexpr (std::is_same_v<std::decay_t<MaskT>, IndexRange>) {
143 /* Having this explicit loop is necessary for MSVC to be able to vectorize this. */
144 const int64_t start = mask.start();
145 const int64_t end = mask.one_after_last();
146 for (int64_t i = start; i < end; i++) {
147 element_fn(args[i]...);
148 }
149 }
150 else {
151 for (const int64_t i : mask) {
152 element_fn(args[i]...);
153 }
154 }
155}
156
161 Materialized,
162};
163
168
172template<typename... ParamTags, typename ElementFn, typename... Chunks>
173#if (defined(__GNUC__) && !defined(__clang__))
174[[gnu::optimize("-funroll-loops")]] [[gnu::optimize("O3")]]
175#endif
177 const ElementFn &element_fn,
178 const int64_t size,
179 Chunks &&__restrict... chunks)
180{
181 for (int64_t i = 0; i < size; i++) {
182 element_fn(chunks[i]...);
183 }
184}
185
191template<typename... ParamTags, size_t... I, typename ElementFn, typename... LoadedParams>
193 std::index_sequence<I...> /*indices*/,
194 const ElementFn &element_fn,
196 const std::tuple<LoadedParams...> &loaded_params)
197{
198
199 /* In theory, all elements could be processed in one chunk. However, that has the disadvantage
200 * that large temporary arrays are needed. Using small chunks allows using small arrays, which
201 * are reused multiple times, which improves cache efficiency. The chunk size also shouldn't be
202 * too small, because then overhead of the outer loop over chunks becomes significant again. */
203 static constexpr int64_t MaxChunkSize = 64;
204 const int64_t mask_size = mask.size();
205 const int64_t tmp_buffer_size = std::min(mask_size, MaxChunkSize);
206
207 /* Local buffers that are used to temporarily store values for processing. */
208 std::tuple<TypedBuffer<typename ParamTags::base_type, MaxChunkSize>...> temporary_buffers;
209
210 /* Information about every parameter. */
211 std::tuple<MaterializeArgInfo<ParamTags>...> args_info;
212
213 (
214 /* Setup information for all parameters. */
215 [&] {
216 /* Use `typedef` instead of `using` to work around a compiler bug. */
217 using ParamTag = ParamTags;
218 using T = typename ParamTag::base_type;
219 [[maybe_unused]] MaterializeArgInfo<ParamTags> &arg_info = std::get<I>(args_info);
221 const GVArrayImpl &varray_impl = *std::get<I>(loaded_params);
222 const CommonVArrayInfo common_info = varray_impl.common_info();
223 if (common_info.type == CommonVArrayInfo::Type::Single) {
224 /* If an input #VArray is a single value, we have to fill the buffer with that value
225 * only once. The same unchanged buffer can then be reused in every chunk. */
226 const T &in_single = *static_cast<const T *>(common_info.data);
227 T *tmp_buffer = std::get<I>(temporary_buffers).ptr();
228 uninitialized_fill_n(tmp_buffer, tmp_buffer_size, in_single);
230 }
231 else if (common_info.type == CommonVArrayInfo::Type::Span) {
232 /* Remember the span so that it doesn't have to be retrieved in every iteration. */
233 arg_info.internal_span_data = static_cast<const T *>(common_info.data);
234 }
235 else {
236 arg_info.internal_span_data = nullptr;
237 }
238 }
239 }(),
240 ...);
241
242 IndexMaskFromSegment index_mask_from_segment;
243 const int64_t segment_offset = mask.offset();
244
245 /* Outer loop over all chunks. */
246 for (int64_t chunk_start = 0; chunk_start < mask_size; chunk_start += MaxChunkSize) {
247 const int64_t chunk_end = std::min<int64_t>(chunk_start + MaxChunkSize, mask_size);
248 const int64_t chunk_size = chunk_end - chunk_start;
249 const IndexMaskSegment sliced_mask = mask.slice(chunk_start, chunk_size);
250 const int64_t mask_start = sliced_mask[0];
251 const bool sliced_mask_is_range = unique_sorted_indices::non_empty_is_range(
252 sliced_mask.base_span());
253
254 /* Move mutable data into temporary array. */
255 if (!sliced_mask_is_range) {
256 (
257 [&] {
258 /* Use `typedef` instead of `using` to work around a compiler bug. */
259 using ParamTag = ParamTags;
260 using T = typename ParamTag::base_type;
262 T *tmp_buffer = std::get<I>(temporary_buffers).ptr();
263 T *param_buffer = std::get<I>(loaded_params);
264 for (int64_t i = 0; i < chunk_size; i++) {
265 new (tmp_buffer + i) T(std::move(param_buffer[sliced_mask[i]]));
266 }
267 }
268 }(),
269 ...);
270 }
271
272 const IndexMask *current_segment_mask = nullptr;
273
276 element_fn,
277 chunk_size,
278 /* Prepare every parameter for this chunk. */
279 [&] {
280 using ParamTag = ParamTags;
281 using T = typename ParamTag::base_type;
282 [[maybe_unused]] MaterializeArgInfo<ParamTags> &arg_info = std::get<I>(args_info);
283 T *tmp_buffer = std::get<I>(temporary_buffers);
285 if (arg_info.mode == MaterializeArgMode::Single) {
286 /* The single value has been filled into a buffer already reused for every chunk. */
287 return const_cast<const T *>(tmp_buffer);
288 }
289 if (sliced_mask_is_range && arg_info.internal_span_data != nullptr) {
290 /* In this case we can just use an existing span instead of "compressing" it into
291 * a new temporary buffer. */
293 return arg_info.internal_span_data + mask_start;
294 }
295 const GVArrayImpl &varray_impl = *std::get<I>(loaded_params);
296 if (current_segment_mask == nullptr) {
297 current_segment_mask = &index_mask_from_segment.update(
298 {segment_offset, sliced_mask.base_span()});
299 }
300 /* As a fallback, do a virtual function call to retrieve all elements in the current
301 * chunk. The elements are stored in a temporary buffer reused for every chunk. */
302 varray_impl.materialize_compressed(*current_segment_mask, tmp_buffer, true);
303 /* Remember that this parameter has been materialized, so that the values are
304 * destructed properly when the chunk is done. */
306 return const_cast<const T *>(tmp_buffer);
307 }
308 else if constexpr (ELEM(ParamTag::category,
311 {
312 /* For outputs, just pass a pointer. This is important so that `__restrict` works. */
313 if (sliced_mask_is_range) {
314 /* Can write into the caller-provided buffer directly. */
315 T *param_buffer = std::get<I>(loaded_params);
316 return param_buffer + mask_start;
317 }
318 /* Use the temporary buffer. The values will have to be copied out of that
319 * buffer into the caller-provided buffer afterwards. */
320 return tmp_buffer;
321 }
322 }()...);
323
324 /* Relocate outputs from temporary buffers to buffers provided by caller. */
325 if (!sliced_mask_is_range) {
326 (
327 [&] {
328 /* Use `typedef` instead of `using` to work around a compiler bug. */
329 using ParamTag = ParamTags;
330 using T = typename ParamTag::base_type;
331 if constexpr (ELEM(ParamTag::category,
334 {
335 T *tmp_buffer = std::get<I>(temporary_buffers).ptr();
336 T *param_buffer = std::get<I>(loaded_params);
337 for (int64_t i = 0; i < chunk_size; i++) {
338 new (param_buffer + sliced_mask[i]) T(std::move(tmp_buffer[i]));
339 std::destroy_at(tmp_buffer + i);
340 }
341 }
342 }(),
343 ...);
344 }
345
346 (
347 /* Destruct values that have been materialized before. */
348 [&] {
349 /* Use `typedef` instead of `using` to work around a compiler bug. */
350 using ParamTag = ParamTags;
351 using T = typename ParamTag::base_type;
352 [[maybe_unused]] MaterializeArgInfo<ParamTags> &arg_info = std::get<I>(args_info);
354 if (arg_info.mode == MaterializeArgMode::Materialized) {
355 T *tmp_buffer = std::get<I>(temporary_buffers).ptr();
356 destruct_n(tmp_buffer, chunk_size);
357 }
358 }
359 }(),
360 ...);
361 }
362
363 (
364 /* Destruct buffers for single value inputs. */
365 [&] {
366 /* Use `typedef` instead of `using` to work around a compiler bug. */
367 using ParamTag = ParamTags;
368 using T = typename ParamTag::base_type;
369 [[maybe_unused]] MaterializeArgInfo<ParamTags> &arg_info = std::get<I>(args_info);
371 if (arg_info.mode == MaterializeArgMode::Single) {
372 T *tmp_buffer = std::get<I>(temporary_buffers).ptr();
373 destruct_n(tmp_buffer, tmp_buffer_size);
374 }
375 }
376 }(),
377 ...);
378}
379
380template<typename ElementFn, typename ExecPreset, typename... ParamTags, size_t... I>
381inline void execute_element_fn_as_multi_function(const ElementFn &element_fn,
382 const ExecPreset exec_preset,
383 const IndexMask &mask,
384 Params params,
385 TypeSequence<ParamTags...> /*param_tags*/,
386 std::index_sequence<I...> /*indices*/)
387{
388
389 /* Load parameters from #Params. */
390 /* Contains `const GVArrayImpl *` for inputs and `T *` for outputs. */
391 const auto loaded_params = std::make_tuple([&]() {
392 /* Use `typedef` instead of `using` to work around a compiler bug. */
393 using ParamTag = ParamTags;
394 using T = typename ParamTag::base_type;
395
397 return params.readonly_single_input(I).get_implementation();
398 }
399 else if constexpr (ParamTag::category == ParamCategory::SingleOutput) {
400 return static_cast<T *>(params.uninitialized_single_output(I).data());
401 }
402 else if constexpr (ParamTag::category == ParamCategory::SingleMutable) {
403 return static_cast<T *>(params.single_mutable(I).data());
404 }
405 }()...);
406
407 /* Try execute devirtualized if enabled and the input types allow it. */
408 bool executed_devirtualized = false;
409 if constexpr (ExecPreset::use_devirtualization) {
410 /* Get segments before devirtualization to avoid generating this code multiple times. */
412 mask.to_spans_and_ranges<16>();
413
414 const auto devirtualizers = exec_preset.create_devirtualizers(
415 TypeSequence<ParamTags...>(), std::index_sequence<I...>(), loaded_params);
416 executed_devirtualized = call_with_devirtualized_parameters(
417 devirtualizers, [&](auto &&...args) {
418 for (const std::variant<IndexRange, IndexMaskSegment> &segment : mask_segments) {
419 if (std::holds_alternative<IndexRange>(segment)) {
420 const auto segment_range = std::get<IndexRange>(segment);
421 execute_array(element_fn, segment_range, std::forward<decltype(args)>(args)...);
422 }
423 else {
424 const auto segment_indices = std::get<IndexMaskSegment>(segment);
425 execute_array(element_fn, segment_indices, std::forward<decltype(args)>(args)...);
426 }
427 }
428 });
429 }
430 else {
431 UNUSED_VARS(exec_preset);
432 }
433
434 /* If devirtualized execution was disabled or not possible, use a fallback method which is
435 * slower but always works. */
436 if (!executed_devirtualized) {
437 /* The materialized method is most common because it avoids most virtual function overhead but
438 * still instantiates the function only once. */
439 if constexpr (ExecPreset::fallback_mode == exec_presets::FallbackMode::Materialized) {
440 mask.foreach_segment([&](const IndexMaskSegment segment) {
442 std::index_sequence<I...>(),
443 element_fn,
444 segment,
445 loaded_params);
446 });
447 }
448 else {
449 /* This fallback is slower because it uses virtual method calls for every element. */
450 mask.foreach_segment([&](const IndexMaskSegment segment) {
451 execute_array(element_fn, segment, [&]() {
452 /* Use `typedef` instead of `using` to work around a compiler bug. */
453 using ParamTag = ParamTags;
454 using T = typename ParamTag::base_type;
456 const GVArrayImpl &varray_impl = *std::get<I>(loaded_params);
457 return GVArray(&varray_impl).typed<T>();
458 }
459 else if constexpr (ELEM(ParamTag::category,
462 {
463 T *ptr = std::get<I>(loaded_params);
464 return ptr;
465 }
466 }()...);
467 });
468 }
469 }
470}
471
478template<typename ElementFn, typename ExecPreset, typename... ParamTags>
479inline auto build_multi_function_call_from_element_fn(ElementFn &&element_fn,
480 const ExecPreset exec_preset,
481 TypeSequence<ParamTags...> /*param_tags*/)
482{
483 return [element_fn = std::forward<ElementFn>(element_fn), exec_preset](const IndexMask &mask,
484 Params params) {
486 exec_preset,
487 mask,
488 params,
490 std::make_index_sequence<sizeof...(ParamTags)>());
491 };
492}
493
497template<typename CallFn, typename... ParamTags> class CustomMF : public MultiFunction {
498 private:
499 Signature signature_;
500 CallFn call_fn_;
501
502 public:
503 CustomMF(const char *name, CallFn call_fn, TypeSequence<ParamTags...> /*param_tags*/)
504 : call_fn_(std::move(call_fn))
505 {
506 SignatureBuilder builder{name, signature_};
507 /* Loop over all parameter types and add an entry for each in the signature. */
508 ([&] { builder.add(ParamTags(), ""); }(), ...);
509 this->set_signature(&signature_);
510 }
511
512 void call(const IndexMask &mask, Params params, Context /*context*/) const override
513 {
514 call_fn_(mask, params);
515 }
516};
517
518template<typename Out, typename... In, typename ElementFn, typename ExecPreset>
520 ElementFn &&element_fn,
521 const ExecPreset exec_preset,
522 TypeSequence<In...> /*in_types*/)
523{
524 constexpr auto param_tags = TypeSequence<ParamTag<ParamCategory::SingleInput, In>...,
527 [element_fn = std::forward<ElementFn>(element_fn)](const In &...in, Out &out) {
528 new (&out) Out(element_fn(in...));
529 },
530 exec_preset,
531 param_tags);
532 return CustomMF(name, std::move(call_fn), param_tags);
533}
534
535template<typename Out1, typename Out2, typename... In, typename ElementFn, typename ExecPreset>
537 ElementFn &&element_fn,
538 const ExecPreset exec_preset,
539 TypeSequence<In...> /*in_types*/)
540{
541 constexpr auto param_tags = TypeSequence<ParamTag<ParamCategory::SingleInput, In>...,
545 std::forward<ElementFn>(element_fn), exec_preset, param_tags);
546 return CustomMF(name, call_fn, param_tags);
547}
548
549} // namespace detail
550
552template<typename In1,
553 typename Out1,
554 typename ElementFn,
555 typename ExecPreset = exec_presets::Materialized>
556inline auto SI1_SO(const char *name,
557 ElementFn &&element_fn,
558 const ExecPreset exec_preset = exec_presets::Materialized())
559{
561 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1>());
562}
563
565template<typename In1,
566 typename In2,
567 typename Out1,
568 typename ElementFn,
569 typename ExecPreset = exec_presets::Materialized>
570inline auto SI2_SO(const char *name,
571 ElementFn &&element_fn,
572 const ExecPreset exec_preset = exec_presets::Materialized())
573{
575 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1, In2>());
576}
577
579template<typename In1,
580 typename In2,
581 typename In3,
582 typename Out1,
583 typename ElementFn,
584 typename ExecPreset = exec_presets::Materialized>
585inline auto SI3_SO(const char *name,
586 ElementFn &&element_fn,
587 const ExecPreset exec_preset = exec_presets::Materialized())
588{
590 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1, In2, In3>());
591}
592
594template<typename In1,
595 typename In2,
596 typename In3,
597 typename In4,
598 typename Out1,
599 typename ElementFn,
600 typename ExecPreset = exec_presets::Materialized>
601inline auto SI4_SO(const char *name,
602 ElementFn &&element_fn,
603 const ExecPreset exec_preset = exec_presets::Materialized())
604{
606 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1, In2, In3, In4>());
607}
608
610template<typename In1,
611 typename In2,
612 typename In3,
613 typename In4,
614 typename In5,
615 typename Out1,
616 typename ElementFn,
617 typename ExecPreset = exec_presets::Materialized>
618inline auto SI5_SO(const char *name,
619 ElementFn &&element_fn,
620 const ExecPreset exec_preset = exec_presets::Materialized())
621{
623 name,
624 std::forward<ElementFn>(element_fn),
625 exec_preset,
627}
628
630template<typename In1,
631 typename In2,
632 typename In3,
633 typename In4,
634 typename In5,
635 typename In6,
636 typename Out1,
637 typename ElementFn,
638 typename ExecPreset = exec_presets::Materialized>
639inline auto SI6_SO(const char *name,
640 ElementFn &&element_fn,
641 const ExecPreset exec_preset = exec_presets::Materialized())
642{
644 name,
645 std::forward<ElementFn>(element_fn),
646 exec_preset,
648}
649
651template<typename In1,
652 typename In2,
653 typename In3,
654 typename In4,
655 typename In5,
656 typename In6,
657 typename In7,
658 typename In8,
659 typename Out1,
660 typename ElementFn,
661 typename ExecPreset = exec_presets::Materialized>
662inline auto SI8_SO(const char *name,
663 ElementFn &&element_fn,
664 const ExecPreset exec_preset = exec_presets::Materialized())
665{
667 name,
668 std::forward<ElementFn>(element_fn),
669 exec_preset,
671}
672
674template<typename Mut1, typename ElementFn, typename ExecPreset = exec_presets::AllSpanOrSingle>
675inline auto SM(const char *name,
676 ElementFn &&element_fn,
677 const ExecPreset exec_preset = exec_presets::AllSpanOrSingle())
678{
681 std::forward<ElementFn>(element_fn), exec_preset, param_tags);
682 return detail::CustomMF(name, call_fn, param_tags);
683}
684
686template<typename In1,
687 typename Out1,
688 typename Out2,
689 typename ElementFn,
690 typename ExecPreset = exec_presets::Materialized>
691inline auto SI1_SO2(const char *name,
692 ElementFn &&element_fn,
693 const ExecPreset exec_preset = exec_presets::Materialized())
694{
696 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1>());
697}
698
700template<typename In1,
701 typename In2,
702 typename Out1,
703 typename Out2,
704 typename ElementFn,
705 typename ExecPreset = exec_presets::Materialized>
706inline auto SI2_SO2(const char *name,
707 ElementFn &&element_fn,
708 const ExecPreset exec_preset = exec_presets::Materialized())
709{
711 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1, In2>());
712}
713
715template<typename In1,
716 typename In2,
717 typename In3,
718 typename Out1,
719 typename Out2,
720 typename ElementFn,
721 typename ExecPreset = exec_presets::Materialized>
722inline auto SI3_SO2(const char *name,
723 ElementFn &&element_fn,
724 const ExecPreset exec_preset = exec_presets::Materialized())
725{
727 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1, In2, In3>());
728}
729
731template<typename In1,
732 typename In2,
733 typename In3,
734 typename In4,
735 typename Out1,
736 typename Out2,
737 typename ElementFn,
738 typename ExecPreset = exec_presets::Materialized>
739inline auto SI4_SO2(const char *name,
740 ElementFn &&element_fn,
741 const ExecPreset exec_preset = exec_presets::Materialized())
742{
744 name, std::forward<ElementFn>(element_fn), exec_preset, TypeSequence<In1, In2, In3, In4>());
745}
746
748template<typename In1,
749 typename In2,
750 typename In3,
751 typename In4,
752 typename In5,
753 typename Out1,
754 typename Out2,
755 typename ElementFn,
756 typename ExecPreset = exec_presets::Materialized>
757inline auto SI5_SO2(const char *name,
758 ElementFn &&element_fn,
759 const ExecPreset exec_preset = exec_presets::Materialized())
760{
762 name,
763 std::forward<ElementFn>(element_fn),
764 exec_preset,
766}
767
769template<typename In1,
770 typename Out1,
771 typename Out2,
772 typename Out3,
773 typename ElementFn,
774 typename ExecPreset = exec_presets::Materialized>
775inline auto SI1_SO3(const char *name,
776 ElementFn &&element_fn,
777 const ExecPreset exec_preset = exec_presets::Materialized())
778{
784 std::forward<ElementFn>(element_fn), exec_preset, param_tags);
785 return detail::CustomMF(name, call_fn, param_tags);
786}
787
789template<typename In1,
790 typename Out1,
791 typename Out2,
792 typename Out3,
793 typename Out4,
794 typename ElementFn,
795 typename ExecPreset = exec_presets::Materialized>
796inline auto SI1_SO4(const char *name,
797 ElementFn &&element_fn,
798 const ExecPreset exec_preset = exec_presets::Materialized())
799{
806 std::forward<ElementFn>(element_fn), exec_preset, param_tags);
807 return detail::CustomMF(name, call_fn, param_tags);
808}
809
810} // namespace fn::multi_function::build
811
812namespace fn::multi_function {
813
820 public:
821 /* For compatible hash with typed class. */
822 static constexpr int8_t HASH_ID = 0;
823
824 private:
825 const CPPType &type_;
826 const void *value_;
827 Signature signature_;
828 bool owns_value_;
829
830 template<typename T> friend class CustomMF_Constant;
831
832 public:
833 CustomMF_GenericConstant(const CPPType &type, const void *value, bool make_value_copy);
834 ~CustomMF_GenericConstant() override;
835 void call(const IndexMask &mask, Params params, Context context) const override;
836 void hash_unique(UniqueHashBytes &hash) const override;
837 bool equals(const MultiFunction &other) const override;
838};
839
845 private:
846 GSpan array_;
847 Signature signature_;
848
849 public:
851 void call(const IndexMask &mask, Params params, Context context) const override;
852};
853
857template<typename T> class CustomMF_Constant : public MultiFunction {
858 private:
859 T value_;
860 Signature signature_;
861
862 public:
863 template<typename U> CustomMF_Constant(U &&value) : value_(std::forward<U>(value))
864 {
865 SignatureBuilder builder{"Constant", signature_};
866 builder.single_output<T>("Value");
867 this->set_signature(&signature_);
868 }
869
870 void call(const IndexMask &mask, Params params, Context /*context*/) const override
871 {
873 mask.foreach_index_optimized<int64_t>([&](const int64_t i) { new (&output[i]) T(value_); });
874 }
875
876 void hash_unique(UniqueHashBytes &hash) const override
877 {
879 hash_unique_default(value_, hash);
880 hash.add(&CPPType::get<T>());
881 }
882
883 bool equals(const MultiFunction &other) const override
884 {
885 const CustomMF_Constant *other1 = dynamic_cast<const CustomMF_Constant *>(&other);
886 if (other1 != nullptr) {
887 return value_ == other1->value_;
888 }
889 const CustomMF_GenericConstant *other2 = dynamic_cast<const CustomMF_GenericConstant *>(
890 &other);
891 if (other2 != nullptr) {
892 const CPPType &type = CPPType::get<T>();
893 if (type == other2->type_) {
894 return type.is_equal_or_false(static_cast<const void *>(&value_), other2->value_);
895 }
896 }
897 return false;
898 }
899};
900
902 private:
903 int output_amount_;
904 Signature signature_;
905
906 public:
907 CustomMF_DefaultOutput(Span<DataType> input_types, Span<DataType> output_types);
908 void call(const IndexMask &mask, Params params, Context context) const override;
909};
910
912 private:
913 Signature signature_;
914
915 public:
917 void call(const IndexMask &mask, Params params, Context context) const override;
918};
919
920} // namespace fn::multi_function
921
922} // namespace blender
#define UNUSED_VARS(...)
#define ELEM(...)
#define output
static const CPPType & get()
bool is_equal_or_false(const void *a, const void *b) const
virtual void materialize_compressed(const IndexMask &mask, void *dst, bool dst_is_uninitialized) const
virtual CommonVArrayInfo common_info() const
GVArray()=default
Span< BaseT > base_span() const
bool equals(const MultiFunction &other) const override
void hash_unique(UniqueHashBytes &hash) const override
void call(const IndexMask &mask, Params params, Context) const override
CustomMF_DefaultOutput(Span< DataType > input_types, Span< DataType > output_types)
void call(const IndexMask &mask, Params params, Context context) const override
void call(const IndexMask &mask, Params params, Context context) const override
CustomMF_GenericConstant(const CPPType &type, const void *value, bool make_value_copy)
bool equals(const MultiFunction &other) const override
void hash_unique(UniqueHashBytes &hash) const override
void call(const IndexMask &mask, Params params, Context context) const override
void call(const IndexMask &mask, Params params, Context context) const override
void set_signature(const Signature *signature)
MutableSpan< T > uninitialized_single_output(int param_index, StringRef name="")
MutableSpan< T > single_mutable(int param_index, StringRef name="")
VArray< T > readonly_single_input(int param_index, StringRef name="")
void add(ParamTag< Category, T >, const char *name)
void single_output(const char *name, const ParamFlag flag=ParamFlag::None)
CustomMF(const char *name, CallFn call_fn, TypeSequence< ParamTags... >)
void call(const IndexMask &mask, Params params, Context) const override
const IndexMask & update(IndexMaskSegment segment)
#define T
auto build_multi_function_with_n_inputs_one_output(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset, TypeSequence< In... >)
auto build_multi_function_with_n_inputs_two_outputs(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset, TypeSequence< In... >)
auto build_multi_function_call_from_element_fn(ElementFn &&element_fn, const ExecPreset exec_preset, TypeSequence< ParamTags... >)
void execute_element_fn_as_multi_function(const ElementFn &element_fn, const ExecPreset exec_preset, const IndexMask &mask, Params params, TypeSequence< ParamTags... >, std::index_sequence< I... >)
void execute_array(const ElementFn &element_fn, MaskT mask, Args &&__restrict... args)
void execute_materialized_impl(TypeSequence< ParamTags... >, const ElementFn &element_fn, const int64_t size, Chunks &&__restrict... chunks)
void execute_materialized(TypeSequence< ParamTags... >, std::index_sequence< I... >, const ElementFn &element_fn, const IndexMaskSegment mask, const std::tuple< LoadedParams... > &loaded_params)
auto SI1_SO2(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI4_SO2(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI4_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI2_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI5_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI8_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI1_SO3(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SM(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::AllSpanOrSingle())
auto SI1_SO4(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI2_SO2(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI5_SO2(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())
auto SI3_SO2(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
auto SI6_SO(const char *name, ElementFn &&element_fn, const ExecPreset exec_preset=exec_presets::Materialized())
bool non_empty_is_range(const Span< T > indices)
BMesh Iterators.
UserDef U
Definition blender.cc:58
__int64 int64_t
Definition BKE_text.h:90
bool call_with_devirtualized_parameters(const std::tuple< Devirtualizers... > &devis, const Fn &fn)
BMesh const char void * data
void uninitialized_fill_n(T *dst, int64_t n, const T &value)
void hash_unique_default(const T &value, UniqueHashBytes &hash)
void destruct_n(T *ptr, int64_t n)
signed char int8_t
Definition BKE_text.h:76
#define I
#define hash
Definition noise_c.cc:156
auto create_devirtualizers(TypeSequence< ParamTags... >, std::index_sequence< I... >, const std::tuple< LoadedParams... > &loaded_params) const
auto create_devirtualizers(TypeSequence< ParamTags... >, std::index_sequence< I... >, const std::tuple< LoadedParams... > &loaded_params) const