[Bf-blender-cvs] [687f9942510] geometry-nodes: Geometry Nodes: improve api for nodes

Jacques Lucke noreply at git.blender.org
Wed Oct 28 14:05:20 CET 2020


Commit: 687f9942510cb74ae7b7099a7460a5d68ff854c2
Author: Jacques Lucke
Date:   Wed Oct 28 14:05:07 2020 +0100
Branches: geometry-nodes
https://developer.blender.org/rB687f9942510cb74ae7b7099a7460a5d68ff854c2

Geometry Nodes: improve api for nodes

The execute callback of a geometry node gets more domain specific
types as parameters now: GeoNodeInputs and GeoNodeOutputs.

Those types are also aware of what node is being executed and can
provide better error messages when they are used incorrectly.

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.c
M	source/blender/functions/CMakeLists.txt
A	source/blender/functions/FN_generic_value_map.hh
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/modifiers/intern/MOD_nodes.h
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/geometry/nodes/node_geo_boolean.cc
M	source/blender/nodes/geometry/nodes/node_geo_edge_split.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_instance.cc
M	source/blender/nodes/geometry/nodes/node_geo_transform.cc
M	source/blender/nodes/geometry/nodes/node_geo_triangulate.cc
A	source/blender/nodes/intern/node_geometry_exec.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 7acf102b40a..857d73cee46 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -112,7 +112,8 @@ namespace blender {
 namespace nodes {
 class SocketMFNetworkBuilder;
 class NodeMFNetworkBuilder;
-class GValueByName;
+class GeoNodeInputs;
+class GeoNodeOutputs;
 }  // namespace nodes
 namespace fn {
 class CPPType;
@@ -122,8 +123,8 @@ class MFDataType;
 
 using NodeExpandInMFNetworkFunction = void (*)(blender::nodes::NodeMFNetworkBuilder &builder);
 using NodeGeometryExecFunction = void (*)(struct bNode *node,
-                                          blender::nodes::GValueByName &inputs,
-                                          blender::nodes::GValueByName &outputs);
+                                          blender::nodes::GeoNodeInputs inputs,
+                                          blender::nodes::GeoNodeOutputs outputs);
 using SocketGetCPPTypeFunction = const blender::fn::CPPType *(*)();
 using SocketGetCPPValueFunction = void (*)(const struct bNodeSocket &socket, void *r_value);
 using SocketExpandInMFNetworkFunction = void (*)(blender::nodes::SocketMFNetworkBuilder &builder);
diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c
index dec474090c2..e4fa50cb693 100644
--- a/source/blender/blenkernel/intern/node.c
+++ b/source/blender/blenkernel/intern/node.c
@@ -3989,7 +3989,7 @@ void ntreeUpdateAllUsers(Main *main, bNodeTree *ngroup)
         if (md->type == eModifierType_Nodes) {
           NodesModifierData *nmd = (NodesModifierData *)md;
           if (nmd->node_group == ngroup) {
-            MOD_nodes_update_interface(nmd);
+            MOD_nodes_update_interface(object, nmd);
           }
         }
       }
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index e005753e228..429959f9c33 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -39,6 +39,7 @@ set(SRC
   FN_attributes_ref.hh
   FN_cpp_type.hh
   FN_generic_pointer.hh
+  FN_generic_value_map.hh
   FN_generic_vector_array.hh
   FN_multi_function.hh
   FN_multi_function_builder.hh
diff --git a/source/blender/functions/FN_generic_value_map.hh b/source/blender/functions/FN_generic_value_map.hh
new file mode 100644
index 00000000000..952be87e5da
--- /dev/null
+++ b/source/blender/functions/FN_generic_value_map.hh
@@ -0,0 +1,113 @@
+/*
+ * 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 "BLI_linear_allocator.hh"
+#include "BLI_map.hh"
+
+#include "FN_generic_pointer.hh"
+
+namespace blender::fn {
+
+/**
+ * This is a map that stores key-value-pairs. What makes it special is that the type of values does
+ * not have to be known at compile time. There just has to be a corresponding CPPType.
+ */
+template<typename Key> class GValueMap {
+ private:
+  /* Used to allocate values owned by this container. */
+  LinearAllocator<> &allocator_;
+  Map<Key, GMutablePointer> values_;
+
+ public:
+  GValueMap(LinearAllocator<> &allocator) : allocator_(allocator)
+  {
+  }
+
+  ~GValueMap()
+  {
+    /* Destruct all values that are still in the map. */
+    for (GMutablePointer value : values_.values()) {
+      value.destruct();
+    }
+  }
+
+  /* Add a value to the container. The container becomes responsible for destructing the value that
+   * is passed in. The caller remains responsible for freeing the value after it has been
+   * destructed. */
+  template<typename ForwardKey> void add_new_direct(ForwardKey &&key, GMutablePointer value)
+  {
+    values_.add_new_as(std::forward<ForwardKey>(key), value);
+  }
+
+  /* Add a value to the container that is move constructed from the given value. The caller remains
+   * responsible for destructing and freeing the given value. */
+  template<typename ForwardKey> void add_new_by_move(ForwardKey &&key, GMutablePointer value)
+  {
+    const CPPType &type = *value.type();
+    void *buffer = allocator_.allocate(type.size(), type.alignment());
+    type.move_to_uninitialized(value.get(), buffer);
+    values_.add_new_as(std::forward<ForwardKey>(key), GMutablePointer{type, buffer});
+  }
+
+  /* Add a value to the container that is copy constructed from the given value. The caller remains
+   * responsible for destructing and freeing the given value. */
+  template<typename ForwardKey> void add_new_by_copy(ForwardKey &&key, GMutablePointer value)
+  {
+    const CPPType &type = *value.type();
+    void *buffer = allocator_.allocate(type.size(), type.alignment());
+    type.copy_to_uninitialized(value.get(), buffer);
+    values_.add_new_as(std::forward<ForwardKey>(key), GMutablePointer{type, buffer});
+  }
+
+  /* Add a value to the container. */
+  template<typename ForwardKey, typename T> void add_new(ForwardKey &&key, T &&value)
+  {
+    if constexpr (std::is_rvalue_reference_v<T>) {
+      this->add_new_by_move(std::forward<ForwardKey>(key), &value);
+    }
+    else {
+      this->add_new_by_copy(std::forward<ForwardKey>(key), &value);
+    }
+  }
+
+  /* Remove the value for the given name from the container and remove it. The caller is
+   * responsible for freeing it. The lifetime of the referenced memory might be bound to lifetime
+   * of the container. */
+  template<typename ForwardKey> GMutablePointer extract(const ForwardKey &key)
+  {
+    return values_.pop_as(key);
+  }
+
+  /* Remove the value for the given name from the container and remove it. */
+  template<typename T, typename ForwardKey> T extract(const ForwardKey &key)
+  {
+    GMutablePointer value = values_.pop_as(key);
+    const CPPType &type = *value.type();
+    BLI_assert(type.is<T>());
+    T return_value;
+    type.relocate_to_initialized(value.get(), &return_value);
+    return return_value;
+  }
+
+  template<typename ForwardKey> bool contains(const ForwardKey &key) const
+  {
+    return values_.contains_as(key);
+  }
+};
+
+}  // namespace blender::fn
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index 5e82ebce528..d9463f778be 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -1601,9 +1601,10 @@ static bool rna_NodesModifier_node_group_poll(PointerRNA *ptr, PointerRNA value)
 
 static void rna_NodesModifier_node_group_update(Main *bmain, Scene *scene, PointerRNA *ptr)
 {
+  Object *object = (Object *)ptr->owner_id;
   NodesModifierData *nmd = ptr->data;
   rna_Modifier_dependency_update(bmain, scene, ptr);
-  MOD_nodes_update_interface(nmd);
+  MOD_nodes_update_interface(object, nmd);
 }
 
 static IDProperty *rna_NodesModifierSettings_properties(PointerRNA *ptr, bool create)
diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc
index fa14c504472..e3a686e50d7 100644
--- a/source/blender/modifiers/intern/MOD_nodes.cc
+++ b/source/blender/modifiers/intern/MOD_nodes.cc
@@ -212,30 +212,33 @@ class GeometryNodesEvaluator {
   void compute_output_and_forward(const DOutputSocket &socket_to_compute)
   {
     const DNode &node = socket_to_compute.node();
+    const bNode &bnode = *node.bnode();
 
     /* Prepare inputs required to execute the node. */
-    GValueByName node_inputs{allocator_};
+    GValueMap<StringRef> node_inputs_map{allocator_};
     for (const DInputSocket *input_socket : node.inputs()) {
       if (input_socket->is_available()) {
         GMutablePointer value = this->get_input_value(*input_socket);
-        node_inputs.transfer_ownership_in(input_socket->identifier(), value);
+        node_inputs_map.add_new_direct(input_socket->identifier(), value);
       }
     }
 
     /* Execute the node. */
-    GValueByName node_outputs{allocator_};
+    GValueMap<StringRef> node_outputs_map{allocator_};
+    GeoNodeInputs node_inputs{bnode, node_inputs_map};
+    GeoNodeOutputs node_outputs{bnode, node_outputs_map};
     this->execute_node(node, node_inputs, node_outputs);
 
     /* Forward computed outputs to linked input sockets. */
     for (const DOutputSocket *output_socket : node.outputs()) {
       if (output_socket->is_available()) {
-        GMutablePointer value = node_outputs.extract(output_socket->identifier());
+        GMutablePointer value = node_outputs_map.extract(output_socket->identifier());
         this->forward_to_inputs(*output_socket, value);
       }
     }
   }
 
-  void execute_node(const DNode &node, GValueByName &node_inputs, GValueByName &node_outputs)
+  void execute_node(const DNode &node, GeoNodeInputs node_inputs, GeoNodeOutputs node_outputs)
   {
     bNode *bnode = node.bnode();
     if (bnode->typeinfo->geometry_node_execute != nullptr) {
@@ -270,7 +273,7 @@ class GeometryNodesEvaluator {
     }
     for (const int i : node.outputs().index_range()) {
       GMutablePointer value = output_data[i];
-      node_outputs.move_in(node.output(i).identifier(), value);
+      node_outputs.set_by_move(node.output(i).identifier(), value);
       value.destruct();
     }
   }
@@ -400,7 +403,7 @@ static const SocketPropertyType *get_socket_property_type(const bNodeSocket &bso
   }
 }
 
-void MOD_nodes_update_interface(NodesModifierData *nmd)
+void MOD_nodes_update_interface(Object *object, NodesModifierData *nmd)
 {
   if (nmd->node_group == nullptr) {
     return;
@@ -428,6 +431,8 @@ void MOD_nodes_update_interface(NodesModifierData *nmd)
       IDP_AddToGroup(nmd->settings.propertie

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list