diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-21 21:31:24 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-21 21:33:13 +0300 |
commit | 4fe8d0419c2f080a248f52b3924ce2a4e897e5cb (patch) | |
tree | 81aba45e26cca99578087835184ce5664362b791 /source/blender/functions/FN_multi_function_builder.hh | |
parent | 68c31c41e52caa1ac5b527f835b16f8e298dfd86 (diff) |
Functions: refactor virtual array data structures
When a function is executed for many elements (e.g. per point) it is often the case
that some parameters are different for every element and other parameters are
the same (there are some more less common cases). To simplify writing such
functions one can use a "virtual array". This is a data structure that has a value
for every index, but might not be stored as an actual array internally. Instead, it
might be just a single value or is computed on the fly. There are various tradeoffs
involved when using this data structure which are mentioned in `BLI_virtual_array.hh`.
It is called "virtual", because it uses inheritance and virtual methods.
Furthermore, there is a new virtual vector array data structure, which is an array
of vectors. Both these types have corresponding generic variants, which can be used
when the data type is not known at compile time. This is typically the case when
building a somewhat generic execution system. The function system used these virtual
data structures before, but now they are more versatile.
I've done this refactor in preparation for the attribute processor and other features of
geometry nodes. I moved the typed virtual arrays to blenlib, so that they can be used
independent of the function system.
One open question for me is whether all the generic data structures (and `CPPType`)
should be moved to blenlib as well. They are well isolated and don't really contain
any business logic. That can be done later if necessary.
Diffstat (limited to 'source/blender/functions/FN_multi_function_builder.hh')
-rw-r--r-- | source/blender/functions/FN_multi_function_builder.hh | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/source/blender/functions/FN_multi_function_builder.hh b/source/blender/functions/FN_multi_function_builder.hh index 0cd1bc262be..b73c41d3f59 100644 --- a/source/blender/functions/FN_multi_function_builder.hh +++ b/source/blender/functions/FN_multi_function_builder.hh @@ -38,7 +38,7 @@ namespace blender::fn { */ template<typename In1, typename Out1> class CustomMF_SI_SO : public MultiFunction { private: - using FunctionT = std::function<void(IndexMask, VSpan<In1>, MutableSpan<Out1>)>; + using FunctionT = std::function<void(IndexMask, const VArray<In1> &, MutableSpan<Out1>)>; FunctionT function_; public: @@ -57,7 +57,7 @@ template<typename In1, typename Out1> class CustomMF_SI_SO : public MultiFunctio template<typename ElementFuncT> static FunctionT create_function(ElementFuncT element_fn) { - return [=](IndexMask mask, VSpan<In1> in1, MutableSpan<Out1> out1) { + return [=](IndexMask mask, const VArray<In1> &in1, MutableSpan<Out1> out1) { mask.foreach_index( [&](int i) { new (static_cast<void *>(&out1[i])) Out1(element_fn(in1[i])); }); }; @@ -65,7 +65,7 @@ template<typename In1, typename Out1> class CustomMF_SI_SO : public MultiFunctio void call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const override { - VSpan<In1> in1 = params.readonly_single_input<In1>(0); + const VArray<In1> &in1 = params.readonly_single_input<In1>(0); MutableSpan<Out1> out1 = params.uninitialized_single_output<Out1>(1); function_(mask, in1, out1); } @@ -80,7 +80,8 @@ template<typename In1, typename Out1> class CustomMF_SI_SO : public MultiFunctio template<typename In1, typename In2, typename Out1> class CustomMF_SI_SI_SO : public MultiFunction { private: - using FunctionT = std::function<void(IndexMask, VSpan<In1>, VSpan<In2>, MutableSpan<Out1>)>; + using FunctionT = + std::function<void(IndexMask, const VArray<In1> &, const VArray<In2> &, MutableSpan<Out1>)>; FunctionT function_; public: @@ -100,7 +101,10 @@ class CustomMF_SI_SI_SO : public MultiFunction { template<typename ElementFuncT> static FunctionT create_function(ElementFuncT element_fn) { - return [=](IndexMask mask, VSpan<In1> in1, VSpan<In2> in2, MutableSpan<Out1> out1) { + return [=](IndexMask mask, + const VArray<In1> &in1, + const VArray<In2> &in2, + MutableSpan<Out1> out1) { mask.foreach_index( [&](int i) { new (static_cast<void *>(&out1[i])) Out1(element_fn(in1[i], in2[i])); }); }; @@ -108,8 +112,8 @@ class CustomMF_SI_SI_SO : public MultiFunction { void call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const override { - VSpan<In1> in1 = params.readonly_single_input<In1>(0); - VSpan<In2> in2 = params.readonly_single_input<In2>(1); + const VArray<In1> &in1 = params.readonly_single_input<In1>(0); + const VArray<In2> &in2 = params.readonly_single_input<In2>(1); MutableSpan<Out1> out1 = params.uninitialized_single_output<Out1>(2); function_(mask, in1, in2, out1); } @@ -125,8 +129,11 @@ class CustomMF_SI_SI_SO : public MultiFunction { template<typename In1, typename In2, typename In3, typename Out1> class CustomMF_SI_SI_SI_SO : public MultiFunction { private: - using FunctionT = - std::function<void(IndexMask, VSpan<In1>, VSpan<In2>, VSpan<In3>, MutableSpan<Out1>)>; + using FunctionT = std::function<void(IndexMask, + const VArray<In1> &, + const VArray<In2> &, + const VArray<In3> &, + MutableSpan<Out1>)>; FunctionT function_; public: @@ -148,9 +155,9 @@ class CustomMF_SI_SI_SI_SO : public MultiFunction { template<typename ElementFuncT> static FunctionT create_function(ElementFuncT element_fn) { return [=](IndexMask mask, - VSpan<In1> in1, - VSpan<In2> in2, - VSpan<In3> in3, + const VArray<In1> &in1, + const VArray<In2> &in2, + const VArray<In3> &in3, MutableSpan<Out1> out1) { mask.foreach_index([&](int i) { new (static_cast<void *>(&out1[i])) Out1(element_fn(in1[i], in2[i], in3[i])); @@ -160,9 +167,9 @@ class CustomMF_SI_SI_SI_SO : public MultiFunction { void call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const override { - VSpan<In1> in1 = params.readonly_single_input<In1>(0); - VSpan<In2> in2 = params.readonly_single_input<In2>(1); - VSpan<In3> in3 = params.readonly_single_input<In3>(2); + const VArray<In1> &in1 = params.readonly_single_input<In1>(0); + const VArray<In2> &in2 = params.readonly_single_input<In2>(1); + const VArray<In3> &in3 = params.readonly_single_input<In3>(2); MutableSpan<Out1> out1 = params.uninitialized_single_output<Out1>(3); function_(mask, in1, in2, in3, out1); } @@ -179,8 +186,12 @@ class CustomMF_SI_SI_SI_SO : public MultiFunction { template<typename In1, typename In2, typename In3, typename In4, typename Out1> class CustomMF_SI_SI_SI_SI_SO : public MultiFunction { private: - using FunctionT = std::function<void( - IndexMask, VSpan<In1>, VSpan<In2>, VSpan<In3>, VSpan<In4>, MutableSpan<Out1>)>; + using FunctionT = std::function<void(IndexMask, + const VArray<In1> &, + const VArray<In2> &, + const VArray<In3> &, + const VArray<In4> &, + MutableSpan<Out1>)>; FunctionT function_; public: @@ -203,10 +214,10 @@ class CustomMF_SI_SI_SI_SI_SO : public MultiFunction { template<typename ElementFuncT> static FunctionT create_function(ElementFuncT element_fn) { return [=](IndexMask mask, - VSpan<In1> in1, - VSpan<In2> in2, - VSpan<In3> in3, - VSpan<In4> in4, + const VArray<In1> &in1, + const VArray<In2> &in2, + const VArray<In3> &in3, + const VArray<In4> &in4, MutableSpan<Out1> out1) { mask.foreach_index([&](int i) { new (static_cast<void *>(&out1[i])) Out1(element_fn(in1[i], in2[i], in3[i], in4[i])); @@ -216,10 +227,10 @@ class CustomMF_SI_SI_SI_SI_SO : public MultiFunction { void call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const override { - VSpan<In1> in1 = params.readonly_single_input<In1>(0); - VSpan<In2> in2 = params.readonly_single_input<In2>(1); - VSpan<In3> in3 = params.readonly_single_input<In3>(2); - VSpan<In4> in4 = params.readonly_single_input<In4>(3); + const VArray<In1> &in1 = params.readonly_single_input<In1>(0); + const VArray<In2> &in2 = params.readonly_single_input<In2>(1); + const VArray<In3> &in3 = params.readonly_single_input<In3>(2); + const VArray<In4> &in4 = params.readonly_single_input<In4>(3); MutableSpan<Out1> out1 = params.uninitialized_single_output<Out1>(4); function_(mask, in1, in2, in3, in4, out1); } @@ -276,7 +287,7 @@ template<typename From, typename To> class CustomMF_Convert : public MultiFuncti void call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const override { - VSpan<From> inputs = params.readonly_single_input<From>(0); + const VArray<From> &inputs = params.readonly_single_input<From>(0); MutableSpan<To> outputs = params.uninitialized_single_output<To>(1); for (int64_t i : mask) { |