]> git.tdb.fi Git - libs/gl.git/blob - source/render/programdata.h
Split UniformBlock into two classes
[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 "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 BufferBackedUniformBlock;
25 class Uniform;
26 class UniformBlock;
27 struct Color;
28
29 /**
30 Stores uniform variables for shader programs.  The uniforms are stored in a
31 program-independent way, and UniformBlocks are created to match the uniform
32 layouts of different programs.  If multiple programs have the same layout, the
33 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                 Uniform *value;
106
107                 TaggedUniform();
108
109                 void replace_value(Uniform *);
110         };
111
112         struct SharedBlock
113         {
114                 Program::LayoutHash block_hash;
115                 Mask used;
116                 Mask dirty;
117                 UniformBlock *block;
118                 union
119                 {
120                         UInt8 type_flag;
121                         UInt8 values[16];
122                         struct
123                         {
124                                 UInt8 type_flag;
125                                 UInt8 *values;
126                         } dynamic;
127                 } indices;
128
129                 SharedBlock(Program::LayoutHash);
130
131                 const UInt8 *get_uniform_indices() const;
132         };
133
134         struct ProgramBlock
135         {
136                 Program::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(Program::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         mutable std::vector<SharedBlock> blocks;
156         mutable std::vector<ProgramBlock> programs;
157         mutable BufferBackedUniformBlock *last_buffer_block;
158         mutable Buffer *buffer;
159         mutable Mask dirty;
160
161 public:
162         ProgramData(const Program * = 0);
163         ProgramData(const ProgramData &);
164         ProgramData(const ProgramData &, const Program *);
165         ProgramData &operator=(const ProgramData &);
166         ~ProgramData();
167
168 private:
169         void uniform(Tag, Uniform *);
170         template<typename T, typename V>
171         void uniform(Tag, V);
172         template<typename T, typename V>
173         void uniform_array(Tag, unsigned, V);
174         bool validate_tag(Tag) const;
175         void add_uniform(Tag, Uniform *);
176 public:
177         void uniform(Tag, const Uniform &);
178         void uniform(Tag, int);
179         void uniform(Tag, float);
180         void uniform(Tag, int, int);
181         void uniform(Tag, float, float);
182         void uniform2(Tag, const int *);
183         void uniform2(Tag, const float *);
184         void uniform(Tag, int, int, int);
185         void uniform(Tag, float, float, float);
186         void uniform(Tag, const Vector3 &);
187         void uniform3(Tag, const int *);
188         void uniform3(Tag, const float *);
189         void uniform(Tag, int, int, int, int);
190         void uniform(Tag, float, float, float, float);
191         void uniform(Tag, const Vector4 &);
192         void uniform(Tag, const Color &);
193         void uniform4(Tag, const int *);
194         void uniform4(Tag, const float *);
195         void uniform(Tag, const LinAl::Matrix<float, 2, 2> &);
196         void uniform_matrix2(Tag, const float *);
197         void uniform(Tag, const LinAl::Matrix<float, 2, 3> &);
198         void uniform_matrix3x2(Tag, const float *);
199         void uniform(Tag, const LinAl::Matrix<float, 2, 4> &);
200         void uniform_matrix4x2(Tag, const float *);
201         void uniform(Tag, const LinAl::Matrix<float, 3, 2> &);
202         void uniform_matrix2x3(Tag, const float *);
203         void uniform(Tag, const LinAl::Matrix<float, 3, 3> &);
204         void uniform_matrix3(Tag, const float *);
205         void uniform(Tag, const LinAl::Matrix<float, 3, 4> &);
206         void uniform_matrix4x3(Tag, const float *);
207         void uniform(Tag, const LinAl::Matrix<float, 4, 2> &);
208         void uniform_matrix2x4(Tag, const float *);
209         void uniform(Tag, const LinAl::Matrix<float, 4, 3> &);
210         void uniform_matrix3x4(Tag, const float *);
211         void uniform(Tag, const Matrix &);
212         void uniform_matrix4(Tag, const float *);
213         void uniform1_array(Tag, unsigned, const int *);
214         void uniform1_array(Tag, unsigned, const float *);
215         void uniform2_array(Tag, unsigned, const int *);
216         void uniform2_array(Tag, unsigned, const float *);
217         void uniform3_array(Tag, unsigned, const int *);
218         void uniform3_array(Tag, unsigned, const float *);
219         void uniform4_array(Tag, unsigned, const int *);
220         void uniform4_array(Tag, unsigned, const float *);
221         void uniform_matrix2_array(Tag, unsigned, const float *);
222         void uniform_matrix3x2_array(Tag, unsigned, const float *);
223         void uniform_matrix4x2_array(Tag, unsigned, const float *);
224         void uniform_matrix2x3_array(Tag, unsigned, const float *);
225         void uniform_matrix3_array(Tag, unsigned, const float *);
226         void uniform_matrix4x3_array(Tag, unsigned, const float *);
227         void uniform_matrix2x4_array(Tag, unsigned, const float *);
228         void uniform_matrix3x4_array(Tag, unsigned, const float *);
229         void uniform_matrix4_array(Tag, unsigned, const float *);
230         void remove_uniform(Tag);
231
232         std::vector<Tag> get_uniform_tags() const;
233         const Uniform &get_uniform(Tag) const;
234         const Uniform *find_uniform(Tag) const;
235
236 private:
237         int find_uniform_index(Tag) const;
238         std::vector<ProgramBlock>::iterator get_program(const Program &) const;
239         void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
240         void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
241
242 public:
243         /** Applies uniform blocks for the currently bound program, creating them
244         if needed. */
245         void apply() const;
246 };
247
248 } // namespace GL
249 } // namespace Msp
250
251 #endif