[Bf-blender-cvs] [e6bf664] object_nodes: New data type for constant (!) strings.

Lukas Tönne noreply at git.blender.org
Mon Dec 14 08:41:12 CET 2015


Commit: e6bf664d789028511e1254ef873226b24b090023
Author: Lukas Tönne
Date:   Sat Dec 12 12:34:57 2015 +0100
Branches: object_nodes
https://developer.blender.org/rBe6bf664d789028511e1254ef873226b24b090023

New data type for constant (!) strings.

The const-ness is very important: strings are only stored as const char*
on the stack, which point to memory inside the instructions list.
The instructions contain the actual char array, with a null terminator.

This does not allow allocating new strings during eval, but it makes
it possible to use static strings as identifiers and keys for global data.
Eventually we could allow string operations through an allocation system
similar to DerivedMesh handling, with init/release nodes to manage
alloc/free.

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

M	source/blender/blenvm/BVM_types.h
M	source/blender/blenvm/bvm/bvm_eval.cc
M	source/blender/blenvm/bvm/bvm_eval_common.h
M	source/blender/blenvm/bvm/bvm_function.h
M	source/blender/blenvm/bvm/bvm_opcode.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/util/bvm_util_typedesc.h
M	source/blender/makesrna/intern/rna_blenvm.c

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

diff --git a/source/blender/blenvm/BVM_types.h b/source/blender/blenvm/BVM_types.h
index f4f292a..3190507 100644
--- a/source/blender/blenvm/BVM_types.h
+++ b/source/blender/blenvm/BVM_types.h
@@ -44,6 +44,7 @@ typedef enum BVMType {
 	BVM_MATRIX44,
 	BVM_POINTER,
 	BVM_MESH,
+	BVM_STRING,
 } BVMType;
 
 typedef enum BVMInputValueType {
diff --git a/source/blender/blenvm/bvm/bvm_eval.cc b/source/blender/blenvm/bvm/bvm_eval.cc
index 7ce9477..728faec 100644
--- a/source/blender/blenvm/bvm/bvm_eval.cc
+++ b/source/blender/blenvm/bvm/bvm_eval.cc
@@ -119,6 +119,11 @@ static void eval_op_value_matrix44(float *stack, matrix44 value, StackIndex offs
 	stack_store_matrix44(stack, offset, value);
 }
 
+static void eval_op_value_string(float *stack, const char *value, StackIndex offset)
+{
+	stack_store_string(stack, offset, value);
+}
+
 /* Note: pointer data is not explicitly stored on the stack,
  * this function always creates simply a NULL pointer.
  */
@@ -329,6 +334,12 @@ void EvalContext::eval_instructions(const EvalGlobals *globals, const Function *
 				eval_op_value_mesh(stack, offset);
 				break;
 			}
+			case OP_VALUE_STRING: {
+				const char *value = fn->read_string(&instr);
+				StackIndex offset = fn->read_stack_index(&instr);
+				eval_op_value_string(stack, value, offset);
+				break;
+			}
 			case OP_FLOAT_TO_INT: {
 				StackIndex offset_from = fn->read_stack_index(&instr);
 				StackIndex offset_to = fn->read_stack_index(&instr);
diff --git a/source/blender/blenvm/bvm/bvm_eval_common.h b/source/blender/blenvm/bvm/bvm_eval_common.h
index 2f2b614..03d6c63 100644
--- a/source/blender/blenvm/bvm/bvm_eval_common.h
+++ b/source/blender/blenvm/bvm/bvm_eval_common.h
@@ -84,6 +84,11 @@ inline static DerivedMesh *stack_load_mesh(float *stack, StackIndex offset)
 	return ((mesh_ptr *)(&stack[offset]))->get();
 }
 
+inline static const char *stack_load_string(float *stack, StackIndex offset)
+{
+	return *(const char **)(&stack[offset]);
+}
+
 inline static void stack_store_float(float *stack, StackIndex offset, float f)
 {
 	*(float *)(&stack[offset]) = f;
@@ -124,6 +129,11 @@ inline static void stack_store_mesh(float *stack, StackIndex offset, DerivedMesh
 	((mesh_ptr *)(&stack[offset]))->set(dm);
 }
 
+inline static void stack_store_string(float *stack, StackIndex offset, const char *s)
+{
+	*(const char **)(&stack[offset]) = s;
+}
+
 } /* namespace bvm */
 
 #endif /* __BVM_EVAL_COMMON_H__ */
diff --git a/source/blender/blenvm/bvm/bvm_function.h b/source/blender/blenvm/bvm/bvm_function.h
index fd436e9..a961d65 100644
--- a/source/blender/blenvm/bvm/bvm_function.h
+++ b/source/blender/blenvm/bvm/bvm_function.h
@@ -175,6 +175,19 @@ struct Function {
 		return m;
 	}
 	
+	const char *read_string(int *instr) const
+	{
+		const char *s = (const char *)(&m_instructions[*instr]);
+		const char *c = s;
+		while (true) {
+			++(*instr);
+			if (c[0]=='\0' || c[1]=='\0' || c[2]=='\0' || c[3]=='\0')
+				break;
+			c += 4;
+		}
+		return s;
+	}
+	
 	void add_instruction(Instruction v);
 	int get_instruction_count() const { return m_instructions.size(); }
 	
diff --git a/source/blender/blenvm/bvm/bvm_opcode.h b/source/blender/blenvm/bvm/bvm_opcode.h
index 8dab7d2..64fc89f 100644
--- a/source/blender/blenvm/bvm/bvm_opcode.h
+++ b/source/blender/blenvm/bvm/bvm_opcode.h
@@ -43,6 +43,8 @@ enum OpCode {
 	OP_VALUE_MATRIX44,
 	OP_VALUE_POINTER,
 	OP_VALUE_MESH,
+	OP_VALUE_STRING,
+	
 	OP_FLOAT_TO_INT,
 	OP_INT_TO_FLOAT,
 	OP_SET_FLOAT3,
diff --git a/source/blender/blenvm/compile/bvm_codegen.cc b/source/blender/blenvm/compile/bvm_codegen.cc
index e669f56..b9a7887 100644
--- a/source/blender/blenvm/compile/bvm_codegen.cc
+++ b/source/blender/blenvm/compile/bvm_codegen.cc
@@ -284,6 +284,17 @@ void BVMCompiler::push_matrix44(matrix44 m) const
 	fn->add_instruction(float_to_instruction(m.data[3][3]));
 }
 
+void BVMCompiler::push_string(const char *s) const
+{
+	const char *c = s;
+	while (true) {
+		fn->add_instruction(int_to_instruction(*(const int *)c));
+		if (c[0]=='\0' || c[1]=='\0' || c[2]=='\0' || c[3]=='\0')
+			break;
+		c += 4;
+	}
+}
+
 void BVMCompiler::push_constant(const Value *value) const
 {
 	switch (value->typedesc().base_type) {
@@ -326,10 +337,15 @@ void BVMCompiler::push_constant(const Value *value) const
 			/* POINTER type can not be stored as a constant */
 			break;
 		}
-		
 		case BVM_MESH:
 			/* MESH type can not be stored as a constant */
 			break;
+		case BVM_STRING:
+			const char *s = "";
+			value->get(&s);
+			
+			push_string(s);
+			break;
 	}
 }
 
