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