[Bf-blender-cvs] [7d5ee7c6897] functions: start using virtual node tree to generate functions

Jacques Lucke noreply at git.blender.org
Mon Jul 22 18:13:10 CEST 2019


Commit: 7d5ee7c6897615128b2b2bdc650295d66fb09e9f
Author: Jacques Lucke
Date:   Mon Jul 22 15:49:13 2019 +0200
Branches: functions
https://developer.blender.org/rB7d5ee7c6897615128b2b2bdc650295d66fb09e9f

start using virtual node tree to generate functions

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

M	source/blender/blenkernel/BKE_node_tree.hpp
M	source/blender/blenkernel/intern/node_tree.cpp
M	source/blender/functions/frontends/data_flow_nodes/builder.cpp
M	source/blender/functions/frontends/data_flow_nodes/builder.hpp
M	source/blender/functions/frontends/data_flow_nodes/function_generation.cpp
M	source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
M	source/blender/functions/frontends/data_flow_nodes/graph_generation.hpp
M	source/blender/functions/frontends/data_flow_nodes/inserters.cpp
M	source/blender/functions/frontends/data_flow_nodes/inserters.hpp
M	source/blender/functions/frontends/data_flow_nodes/inserters/conversions.cpp
M	source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
M	source/blender/simulations/bparticles/inserters.cpp
M	source/blender/simulations/bparticles/inserters.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp

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

