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