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