Commit: cf27fdb65995527ef538accd2241dbbdd9b113fa
Author: Lukas Tönne
Date:   Thu Jun 23 12:36:05 2016 +0200
Branches: object_nodes
https://developer.blender.org/rBcf27fdb65995527ef538accd2241dbbdd9b113fa

Reduce repetitive code in the C API by defining static node graph signatures.

Node graph inputs/outputs are now defined statically and graph instances
can be constructed by passing these as ArrayRefs. The code for compiling
various node graph types can then be unified.

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

M       source/blender/blenvm/BVM_api.h
M       source/blender/blenvm/compile/node_graph.cc
M       source/blender/blenvm/compile/node_graph.h
M       source/blender/blenvm/intern/bvm_api.cc

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

diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index 467e147..db83831 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -173,6 +173,7 @@ struct DerivedMesh;
 struct Mesh;
 
 struct BVMFunction *BVM_gen_modifier_function_bvm(struct bNodeTree *btree, 
bool use_cache);
+struct BVMFunction *BVM_gen_modifier_function_llvm(struct bNodeTree *btree, 
bool use_cache);
 void BVM_debug_modifier_nodes(struct bNodeTree *btree, FILE *debug_file, const 
char *label, BVMDebugMode mode);
 
 struct DerivedMesh *BVM_eval_modifier_bvm(struct BVMEvalGlobals *globals,
diff --git a/source/blender/blenvm/compile/node_graph.cc 
b/source/blender/blenvm/compile/node_graph.cc
index 2860737..ab751d9 100644
--- a/source/blender/blenvm/compile/node_graph.cc
+++ b/source/blender/blenvm/compile/node_graph.cc
@@ -612,6 +612,15 @@ NodeGraph::NodeGraph()
 {
 }
 
+NodeGraph::NodeGraph(ArrayRef<NodeInputParam> inputs, 
ArrayRef<NodeOutputParam> outputs)
+{
+       for (size_t i = 0; i < inputs.size(); ++i)
+               add_input(inputs[i].name, inputs[i].type);
+       for (size_t i = 0; i < outputs.size(); ++i)
+               add_output(outputs[i].name, outputs[i].type,
+                          outputs[i].default_value->copy());
+}
+
 NodeGraph::~NodeGraph()
 {
        remove_all_nodes();
@@ -689,20 +698,6 @@ const NodeGraph::Output *NodeGraph::get_output(const 
string &name) const
        return NULL;
 }
 
-void NodeGraph::set_output_socket(int index, const OutputKey &key)
-{
-       BLI_assert(index >= 0 && index < outputs.size());
-       outputs[index].key = key;
-}
-
-void NodeGraph::set_output_socket(const string &name, const OutputKey &key)
-{
-       for (OutputList::iterator it = outputs.begin(); it != outputs.end(); 
++it) {
-               if (it->name == name)
-                       it->key = key;
-       }
-}
-
 const NodeGraph::Input *NodeGraph::add_input(const string &name, const string 
&type)
 {
        BLI_assert(!get_input(name));
diff --git a/source/blender/blenvm/compile/node_graph.h 
b/source/blender/blenvm/compile/node_graph.h
index c5d50e7..db16c21 100644
--- a/source/blender/blenvm/compile/node_graph.h
+++ b/source/blender/blenvm/compile/node_graph.h
@@ -52,6 +52,7 @@ extern "C" {
 
 #include "node_value.h"
 
+#include "util_array.h"
 #include "util_opcode.h"
 #include "util_string.h"
 
@@ -305,6 +306,52 @@ private:
 
 typedef std::set<NodeBlock *> NodeBlockSet;
 
+/** Formal input parameter of a NodeGraph. */
+struct NodeInputParam {
+       NodeInputParam(const string &name, const string &type) :
+           name(name), type(type)
+       {}
+       
+       string name;
+       string type;
+};
+
+/** Formal output parameter of a NodeGraph.
+ * \note The parameter takes ownership of the default_value!
+ */
+struct NodeOutputParam {
+       NodeOutputParam(const string &name, const string &type, NodeConstant 
*default_value) :
+           name(name), type(type), default_value(default_value)
+       {}
+       
+       NodeOutputParam(const NodeOutputParam &other) :
+           name(other.name), type(other.type), 
default_value(other.default_value->copy())
+       {}
+       
+       ~NodeOutputParam()
+       {
+               if (default_value)
+                       delete default_value;
+       }
+       
+       NodeOutputParam& operator = (const NodeOutputParam &other)
+       {
+               name = other.name;
+               type = other.type;
+               default_value = other.default_value->copy();
+               return *this;
+       }
+       
+       template <typename T>
+       NodeOutputParam(const string &name, const string &type, const T 
&default_value) :
+           name(name), type(type), 
default_value(NodeConstant::create(TypeDesc(type), default_value))
+       {}
+       
+       string name;
+       string type;
+       NodeConstant *default_value;
+};
+
 struct NodeGraph {
        struct Input {
                Input(const string &name, const TypeDesc &typedesc, const 
OutputKey &key) :
@@ -339,6 +386,7 @@ struct NodeGraph {
        static void remove_node_type(const string &name);
        
        NodeGraph();
+       NodeGraph(ArrayRef<NodeInputParam> inputs, ArrayRef<NodeOutputParam> 
outputs);
        ~NodeGraph();
        
        NodeInstance *get_node(const string &name);
@@ -348,18 +396,6 @@ struct NodeGraph {
        const Input *get_input(const string &name) const;
        const Output *get_output(int index) const;
        const Output *get_output(const string &name) const;
-       void set_output_socket(int index, const OutputKey &key);
-       void set_output_socket(const string &name, const OutputKey &key);
-       
-       const Input *add_input(const string &name, const string &type);
-       const Output *add_output(const string &name, const string &type, 
NodeConstant *default_value);
-       
-       template <typename T>
-       const Output *add_output(const string &name, const string &type, const 
T &default_value)
-       {
-               TypeDesc td(type);
-               return add_output(name, type, NodeConstant::create(td, 
default_value));
-       }
        
        void finalize();
        
@@ -367,6 +403,9 @@ struct NodeGraph {
        const NodeBlock &main_block() const { return blocks.front(); }
        
 protected:
+       const Input *add_input(const string &name, const string &type);
+       const Output *add_output(const string &name, const string &type, 
NodeConstant *default_value);
+       
        NodeInstance *add_proxy(const TypeDesc &typedesc, NodeConstant 
*default_value = NULL);
        OutputKey add_value_node(NodeConstant *value);
        OutputKey add_argument_node(const TypeDesc &typedesc);
diff --git a/source/blender/blenvm/intern/bvm_api.cc 
b/source/blender/blenvm/intern/bvm_api.cc
index 39c2c86..4e3eb14 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -69,6 +69,7 @@ extern "C" {
 #include "llvm_function.h"
 #endif
 
+#include "util_array.h"
 #include "util_debug.h"
 #include "util_map.h"
 #include "util_thread.h"
@@ -84,6 +85,41 @@ static blenvm::EvalGlobals *eval_globals_default()
        return &default_globals;
 }
 
+static std::vector<NodeInputParam> forcefield_inputs;
+static std::vector<NodeOutputParam> forcefield_outputs;
+static std::vector<NodeInputParam> texture_inputs;
+static std::vector<NodeOutputParam> texture_outputs;
+static std::vector<NodeInputParam> modifier_inputs;
+static std::vector<NodeOutputParam> modifier_outputs;
+static std::vector<NodeInputParam> dupli_inputs;
+static std::vector<NodeOutputParam> dupli_outputs;
+
+static void register_graph_types()
+{
+       static const float3 zerovec(0.0f, 0.0f, 0.0f);
+       static const float C[4] = {0.0f, 0.0f, 0.0f, 1.0f};
+       static const float N[3] = {0.0f, 0.0f, 0.0f};
+       
+       forcefield_inputs.push_back(NodeInputParam("effector.object", 
"RNAPOINTER"));
+       forcefield_inputs.push_back(NodeInputParam("effector.position", 
"FLOAT3"));
+       forcefield_inputs.push_back(NodeInputParam("effector.velocity", 
"FLOAT3"));
+       forcefield_outputs.push_back(NodeOutputParam("force", "FLOAT3", 
zerovec));
+       forcefield_outputs.push_back(NodeOutputParam("impulse", "FLOAT3", 
zerovec));
+       
+       texture_inputs.push_back(NodeInputParam("texture.co", "FLOAT3"));
+       texture_inputs.push_back(NodeInputParam("texture.cfra", "INT"));
+       texture_inputs.push_back(NodeInputParam("texture.osatex", "INT"));
+       texture_outputs.push_back(NodeOutputParam("color", "FLOAT4", C));
+       texture_outputs.push_back(NodeOutputParam("normal", "FLOAT3", N));
+       
+       modifier_inputs.push_back(NodeInputParam("modifier.object", 
"RNAPOINTER"));
+       modifier_inputs.push_back(NodeInputParam("modifier.base_mesh", 
"RNAPOINTER"));
+       modifier_outputs.push_back(NodeOutputParam("mesh", "MESH", 
__empty_mesh__));
+       
+       dupli_inputs.push_back(NodeInputParam("dupli.object", "RNAPOINTER"));
+       dupli_outputs.push_back(NodeOutputParam("dupli.result", "DUPLIS", 
__empty_duplilist__));
+}
+
 }
 
 void BVM_init(void)
@@ -93,6 +129,7 @@ void BVM_init(void)
        create_empty_mesh(__empty_mesh__);
        
        nodes_init();
+       register_graph_types();
        
 #ifdef WITH_LLVM
        llvm_init();
@@ -428,7 +465,9 @@ void BVM_function_llvm_cache_remove(void */*key*/) {}
 
 /* ------------------------------------------------------------------------- */
 
-static blenvm::string get_ntree_unique_function_name(bNodeTree *ntree)
+namespace blenvm {
+
+static string get_ntree_unique_function_name(bNodeTree *ntree)
 {
        std::stringstream ss;
        ss << "nodetree_" << ntree;
@@ -436,7 +475,7 @@ static blenvm::string 
get_ntree_unique_function_name(bNodeTree *ntree)
        return ss.str();
 }
 
-static void parse_py_nodes(bNodeTree *btree, blenvm::NodeGraph *graph)
+static void parse_py_nodes(bNodeTree *btree, NodeGraph *graph)
 {
        PointerRNA ptr;
        ParameterList list;
@@ -457,8 +496,6 @@ static void parse_py_nodes(bNodeTree *btree, 
blenvm::NodeGraph *graph)
 
 static void debug_node_graph(blenvm::NodeGraph &graph, FILE *debug_file, const 
char *label, BVMDebugMode mode)
 {
-       using namespace blenvm;
-       
        if (mode != BVM_DEBUG_NODES_UNOPTIMIZED)
                graph.finalize();
        
@@ -493,21 +530,9 @@ static void debug_node_graph(blenvm::NodeGraph &graph, 
FILE *debug_file, const c
        }
 }
 
-
-static void init_forcefield_graph(blenvm::NodeGraph &graph)
-{
-       using namespace blenvm;
-       
-       graph.add_input("effector.object", "RNAPOINTER");
-       graph.add_input("effector.position", "FLOAT3");
-       graph.add_input("effector.velocity", "FLOAT3");
-       
-       float zero[3] = {0.0f, 0.0f, 0.0f};
-       graph.add_output("force", "FLOAT3", zero);
-       graph.add_output("impulse", "FLOAT3", zero);
-}
-
-struct BVMFunction *BVM_gen_forcefield_function_bvm(bNodeTree *btree, bool 
use_cache)
+static struct BVMFunction *gen_function_bvm(struct bNodeTree *btree, bool 
use_cache,
+                                            ArrayRef<NodeInputParam> inputs,
+                                            ArrayRef<NodeOutputParam> outputs)
 {
        using namespace blenvm;
        
@@ -519,8 +544,7 @@ struct BVMFunction 
*BVM_gen_forcefield_function_bvm(bNodeTree *btree, bool use_c
        }
        
        if (!fn) {
-               NodeGraph graph;
-               init_forcefield_graph(graph);
+               NodeGraph graph(inputs, outputs);
                parse_py_nodes(btree, &graph);
                graph.finalize();
                
@@ -539,17 +563,74 @@ struct BVMFunction 
*BVM_gen_forcefield_function_bvm(bNodeTree *btree, bool use_c
        return (BVMFunction *)fn;
 }
 
-void BVM_debug_forcefield_nodes(bNodeTree *btree, FILE *debug_file, const char 
*label, BVMDebugMode mode)
+static struct BVMFunction *gen_function_llvm(struct bNodeTree *btree, bool 
use_cache,
+                                             ArrayRef<NodeInputParam> inputs,
+                                             ArrayRef<NodeOutputParam> outputs)
 {
+#ifdef WITH_LLVM
        using namespace blenvm;
        
-       NodeGraph graph;
-       init_forcefield_graph(graph);
+       llvm_

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to