[Bf-blender-cvs] [b3146200a8c] master: Functions: refactor multi-function builder API

Jacques Lucke noreply at git.blender.org
Sat Jan 7 16:21:09 CET 2023


Commit: b3146200a8c4265de7fba2e2d40ef84f99d979e2
Author: Jacques Lucke
Date:   Sat Jan 7 16:19:59 2023 +0100
Branches: master
https://developer.blender.org/rBb3146200a8c4265de7fba2e2d40ef84f99d979e2

Functions: refactor multi-function builder API

* New `build_mf` namespace for the multi-function builders.
* The type name of the created multi-functions is now "private",
  i.e. the caller has to use `auto`. This has the benefit that the
  implementation can change more freely without affecting
  the caller.
* `CustomMF` does not use `std::function` internally anymore.
  This reduces some overhead during code generation and at
  run-time.
* `CustomMF` now supports single-mutable parameters.

===================================================================

M	source/blender/blenkernel/intern/geometry_component_curves.cc
M	source/blender/blenkernel/intern/geometry_component_mesh.cc
M	source/blender/blenkernel/intern/type_conversions.cc
M	source/blender/functions/FN_multi_function_builder.hh
M	source/blender/functions/intern/field.cc
M	source/blender/functions/tests/FN_field_test.cc
M	source/blender/functions/tests/FN_multi_function_procedure_test.cc
M	source/blender/functions/tests/FN_multi_function_test.cc
M	source/blender/geometry/intern/resample_curves.cc
M	source/blender/nodes/NOD_math_functions.hh
M	source/blender/nodes/function/nodes/node_fn_boolean_math.cc
M	source/blender/nodes/function/nodes/node_fn_combine_color.cc
M	source/blender/nodes/function/nodes/node_fn_compare.cc
M	source/blender/nodes/function/nodes/node_fn_float_to_int.cc
M	source/blender/nodes/function/nodes/node_fn_random_value.cc
M	source/blender/nodes/function/nodes/node_fn_replace_string.cc
M	source/blender/nodes/function/nodes/node_fn_rotate_euler.cc
M	source/blender/nodes/function/nodes/node_fn_slice_string.cc
M	source/blender/nodes/function/nodes/node_fn_string_length.cc
M	source/blender/nodes/function/nodes/node_fn_value_to_string.cc
M	source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_to_points.cc
M	source/blender/nodes/geometry/nodes/node_geo_switch.cc
M	source/blender/nodes/shader/nodes/node_shader_clamp.cc
M	source/blender/nodes/shader/nodes/node_shader_map_range.cc
M	source/blender/nodes/shader/nodes/node_shader_math.cc
M	source/blender/nodes/shader/nodes/node_shader_mix.cc
M	source/blender/nodes/shader/nodes/node_shader_sepcomb_rgb.cc
M	source/blender/nodes/shader/nodes/node_shader_sepcomb_xyz.cc
M	source/blender/nodes/shader/nodes/node_shader_vector_math.cc
M	source/blender/nodes/shader/nodes/node_shader_vector_rotate.cc

===================================================================

diff --git a/source/blender/blenkernel/intern/geometry_component_curves.cc b/source/blender/blenkernel/intern/geometry_component_curves.cc
index debcb35699e..d1d0af2b725 100644
--- a/source/blender/blenkernel/intern/geometry_component_curves.cc
+++ b/source/blender/blenkernel/intern/geometry_component_curves.cc
@@ -440,12 +440,12 @@ static ComponentAttributeProviders create_attribute_providers_for_curve()
                                                     make_array_write_attribute<float3>,
                                                     tag_component_positions_changed);
 
