]> git.tdb.fi Git - libs/gl.git/blob - source/render/programdata.cpp
Remove the notion of default binding point from Buffer
[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                         {
569                                 buffer = new Buffer();
570
571 #ifdef DEBUG
572                                 if(!debug_name.empty())
573                                         buffer->set_debug_name(debug_name);
574 #endif
575                         }
576
577                         BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size);
578                         block.block = bb_block;
579                         bb_block->use_buffer(buffer, last_buffer_block);
580                         last_buffer_block = bb_block;
581                 }
582                 else
583                         block.block = new DefaultUniformBlock;
584         }
585 }
586
587 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
588 {
589         const UInt8 *indices = block.get_uniform_indices();
590         for(unsigned i=0; i<info.uniforms.size(); ++i)
591         {
592                 if(is_image(info.uniforms[i]->type))
593                         ;  // Temporarily ignore deprecated use of sampler uniforms in ProgramData
594                 else if(indices[i]!=0xFF)
595                         block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
596         }
597 }
598
599 void ProgramData::apply() const
600 {
601         const Program *prog = Program::current();
602         if(!prog)
603                 throw invalid_operation("ProgramData::apply");
604
605         BufferBackedUniformBlock *old_last_block = last_buffer_block;
606         vector<ProgramBlock>::iterator prog_begin = get_program(*prog);
607         Program::LayoutHash prog_hash = prog->get_uniform_layout_hash();
608
609         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
610         Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
611         if(affected|prog_begin->masks.dirty)
612         {
613                 /* If the global dirty flag affects this program, add it to per-block and
614                 per-program dirty flags and clear the global flag.  A previously unseen
615                 program will cause this to happen if there's any dirty uniforms. */
616                 if(affected)
617                 {
618                         for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
619                                 i->dirty |= (dirty&i->used) | force_dirty;
620                         for(vector<ProgramBlock>::iterator i=programs.begin(); i!=programs.end(); ++i)
621                                 if(i->block_index<0)
622                                         i->masks.dirty |= (dirty&i->masks.used) | force_dirty;
623                         dirty = 0;
624                 }
625
626                 const vector<Program::UniformBlockInfo> &block_infos = prog->get_uniform_blocks();
627
628                 if(prog_begin->masks.dirty==ALL_ONES)
629                 {
630                         /* The set of uniforms has changed since this program was last used.
631                         Refresh uniform indices within the program's blocks. */
632                         prog_begin->masks.used = 0;
633                         vector<ProgramBlock>::iterator j = prog_begin+1;
634                         for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
635                         {
636                                 SharedBlock &shared = blocks[j->block_index];
637                                 if(shared.dirty==ALL_ONES)
638                                         update_block_uniform_indices(shared, *i);
639                                 prog_begin->masks.used |= shared.used;
640                                 j->block = (shared.used ? shared.block : 0);
641                         }
642                 }
643
644                 // Update the contents of all dirty blocks.
645                 bool buffered_blocks_updated = false;
646                 vector<ProgramBlock>::iterator j = prog_begin+1;
647                 for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
648                 {
649                         SharedBlock &shared = blocks[j->block_index];
650                         if(shared.dirty)
651                         {
652                                 update_block(shared, *i);
653                                 shared.dirty = 0;
654                                 buffered_blocks_updated |= (j->bind_point>=0);
655                         }
656                 }
657
658                 prog_begin->masks.dirty = 0;
659
660                 if(last_buffer_block!=old_last_block)
661                 {
662                         unsigned required_size = last_buffer_block->get_required_buffer_size();
663                         if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
664                         {
665                                 if(buffer->get_size()>0)
666                                 {
667                                         delete buffer;
668                                         buffer = new Buffer();
669                                         last_buffer_block->change_buffer(buffer);
670
671 #ifdef DEBUG
672                                         if(!debug_name.empty())
673                                                 buffer->set_debug_name(debug_name);
674 #endif
675                                 }
676
677                                 buffer->storage(required_size);
678                         }
679                 }
680         }
681
682         for(vector<ProgramBlock>::iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
683                 if(i->block)
684                         i->block->apply(i->bind_point);
685 }
686
687 void ProgramData::set_debug_name(const string &name)
688 {
689 #ifdef DEBUG
690         debug_name = name;
691         if(buffer)
692                 buffer->set_debug_name(name);
693 #else
694         (void)name;
695 #endif
696 }
697
698
699 ProgramData::TaggedUniform::TaggedUniform():
700         value(0)
701 { }
702
703 void ProgramData::TaggedUniform::replace_value(Uniform *v)
704 {
705         /* UniformBlock does not copy the uniforms, so existing default blocks
706         will be left with stale pointers.  This is not a problem as long as no
707         one stores pointers to the blocks and expects them to stay valid. */
708         delete value;
709         value = v;
710 }
711
712
713 ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h):
714         block_hash(h),
715         used(0),
716         dirty(0),
717         block(0)
718 {
719         indices.type_flag = 0xFD;
720 }
721
722 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
723 {
724         return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
725 }
726
727
728 ProgramData::ProgramBlock::ProgramBlock(Program::LayoutHash h):
729         prog_hash(h),
730         bind_point(-1),
731         block_index(-1)
732 {
733         masks.used = ALL_ONES;
734         masks.dirty = ALL_ONES;
735 }
736
737
738 ProgramData::Loader::Loader(ProgramData &pd):
739         DataFile::ObjectLoader<ProgramData>(pd)
740 {
741         add("uniform", &Loader::uniform1i);
742         add("uniform1i", &Loader::uniform1i);
743         add("uniform", &Loader::uniform1f);
744         add("uniform1f", &Loader::uniform1f);
745         add("uniform", &Loader::uniform2i);
746         add("uniform2i", &Loader::uniform2i);
747         add("uniform", &Loader::uniform2f);
748         add("uniform2f", &Loader::uniform2f);
749         add("uniform", &Loader::uniform3i);
750         add("uniform3i", &Loader::uniform3i);
751         add("uniform", &Loader::uniform3f);
752         add("uniform3f", &Loader::uniform3f);
753         add("uniform", &Loader::uniform4i);
754         add("uniform4i", &Loader::uniform4i);
755         add("uniform", &Loader::uniform4f);
756         add("uniform4f", &Loader::uniform4f);
757         add("uniform1i_array", &Loader::uniform1i_array);
758         add("uniform1f_array", &Loader::uniform1f_array);
759         add("uniform2f_array", &Loader::uniform2f_array);
760         add("uniform3f_array", &Loader::uniform3f_array);
761         add("uniform4f_array", &Loader::uniform4f_array);
762         add("uniform_array", &Loader::uniform_array);
763 }
764
765 void ProgramData::Loader::uniform1i(const string &n, int v)
766 {
767         obj.uniform(n, v);
768 }
769
770 void ProgramData::Loader::uniform1f(const string &n, float v)
771 {
772         obj.uniform(n, v);
773 }
774
775 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
776 {
777         obj.uniform(n, v0, v1);
778 }
779
780 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
781 {
782         obj.uniform(n, v0, v1);
783 }
784
785 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
786 {
787         obj.uniform(n, v0, v1, v2);
788 }
789
790 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
791 {
792         obj.uniform(n, v0, v1, v2);
793 }
794
795 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
796 {
797         obj.uniform(n, v0, v1, v2, v3);
798 }
799
800 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
801 {
802         obj.uniform(n, v0, v1, v2, v3);
803 }
804
805 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
806 {
807         ArrayLoader ldr(t, e);
808         load_sub_with(ldr);
809         unsigned size = ldr.get_size();
810         if(!size)
811                 throw logic_error("empty uniform array");
812
813         DataType type = ldr.get_data_type();
814         unsigned elem_size = ldr.get_element_size();
815         if(type==INT)
816         {
817                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
818                 if(elem_size==1)
819                         obj.uniform1_array(n, size, data);
820                 else if(elem_size==2)
821                         obj.uniform2_array(n, size, data);
822                 else if(elem_size==3)
823                         obj.uniform3_array(n, size, data);
824                 else if(elem_size==4)
825                         obj.uniform4_array(n, size, data);
826                 else
827                         throw logic_error("unsupported combination of array type and element size");
828         }
829         else if(type==FLOAT)
830         {
831                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
832                 if(elem_size==1)
833                         obj.uniform1_array(n, size, data);
834                 else if(elem_size==2)
835                         obj.uniform2_array(n, size, data);
836                 else if(elem_size==3)
837                         obj.uniform3_array(n, size, data);
838                 else if(elem_size==4)
839                         obj.uniform4_array(n, size, data);
840                 else
841                         throw logic_error("unsupported combination of array type and element size");
842         }
843         else
844                 throw logic_error("unsupported array type");
845 }
846
847 void ProgramData::Loader::uniform1i_array(const string &n)
848 {
849         uniform_array_(n, INT, 1);
850 }
851
852 void ProgramData::Loader::uniform1f_array(const string &n)
853 {
854         uniform_array_(n, FLOAT, 1);
855 }
856
857 void ProgramData::Loader::uniform2i_array(const string &n)
858 {
859         uniform_array_(n, INT, 2);
860 }
861
862 void ProgramData::Loader::uniform2f_array(const string &n)
863 {
864         uniform_array_(n, FLOAT, 2);
865 }
866
867 void ProgramData::Loader::uniform3i_array(const string &n)
868 {
869         uniform_array_(n, INT, 3);
870 }
871
872 void ProgramData::Loader::uniform3f_array(const string &n)
873 {
874         uniform_array_(n, FLOAT, 3);
875 }
876
877 void ProgramData::Loader::uniform4i_array(const string &n)
878 {
879         uniform_array_(n, INT, 4);
880 }
881
882 void ProgramData::Loader::uniform4f_array(const string &n)
883 {
884         uniform_array_(n, FLOAT, 4);
885 }
886
887 void ProgramData::Loader::uniform_array(const string &n)
888 {
889         uniform_array_(n, static_cast<DataType>(0), 0);
890 }
891
892
893 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
894         type(t),
895         element_size(e)
896 {
897         add("uniform", &ArrayLoader::uniform1i);
898         add("uniform1i", &ArrayLoader::uniform1i);
899         add("uniform", &ArrayLoader::uniform1f);
900         add("uniform1f", &ArrayLoader::uniform1f);
901         add("uniform", &ArrayLoader::uniform2f);
902         add("uniform2f", &ArrayLoader::uniform2f);
903         add("uniform", &ArrayLoader::uniform3f);
904         add("uniform3f", &ArrayLoader::uniform3f);
905         add("uniform", &ArrayLoader::uniform4f);
906         add("uniform4f", &ArrayLoader::uniform4f);
907 }
908
909 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
910 {
911         if(element_size && (t!=type || e!=element_size))
912                 throw logic_error("heterogeneous array contents");
913
914         if(!element_size)
915         {
916                 type = t;
917                 element_size = e;
918         }
919
920         const char *cv = reinterpret_cast<const char *>(v);
921         data.insert(data.end(), cv, cv+element_size*4);
922 }
923
924 void ProgramData::ArrayLoader::uniform1i(int v)
925 {
926         uniform(INT, 1, &v);
927 }
928
929 void ProgramData::ArrayLoader::uniform1f(float v)
930 {
931         uniform(FLOAT, 1, &v);
932 }
933
934 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
935 {
936         int va[2] = { v0, v1 };
937         uniform(INT, 2, va);
938 }
939
940 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
941 {
942         float va[2] = { v0, v1 };
943         uniform(FLOAT, 2, va);
944 }
945
946 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
947 {
948         int va[3] = { v0, v1, v2 };
949         uniform(INT, 3, va);
950 }
951
952 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
953 {
954         float va[3] = { v0, v1, v2 };
955         uniform(FLOAT, 3, va);
956 }
957
958 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
959 {
960         int va[4] = { v0, v1, v2, v3 };
961         uniform(INT, 4, va);
962 }
963
964 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
965 {
966         float va[4] = { v0, v1, v2, v3 };
967         uniform(FLOAT, 4, va);
968 }
969
970 } // namespace GL
971 } // namespace Msp