]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Make setting a nonexistent uniform only a warning in debug builds
[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                 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); (!any_found && j!=info.uniforms.end()); ++j)
493                         any_found = (find_uniform_index((*j)->name)>=0);
494
495                 // TODO throw if all uniforms for a buffer-backed block are not found
496                 if(!any_found)
497                         return 0;
498
499                 UniformBlock *block;
500                 if(info.bind_point>=0)
501                 {
502                         if(!buffer)
503                         {
504                                 buffer = new Buffer(UNIFORM_BUFFER);
505                                 buffer->set_usage(STREAM_DRAW);
506                         }
507
508                         block = new UniformBlock(info.data_size);
509                         block->use_buffer(buffer, last_block);
510                         last_block = block;
511                 }
512                 else
513                         block = new UniformBlock;
514
515                 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
516                 update_block_uniform_indices(i->second, info);
517         }
518
519         return &i->second;
520 }
521
522 void ProgramData::apply() const
523 {
524         const Program *prog = Program::current();
525         if(!prog)
526                 throw invalid_operation("ProgramData::apply");
527
528         Program::LayoutHash layout = prog->get_uniform_layout_hash();
529         ProgramUniforms &pu = programs[layout];
530
531         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
532         Mask affected = (dirty&pu.used) | force_dirty;
533         if(affected|pu.dirty)
534         {
535                 /* If the global dirty flag affects this program, add it to per-program
536                 dirty flags and clear the global flag.  A previously unseen program will
537                 always cause this to happen. */
538                 if(affected)
539                 {
540                         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
541                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
542                         for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
543                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
544                         dirty = 0;
545                 }
546
547                 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
548
549                 if(pu.dirty==ALL_ONES)
550                 {
551                         /* The set of uniforms has changed since this program was last used.
552                         Regenerate the list of uniform blocks. */
553                         pu.blocks.clear();
554                         pu.blocks.reserve(prog_blocks.size());
555
556                         pu.used = 0;
557                         for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
558                         {
559                                 SharedBlock *shared = get_shared_block(i->second);
560                                 if(shared)
561                                 {
562                                         if(shared->dirty==ALL_ONES)
563                                                 update_block_uniform_indices(*shared, i->second);
564                                         pu.used |= shared->used;
565                                 }
566
567                                 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
568                         }
569                 }
570
571                 // Update the contents of all dirty blocks.
572                 bool buffered_blocks_updated = false;
573                 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
574                 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
575                 {
576                         if(!j->shared || !j->shared->dirty)
577                                 continue;
578
579                         update_block(*j->shared, i->second);
580                         j->shared->dirty = 0;
581                         buffered_blocks_updated |= (j->bind_point>=0);
582                 }
583
584                 pu.dirty = 0;
585
586                 /* If any blocks stored in the buffer were updated, bind the buffer here
587                 to avoid state thrashing. */
588                 if(buffered_blocks_updated && !ARB_direct_state_access)
589                         buffer->bind();
590         }
591
592         for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
593                 if(i->block)
594                         i->block->apply(i->bind_point);
595 }
596
597
598 ProgramData::NamedUniform::NamedUniform():
599         value(0)
600 { }
601
602 void ProgramData::NamedUniform::replace_value(Uniform *v)
603 {
604         /* UniformBlock does not copy the uniforms, so existing default blocks
605         will be left with stale pointers.  This is not a problem as long as no
606         one stores pointers to the blocks and expects them to stay valid. */
607         delete value;
608         value = v;
609 }
610
611
612 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
613         used(0),
614         dirty(0),
615         block(b)
616 {
617         indices.type_flag = 0xFD;
618 }
619
620 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
621 {
622         return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
623 }
624
625
626 ProgramData::ProgramBlock::ProgramBlock():
627         bind_point(-1),
628         block(0),
629         shared(0)
630 { }
631
632 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
633         bind_point(p),
634         block((b && b->used) ? b->block : 0),
635         shared(b)
636 { }
637
638
639 ProgramData::ProgramUniforms::ProgramUniforms():
640         used(ALL_ONES),
641         dirty(ALL_ONES)
642 { }
643
644
645 ProgramData::Loader::Loader(ProgramData &pd):
646         DataFile::ObjectLoader<ProgramData>(pd)
647 {
648         add("uniform", &Loader::uniform1i);
649         add("uniform1i", &Loader::uniform1i);
650         add("uniform", &Loader::uniform1f);
651         add("uniform1f", &Loader::uniform1f);
652         add("uniform", &Loader::uniform2i);
653         add("uniform2i", &Loader::uniform2i);
654         add("uniform", &Loader::uniform2f);
655         add("uniform2f", &Loader::uniform2f);
656         add("uniform", &Loader::uniform3i);
657         add("uniform3i", &Loader::uniform3i);
658         add("uniform", &Loader::uniform3f);
659         add("uniform3f", &Loader::uniform3f);
660         add("uniform", &Loader::uniform4i);
661         add("uniform4i", &Loader::uniform4i);
662         add("uniform", &Loader::uniform4f);
663         add("uniform4f", &Loader::uniform4f);
664         add("uniform1i_array", &Loader::uniform1i_array);
665         add("uniform1f_array", &Loader::uniform1f_array);
666         add("uniform2f_array", &Loader::uniform2f_array);
667         add("uniform3f_array", &Loader::uniform3f_array);
668         add("uniform4f_array", &Loader::uniform4f_array);
669         add("uniform_array", &Loader::uniform_array);
670 }
671
672 void ProgramData::Loader::uniform1i(const string &n, int v)
673 {
674         obj.uniform(n, v);
675 }
676
677 void ProgramData::Loader::uniform1f(const string &n, float v)
678 {
679         obj.uniform(n, v);
680 }
681
682 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
683 {
684         obj.uniform(n, v0, v1);
685 }
686
687 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
688 {
689         obj.uniform(n, v0, v1);
690 }
691
692 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
693 {
694         obj.uniform(n, v0, v1, v2);
695 }
696
697 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
698 {
699         obj.uniform(n, v0, v1, v2);
700 }
701
702 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
703 {
704         obj.uniform(n, v0, v1, v2, v3);
705 }
706
707 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
708 {
709         obj.uniform(n, v0, v1, v2, v3);
710 }
711
712 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
713 {
714         ArrayLoader ldr(t, e);
715         load_sub_with(ldr);
716         unsigned size = ldr.get_size();
717         if(!size)
718                 throw logic_error("empty uniform array");
719
720         DataType type = ldr.get_data_type();
721         unsigned elem_size = ldr.get_element_size();
722         if(type==INT)
723         {
724                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
725                 if(elem_size==1)
726                         obj.uniform1_array(n, size, data);
727                 else if(elem_size==2)
728                         obj.uniform2_array(n, size, data);
729                 else if(elem_size==3)
730                         obj.uniform3_array(n, size, data);
731                 else if(elem_size==4)
732                         obj.uniform4_array(n, size, data);
733                 else
734                         throw logic_error("unsupported combination of array type and element size");
735         }
736         else if(type==FLOAT)
737         {
738                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
739                 if(elem_size==1)
740                         obj.uniform1_array(n, size, data);
741                 else if(elem_size==2)
742                         obj.uniform2_array(n, size, data);
743                 else if(elem_size==3)
744                         obj.uniform3_array(n, size, data);
745                 else if(elem_size==4)
746                         obj.uniform4_array(n, size, data);
747                 else
748                         throw logic_error("unsupported combination of array type and element size");
749         }
750         else
751                 throw logic_error("unsupported array type");
752 }
753
754 void ProgramData::Loader::uniform1i_array(const string &n)
755 {
756         uniform_array_(n, INT, 1);
757 }
758
759 void ProgramData::Loader::uniform1f_array(const string &n)
760 {
761         uniform_array_(n, FLOAT, 1);
762 }
763
764 void ProgramData::Loader::uniform2i_array(const string &n)
765 {
766         uniform_array_(n, INT, 2);
767 }
768
769 void ProgramData::Loader::uniform2f_array(const string &n)
770 {
771         uniform_array_(n, FLOAT, 2);
772 }
773
774 void ProgramData::Loader::uniform3i_array(const string &n)
775 {
776         uniform_array_(n, INT, 3);
777 }
778
779 void ProgramData::Loader::uniform3f_array(const string &n)
780 {
781         uniform_array_(n, FLOAT, 3);
782 }
783
784 void ProgramData::Loader::uniform4i_array(const string &n)
785 {
786         uniform_array_(n, INT, 4);
787 }
788
789 void ProgramData::Loader::uniform4f_array(const string &n)
790 {
791         uniform_array_(n, FLOAT, 4);
792 }
793
794 void ProgramData::Loader::uniform_array(const string &n)
795 {
796         uniform_array_(n, static_cast<DataType>(0), 0);
797 }
798
799
800 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
801         type(t),
802         element_size(e)
803 {
804         add("uniform", &ArrayLoader::uniform1i);
805         add("uniform1i", &ArrayLoader::uniform1i);
806         add("uniform", &ArrayLoader::uniform1f);
807         add("uniform1f", &ArrayLoader::uniform1f);
808         add("uniform", &ArrayLoader::uniform2f);
809         add("uniform2f", &ArrayLoader::uniform2f);
810         add("uniform", &ArrayLoader::uniform3f);
811         add("uniform3f", &ArrayLoader::uniform3f);
812         add("uniform", &ArrayLoader::uniform4f);
813         add("uniform4f", &ArrayLoader::uniform4f);
814 }
815
816 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
817 {
818         if(element_size && (t!=type || e!=element_size))
819                 throw logic_error("heterogeneous array contents");
820
821         if(!element_size)
822         {
823                 type = t;
824                 element_size = e;
825         }
826
827         const char *cv = reinterpret_cast<const char *>(v);
828         data.insert(data.end(), cv, cv+element_size*4);
829 }
830
831 void ProgramData::ArrayLoader::uniform1i(int v)
832 {
833         uniform(INT, 1, &v);
834 }
835
836 void ProgramData::ArrayLoader::uniform1f(float v)
837 {
838         uniform(FLOAT, 1, &v);
839 }
840
841 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
842 {
843         int va[2] = { v0, v1 };
844         uniform(INT, 2, va);
845 }
846
847 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
848 {
849         float va[2] = { v0, v1 };
850         uniform(FLOAT, 2, va);
851 }
852
853 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
854 {
855         int va[3] = { v0, v1, v2 };
856         uniform(INT, 3, va);
857 }
858
859 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
860 {
861         float va[3] = { v0, v1, v2 };
862         uniform(FLOAT, 3, va);
863 }
864
865 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
866 {
867         int va[4] = { v0, v1, v2, v3 };
868         uniform(INT, 4, va);
869 }
870
871 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
872 {
873         float va[4] = { v0, v1, v2, v3 };
874         uniform(FLOAT, 4, va);
875 }
876
877 } // namespace GL
878 } // namespace Msp