]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Recreate RenderPass shdata if a new program is specified
[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 bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
425 {
426         return nu.name<name;
427 }
428
429 int ProgramData::find_uniform_index(const string &name) const
430 {
431         vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
432         return ((i!=uniforms.end() && i->name==name) ? i-uniforms.begin() : -1);
433 }
434
435 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
436 {
437         UInt8 *indices = block.indices.values;
438         if(info.uniforms.size()>16)
439         {
440                 if(block.indices.type_flag==0xFD)
441                 {
442                         block.indices.dynamic.values = new UInt8[info.uniforms.size()];
443                         block.indices.type_flag = 0xFE;
444                 }
445                 indices = block.indices.dynamic.values;
446         }
447
448         block.used = 0;
449         for(unsigned i=0; i<info.uniforms.size(); ++i)
450         {
451                 int j = find_uniform_index(info.uniforms[i]->name);
452                 if(j>=0)
453                 {
454                         indices[i] = j;
455                         if(static_cast<unsigned>(j)<MASK_BITS)
456                                 block.used |= 1<<j;
457                 }
458                 else
459                         indices[i] = 0xFF;
460         }
461
462         block.dirty = block.used;
463 }
464
465 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
466 {
467         const UInt8 *indices = block.get_uniform_indices();
468         for(unsigned i=0; i<info.uniforms.size(); ++i)
469                 if(indices[i]!=0xFF)
470                         block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
471 }
472
473 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
474 {
475         BlockMap::iterator i = blocks.find(info.layout_hash);
476         if(i==blocks.end())
477         {
478                 bool any_found = false;
479                 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); (!any_found && j!=info.uniforms.end()); ++j)
480                         any_found = (find_uniform_index((*j)->name)>=0);
481
482                 // TODO throw if all uniforms for a buffer-backed block are not found
483                 if(!any_found)
484                         return 0;
485
486                 UniformBlock *block;
487                 if(info.bind_point>=0)
488                 {
489                         if(!buffer)
490                         {
491                                 buffer = new Buffer(UNIFORM_BUFFER);
492                                 buffer->set_usage(STREAM_DRAW);
493                         }
494
495                         block = new UniformBlock(info.data_size);
496                         block->use_buffer(buffer, last_block);
497                         last_block = block;
498                 }
499                 else
500                         block = new UniformBlock;
501
502                 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
503                 update_block_uniform_indices(i->second, info);
504         }
505
506         return &i->second;
507 }
508
509 void ProgramData::apply() const
510 {
511         const Program *prog = Program::current();
512         if(!prog)
513                 throw invalid_operation("ProgramData::apply");
514
515         Program::LayoutHash layout = prog->get_uniform_layout_hash();
516         ProgramUniforms &pu = programs[layout];
517
518         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
519         Mask affected = (dirty&pu.used) | force_dirty;
520         if(affected|pu.dirty)
521         {
522                 /* If the global dirty flag affects this program, add it to per-program
523                 dirty flags and clear the global flag.  A previously unseen program will
524                 always cause this to happen. */
525                 if(affected)
526                 {
527                         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
528                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
529                         for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
530                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
531                         dirty = 0;
532                 }
533
534                 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
535
536                 if(pu.dirty==ALL_ONES)
537                 {
538                         /* The set of uniforms has changed since this program was last used.
539                         Regenerate the list of uniform blocks. */
540                         pu.blocks.clear();
541                         pu.blocks.reserve(prog_blocks.size());
542
543                         pu.used = 0;
544                         for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
545                         {
546                                 SharedBlock *shared = get_shared_block(i->second);
547                                 if(shared)
548                                 {
549                                         if(shared->dirty==ALL_ONES)
550                                                 update_block_uniform_indices(*shared, i->second);
551                                         pu.used |= shared->used;
552                                 }
553
554                                 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
555                         }
556                 }
557
558                 // Update the contents of all dirty blocks.
559                 bool buffered_blocks_updated = false;
560                 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
561                 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
562                 {
563                         if(!j->shared || !j->shared->dirty)
564                                 continue;
565
566                         update_block(*j->shared, i->second);
567                         j->shared->dirty = 0;
568                         buffered_blocks_updated |= (j->bind_point>=0);
569                 }
570
571                 pu.dirty = 0;
572
573                 /* If any blocks stored in the buffer were updated, bind the buffer here
574                 to avoid state thrashing. */
575                 if(buffered_blocks_updated && !ARB_direct_state_access)
576                         buffer->bind();
577         }
578
579         for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
580                 if(i->block)
581                         i->block->apply(i->bind_point);
582 }
583
584
585 ProgramData::NamedUniform::NamedUniform():
586         value(0)
587 { }
588
589 void ProgramData::NamedUniform::replace_value(Uniform *v)
590 {
591         /* UniformBlock does not copy the uniforms, so existing default blocks
592         will be left with stale pointers.  This is not a problem as long as no
593         one stores pointers to the blocks and expects them to stay valid. */
594         delete value;
595         value = v;
596 }
597
598
599 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
600         used(0),
601         dirty(0),
602         block(b)
603 {
604         indices.type_flag = 0xFD;
605 }
606
607 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
608 {
609         return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
610 }
611
612
613 ProgramData::ProgramBlock::ProgramBlock():
614         bind_point(-1),
615         block(0),
616         shared(0)
617 { }
618
619 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
620         bind_point(p),
621         block((b && b->used) ? b->block : 0),
622         shared(b)
623 { }
624
625
626 ProgramData::ProgramUniforms::ProgramUniforms():
627         used(ALL_ONES),
628         dirty(ALL_ONES)
629 { }
630
631
632 ProgramData::Loader::Loader(ProgramData &pd):
633         DataFile::ObjectLoader<ProgramData>(pd)
634 {
635         add("uniform", &Loader::uniform1i);
636         add("uniform1i", &Loader::uniform1i);
637         add("uniform", &Loader::uniform1f);
638         add("uniform1f", &Loader::uniform1f);
639         add("uniform", &Loader::uniform2i);
640         add("uniform2i", &Loader::uniform2i);
641         add("uniform", &Loader::uniform2f);
642         add("uniform2f", &Loader::uniform2f);
643         add("uniform", &Loader::uniform3i);
644         add("uniform3i", &Loader::uniform3i);
645         add("uniform", &Loader::uniform3f);
646         add("uniform3f", &Loader::uniform3f);
647         add("uniform", &Loader::uniform4i);
648         add("uniform4i", &Loader::uniform4i);
649         add("uniform", &Loader::uniform4f);
650         add("uniform4f", &Loader::uniform4f);
651         add("uniform1i_array", &Loader::uniform1i_array);
652         add("uniform1f_array", &Loader::uniform1f_array);
653         add("uniform2f_array", &Loader::uniform2f_array);
654         add("uniform3f_array", &Loader::uniform3f_array);
655         add("uniform4f_array", &Loader::uniform4f_array);
656         add("uniform_array", &Loader::uniform_array);
657 }
658
659 void ProgramData::Loader::uniform1i(const string &n, int v)
660 {
661         obj.uniform(n, v);
662 }
663
664 void ProgramData::Loader::uniform1f(const string &n, float v)
665 {
666         obj.uniform(n, v);
667 }
668
669 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
670 {
671         obj.uniform(n, v0, v1);
672 }
673
674 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
675 {
676         obj.uniform(n, v0, v1);
677 }
678
679 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
680 {
681         obj.uniform(n, v0, v1, v2);
682 }
683
684 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
685 {
686         obj.uniform(n, v0, v1, v2);
687 }
688
689 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
690 {
691         obj.uniform(n, v0, v1, v2, v3);
692 }
693
694 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
695 {
696         obj.uniform(n, v0, v1, v2, v3);
697 }
698
699 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
700 {
701         ArrayLoader ldr(t, e);
702         load_sub_with(ldr);
703         unsigned size = ldr.get_size();
704         if(!size)
705                 throw logic_error("empty uniform array");
706
707         DataType type = ldr.get_data_type();
708         unsigned elem_size = ldr.get_element_size();
709         if(type==INT)
710         {
711                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
712                 if(elem_size==1)
713                         obj.uniform1_array(n, size, data);
714                 else if(elem_size==2)
715                         obj.uniform2_array(n, size, data);
716                 else if(elem_size==3)
717                         obj.uniform3_array(n, size, data);
718                 else if(elem_size==4)
719                         obj.uniform4_array(n, size, data);
720                 else
721                         throw logic_error("unsupported combination of array type and element size");
722         }
723         else if(type==FLOAT)
724         {
725                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
726                 if(elem_size==1)
727                         obj.uniform1_array(n, size, data);
728                 else if(elem_size==2)
729                         obj.uniform2_array(n, size, data);
730                 else if(elem_size==3)
731                         obj.uniform3_array(n, size, data);
732                 else if(elem_size==4)
733                         obj.uniform4_array(n, size, data);
734                 else
735                         throw logic_error("unsupported combination of array type and element size");
736         }
737         else
738                 throw logic_error("unsupported array type");
739 }
740
741 void ProgramData::Loader::uniform1i_array(const string &n)
742 {
743         uniform_array_(n, INT, 1);
744 }
745
746 void ProgramData::Loader::uniform1f_array(const string &n)
747 {
748         uniform_array_(n, FLOAT, 1);
749 }
750
751 void ProgramData::Loader::uniform2i_array(const string &n)
752 {
753         uniform_array_(n, INT, 2);
754 }
755
756 void ProgramData::Loader::uniform2f_array(const string &n)
757 {
758         uniform_array_(n, FLOAT, 2);
759 }
760
761 void ProgramData::Loader::uniform3i_array(const string &n)
762 {
763         uniform_array_(n, INT, 3);
764 }
765
766 void ProgramData::Loader::uniform3f_array(const string &n)
767 {
768         uniform_array_(n, FLOAT, 3);
769 }
770
771 void ProgramData::Loader::uniform4i_array(const string &n)
772 {
773         uniform_array_(n, INT, 4);
774 }
775
776 void ProgramData::Loader::uniform4f_array(const string &n)
777 {
778         uniform_array_(n, FLOAT, 4);
779 }
780
781 void ProgramData::Loader::uniform_array(const string &n)
782 {
783         uniform_array_(n, static_cast<DataType>(0), 0);
784 }
785
786
787 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
788         type(t),
789         element_size(e)
790 {
791         add("uniform", &ArrayLoader::uniform1i);
792         add("uniform1i", &ArrayLoader::uniform1i);
793         add("uniform", &ArrayLoader::uniform1f);
794         add("uniform1f", &ArrayLoader::uniform1f);
795         add("uniform", &ArrayLoader::uniform2f);
796         add("uniform2f", &ArrayLoader::uniform2f);
797         add("uniform", &ArrayLoader::uniform3f);
798         add("uniform3f", &ArrayLoader::uniform3f);
799         add("uniform", &ArrayLoader::uniform4f);
800         add("uniform4f", &ArrayLoader::uniform4f);
801 }
802
803 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
804 {
805         if(element_size && (t!=type || e!=element_size))
806                 throw logic_error("heterogeneous array contents");
807
808         if(!element_size)
809         {
810                 type = t;
811                 element_size = e;
812         }
813
814         const char *cv = reinterpret_cast<const char *>(v);
815         data.insert(data.end(), cv, cv+element_size*4);
816 }
817
818 void ProgramData::ArrayLoader::uniform1i(int v)
819 {
820         uniform(INT, 1, &v);
821 }
822
823 void ProgramData::ArrayLoader::uniform1f(float v)
824 {
825         uniform(FLOAT, 1, &v);
826 }
827
828 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
829 {
830         int va[2] = { v0, v1 };
831         uniform(INT, 2, va);
832 }
833
834 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
835 {
836         float va[2] = { v0, v1 };
837         uniform(FLOAT, 2, va);
838 }
839
840 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
841 {
842         int va[3] = { v0, v1, v2 };
843         uniform(INT, 3, va);
844 }
845
846 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
847 {
848         float va[3] = { v0, v1, v2 };
849         uniform(FLOAT, 3, va);
850 }
851
852 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
853 {
854         int va[4] = { v0, v1, v2, v3 };
855         uniform(INT, 4, va);
856 }
857
858 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
859 {
860         float va[4] = { v0, v1, v2, v3 };
861         uniform(FLOAT, 4, va);
862 }
863
864 } // namespace GL
865 } // namespace Msp