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