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