From 73bef37da97b6da0b99227f63235cb52c4e56c44 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 18 Feb 2021 13:31:48 +0200 Subject: [PATCH] Refactor the interface of SL::Compiler --- source/core/program.cpp | 5 +-- source/glsl/compiler.cpp | 65 +++++++++++++++++----------------- source/glsl/compiler.h | 17 ++++----- source/resources/resources.cpp | 3 +- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/source/core/program.cpp b/source/core/program.cpp index 74c1df3e..4cdbf97c 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -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 = Resources::get_builtins().open(source)) - compiler.compile(*io, source); + compiler.load_source(*io, source); else throw IO::file_not_found(source); } else - compiler.compile(source); + compiler.set_source(source); + compiler.compile(); compiler.add_shaders(*this); link(); } diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 21060cb4..0579bb01 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -9,6 +9,7 @@ #include "generate.h" #include "optimize.h" #include "output.h" +#include "resources.h" #include "shader.h" #undef interface @@ -20,7 +21,6 @@ namespace GL { namespace SL { Compiler::Compiler(): - resources(0), module(0) { } @@ -29,31 +29,47 @@ Compiler::~Compiler() delete module; } -void Compiler::compile(const string &source, const string &src_name) +void Compiler::clear() { - resources = 0; 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); - 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); - 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::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) + generate(*i); + for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ) + { + if(optimize(*i)) + i = module->stages.begin(); + else + ++i; + } + for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) + finalize(*i); } 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 imports = NodeGatherer().apply(mod.shared); for(vector::iterator i=imports.begin(); i!=imports.end(); ++i) - import((*i)->module); + import(res, (*i)->module); NodeRemover(set(imports.begin(), imports.end())).apply(mod.shared); append_stage(mod.shared); @@ -165,22 +181,7 @@ void Compiler::append_stage(Stage &stage) DeclarationCombiner().apply(*target); } -void Compiler::process() -{ - for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) - generate(*i); - for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ) - { - if(optimize(*i)) - i = module->stages.begin(); - else - ++i; - } - for(list::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()) @@ -191,7 +192,7 @@ void Compiler::import(const string &name) 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) diff --git a/source/glsl/compiler.h b/source/glsl/compiler.h index 2afc2f56..8e839906 100644 --- a/source/glsl/compiler.h +++ b/source/glsl/compiler.h @@ -4,7 +4,6 @@ #include #include "parser.h" #include "program.h" -#include "resources.h" #include "syntax.h" namespace Msp { @@ -14,7 +13,6 @@ namespace SL { class Compiler { private: - Resources *resources; Module *module; std::vector imported_names; @@ -22,16 +20,19 @@ public: Compiler(); ~Compiler(); - void compile(const std::string &, const std::string & = ""); - void compile(IO::Base &, Resources * = 0, const std::string & = ""); - void compile(IO::Base &, const std::string &); +private: + void clear(); +public: + void set_source(const std::string &, const std::string & = ""); + void load_source(IO::Base &, DataFile::Collection * = 0, const std::string & = ""); + void load_source(IO::Base &, const std::string &); + void compile(); void add_shaders(Program &); private: - void append_module(Module &); + void append_module(Module &, DataFile::Collection *); 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 &); diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 11099ae0..114a08ec 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -173,7 +173,8 @@ Program *Resources::create_program(const string &name) if(RefPtr io = open_raw(name)) { SL::Compiler compiler; - compiler.compile(*io, this, name); + compiler.load_source(*io, this, name); + compiler.compile(); RefPtr program = new Program; compiler.add_shaders(*program); program->link(); -- 2.45.2