]> git.tdb.fi Git - libs/gl.git/commitdiff
Implement an import system
authorMikko Rasa <tdb@tdb.fi>
Mon, 14 Nov 2016 12:22:02 +0000 (14:22 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 14 Nov 2016 12:22:02 +0000 (14:22 +0200)
This is the first of two major steps required to replicate the core
functionality of the old ProgramBuilder system, namely generating
standard shaders.  The other is conditionally including pieces of code,
which will be added soon.

source/programcompiler.cpp
source/programcompiler.h
source/programparser.cpp
source/programparser.h
source/programsyntax.cpp
source/programsyntax.h

index 63939409fc4c7d28c6f38cc031925cacb919257f..82a324dec52d8e7c9e92b8d8c56cb15264e423f3 100644 (file)
@@ -4,6 +4,7 @@
 #include "error.h"
 #include "program.h"
 #include "programcompiler.h"
+#include "resources.h"
 #include "shader.h"
 
 using namespace std;
@@ -34,17 +35,20 @@ namespace GL {
 using namespace ProgramSyntax;
 
 ProgramCompiler::ProgramCompiler():
+       resources(0),
        module(0)
 { }
 
 void ProgramCompiler::compile(const string &source)
 {
+       resources = 0;
        module = &parser.parse(source);
        process();
 }
 
-void ProgramCompiler::compile(IO::Base &io)
+void ProgramCompiler::compile(IO::Base &io, Resources *res)
 {
+       resources = res;
        module = &parser.parse(io);
        process();
 }
@@ -102,6 +106,11 @@ Stage *ProgramCompiler::get_builtins(StageType type)
 
 void ProgramCompiler::process()
 {
+       list<Import *> imports = apply<NodeGatherer<Import> >(module->shared);
+       for(list<Import *>::iterator i=imports.end(); i!=imports.begin(); )
+               import((*--i)->module);
+       apply<NodeRemover>(module->shared, set<Node *>(imports.begin(), imports.end()));
+
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
                generate(*i);
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
@@ -113,6 +122,39 @@ void ProgramCompiler::process()
        }
 }
 
+void ProgramCompiler::import(const string &name)
+{
+       if(!resources)
+               throw runtime_error("no resources");
+       RefPtr<IO::Seekable> io = resources->open_raw(name+".glsl");
+       if(!io)
+               throw runtime_error(format("module %s not found", name));
+       ProgramParser import_parser;
+       Module &imported_module = import_parser.parse(*io);
+
+       inject_block(module->shared.content, imported_module.shared.content);
+       apply<DeclarationCombiner>(module->shared);
+       for(list<Stage>::iterator i=imported_module.stages.begin(); i!=imported_module.stages.end(); ++i)
+       {
+               list<Stage>::iterator j;
+               for(j=module->stages.begin(); (j!=module->stages.end() && j->type<i->type); ++j) ;
+               if(j==module->stages.end() || j->type>i->type)
+               {
+                       j = module->stages.insert(j, *i);
+                       list<Stage>::iterator k = j;
+                       if(++k!=module->stages.end())
+                               k->previous = &*j;
+                       if(j!=module->stages.begin())
+                               j->previous = &*--(k=j);
+               }
+               else
+               {
+                       inject_block(j->content, i->content);
+                       apply<DeclarationCombiner>(*j);
+               }
+       }
+}
+
 void ProgramCompiler::generate(Stage &stage)
 {
        inject_block(stage.content, module->shared.content);
@@ -262,6 +304,11 @@ void ProgramCompiler::Formatter::visit(Block &block)
                formatted += format("\n%s}", string(brace_indent*2, ' '));
 }
 
+void ProgramCompiler::Formatter::visit(Import &import)
+{
+       formatted += format("import %s;", import.module);
+}
+
 void ProgramCompiler::Formatter::visit(Layout &layout)
 {
        formatted += "layout(";
@@ -327,7 +374,7 @@ void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
                (*i)->visit(*this);
        }
        formatted += ')';
-       if(func.definition)
+       if(func.definition==&func)
        {
                formatted += '\n';
                func.body.visit(*this);
@@ -376,6 +423,56 @@ void ProgramCompiler::Formatter::visit(Return &ret)
 }
 
 
+ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
+       toplevel(true),
+       remove_node(false)
+{ }
+
+void ProgramCompiler::DeclarationCombiner::visit(Block &block)
+{
+       if(!toplevel)
+               return;
+
+       SetForScope<bool> set(toplevel, false);
+       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               remove_node = false;
+               (*i)->visit(*this);
+               if(remove_node)
+                       block.body.erase(i++);
+               else
+                       ++i;
+       }
+}
+
+void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
+{
+       vector<FunctionDeclaration *> &decls = functions[func.name];
+       if(func.definition)
+       {
+               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+               {
+                       (*i)->definition = func.definition;
+                       (*i)->body.body.clear();
+               }
+       }
+       decls.push_back(&func);
+}
+
+void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
+{
+       VariableDeclaration *&ptr = variables[var.name];
+       if(ptr)
+       {
+               if(var.init_expression)
+                       ptr->init_expression = var.init_expression;
+               remove_node = true;
+       }
+       else
+               ptr = &var;
+}
+
+
 ProgramCompiler::VariableResolver::VariableResolver():
        anonymous(false),
        record_target(false),
