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