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