index 2ef4aa7d9b77c5f853968da6e4e9dbc310c9abec..0c87acf0b953a40198b60299f4288b57a0c59cbe 100644 (file)
@@ -9,6 +9,7 @@ namespace Msp {
 namespace GL {
 
 class Program;
+class Resources;
 
 class ProgramCompiler
 {
@@ -48,6 +49,7 @@ private:
                virtual void visit(ProgramSyntax::Assignment &);
                virtual void visit(ProgramSyntax::FunctionCall &);
                virtual void visit(ProgramSyntax::ExpressionStatement &);
+               virtual void visit(ProgramSyntax::Import &);
                virtual void visit(ProgramSyntax::Layout &);
                virtual void visit(ProgramSyntax::StructDeclaration &);
                virtual void visit(ProgramSyntax::VariableDeclaration &);
@@ -58,6 +60,31 @@ private:
                virtual void visit(ProgramSyntax::Return &);
        };
 
+       template<typename T>
+       struct NodeGatherer: Visitor
+       {
+               typedef std::list<T *> ResultType;
+
+               std::list<T *> nodes;
+
+               const ResultType &get_result() const { return nodes; }
+               virtual void visit(T &n) { nodes.push_back(&n); }
+       };
+
+       struct DeclarationCombiner: Visitor
+       {
+               bool toplevel;
+               std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
+               std::map<std::string, ProgramSyntax::VariableDeclaration *> variables;
+               bool remove_node;
+
+               DeclarationCombiner();
+
+               virtual void visit(ProgramSyntax::Block &);
+               virtual void visit(ProgramSyntax::FunctionDeclaration &);
+               virtual void visit(ProgramSyntax::VariableDeclaration &);
+       };
+
        struct VariableResolver: Visitor
        {
                std::vector<ProgramSyntax::Block *> blocks;
@@ -158,6 +185,7 @@ private:
                virtual void visit(ProgramSyntax::VariableDeclaration &);
        };
 
+       Resources *resources;
        ProgramParser parser;
        ProgramSyntax::Module *module;
 
@@ -165,7 +193,7 @@ public:
        ProgramCompiler();
 
        void compile(const std::string &);
-       void compile(IO::Base &);
+       void compile(IO::Base &, Resources * = 0);
        void add_shaders(Program &);
 
 private:
@@ -173,6 +201,7 @@ private:
        static ProgramSyntax::Module &get_builtins_module();
        static ProgramSyntax::Stage *get_builtins(ProgramSyntax::StageType);
        void process();
+       void import(const std::string &);
        void generate(ProgramSyntax::Stage &);
        bool optimize(ProgramSyntax::Stage &);
        static void inject_block(ProgramSyntax::Block &, const ProgramSyntax::Block &);
index f11b5bf2e73bb570981072de07cf69d6c32e1824..ddea96409dea4475e911e94fac01fda171451b57 100644 (file)
@@ -332,7 +332,9 @@ bool ProgramParser::is_identifier(const string &token)
 Node *ProgramParser::parse_global_declaration()
 {
        string token = peek_token();
-       if(token=="layout")
+       if(token=="import")
+               return parse_import();
+       else if(token=="layout")
                return parse_layout();
        else if(token=="struct")
                return parse_struct_declaration();
@@ -383,6 +385,18 @@ Node *ProgramParser::parse_statement()
                throw runtime_error(format("Syntax error at '%s': expected a statement", token));
 }
 
+Import *ProgramParser::parse_import()
+{
+       if(cur_stage->type!=SHARED)
+               throw runtime_error("Imports are only allowed in the shared section");
+
+       expect("import");
+       RefPtr<Import> import = new Import;
+       import->module = parse_token();
+       expect(";");
+       return import.release();
+}
+
 Layout *ProgramParser::parse_layout()
 {
        expect("layout");
@@ -628,7 +642,7 @@ FunctionDeclaration *ProgramParser::parse_function_declaration()
        string token = peek_token();
        if(token=="{")
        {
-               func->definition = true;
+               func->definition = func.get();
                parse_block(func->body, true);
        }
        else if(token==";")
index e246251676bdd4b7b453b0c884c55c2da8ae4122..4e8e38f0fcd48fb0c5033ffae76bad2add532783 100644 (file)
@@ -76,6 +76,7 @@ private:
 
        ProgramSyntax::Node *parse_global_declaration();
        ProgramSyntax::Node *parse_statement();
+       ProgramSyntax::Import *parse_import();
        ProgramSyntax::Layout *parse_layout();
        void parse_block(ProgramSyntax::Block &, bool);
        ProgramSyntax::Expression *parse_expression(unsigned = 0);
index 83cfbb089b4d5d3278722b384c82a91a382aa4d1..f87488dc7a2b530a56c38841965430c0973f5738 100644 (file)
@@ -87,6 +87,12 @@ void ExpressionStatement::visit(NodeVisitor &visitor)
 }
 
 
+void Import::visit(NodeVisitor &visitor)
+{
+       visitor.visit(*this);
+}
+
+
 void Layout::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -130,7 +136,15 @@ void InterfaceBlock::visit(NodeVisitor &visitor)
 
 
 FunctionDeclaration::FunctionDeclaration():
-       definition(false)
+       definition(0)
+{ }
+
+FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
+       return_type(other.return_type),
+       name(other.name),
+       parameters(other.parameters),
+       definition(other.definition==&other ? this : other.definition),
+       body(other.body)
 { }
 
 void FunctionDeclaration::visit(NodeVisitor &visitor)
index 03b5aa8863d1e70af4e9be52383431ed9b331a96..9545760c47b31bd8cfa98f61ee59ef626d2d851c 100644 (file)
@@ -162,6 +162,14 @@ struct ExpressionStatement: Node
        virtual void visit(NodeVisitor &);
 };
 
+struct Import: Node
+{
+       std::string module;
+
+       virtual Import *clone() const { return new Import(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct Layout: Node
 {
        struct Qualifier
@@ -226,10 +234,11 @@ struct FunctionDeclaration: Node
        std::string return_type;
        std::string name;
        std::vector<NodePtr<VariableDeclaration> > parameters;
-       bool definition;
+       FunctionDeclaration *definition;
        Block body;
 
        FunctionDeclaration();
+       FunctionDeclaration(const FunctionDeclaration &);
 
        virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
        virtual void visit(NodeVisitor &);
@@ -286,6 +295,7 @@ struct NodeVisitor
        virtual void visit(Assignment &);
        virtual void visit(FunctionCall &) { }
        virtual void visit(ExpressionStatement &) { }
+       virtual void visit(Import &) { }
        virtual void visit(Layout &) { }
        virtual void visit(StructDeclaration &) { }
        virtual void visit(VariableDeclaration &) { }