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