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