[Bf-blender-cvs] [3576e669cbc] geometry-nodes: Nodes: initial abstraction to simplify math node implementations

Jacques Lucke noreply at git.blender.org
Wed Nov 25 15:40:47 CET 2020


Commit: 3576e669cbca2957ed29eb877d9161310374da63
Author: Jacques Lucke
Date:   Wed Nov 25 15:38:08 2020 +0100
Branches: geometry-nodes
https://developer.blender.org/rB3576e669cbca2957ed29eb877d9161310374da63

Nodes: initial abstraction to simplify math node implementations

Currently, implementing a node that supports math operations requires
you to write a long switch statement. The goal of this abstraction
is to reduce the number of these switch statements.

Note, this commit adds new such long switch cases. However, in the upcoming
commits existing math nodes are updated to use this abstraction, resulting in
less code overall.

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

M	source/blender/nodes/CMakeLists.txt
A	source/blender/nodes/NOD_math_functions.hh
A	source/blender/nodes/intern/math_functions.cc

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

diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 7115c20cf97..a367f40dca7 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -274,6 +274,7 @@ set(SRC
   texture/node_texture_util.c
 
   intern/derived_node_tree.cc
+  intern/math_functions.cc
   intern/node_common.c
   intern/node_exec.c
   intern/node_geometry_exec.cc
@@ -299,6 +300,7 @@ set(SRC
   NOD_node_tree_ref.hh
   NOD_shader.h
   NOD_geometry.h
+  NOD_math_functions.hh
   NOD_socket.h
   NOD_static_types.h
   NOD_texture.h
diff --git a/source/blender/nodes/NOD_math_functions.hh b/source/blender/nodes/NOD_math_functions.hh
new file mode 100644
index 00000000000..70e4174a844
--- /dev/null
+++ b/source/blender/nodes/NOD_math_functions.hh
@@ -0,0 +1,200 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include "DNA_node_types.h"
+
+#include "BLI_math_base_safe.h"
+#include "BLI_math_rotation.h"
+#include "BLI_string_ref.hh"
+
+namespace blender::nodes {
+
+struct FloatMathOperationInfo {
+  StringRefNull title_case_name;
+  StringRefNull shader_name;
+
+  FloatMathOperationInfo() = delete;
+  FloatMathOperationInfo(StringRefNull title_case_name, StringRefNull shader_name)
+      : title_case_name(title_case_name), shader_name(shader_name)
+  {
+  }
+};
+
+const FloatMathOperationInfo *get_float_math_operation_info(const int operation);
+
+/**
+ * This calls the `callback` with two arguments:
+ *  1. The math function that takes a float as input and outputs a new float.
+ *  2. A #FloatMathOperationInfo struct reference.
+ * Returns true when the callback has been called, otherwise false.
+ *
+ * The math function that is passed to the callback is actually a lambda function that is different
+ * for every operation. Therefore, if the callback is templated on the math function, it will get
+ * instantiated for every operation separately. This has two benefits:
+ *  - The compiler can optimize the callback for every operation separately.
+ *  - A static variable declared in the callback will be generated for every operation separately.
+ *
+ * If separate instantiations are not desired, the callback can also take a function pointer with
+ * the following signature as input instead: float (*math_function)(float a).
+ */
+template<typename Callback>
+inline bool try_dispatch_float_math_fl_to_fl(const int operation, Callback &&callback)
+{
+  const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
+  if (info == nullptr) {
+    return false;
+  }
+
+  /* This is just an utility function to keep the individual cases smaller. */
+  auto dispatch = [&](auto math_function) -> bool {
+    callback(math_function, *info);
+    return true;
+  };
+
+  switch (operation) {
+    case NODE_MATH_EXPONENT:
+      return dispatch([](float a) { return expf(a); });
+    case NODE_MATH_SQRT:
+      return dispatch([](float a) { return safe_sqrtf(a); });
+    case NODE_MATH_INV_SQRT:
+      return dispatch([](float a) { return safe_inverse_sqrtf(a); });
+    case NODE_MATH_ABSOLUTE:
+      return dispatch([](float a) { return fabs(a); });
+    case NODE_MATH_RADIANS:
+      return dispatch([](float a) { return (float)DEG2RAD(a); });
+    case NODE_MATH_DEGREES:
+      return dispatch([](float a) { return (float)RAD2DEG(a); });
+    case NODE_MATH_SIGN:
+      return dispatch([](float a) { return compatible_signf(a); });
+    case NODE_MATH_ROUND:
+      return dispatch([](float a) { return floorf(a + 0.5f); });
+    case NODE_MATH_FLOOR:
+      return dispatch([](float a) { return floorf(a); });
+    case NODE_MATH_CEIL:
+      return dispatch([](float a) { return ceilf(a); });
+    case NODE_MATH_FRACTION:
+      return dispatch([](float a) { return a - floorf(a); });
+    case NODE_MATH_TRUNC:
+      return dispatch([](float a) { return a >= 0.0f ? floorf(a) : ceilf(a); });
+    case NODE_MATH_SINE:
+      return dispatch([](float a) { return sinf(a); });
+    case NODE_MATH_COSINE:
+      return dispatch([](float a) { return cosf(a); });
+    case NODE_MATH_TANGENT:
+      return dispatch([](float a) { return tanf(a); });
+    case NODE_MATH_SINH:
+      return dispatch([](float a) { return sinhf(a); });
+    case NODE_MATH_COSH:
+      return dispatch([](float a) { return coshf(a); });
+    case NODE_MATH_TANH:
+      return dispatch([](float a) { return tanhf(a); });
+    case NODE_MATH_ARCSINE:
+      return dispatch([](float a) { return safe_asinf(a); });
+    case NODE_MATH_ARCCOSINE:
+      return dispatch([](float a) { return safe_acosf(a); });
+    case NODE_MATH_ARCTANGENT:
+      return dispatch([](float a) { return atanf(a); });
+  }
+  return false;
+}
+
+/**
+ * This is similar to try_dispatch_float_math_fl_to_fl, just with a different callback signature.
+ */
+template<typename Callback>
+inline bool try_dispatch_float_math_fl_fl_to_fl(const int operation, Callback &&callback)
+{
+  const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
+  if (info == nullptr) {
+    return false;
+  }
+
+  /* This is just an utility function to keep the individual cases smaller. */
+  auto dispatch = [&](auto math_function) -> bool {
+    callback(math_function, *info);
+    return true;
+  };
+
+  switch (operation) {
+    case NODE_MATH_ADD:
+      return dispatch([](float a, float b) { return a + b; });
+    case NODE_MATH_SUBTRACT:
+      return dispatch([](float a, float b) { return a - b; });
+    case NODE_MATH_MULTIPLY:
+      return dispatch([](float a, float b) { return a * b; });
+    case NODE_MATH_DIVIDE:
+      return dispatch([](float a, float b) { return safe_divide(a, b); });
+    case NODE_MATH_POWER:
+      return dispatch([](float a, float b) { return safe_powf(a, b); });
+    case NODE_MATH_LOGARITHM:
+      return dispatch([](float a, float b) { return safe_logf(a, b); });
+    case NODE_MATH_MINIMUM:
+      return dispatch([](float a, float b) { return std::min(a, b); });
+    case NODE_MATH_MAXIMUM:
+      return dispatch([](float a, float b) { return std::max(a, b); });
+    case NODE_MATH_LESS_THAN:
+      return dispatch([](float a, float b) { return (float)(a < b); });
+    case NODE_MATH_GREATER_THAN:
+      return dispatch([](float a, float b) { return (float)(a > b); });
+    case NODE_MATH_MODULO:
+      return dispatch([](float a, float b) { return safe_modf(a, b); });
+    case NODE_MATH_SNAP:
+      return dispatch([](float a, float b) { return floorf(safe_divide(a, b)) * b; });
+    case NODE_MATH_ARCTAN2:
+      return dispatch([](float a, float b) { return atan2f(a, b); });
+    case NODE_MATH_PINGPONG:
+      return dispatch([](float a, float b) { return pingpongf(a, b); });
+  }
+  return false;
+}
+
+/**
+ * This is similar to try_dispatch_float_math_fl_to_fl, just with a different callback signature.
+ */
+template<typename Callback>
+inline bool try_dispatch_float_math_fl_fl_fl_to_fl(const int operation, Callback &&callback)
+{
+  const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
+  if (info == nullptr) {
+    return false;
+  }
+
+  /* This is just an utility function to keep the individual cases smaller. */
+  auto dispatch = [&](auto math_function) -> bool {
+    callback(math_function, *info);
+    return true;
+  };
+
+  switch (operation) {
+    case NODE_MATH_MULTIPLY_ADD:
+      return dispatch([](float a, float b, float c) { return a * b + c; });
+    case NODE_MATH_COMPARE:
+      return dispatch([](float a, float b, float c) -> float {
+        return ((a == b) || (fabsf(a - b) <= fmaxf(c, FLT_EPSILON))) ? 1.0f : 0.0f;
+      });
+    case NODE_MATH_SMOOTH_MIN:
+      return dispatch([](float a, float b, float c) { return smoothminf(a, b, c); });
+    case NODE_MATH_SMOOTH_MAX:
+      return dispatch([](float a, float b, float c) { return -smoothminf(-a, -b, -c); });
+    case NODE_MATH_WRAP:
+      return dispatch([](float a, float b, float c) { return wrapf(a, b, c); });
+  }
+  return false;
+}
+
+}  // namespace blender::nodes
diff --git a/source/blender/nodes/intern/math_functions.cc b/source/blender/nodes/intern/math_functions.cc
new file mode 100644
index 00000000000..cc5e9547a96
--- /dev/null
+++ b/source/blender/nodes/intern/math_functions.cc
@@ -0,0 +1,117 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "NOD_math_functions.hh"
+
+namespace blender::nodes {
+
+const FloatMathOperationInfo *get_float_math_operation_info(const int operation)
+{
+
+#define RETURN_OPERATION_INFO(title_case_name, shader_name) \
+  { \
+    static const FloatMathOperationInfo info{title_case_name, shader_name}; \
+    return &info; \
+  } \
+  ((void)0)
+
+  switch (operation) {
+    case NODE_MATH_ADD:
+      RETURN_OPERATION_INFO("Add", "math_add");
+    case NODE_MATH_SUBTRACT:
+      RETURN_OPERATION_INFO("Subtract", 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list