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