]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Throw an exception if there's too many uniforms in a ProgramData
[libs/gl.git] / source / programdata.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/io/print.h>
5 #include "buffer.h"
6 #include "color.h"
7 #include "error.h"
8 #include "matrix.h"
9 #include "program.h"
10 #include "programdata.h"
11 #include "uniform.h"
12 #include "uniformblock.h"
13 #include "vector.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 ProgramData::ProgramData(const Program *p):
21         tied_program(p),
22         last_block(0),
23         buffer(0),
24         dirty(0)
25 { }
26
27 // Blocks are intentionally left uncopied
28 ProgramData::ProgramData(const ProgramData &other):
29         tied_program(other.tied_program),
30         uniforms(other.uniforms),
31         last_block(0),
32         buffer(0),
33         dirty(0)
34 {
35         for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
36                 i->value = i->value->clone();
37 }
38
39 ProgramData::ProgramData(const ProgramData &other, const Program *p):
40         tied_program(p),
41         last_block(0),
42         buffer(0),
43         dirty(0)
44 {
45         if(tied_program)
46         {
47                 for(vector<NamedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
48                         tied_program->get_uniform_info(i->name);
49         }
50
51         uniforms = other.uniforms;
52         for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
53                 i->value = i->value->clone();
54 }
55
56 ProgramData &ProgramData::operator=(const ProgramData &other)
57 {
58         tied_program = other.tied_program;
59
60         uniforms = other.uniforms;
61         for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
62                 i->value = i->value->clone();
63
64         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
65                 delete i->second.block;
66         programs.clear();
67
68         last_block = 0;
69         buffer = 0;
70         dirty = 0;
71
72         return *this;
73 }
74
75 ProgramData::~ProgramData()
76 {
77         for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
78                 delete i->value;
79         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
80         {
81                 if(i->second.indices.type_flag==0xFE)
82                         delete i->second.indices.dynamic.values;
83                 delete i->second.block;
84         }
85         delete buffer;
86 }
87
88 void ProgramData::uniform(const string &name, Uniform *uni)
89 {
90         try
91         {
92                 if(tied_program)
93                         tied_program->get_uniform_info(name);
94                 else if(name[name.size()-1]==']')
95                         throw invalid_argument("ProgramData::uniform");
96         }
97         catch(const exception &e)
98         {
99                 delete uni;
100 #ifdef DEBUG
101                 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what());
102                 return;
103 #else
104                 throw;
105 #endif
106         }
107
108         int i = find_uniform_index(name);
109         if(i>=0)
110         {
111                 uniforms[i].replace_value(uni);
112                 dirty |= 1<<i;
113                 return;
114         }
115
116         if(uniforms.size()>=MASK_BITS)
117         {
118                 delete uni;
119                 throw too_many_uniforms(name);
120         }
121
122         vector<NamedUniform>::iterator j = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
123
124         NamedUniform nu;
125         nu.name = name;
126         nu.value = uni;
127         uniforms.insert(j, nu);
128
129         dirty = ALL_ONES;
130 }
131
132 void ProgramData::uniform(const string &name, const Uniform &u)
133 {
134         uniform(name, u.clone());
135 }
136
137 void ProgramData::uniform(const string &name, int v)
138 {
139         uniform(name, new Uniform1i(v));
140 }
141
142 void ProgramData::uniform(const string &name, float v)
143 {
144         uniform(name, new Uniform1f(v));
145 }
146
147 void ProgramData::uniform(const string &name, int v0, int v1)
148 {
149         int va[2] = { v0, v1 };
150         uniform2(name, va);
151 }
152
153 void ProgramData::uniform(const string &name, float v0, float v1)
154 {
155         float va[2] = { v0, v1 };
156         uniform2(name, va);
157 }
158
159 void ProgramData::uniform2(const string &name, const int *v)
160 {
161         uniform(name, new Uniform2i(v));
162 }
163
164 void ProgramData::uniform2(const string &name, const float *v)
165 {
166         uniform(name, new Uniform2f(v));
167 }
168
169 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
170 {
171         int va[3] = { v0, v1, v2 };
172         uniform3(name, va);
173 }
174
175 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
176 {
177         float va[3] = { v0, v1, v2 };
178         uniform3(name, va);
179 }
180
181 void ProgramData::uniform(const string &name, const Vector3 &v)
182 {
183         uniform(name, v.x, v.y, v.z);
184 }
185
186 void ProgramData::uniform3(const string &name, const int *v)
187 {
188         uniform(name, new Uniform3i(v));
189 }
190
191 void ProgramData::uniform3(const string &name, const float *v)
192 {
193         uniform(name, new Uniform3f(v));
194 }
195
196 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
197 {
198         int va[4] = { v0, v1, v2, v3 };
199         uniform4(name, va);
200 }
201
202 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
203 {
204         float va[4] = { v0, v1, v2, v3 };
205         uniform4(name, va);
206 }
207
208 void ProgramData::uniform(const string &name, const Vector4 &v)
209 {
210         uniform(name, v.x, v.y, v.z, v.w);
211 }
212
213 void ProgramData::uniform(const string &name, const Color &c)
214 {
215         uniform(name, c.r, c.g, c.b, c.a);
216 }
217
218 void ProgramData::uniform4(const string &name, const int *v)
219 {
220         uniform(name, new Uniform4i(v));
221 }
222
223 void ProgramData::uniform4(const string &name, const float *v)
224 {
225         uniform(name, new Uniform4f(v));
226 }
227
228 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
229 {
230         uniform_matrix2(name, &m(0, 0));
231 }
232
233 void ProgramData::uniform_matrix2(const string &name, const float *v)
234 {
235         uniform(name, new UniformMatrix2x2f(v));
236 }
237
238 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 3> &m)
239 {
240         uniform_matrix3x2(name, &m(0, 0));
241 }
242
243 void ProgramData::uniform_matrix3x2(const string &name, const float *v)
244 {
245         uniform(name, new UniformMatrix3x2f(v));
246 }
247
248 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 4> &m)
249 {
250         uniform_matrix4x2(name, &m(0, 0));
251 }
252
253 void ProgramData::uniform_matrix4x2(const string &name, const float *v)
254 {
255         uniform(name, new UniformMatrix4x2f(v));
256 }
257
258 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 2> &m)
259 {
260         uniform_matrix2x3(name, &m(0, 0));
261 }
262
263 void ProgramData::uniform_matrix2x3(const string &name, const float *v)
264 {
265         uniform(name, new UniformMatrix2x3f(v));
266 }
267
268 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
269 {
270         uniform_matrix3(name, &m(0, 0));
271 }
272
273 void ProgramData::uniform_matrix3(const string &name, const float *v)
274 {
275         uniform(name, new UniformMatrix3x3f(v));
276 }
277
278 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 4> &m)
279 {
280         uniform_matrix4x3(name, &m(0, 0));
281 }
282
283 void ProgramData::uniform_matrix4x3(const string &name, const float *v)
284 {
285         uniform(name, new UniformMatrix4x3f(v));
286 }
287
288 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 2> &m)
289 {
290         uniform_matrix2x4(name, &m(0, 0));
291 }
292
293 void ProgramData::uniform_matrix2x4(const string &name, const float *v)
294 {
295         uniform(name, new UniformMatrix2x4f(v));
296 }
297
298 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 3> &m)
299 {
300         uniform_matrix3x4(name, &m(0, 0));
301 }
302
303 void ProgramData::uniform_matrix3x4(const string &name, const float *v)
304 {
305         uniform(name, new UniformMatrix3x4f(v));
306 }
307
308 void ProgramData::uniform(const string &name, const Matrix &m)
309 {
310         uniform_matrix4(name, m.data());
311 }
312
313 void ProgramData::uniform_matrix4(const string &name, const float *v)
314 {
315         uniform(name, new UniformMatrix4x4f(v));
316 }
317
318 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
319 {
320         uniform(name, new UniformArray<Uniform1i>(n, v));
321 }
322
323 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
324 {
325         uniform(name, new UniformArray<Uniform1f>(n, v));
326 }
327
328 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
329 {
330         uniform(name, new UniformArray<Uniform2i>(n, v));
331 }
332
333 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
334 {
335         uniform(name, new UniformArray<Uniform2f>(n, v));
336 }
337
338 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
339 {
340         uniform(name, new UniformArray<Uniform3i>(n, v));
341 }
342
343 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
344 {
345         uniform(name, new UniformArray<Uniform3f>(n, v));
346 }
347
348 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
349 {
350         uniform(name, new UniformArray<Uniform4i>(n, v));
351 }
352
353 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
354 {
355         uniform(name, new UniformArray<Uniform4f>(n, v));
356 }
357
358 void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v)
359 {
360         uniform(name, new UniformArray<UniformMatrix2x2f>(n, v));
361 }
362
363 void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v)
364 {
365         uniform(name, new UniformArray<UniformMatrix3x2f>(n, v));
366 }
367
368 void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v)
369 {
370         uniform(name, new UniformArray<UniformMatrix4x2f>(n, v));
371 }
372
373 void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v)
374 {
375         uniform(name, new UniformArray<UniformMatrix2x3f>(n, v));
376 }
377
378 void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
379 {
380         uniform(name, new UniformArray<UniformMatrix3x3f>(n, v));
381 }
382
383 void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v)
384 {
385         uniform(name, new UniformArray<UniformMatrix4x3f>(n, v));
386 }
387
388 void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v)
389 {
390         uniform(name, new UniformArray<UniformMatrix2x4f>(n, v));
391 }
392
393 void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v)
394 {
395         uniform(name, new UniformArray<UniformMatrix3x4f>(n, v));
396 }
397
398 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
399 {
400         uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
401 }
402
403 void ProgramData::remove_uniform(const string &name)
404 {
405         vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
406         if(i==uniforms.end() || i->name!=name)
407                 return;
408
409         delete i->value;
410         uniforms.erase(i);
411
412         dirty = ALL_ONES;
413 }
414
415 vector<string> ProgramData::get_uniform_names() const
416 {
417         vector<string> names;
418         names.reserve(uniforms.size());
419         for(vector<NamedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
420                 names.push_back(i->name);
421         return names;
422 }
423
424 const Uniform &ProgramData::get_uniform(const string &name) const
425 {
426         int i = find_uniform_index(name);
427         if(i<0)
428                 throw key_error(name);
429         return *uniforms[i].value;
430 }
431
432 const Uniform *ProgramData::find_uniform(const string &name) const
433 {
434         int i = find_uniform_index(name);
435         return (i>=0 ? uniforms[i].value : 0);
436 }
437
438 bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
439 {
440         return nu.name<name;
441 }
442
443 int ProgramData::find_uniform_index(const string &name) const
444 {
445         vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
446         return ((i!=uniforms.end() && i->name==name) ? i-uniforms.begin() : -1);
447 }
448
449 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
450 {
451         UInt8 *indices = block.indices.values;
452         if(info.uniforms.size()>16)
453         {
454                 if(block.indices.type_flag==0xFD)
455                 {
456                         block.indices.dynamic.values = new UInt8[info.uniforms.size()];
457                         block.indices.type_flag = 0xFE;
458                 }
459                 indices = block.indices.dynamic.values;
460         }
461
462         block.used = 0;
463         for(unsigned i=0; i<info.uniforms.size(); ++i)
464         {
465                 int j = find_uniform_index(info.uniforms[i]->name);
466                 if(j>=0)
467                 {
468                         indices[i] = j;
469                         if(static_cast<unsigned>(j)<MASK_BITS)
470                                 block.used |= 1<<j;
471                 }
472                 else
473                         indices[i] = 0xFF;
474         }
475
476         block.dirty = block.used;
477 }
478
479 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
480 {
481         const UInt8 *indices = block.get_uniform_indices();
482         for(unsigned i=0; i<info.uniforms.size(); ++i)
483                 if(indices[i]!=0xFF)
484                         block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
485 }
486
487 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
488 {
489         BlockMap::iterator i = blocks.find(info.layout_hash);
490         if(i==blocks.end())
491         {
492                 bool any_found = false;
493                 bool all_found = true;
494                 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); j!=info.uniforms.end(); ++j)
495                 {
496                         if(find_uniform_index((*j)->name)>=0)
497                                 any_found = true;
498                         else
499                                 all_found = false;
500                 }
501
502                 if(!any_found)
503                         return 0;
504                 else if(!all_found && info.bind_point>=0)
505                 {
506 #ifdef DEBUG
507                         IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
508 #else
509                         throw incomplete_uniform_block(info.name);
510 #endif
511                 }
512
513                 UniformBlock *block;
514                 if(info.bind_point>=0)
515                 {
516                         if(!buffer)
517                                 buffer = new Buffer(UNIFORM_BUFFER);
518
519                         block = new UniformBlock(info.data_size);
520                         block->use_buffer(buffer, last_block);
521                         last_block = block;
522                 }
523                 else
524                         block = new UniformBlock;
525
526                 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
527                 update_block_uniform_indices(i->second, info);
528         }
529
530         return &i->second;
531 }
532
533 void ProgramData::apply() const
534 {
535         const Program *prog = Program::current();
536         if(!prog)
537                 throw invalid_operation("ProgramData::apply");
538
539         Program::LayoutHash layout = prog->get_uniform_layout_hash();
540         ProgramUniforms &pu = programs[layout];
541
542         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
543         Mask affected = (dirty&pu.used) | force_dirty;
544         if(affected|pu.dirty)
545         {
546                 /* If the global dirty flag affects this program, add it to per-program
547                 dirty flags and clear the global flag.  A previously unseen program will
548                 always cause this to happen. */
549                 if(affected)
550                 {
551                         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
552                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
553                         for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
554                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
555                         dirty = 0;
556                 }
557
558                 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
559
560                 UniformBlock *old_last_block = last_block;
561                 if(pu.dirty==ALL_ONES)
562                 {
563                         /* The set of uniforms has changed since this program was last used.
564                         Regenerate the list of uniform blocks. */
565                         pu.blocks.clear();
566                         pu.blocks.reserve(prog_blocks.size());
567
568                         pu.used = 0;
569                         for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
570                         {
571                                 SharedBlock *shared = get_shared_block(i->second);
572                                 if(shared)
573                                 {
574                                         if(shared->dirty==ALL_ONES)
575                                                 update_block_uniform_indices(*shared, i->second);
576                                         pu.used |= shared->used;
577                                 }
578
579                                 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
580                         }
581                 }
582
583                 // Update the contents of all dirty blocks.
584                 bool buffered_blocks_updated = false;
585                 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
586                 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
587                 {
588                         if(!j->shared || !j->shared->dirty)
589                                 continue;
590
591                         update_block(*j->shared, i->second);
592                         j->shared->dirty = 0;
593                         buffered_blocks_updated |= (j->bind_point>=0);
594                 }
595
596                 pu.dirty = 0;
597
598                 /* If any blocks stored in the buffer were updated, bind the buffer here
599                 to avoid state thrashing. */
600                 if(buffered_blocks_updated && !ARB_direct_state_access)
601                         buffer->bind();
602
603                 if(last_block!=old_last_block)
604                 {
605                         unsigned required_size = last_block->get_required_buffer_size();
606                         if(last_block->get_required_buffer_size()>buffer->get_size())
607                         {
608                                 if(buffer->get_size()>0)
609                                 {
610                                         delete buffer;
611                                         buffer = new Buffer(UNIFORM_BUFFER);
612                                         last_block->change_buffer(buffer);
613                                 }
614
615                                 buffer->storage(required_size);
616                         }
617                 }
618         }
619
620         for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
621                 if(i->block)
622                         i->block->apply(i->bind_point);
623 }
624
625
626 ProgramData::NamedUniform::NamedUniform():
627         value(0)
628 { }
629
630 void ProgramData::NamedUniform::replace_value(Uniform *v)
631 {
632         /* UniformBlock does not copy the uniforms, so existing default blocks
633         will be left with stale pointers.  This is not a problem as long as no
634         one stores pointers to the blocks and expects them to stay valid. */
635         delete value;
636         value = v;
637 }
638
639
640 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
641         used(0),
642         dirty(0),
643         block(b)
644 {
645         indices.type_flag = 0xFD;
646 }
647
648 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
649 {
650         return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
651 }
652
653
654 ProgramData::ProgramBlock::ProgramBlock():
655         bind_point(-1),
656         block(0),
657         shared(0)
658 { }
659
660 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
661         bind_point(p),
662         block((b && b->used) ? b->block : 0),
663         shared(b)
664 { }
665
666
667 ProgramData::ProgramUniforms::ProgramUniforms():
668         used(ALL_ONES),
669         dirty(ALL_ONES)
670 { }
671
672
673 ProgramData::Loader::Loader(ProgramData &pd):
674         DataFile::ObjectLoader<ProgramData>(pd)
675 {
676         add("uniform", &Loader::uniform1i);
677         add("uniform1i", &Loader::uniform1i);
678         add("uniform", &Loader::uniform1f);
679         add("uniform1f", &Loader::uniform1f);
680         add("uniform", &Loader::uniform2i);
681         add("uniform2i", &Loader::uniform2i);
682         add("uniform", &Loader::uniform2f);
683         add("uniform2f", &Loader::uniform2f);
684         add("uniform", &Loader::uniform3i);
685         add("uniform3i", &Loader::uniform3i);
686         add("uniform", &Loader::uniform3f);
687         add("uniform3f", &Loader::uniform3f);
688         add("uniform", &Loader::uniform4i);
689         add("uniform4i", &Loader::uniform4i);
690         add("uniform", &Loader::uniform4f);
691         add("uniform4f", &Loader::uniform4f);
692         add("uniform1i_array", &Loader::uniform1i_array);
693         add("uniform1f_array", &Loader::uniform1f_array);
694         add("uniform2f_array", &Loader::uniform2f_array);
695         add("uniform3f_array", &Loader::uniform3f_array);
696         add("uniform4f_array", &Loader::uniform4f_array);
697         add("uniform_array", &Loader::uniform_array);
698 }
699
700 void ProgramData::Loader::uniform1i(const string &n, int v)
701 {
702         obj.uniform(n, v);
703 }
704
705 void ProgramData::Loader::uniform1f(const string &n, float v)
706 {
707         obj.uniform(n, v);
708 }
709
710 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
711 {
712         obj.uniform(n, v0, v1);
713 }
714
715 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
716 {
717         obj.uniform(n, v0, v1);
718 }
719
720 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
721 {
722         obj.uniform(n, v0, v1, v2);
723 }
724
725 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
726 {
727         obj.uniform(n, v0, v1, v2);
728 }
729
730 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
731 {
732         obj.uniform(n, v0, v1, v2, v3);
733 }
734
735 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
736 {
737         obj.uniform(n, v0, v1, v2, v3);
738 }
739
740 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
741 {
742         ArrayLoader ldr(t, e);
743         load_sub_with(ldr);
744         unsigned size = ldr.get_size();
745         if(!size)
746                 throw logic_error("empty uniform array");
747
748         DataType type = ldr.get_data_type();
749         unsigned elem_size = ldr.get_element_size();
750         if(type==INT)
751         {
752                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
753                 if(elem_size==1)
754                         obj.uniform1_array(n, size, data);
755                 else if(elem_size==2)
756                         obj.uniform2_array(n, size, data);
757                 else if(elem_size==3)
758                         obj.uniform3_array(n, size, data);
759                 else if(elem_size==4)
760                         obj.uniform4_array(n, size, data);
761                 else
762                         throw logic_error("unsupported combination of array type and element size");
763         }
764         else if(type==FLOAT)
765         {
766                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
767                 if(elem_size==1)
768                         obj.uniform1_array(n, size, data);
769                 else if(elem_size==2)
770                         obj.uniform2_array(n, size, data);
771                 else if(elem_size==3)
772                         obj.uniform3_array(n, size, data);
773                 else if(elem_size==4)
774                         obj.uniform4_array(n, size, data);
775                 else
776                         throw logic_error("unsupported combination of array type and element size");
777         }
778         else
779                 throw logic_error("unsupported array type");
780 }
781
782 void ProgramData::Loader::uniform1i_array(const string &n)
783 {
784         uniform_array_(n, INT, 1);
785 }
786
787 void ProgramData::Loader::uniform1f_array(const string &n)
788 {
789         uniform_array_(n, FLOAT, 1);
790 }
791
792 void ProgramData::Loader::uniform2i_array(const string &n)
793 {
794         uniform_array_(n, INT, 2);
795 }
796
797 void ProgramData::Loader::uniform2f_array(const string &n)
798 {
799         uniform_array_(n, FLOAT, 2);
800 }
801
802 void ProgramData::Loader::uniform3i_array(const string &n)
803 {
804         uniform_array_(n, INT, 3);
805 }
806
807 void ProgramData::Loader::uniform3f_array(const string &n)
808 {
809         uniform_array_(n, FLOAT, 3);
810 }
811
812 void ProgramData::Loader::uniform4i_array(const string &n)
813 {
814         uniform_array_(n, INT, 4);
815 }
816
817 void ProgramData::Loader::uniform4f_array(const string &n)
818 {
819         uniform_array_(n, FLOAT, 4);
820 }
821
822 void ProgramData::Loader::uniform_array(const string &n)
823 {
824         uniform_array_(n, static_cast<DataType>(0), 0);
825 }
826
827
828 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
829         type(t),
830         element_size(e)
831 {
832         add("uniform", &ArrayLoader::uniform1i);
833         add("uniform1i", &ArrayLoader::uniform1i);
834         add("uniform", &ArrayLoader::uniform1f);
835         add("uniform1f", &ArrayLoader::uniform1f);
836         add("uniform", &ArrayLoader::uniform2f);
837         add("uniform2f", &ArrayLoader::uniform2f);
838         add("uniform", &ArrayLoader::uniform3f);
839         add("uniform3f", &ArrayLoader::uniform3f);
840         add("uniform", &ArrayLoader::uniform4f);
841         add("uniform4f", &ArrayLoader::uniform4f);
842 }
843
844 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
845 {
846         if(element_size && (t!=type || e!=element_size))
847                 throw logic_error("heterogeneous array contents");
848
849         if(!element_size)
850         {
851                 type = t;
852                 element_size = e;
853         }
854
855         const char *cv = reinterpret_cast<const char *>(v);
856         data.insert(data.end(), cv, cv+element_size*4);
857 }
858
859 void ProgramData::ArrayLoader::uniform1i(int v)
860 {
861         uniform(INT, 1, &v);
862 }
863
864 void ProgramData::ArrayLoader::uniform1f(float v)
865 {
866         uniform(FLOAT, 1, &v);
867 }
868
869 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
870 {
871         int va[2] = { v0, v1 };
872         uniform(INT, 2, va);
873 }
874
875 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
876 {
877         float va[2] = { v0, v1 };
878         uniform(FLOAT, 2, va);
879 }
880
881 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
882 {
883         int va[3] = { v0, v1, v2 };
884         uniform(INT, 3, va);
885 }
886
887 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
888 {
889         float va[3] = { v0, v1, v2 };
890         uniform(FLOAT, 3, va);
891 }
892
893 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
894 {
895         int va[4] = { v0, v1, v2, v3 };
896         uniform(INT, 4, va);
897 }
898
899 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
900 {
901         float va[4] = { v0, v1, v2, v3 };
902         uniform(FLOAT, 4, va);
903 }
904
905 } // namespace GL
906 } // namespace Msp