]> git.tdb.fi Git - libs/gl.git/blob - source/render/programdata.cpp
Split UniformBlock into two classes
[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_buffer_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_buffer_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_buffer_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_buffer_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                         BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size);
598                         block.block = bb_block;
599                         bb_block->use_buffer(buffer, last_buffer_block);
600                         last_buffer_block = bb_block;
601                 }
602                 else
603                         block.block = new DefaultUniformBlock;
604         }
605 }
606
607 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
608 {
609         const UInt8 *indices = block.get_uniform_indices();
610         for(unsigned i=0; i<info.uniforms.size(); ++i)
611                 if(indices[i]!=0xFF)
612                         block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
613 }
614
615 void ProgramData::apply() const
616 {
617         const Program *prog = Program::current();
618         if(!prog)
619                 throw invalid_operation("ProgramData::apply");
620
621         BufferBackedUniformBlock *old_last_block = last_buffer_block;
622         vector<ProgramBlock>::iterator prog_begin = get_program(*prog);
623         Program::LayoutHash prog_hash = prog->get_uniform_layout_hash();
624
625         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
626         Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
627         if(affected|prog_begin->masks.dirty)
628         {
629                 /* If the global dirty flag affects this program, add it to per-block and
630                 per-program dirty flags and clear the global flag.  A previously unseen
631                 program will cause this to happen if there's any dirty uniforms. */
632                 if(affected)
633                 {
634                         for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
635                                 i->dirty |= (dirty&i->used) | force_dirty;
636                         for(vector<ProgramBlock>::iterator i=programs.begin(); i!=programs.end(); ++i)
637                                 if(i->block_index<0)
638                                         i->masks.dirty |= (dirty&i->masks.used) | force_dirty;
639                         dirty = 0;
640                 }
641
642                 const vector<Program::UniformBlockInfo> &block_infos = prog->get_uniform_blocks();
643
644                 if(prog_begin->masks.dirty==ALL_ONES)
645                 {
646                         /* The set of uniforms has changed since this program was last used.
647                         Refresh uniform indices within the program's blocks. */
648                         prog_begin->masks.used = 0;
649                         vector<ProgramBlock>::iterator j = prog_begin+1;
650                         for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
651                         {
652                                 SharedBlock &shared = blocks[j->block_index];
653                                 if(shared.dirty==ALL_ONES)
654                                         update_block_uniform_indices(shared, *i);
655                                 prog_begin->masks.used |= shared.used;
656                                 j->block = (shared.used ? shared.block : 0);
657                         }
658                 }
659
660                 // Update the contents of all dirty blocks.
661                 bool buffered_blocks_updated = false;
662                 vector<ProgramBlock>::iterator j = prog_begin+1;
663                 for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
664                 {
665                         SharedBlock &shared = blocks[j->block_index];
666                         if(shared.dirty)
667                         {
668                                 update_block(shared, *i);
669                                 shared.dirty = 0;
670                                 buffered_blocks_updated |= (j->bind_point>=0);
671                         }
672                 }
673
674                 prog_begin->masks.dirty = 0;
675
676                 /* If any blocks stored in the buffer were updated, bind the buffer here
677                 to avoid state thrashing. */
678                 if(buffered_blocks_updated && !ARB_direct_state_access)
679                         buffer->bind();
680
681                 if(last_buffer_block!=old_last_block)
682                 {
683                         unsigned required_size = last_buffer_block->get_required_buffer_size();
684                         if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
685                         {
686                                 if(buffer->get_size()>0)
687                                 {
688                                         delete buffer;
689                                         buffer = new Buffer(UNIFORM_BUFFER);
690                                         last_buffer_block->change_buffer(buffer);
691                                 }
692
693                                 buffer->storage(required_size);
694                         }
695                 }
696         }
697
698         for(vector<ProgramBlock>::iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
699                 if(i->block)
700                         i->block->apply(i->bind_point);
701 }
702
703
704 ProgramData::TaggedUniform::TaggedUniform():
705         value(0)
706 { }
707
708 void ProgramData::TaggedUniform::replace_value(Uniform *v)
709 {
710         /* UniformBlock does not copy the uniforms, so existing default blocks
711         will be left with stale pointers.  This is not a problem as long as no
712         one stores pointers to the blocks and expects them to stay valid. */
713         delete value;
714         value = v;
715 }
716
717
718 ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h):
719         block_hash(h),
720         used(0),
721         dirty(0),
722         block(0)
723 {
724         indices.type_flag = 0xFD;
725 }
726
727 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
728 {
729         return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
730 }
731
732
733 ProgramData::ProgramBlock::ProgramBlock(Program::LayoutHash h):
734         prog_hash(h),
735         bind_point(-1),
736         block_index(-1)
737 {
738         masks.used = ALL_ONES;
739         masks.dirty = ALL_ONES;
740 }
741
742
743 ProgramData::Loader::Loader(ProgramData &pd):
744         DataFile::ObjectLoader<ProgramData>(pd)
745 {
746         add("uniform", &Loader::uniform1i);
747         add("uniform1i", &Loader::uniform1i);
748         add("uniform", &Loader::uniform1f);
749         add("uniform1f", &Loader::uniform1f);
750         add("uniform", &Loader::uniform2i);
751         add("uniform2i", &Loader::uniform2i);
752         add("uniform", &Loader::uniform2f);
753         add("uniform2f", &Loader::uniform2f);
754         add("uniform", &Loader::uniform3i);
755         add("uniform3i", &Loader::uniform3i);
756         add("uniform", &Loader::uniform3f);
757         add("uniform3f", &Loader::uniform3f);
758         add("uniform", &Loader::uniform4i);
759         add("uniform4i", &Loader::uniform4i);
760         add("uniform", &Loader::uniform4f);
761         add("uniform4f", &Loader::uniform4f);
762         add("uniform1i_array", &Loader::uniform1i_array);
763         add("uniform1f_array", &Loader::uniform1f_array);
764         add("uniform2f_array", &Loader::uniform2f_array);
765         add("uniform3f_array", &Loader::uniform3f_array);
766         add("uniform4f_array", &Loader::uniform4f_array);
767         add("uniform_array", &Loader::uniform_array);
768 }
769
770 void ProgramData::Loader::uniform1i(const string &n, int v)
771 {
772         obj.uniform(n, v);
773 }
774
775 void ProgramData::Loader::uniform1f(const string &n, float v)
776 {
777         obj.uniform(n, v);
778 }
779
780 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
781 {
782         obj.uniform(n, v0, v1);
783 }
784
785 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
786 {
787         obj.uniform(n, v0, v1);
788 }
789
790 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
791 {
792         obj.uniform(n, v0, v1, v2);
793 }
794
795 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
796 {
797         obj.uniform(n, v0, v1, v2);
798 }
799
800 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
801 {
802         obj.uniform(n, v0, v1, v2, v3);
803 }
804
805 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
806 {
807         obj.uniform(n, v0, v1, v2, v3);
808 }
809
810 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
811 {
812         ArrayLoader ldr(t, e);
813         load_sub_with(ldr);
814         unsigned size = ldr.get_size();
815         if(!size)
816                 throw logic_error("empty uniform array");
817
818         DataType type = ldr.get_data_type();
819         unsigned elem_size = ldr.get_element_size();
820         if(type==INT)
821         {
822                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
823                 if(elem_size==1)
824                         obj.uniform1_array(n, size, data);
825                 else if(elem_size==2)
826                         obj.uniform2_array(n, size, data);
827                 else if(elem_size==3)
828                         obj.uniform3_array(n, size, data);
829                 else if(elem_size==4)
830                         obj.uniform4_array(n, size, data);
831                 else
832                         throw logic_error("unsupported combination of array type and element size");
833         }
834         else if(type==FLOAT)
835         {
836                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
837                 if(elem_size==1)
838                         obj.uniform1_array(n, size, data);
839                 else if(elem_size==2)
840                         obj.uniform2_array(n, size, data);
841                 else if(elem_size==3)
842                         obj.uniform3_array(n, size, data);
843                 else if(elem_size==4)
844                         obj.uniform4_array(n, size, data);
845                 else
846                         throw logic_error("unsupported combination of array type and element size");
847         }
848         else
849                 throw logic_error("unsupported array type");
850 }
851
852 void ProgramData::Loader::uniform1i_array(const string &n)
853 {
854         uniform_array_(n, INT, 1);
855 }
856
857 void ProgramData::Loader::uniform1f_array(const string &n)
858 {
859         uniform_array_(n, FLOAT, 1);
860 }
861
862 void ProgramData::Loader::uniform2i_array(const string &n)
863 {
864         uniform_array_(n, INT, 2);
865 }
866
867 void ProgramData::Loader::uniform2f_array(const string &n)
868 {
869         uniform_array_(n, FLOAT, 2);
870 }
871
872 void ProgramData::Loader::uniform3i_array(const string &n)
873 {
874         uniform_array_(n, INT, 3);
875 }
876
877 void ProgramData::Loader::uniform3f_array(const string &n)
878 {
879         uniform_array_(n, FLOAT, 3);
880 }
881
882 void ProgramData::Loader::uniform4i_array(const string &n)
883 {
884         uniform_array_(n, INT, 4);
885 }
886
887 void ProgramData::Loader::uniform4f_array(const string &n)
888 {
889         uniform_array_(n, FLOAT, 4);
890 }
891
892 void ProgramData::Loader::uniform_array(const string &n)
893 {
894         uniform_array_(n, static_cast<DataType>(0), 0);
895 }
896
897
898 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
899         type(t),
900         element_size(e)
901 {
902         add("uniform", &ArrayLoader::uniform1i);
903         add("uniform1i", &ArrayLoader::uniform1i);
904         add("uniform", &ArrayLoader::uniform1f);
905         add("uniform1f", &ArrayLoader::uniform1f);
906         add("uniform", &ArrayLoader::uniform2f);
907         add("uniform2f", &ArrayLoader::uniform2f);
908         add("uniform", &ArrayLoader::uniform3f);
909         add("uniform3f", &ArrayLoader::uniform3f);
910         add("uniform", &ArrayLoader::uniform4f);
911         add("uniform4f", &ArrayLoader::uniform4f);
912 }
913
914 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
915 {
916         if(element_size && (t!=type || e!=element_size))
917                 throw logic_error("heterogeneous array contents");
918
919         if(!element_size)
920         {
921                 type = t;
922                 element_size = e;
923         }
924
925         const char *cv = reinterpret_cast<const char *>(v);
926         data.insert(data.end(), cv, cv+element_size*4);
927 }
928
929 void ProgramData::ArrayLoader::uniform1i(int v)
930 {
931         uniform(INT, 1, &v);
932 }
933
934 void ProgramData::ArrayLoader::uniform1f(float v)
935 {
936         uniform(FLOAT, 1, &v);
937 }
938
939 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
940 {
941         int va[2] = { v0, v1 };
942         uniform(INT, 2, va);
943 }
944
945 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
946 {
947         float va[2] = { v0, v1 };
948         uniform(FLOAT, 2, va);
949 }
950
951 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
952 {
953         int va[3] = { v0, v1, v2 };
954         uniform(INT, 3, va);
955 }
956
957 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
958 {
959         float va[3] = { v0, v1, v2 };
960         uniform(FLOAT, 3, va);
961 }
962
963 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
964 {
965         int va[4] = { v0, v1, v2, v3 };
966         uniform(INT, 4, va);
967 }
968
969 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
970 {
971         float va[4] = { v0, v1, v2, v3 };
972         uniform(FLOAT, 4, va);
973 }
974
975 } // namespace GL
976 } // namespace Msp