[Bf-blender-cvs] [6b7026dd044] geometry-nodes-deduplicate-float-math: start deduplicating float math

Jacques Lucke noreply at git.blender.org
Tue Nov 24 17:17:47 CET 2020


Commit: 6b7026dd0441aaab807d7b0268f354699765e2ba
Author: Jacques Lucke
Date:   Tue Nov 24 15:41:23 2020 +0100
Branches: geometry-nodes-deduplicate-float-math
https://developer.blender.org/rB6b7026dd0441aaab807d7b0268f354699765e2ba

start deduplicating float math

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

M	source/blender/nodes/CMakeLists.txt
A	source/blender/nodes/NOD_math_functions.hh
A	source/blender/nodes/intern/math_functions.cc
M	source/blender/nodes/shader/nodes/node_shader_math.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..7dbd8f598e4
--- /dev/null
+++ b/source/blender/nodes/NOD_math_functions.hh
@@ -0,0 +1,108 @@
+/*
+ * 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_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);
+
+template<typename OpType>
+inline bool dispatch_float_math_fl_fl_to_fl(const int operation, OpType &&op)
+{
+  const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
+  if (info == nullptr) {
+    return false;
+  }
+
+  switch (operation) {
+    case NODE_MATH_ADD: {
+      op([](float a, float b) { return a + b; }, *info);
+      return true;
+    }
+    case NODE_MATH_SUBTRACT: {
+      op([](float a, float b) { return a - b; }, *info);
+      return true;
+    }
+    case NODE_MATH_MULTIPLY: {
+      op([](float a, float b) { return a * b; }, *info);
+      return true;
+    }
+    case NODE_MATH_DIVIDE: {
+      op(safe_divide, *info);
+      return true;
+    }
+    case NODE_MATH_POWER: {
+      op(safe_powf, *info);
+      return true;
+    }
+    case NODE_MATH_LOGARITHM: {
+      op(safe_logf, *info);
+      return true;
+    }
+    case NODE_MATH_MINIMUM: {
+      op([](float a, float b) { return std::min(a, b); }, *info);
+      return true;
+    }
+    case NODE_MATH_MAXIMUM: {
+      op([](float a, float b) { return std::max(a, b); }, *info);
+      return true;
+    }
+    case NODE_MATH_LESS_THAN: {
+      op([](float a, float b) { return (float)(a < b); }, *info);
+      return true;
+    }
+    case NODE_MATH_GREATER_THAN: {
+      op([](float a, float b) { return (float)(a > b); }, *info);
+      return true;
+    }
+    case NODE_MATH_MODULO: {
+      op(safe_modf, *info);
+      return true;
+    }
+    case NODE_MATH_TRUNC: {
+      op([](float a, float b) { return a >= 0.0f ? floorf(a) : ceilf(a); }, *info);
+      return true;
+    }
+    case NODE_MATH_SNAP: {
+      op([](float a, float b) { return floorf(safe_divide(a, b)) * b; }, *info);
+      return true;
+    }
+    case NODE_MATH_ARCTAN2: {
+      op([](float a, float b) { return atan2f(a, b); }, *info);
+      return true;
+    }
+  }
+  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..ad2c51785cf
--- /dev/null
+++ b/source/blender/nodes/intern/math_functions.cc
@@ -0,0 +1,52 @@
+/*
+ * 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)
+{
+  switch (operation) {
+    case NODE_MATH_ADD: {
+      static const FloatMathOperationInfo info{"Add", "math_add"};
+      return &info;
+    }
+    case NODE_MATH_SUBTRACT: {
+      static const FloatMathOperationInfo info{"Subtract", "math_subtract"};
+      return &info;
+    }
+    case NODE_MATH_MULTIPLY: {
+      static const FloatMathOperationInfo info{"Multiply", "math_multiply"};
+      return &info;
+    }
+    case NODE_MATH_DIVIDE: {
+      static const FloatMathOperationInfo info{"Divide", "math_divide"};
+      return &info;
+    }
+    case NODE_MATH_MULTIPLY_ADD: {
+      static const FloatMathOperationInfo info{"Multiply Add", "math_multiply_add"};
+      return &info;
+    }
+    case NODE_MATH_POWER: {
+      static const FloatMathOperationInfo info{"Power", "math_power"};
+      return &info;
+    }
+  }
+  return nullptr;
+}
+
+}  // namespace blender::nodes
diff --git a/source/blender/nodes/shader/nodes/node_shader_math.cc b/source/blender/nodes/shader/nodes/node_shader_math.cc
index e7bbadfbcb0..7f0eb928bf1 100644
--- a/source/blender/nodes/shader/nodes/node_shader_math.cc
+++ b/source/blender/nodes/shader/nodes/node_shader_math.cc
@@ -23,6 +23,8 @@
 
 #include "node_shader_util.h"
 
+#include "NOD_math_functions.hh"
+
 /* **************** SCALAR MATH ******************** */
 static bNodeSocketTemplate sh_node_math_in[] = {
     {SOCK_FLOAT, N_("Value"), 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f, PROP_NONE},
@@ -34,93 +36,15 @@ static bNodeSocketTemplate sh_node_math_out[] = {{SOCK_FLOAT, N_("Value")}, {-1,
 
 static const char *gpu_shader_get_name(int mode)
 {
-  switch (mode) {
-    case NODE_MATH_ADD:
-      return "math_add";
-    case NODE_MATH_SUBTRACT:
-      return "math_subtract";
-    case NODE_MATH_MULTIPLY:
-      return "math_multiply";
-    case NODE_MATH_DIVIDE:
-      return "math_divide";
-    case NODE_MATH_MULTIPLY_ADD:
-      return "math_multiply_add";
-
-    case NODE_MATH_POWER:
-      return "math_power";
-    case NODE_MATH_LOGARITHM:
-      return "math_logarithm";
-    case NODE_MATH_EXPONENT:
-      return "math_exponent";
-    case NODE_MATH_SQRT:
-      return "math_sqrt";
-    case NODE_MATH_INV_SQRT:
-      return "math_inversesqrt";
-    case NODE_MATH_ABSOLUTE:
-      return "math_absolute";
-    case NODE_MATH_RADIANS:
-      return "math_radians";
-    case NODE_MATH_DEGREES:
-      return "math_degrees";
-
-    case NODE_MATH_MINIMUM:
-      return "math_minimum";
-    case NODE_MATH_MAXIMUM:
-      return "math_maximum";
-    case NODE_MATH_LESS_THAN:
-      return "math_less_than";
-    case NODE_MATH_GREATER_THAN:
-      return "math_greater_than";
-    case NODE_MATH_SIGN:
-      return "math_sign";
-    case NODE_MATH_COMPARE:
-      return "math_compare";
-    case NODE_MATH_SMOOTH_MIN:
-      return "math_smoothmin";
-    case NODE_MATH_SMOOTH_MAX:
-      return "math_smoothmax";
-
-    case NODE_MATH_ROUND:
-      return "math_round";
-    case NODE_MATH_FLOOR:
-      return "math_floor";
-    case NODE_MATH_CEIL:
-      return "math_ceil";
-    case NODE_MATH_FRACTION:
-      return "math_fraction";
-    case NODE_MATH_MODULO:
-      return "math_modulo";
-    case NODE_MATH_TRUNC:
-      return "math_trunc";
-    case NODE_MATH_SNAP:
-      return "math_snap";
-    case NODE_MATH_WRAP:
-      return "math_wrap";
-    case NODE_MATH_PINGPONG:
-      return "math_pingpong";
-
-    case NODE_MATH_SINE:
-      return "math_sine";
-    case NODE_MATH_COSINE:
-      return "math_cosine";
-    case NODE_MATH_TANGENT:
-      return "math_tangent";
-    case NODE_MATH_SINH:
-      return "math_sinh";
-    case NODE_MATH_COSH:
-      return "math_cosh";
-    case NODE_MATH_TANH:
-      return "math_tanh";
-    case NODE_MATH_ARCSINE:
-      return "math_arcsine";
-    case NODE_MATH_ARCCOSINE:
-      return "math_arccosine";
-    case NODE_MATH_ARCTANGENT:
-      return "math_arctangent";
-    case NODE_MATH_ARCTAN2:
-      return "math_arctan2";
+  const blender::nodes::FloatMathOperationInfo *info =
+      blender::nodes::get_float_math_operation_info(mode);
+  if (!info) {
+    return nullptr;
+  }
+  if (info->shader_name.is_empty()) {
+    return nullptr;
   }
-  return nullptr;
+  return info->shader_name.c_str();
 }
 
 static int gpu_shader_math(GPUMaterial *mat,
@@ -149,6 +73,18 @@ static const blender::fn::MultiFunction &get_base_multi_function(
     blender::nodes::NodeMFNetworkBuilder &builder)
 {
   const int mode = builder.bnode().custom1;
+
+  const blender::fn::MultiFunction *base_fn = nullptr;
+  blender::nodes::dispatch_float_math_fl_fl_to_fl(
+      mode, [&](auto function, const blender::nodes::FloatMathOperationInfo &info) {
+        static blender::fn::CustomMF_SI_SI_SO<float, float, float> fn{info.title_case_name,
+                                                                      function};
+        base_fn = &fn;
+      });
+  if (base_fn != nullptr) {
+    return *base_fn;
+  }
+
   switch (mode) {
     case NODE_MATH_ADD: {
       static blender::fn::CustomMF_SI_SI_SO<float, float, float> fn{



More information about the Bf-blender-cvs mailing list