[Bf-blender-cvs] [d9348adb33f] functions: better error handling when creating function for emitter inputs

Jacques Lucke noreply at git.blender.org
Wed Jul 24 19:11:28 CEST 2019


Commit: d9348adb33f821c049bd0c1a935d26253d46cca1
Author: Jacques Lucke
Date:   Wed Jul 24 11:33:15 2019 +0200
Branches: functions
https://developer.blender.org/rBd9348adb33f821c049bd0c1a935d26253d46cca1

better error handling when creating function for emitter inputs

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

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/simulations/bparticles/inserters.cpp

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

diff --git a/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp b/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
index 6e98e2987bc..cbe995b66e9 100644
--- a/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/graph_generation.cpp
@@ -145,18 +145,23 @@ ValueOrError<VTreeDataGraph> generate_graph(VirtualNodeTree &vtree)
                         build_mapping_for_original_sockets(socket_map, build_result.mapping));
 }
 
-Vector<VirtualSocket *> VTreeDataGraph::find_placeholder_dependencies(
-    ArrayRef<VirtualSocket *> sockets)
+VTreeDataGraph::PlaceholderDependencies VTreeDataGraph::find_placeholder_dependencies(
+    ArrayRef<VirtualSocket *> vsockets)
 {
-  Stack<DFGraphSocket> to_be_checked;
-  Set<DFGraphSocket> found;
-  Vector<VirtualSocket *> dependencies;
-
-  for (VirtualSocket *vsocket : sockets) {
+  Vector<DFGraphSocket> sockets;
+  for (VirtualSocket *vsocket : vsockets) {
     DFGraphSocket socket = this->lookup_socket(vsocket);
-    to_be_checked.push(socket);
-    found.add_new(socket);
+    sockets.append(socket);
   }
+  return this->find_placeholder_dependencies(sockets);
+}
+
+VTreeDataGraph::PlaceholderDependencies VTreeDataGraph::find_placeholder_dependencies(
+    ArrayRef<DFGraphSocket> sockets)
+{
+  Stack<DFGraphSocket> to_be_checked = sockets;
+  Set<DFGraphSocket> found = sockets;
+  PlaceholderDependencies dependencies;
 
   while (!to_be_checked.empty()) {
     DFGraphSocket socket = to_be_checked.pop();
@@ -181,7 +186,8 @@ Vector<VirtualSocket *> VTreeDataGraph::find_placeholder_dependencies(
         VirtualNode *vnode = body->vnode();
         uint data_output_index = m_graph->index_of_output(socket);
         VirtualSocket *vsocket = this->find_data_output(vnode, data_output_index);
-        dependencies.append(vsocket);
+        dependencies.sockets.append(socket);
+        dependencies.vsockets.append(vsocket);
       }
     }
   }
diff --git a/source/blender/functions/frontends/data_flow_nodes/graph_generation.hpp b/source/blender/functions/frontends/data_flow_nodes/graph_generation.hpp
index 5411614081b..127cdd34a9f 100644
--- a/source/blender/functions/frontends/data_flow_nodes/graph_generation.hpp
+++ b/source/blender/functions/frontends/data_flow_nodes/graph_generation.hpp
@@ -37,6 +37,11 @@ class VTreeDataGraph {
     return m_graph;
   }
 
+  DFGraphSocket *lookup_socket_ptr(VirtualSocket *vsocket)
+  {
+    return m_mapping.lookup_ptr(vsocket);
+  }
+
   DFGraphSocket lookup_socket(VirtualSocket *vsocket)
   {
     return m_mapping.lookup(vsocket);
@@ -47,7 +52,19 @@ class VTreeDataGraph {
     return m_mapping.contains(vsocket);
   }
 
-  Vector<VirtualSocket *> find_placeholder_dependencies(ArrayRef<VirtualSocket *> sockets);
+  struct PlaceholderDependencies {
+    Vector<VirtualSocket *> vsockets;
+    Vector<DFGraphSocket> sockets;
+
+    uint size() const
+    {
+      BLI_assert(this->vsockets.size() == this->sockets.size());
+      return this->vsockets.size();
+    }
+  };
+
+  PlaceholderDependencies find_placeholder_dependencies(ArrayRef<VirtualSocket *> vsockets);
+  PlaceholderDependencies find_placeholder_dependencies(ArrayRef<DFGraphSocket> sockets);
 
  private:
   VirtualSocket *find_data_output(VirtualNode *vnode, uint index);
diff --git a/source/blender/simulations/bparticles/inserters.cpp b/source/blender/simulations/bparticles/inserters.cpp
index 7aeb64bc687..74a529dc4ef 100644
--- a/source/blender/simulations/bparticles/inserters.cpp
+++ b/source/blender/simulations/bparticles/inserters.cpp
@@ -20,6 +20,8 @@ namespace BParticles {
 
 using BLI::ValueOrError;
 using FN::DFGraphSocket;
+using FN::FunctionBuilder;
+using FN::FunctionGraph;
 using FN::SharedFunction;
 using FN::SharedType;
 
@@ -27,17 +29,15 @@ static Vector<DFGraphSocket> insert_function_inputs(FN::FunctionBuilder &fn_buil
                                                     VTreeDataGraph &data_graph,
                                                     ArrayRef<VirtualSocket *> output_vsockets)
 {
-  Vector<VirtualSocket *> placeholder_dependencies = data_graph.find_placeholder_dependencies(
-      output_vsockets);
-
-  Vector<DFGraphSocket> function_inputs;
+  auto dependencies = data_graph.find_placeholder_dependencies(output_vsockets);
 
   auto &graph = data_graph.graph();
 
-  for (VirtualSocket *vsocket : placeholder_dependencies) {
-    BLI_assert(vsocket->is_output());
+  for (uint i = 0; i < dependencies.size(); i++) {
+    VirtualSocket *vsocket = dependencies.vsockets[i];
+    DFGraphSocket socket = dependencies.sockets[i];
     VirtualNode *vnode = vsocket->vnode();
-    DFGraphSocket socket = data_graph.lookup_socket(vsocket);
+    BLI_assert(vsocket->is_output());
 
     SharedType &type = graph->type_of_output(socket);
     std::string name_prefix;
@@ -51,10 +51,9 @@ static Vector<DFGraphSocket> insert_function_inputs(FN::FunctionBuilder &fn_buil
       BLI_assert(false);
     }
     fn_builder.add_input(name_prefix + vsocket->name(), type);
-    function_inputs.append(socket);
   }
 
-  return function_inputs;
+  return dependencies.sockets;
 }
 
 static SharedFunction create_function(VTreeDataGraph &data_graph,
@@ -78,6 +77,19 @@ static SharedFunction create_function(VTreeDataGraph &data_graph,
   return fn;
 }
 
+static Vector<DFGraphSocket> find_input_data_sockets(VirtualNode *vnode,
+                                                     VTreeDataGraph &data_graph)
+{
+  Vector<DFGraphSocket> inputs;
+  for (VirtualSocket *vsocket : vnode->inputs()) {
+    DFGraphSocket *socket = data_graph.lookup_socket_ptr(vsocket);
+    if (socket != nullptr) {
+      inputs.append(*socket);
+    }
+  }
+  return inputs;
+}
+
 static SharedFunction create_function_for_data_inputs(VirtualNode *vnode,
                                                       VTreeDataGraph &data_graph)
 {
@@ -90,6 +102,22 @@ static SharedFunction create_function_for_data_inputs(VirtualNode *vnode,
   return create_function(data_graph, bsockets_to_compute, vnode->name());
 }
 
+static ValueOrError<SharedFunction> create_function__emitter_inputs(VirtualNode *emitter_vnode,
+                                                                    VTreeDataGraph &data_graph)
+{
+  Vector<DFGraphSocket> sockets_to_compute = find_input_data_sockets(emitter_vnode, data_graph);
+  auto dependencies = data_graph.find_placeholder_dependencies(sockets_to_compute);
+
+  if (dependencies.size() > 0) {
+    return BLI_ERROR_CREATE("Emitter inputs cannot have dependencies currently.");
+  }
+
+  FunctionGraph fgraph(data_graph.graph(), {}, sockets_to_compute);
+  SharedFunction fn = fgraph.new_function(emitter_vnode->name());
+  FN::fgraph_add_TupleCallBody(fn, fgraph);
+  return fn;
+}
+
 static std::unique_ptr<Action> build_action(BuildContext &ctx, VirtualSocket *start);
 using ActionFromNodeCallback =
     std::function<std::unique_ptr<Action>(BuildContext &ctx, VirtualNode *vnode)>;
@@ -262,8 +290,11 @@ static std::unique_ptr<Emitter> BUILD_EMITTER_moving_point(BuildContext &ctx,
                                                            VirtualNode *vnode,
                                                            StringRef particle_type_name)
 {
-  SharedFunction fn = create_function_for_data_inputs(vnode, ctx.data_graph);
-  BLI_assert(fn->input_amount() == 0);
+  auto fn_or_error = create_function__emitter_inputs(vnode, ctx.data_graph);
+  if (fn_or_error.is_error()) {
+    return {};
+  }
+  SharedFunction fn = fn_or_error.extract_value();
 
   auto body = fn->body<TupleCallBody>();
   FN_TUPLE_CALL_ALLOC_TUPLES(body, fn_in, fn_out);



More information about the Bf-blender-cvs mailing list