[Bf-blender-cvs] [d125723] object_nodes: Codegen: for function inputs store the entry point in the instructions.

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


Commit: d1257237196c364d9bbfcb3a0212f36b9cb54206
Author: Lukas Tönne
Date:   Mon Nov 30 16:16:45 2015 +0100
Branches: object_nodes
https://developer.blender.org/rBd1257237196c364d9bbfcb3a0212f36b9cb54206

Codegen: for function inputs store the entry point in the instructions.

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

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

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

diff --git a/source/blender/blenvm/bvm/bvm_eval.cc b/source/blender/blenvm/bvm/bvm_eval.cc
index 76ed42b..3ae7843 100644
--- a/source/blender/blenvm/bvm/bvm_eval.cc
+++ b/source/blender/blenvm/bvm/bvm_eval.cc
@@ -877,9 +877,10 @@ void EvalContext::eval_instructions(const EvalGlobals *globals, const EvalData *
 			case OP_MESH_ARRAY: {
 				StackIndex offset_mesh_in = fn->read_stack_index(&instr);
 				StackIndex offset_count = fn->read_stack_index(&instr);
+				int fn_transform = fn->read_jump_address(&instr);
 				StackIndex offset_transform = fn->read_stack_index(&instr);
 				StackIndex offset_mesh_out = fn->read_stack_index(&instr);
-				eval_op_mesh_array(stack, offset_mesh_in, offset_mesh_out, offset_count, offset_transform);
+				eval_op_mesh_array(stack, offset_mesh_in, offset_mesh_out, offset_count, fn_transform, offset_transform);
 				break;
 			}
 			case OP_END:
diff --git a/source/blender/blenvm/bvm/bvm_eval_mesh.h b/source/blender/blenvm/bvm/bvm_eval_mesh.h
index c930861..5e0ecef 100644
--- a/source/blender/blenvm/bvm/bvm_eval_mesh.h
+++ b/source/blender/blenvm/bvm/bvm_eval_mesh.h
@@ -150,7 +150,7 @@ static DerivedMesh *do_array(DerivedMesh *dm, int count, const matrix44 &tfm)
 }
 
 static void eval_op_mesh_array(float *stack, StackIndex offset_mesh_in, StackIndex offset_mesh_out,
-                               StackIndex offset_count, StackIndex offset_transform)
+                               StackIndex offset_count, int fn_transform, StackIndex offset_transform)
 {
 	DerivedMesh *dm = stack_load_mesh(stack, offset_mesh_in);
 	int count = stack_load_int(stack, offset_count);
diff --git a/source/blender/blenvm/bvm/bvm_function.h b/source/blender/blenvm/bvm/bvm_function.h
index d1ffd05..dd40ce7 100644
--- a/source/blender/blenvm/bvm/bvm_function.h
+++ b/source/blender/blenvm/bvm/bvm_function.h
@@ -132,6 +132,13 @@ struct Function {
 		return index;
 	}
 	
+	int read_jump_address(int *instr) const
+	{
+		int address = m_instructions[*instr];
+		++(*instr);
+		return address;
+	}
+	
 	float read_float(int *instr) const
 	{
 		float f = instruction_to_float(m_instructions[*instr]);
@@ -203,9 +210,10 @@ struct Function {
 	}
 	
 	void add_instruction(Instruction v);
+	int get_instruction_count() const { return m_instructions.size(); }
 	
 	int entry_point() const { return m_entry_point; }
-	void set_entry_point(int m_entry_point);
+	void set_entry_point(int entry_point);
 	
 	void add_return_value(const TypeDesc &typedesc, const string &name, StackIndex stack_offset);
 	size_t return_values_size() const;
diff --git a/source/blender/blenvm/compile/bvm_codegen.cc b/source/blender/blenvm/compile/bvm_codegen.cc
index 391482d..506797e 100644
--- a/source/blender/blenvm/compile/bvm_codegen.cc
+++ b/source/blender/blenvm/compile/bvm_codegen.cc
@@ -93,6 +93,11 @@ void BVMCompiler::push_stack_index(StackIndex arg)
 		fn->add_instruction(arg);
 }
 
+void BVMCompiler::push_jump_address(int address)
+{
+	fn->add_instruction(int_to_instruction(address));
+}
+
 void BVMCompiler::push_float(float f)
 {
 	fn->add_instruction(float_to_instruction(f));
@@ -268,70 +273,6 @@ void BVMCompiler::codegen_constant(const Value *value)
 	}
 }
 
