From: Mikko Rasa Date: Sat, 12 Nov 2016 00:25:49 +0000 (+0200) Subject: Integrate ProgramCompiler with Program and Resources X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=fa196218c247aba70dd7fb17178fa7474121acec;p=libs%2Fgl.git Integrate ProgramCompiler with Program and Resources --- diff --git a/source/program.cpp b/source/program.cpp index 82308b34..2d298e82 100644 --- a/source/program.cpp +++ b/source/program.cpp @@ -14,6 +14,7 @@ #include "error.h" #include "misc.h" #include "program.h" +#include "programcompiler.h" #include "shader.h" using namespace std; @@ -35,6 +36,16 @@ Program::Program(const ProgramBuilder::StandardFeatures &features) link(); } +Program::Program(const std::string &source) +{ + init(); + + ProgramCompiler compiler; + compiler.compile(source); + compiler.add_shaders(*this); + link(); +} + Program::Program(const string &vert, const string &frag) { init(); diff --git a/source/program.h b/source/program.h index 53f060d2..a3fe57bf 100644 --- a/source/program.h +++ b/source/program.h @@ -80,6 +80,9 @@ public: /// Constructs a Program with standard features. Program(const ProgramBuilder::StandardFeatures &); + /// Constructs a Program from unified source code using ProgramCompiler. + Program(const std::string &); + /// Constructs a Program from vertex and fragment shader source code. Program(const std::string &, const std::string &); diff --git a/source/resources.cpp b/source/resources.cpp index 68b8fd5b..bba69315 100644 --- a/source/resources.cpp +++ b/source/resources.cpp @@ -9,6 +9,7 @@ #include "object.h" #include "pose.h" #include "program.h" +#include "programcompiler.h" #include "resourcemanager.h" #include "resources.h" #include "technique.h" @@ -35,7 +36,7 @@ Resources::Resources(): add_type().keyword("mesh").creator(&Resources::create_mesh); add_type().keyword("object"); add_type().keyword("pose"); - add_type().keyword("shader"); + add_type().keyword("shader").suffix(".glsl").creator(&Resources::create_program); add_type().suffix(".tech").keyword("technique"); add_type().base().suffix(".tex1d").keyword("texture1d"); add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d); @@ -107,5 +108,24 @@ Texture2D *Resources::create_texture2d(const string &name) return 0; } +Program *Resources::create_program(const string &name) +{ + string ext = FS::extpart(name); + if(ext==".shader") + return 0; + + if(RefPtr io = open_from_sources(name)) + { + ProgramCompiler compiler; + compiler.compile(*io); + RefPtr program = new Program; + compiler.add_shaders(*program); + program->link(); + return program.release(); + } + + return 0; +} + } // namespace GL } // namespace Msp diff --git a/source/resources.h b/source/resources.h index e939f7ce..1f3880aa 100644 --- a/source/resources.h +++ b/source/resources.h @@ -8,6 +8,7 @@ namespace Msp { namespace GL { class Mesh; +class Program; class ResourceManager; class Texture2D; @@ -38,6 +39,7 @@ public: protected: Mesh *create_mesh(const std::string &); Texture2D *create_texture2d(const std::string &); + Program *create_program(const std::string &); }; } // namespace GL