[Bf-blender-cvs] [96247852] object_nodes: Generate instructions for a subgraph in a separate codegen function.

Lukas Tönne noreply at git.blender.org
Tue Dec 1 09:53:34 CET 2015


Commit: 962478523eedaa348b301a2018fe4ae3d278d8d6
Author: Lukas Tönne
Date:   Sat Nov 28 17:07:11 2015 +0100
Branches: object_nodes
https://developer.blender.org/rB962478523eedaa348b301a2018fe4ae3d278d8d6

Generate instructions for a subgraph in a separate codegen function.

This will be useful for separately generating kernel functions, which
are to be stored as own code blocks so kernels can call them repeatedly
as needed.

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

M	source/blender/blenvm/bvm/bvm_function.cc
M	source/blender/blenvm/bvm/bvm_function.h
M	source/blender/blenvm/compile/bvm_codegen.cc
M	source/blender/blenvm/compile/bvm_codegen.h

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

diff --git a/source/blender/blenvm/bvm/bvm_function.cc b/source/blender/blenvm/bvm/bvm_function.cc
index 8a93910..e1f6753 100644
--- a/source/blender/blenvm/bvm/bvm_function.cc
+++ b/source/blender/blenvm/bvm/bvm_function.cc
@@ -52,10 +52,9 @@ void Function::set_entry_point(int entry_point)
 	m_entry_point = entry_point;
 }
 
-ReturnValue &Function::add_return_value(const TypeDesc &typedesc, const string &name)
+void Function::add_return_value(const TypeDesc &typedesc, const string &name, StackIndex stack_offset)
 {
-	m_return_values.push_back(ReturnValue(typedesc, name));
-	return m_return_values.back();
+	m_return_values.push_back(ReturnValue(typedesc, name, stack_offset));
 }
 
 size_t Function::return_values_size() const
diff --git a/source/blender/blenvm/bvm/bvm_function.h b/source/blender/blenvm/bvm/bvm_function.h
index 2822ed7..d1ffd05 100644
--- a/source/blender/blenvm/bvm/bvm_function.h
+++ b/source/blender/blenvm/bvm/bvm_function.h
@@ -98,10 +98,10 @@ static inline void *instruction_to_pointer(Instruction hi, Instruction lo)
 }
 
 struct ReturnValue {
-	ReturnValue(const TypeDesc &typedesc, const string &name) :
+	ReturnValue(const TypeDesc &typedesc, const string &name, StackIndex stack_offset) :
 	    typedesc(typedesc),
 	    name(name),
-	    stack_offset(BVM_STACK_INVALID)
+	    stack_offset(stack_offset)
 	{}
 	
 	TypeDesc typedesc;
@@ -207,7 +207,7 @@ struct Function {
 	int entry_point() const { return m_entry_point; }
 	void set_entry_point(int m_entry_point);
 	
-	ReturnValue &add_return_value(const TypeDesc &typedesc, const string &name = "");
+	void add_return_value(const TypeDesc &typedesc, const string &name, StackIndex stack_offset);
 	size_t return_values_size() const;
 	const ReturnValue &return_value(size_t index) const;
 	const ReturnValue &return_value(const string &name) const;
diff --git a/source/blender/blenvm/compile/bvm_codegen.cc b/source/blender/blenvm/compile/bvm_codegen.cc
index 374cf8b..391482d 100644
--- a/source/blender/blenvm/compile/bvm_codegen.cc
+++ b/source/blender/blenvm/compile/bvm_codegen.cc
@@ -268,9 +268,6 @@ void BVMCompiler::codegen_constant(const Value *value)
 	}
 }
 
-typedef std::vector<const NodeInstance *> NodeList;
-typedef std::set<const NodeInstance *> NodeSet;
-
 static void sort_nodes_append(const NodeInstance *node, NodeList &result, NodeSet &visited)
 {
 	if (visited.find(node) != visited.end())
@@ -296,9 +293,8 @@ static void sort_nodes(const NodeGraph &graph, NodeList &result)
 	}
 }
 
-typedef std::map<ConstSocketPair, int> SocketUserMap;
-
-static void count_output_users(const NodeGraph &graph, SocketUserMap &users)
+static void count_output_users(const NodeGraph &graph,
+                               SocketUserMap &users)
 {
 	users.clear();
 	for (NodeGraph::NodeInstanceMap::const_iterator it = graph.nodes.begin(); it != graph.nodes.end(); ++it) {
@@ -324,12 +320,15 @@ static void count_output_users(const NodeGraph &graph, SocketUserMap &users)
 			}
 		}
 	}
+	/* inputs are defined externally, they should be retained during evaluation */
+	for (NodeGraph::InputList::const_iterator it = graph.inputs.begin(); it != graph.inputs.end(); ++it) {
+		const NodeGraph::Input &input = *it;
+		users[input.key] += 1;
+	}
+	/* outputs are passed on to the caller, which is responsible for freeing them */
 	for (NodeGraph::OutputList::const_iterator it = graph.outputs.begin(); it != graph.outputs.end(); ++it) {
 		const NodeGraph::Output &output = *it;
-		
-		if (output.key.node) {
-			users[output.key] += 1;
-		}
+		users[output.key] += 1;
 	}
 }
 
