]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.h
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / 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 "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 Uniform;
24 class UniformBlock;
25 struct Color;
26
27 /**
28 Stores uniform variables for shader programs.  The uniforms are stored in a
29 program-independent way, and UniformBlocks are created to match the uniform
30 layouts of different programs.  If multiple programs have the same layout, the
31 same block is used for them.
32
33 The class is optimized for an access pattern where the set of uniforms and
34 programs stays constants, with only the values changing.
35 */
36 class ProgramData
37 {
38 public:
39         class Loader: public DataFile::ObjectLoader<ProgramData>
40         {
41         public:
42                 Loader(ProgramData &);
43         private:
44                 void uniform1i(const std::string &, int);
45                 void uniform1f(const std::string &, float);
46                 void uniform2i(const std::string &, int, int);
47                 void uniform2f(const std::string &, float, float);
48                 void uniform3i(const std::string &, int, int, int);
49                 void uniform3f(const std::string &, float, float, float);
50                 void uniform4i(const std::string &, int, int, int, int);
51                 void uniform4f(const std::string &, float, float, float, float);
52                 void uniform_array_(const std::string &, DataType, unsigned);
53                 void uniform1i_array(const std::string &);
54                 void uniform1f_array(const std::string &);
55                 void uniform2i_array(const std::string &);
56                 void uniform2f_array(const std::string &);
57                 void uniform3i_array(const std::string &);
58                 void uniform3f_array(const std::string &);
59                 void uniform4i_array(const std::string &);
60                 void uniform4f_array(const std::string &);
61                 void uniform_array(const std::string &);
62         };
63
64 private:
65         class ArrayLoader: public DataFile::Loader
66         {
67         private:
68                 DataType type;
69                 unsigned element_size;
70                 std::vector<char> data;
71
72         public:
73                 ArrayLoader(DataType, unsigned);
74
75                 DataType get_data_type() const { return type; }
76                 unsigned get_element_size() const { return element_size; }
77                 const void *get_data() const { return &data[0]; }
78                 unsigned get_size() const { return data.size()/(4*element_size); }
79
80         private:
81                 void uniform(DataType, unsigned, const void *);
82                 void uniform1i(int);
83                 void uniform1f(float);
84                 void uniform2i(int, int);
85                 void uniform2f(float, float);
86                 void uniform3i(int, int, int);
87                 void uniform3f(float, float, float);
88                 void uniform4i(int, int, int, int);
89                 void uniform4f(float, float, float, float);
90         };
91
92         typedef unsigned Mask;
93
94         enum
95         {
96                 MASK_BITS = sizeof(Mask)*8,
97                 ALL_ONES = static_cast<Mask>(-1)
98         };
99
100         struct NamedUniform
101         {
102                 std::string name;
103                 Uniform *value;
104
105                 NamedUniform();
106
107                 bool compare_name(const std::string &, unsigned) const;
108                 void replace_value(Uniform *);
109         };
110
111         struct SharedBlock
112         {
113                 Mask used;
114                 Mask dirty;
115                 UniformBlock *block;
116                 union
117                 {
118                         UInt8 type_flag;
119                         UInt8 values[16];
120                         struct
121                         {
122                                 UInt8 type_flag;
123                                 UInt8 *values;
124                         } dynamic;
125                 } indices;
126
127                 SharedBlock(UniformBlock *);
128
129                 const UInt8 *get_uniform_indices() const;
130         };
131
132         struct ProgramBlock
133         {
134                 int bind_point;
135                 UniformBlock *block;
136                 SharedBlock *shared;
137
138                 ProgramBlock();
139                 ProgramBlock(int, SharedBlock *);
140         };
141
142         struct ProgramUniforms
143         {
144                 std::vector<ProgramBlock> blocks;
145                 Mask used;
146                 Mask dirty;
147
148                 ProgramUniforms();
149         };
150
151         typedef std::map<Program::LayoutHash, SharedBlock> BlockMap;
152         typedef std::map<Program::LayoutHash, ProgramUniforms> ProgramMap;
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<NamedUniform> uniforms;
157         mutable BlockMap blocks;
158         mutable ProgramMap programs;
159         mutable UniformBlock *last_block;
160         mutable Buffer *buffer;
161         mutable unsigned dirty;
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();
169
170 private:
171         void uniform(const std::string &, Uniform *);
172         template<typename T, typename V>
173         void uniform(const std::string &, V);
174         template<typename T, typename V>
175         void uniform_array(const std::string &, unsigned, V);
176         bool validate_name(const std::string &) const;
177         void add_uniform(const std::string &, Uniform *);
178 public:
179         void uniform(const std::string &, const Uniform &);
180         void uniform(const std::string &, int);
181         void uniform(const std::string &, float);
182         void uniform(const std::string &, int, int);
183         void uniform(const std::string &, float, float);
184         void uniform2(const std::string &, const int *);
185         void uniform2(const std::string &, const float *);
186         void uniform(const std::string &, int, int, int);
187         void uniform(const std::string &, float, float, float);
188         void uniform(const std::string &, const Vector3 &);
189         void uniform3(const std::string &, const int *);
190         void uniform3(const std::string &, const float *);
191         void uniform(const std::string &, int, int, int, int);
192         void uniform(const std::string &, float, float, float, float);
193         void uniform(const std::string &, const Vector4 &);
194         void uniform(const std::string &, const Color &);
195         void uniform4(const std::string &, const int *);
196         void uniform4(const std::string &, const float *);
197         void uniform(const std::string &, const LinAl::Matrix<float, 2, 2> &);
198         void uniform_matrix2(const std::string &, const float *);
199         void uniform(const std::string &, const LinAl::Matrix<float, 2, 3> &);
200         void uniform_matrix3x2(const std::string &, const float *);
201         void uniform(const std::string &, const LinAl::Matrix<float, 2, 4> &);
202         void uniform_matrix4x2(const std::string &, const float *);
203         void uniform(const std::string &, const LinAl::Matrix<float, 3, 2> &);
204         void uniform_matrix2x3(const std::string &, const float *);
205         void uniform(const std::string &, const LinAl::Matrix<float, 3, 3> &);
206         void uniform_matrix3(const std::string &, const float *);
207         void uniform(const std::string &, const LinAl::Matrix<float, 3, 4> &);
208         void uniform_matrix4x3(const std::string &, const float *);
209         void uniform(const std::string &, const LinAl::Matrix<float, 4, 2> &);
210         void uniform_matrix2x4(const std::string &, const float *);
211         void uniform(const std::string &, const LinAl::Matrix<float, 4, 3> &);
212         void uniform_matrix3x4(const std::string &, const float *);
213         void uniform(const std::string &, const Matrix &);
214         void uniform_matrix4(const std::string &, const float *);
215         void uniform1_array(const std::string &, unsigned, const int *);
216         void uniform1_array(const std::string &, unsigned, const float *);
217         void uniform2_array(const std::string &, unsigned, const int *);
218         void uniform2_array(const std::string &, unsigned, const float *);
219         void uniform3_array(const std::string &, unsigned, const int *);
220         void uniform3_array(const std::string &, unsigned, const float *);
221         void uniform4_array(const std::string &, unsigned, const int *);
222         void uniform4_array(const std::string &, unsigned, const float *);
223         void uniform_matrix2_array(const std::string &, unsigned, const float *);
224         void uniform_matrix3x2_array(const std::string &, unsigned, const float *);
225         void uniform_matrix4x2_array(const std::string &, unsigned, const float *);
226         void uniform_matrix2x3_array(const std::string &, unsigned, const float *);
227         void uniform_matrix3_array(const std::string &, unsigned, const float *);
228         void uniform_matrix4x3_array(const std::string &, unsigned, const float *);
229         void uniform_matrix2x4_array(const std::string &, unsigned, const float *);
230         void uniform_matrix3x4_array(const std::string &, unsigned, const float *);
231         void uniform_matrix4_array(const std::string &, unsigned, const float *);
232         void remove_uniform(const std::string &);
233
234         std::vector<std::string> get_uniform_names() const;
235         const Uniform &get_uniform(const std::string &) const;
236         const Uniform *find_uniform(const std::string &) const;
237
238 private:
239         static bool uniform_name_compare(const NamedUniform &, const std::string &);
240         int find_uniform_index(const std::string &) const;
241         void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
242         void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
243         SharedBlock *get_shared_block(const Program::UniformBlockInfo &) const;
244
245 public:
246         /** Applies uniform blocks for the currently bound program, creating them
247         if needed. */
248         void apply() const;
249 };
250
251 } // namespace GL
252 } // namespace Msp
253
254 #endif