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