1 #ifndef MSP_GL_PROGRAMDATA_H_
2 #define MSP_GL_PROGRAMDATA_H_
6 #include <msp/datafile/objectloader.h>
16 class too_many_uniforms: public std::runtime_error
19 too_many_uniforms(const std::string &w): std::runtime_error(w) { }
20 virtual ~too_many_uniforms() throw() { }
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.
34 The class is optimized for an access pattern where the set of uniforms and
35 programs stays constants, with only the values changing.
40 class Loader: public DataFile::ObjectLoader<ProgramData>
43 Loader(ProgramData &);
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 &);
66 class ArrayLoader: public DataFile::Loader
70 unsigned element_size;
71 std::vector<char> data;
74 ArrayLoader(DataType, unsigned);
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); }
82 void uniform(DataType, unsigned, const void *);
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);
93 typedef unsigned Mask;
97 MASK_BITS = sizeof(Mask)*8,
98 ALL_ONES = static_cast<Mask>(-1)
108 void replace_value(Uniform *);
127 SharedBlock(UniformBlock *);
129 const UInt8 *get_uniform_indices() const;
139 ProgramBlock(int, SharedBlock *);
142 struct ProgramUniforms
144 std::vector<ProgramBlock> blocks;
151 typedef std::map<Program::LayoutHash, SharedBlock> BlockMap;
152 typedef std::map<Program::LayoutHash, ProgramUniforms> ProgramMap;
154 // XXX All these mutables are a bit silly, but I'm out of better ideas
155 const Program *tied_program;
156 std::vector<TaggedUniform> uniforms;
157 mutable BlockMap blocks;
158 mutable ProgramMap programs;
159 mutable UniformBlock *last_block;
160 mutable Buffer *buffer;
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 *);
179 void uniform(Tag, const Uniform &);
180 void uniform(Tag, int);
181 void uniform(Tag, float);
182 void uniform(Tag, int, int);
183 void uniform(Tag, float, float);
184 void uniform2(Tag, const int *);
185 void uniform2(Tag, const float *);
186 void uniform(Tag, int, int, int);
187 void uniform(Tag, float, float, float);
188 void uniform(Tag, const Vector3 &);
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 Vector4 &);
194 void uniform(Tag, const Color &);
195 void uniform4(Tag, const int *);
196 void uniform4(Tag, const float *);
197 void uniform(Tag, const LinAl::Matrix<float, 2, 2> &);
198 void uniform_matrix2(Tag, const float *);
199 void uniform(Tag, const LinAl::Matrix<float, 2, 3> &);
200 void uniform_matrix3x2(Tag, const float *);
201 void uniform(Tag, const LinAl::Matrix<float, 2, 4> &);
202 void uniform_matrix4x2(Tag, const float *);
203 void uniform(Tag, const LinAl::Matrix<float, 3, 2> &);
204 void uniform_matrix2x3(Tag, const float *);
205 void uniform(Tag, const LinAl::Matrix<float, 3, 3> &);
206 void uniform_matrix3(Tag, const float *);
207 void uniform(Tag, const LinAl::Matrix<float, 3, 4> &);
208 void uniform_matrix4x3(Tag, const float *);
209 void uniform(Tag, const LinAl::Matrix<float, 4, 2> &);
210 void uniform_matrix2x4(Tag, const float *);
211 void uniform(Tag, const LinAl::Matrix<float, 4, 3> &);
212 void uniform_matrix3x4(Tag, const float *);
213 void uniform(Tag, const Matrix &);
214 void uniform_matrix4(Tag, const float *);
215 void uniform1_array(Tag, unsigned, const int *);
216 void uniform1_array(Tag, unsigned, const float *);
217 void uniform2_array(Tag, unsigned, const int *);
218 void uniform2_array(Tag, unsigned, const float *);
219 void uniform3_array(Tag, unsigned, const int *);
220 void uniform3_array(Tag, unsigned, const float *);
221 void uniform4_array(Tag, unsigned, const int *);
222 void uniform4_array(Tag, unsigned, const float *);
223 void uniform_matrix2_array(Tag, unsigned, const float *);
224 void uniform_matrix3x2_array(Tag, unsigned, const float *);
225 void uniform_matrix4x2_array(Tag, unsigned, const float *);
226 void uniform_matrix2x3_array(Tag, unsigned, const float *);
227 void uniform_matrix3_array(Tag, unsigned, const float *);
228 void uniform_matrix4x3_array(Tag, unsigned, const float *);
229 void uniform_matrix2x4_array(Tag, unsigned, const float *);
230 void uniform_matrix3x4_array(Tag, unsigned, const float *);
231 void uniform_matrix4_array(Tag, unsigned, const float *);
232 void remove_uniform(Tag);
234 std::vector<Tag> get_uniform_tags() const;
235 const Uniform &get_uniform(Tag) const;
236 const Uniform *find_uniform(Tag) const;
239 int find_uniform_index(Tag) const;
240 void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
241 void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
242 SharedBlock *get_shared_block(const Program::UniformBlockInfo &) const;
245 /** Applies uniform blocks for the currently bound program, creating them