@@ -386,11 +402,18 @@ void BVMCompiler::codegen_value(const Value *value, StackIndex offset) const
 			push_stack_index(offset);
 			break;
 		}
-		
 		case BVM_MESH:
 			push_opcode(OP_VALUE_MESH);
 			push_stack_index(offset);
 			break;
+		case BVM_STRING:
+			const char *s = "";
+			value->get(&s);
+			
+			push_opcode(OP_VALUE_STRING);
+			push_string(s);
+			push_stack_index(offset);
+			break;
 	}
 }
 
@@ -403,6 +426,7 @@ static OpCode ptr_init_opcode(const TypeDesc &typedesc)
 		case BVM_INT:
 		case BVM_MATRIX44:
 		case BVM_POINTER:
+		case BVM_STRING:
 			return OP_NOOP;
 		
 		case BVM_MESH:
@@ -420,6 +444,7 @@ static OpCode ptr_release_opcode(const TypeDesc &typedesc)
 		case BVM_INT:
 		case BVM_MATRIX44:
 		case BVM_POINTER:
+		case BVM_STRING:
 			return OP_NOOP;
 		
 		case BVM_MESH:
diff --git a/source/blender/blenvm/compile/bvm_codegen.h b/source/blender/blenvm/compile/bvm_codegen.h
index 53ddad2..6743b93 100644
--- a/source/blender/blenvm/compile/bvm_codegen.h
+++ b/source/blender/blenvm/compile/bvm_codegen.h
@@ -88,6 +88,7 @@ protected:
 	void push_int(int i) const;
 	void push_matrix44(matrix44 m) const;
 	void push_pointer(PointerRNA p) const;
+	void push_string(const char *s) const;
 	
 	void push_constant(const Value *value) const;
 	
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.cc b/source/blender/blenvm/compile/bvm_nodegraph.cc
index 3f3c714..a5d5c92 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.cc
+++ b/source/blender/blenvm/compile/bvm_nodegraph.cc
@@ -604,6 +604,7 @@ SocketPair NodeGraph::add_proxy(const TypeDesc &typedesc, Value *default_value)
 		case BVM_MATRIX44: node = add_node("PASS_MATRIX44"); break;
 		case BVM_MESH: node = add_node("PASS_MESH"); break;
 		case BVM_POINTER: node = add_node("PASS_POINTER"); break;
+		case BVM_STRING: node = add_node("PASS_STRING"); break;
 	}
 	if (node && default_value)
 		node->set_input_value("value", default_value);
@@ -619,8 +620,9 @@ SocketPair NodeGraph::add_value_node(Value *value)
 		case BVM_FLOAT4: node = add_node("VALUE_FLOAT4"); break;
 		case BVM_INT: node = add_node("VALUE_INT"); break;
 		case BVM_MATRIX44: node = add_node("VALUE_MATRIX44"); break;