-  static const fn::CustomMF_SI_SO<int8_t, int8_t> handle_type_clamp{
+  static auto handle_type_clamp = fn::build_mf::SI1_SO<int8_t, int8_t>(
       "Handle Type Validate",
       [](int8_t value) {
         return std::clamp<int8_t>(value, BEZIER_HANDLE_FREE, BEZIER_HANDLE_ALIGN);
       },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider handle_type_right("handle_type_right",
                                                           ATTR_DOMAIN_POINT,
                                                           CD_PROP_INT8,
@@ -484,10 +484,10 @@ static ComponentAttributeProviders create_attribute_providers_for_curve()
                                                      make_array_write_attribute<float>,
                                                      tag_component_positions_changed);
 
-  static const fn::CustomMF_SI_SO<int8_t, int8_t> nurbs_order_clamp{
+  static const auto nurbs_order_clamp = fn::build_mf::SI1_SO<int8_t, int8_t>(
       "NURBS Order Validate",
       [](int8_t value) { return std::max<int8_t>(value, 0); },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider nurbs_order("nurbs_order",
                                                     ATTR_DOMAIN_CURVE,
                                                     CD_PROP_INT8,
@@ -501,12 +501,12 @@ static ComponentAttributeProviders create_attribute_providers_for_curve()
                                                     tag_component_topology_changed,
                                                     AttributeValidator{&nurbs_order_clamp});
 
-  static const fn::CustomMF_SI_SO<int8_t, int8_t> normal_mode_clamp{
+  static const auto normal_mode_clamp = fn::build_mf::SI1_SO<int8_t, int8_t>(
       "Normal Mode Validate",
       [](int8_t value) {
         return std::clamp<int8_t>(value, NORMAL_MODE_MINIMUM_TWIST, NORMAL_MODE_Z_UP);
       },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider normal_mode("normal_mode",
                                                     ATTR_DOMAIN_CURVE,
                                                     CD_PROP_INT8,
@@ -520,12 +520,12 @@ static ComponentAttributeProviders create_attribute_providers_for_curve()
                                                     tag_component_normals_changed,
                                                     AttributeValidator{&normal_mode_clamp});
 
-  static const fn::CustomMF_SI_SO<int8_t, int8_t> knots_mode_clamp{
+  static const auto knots_mode_clamp = fn::build_mf::SI1_SO<int8_t, int8_t>(
       "Knots Mode Validate",
       [](int8_t value) {
         return std::clamp<int8_t>(value, NURBS_KNOT_MODE_NORMAL, NURBS_KNOT_MODE_ENDPOINT_BEZIER);
       },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider nurbs_knots_mode("knots_mode",
                                                          ATTR_DOMAIN_CURVE,
                                                          CD_PROP_INT8,
@@ -539,12 +539,12 @@ static ComponentAttributeProviders create_attribute_providers_for_curve()
                                                          tag_component_topology_changed,
                                                          AttributeValidator{&knots_mode_clamp});
 
-  static const fn::CustomMF_SI_SO<int8_t, int8_t> curve_type_clamp{
+  static const auto curve_type_clamp = fn::build_mf::SI1_SO<int8_t, int8_t>(
       "Curve Type Validate",
       [](int8_t value) {
         return std::clamp<int8_t>(value, CURVE_TYPE_CATMULL_ROM, CURVE_TYPES_NUM);
       },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider curve_type("curve_type",
                                                    ATTR_DOMAIN_CURVE,
                                                    CD_PROP_INT8,
@@ -558,10 +558,10 @@ static ComponentAttributeProviders create_attribute_providers_for_curve()
                                                    tag_component_curve_types_changed,
                                                    AttributeValidator{&curve_type_clamp});
 
-  static const fn::CustomMF_SI_SO<int, int> resolution_clamp{
+  static const auto resolution_clamp = fn::build_mf::SI1_SO<int, int>(
       "Resolution Validate",
       [](int value) { return std::max<int>(value, 1); },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider resolution("resolution",
                                                    ATTR_DOMAIN_CURVE,
                                                    CD_PROP_INT32,
diff --git a/source/blender/blenkernel/intern/geometry_component_mesh.cc b/source/blender/blenkernel/intern/geometry_component_mesh.cc
index 855d422251d..b18bdc62d82 100644
--- a/source/blender/blenkernel/intern/geometry_component_mesh.cc
+++ b/source/blender/blenkernel/intern/geometry_component_mesh.cc
@@ -1264,13 +1264,13 @@ static ComponentAttributeProviders create_attribute_providers_for_mesh()
                                            make_array_write_attribute<int>,
                                            nullptr);
 
-  static const fn::CustomMF_SI_SO<int, int> material_index_clamp{
+  static const auto material_index_clamp = fn::build_mf::SI1_SO<int, int>(
       "Material Index Validate",
       [](int value) {
         /* Use #short for the maximum since many areas still use that type for indices. */
         return std::clamp<int>(value, 0, std::numeric_limits<short>::max());
       },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static BuiltinCustomDataLayerProvider material_index("material_index",
                                                        ATTR_DOMAIN_FACE,
                                                        CD_PROP_INT32,
diff --git a/source/blender/blenkernel/intern/type_conversions.cc b/source/blender/blenkernel/intern/type_conversions.cc
index c3d8f84d5c3..5aca83d415d 100644
--- a/source/blender/blenkernel/intern/type_conversions.cc
+++ b/source/blender/blenkernel/intern/type_conversions.cc
@@ -20,12 +20,12 @@ static void add_implicit_conversion(DataTypeConversions &conversions)
   static const CPPType &to_type = CPPType::get<To>();
   static const std::string conversion_name = from_type.name() + " to " + to_type.name();
 
-  static fn::CustomMF_SI_SO<From, To> multi_function{
+  static auto multi_function = fn::build_mf::SI1_SO<From, To>(
       conversion_name.c_str(),
       /* Use lambda instead of passing #ConversionF directly, because otherwise the compiler won't
        * inline the function. */
       [](const From &a) { return ConversionF(a); },
-      fn::CustomMF_presets::AllSpanOrSingle()};
+      fn::build_mf::exec_presets::AllSpanOrSingle());
   static auto convert_single_to_initialized = [](const void *src, void *dst) {
     *(To *)dst = ConversionF(*(const From *)src);
   };
diff --git a/source/blender/functions/FN_multi_function_builder.hh b/source/blender/functions/FN_multi_function_builder.hh
index b0876c13729..3a491a63e6c 100644
--- a/source/blender/functions/FN_multi_function_builder.hh
+++ b/source/blender/functions/FN_multi_function_builder.hh
@@ -8,17 +8,15 @@
  * This file contains several utilities to create multi-functions with less redundant code.
  */
 
-#include <functional>
-
 #include "FN_multi_function.hh"
 
-namespace blender::fn {
+namespace blender::fn::build_mf {
 
 /**
  * These presets determine what code is generated for a #CustomMF. Different presets make different
  * trade-offs between run-time performance and compile-time/binary size.
  */
-namespace CustomMF_presets {
+namespace exec_presets {
 
 /** Method to execute a function in case devirtualization was not possible. */
 enum class FallbackMode {
@@ -63,7 +61,7 @@ struct AllSpanOrSingle {
   auto create_devirtualizers(TypeSequence<ParamTags...> /*param_tags*/,
                              std::index_sequence<I...> /*indices*/,
                              const IndexMask &mask,
-                             const std::tuple<LoadedParams...> &loaded_params)
+                             const std::tuple<LoadedParams...> &loaded_params) const
   {
     return std::make_tuple(IndexMaskDevirtualizer<true, true>{mask}, [&]() {
       typedef ParamTags ParamTag;
@@ -72,7 +70,9 @@ struct AllSpanOrSingle {
         const GVArrayImpl &varray_impl = *std::get<I>(loaded_params);
         return GVArrayDevirtualizer<T, true, true>{varray_impl};
       }
-      else if constexpr (ParamTag::category == MFParamCategory::SingleOutput) {
+      else if constexpr (ELEM(ParamTag::category,
+                              MFParamCategory::SingleOutput,
+                              MFParamCategory::SingleMutable)) {
         T *ptr = std::get<I>(loaded_params);
         return BasicDevirtualizer<T *>{ptr};
       }
@@ -93,7 +93,7 @@ template<size_t... Indices> struct SomeSpanOrSingle {
   auto create_devirtualizers(TypeSequence<ParamTags...> /*param_tags*/,
                              std::index_sequence<I...> /*indices*/,
                              const IndexMask &mask,
-                             const std::tuple<LoadedParams...> &loaded_params)
+                             const std::tuple<LoadedParams...> &load

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list