]> git.tdb.fi Git - libs/gl.git/blob - source/materials/programdata.h
dbbf5f488d6173e2b9436df925d81bdce2db8f16
[libs/gl.git] / source / materials / programdata.h
1 #ifndef MSP_GL_PROGRAMDATA_H_
2 #define MSP_GL_PROGRAMDATA_H_
3
4 #include <stdexcept>
5 #include <msp/core/noncopyable.h>
6 #include <msp/datafile/objectloader.h>
7 #include "datatype.h"
8 #include "matrix.h"
9 #include "reflectdata.h"
10 #include "tag.h"
11 #include "vector.h"
12
13 namespace Msp {
14 namespace GL {
15
16 class too_many_uniforms: public std::runtime_error
17 {
18 public:
19         too_many_uniforms(const std::string &w): std::runtime_error(w) { }
20         virtual ~too_many_uniforms() throw() { }
21 };
22
23 class Buffer;
24 class PipelineState;
25 class Program;
26 class UniformBlock;
27 struct Color;
28
29 /**
30 Stores uniform values for shader programs.
31
32 The uniforms are stored in a program-independent way, and UniformBlocks are
33 created to match the uniform layouts of different programs.  If multiple
34 programs have the same layout, the 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: public NonCopyable
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                 DataType type = VOID;
107                 unsigned array_size = 0;
108                 unsigned data_offset = 0;
109                 unsigned data_size = 0;
110         };
111
112         struct SharedBlock
113         {
114                 ReflectData::LayoutHash block_hash;
115                 Mask used;
116                 Mask dirty;
117                 UniformBlock *block;
118                 union
119                 {
120                         std::uint8_t type_flag;
121                         std::uint8_t values[16];
122                         struct
123                         {
124                                 std::uint8_t type_flag;
125                                 std::uint8_t *values;
126                         } dynamic;
127                 } indices;
128
129                 SharedBlock(ReflectData::LayoutHash);
130
131                 const std::uint8_t *get_uniform_indices() const;
132         };
133
134         struct ProgramBlock
135         {
136                 ReflectData::LayoutHash prog_hash;
137                 int bind_point;
138                 int block_index;
139                 union
140                 {
141                         UniformBlock *block;
142                         struct
143                         {
144                                 Mask used;
145                                 Mask dirty;
146                         } masks;
147                 };
148
149                 ProgramBlock(ReflectData::LayoutHash);
150         };
151
152         // XXX All these mutables are a bit silly, but I'm out of better ideas
153         const Program *tied_program;
154         std::vector<TaggedUniform> uniforms;
155         std::vector<char> uniform_data;
156         unsigned generation = 0;
157         mutable std::vector<SharedBlock> blocks;
158         mutable std::vector<ProgramBlock> programs;
159         mutable UniformBlock *last_buffer_block = 0;
160         mutable Buffer *buffer = 0;
161         bool streaming = false;
162         mutable Mask dirty = 0;
163         std::string debug_name;
164
165 public:
166         ProgramData(const Program * = 0);
167         ProgramData(ProgramData &&);
168         ~ProgramData();
169
170 private:
171         void uniform(Tag, DataType, unsigned, const void *);
172         bool validate_tag(Tag) const;
173         void mark_dirty(Mask);
174 public:
175         void uniform(Tag, int);
176         void uniform(Tag, unsigned);
177         void uniform(Tag, float);
178         void uniform(Tag, int, int);
179         void uniform(Tag, unsigned, unsigned);
180         void uniform(Tag, float, float);
181         void uniform2(Tag, const int *);
182         void uniform2(Tag, const unsigned *);
183         void uniform2(Tag, const float *);
184         void uniform(Tag, int, int, int);
185         void uniform(Tag, unsigned, unsigned, unsigned);
186         void uniform(Tag, float, float, float);
187         void uniform3(Tag, const int *);
188         void uniform3(Tag, const unsigned *);
189         void uniform3(Tag, const float *);
190         void uniform(Tag, int, int, int, int);
191         void uniform(Tag, unsigned, unsigned, unsigned, unsigned);
192         void uniform(Tag, float, float, float, float);
193         void uniform(Tag, const Color &);
194         void uniform4(Tag, const int *);
195         void uniform4(Tag, const unsigned *);
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 unsigned *);
209         void uniform_array(Tag, unsigned, const float *);
210         void uniform1_array(Tag, unsigned, const int *);
211         void uniform1_array(Tag, unsigned, const unsigned *);
212         void uniform1_array(Tag, unsigned, const float *);
213         void uniform2_array(Tag, unsigned, const int *);
214         void uniform2_array(Tag, unsigned, const unsigned *);
215         void uniform2_array(Tag, unsigned, const float *);
216         void uniform3_array(Tag, unsigned, const int *);
217         void uniform3_array(Tag, unsigned, const unsigned *);
218         void uniform3_array(Tag, unsigned, const float *);
219         void uniform4_array(Tag, unsigned, const int *);
220         void uniform4_array(Tag, unsigned, const unsigned *);
221         void uniform4_array(Tag, unsigned, const float *);
222         void uniform_matrix2_array(Tag, unsigned, const float *);
223         void uniform_matrix3x2_array(Tag, unsigned, const float *);
224         void uniform_matrix4x2_array(Tag, unsigned, const float *);
225         void uniform_matrix2x3_array(Tag, unsigned, const float *);
226         void uniform_matrix3_array(Tag, unsigned, const float *);
227         void uniform_matrix4x3_array(Tag, unsigned, const float *);
228         void uniform_matrix2x4_array(Tag, unsigned, const float *);
229         void uniform_matrix3x4_array(Tag, unsigned, const float *);
230         void uniform_matrix4_array(Tag, unsigned, const float *);
231
232         template<typename T, unsigned N>
233         void uniform(Tag, const LinAl::Vector<T, N> &);
234
235         template<typename T, unsigned R, unsigned C>
236         void uniform(Tag, const LinAl::Matrix<T, R, C> &);
237
238         template<typename T, unsigned N>
239         void uniform_array(Tag, unsigned, const LinAl::Vector<T, N> *);
240
241         template<typename T, unsigned R, unsigned C>
242         void uniform_array(Tag, unsigned, const LinAl::Matrix<T, R, C> *);
243
244         void remove_uniform(Tag);
245
246         unsigned get_generation() const { return generation; }
247
248         std::vector<Tag> get_uniform_tags() const;
249         void copy_uniform(const ProgramData &, Tag);
250         void copy_uniforms(const ProgramData &);
251
252 private:
253         int find_uniform_index(Tag) const;
254         std::vector<ProgramBlock>::iterator get_program(const Program &) const;
255         void recreate_buffer() const;
256         void update_block_uniform_indices(SharedBlock &, const ReflectData::UniformBlockInfo &) const;
257         void update_block(SharedBlock &, const ReflectData::UniformBlockInfo &) const;
258
259         std::vector<ProgramBlock>::const_iterator prepare_program(const Program &) const;
260 public:
261         /** Creates or updates UniformBlocks for a specific program if necessary,
262         then sets them to the PipelineState. */
263         void apply(const Program &, PipelineState &, unsigned) const;
264
265         void set_debug_name(const std::string &);
266 };
267
268 template<typename T, unsigned N>
269 void ProgramData::uniform(Tag tag, const LinAl::Vector<T, N> &v)
270 { uniform(tag, TypeTraits<LinAl::Vector<T, N>>::type, 1, &v.x); }
271
272 template<typename T, unsigned R, unsigned C>
273 void ProgramData::uniform(Tag tag, const LinAl::Matrix<T, R, C> &v)
274 { uniform(tag, TypeTraits<LinAl::Matrix<T, R, C>>::type, 1, &v(0, 0)); }
275
276 template<typename T, unsigned N>
277 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector<T, N> *v)
278 { uniform(tag, TypeTraits<LinAl::Vector<T, N>>::type, n, &v[0].x); }
279
280 template<typename T, unsigned R, unsigned C>
281 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix<T, R, C> *v)
282 { uniform(tag, TypeTraits<LinAl::Matrix<T, R, C>>::type, n, &v[0](0, 0)); }
283
284 } // namespace GL
285 } // namespace Msp
286
287 #endif