]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor the interface of SL::Compiler
authorMikko Rasa <tdb@tdb.fi>
Thu, 18 Feb 2021 11:31:48 +0000 (13:31 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 18 Feb 2021 11:31:48 +0000 (13:31 +0200)
source/core/program.cpp
source/glsl/compiler.cpp
source/glsl/compiler.h
source/resources/resources.cpp

index 74c1df3e3058ac069bda809cf96a9566bf1e2904..4cdbf97c0fcdf84431b1ba88044cc8bd41addb07 100644 (file)
@@ -36,12 +36,13 @@ Program::Program(const std::string &source)
        if(source.find(';')==string::npos && source.size()>5 && !source.compare(source.size()-5, 5, ".glsl"))
        {
                if(RefPtr<IO::Seekable> io = Resources::get_builtins().open(source))
        if(source.find(';')==string::npos && source.size()>5 && !source.compare(source.size()-5, 5, ".glsl"))
        {
                if(RefPtr<IO::Seekable> io = Resources::get_builtins().open(source))
-                       compiler.compile(*io, source);
+                       compiler.load_source(*io, source);
                else
                        throw IO::file_not_found(source);
        }
        else
                else
                        throw IO::file_not_found(source);
        }
        else
-               compiler.compile(source);
+               compiler.set_source(source);
+       compiler.compile();
        compiler.add_shaders(*this);
        link();
 }
        compiler.add_shaders(*this);
        link();
 }
index 21060cb4b783975f72da2a039de2f6a07b86888f..0579bb017267c08dfab0af9f177a8077e048244d 100644 (file)
@@ -9,6 +9,7 @@
 #include "generate.h"
 #include "optimize.h"
 #include "output.h"
 #include "generate.h"
 #include "optimize.h"
 #include "output.h"
+#include "resources.h"
 #include "shader.h"
 
 #undef interface
 #include "shader.h"
 
 #undef interface
@@ -20,7 +21,6 @@ namespace GL {
 namespace SL {
 
 Compiler::Compiler():
 namespace SL {
 
 Compiler::Compiler():
-       resources(0),
        module(0)
 { }
 
        module(0)
 { }
 
@@ -29,31 +29,47 @@ Compiler::~Compiler()
        delete module;
 }
 
        delete module;
 }
 
-void Compiler::compile(const string &source, const string &src_name)
+void Compiler::clear()
 {
 {
-       resources = 0;
        delete module;
        module = new Module();
        delete module;
        module = new Module();
+       imported_names.clear();
+}
+
+void Compiler::set_source(const string &source, const string &src_name)
+{
+       clear();
        Parser parser;
        imported_names.push_back(src_name);
        Parser parser;
        imported_names.push_back(src_name);
-       append_module(parser.parse(source, src_name, 1));
-       process();
+       append_module(parser.parse(source, src_name, 1), 0);
 }
 
 }
 
-void Compiler::compile(IO::Base &io, Resources *res, const string &src_name)
+void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
 {
 {
-       resources = res;
-       delete module;
-       module = new Module();
+       clear();
        Parser parser;
        imported_names.push_back(src_name);
        Parser parser;
        imported_names.push_back(src_name);
-       append_module(parser.parse(io, src_name, 1));
-       process();
+       append_module(parser.parse(io, src_name, 1), res);
 }
 
 }
 
-void Compiler::compile(IO::Base &io, const string &src_name)
+void Compiler::load_source(IO::Base &io, const string &src_name)
 {
 {
-       compile(io, 0, src_name);
+       load_source(io, 0, src_name);
+}
+
+void Compiler::compile()
+{
+       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(); )
+       {
+               if(optimize(*i))
+                       i = module->stages.begin();
+               else
+                       ++i;
+       }
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               finalize(*i);
 }
 
 void Compiler::add_shaders(Program &program)
 }
 
 void Compiler::add_shaders(Program &program)
@@ -124,11 +140,11 @@ void Compiler::add_shaders(Program &program)
        }
 }
 
        }
 }
 
