X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramdata.h;h=3cef2d4c1b5cd921fa75c7f8d6dc449300151909;hb=8f7d5b6460ef68e7316c7f556b7152d9c9f7bfe2;hp=d74017b50ba237965f4aea15e9a8f295ca7e29f7;hpb=0070eec93efbf27bcc70720141d8730b059eb964;p=libs%2Fgl.git diff --git a/source/programdata.h b/source/programdata.h index d74017b5..3cef2d4c 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -3,20 +3,27 @@ #include #include +#include "program.h" namespace Msp { 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 { @@ -34,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(); }; @@ -45,9 +60,12 @@ private: typedef std::map UniformMap; typedef std::map BlockMap; + // XXX All these mutables are a bit silly, but I'm out of better ideas UniformMap uniforms; mutable BlockMap blocks; - mutable bool modified; + mutable UniformBlock *last_block; + mutable Buffer *buffer; + mutable Changes changes; ProgramData &operator=(const ProgramData &); public: @@ -69,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 *); @@ -77,8 +97,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; +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; };