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