X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramdata.h;h=38803c571ee2e210451ccc6abb3abd41ab005b23;hb=904de4f7fd994886adbd3a6c03bc1b7c14ebc562;hp=885804f010e16d868e7ab551caccb1b144824395;hpb=8eb9a8d90e5597154dab666481037b306b7bbca2;p=libs%2Fgl.git diff --git a/source/programdata.h b/source/programdata.h index 885804f0..38803c57 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -3,6 +3,7 @@ #include #include +#include "program.h" namespace Msp { namespace GL { @@ -10,14 +11,19 @@ namespace GL { class Buffer; class Color; class Matrix; -class Program; class Uniform; class UniformBlock; 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 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: @@ -81,8 +95,18 @@ public: void uniform4_array(const std::string &, unsigned, const float *); void uniform_matrix4_array(const std::string &, unsigned, const float *); - const UniformBlock &get_block(const Program &, const std::string &) const; +private: + 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: + /** 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; };