-void Compiler::append_module(Module &mod)
+void Compiler::append_module(Module &mod, DataFile::Collection *res)
 {
        vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
        for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
 {
        vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
        for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
-               import((*i)->module);
+               import(res, (*i)->module);
        NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
 
        append_stage(mod.shared);
        NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
 
        append_stage(mod.shared);
@@ -165,22 +181,7 @@ void Compiler::append_stage(Stage &stage)
        DeclarationCombiner().apply(*target);
 }
 
        DeclarationCombiner().apply(*target);
 }
 
-void Compiler::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(); )
-       {
-               if(optimize(*i))
-                       i = module->stages.begin();
-               else
-                       ++i;
-       }
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-               finalize(*i);
-}
-
-void Compiler::import(const string &name)
+void Compiler::import(DataFile::Collection *resources, const string &name)
 {
        string fn = name+".glsl";
        if(find(imported_names, fn)!=imported_names.end())
 {
        string fn = name+".glsl";
        if(find(imported_names, fn)!=imported_names.end())
@@ -191,7 +192,7 @@ void Compiler::import(const string &name)
        if(!io)
                throw runtime_error(format("module %s not found", name));
        Parser import_parser;
        if(!io)
                throw runtime_error(format("module %s not found", name));
        Parser import_parser;
-       append_module(import_parser.parse(*io, fn, imported_names.size()));
+       append_module(import_parser.parse(*io, fn, imported_names.size()), resources);
 }
 
 void Compiler::generate(Stage &stage)
 }
 
 void Compiler::generate(Stage &stage)
index 2afc2f560bb00c17ecbdbfbc10f501ef11ad2813..8e83990613343c0fdc8510053ffaac1617d4d283 100644 (file)
@@ -4,7 +4,6 @@
 #include <vector>
 #include "parser.h"
 #include "program.h"
 #include <vector>
 #include "parser.h"
 #include "program.h"
-#include "resources.h"
 #include "syntax.h"
 
 namespace Msp {
 #include "syntax.h"
 
 namespace Msp {
@@ -14,7 +13,6 @@ namespace SL {
 class Compiler
 {
 private:
 class Compiler
 {
 private:
-       Resources *resources;
        Module *module;
        std::vector<std::string> imported_names;
 
        Module *module;
        std::vector<std::string> imported_names;
 
@@ -22,16 +20,19 @@ public:
        Compiler();
        ~Compiler();
 
        Compiler();
        ~Compiler();
 
-       void compile(const std::string &, const std::string & = "<string>");
-       void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
-       void compile(IO::Base &, const std::string &);
+private:
+       void clear();
+public:
+       void set_source(const std::string &, const std::string & = "<string>");
+       void load_source(IO::Base &, DataFile::Collection * = 0, const std::string & = "<file>");
+       void load_source(IO::Base &, const std::string &);
+       void compile();
        void add_shaders(Program &);
 
 private:
        void add_shaders(Program &);
 
 private:
-       void append_module(Module &);
+       void append_module(Module &, DataFile::Collection *);
        void append_stage(Stage &);
        void append_stage(Stage &);
-       void process();
-       void import(const std::string &);
+       void import(DataFile::Collection *, const std::string &);
        void generate(Stage &);
        bool optimize(Stage &);
        void finalize(Stage &);
        void generate(Stage &);
        bool optimize(Stage &);
        void finalize(Stage &);
index 11099ae077d935b8daf66318d37cf64e5eb768f4..114a08ecf838f0f6fd6fbf5c517fa64e9a07fe5f 100644 (file)
@@ -173,7 +173,8 @@ Program *Resources::create_program(const string &name)
        if(RefPtr<IO::Seekable> io = open_raw(name))
        {
                SL::Compiler compiler;
        if(RefPtr<IO::Seekable> io = open_raw(name))
        {
                SL::Compiler compiler;
-               compiler.compile(*io, this, name);
+               compiler.load_source(*io, this, name);
+               compiler.compile();
                RefPtr<Program> program = new Program;
                compiler.add_shaders(*program);
                program->link();
                RefPtr<Program> program = new Program;
                compiler.add_shaders(*program);
                program->link();