@@ -367,23 +366,13 @@ static OpCode ptr_release_opcode(const TypeDesc &typedesc)
 	return OP_NOOP;
 }
 
-Function *BVMCompiler::codegen_function(const NodeGraph &graph)
+void BVMCompiler::codegen_subgraph(const NodeList &nodes,
+                                   const SocketUserMap &output_users,
+                                   SocketIndexMap &output_index)
 {
-	typedef std::map<ConstSocketPair, StackIndex> SocketIndexMap;
-	
-	fn = new Function();
-	int entry_point = 0;
-	
-	NodeList sorted_nodes;
-	sort_nodes(graph, sorted_nodes);
-	
-	SocketUserMap output_users;
-	count_output_users(graph, output_users);
-	
 	SocketIndexMap input_index;
-	SocketIndexMap output_index;
 	
-	for (NodeList::const_iterator it = sorted_nodes.begin(); it != sorted_nodes.end(); ++it) {
+	for (NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
 		const NodeInstance &node = **it;
 		
 		OpCode op = get_opcode_from_node_type(node.type->name());
@@ -427,7 +416,8 @@ Function *BVMCompiler::codegen_function(const NodeGraph &graph)
 			/* if necessary, add a user count initializer */
 			OpCode init_op = ptr_init_opcode(output->typedesc);
 			if (init_op != OP_NOOP) {
-				int users = output_users[key];
+				assert(output_users.find(key) != output_users.end());
+				int users = output_users.find(key)->second;
 				if (users > 0) {
 					push_opcode(init_op);
 					push_stack_index(output_index[key]);
@@ -480,22 +470,34 @@ Function *BVMCompiler::codegen_function(const NodeGraph &graph)
 		}
 	}
 	
-	for (NodeGraph::OutputList::const_iterator it = graph.outputs.begin();
-	     it != graph.outputs.end();
-	     ++it) {
-		const NodeGraph::Output &output = *it;
-		
-		const NodeSocket *socket = output.key.node->type->find_output(output.key.socket);
-		ReturnValue &rval = fn->add_return_value(socket->typedesc, output.name);
-		
-		assert(output_index.find(output.key) != output_index.end());
-		rval.stack_offset = output_index[output.key];
-	}
-	
 	push_opcode(OP_END);
+}
+
+Function *BVMCompiler::codegen_function(const NodeGraph &graph)
+{
+	fn = new Function();
+	int entry_point = 0;
+	
+	NodeList sorted_nodes;
+	sort_nodes(graph, sorted_nodes);
+	
+	SocketUserMap output_users;
+	count_output_users(graph, output_users);
+	
+	SocketIndexMap output_index;
+	codegen_subgraph(sorted_nodes, output_users, output_index);
 	
 	fn->set_entry_point(entry_point);
 	
+	/* store final stack indices for outputs, so we can return results to the caller */
+	for (size_t i = 0; i < graph.outputs.size(); ++i) {
+		const NodeGraph::Output &output = graph.outputs[i];
+		const NodeSocket *socket = output.key.node->type->find_output(output.key.socket);
+		assert(output_index.find(output.key) != output_index.end());
+		StackIndex offset = output_index[output.key];
+		fn->add_return_value(socket->typedesc, output.name, offset);
+	}
+	
 	Function *result = fn;
 	fn = NULL;
 	return result;
diff --git a/source/blender/blenvm/compile/bvm_codegen.h b/source/blender/blenvm/compile/bvm_codegen.h
index 28b6c18..77c0b2d 100644
--- a/source/blender/blenvm/compile/bvm_codegen.h
+++ b/source/blender/blenvm/compile/bvm_codegen.h
@@ -32,11 +32,13 @@
  *  \ingroup bvm
  */
 
+#include <set>
 #include <vector>
 
 #include "MEM_guardedalloc.h"
 
 #include "bvm_function.h"
+#include "bvm_nodegraph.h"
 #include "bvm_opcode.h"
 #include "bvm_util_string.h"
 
@@ -47,6 +49,11 @@ struct NodeGraph;
 struct NodeInstance;
 struct TypeDesc;
 
+typedef std::vector<const NodeInstance *> NodeList;
+typedef std::set<const NodeInstance *> NodeSet;
+typedef std::map<ConstSocketPair, StackIndex> SocketIndexMap;
+typedef std::map<ConstSocketPair, int> SocketUserMap;
+
 struct BVMCompiler {
 	typedef std::vector<int> StackUsers;
 	
@@ -67,6 +74,9 @@ struct BVMCompiler {
 	
 	StackIndex codegen_value(const Value *value);
 	void codegen_constant(const Value *value);
+	void codegen_subgraph(const NodeList &nodes,
+	                      const SocketUserMap &output_users,
+	                      SocketIndexMap &output_index);
 	Function *codegen_function(const NodeGraph &graph);
 	
 private:




More information about the Bf-blender-cvs mailing list