From c93e0613e96ec6817e26b533e90dc49d45787941 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 30 Mar 2021 14:55:01 +0300 Subject: [PATCH] Split Module into a base class and format-specific class --- source/core/module.cpp | 6 ++---- source/core/module.h | 29 ++++++++++++++++++++++++----- source/core/program.cpp | 28 +++++++++++++++++++--------- source/core/program.h | 8 ++++---- source/resources/resources.cpp | 13 ++++++++++--- 5 files changed, 59 insertions(+), 25 deletions(-) diff --git a/source/core/module.cpp b/source/core/module.cpp index 009733a9..74e886b0 100644 --- a/source/core/module.cpp +++ b/source/core/module.cpp @@ -7,9 +7,6 @@ using namespace std; namespace Msp { namespace GL { -Module::Module() -{ } - void Module::set_source(const string &src) { SL::Compiler compiler; @@ -29,7 +26,8 @@ void Module::load_source(IO::Base &io, const string &name) load_source(io, 0, name); } -void Module::compile(SL::Compiler &compiler) + +void GlslModule::compile(SL::Compiler &compiler) { compiler.compile(SL::Compiler::MODULE); prepared_source = compiler.get_combined_glsl(); diff --git a/source/core/module.h b/source/core/module.h index f65f739a..78afcfc9 100644 --- a/source/core/module.h +++ b/source/core/module.h @@ -13,18 +13,37 @@ class Resources; class Module { -private: - std::string prepared_source; - SL::SourceMap source_map; +public: + enum Format + { + GLSL, + }; +protected: + Module() { } public: - Module(); + virtual ~Module() { } + + virtual Format get_format() const = 0; void set_source(const std::string &); void load_source(IO::Base &, Resources *, const std::string &); void load_source(IO::Base &, const std::string &); private: - void compile(SL::Compiler &); + virtual void compile(SL::Compiler &) = 0; +}; + +class GlslModule: public Module +{ +private: + std::string prepared_source; + SL::SourceMap source_map; + +public: + virtual Format get_format() const { return GLSL; } + +private: + virtual void compile(SL::Compiler &); public: const std::string &get_prepared_source() const { return prepared_source; } diff --git a/source/core/program.cpp b/source/core/program.cpp index 6cad69c4..789fa324 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -35,7 +35,7 @@ Program::Program(const std::string &source) { init(); - Module mod; + GlslModule mod; mod.set_source(source); add_stages(mod); @@ -78,6 +78,15 @@ Program::~Program() glDeleteProgram(id); } +void Program::add_stages(const Module &mod, const map &spec_values) +{ + switch(mod.get_format()) + { + case Module::GLSL: return add_glsl_stages(static_cast(mod), spec_values); + default: throw invalid_argument("Program::add_stages"); + } +} + unsigned Program::add_stage(GLenum type) { switch(type) @@ -95,12 +104,12 @@ unsigned Program::add_stage(GLenum type) return stage_id; } -void Program::add_stages(const Module &mod, const map &spec_values) +void Program::add_glsl_stages(const GlslModule &mod, const map &spec_values) { module = &mod; SL::Compiler compiler; - compiler.set_source(module->get_prepared_source(), ""); + compiler.set_source(mod.get_prepared_source(), ""); compiler.specialize(spec_values); compiler.compile(SL::Compiler::PROGRAM); #ifdef DEBUG @@ -140,11 +149,11 @@ void Program::add_stages(const Module &mod, const map &spec_values) glBindFragDataLocation(id, j->second, j->first.c_str()); } - compile_stage(stage_id); + compile_glsl_stage(stage_id); } } -void Program::compile_stage(unsigned stage_id) +void Program::compile_glsl_stage(unsigned stage_id) { glCompileShader(stage_id); bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS); @@ -153,8 +162,8 @@ void Program::compile_stage(unsigned stage_id) string info_log(info_log_len+1, 0); glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]); info_log.erase(info_log_len); - if(module) - info_log = module->get_source_map().translate_errors(info_log); + if(module && module->get_format()==Module::GLSL) + info_log = static_cast(module)->get_source_map().translate_errors(info_log); if(!compiled) throw compile_error(info_log); @@ -172,7 +181,7 @@ void Program::attach_shader(Shader &shader) if(!shader_id) throw invalid_argument("Program::attach_shader"); stage_ids.push_back(shader_id); - compile_stage(shader_id); + compile_glsl_stage(shader_id); } void Program::attach_shader_owned(Shader *shader) @@ -220,7 +229,8 @@ void Program::link() string info_log(info_log_len+1, 0); glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]); info_log.erase(info_log_len); - info_log = module->get_source_map().translate_errors(info_log); + if(module && module->get_format()==Module::GLSL) + info_log = static_cast(module)->get_source_map().translate_errors(info_log); if(!linked) throw compile_error(info_log); diff --git a/source/core/program.h b/source/core/program.h index 7038468b..9fa75747 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -11,6 +11,7 @@ namespace Msp { namespace GL { +class GlslModule; class Module; class Shader; @@ -118,12 +119,11 @@ private: public: virtual ~Program(); -private: - unsigned add_stage(GLenum); -public: void add_stages(const Module &, const std::map & = std::map()); private: - void compile_stage(unsigned); + unsigned add_stage(GLenum); + void add_glsl_stages(const GlslModule &, const std::map &); + void compile_glsl_stage(unsigned); public: DEPRECATED void attach_shader(Shader &shader); diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 68d9739e..bb3243f6 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -174,11 +174,18 @@ Texture2D *Resources::create_texture2d(const string &name) Module *Resources::create_module(const string &name) { + string ext = FS::extpart(name); + if(ext!=".glsl") + return 0; + if(RefPtr io = open_raw(name)) { - RefPtr module = new Module; - module->load_source(*io, this, name); - return module.release(); + if(ext==".glsl") + { + RefPtr module = new GlslModule; + module->load_source(*io, this, name); + return module.release(); + } } return 0; -- 2.43.0