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