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