]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.h
Introduce a typedef for uniform layout hashes in Program
[libs/gl.git] / source / programdata.h
1 #ifndef MSP_GL_PROGRAMDATA_H_
2 #define MSP_GL_PROGRAMDATA_H_
3
4 #include <map>
5 #include <msp/datafile/objectloader.h>
6 #include "program.h"
7 #include "vector.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Buffer;
13 class Matrix;
14 class Uniform;
15 class UniformBlock;
16 struct Color;
17
18 /**
19 Stores uniform variables for shader programs.  The uniforms are stored in a
20 program-independent way, and UniformBlocks are created to match the uniform
21 layouts of different programs.  If multiple programs have the same layout, the
22 same block is used for them.
23
24 The class is optimized for an access pattern where the set of uniforms and
25 programs stays constants, with only the values changing.
26 */
27 class ProgramData
28 {
29 public:
30         class Loader: public DataFile::ObjectLoader<ProgramData>
31         {
32         public:
33                 Loader(ProgramData &);
34         private:
35                 void uniform1i(const std::string &, int);
36                 void uniform1f(const std::string &, float);
37                 void uniform2f(const std::string &, float, float);
38                 void uniform3f(const std::string &, float, float, float);
39                 void uniform4f(const std::string &, float, float, float, float);
40         };
41
42 private:
43         enum Changes
44         {
45                 NO_CHANGES,
46                 VALUES_CHANGED,
47                 KEYS_CHANGED
48         };
49
50         struct Block
51         {
52                 Changes changes;
53                 UniformBlock *block;
54                 std::map<unsigned, const Uniform *const *> uniforms;
55
56                 Block();
57         };
58
59         typedef std::map<std::string, Uniform *> UniformMap;
60         typedef std::map<Program::LayoutHash, Block> BlockMap;
61
62         // XXX All these mutables are a bit silly, but I'm out of better ideas
63         UniformMap uniforms;
64         mutable BlockMap blocks;
65         mutable UniformBlock *last_block;
66         mutable Buffer *buffer;
67         mutable Changes changes;
68
69 public:
70         ProgramData();
71         ProgramData(const ProgramData &);
72         ProgramData &operator=(const ProgramData &);
73         ~ProgramData();
74
75 private:
76         void uniform(const std::string &, Uniform *);
77 public:
78         void uniform(const std::string &, int);
79         void uniform(const std::string &, float);
80         void uniform(const std::string &, float, float);
81         void uniform2(const std::string &, const float *);
82         void uniform(const std::string &, float, float, float);
83         void uniform(const std::string &, const Vector3 &);
84         void uniform3(const std::string &, const float *);
85         void uniform(const std::string &, float, float, float, float);
86         void uniform(const std::string &, const Vector4 &);
87         void uniform(const std::string &, const Color &);
88         void uniform4(const std::string &, const float *);
89         void uniform_matrix2(const std::string &, const float *);
90         void uniform_matrix3(const std::string &, const float *);
91         void uniform(const std::string &, const Matrix &);
92         void uniform_matrix4(const std::string &, const float *);
93         void uniform1_array(const std::string &, unsigned, const float *);
94         void uniform2_array(const std::string &, unsigned, const float *);
95         void uniform3_array(const std::string &, unsigned, const float *);
96         void uniform4_array(const std::string &, unsigned, const float *);
97         void uniform_matrix4_array(const std::string &, unsigned, const float *);
98
99 private:
100         void find_uniforms_for_block(Block &, const Program::UniformBlockInfo &) const;
101         UniformBlock *create_block(const Program::UniformBlockInfo &) const;
102         const UniformBlock *get_block(const Program &, const Program::UniformBlockInfo *) const;
103
104 public:
105         /** Returns a UniformBlock matching the program's layout.  If name is empty,
106         uniforms for the default uniform block (outside any uniform block
107         declarations) are returned. */
108         const UniformBlock *get_block(const Program &prog, const std::string &name) const;
109
110         /// Creates blocks for the currently bound program and applies them.
111         void apply() const;
112 };
113
114 } // namespace GL
115 } // namespace Msp
116
117 #endif