[Bf-blender-cvs] [3e533af7020] functions: make some functions more reusable

Jacques Lucke noreply at git.blender.org
Fri Feb 14 11:40:09 CET 2020


Commit: 3e533af70207e78ed83d4213a3caa169970c7608
Author: Jacques Lucke
Date:   Fri Feb 14 11:29:49 2020 +0100
Branches: functions
https://developer.blender.org/rB3e533af70207e78ed83d4213a3caa169970c7608

make some functions more reusable

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

M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_multi_functions.h
M	source/blender/functions/intern/initialize.cc
A	source/blender/functions/intern/multi_functions/global_functions.cc
A	source/blender/functions/intern/multi_functions/global_functions.h
M	source/blender/functions/intern/node_tree_multi_function_network/builder.h
M	source/blender/functions/intern/node_tree_multi_function_network/mappings_nodes.cc

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

diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 53b61cfe89c..fc9f97a6fcf 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -24,6 +24,7 @@ endif()
 
 set(SRC
   intern/multi_functions/constants.cc
+  intern/multi_functions/global_functions.cc
   intern/multi_functions/lists.cc
   intern/multi_functions/mixed.cc
   intern/multi_functions/network.cc
@@ -71,6 +72,7 @@ set(SRC
 
   intern/multi_functions/constants.h
   intern/multi_functions/customizable.h
+  intern/multi_functions/global_functions.h
   intern/multi_functions/lists.h
   intern/multi_functions/mixed.h
   intern/multi_functions/network.h
diff --git a/source/blender/functions/FN_multi_functions.h b/source/blender/functions/FN_multi_functions.h
index 2c9b4ac5a9d..ff59767f518 100644
--- a/source/blender/functions/FN_multi_functions.h
+++ b/source/blender/functions/FN_multi_functions.h
@@ -9,5 +9,6 @@
 #include "intern/multi_functions/particles.h"
 #include "intern/multi_functions/surface_hook.h"
 #include "intern/multi_functions/vectorize.h"
+#include "intern/multi_functions/global_functions.h"
 
 #endif /* __FN_MULTI_FUNCTIONS_H__ */
diff --git a/source/blender/functions/intern/initialize.cc b/source/blender/functions/intern/initialize.cc
index 962998e6796..f079aaa7837 100644
--- a/source/blender/functions/intern/initialize.cc
+++ b/source/blender/functions/intern/initialize.cc
@@ -1,15 +1,18 @@
 #include "FN_initialize.h"
 #include "cpp_types.h"
 #include "node_tree_multi_function_network/mappings.h"
+#include "multi_functions/global_functions.h"
 
 void FN_initialize(void)
 {
   FN::init_cpp_types();
+  FN::init_global_functions();
   FN::MFGeneration::init_function_tree_mf_mappings();
 }
 
 void FN_exit(void)
 {
   FN::MFGeneration::free_function_tree_mf_mappings();
+  FN::free_global_functions();
   FN::free_cpp_types();
 }
diff --git a/source/blender/functions/intern/multi_functions/global_functions.cc b/source/blender/functions/intern/multi_functions/global_functions.cc
new file mode 100644
index 00000000000..31d2f667a34
--- /dev/null
+++ b/source/blender/functions/intern/multi_functions/global_functions.cc
@@ -0,0 +1,33 @@
+#include "global_functions.h"
+#include "FN_multi_functions.h"
+
+namespace FN {
+
+const MultiFunction *MF_GLOBAL_add_floats_2 = nullptr;
+const MultiFunction *MF_GLOBAL_multiply_floats_2 = nullptr;
+const MultiFunction *MF_GLOBAL_subtract_floats = nullptr;
+const MultiFunction *MF_GLOBAL_safe_division_floats = nullptr;
+
+void init_global_functions()
+{
+  MF_GLOBAL_add_floats_2 = new MF_Custom_In2_Out1<float, float, float>(
+      "add 2 floats", [](float a, float b) { return a + b; }, BLI_RAND_PER_LINE_UINT32);
+  MF_GLOBAL_multiply_floats_2 = new MF_Custom_In2_Out1<float, float, float>(
+      "multiply 2 floats", [](float a, float b) { return a * b; }, BLI_RAND_PER_LINE_UINT32);
+  MF_GLOBAL_subtract_floats = new MF_Custom_In2_Out1<float, float, float>(
+      "subtract 2 floats", [](float a, float b) { return a - b; }, BLI_RAND_PER_LINE_UINT32);
+  MF_GLOBAL_safe_division_floats = new MF_Custom_In2_Out1<float, float, float>(
+      "safe divide 2 floats",
+      [](float a, float b) { return (b != 0.0f) ? a / b : 0.0f; },
+      BLI_RAND_PER_LINE_UINT32);
+}
+
+void free_global_functions()
+{
+  delete MF_GLOBAL_add_floats_2;
+  delete MF_GLOBAL_multiply_floats_2;
+  delete MF_GLOBAL_subtract_floats;
+  delete MF_GLOBAL_safe_division_floats;
+}
+
+}  // namespace FN
diff --git a/source/blender/functions/intern/multi_functions/global_functions.h b/source/blender/functions/intern/multi_functions/global_functions.h
new file mode 100644
index 00000000000..6a77ec16621
--- /dev/null
+++ b/source/blender/functions/intern/multi_functions/global_functions.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "FN_multi_function.h"
+
+namespace FN {
+
+void init_global_functions();
+void free_global_functions();
+
+extern const MultiFunction *MF_GLOBAL_add_floats_2;
+extern const MultiFunction *MF_GLOBAL_multiply_floats_2;
+extern const MultiFunction *MF_GLOBAL_subtract_floats;
+extern const MultiFunction *MF_GLOBAL_safe_division_floats;
+
+}  // namespace FN
diff --git a/source/blender/functions/intern/node_tree_multi_function_network/builder.h b/source/blender/functions/intern/node_tree_multi_function_network/builder.h
index 895b28c82a6..6c22beb6280 100644
--- a/source/blender/functions/intern/node_tree_multi_function_network/builder.h
+++ b/source/blender/functions/intern/node_tree_multi_function_network/builder.h
@@ -346,6 +346,12 @@ class FNodeMFBuilder : public CommonBuilderBase {
                                               Args &&... args)
   {
     const MultiFunction &base_fn = this->construct_fn<T>(std::forward<Args>(args)...);
+    this->set_vectorized_matching_fn(is_vectorized_prop_names, base_fn);
+  }
+
+  void set_vectorized_matching_fn(ArrayRef<const char *> is_vectorized_prop_names,
+                                  const MultiFunction &base_fn)
+  {
     const MultiFunction &fn = this->get_vectorized_function(base_fn, is_vectorized_prop_names);
     this->set_matching_fn(fn);
   }
diff --git a/source/blender/functions/intern/node_tree_multi_function_network/mappings_nodes.cc b/source/blender/functions/intern/node_tree_multi_function_network/mappings_nodes.cc
index efdfe63fe7d..ea77b560e14 100644
--- a/source/blender/functions/intern/node_tree_multi_function_network/mappings_nodes.cc
+++ b/source/blender/functions/intern/node_tree_multi_function_network/mappings_nodes.cc
@@ -179,6 +179,11 @@ static void build_math_fn_in2_out1(FNodeMFBuilder &builder,
       {"use_list__a", "use_list__b"}, builder.fnode().name(), element_func, operation_hash);
 }
 
+static void build_math_fn_in2_out1(FNodeMFBuilder &builder, const MultiFunction &base_fn)
+{
+  builder.set_vectorized_matching_fn({"use_list__a", "use_list__b"}, base_fn);
+}
+
 template<typename T, typename FuncT>
 static void build_variadic_math_fn(FNodeMFBuilder &builder,
                                    FuncT element_func,
@@ -233,16 +238,12 @@ static void INSERT_maximum_floats(FNodeMFBuilder &builder)
 
 static void INSERT_subtract_floats(FNodeMFBuilder &builder)
 {
-  build_math_fn_in2_out1<float, float, float>(
-      builder, [](float a, float b) -> float { return a - b; }, BLI_RAND_PER_LINE_UINT32);
+  build_math_fn_in2_out1(builder, *MF_GLOBAL_subtract_floats);
 }
 
 static void INSERT_divide_floats(FNodeMFBuilder &builder)
 {
-  build_math_fn_in2_out1<float, float, float>(
-      builder,
-      [](float a, float b) -> float { return (b != 0.0f) ? a / b : 0.0f; },
-      BLI_RAND_PER_LINE_UINT32);
+  build_math_fn_in2_out1(builder, *MF_GLOBAL_safe_division_floats);
 }
 
 static void INSERT_power_floats(FNodeMFBuilder &builder)



More information about the Bf-blender-cvs mailing list