]> git.tdb.fi Git - libs/gl.git/commitdiff
Add interfaces for using specialization constants with Programs
authorMikko Rasa <tdb@tdb.fi>
Fri, 26 Feb 2021 23:21:11 +0000 (01:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 26 Feb 2021 23:21:11 +0000 (01:21 +0200)
This replaces the old way of redefining variables withing the GLSL
source, and as such the Program constructor from string is now
deprecated.

source/core/program.cpp
source/core/program.h

index 2a34feb32706c161c602a151c7217d47955aa4d2..9ef5d6d7eebbf4eef2173b0b33abaec28379c37e 100644 (file)
@@ -55,6 +55,13 @@ Program::Program(const string &vert, const string &frag)
        link();
 }
 
+Program::Program(const Module &mod, const map<string, int> &spec_values)
+{
+       init();
+       add_stages(mod, spec_values);
+       link();
+}
+
 void Program::init()
 {
        static Require _req(ARB_shader_objects);
@@ -71,12 +78,13 @@ Program::~Program()
        glDeleteProgram(id);
 }
 
-void Program::add_stages(const Module &mod)
+void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
 {
        module = &mod;
 
        SL::Compiler compiler;
        compiler.set_source(module->get_prepared_source(), "<module>");
+       compiler.specialize(spec_values);
        compiler.compile(SL::Compiler::PROGRAM);
 
        vector<SL::Stage::Type> stages = compiler.get_stages();
@@ -479,7 +487,10 @@ void Program::Loader::finish()
 
 void Program::Loader::module(const string &n)
 {
-       obj.add_stages(get_collection().get<Module>(n));
+       map<string, int> spec_values;
+       SpecializationLoader ldr(spec_values);
+       load_sub_with(ldr);
+       obj.add_stages(get_collection().get<Module>(n), spec_values);
 }
 
 #pragma GCC diagnostic push
@@ -505,5 +516,30 @@ void Program::Loader::vertex_shader(const string &src)
 }
 #pragma GCC diagnostic pop
 
+
+DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
+
+Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
+       spec_values(sv)
+{
+       set_actions(shared_actions);
+}
+
+void Program::SpecializationLoader::init_actions()
+{
+       add("specialize", &SpecializationLoader::specialize_bool);
+       add("specialize", &SpecializationLoader::specialize_int);
+}
+
+void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
+{
+       spec_values[name] = value;
+}
+
+void Program::SpecializationLoader::specialize_int(const string &name, int value)
+{
+       spec_values[name] = value;
+}
+
 } // namespace GL
 } // namespace Msp
index e86ee0595f0ed307047681f74be256fbf06b3d70..052236270d4b54b3d0ca2b29ebecc908f8f82ea5 100644 (file)
@@ -36,6 +36,25 @@ public:
                void vertex_shader(const std::string &);
        };
 
+private:
+       class SpecializationLoader: public DataFile::Loader
+       {
+       private:
+               std::map<std::string, int> &spec_values;
+
+               static ActionMap shared_actions;
+
+       public:
+               SpecializationLoader(std::map<std::string, int> &);
+
+       private:
+               virtual void init_actions();
+
+               void specialize_bool(const std::string &, bool);
+               void specialize_int(const std::string &, int);
+       };
+
+public:
        typedef unsigned LayoutHash;
        struct UniformBlockInfo;
 
@@ -82,21 +101,24 @@ private:
        AttributeMap attributes;
 
 public:
-       /// Constructs an empty Program with no Shaders attached.
+       /// Constructs an empty Program with no shader stages attached.
        Program();
 
        /// Constructs a Program from unified source code using ProgramCompiler.
-       Program(const std::string &);
+       DEPRECATED Program(const std::string &);
 
        /// Constructs a Program from vertex and fragment shader source code.
        DEPRECATED Program(const std::string &, const std::string &);
 
+       /// Constructs a Program from a Module, with specialization constants.
+       Program(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
+
 private:
        void init();
 public:
        virtual ~Program();
 
-       void add_stages(const Module &);
+       void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
 
        DEPRECATED void attach_shader(Shader &shader);
        DEPRECATED void attach_shader_owned(Shader *shader);