]> git.tdb.fi Git - libs/gl.git/commitdiff
Allow imported GLSL modules to import other modules
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Apr 2018 21:02:38 +0000 (00:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Apr 2018 21:02:38 +0000 (00:02 +0300)
source/programcompiler.cpp
source/programcompiler.h

index 425941baff567e454f8d9ce1390e0b93731cfea4..e102349a6482f0dc4a679ee325561210948dd52b 100644 (file)
@@ -40,17 +40,30 @@ ProgramCompiler::ProgramCompiler():
        module(0)
 { }
 
+ProgramCompiler::~ProgramCompiler()
+{
+       delete module;
+}
+
 void ProgramCompiler::compile(const string &source, const string &src_name)
 {
        resources = 0;
-       module = &parser.parse(source, src_name);
+       delete module;
+       module = new Module();
+       ProgramParser parser;
+       imported_names.insert(src_name);
+       append_module(parser.parse(source, src_name));
        process();
 }
 
 void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
 {
        resources = res;
-       module = &parser.parse(io, src_name);
+       delete module;
+       module = new Module();
+       ProgramParser parser;
+       imported_names.insert(src_name);
+       append_module(parser.parse(io, src_name));
        process();
 }
 
@@ -115,13 +128,47 @@ Stage *ProgramCompiler::get_builtins(StageType type)
        return 0;
 }
 
-void ProgramCompiler::process()
+void ProgramCompiler::append_module(ProgramSyntax::Module &mod)
 {
-       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()));
+       list<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
+       for(list<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
+               import((*i)->module);
+       apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
+
+       append_stage(mod.shared);
+       for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
+               append_stage(*i);
+}
+
+void ProgramCompiler::append_stage(Stage &stage)
+{
+       Stage *target = 0;
+       if(stage.type==SHARED)
+               target = &module->shared;
+       else
+       {
+               list<Stage>::iterator i;
+               for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
+               if(i==module->stages.end() || i->type>stage.type)
+               {
+                       list<Stage>::iterator j = module->stages.insert(i, stage.type);
+                       if(i!=module->stages.end())
+                               i->previous = &*j;
+                       i = j;
+                       if(i!=module->stages.begin())
+                               i->previous = &*--j;
+               }
+
+               target = &*i;
+       }
+
+       for(NodeList<Node>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
+               target->content.body.push_back(*i);
+       apply<DeclarationCombiner>(*target);
+}
 
+void ProgramCompiler::process()
+{
        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(); )
@@ -138,33 +185,15 @@ void ProgramCompiler::process()
 void ProgramCompiler::import(const string &name)
 {
        string fn = name+".glsl";
+       if(imported_names.count(fn))
+               return;
+       imported_names.insert(fn);
+
        RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
        if(!io)
                throw runtime_error(format("module %s not found", name));
        ProgramParser import_parser;
-       Module &imported_module = import_parser.parse(*io, fn);
-
-       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);
-               }
-       }
+       append_module(import_parser.parse(*io, fn));
 }
 
 void ProgramCompiler::generate(Stage &stage)
index fea7a73fb78c3e8a0b68d12c7a9aa25f73018840..d7b8a8701ff0f8687cf871e59f000ba18c134ed2 100644 (file)
@@ -370,11 +370,12 @@ private:
        };
 
        Resources *resources;
-       ProgramParser parser;
        ProgramSyntax::Module *module;
+       std::set<std::string> imported_names;
 
 public:
        ProgramCompiler();
+       ~ProgramCompiler();
 
        void compile(const std::string &, const std::string & = "<string>");
        void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
@@ -385,6 +386,8 @@ private:
        static ProgramSyntax::Module *create_builtins_module();
        static ProgramSyntax::Module &get_builtins_module();
        static ProgramSyntax::Stage *get_builtins(ProgramSyntax::StageType);
+       void append_module(ProgramSyntax::Module &);
+       void append_stage(ProgramSyntax::Stage &);
        void process();
        void import(const std::string &);
        void generate(ProgramSyntax::Stage &);