]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programdata.h
Add 3x3 and 2x2 uniform matrices
[libs/gl.git] / source / programdata.h
index 4d80bb775afc92f20c05714c998399909a2299a7..3cef2d4c1b5cd921fa75c7f8d6dc449300151909 100644 (file)
@@ -17,7 +17,13 @@ class Vector3;
 class Vector4;
 
 /**
-Stores uniform variables for a shader program.
+Stores uniform variables for shader programs.  The uniforms are stored in a
+program-independent way, and UniformBlocks are created to match the uniform
+layouts of different programs.  If multiple programs have the same layout, the
+same block is used for them.
+
+The class is optimized for an access pattern where the set of uniforms and
+programs stays constants, with only the values changing.
 */
 class ProgramData
 {
@@ -35,10 +41,18 @@ public:
        };
 
 private:
+       enum Changes
+       {
+               NO_CHANGES,
+               VALUES_CHANGED,
+               KEYS_CHANGED
+       };
+
        struct Block
        {
-               bool dirty;
+               Changes changes;
                UniformBlock *block;
+               std::map<unsigned, const Uniform *const *> uniforms;
 
                Block();
        };
@@ -51,7 +65,7 @@ private:
        mutable BlockMap blocks;
        mutable UniformBlock *last_block;
        mutable Buffer *buffer;
-       mutable bool modified;
+       mutable Changes changes;
 
        ProgramData &operator=(const ProgramData &);
 public:
@@ -73,6 +87,8 @@ public:
        void uniform(const std::string &, const Vector4 &);
        void uniform(const std::string &, const Color &);
        void uniform4(const std::string &, const float *);
+       void uniform_matrix2(const std::string &, const float *);
+       void uniform_matrix3(const std::string &, const float *);
        void uniform_matrix4(const std::string &, const float *);
        void uniform_matrix4(const std::string &, const Matrix &);
        void uniform1_array(const std::string &, unsigned, const float *);
@@ -82,10 +98,17 @@ public:
        void uniform_matrix4_array(const std::string &, unsigned, const float *);
 
 private:
-       const UniformBlock &get_block(const Program &, const Program::UniformBlockInfo *) const;
+       void find_uniforms_for_block(Block &, const Program::UniformBlockInfo &) const;
+       UniformBlock *create_block(const Program::UniformBlockInfo &) const;
+       const UniformBlock *get_block(const Program &, const Program::UniformBlockInfo *) const;
+
 public:
-       const UniformBlock &get_block(const Program &, const std::string &) const;
+       /** Returns a UniformBlock matching the program's layout.  If name is empty,
+       uniforms for the default uniform block (outside any uniform block
+       declarations) are returned. */
+       const UniformBlock *get_block(const Program &prog, const std::string &name) const;
 
+       /// Creates blocks for the currently bound program and applies them.
        void apply() const;
 };