From 35646cafcdff8be76f6ef96018415d9ddec6f77c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 12 Apr 2018 00:02:38 +0300 Subject: [PATCH] Allow imported GLSL modules to import other modules --- source/programcompiler.cpp | 89 +++++++++++++++++++++++++------------- source/programcompiler.h | 5 ++- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 425941ba..e102349a 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -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 imports = apply >(module->shared); - for(list::iterator i=imports.end(); i!=imports.begin(); ) - import((*--i)->module); - apply(module->shared, set(imports.begin(), imports.end())); + list imports = apply >(mod.shared); + for(list::iterator i=imports.begin(); i!=imports.end(); ++i) + import((*i)->module); + apply(mod.shared, set(imports.begin(), imports.end())); + + append_stage(mod.shared); + for(list::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::iterator i; + for(i=module->stages.begin(); (i!=module->stages.end() && i->typestages.end() || i->type>stage.type) + { + list::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::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i) + target->content.body.push_back(*i); + apply(*target); +} +void ProgramCompiler::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(); ) @@ -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 = (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(module->shared); - for(list::iterator i=imported_module.stages.begin(); i!=imported_module.stages.end(); ++i) - { - list::iterator j; - for(j=module->stages.begin(); (j!=module->stages.end() && j->typetype); ++j) ; - if(j==module->stages.end() || j->type>i->type) - { - j = module->stages.insert(j, *i); - list::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(*j); - } - } + append_module(import_parser.parse(*io, fn)); } void ProgramCompiler::generate(Stage &stage) diff --git a/source/programcompiler.h b/source/programcompiler.h index fea7a73f..d7b8a870 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -370,11 +370,12 @@ private: }; Resources *resources; - ProgramParser parser; ProgramSyntax::Module *module; + std::set imported_names; public: ProgramCompiler(); + ~ProgramCompiler(); void compile(const std::string &, const std::string & = ""); void compile(IO::Base &, Resources * = 0, const std::string & = ""); @@ -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 &); -- 2.43.0