-static void sort_nodes_append(const NodeInstance *node, NodeList &result, NodeSet &visited)
-{
-	if (visited.find(node) != visited.end())
-		return;
-	visited.insert(node);
-	
-	for (size_t i = 0; i < node->num_inputs(); ++i) {
-		const NodeInstance *link_node = node->find_input_link_node(i);
-		if (link_node) {
-			sort_nodes_append(link_node, result, visited);
-		}
-	}
-	
-	result.push_back(node);
-}
-
-static void sort_nodes(const NodeGraph &graph, NodeList &result)
-{
-	NodeSet visited;
-	
-	for (NodeGraph::NodeInstanceMap::const_iterator it = graph.nodes.begin(); it != graph.nodes.end(); ++it) {
-		sort_nodes_append(it->second, result, visited);
-	}
-}
-
-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) {
-		const NodeInstance *node = it->second;
-		for (int i = 0; i < node->num_outputs(); ++i) {
-			ConstSocketPair key(node, node->type->find_output(i)->name);
-			users[key] = 0;
-		}
-	}
-	
-	for (NodeGraph::NodeInstanceMap::const_iterator it = graph.nodes.begin(); it != graph.nodes.end(); ++it) {
-		const NodeInstance *node = it->second;
-		
-		/* note: pass nodes are normally removed, but can exist for debugging purposes */
-		if (node->type->is_pass_node())
-			continue;
-		
-		for (int i = 0; i < node->num_inputs(); ++i) {
-			if (node->has_input_link(i)) {
-				ConstSocketPair key(node->find_input_link_node(i),
-				                    node->find_input_link_socket(i)->name);
-				users[key] += 1;
-			}
-		}
-	}
-	/* 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;
-		users[output.key] += 1;
-	}
-}
-
 static OpCode ptr_init_opcode(const TypeDesc &typedesc)
 {
 	switch (typedesc.base_type) {
@@ -366,12 +307,15 @@ static OpCode ptr_release_opcode(const TypeDesc &typedesc)
 	return OP_NOOP;
 }
 
-void BVMCompiler::codegen_subgraph(const NodeList &nodes,
-                                   const SocketUserMap &output_users,
-                                   SocketIndexMap &output_index)
+int BVMCompiler::codegen_subgraph(const NodeList &nodes,
+                                  const SocketUserMap &socket_users,
+                                  SubgraphOutputList &outputs)
 {
-	SocketIndexMap input_index;
+	typedef std::map<ConstSocketPair, StackIndex> SocketIndexMap;
+	
+	int entry_point = fn->get_instruction_count();
 	
+	SocketIndexMap output_index;
 	for (NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
 		const NodeInstance &node = **it;
 		
@@ -380,15 +324,14 @@ void BVMCompiler::codegen_subgraph(const NodeList &nodes,
 			continue;
 		
 		/* prepare input stack entries */
