]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.h
Add support for non-square matrix uniforms
[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 "datatype.h"
7 #include "matrix.h"
8 #include "program.h"
9 #include "vector.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Buffer;
15 class Uniform;
16 class UniformBlock;
17 struct Color;
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 uniform2i(const std::string &, int, int);
39                 void uniform2f(const std::string &, float, float);
40                 void uniform3i(const std::string &, int, int, int);
41                 void uniform3f(const std::string &, float, float, float);
42                 void uniform4i(const std::string &, int, int, int, int);
43                 void uniform4f(const std::string &, float, float, float, float);
44                 void uniform_array_(const std::string &, DataType, unsigned);
45                 void uniform1i_array(const std::string &);
46                 void uniform1f_array(const std::string &);
47                 void uniform2i_array(const std::string &);
48                 void uniform2f_array(const std::string &);
49                 void uniform3i_array(const std::string &);
50                 void uniform3f_array(const std::string &);
51                 void uniform4i_array(const std::string &);
52                 void uniform4f_array(const std::string &);
53                 void uniform_array(const std::string &);
54         };
55
56 private:
57         class ArrayLoader: public DataFile::Loader
58         {
59         private:
60                 DataType type;
61                 unsigned element_size;
62                 std::vector<char> data;
63
64         public:
65                 ArrayLoader(DataType, unsigned);
66
67                 DataType get_data_type() const { return type; }
68                 unsigned get_element_size() const { return element_size; }
69                 const void *get_data() const { return &data[0]; }
70                 unsigned get_size() const { return data.size()/(4*element_size); }
71
72         private:
73                 void uniform(DataType, unsigned, const void *);
74                 void uniform1i(int);
75                 void uniform1f(float);
76                 void uniform2i(int, int);
77                 void uniform2f(float, float);
78                 void uniform3i(int, int, int);
79                 void uniform3f(float, float, float);
80                 void uniform4i(int, int, int, int);
81                 void uniform4f(float, float, float, float);
82         };
83
84         typedef unsigned Mask;
85
86         enum
87         {
88                 MASK_BITS = sizeof(Mask)*8,
89                 ALL_ONES = static_cast<Mask>(-1)
90         };
91
92         struct NamedUniform
93         {
94                 std::string name;
95                 Uniform *value;
96
97                 NamedUniform();
98
99                 bool compare_name(const std::string &, unsigned) const;
100                 void replace_value(Uniform *);
101         };
102
103         struct SharedBlock
104         {
105                 Mask used;
106                 Mask dirty;
107                 UniformBlock *block;
108                 union
109                 {
110                         UInt8 type_flag;
111                         UInt8 values[16];
112                         struct
113                         {
114                                 UInt8 type_flag;
115                                 UInt8 *values;
116                         } dynamic;
117                 } indices;
118
119                 SharedBlock(UniformBlock *);
120
121                 const UInt8 *get_uniform_indices() const;
122         };
123
124         struct ProgramBlock
125         {
126                 int bind_point;
127                 UniformBlock *block;
128                 SharedBlock *shared;
129
130                 ProgramBlock();
131                 ProgramBlock(int, SharedBlock *);
132         };
133
134         struct ProgramUniforms
135         {
136                 std::vector<ProgramBlock> blocks;
137                 Mask used;
138                 Mask dirty;
139
140                 ProgramUniforms();
141         };
142
143         typedef std::map<Program::LayoutHash, SharedBlock> BlockMap;
144         typedef std::map<Program::LayoutHash, ProgramUniforms> ProgramMap;
145
146         // XXX All these mutables are a bit silly, but I'm out of better ideas
147         const Program *tied_program;
148         std::vector<NamedUniform> uniforms;
149         mutable BlockMap blocks;
150         mutable ProgramMap programs;
151         mutable UniformBlock *last_block;
152         mutable Buffer *buffer;
153         mutable unsigned dirty;
154
155 public:
156         ProgramData(const Program * = 0);
157         ProgramData(const ProgramData &);
158         ProgramData &operator=(const ProgramData &);
159         ~ProgramData();
160
161 private:
162         void uniform(const std::string &, Uniform *);
163 public:
164         void uniform(const std::string &, const Uniform &);
165         void uniform(const std::string &, int);
166         void uniform(const std::string &, float);
167         void uniform(const std::string &, int, int);
168         void uniform(const std::string &, float, float);
169         void uniform2(const std::string &, const int *);
170         void uniform2(const std::string &, const float *);
171         void uniform(const std::string &, int, int, int);
172         void uniform(const std::string &, float, float, float);
173         void uniform(const std::string &, const Vector3 &);
174         void uniform3(const std::string &, const int *);
175         void uniform3(const std::string &, const float *);
176         void uniform(const std::string &, int, int, int, int);
177         void uniform(const std::string &, float, float, float, float);
178         void uniform(const std::string &, const Vector4 &);
179         void uniform(const std::string &, const Color &);
180         void uniform4(const std::string &, const int *);
181         void uniform4(const std::string &, const float *);
182         void uniform(const std::string &, const LinAl::Matrix<float, 2, 2> &);
183         void uniform_matrix2(const std::string &, const float *);
184         void uniform(const std::string &, const LinAl::Matrix<float, 2, 3> &);
185         void uniform_matrix3x2(const std::string &, const float *);
186         void uniform(const std::string &, const LinAl::Matrix<float, 2, 4> &);
187         void uniform_matrix4x2(const std::string &, const float *);
188         void uniform(const std::string &, const LinAl::Matrix<float, 3, 2> &);
189         void uniform_matrix2x3(const std::string &, const float *);
190         void uniform(const std::string &, const LinAl::Matrix<float, 3, 3> &);
191         void uniform_matrix3(const std::string &, const float *);
192         void uniform(const std::string &, const LinAl::Matrix<float, 3, 4> &);
193         void uniform_matrix4x3(const std::string &, const float *);
194         void uniform(const std::string &, const LinAl::Matrix<float, 4, 2> &);
195         void uniform_matrix2x4(const std::string &, const float *);
196         void uniform(const std::string &, const LinAl::Matrix<float, 4, 3> &);
197         void uniform_matrix3x4(const std::string &, const float *);
198         void uniform(const std::string &, const Matrix &);
199         void uniform_matrix4(const std::string &, const float *);
200         void uniform1_array(const std::string &, unsigned, const int *);
201         void uniform1_array(const std::string &, unsigned, const float *);
202         void uniform2_array(const std::string &, unsigned, const int *);
203         void uniform2_array(const std::string &, unsigned, const float *);
204         void uniform3_array(const std::string &, unsigned, const int *);
205         void uniform3_array(const std::string &, unsigned, const float *);
206         void uniform4_array(const std::string &, unsigned, const int *);
207         void uniform4_array(const std::string &, unsigned, const float *);
208         void uniform_matrix2_array(const std::string &, unsigned, const float *);
209         void uniform_matrix3x2_array(const std::string &, unsigned, const float *);
210         void uniform_matrix4x2_array(const std::string &, unsigned, const float *);
211         void uniform_matrix2x3_array(const std::string &, unsigned, const float *);
212         void uniform_matrix3_array(const std::string &, unsigned, const float *);
213         void uniform_matrix4x3_array(const std::string &, unsigned, const float *);
214         void uniform_matrix2x4_array(const std::string &, unsigned, const float *);
215         void uniform_matrix3x4_array(const std::string &, unsigned, const float *);
216         void uniform_matrix4_array(const std::string &, unsigned, const float *);
217         void remove_uniform(const std::string &);
218
219         std::vector<std::string> get_uniform_names() const;
220         const Uniform &get_uniform(const std::string &) const;
221
222 private:
223         static bool uniform_name_compare(const NamedUniform &, const std::string &);
224         int find_uniform_index(const std::string &) const;
225         void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
226         void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
227         SharedBlock *get_shared_block(const Program::UniformBlockInfo &) const;
228
229 public:
230         /** Applies uniform blocks for the currently bound program, creating them
231         if needed. */
232         void apply() const;
233 };
234
235 } // namespace GL
236 } // namespace Msp
237
238 #endif