[Bf-blender-cvs] [c4f8562] strand_nodes: New code generator for GLSL code.
Lukas Tönne
noreply at git.blender.org
Thu Jul 21 17:41:06 CEST 2016
Commit: c4f85627c34a3a79d91b15946508d0b725514599
Author: Lukas Tönne
Date: Wed Jul 20 12:08:59 2016 +0200
Branches: strand_nodes
https://developer.blender.org/rBc4f85627c34a3a79d91b15946508d0b725514599
New code generator for GLSL code.
This can be used as part of shaders.
===================================================================
M build_files/cmake/macros.cmake
M source/blender/blenvm/BVM_api.h
M source/blender/blenvm/CMakeLists.txt
A source/blender/blenvm/glsl/CMakeLists.txt
A source/blender/blenvm/glsl/glsl_codegen.cc
A source/blender/blenvm/glsl/glsl_codegen.h
M source/blender/blenvm/intern/bvm_api.cc
M source/blender/blenvm/util/util_string.h
M source/blenderplayer/CMakeLists.txt
===================================================================
diff --git a/build_files/cmake/macros.cmake b/build_files/cmake/macros.cmake
index 595b634..9949436 100644
--- a/build_files/cmake/macros.cmake
+++ b/build_files/cmake/macros.cmake
@@ -597,6 +597,7 @@ function(SETUP_BLENDER_SORTED_LIBS)
bf_blenvm
bf_blenvm_compile
bf_blenvm_bvm
+ bf_blenvm_glsl
bf_physics
bf_nodes
bf_rna
diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index ee1bdcf..74cfe90 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -123,6 +123,7 @@ typedef enum BVMDebugMode {
BVM_DEBUG_BVM_CODE,
BVM_DEBUG_LLVM_CODE,
BVM_DEBUG_LLVM_CODE_UNOPTIMIZED,
+ BVM_DEBUG_GLSL_CODE,
} BVMDebugMode;
/* ------------------------------------------------------------------------- */
diff --git a/source/blender/blenvm/CMakeLists.txt b/source/blender/blenvm/CMakeLists.txt
index 184a0ec..0f9f4f2 100644
--- a/source/blender/blenvm/CMakeLists.txt
+++ b/source/blender/blenvm/CMakeLists.txt
@@ -27,6 +27,7 @@ set(INC
.
bvm
compile
+ glsl
llvm
intern
util
@@ -111,6 +112,7 @@ endif()
add_subdirectory(bvm)
add_subdirectory(compile)
+add_subdirectory(glsl)
if(WITH_LLVM)
add_subdirectory(llvm)
diff --git a/source/blender/blenvm/glsl/CMakeLists.txt b/source/blender/blenvm/glsl/CMakeLists.txt
new file mode 100644
index 0000000..8e684e8
--- /dev/null
+++ b/source/blender/blenvm/glsl/CMakeLists.txt
@@ -0,0 +1,48 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# The Original Code is Copyright (C) 2016, Blender Foundation
+# All rights reserved.
+#
+# The Original Code is: all of this file.
+#
+# Contributor(s): Lukas Toenne.
+#
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+ .
+ ..
+ ../compile
+ ../intern
+ ../modules
+ ../util
+ ../../blenkernel
+ ../../blenlib
+ ../../makesdna
+ ../../makesrna
+ ../../../../intern/guardedalloc
+)
+
+set(INC_SYS
+)
+
+set(SRC
+ glsl_codegen.cc
+ glsl_codegen.h
+)
+
+blender_add_lib(bf_blenvm_glsl "${SRC}" "${INC}" "${INC_SYS}")
diff --git a/source/blender/blenvm/glsl/glsl_codegen.cc b/source/blender/blenvm/glsl/glsl_codegen.cc
new file mode 100644
index 0000000..2197f70
--- /dev/null
+++ b/source/blender/blenvm/glsl/glsl_codegen.cc
@@ -0,0 +1,150 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Lukas Toenne
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file glsl_codegen.cc
+ * \ingroup glsl
+ */
+
+#include <cstdio>
+#include <set>
+
+extern "C" {
+#include "BLI_utildefines.h"
+}
+
+#include "node_graph.h"
+
+#include "glsl_codegen.h"
+
+namespace blenvm {
+
+GLSLValue::GLSLValue(const string &name) :
+ m_name(name)
+{
+}
+
+GLSLValue::~GLSLValue()
+{
+}
+
+/* ------------------------------------------------------------------------- */
+
+GLSLCodeGenerator::GLSLCodeGenerator()
+{
+}
+
+GLSLCodeGenerator::~GLSLCodeGenerator()
+{
+}
+
+ValueHandle GLSLCodeGenerator::get_handle(const GLSLValue *value)
+{
+ return (ValueHandle)value;
+}
+
+GLSLValue *GLSLCodeGenerator::create_value(const TypeSpec *typespec, const string &name, bool make_unique)
+{
+ string varname = name;
+ if (make_unique) {
+ stringstream ss;
+ ss << varname << "_" << (m_values.size() + 1);
+ }
+
+ m_values.push_back(GLSLValue(varname));
+ GLSLValue *value = &m_values.back();
+
+ return value;
+}
+
+void GLSLCodeGenerator::finalize_function()
+{
+}
+
+void GLSLCodeGenerator::debug_function(FILE *file)
+{
+ string s = m_code.str();
+ fwrite(s.c_str(), sizeof(char), s.size(), file);
+}
+
+void GLSLCodeGenerator::node_graph_begin(const string &UNUSED(name), const NodeGraph *graph, bool UNUSED(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();
+ /* Note: argument names are unique! */
+ GLSLValue *value = create_value(typespec, input->name, false);
+ m_input_args.push_back(value);
+ }
+}
+
+void GLSLCodeGenerator::node_graph_end()
+{
+}
+
+void GLSLCodeGenerator::store_return_value(size_t output_index, const TypeSpec *typespec, ValueHandle value)
+{
+}
+
+ValueHandle GLSLCodeGenerator::map_argument(size_t input_index, const TypeSpec *UNUSED(typespec))
+{
+ GLSLValue *arg = m_input_args[input_index];
+ return get_handle(arg);
+}
+
+ValueHandle GLSLCodeGenerator::alloc_node_value(const TypeSpec *typespec, const string &name)
+{
+ GLSLValue *value = create_value(typespec, name, true);
+ return get_handle(value);
+}
+
+ValueHandle GLSLCodeGenerator::create_constant(const TypeSpec *typespec, const NodeConstant *node_value)
+{
+ GLSLValue *value = create_value(typespec, "constval", true);
+// value->set_constant_value(node_value);
+ return get_handle(value);
+}
+
+void GLSLCodeGenerator::eval_node(const NodeType *nodetype,
+ ArrayRef<ValueHandle> input_args,
+ ArrayRef<ValueHandle> output_args)
+{
+}
+
+
+void GLSLCodeGenerator::append(const string &s)
+{
+ m_code << s;
+}
+
+void GLSLCodeGenerator::newline()
+{
+ m_code << "\n";
+}
+
+} /* namespace blenvm */
diff --git a/source/blender/blenvm/glsl/glsl_codegen.h b/source/blender/blenvm/glsl/glsl_codegen.h
new file mode 100644
index 0000000..aecfe46
--- /dev/null
+++ b/source/blender/blenvm/glsl/glsl_codegen.h
@@ -0,0 +1,105 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Lukas Toenne
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef __GLSL_CODEGEN_H__
+#define __GLSL_CODEGEN_H__
+
+/** \file glsl_codegen.h
+ * \ingroup glsl
+ */
+
+#include <set>
+#include <vector>
+
+#include "MEM_guardedalloc.h"
+
+#include "compiler.h"
+#include "node_graph.h"
+
+#include "util_opcode.h"
+#include "util_string.h"
+
+namespace blenvm {
+
+struct NodeGraph;
+struct NodeInstance;
+struct TypeDesc;
+
+struct GLSLValue {
+ GLSLValue(const string &name);
+ ~GLSLValue();
+
+ const string &name() const { return m_name; }
+
+private:
+ string m_name;
+};
+
+struct GLSLCodeGenerator : public CodeGenerator {
+ typedef std::vector<GLSLValue> ValueStack;
+ typedef std::vector<GLSLValue*> Arguments;
+
+ GLSLCodeGenerator();
+ ~GLSLCodeGenerator();
+
+ const stringstream &code() const { return m_code; }
+
+ ValueHandle get_handle(const GLSLValue *value);
+
+ 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, const string &name);
+ 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:
+ GLSLValue *create_value(const TypeSpec *typespec, const string &name, bool make_unique);
+
+ void append(const string &s);
+ void newline();
+
+private:
+ ValueStack m_values;
+ Arguments m_input_args;
+ Arguments m_output_args;
+
+ stringstream m_code;
+};
+
+} /* namespace blenvm */
+
+#endif /* __GLSL_CODEGEN_H__ */
diff --git a/source/blender/blenvm/intern/bvm_api.cc b/source/blender/blenvm/intern/bvm_api.cc
index 32a51a4..2a9b493 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -38,6 +38,7 @@ extern "C" {
#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "BLI_listbase.h"
+#include "BLI_string.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
@@ -69,6 +70,8 @@ extern "C"
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list