]> git.tdb.fi Git - libs/gl.git/blob - source/render/programdata.h
e5c3e4f5259be76a3965f052e077a67bcaba1d34
[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 variables for shader programs.  The uniforms are stored in a
30 program-independent way, and UniformBlocks are created to match the uniform
31 layouts of different programs.  If multiple programs have the same layout, the
32 same block is used for them.
33
34 The class is optimized for an access pattern where the set of uniforms and
35 programs stays constants, with only the values changing.
36 */
37 class ProgramData
38 {
39 public:
40         class Loader: public DataFile::ObjectLoader<ProgramData>
41         {
42         public:
43                 Loader(ProgramData &);
44         private:
45                 void uniform1i(const std::string &, int);
46                 void uniform1f(const std::string &, float);
47                 void uniform2i(const std::string &, int, int);
48                 void uniform2f(const std::string &, float, float);
49                 void uniform3i(const std::string &, int, int, int);
50                 void uniform3f(const std::string &, float, float, float);
51                 void uniform4i(const std::string &, int, int, int, int);
52                 void uniform4f(const std::string &, float, float, float, float);
53                 void uniform_array_(const std::string &, DataType, unsigned);
54                 void uniform1i_array(const std::string &);
55                 void uniform1f_array(const std::string &);
56                 void uniform2i_array(const std::string &);
57                 void uniform2f_array(const std::string &);
58                 void uniform3i_array(const std::string &);
59                 void uniform3f_array(const std::string &);
60                 void uniform4i_array(const std::string &);
61                 void uniform4f_array(const std::string &);
62                 void uniform_array(const std::string &);
63         };
64
65 private:
66         class ArrayLoader: public DataFile::Loader
67         {
68         private:
69                 DataType type;
70                 unsigned element_size;
71                 std::vector<char> data;
72
73         public:
74                 ArrayLoader(DataType, unsigned);
75
76                 DataType get_data_type() const { return type; }
77                 unsigned get_element_size() const { return element_size; }
78                 const void *get_data() const { return &data[0]; }
79                 unsigned get_size() const { return data.size()/(4*element_size); }
80
81         private:
82                 void uniform(DataType, unsigned, const void *);
83                 void uniform1i(int);
84                 void uniform1f(float);
85                 void uniform2i(int, int);
86                 void uniform2f(float, float);
87                 void uniform3i(int, int, int);
88                 void uniform3f(float, float, float);
89                 void uniform4i(int, int, int, int);
90                 void uniform4f(float, float, float, float);
91         };
92
93         typedef unsigned Mask;
94
95         enum
96         {
97                 MASK_BITS = sizeof(Mask)*8,
98                 ALL_ONES = static_cast<Mask>(-1)
99         };
100
101         struct TaggedUniform
102         {
103                 Tag tag;
104                 DataType type;
105                 unsigned array_size;
106                 unsigned data_offset;
107                 unsigned data_size;
108
109                 TaggedUniform();
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;
157         mutable std::vector<SharedBlock> blocks;
158         mutable std::vector<ProgramBlock> programs;
159         mutable UniformBlock *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, 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, float);
178         void uniform(Tag, int, int);
179         void uniform(Tag, float, float);
180         void uniform2(Tag, const int *);
181         void uniform2(Tag, const float *);
182         void uniform(Tag, int, int, int);
183         void uniform(Tag, float, float, float);
184         void uniform3(Tag, const int *);
185         void uniform3(Tag, const float *);
186         void uniform(Tag, int, int, int, int);
187         void uniform(Tag, float, float, float, float);
188         void uniform(Tag, const Color &);
189         void uniform4(Tag, const int *);
190         void uniform4(Tag, const float *);
191         void uniform_matrix2(Tag, const float *);
192         void uniform_matrix3x2(Tag, const float *);
193         void uniform_matrix4x2(Tag, const float *);
194         void uniform_matrix2x3(Tag, const float *);
195         void uniform_matrix3(Tag, const float *);
196         void uniform_matrix4x3(Tag, const float *);
197         void uniform_matrix2x4(Tag, const float *);
198         void uniform_matrix3x4(Tag, const float *);
199         void uniform(Tag, const Matrix &);
200         void uniform_matrix4(Tag, const float *);
201         void uniform_array(Tag, unsigned, const int *);
202         void uniform_array(Tag, unsigned, const float *);
203         void uniform1_array(Tag, unsigned, const int *);
204         void uniform1_array(Tag, unsigned, const float *);
205         void uniform2_array(Tag, unsigned, const int *);
206         void uniform2_array(Tag, unsigned, const float *);
207         void uniform3_array(Tag, unsigned, const int *);
208         void uniform3_array(Tag, unsigned, const float *);
209         void uniform4_array(Tag, unsigned, const int *);
210         void uniform4_array(Tag, unsigned, const float *);
211         void uniform_matrix2_array(Tag, unsigned, const float *);
212         void uniform_matrix3x2_array(Tag, unsigned, const float *);
213         void uniform_matrix4x2_array(Tag, unsigned, const float *);
214         void uniform_matrix2x3_array(Tag, unsigned, const float *);
215         void uniform_matrix3_array(Tag, unsigned, const float *);
216         void uniform_matrix4x3_array(Tag, unsigned, const float *);
217         void uniform_matrix2x4_array(Tag, unsigned, const float *);
218         void uniform_matrix3x4_array(Tag, unsigned, const float *);
219         void uniform_matrix4_array(Tag, unsigned, const float *);
220
221         template<typename T, unsigned N>
222         void uniform(Tag, const LinAl::Vector<T, N> &);
223
224         template<typename T, unsigned R, unsigned C>
225         void uniform(Tag, const LinAl::Matrix<T, R, C> &);
226
227         template<typename T, unsigned N>
228         void uniform_array(Tag, unsigned, const LinAl::Vector<T, N> *);
229
230         template<typename T, unsigned R, unsigned C>
231         void uniform_array(Tag, unsigned, const LinAl::Matrix<T, R, C> *);
232
233         void remove_uniform(Tag);
234
235         unsigned get_generation() const { return generation; }
236
237         std::vector<Tag> get_uniform_tags() const;
238         void copy_uniform(const ProgramData &, Tag);
239
240 private:
241         int find_uniform_index(Tag) const;
242         std::vector<ProgramBlock>::iterator get_program(const Program &) const;
243         void update_block_uniform_indices(SharedBlock &, const ReflectData::UniformBlockInfo &) const;
244         void update_block(SharedBlock &, const ReflectData::UniformBlockInfo &) const;
245
246         std::vector<ProgramBlock>::const_iterator prepare_program(const Program &) const;
247 public:
248         void apply(const Program &, PipelineState &) const;
249
250         void set_debug_name(const std::string &);
251 };
252
253 template<typename T, unsigned N>
254 void ProgramData::uniform(Tag tag, const LinAl::Vector<T, N> &v)
255 { uniform(tag, TypeTraits<LinAl::Vector<T, N>>::type, 1, &v.x); }
256
257 template<typename T, unsigned R, unsigned C>
258 void ProgramData::uniform(Tag tag, const LinAl::Matrix<T, R, C> &v)
259 { uniform(tag, TypeTraits<LinAl::Matrix<T, R, C>>::type, 1, &v(0, 0)); }
260
261 template<typename T, unsigned N>
262 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector<T, N> *v)
263 { uniform(tag, TypeTraits<LinAl::Vector<T, N>>::type, n, &v[0].x); }
264
265 template<typename T, unsigned R, unsigned C>
266 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix<T, R, C> *v)
267 { uniform(tag, TypeTraits<LinAl::Matrix<T, R, C>>::type, n, &v[0](0, 0)); }
268
269 } // namespace GL
270 } // namespace Msp
271
272 #endif