Commit: e4dea76bddf59981779a3d98c5bdd8d1ec7a0d65
Author: Lukas Tönne
Date:   Mon Jun 20 17:38:37 2016 +0200
Branches: object_nodes
https://developer.blender.org/rBe4dea76bddf59981779a3d98c5bdd8d1ec7a0d65

New CodeGenerator implementation for replacing the old BVM compiler.

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

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

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

diff --git a/source/blender/blenvm/bvm/bvm_codegen.cc 
b/source/blender/blenvm/bvm/bvm_codegen.cc
index fb6fefa..efba057 100644
--- a/source/blender/blenvm/bvm/bvm_codegen.cc
+++ b/source/blender/blenvm/bvm/bvm_codegen.cc
@@ -40,6 +40,163 @@
 
 namespace blenvm {
 
+Value::Value(StackIndex stack_index) :
+    m_stack_index(stack_index),
+    m_constant_value(NULL)
+{
+}
+
+Value::~Value()
+{
+       if (m_constant_value)
+               delete m_constant_value;
+}
+
+void Value::set_constant_value(const NodeConstant *value)
+{
+       if (m_constant_value)
+               delete m_constant_value;
+       m_constant_value = value->copy();
+}
+
+/* ------------------------------------------------------------------------- */
+
+BVMCodeGenerator::BVMCodeGenerator()
+{
+       m_stack_users.resize(BVM_STACK_SIZE, 0);
+}
+
+BVMCodeGenerator::~BVMCodeGenerator()
+{
+}
+
+ValueHandle BVMCodeGenerator::get_handle(const Value *value)
+{
+       return (ValueHandle)value;
+}
+
+void BVMCodeGenerator::finalize_function()
+{
+}
+
+void BVMCodeGenerator::debug_function(FILE *file)
+{
+}
+
+void BVMCodeGenerator::node_graph_begin(const string &name, const NodeGraph 
*graph, bool use_globals)
+{
+       /* storage for function arguments */
+       {
+               size_t num_inputs = graph->inputs.size();
+               for (int i = 0; i < num_inputs; ++i) {
+                       const NodeGraph::Input *input = graph->get_input(i);
+                       const TypeSpec *typespec = 
input->typedesc.get_typespec();
+                       m_input_args.push_back(create_value(typespec));
+               }
+       }
+}
+
+void BVMCodeGenerator::node_graph_end()
+{
+}
+
+void BVMCodeGenerator::store_return_value(size_t output_index, const TypeSpec 
*UNUSED(typespec), ValueHandle handle)
+{
+       Value *value = m_value_map.at(handle);
+       m_output_args[output_index] = value;
+}
+
+ValueHandle BVMCodeGenerator::map_argument(size_t input_index, const TypeSpec 
*UNUSED(typespec))
+{
+       Value *arg = m_input_args[input_index];
+       
+       ValueHandle handle = get_handle(arg);
+       bool ok = m_value_map.insert(HandleValueMap::value_type(handle, 
arg)).second;
+       BLI_assert(ok && "Could not insert value!");
+       UNUSED_VARS(ok);
+       
+       return handle;
+}
+
+Value *BVMCodeGenerator::create_value(const TypeSpec *typespec)
+{
+       StackIndex stack_index = find_stack_index(typespec->size());
+       m_values.push_back(Value(stack_index));
+       Value *value = &m_values.back();
+       
+       return value;
+}
+
+ValueHandle BVMCodeGenerator::alloc_node_value(const TypeSpec *typespec)
+{
+       Value *value = create_value(typespec);
+       ValueHandle handle = get_handle(value);
+       
+       bool ok = m_value_map.insert(HandleValueMap::value_type(handle, 
value)).second;
+       BLI_assert(ok && "Could not insert value!");
+       UNUSED_VARS(ok);
+       
+       return handle;
+}
+
+ValueHandle BVMCodeGenerator::create_constant(const TypeSpec 
*UNUSED(typespec), const NodeConstant *node_value)
+{
+       m_values.push_back(Value(BVM_STACK_INVALID));
+       Value *value = &m_values.back();
+       value->set_constant_value(node_value);
+       ValueHandle handle = get_handle(value);
+       
+       bool ok = m_value_map.insert(HandleValueMap::value_type(handle, 
value)).second;
+       BLI_assert(ok && "Could not insert value!");
+       UNUSED_VARS(ok);
+       
+       return handle;
+}
+
+void BVMCodeGenerator::eval_node(const NodeType *nodetype,
+                                 ArrayRef<ValueHandle> input_args,
+                                 ArrayRef<ValueHandle> output_args)
+{
+}
+
+StackIndex BVMCodeGenerator::find_stack_index(int size) const
+{
+       int unused = 0;
+       
+       for (int i = 0; i < BVM_STACK_SIZE; ++i) {
+               if (m_stack_users[i] == 0) {
+                       ++unused;
+                       if (unused == size)
+                               return i + 1 - size;
+               }
+               else
+                       unused = 0;
+       }
+       
+       // TODO better reporting ...
+       printf("ERROR: out of stack space");
+       
+       return BVM_STACK_INVALID;
+}
+
+StackIndex BVMCodeGenerator::assign_stack_index(const TypeDesc &typedesc)
+{
+       const TypeSpec *typespec = typedesc.get_typespec();
+       int stack_size = EvalStack::stack_size(typespec->size());
+       
+       StackIndex stack_offset = find_stack_index(stack_size);
+       for (int i = 0; i < stack_size; ++i) {
+               // TODO keep track of value users
+               m_stack_users[stack_offset + i] += 1;
+       }
+       
+       return stack_offset;
+}
+
+
+/* ========================================================================= */
+
+
 BVMCompilerBase::BVMCompilerBase()
 {
        stack_users.resize(BVM_STACK_SIZE, 0);
diff --git a/source/blender/blenvm/bvm/bvm_codegen.h 
b/source/blender/blenvm/bvm/bvm_codegen.h
index 114d178..b6fa88a 100644
--- a/source/blender/blenvm/bvm/bvm_codegen.h
+++ b/source/blender/blenvm/bvm/bvm_codegen.h
@@ -37,6 +37,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "compiler.h"
 #include "node_graph.h"
 
 #include "bvm_instruction_list.h"
@@ -51,6 +52,66 @@ struct NodeGraph;
 struct NodeInstance;
 struct TypeDesc;
 
+struct Value {
+       Value(StackIndex m_stack_index);
+       ~Value();
+       
+       StackIndex stack_index() const { return m_stack_index; }
+       
+       const NodeConstant *constant_value() const { return m_constant_value; }
+       void set_constant_value(const NodeConstant *value);
+       
+private:
+       StackIndex m_stack_index;
+       NodeConstant *m_constant_value;
+};
+
+struct BVMCodeGenerator : public CodeGenerator {
+       typedef std::vector<Value> ValueStack;
+       typedef std::map<ValueHandle, Value*> HandleValueMap;
+       typedef std::vector<int> StackUsers;
+       typedef std::vector<Value*> Arguments;
+       
+       BVMCodeGenerator();
+       ~BVMCodeGenerator();
+       
+       void finalize_function();
+       void debug_function(FILE *file);
+       
+       void node_graph_begin(const string &name, const NodeGraph *graph, bool 
use_globals);
+       void node_graph_end();
+       
+       void store_return_value(size_t output_index, const TypeSpec *typespec, 
ValueHandle value);
+       ValueHandle map_argument(size_t input_index, const TypeSpec *typespec);
+       
+       ValueHandle alloc_node_value(const TypeSpec *typespec);
+       ValueHandle create_constant(const TypeSpec *typespec, const 
NodeConstant *node_value);
+       
+       void eval_node(const NodeType *nodetype,
+                      ArrayRef<ValueHandle> input_args,
+                      ArrayRef<ValueHandle> output_args);
+       
+protected:
+       static ValueHandle get_handle(const Value *value);
+       
+       StackIndex find_stack_index(int size) const;
+       StackIndex assign_stack_index(const TypeDesc &typedesc);
+       Value *create_value(const TypeSpec *typespec);
+       
+private:
+       ValueStack m_values;
+       HandleValueMap m_value_map;
+       StackUsers m_stack_users;
+       Arguments m_input_args;
+       Arguments m_output_args;
+       
+       InstructionList m_instructions;
+};
+
+
+/* ========================================================================= */
+
+
 typedef std::map<ConstInputKey, StackIndex> InputIndexMap;
 typedef std::map<ConstOutputKey, StackIndex> OutputIndexMap;
 typedef std::map<ConstOutputKey, int> OutputUsersMap;

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

Reply via email to