+		SocketIndexMap input_index;
 		for (int i = 0; i < node.num_inputs(); ++i) {
 			const NodeSocket *input = node.type->find_input(i);
 			ConstSocketPair key(&node, input->name);
 			assert(input_index.find(key) == input_index.end());
 			
-			if (node.is_input_constant(i)) {
-				/* value is stored directly in the instructions list,
-				 * after the opcode
-				 */
+			if (node.is_input_constant(i) || node.is_input_function(i)) {
+				/* stored directly in the instructions list after creating values */
 			}
 			else if (node.has_input_link(i)) {
 				ConstSocketPair link_key(node.find_input_link_node(i),
@@ -409,15 +352,14 @@ void BVMCompiler::codegen_subgraph(const NodeList &nodes,
 		for (int i = 0; i < node.num_outputs(); ++i) {
 			const NodeSocket *output = node.type->find_output(i);
 			ConstSocketPair key(&node, output->name);
-			assert(output_index.find(key) == output_index.end());
 			
 			output_index[key] = assign_stack_index(output->typedesc);
 			
 			/* if necessary, add a user count initializer */
 			OpCode init_op = ptr_init_opcode(output->typedesc);
 			if (init_op != OP_NOOP) {
-				assert(output_users.find(key) != output_users.end());
-				int users = output_users.find(key)->second;
+				assert(socket_users.find(key) != socket_users.end());
+				int users = socket_users.find(key)->second;
 				if (users > 0) {
 					push_opcode(init_op);
 					push_stack_index(output_index[key]);
@@ -437,6 +379,12 @@ void BVMCompiler::codegen_subgraph(const NodeList &nodes,
 				Value *value = node.find_input_value(i);
 				codegen_constant(value);
 			}
+			else if (node.is_input_function(i)) {
+				assert(func_entry_map.find(key) != func_entry_map.end());
+				FunctionInfo &func = func_entry_map[key];
+				push_jump_address(func.entry_point);
+				push_stack_index(func.return_index);
+			}
 			else {
 				assert(input_index.find(key) != input_index.end());
 				push_stack_index(input_index[key]);
@@ -455,7 +403,10 @@ void BVMCompiler::codegen_subgraph(const NodeList &nodes,
 		for (int i = 0; i < node.num_inputs(); ++i) {
 			const NodeSocket *input = node.type->find_input(i);
 			
-			if (node.has_input_link(i)) {
+			if (node.is_input_constant(i) || node.is_input_function(i)) {
+				/* pass */
+			}
+			else if (node.has_input_link(i)) {
 				ConstSocketPair link_key(node.find_input_link_node(i),
 				                         node.find_input_link_socket(i)->name);
 				assert(output_index.find(link_key) != output_index.end());
@@ -471,31 +422,178 @@ void BVMCompiler::codegen_subgraph(const NodeList &nodes,
 	}
 	
 	push_opcode(OP_END);
+	
+	for (SubgraphOutputList::iterator it = outputs.begin(); it != outputs.end(); ++it) {
+		SubgraphOutput &output = *it;
+		
+		if (output.key.node) {
+			assert(output_index.find(output.key) != output_index.end());
+			output.stack_index = output_index[output.key];
+		}
+		else {
+			output.stack_index = codegen_value(output.value);
+		}
+	}
+	
+	return entry_point;
 }
 
-Function *BVMCompiler::codegen_function(const NodeGraph &graph)
+void BVMCompiler::graph_node_append(const NodeInstance *node,
+                                    NodeList &sorted_nodes,
+                                    NodeSet &visited)
 {
-	fn = new Function();
-	int entry_point = 0;
+	if (visited.find(node) != visited.end())
+		return;
+	visited.insert(node);
 	
-	NodeList sorted_nodes;
-	sort_nodes(graph, sorted_nodes);
+	for (size_t i = 0; i < node->num_inputs(); ++i) {
+		const NodeSocket *socket = node->type->find_input(i);
+		if (socket->value_type == VALUE_FUNCTION) {
+			func_entry_map[node->input(i)] = FunctionInfo();
+		}
+		else {
+			const NodeInstance *link_node = node->find_input_link_node(i);
+			if (link_node) {
+				graph_node_append(link_node, sorted_nodes, visited);
+			}
+		}
+	}
 	
+	sorted_nodes.push_back(node);
+}
+
+void BVMCompiler::sort_graph_nodes(cons

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list