1 #ifndef MSP_GL_PROGRAMDATA_H_
2 #define MSP_GL_PROGRAMDATA_H_
6 #include <msp/datafile/objectloader.h>
17 class too_many_uniforms: public std::runtime_error
20 too_many_uniforms(const std::string &w): std::runtime_error(w) { }
21 virtual ~too_many_uniforms() throw() { }
25 class BufferBackedUniformBlock;
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.
35 The class is optimized for an access pattern where the set of uniforms and
36 programs stays constants, with only the values changing.
41 class Loader: public DataFile::ObjectLoader<ProgramData>
44 Loader(ProgramData &);
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 &);
67 class ArrayLoader: public DataFile::Loader
71 unsigned element_size;
72 std::vector<char> data;
75 ArrayLoader(DataType, unsigned);
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); }
83 void uniform(DataType, unsigned, const void *);
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);
94 typedef unsigned Mask;
98 MASK_BITS = sizeof(Mask)*8,
99 ALL_ONES = static_cast<Mask>(-1)
109 void replace_value(Uniform *);
114 Program::LayoutHash block_hash;
129 SharedBlock(Program::LayoutHash);
131 const UInt8 *get_uniform_indices() const;
136 Program::LayoutHash prog_hash;
149 ProgramBlock(Program::LayoutHash);
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;
156 mutable std::vector<SharedBlock> blocks;
157 mutable std::vector<ProgramBlock> programs;
158 mutable BufferBackedUniformBlock *last_buffer_block;
159 mutable Buffer *buffer;
161 std::string debug_name;
164 ProgramData(const Program * = 0);
165 ProgramData(const ProgramData &);
166 ProgramData(const ProgramData &, const Program *);
167 ProgramData &operator=(const ProgramData &);
171 void uniform(Tag, Uniform *);
172 template<typename T, typename V>
173 void uniform(Tag, V);
174 template<typename T, typename V>
175 void uniform_array(Tag, unsigned, V);
176 bool validate_tag(Tag) const;
177 void add_uniform(Tag, Uniform *);
178 void mark_dirty(Mask);
180 void uniform(Tag, const Uniform &);
181 void uniform(Tag, int);
182 void uniform(Tag, float);
183 void uniform(Tag, int, int);
184 void uniform(Tag, float, float);
185 void uniform2(Tag, const int *);
186 void uniform2(Tag, const float *);
187 void uniform(Tag, int, int, int);
188 void uniform(Tag, float, float, float);
189 void uniform3(Tag, const int *);
190 void uniform3(Tag, const float *);
191 void uniform(Tag, int, int, int, int);
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 float *);
196 void uniform_matrix2(Tag, const float *);
197 void uniform_matrix3x2(Tag, const float *);
198 void uniform_matrix4x2(Tag, const float *);
199 void uniform_matrix2x3(Tag, const float *);
200 void uniform_matrix3(Tag, const float *);
201 void uniform_matrix4x3(Tag, const float *);
202 void uniform_matrix2x4(Tag, const float *);
203 void uniform_matrix3x4(Tag, const float *);
204 void uniform(Tag, const Matrix &);
205 void uniform_matrix4(Tag, const float *);
206 void uniform_array(Tag, unsigned, const int *);
207 void uniform_array(Tag, unsigned, const float *);
208 void uniform1_array(Tag, unsigned, const int *);
209 void uniform1_array(Tag, unsigned, const float *);
210 void uniform2_array(Tag, unsigned, const int *);
211 void uniform2_array(Tag, unsigned, const float *);
212 void uniform3_array(Tag, unsigned, const int *);
213 void uniform3_array(Tag, unsigned, const float *);
214 void uniform4_array(Tag, unsigned, const int *);
215 void uniform4_array(Tag, unsigned, const float *);
216 void uniform_matrix2_array(Tag, unsigned, const float *);
217 void uniform_matrix3x2_array(Tag, unsigned, const float *);
218 void uniform_matrix4x2_array(Tag, unsigned, const float *);
219 void uniform_matrix2x3_array(Tag, unsigned, const float *);
220 void uniform_matrix3_array(Tag, unsigned, const float *);
221 void uniform_matrix4x3_array(Tag, unsigned, const float *);
222 void uniform_matrix2x4_array(Tag, unsigned, const float *);
223 void uniform_matrix3x4_array(Tag, unsigned, const float *);
224 void uniform_matrix4_array(Tag, unsigned, const float *);
226 template<typename T, unsigned N>
227 void uniform(Tag, const LinAl::Vector<T, N> &);
229 template<typename T, unsigned R, unsigned C>
230 void uniform(Tag, const LinAl::Matrix<T, R, C> &);
232 template<typename T, unsigned N>
233 void uniform_array(Tag, unsigned, const LinAl::Vector<T, N> *);
235 template<typename T, unsigned R, unsigned C>
236 void uniform_array(Tag, unsigned, const LinAl::Matrix<T, R, C> *);
238 void remove_uniform(Tag);
240 unsigned get_generation() const { return generation; }
242 std::vector<Tag> get_uniform_tags() const;
243 const Uniform &get_uniform(Tag) const;
244 const Uniform *find_uniform(Tag) const;
247 int find_uniform_index(Tag) const;
248 std::vector<ProgramBlock>::iterator get_program(const Program &) const;
249 void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
250 void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
253 /** Applies uniform blocks for the currently bound program, creating them
257 void set_debug_name(const std::string &);
260 template<typename T, unsigned N>
261 void ProgramData::uniform(Tag tag, const LinAl::Vector<T, N> &v)
262 { uniform<UniformVector<T, N> >(tag, &v.x); }
264 template<typename T, unsigned R, unsigned C>
265 void ProgramData::uniform(Tag tag, const LinAl::Matrix<T, R, C> &v)
266 { uniform<UniformMatrix<T, R, C> >(tag, &v(0, 0)); }
268 template<typename T, unsigned N>
269 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector<T, N> *v)
270 { uniform_array<UniformVector<T, N> >(tag, n, &v[0].x); }
272 template<typename T, unsigned R, unsigned C>
273 void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix<T, R, C> *v)
274 { uniform_array<UniformMatrix<T, R, C> >(tag, n, &v[0](0, 0)); }