-		case BVM_MESH: node = add_node("VALUE_MESH"); break;
 		case BVM_POINTER: node = add_node("VALUE_POINTER"); break;
+		case BVM_MESH: node = add_node("VALUE_MESH"); break;
+		case BVM_STRING: node = add_node("VALUE_STRING"); break;
 	}
 	if (node)
 		node->set_input_value("value", value);
@@ -636,8 +638,9 @@ SocketPair NodeGraph::add_argument_node(const TypeDesc &typedesc)
 		case BVM_FLOAT4: node = add_node("ARG_FLOAT4"); break;
 		case BVM_INT: node = add_node("ARG_INT"); break;
 		case BVM_MATRIX44: node = add_node("ARG_MATRIX44"); break;
-		case BVM_MESH: node = add_node("ARG_MESH"); break;
 		case BVM_POINTER: node = add_node("ARG_POINTER"); break;
+		case BVM_MESH: node = add_node("ARG_MESH"); break;
+		case BVM_STRING: node = add_node("ARG_STRING"); break;
 	}
 	return SocketPair(node, "value");
 }
@@ -776,6 +779,7 @@ OpCode get_opcode_from_node_type(const string &node)
 	NODETYPE(VALUE_MATRIX44);
 	NODETYPE(VALUE_POINTER);
 	NODETYPE(VALUE_MESH);
+	NODETYPE(VALUE_STRING);
 	
 	NODETYPE(FLOAT_TO_INT);
 	NODETYPE(INT_TO_FLOAT);
@@ -907,6 +911,10 @@ static void register_opcode_node_types()
 	nt->add_input("value", BVM_MESH, __empty_mesh__);
 	nt->add_output("value", BVM_MESH);
 	
+	nt = NodeGraph::add_pass_node_type("PASS_STRING");
+	nt->add_input("value", BVM_STRING, "");
+	nt->add_output("value", BVM_STRING);
+	
 	nt = NodeGraph::add_function_node_type("ARG_FLOAT");
 	nt->add_output("value", BVM_FLOAT);
 	
@@ -928,6 +936,9 @@ static void register_opcode_node_types()
 	nt = NodeGraph::add_function_node_type("ARG_MESH");
 	nt->add_output("value", BVM_MESH);
 	
+	nt = NodeGraph::add_function_node_type("ARG_STRING");
+	nt->add_output("value", BVM_STRING);
+	
 	nt = NodeGraph::add_function_node_type("VALUE_FLOAT");
 	nt->add_input("value", BVM_FLOAT, 0.0f, INPUT_CONSTANT);
 	nt->add_output("value", BVM_FLOAT);
@@ -956,6 +967,10 @@ static void register_opcode_node_types()
 	nt->add_input("value", BVM_MESH, __empty_mesh__, INPUT_CONSTANT);
 	nt->add_output("value", BVM_MESH);
 	
+	nt = NodeGraph::add_function_node_type("VALUE_STRING");
+	nt->add_input("value", BVM_STRING, "", INPUT_CONSTANT);
+	nt->add_output("value", BVM_STRING);
+	
 	nt = NodeGraph::add_function_node_type("GET_ELEM_FLOAT3");
 	nt->add_input("index", BVM_INT, 0, INPUT_CONSTANT);
 	nt->add_input("value", BVM_FLOAT3, float3(0.0f, 0.0f, 0.0f));
diff --git a/source/blender/blenvm/util/bvm_util_typedesc.h b/source/blender/blenvm/util/bvm_util_typedesc.h
index fdb6fc0..c53132a 100644
--- a/source/blender/blenvm/util/bvm_util_typedesc.h
+++ b/source/blender/blenvm/util/bvm_util_typedesc.h
@@ -401,6 +401,18 @@ struct BaseTypeTraits<BVM_MESH> {
 	}
 };
 
+template <>
+struct BaseTypeTraits<BVM_STRING> {
+	typedef const char* POD;
+	
+	enum eStackSize { stack_size = 2 };
+	
+	static inline void copy(POD *to, const POD *from)
+	{
+		*to = *from;
+	}
+};
+
 /* ------------------------------------------------------------------------- */
 
 struct TypeDesc {
@@ -487,8 +499,8 @@ Value *Value::create(const TypeDesc &typedesc, T data)
 		case BVM_INT: return new ValueType<BVM_INT>(data);
 		case BVM_MATRIX44: return new ValueType<BVM_MATRIX44>(data);
 		case BVM_POINTER: return new ValueType<BVM_POINTER>(data);
-		
 		case BVM_MESH: return new ValueType<BVM_MESH>(data);
+		case BVM_STRING: return new ValueType<BVM_STRING>(data);
 	}
 	return 0;
 }
@@ -503,8 +515,8 @@ bool Value::get(T *data) const
 		case BVM_INT: return static_cast< const ValueType<BVM_INT>* >(this)->get(da

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list