[Bf-blender-cvs] [b4d47523c2d] temp-enum-socket: evaluate enum node

Jacques Lucke noreply at git.blender.org
Mon Nov 8 16:07:04 CET 2021


Commit: b4d47523c2d003482518c4ce5c12273495e7eb5c
Author: Jacques Lucke
Date:   Mon Nov 8 15:53:24 2021 +0100
Branches: temp-enum-socket
https://developer.blender.org/rBb4d47523c2d003482518c4ce5c12273495e7eb5c

evaluate enum node

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

M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/function/nodes/node_fn_enum.cc
M	source/blender/nodes/intern/node_socket.cc

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

diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh
index 331cb9b1dd5..25a54866577 100644
--- a/source/blender/nodes/NOD_geometry_exec.hh
+++ b/source/blender/nodes/NOD_geometry_exec.hh
@@ -359,6 +359,22 @@ class GeoNodeExecParams {
 /** Wrapper for an integer so that one can not perform various operations like addition on it. */
 struct EnumValue {
   int value;
+
+  friend std::ostream &operator<<(std::ostream &stream, const EnumValue &value)
+  {
+    stream << value.value;
+    return stream;
+  }
+
+  friend bool operator==(const EnumValue &a, const EnumValue &b)
+  {
+    return a.value == b.value;
+  }
+
+  uint64_t hash() const
+  {
+    return this->value;
+  }
 };
 
 }  // namespace blender::nodes
diff --git a/source/blender/nodes/function/nodes/node_fn_enum.cc b/source/blender/nodes/function/nodes/node_fn_enum.cc
index 3edd2ed898a..928462c53d8 100644
--- a/source/blender/nodes/function/nodes/node_fn_enum.cc
+++ b/source/blender/nodes/function/nodes/node_fn_enum.cc
@@ -22,6 +22,8 @@
 #include "UI_interface.h"
 #include "UI_resources.h"
 
+#include "NOD_geometry_exec.hh"
+
 #include "node_function_util.hh"
 
 namespace blender::nodes {
@@ -96,15 +98,58 @@ static void fn_node_enum_layout(uiLayout *layout, bContext *UNUSED(C), PointerRN
   uiItemStringO(layout, "Add", ICON_PLUS, "node.enum_item_add", "node_name", node->name);
 }
 
-static const fn::MultiFunction *get_multi_function(bNode &UNUSED(bnode))
-{
-  return nullptr;
-}
+class EnumFunction : public fn::MultiFunction {
+ private:
+  Vector<int> enum_values_;
+  fn::MFSignature signature_;
+
+ public:
+  EnumFunction(const bNode &node)
+  {
+    fn::MFSignatureBuilder signature{"Enum Function"};
+    signature.single_input<EnumValue>("Enum");
+    signature.single_output<int>("Index");
+
+    const NodeFunctionEnum *storage = (NodeFunctionEnum *)node.storage;
+    LISTBASE_FOREACH (const NodeFunctionEnumItem *, item, &storage->items) {
+      signature.single_output<bool>(item->name);
+      enum_values_.append(item->value);
+    }
+
+    signature_ = signature.build();
+    this->set_signature(&signature_);
+  }
+
+  void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
+  {
+    const VArray<EnumValue> &enum_in = params.readonly_single_input<EnumValue>(0, "Enum");
+    MutableSpan<int> r_indices = params.uninitialized_single_output_if_required<int>(1, "Index");
+
+    if (!r_indices.is_empty()) {
+      for (const int i : mask) {
+        const int in_value = enum_in[i].value;
+        r_indices[i] = enum_values_.first_index_of_try(in_value);
+      }
+    }
+
+    for (const int enum_index : enum_values_.index_range()) {
+      MutableSpan<bool> r_bools = params.uninitialized_single_output_if_required<bool>(2 +
+                                                                                       enum_index);
+      if (r_bools.is_empty()) {
+        continue;
+      }
+      const int enum_value = enum_values_[enum_index];
+      for (const int i : mask) {
+        const int in_value = enum_in[i].value;
+        r_bools[i] = in_value == enum_value;
+      }
+    }
+  }
+};
 
 static void fn_node_enum_build_multi_function(NodeMultiFunctionBuilder &builder)
 {
-  const fn::MultiFunction *fn = get_multi_function(builder.node());
-  builder.set_matching_fn(fn);
+  builder.construct_and_set_matching_fn<EnumFunction>(builder.node());
 }
 
 static void fn_node_enum_copy_storage(bNodeTree *UNUSED(dest_ntree),
diff --git a/source/blender/nodes/intern/node_socket.cc b/source/blender/nodes/intern/node_socket.cc
index 7a79f2d708a..98c27cfb639 100644
--- a/source/blender/nodes/intern/node_socket.cc
+++ b/source/blender/nodes/intern/node_socket.cc
@@ -50,6 +50,7 @@
 
 #include "FN_cpp_type_make.hh"
 #include "FN_field.hh"
+#include "FN_field_cpp_type.hh"
 
 using namespace blender;
 using blender::nodes::SocketDeclarationPtr;
@@ -827,6 +828,7 @@ MAKE_CPP_TYPE(Texture, Tex *, CPPTypeFlags::BasicType)
 MAKE_CPP_TYPE(Image, Image *, CPPTypeFlags::BasicType)
 MAKE_CPP_TYPE(Material, Material *, CPPTypeFlags::BasicType)
 MAKE_CPP_TYPE(EnumValue, blender::nodes::EnumValue, CPPTypeFlags::None)
+MAKE_FIELD_CPP_TYPE(EnumValueField, blender::nodes::EnumValue);
 
 static bNodeSocketType *make_socket_type_object()
 {
@@ -902,16 +904,21 @@ static bNodeSocketType *make_socket_type_material()
 
 static bNodeSocketType *make_socket_type_enum()
 {
+  using blender::nodes::EnumValue;
   bNodeSocketType *socktype = make_standard_socket_type(SOCK_ENUM, PROP_NONE);
-  socktype->get_base_cpp_type = []() {
-    return &blender::fn::CPPType::get<blender::nodes::EnumValue>();
-  };
+  socktype->get_base_cpp_type = []() { return &blender::fn::CPPType::get<EnumValue>(); };
   socktype->get_base_cpp_value = [](const bNodeSocket &socket, void *r_value) {
-    ((blender::nodes::EnumValue *)r_value)->value =
-        ((bNodeSocketValueEnum *)socket.default_value)->value;
+    ((EnumValue *)r_value)->value = ((bNodeSocketValueEnum *)socket.default_value)->value;
+  };
+
+  socktype->get_geometry_nodes_cpp_type = []() {
+    return &blender::fn::CPPType::get<blender::fn::Field<EnumValue>>();
+  };
+  socktype->get_geometry_nodes_cpp_value = [](const bNodeSocket &socket, void *r_value) {
+    EnumValue value;
+    socket.typeinfo->get_base_cpp_value(socket, &value);
+    new (r_value) blender::fn::Field<EnumValue>(blender::fn::make_constant_field(value));
   };
-  socktype->get_geometry_nodes_cpp_type = socktype->get_base_cpp_type;
-  socktype->get_geometry_nodes_cpp_value = socktype->get_base_cpp_value;
   return socktype;
 }



More information about the Bf-blender-cvs mailing list