]> git.tdb.fi Git - libs/gl.git/blob - source/render/programdata.h
Use standard fixed-size integer types
[libs/gl.git] / source / render / programdata.h
1 #ifndef MSP_GL_PROGRAMDATA_H_
2 #define MSP_GL_PROGRAMDATA_H_
3
4 #include <map>
5 #include <stdexcept>
6 #include <msp/datafile/objectloader.h>
7 #include "datatype.h"
8 #include "matrix.h"
9 #include "program.h"
10 #include "tag.h"
11 #include "uniform.h"
12 #include "vector.h"
13
14 namespace Msp {
15 namespace GL {
16
17 class too_many_uniforms: public std::runtime_error
18 {
19 public:
20         too_many_uniforms(const std::string &w): std::runtime_error(w) { }
21         virtual ~too_many_uniforms() throw() { }
22 };
23
24 class Buffer;
25 class BufferBackedUniformBlock;
26 class PipelineState;
27 class UniformBlock;
28 struct Color;
29
30 /**
31 Stores uniform variables for shader programs.  The uniforms are stored in a
32 program-independent way, and UniformBlocks are created to match the uniform
33 layouts of different programs.  If multiple programs have the same layout, the
34 same block is used for them.
35
36 The class is optimized for an access pattern where the set of uniforms and
37 programs stays constants, with only the values changing.
38 */
39 class ProgramData
40 {
41 public:
42         class Loader: public DataFile::ObjectLoader<ProgramData>
43         {
44         public:
45                 Loader(ProgramData &);
46         private:
47                 void uniform1i(const std::string &, int);
48                 void uniform1f(const std::string &, float);
49                 void uniform2i(const std::string &, int, int);
50                 void uniform2f(const std::string &, float, float);
51                 void uniform3i(const std::string &, int, int, int);
52                 void uniform3f(const std::string &, float, float, float);
53                 void uniform4i(const std::string &, int, int, int, int);
54                 void uniform4f(const std::string &, float, float, float, float);
55                 void uniform_array_(const std::string &, DataType, unsigned);
56                 void uniform1i_array(const std::string &);
57                 void uniform1f_array(const std::string &);
58                 void uniform2i_array(const std::string &);
59                 void uniform2f_array(const std::string &);
60                 void uniform3i_array(const std::string &);
61                 void uniform3f_array(const std::string &);
62                 void uniform4i_array(const std::string &);
63                 void uniform4f_array(const std::string &);
64                 void uniform_array(const std::string &);
65         };
66
67 private:
68         class ArrayLoader: public DataFile::Loader
69         {
70         private:
71                 DataType type;
72                 unsigned element_size;
73                 std::vector<char> data;
74
75         public:
76                 ArrayLoader(DataType, unsigned);
77
78                 DataType get_data_type() const { return type; }
79                 unsigned get_element_size() const { return element_size; }
80                 const void *get_data() const { return &data[0]; }
81                 unsigned get_size() const { return data.size()/(4*element_size); }
82
83         private:
84                 void uniform(DataType, unsigned, const void *);
85                 void uniform1i(int);
86                 void uniform1f(float);
87                 void uniform2i(int, int);
88                 void uniform2f(float, float);
89                 void uniform3i(int, int, int);
90                 void uniform3f(float, float, float);
91                 void uniform4i(int, int, int, int);
92                 void uniform4f(float, float, float, float);
93         };
94
95         typedef unsigned Mask;
96
97         enum
98         {
99                 MASK_BITS = sizeof(Mask)*8,
100                 ALL_ONES = static_cast<Mask>(-1)
101         };
102
103         struct TaggedUniform
104         {
105                 Tag tag;
106                 Uniform *value;
107
108                 TaggedUniform();
109
110                 void replace_value(Uniform *);
111         };
112
113         struct SharedBlock
114         {
115                 Program::LayoutHash block_hash;
116                 Mask used;
117                 Mask dirty;
118                 UniformBlock *block;
119                 union
120                 {
121                         std::uint8_t type_flag;
122                         std::uint8_t values[16];
123                         struct
124                         {
125                                 std::uint8_t type_flag;
126                                 std::uint8_t *values;
127                         } dynamic;
128                 } indices;
129
130                 SharedBlock(Program::LayoutHash);
131
132                 const std::uint8_t *get_uniform_indices() const;
133         };
134
135         struct ProgramBlock
136         {
137                 Program::LayoutHash prog_hash;
138                 int bind_point;
139                 int block_index;
140                 union
141                 {
142                         UniformBlock *block;
143                         struct
144                         {
145                                 Mask used;
146                                 Mask dirty;
147                         } masks;
148                 };
149
150                 ProgramBlock(Program::LayoutHash);
151         };
152
153         // XXX All these mutables are a bit silly, but I'm out of better ideas
154         const Program *tied_program;
155         std::vector<TaggedUniform> uniforms;
156         unsigned generation;
157         mutable std::vector<SharedBlock> blocks;
158         mutable std::vector<ProgramBlock> programs;
159         mutable BufferBackedUniformBlock *last_buffer_block;
160         mutable Buffer *buffer;
161         mutable Mask dirty;
162         std::string debug_name;
163
164 public:
165         ProgramData(const Program * = 0);
166         ProgramData(const ProgramData &);
167         ProgramData(const ProgramData &, const Program *);
168         ProgramData &operator=(const ProgramData &);
169         ~ProgramData();
170
171 private:
172         void uniform(Tag, Uniform *);
173         template<typename T, typename V>
174         void uniform(Tag, V);
175         template<typename T, typename V>
176         void uniform_array(Tag, unsigned, V);
177         bool validate_tag(Tag) const;
178         void add_uniform(Tag, Uniform *);
179         void mark_dirty(Mask);
180 public:
181         void uniform(Tag, const Uniform &);
182         void uniform(Tag, int);
183         void uniform(Tag, float);
184         void uniform(Tag, int, int);
185         void uniform(Tag, float, float);
186         void uniform2(Tag, const int *);
187         void uniform2(Tag, const float *);
188         void uniform(Tag, int, int, int);
189         void uniform(Tag, float, float, float);
190         void uniform3(Tag, const int *);
191         void uniform3(Tag, const float *);
192         void uniform(Tag, int, int, int, int);
193         void uniform(Tag, float, float, float, float);
194         void uniform(Tag, const Color &);
195         void uniform4(Tag, const int *);
196         void uniform4(Tag, const float *);
197         void uniform_matrix2(Tag, const float *);
198         void uniform_matrix3x2(Tag, const float *);
199         void uniform_matrix4x2(Tag, const float *);
200         void uniform_matrix2x3(Tag, const float *);
201         void uniform_matrix3(Tag, const float *);
202         void uniform_matrix4x3(Tag, const float *);
203         void uniform_matrix2x4(Tag, const float *);
204         void uniform_matrix3x4(Tag, const float *);
205         void uniform(Tag, const Matrix &);
206         void uniform_matrix4(Tag, const float *);
207         void uniform_array(Tag, unsigned, const int *);
208         void uniform_array(Tag, unsigned, const float *);
209         void uniform1_array(Tag, unsigned, const int *);
210         void uniform1_array(Tag, unsigned, const float *);
211         void uniform2_array(Tag, unsigned, const int *);
212         void uniform2_array(Tag, unsigned, const float *);
213         void uniform3_array(Tag, unsigned, const int *);
214         void uniform3_array(Tag, unsigned, const float *);
215         void uniform4_array(Tag, unsigned, const int *);
216         void uniform4_array(Tag, unsigned, const float *);
217         void uniform_matrix2_array(Tag, unsigned, const float *);
218         void uniform_matrix3x2_array(Tag, unsigned, const float *);
219         void uniform_matrix4x2_array(Tag, unsigned, const float *);
220         void uniform_matrix2x3_array(Tag, unsigned, const float *);
221         void uniform_matrix3_array(Tag, unsigned, const float *);
222         void uniform_matrix4x3_array(Tag, unsigned, const float *);
223         void uniform_matrix2x4_array(Tag, unsigned, const float *);
224         void uniform_matrix3x4_array(Tag, unsigned, const float *);
225         void uniform_matrix4_array(Tag, unsigned, const float *);
226
227         template<typename T, unsigned N>
228         void uniform(Tag, const LinAl::Vector<T, N> &);
229
230         template<typename T, unsigned R, unsigned C>
231         void uniform(Tag, const LinAl::Matrix<T, R, C> &);
232
233         template<typename T, unsigned N>
234         void uniform_array(Tag, unsigned, const LinAl::Vector<T, N> *);
235
236         template<typename T, unsigned R, unsigned C>
237         void uniform_array(Tag, unsigned, const LinAl::Matrix<T, R, C> *);
238
239         void remove_uniform(Tag);
240
241         unsigned get_generation() const { return generation; }
242
243         std::vector<Tag> get_uniform_tags() const;
244         const Uniform &get_uniform(Tag) const;
245         const Uniform *find_uniform(Tag) const;
246
247 private:
248         int find_uniform_index(Tag) const;
249         std::vector<ProgramBlock>::iterator get_program(const Program &) const;
250         void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
251         void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
252
253         std::vector<ProgramBlock>::const_iterator prepare_program(const Program &) const;
254 public:
255         void apply(const Program &, PipelineState &) const;
256
257         void set_debug_name(const std::string &);
258 };
259
260 template<typename T, unsigned N>
261 void ProgramData::uniform(Tag tag, const LinAl::Vector<T, N> &v)
262 { uniform<UniformVector<T, N> >(tag, &v.x); }
263
264 template<typename T, unsigned R, unsigned C>
265 void ProgramData::uniform(Tag tag, const LinAl::Matrix<T, R, C> &v)
266 { uniform<UniformMatrix<T, R, C> >(tag, &v(0, 0)); }
267
268 template<typename T, unsigned N>
269 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector<T, N> *v)
270 { uniform_array<UniformVector<T, N> >(tag, n, &v[0].x); }
271
272 template<typename T, unsigned R, unsigned C>
273 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix<T, R, C> *v)
274 { uniform_array<UniformMatrix<T, R, C> >(tag, n, &v[0](0, 0)); }
275
276 } // namespace GL
277 } // namespace Msp
278
279 #endif