diff --git a/source/blender/blenkernel/BKE_node_tree.hpp b/source/blender/blenkernel/BKE_node_tree.hpp
index 69e0e8958e9..c4d0abd6ede 100644
--- a/source/blender/blenkernel/BKE_node_tree.hpp
+++ b/source/blender/blenkernel/BKE_node_tree.hpp
@@ -131,8 +131,11 @@ class VirtualNodeTree {
   MonotonicAllocator<> m_allocator;
   SmallVector<VirtualNode *> m_nodes;
   SmallVector<VirtualLink *> m_links;
+  SmallVector<VirtualSocket *> m_inputs_with_links;
+  SmallMultiMap<std::string, VirtualNode *> m_nodes_by_idname;
 
  public:
+  void add_all_of_tree(bNodeTree *btree);
   VirtualNode *add_bnode(bNodeTree *btree, bNode *bnode);
   void add_link(VirtualSocket *a, VirtualSocket *b);
 
@@ -148,6 +151,18 @@ class VirtualNodeTree {
     return m_links;
   }
 
+  ArrayRef<VirtualSocket *> inputs_with_links()
+  {
+    BLI_assert(m_frozen);
+    return m_inputs_with_links;
+  }
+
+  ArrayRef<VirtualNode *> nodes_with_idname(StringRef idname)
+  {
+    BLI_assert(m_frozen);
+    return m_nodes_by_idname.lookup_default(idname.to_std_string());
+  }
+
   bool is_frozen()
   {
     return m_frozen;
@@ -156,6 +171,7 @@ class VirtualNodeTree {
  private:
   void initialize_direct_links();
   void initialize_links();
+  void initialize_nodes_by_idname();
 };
 
 class VirtualNode {
@@ -194,6 +210,16 @@ class VirtualNode {
   {
     return m_bnode;
   }
+
+  bNodeTree *btree()
+  {
+    return m_btree;
+  }
+
+  ID *btree_id()
+  {
+    return &m_btree->id;
+  }
 };
 
 class VirtualSocket {
@@ -228,6 +254,11 @@ class VirtualSocket {
     return m_btree;
   }
 
+  ID *btree_id()
+  {
+    return &m_btree->id;
+  }
+
   VirtualNode *vnode()
   {
     return m_vnode;
diff --git a/source/blender/blenkernel/intern/node_tree.cpp b/source/blender/blenkernel/intern/node_tree.cpp
index ee36b754649..f643431912f 100644
--- a/source/blender/blenkernel/intern/node_tree.cpp
+++ b/source/blender/blenkernel/intern/node_tree.cpp
@@ -104,6 +104,39 @@ ArrayRef<SingleOriginLink> IndexedNodeTree::single_origin_links() const
 /* Virtual Node Tree
  *****************************************/
 
+void VirtualNodeTree::add_all_of_tree(bNodeTree *btree)
+{
+  SmallMap<bNode *, VirtualNode *> node_mapping;
+  for (bNode *bnode : BKE::bNodeList(btree->nodes)) {
+    VirtualNode *vnode = this->add_bnode(btree, bnode);
+    node_mapping.add_new(bnode, vnode);
+  }
+  for (bNodeLink *blink : BKE::bLinkList(btree->links)) {
+    VirtualNode *from_node = node_mapping.lookup(blink->fromnode);
+    VirtualNode *to_node = node_mapping.lookup(blink->tonode);
+    VirtualSocket *from_vsocket = nullptr;
+    VirtualSocket *to_vsocket = nullptr;
+
+    for (VirtualSocket &output : from_node->outputs()) {
+      if (output.bsocket() == blink->fromsock) {
+        from_vsocket = &output;
+        break;
+      }
+    }
+
+    for (VirtualSocket &input : to_node->inputs()) {
+      if (input.bsocket() == blink->tosock) {
+        to_vsocket = &input;
+        break;
+      }
+    }
+
+    BLI_assert(from_vsocket);
+    BLI_assert(to_vsocket);
+    this->add_link(from_vsocket, to_vsocket);
+  }
+}
+
 VirtualNode *VirtualNodeTree::add_bnode(bNodeTree *btree, bNode *bnode)
 {
   BLI_assert(!m_frozen);
@@ -114,7 +147,7 @@ VirtualNode *VirtualNodeTree::add_bnode(bNodeTree *btree, bNode *bnode)
   vnode->m_btree = btree;
 
   SmallVector<bNodeSocket *, 10> original_inputs(bnode->inputs, true);
-  SmallVector<bNodeSocket *, 10> original_outputs(bnode->inputs, true);
+  SmallVector<bNodeSocket *, 10> original_outputs(bnode->outputs, true);
 
   vnode->m_inputs = m_allocator.allocate_array<VirtualSocket>(original_inputs.size());
   vnode->m_outputs = m_allocator.allocate_array<VirtualSocket>(original_outputs.size());
@@ -145,13 +178,13 @@ void VirtualNodeTree::add_link(VirtualSocket *a, VirtualSocket *b)
   VirtualLink *vlink = m_allocator.allocate<VirtualLink>();
   if (a->is_input()) {
     BLI_assert(b->is_output());
-    vlink->m_from = a;
-    vlink->m_to = b;
+    vlink->m_from = b;
+    vlink->m_to = a;
   }
   else {
     BLI_assert(b->is_input());
-    vlink->m_from = b;
-    vlink->m_to = a;
+    vlink->m_from = a;
+    vlink->m_to = b;
   }
 
   m_links.append(vlink);
@@ -162,6 +195,7 @@ void VirtualNodeTree::freeze_and_index()
   m_frozen = true;
   this->initialize_direct_links();
   this->initialize_links();
+  this->initialize_nodes_by_idname();
 }
 
 BLI_NOINLINE void VirtualNodeTree::initialize_direct_links()
@@ -230,8 +264,20 @@ BLI_NOINLINE void VirtualNodeTree::initialize_links()
       find_connected_sockets_left(vsocket, found);
       vsocket->m_links = m_allocator.allocate_array<VirtualSocket *>(found.size());
       vsocket->m_links.copy_from(found);
+
+      if (vsocket->m_links.size() > 0) {
+        m_inputs_with_links.append(vlink->m_to);
+      }
     }
   }
 }
 
+BLI_NOINLINE void VirtualNodeTree::initialize_nodes_by_idname()
+{
+  for (VirtualNode *vnode : m_nodes) {
+    bNode *bnode = vnode->bnode();
+    m_nodes_by_idname.add(bnode->idname, vnode);
+  }
+}
+
 }  // namespace BKE
diff --git a/source/blender/functions/frontends/data_flow_nodes/builder.cpp b/source/blender/functions/frontends/data_flow_nodes/builder.cpp
index 50962b7519f..07cd971418e 100644
--- a/source/blender/functions/frontends/data_flow_nodes/builder.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/builder.cpp
@@ -28,11 +28,11 @@ static PyObject *get_py_bnode(bNodeTree *btree, bNode *bnode)
 namespace FN {
 namespace DataFlowNodes {
 
-BTreeGraphBuilder::BTreeGraphBuilder(IndexedNodeTree &indexed_btree,
+BTreeGraphBuilder::BTreeGraphBuilder(VirtualNodeTree &vtree,
                                      DataFlowGraphBuilder &graph,
-                                     SmallMap<struct bNodeSocket *, DFGB_Socket> &socket_map)
+                                     SmallMap<VirtualSocket *, DFGB_Socket> &socket_map)
     : m_graph(graph),
-      m_indexed_btree(indexed_btree),
+      m_vtree(vtree),
       m_socket_map(socket_map),
       m_type_by_idname(get_type_by_idname_map()),
       m_type_by_data_type(get_type_by_data_type_map())
@@ -45,7 +45,7 @@ class NodeSource : public SourceInfo {
   bNode *m_bnode;
 
  public:
-  NodeSource(bNodeTree *btree, bNode *bnode) : m_btree(btree), m_bnode(bnode)
+  NodeSource(bNodeTree *btree, bNode *vnode) : m_btree(btree), m_bnode(vnode)
   {
   }
 
@@ -76,48 +76,22 @@ class NodeSource : public SourceInfo {
   }
 };
 
-class LinkSource : public SourceInfo {
- private:
-  bNodeTree *m_btree;
-  bNodeLink *m_blink;
-
- public:
-  LinkSource(bNodeTree *btree, bNodeLink *blink) : m_btree(btree), m_blink(blink)
-  {
-  }
-
-  std::string to_string() const override
-  {
-    std::stringstream ss;
-    ss << "NodeTree \"" << m_btree->id.name + 2 << "\"";
-    ss << " - Link";
-    return ss.str();
-  }
-};
-
 DFGB_Node *BTreeGraphBuilder::insert_function(SharedFunction &fn)
 {
   return m_graph.insert_function(fn);
 }
 
-DFGB_Node *BTreeGraphBuilder::insert_matching_function(SharedFunction &fn, bNode *bnode)
+DFGB_Node *BTreeGraphBuilder::insert_matching_function(SharedFunction &fn, VirtualNode *vnode)
 {
-  DFGB_Node *node = this->insert_function(fn, bnode);
-  this->map_sockets(node, bnode);
+  DFGB_Node *node = this->insert_function(fn, vnode);
+  this->map_sockets(node, vnode);
   return node;
 }
 
-DFGB_Node *BTreeGraphBuilder::insert_function(SharedFunction &fn, bNode *bnode)
-{
-  BLI_assert(bnode != nullptr);
-  NodeSource *source = m_graph.new_source_info<NodeSource>(m_indexed_btree.btree(), bnode);
-  return m_graph.insert_function(fn, source);
-}
-
-DFGB_Node *BTreeGraphBuilder::insert_function(SharedFunction &fn, bNodeLink *blink)
+DFGB_Node *BTreeGraphBuilder::insert_function(SharedFunction &fn, VirtualNode *vnode)
 {
-  BLI_assert(blink != nullptr);
-  LinkSource *source = m_graph.new_source_info<LinkSource>(m_indexed_btree.btree(), blink);
+  BLI_assert(vnode != nullptr);
+  NodeSource *source = m_graph.new_source_info<NodeSource>(vnode->btree(), vnode->bnode());
   return m_graph.insert_function(fn, source);
 }
 
@@ -126,81 +100,81 @@ void BTreeGraphBuilder::insert_link(DFGB_Socket a, DFGB_Socket b)
   m_graph.insert_link(a, b);
 }
 
-void BTreeGraphBuilder::map_socket(DFGB_Socket socket, bNodeSocket *bsocket)
+void BTreeGraphBuilder::map_socket(DFGB_Socket socket, VirtualSocket *vsocket)
 {
-  BLI_assert(this->is_data_socket(bsocket) ? socket.type() == this->query_socket_type(bsocket) :
+  BLI_assert(this->is_data_socket(vsocket) ? socket.type() == this->query_socket_type(vsocket) :
                                              true);
-  m_socket_map.add(bsocket, socket);
+  m_socket_map.add(vsocket, socket);
 }
 
-void BTreeGraphBuilder::map_sockets(DFGB_Node *node, struct bNode *bnode)
+void BTreeGraphBuilder::map_sockets(DFGB_Node *node, VirtualNode *vnode)
 {
-  BLI_assert(BLI_listbase_count(&bnode->inputs) == node->input_amount());
-  BLI_assert(BLI_listbase_count(&bnode->outputs) == node->output_amount());
+  BLI_assert(vnode->inputs().size() == node->input_amount());
+  BLI_assert(vnode->outputs().size() == node->output_amount());
 
   uint input_index = 0;
-  for (bNodeSocket *bsocket : bSocketList(bnode->inputs)) {
-    this->map_socket(node->input(input_index), bsocket);
+  for (VirtualSocket &vsocket : vnode->inputs()) {
+    this->map_socket(node->input(input_index), &vsocket);
     input_index++;
   }
 
   uint output_index = 0;
-  for (bNodeSocket *bsocket : bSocketList(bnode->outputs)) {
-    this->map_socket(node->output(output_index), bsocket);
+  for (VirtualSocket &vsocket : vnode->outputs()) {
+    this->map_socket(node->output(output_index), &vsocket);
     output_index++;
   }
 }
 
-void BTreeGraphBuilder::map_data_sockets(DFGB_Node *node, struct bNode *bnode)
+void BTreeGraphBuilder::map_data_sockets(DFGB_Node *node, VirtualNode *vnode)
 {
   uint input_index = 0;
-  for (bNodeSocket *bsocket : bSocketList(bnode->inputs)) {
-    if (this->is_data_socket(bsocket)) {
-      this->map_socket(node->input(input_index), bsocket);
+  for (VirtualSocket &vsocket : vnode->inputs()) {
+    if (this->is_data_socket(&vsocket)) {
+      this->map_socket(node->input(input_index), &vsocket);
       input_index++;
     }
   }
 
   uint output_index =

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list