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