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