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