]> git.tdb.fi Git - libs/gl.git/blob - source/render/programdata.cpp
Add correct copy and move semantics to most classes
[libs/gl.git] / source / render / programdata.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/io/print.h>
4 #include "buffer.h"
5 #include "color.h"
6 #include "error.h"
7 #include "matrix.h"
8 #include "pipelinestate.h"
9 #include "program.h"
10 #include "programdata.h"
11 #include "uniformblock.h"
12 #include "vector.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 ProgramData::ProgramData(const Program *p):
20         tied_program(p)
21 { }
22
23 // Blocks are intentionally left uncopied
24 ProgramData::ProgramData(const ProgramData &other):
25         tied_program(other.tied_program),
26         uniforms(other.uniforms),
27         uniform_data(other.uniform_data),
28         generation(other.generation)
29 { }
30
31 ProgramData::ProgramData(const ProgramData &other, const Program *p):
32         tied_program(p)
33 {
34         if(tied_program)
35         {
36                 for(const TaggedUniform &u: other.uniforms)
37                         validate_tag(u.tag);
38         }
39
40         uniforms = other.uniforms;
41         uniform_data = other.uniform_data;
42 }
43
44 ProgramData &ProgramData::operator=(const ProgramData &other)
45 {
46         tied_program = other.tied_program;
47
48         uniforms = other.uniforms;
49         uniform_data = other.uniform_data;
50
51         for(SharedBlock &b: blocks)
52                 delete b.block;
53         blocks.clear();
54         programs.clear();
55
56         last_buffer_block = 0;
57         buffer = 0;
58         dirty = 0;
59
60         return *this;
61 }
62
63 ProgramData::ProgramData(ProgramData &&other):
64         tied_program(other.tied_program),
65         uniforms(move(other.uniforms)),
66         uniform_data(move(other.uniform_data)),
67         generation(other.generation),
68         blocks(move(other.blocks)),
69         programs(move(other.programs)),
70         last_buffer_block(other.last_buffer_block),
71         buffer(other.buffer),
72         dirty(other.dirty),
73         debug_name(move(other.debug_name))
74 {
75         other.blocks.clear();
76         other.buffer = 0;
77 }
78
79 ProgramData::~ProgramData()
80 {
81         for(SharedBlock &b: blocks)
82         {
83                 if(b.indices.type_flag==0xFE)
84                         delete[] b.indices.dynamic.values;
85                 delete b.block;
86         }
87         delete buffer;
88 }
89
90 void ProgramData::uniform(Tag tag, DataType type, unsigned array_size, const void *value)
91 {
92         if(!validate_tag(tag))
93                 return;
94
95         auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
96         if(i==uniforms.end() || i->tag!=tag)
97         {
98                 if(uniforms.size()>=MASK_BITS)
99                         throw too_many_uniforms(tag.str());
100
101                 TaggedUniform tu;
102                 tu.tag = tag;
103                 tu.type = type;
104                 tu.array_size = array_size;
105                 tu.data_offset = uniform_data.size();
106                 tu.data_size = array_size*get_type_size(type);
107                 i = uniforms.insert(i, tu);
108                 uniform_data.resize(tu.data_offset+tu.data_size);
109
110                 mark_dirty(ALL_ONES);
111         }
112         else if(type!=i->type)
113                 throw invalid_operation("ProgramData::uniform");
114         else if(array_size>i->array_size)
115         {
116                 unsigned add_bytes = (array_size-i->array_size)*get_type_size(type);
117                 uniform_data.insert(uniform_data.begin()+i->data_offset+i->data_size, add_bytes, 0);
118                 for(TaggedUniform &u: uniforms)
119                         if(u.data_offset>i->data_offset)
120                                 u.data_offset += add_bytes;
121                 i->array_size = array_size;
122                 i->data_size = array_size*get_type_size(type);
123         }
124
125         const char *val_begin = static_cast<const char *>(value);
126         const char *val_end = val_begin+array_size*get_type_size(type);
127         char *store_begin = uniform_data.data()+i->data_offset;
128         copy(val_begin, val_end, store_begin);
129
130         mark_dirty(1<<(i-uniforms.begin()));
131 }
132
133 bool ProgramData::validate_tag(Tag tag) const
134 {
135 #ifdef DEBUG
136         try
137 #endif
138         {
139                 if(tied_program)
140                 {
141                         const ReflectData::UniformInfo &info = tied_program->get_uniform_info(tag);
142                         if(is_image(info.type))
143                                 throw invalid_operation("ProgramData::uniform");
144                 }
145                 return true;
146         }
147 #ifdef DEBUG
148         catch(const exception &e)
149         {
150                 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
151                 return false;
152         }
153 #endif
154 }
155
156 void ProgramData::mark_dirty(Mask bits)
157 {
158         if(!dirty)
159                 ++generation;
160         dirty |= bits;
161 }
162
163 void ProgramData::uniform(Tag tag, int v)
164 {
165         uniform(tag, INT, 1, &v);
166 }
167
168 void ProgramData::uniform(Tag tag, unsigned v)
169 {
170         uniform(tag, UNSIGNED_INT, 1, &v);
171 }
172
173 void ProgramData::uniform(Tag tag, float v)
174 {
175         uniform(tag, FLOAT, 1, &v);
176 }
177
178 void ProgramData::uniform(Tag tag, int v0, int v1)
179 {
180         int va[2] = { v0, v1 };
181         uniform2(tag, va);
182 }
183
184 void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1)
185 {
186         unsigned va[2] = { v0, v1 };
187         uniform2(tag, va);
188 }
189
190 void ProgramData::uniform(Tag tag, float v0, float v1)
191 {
192         float va[2] = { v0, v1 };
193         uniform2(tag, va);
194 }
195
196 void ProgramData::uniform2(Tag tag, const int *v)
197 {
198         uniform(tag, INT_VEC2, 1, v);
199 }
200
201 void ProgramData::uniform2(Tag tag, const unsigned *v)
202 {
203         uniform(tag, UINT_VEC2, 1, v);
204 }
205
206 void ProgramData::uniform2(Tag tag, const float *v)
207 {
208         uniform(tag, FLOAT_VEC2, 1, v);
209 }
210
211 void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
212 {
213         int va[3] = { v0, v1, v2 };
214         uniform3(tag, va);
215 }
216
217 void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1, unsigned v2)
218 {
219         unsigned va[3] = { v0, v1, v2 };
220         uniform3(tag, va);
221 }
222
223 void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
224 {
225         float va[3] = { v0, v1, v2 };
226         uniform3(tag, va);
227 }
228
229 void ProgramData::uniform3(Tag tag, const int *v)
230 {
231         uniform(tag, INT_VEC3, 1, v);
232 }
233
234 void ProgramData::uniform3(Tag tag, const unsigned *v)
235 {
236         uniform(tag, UINT_VEC3, 1, v);
237 }
238
239 void ProgramData::uniform3(Tag tag, const float *v)
240 {
241         uniform(tag, FLOAT_VEC3, 1, v);
242 }
243
244 void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
245 {
246         int va[4] = { v0, v1, v2, v3 };
247         uniform4(tag, va);
248 }
249
250 void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1, unsigned v2, unsigned v3)
251 {
252         unsigned va[4] = { v0, v1, v2, v3 };
253         uniform4(tag, va);
254 }
255
256 void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
257 {
258         float va[4] = { v0, v1, v2, v3 };
259         uniform4(tag, va);
260 }
261
262 void ProgramData::uniform(Tag tag, const Color &c)
263 {
264         uniform(tag, c.r, c.g, c.b, c.a);
265 }
266
267 void ProgramData::uniform4(Tag tag, const int *v)
268 {
269         uniform(tag, INT_VEC4, 1, v);
270 }
271
272 void ProgramData::uniform4(Tag tag, const unsigned *v)
273 {
274         uniform(tag, UINT_VEC4, 1, v);
275 }
276
277 void ProgramData::uniform4(Tag tag, const float *v)
278 {
279         uniform(tag, FLOAT_VEC4, 1, v);
280 }
281
282 void ProgramData::uniform_matrix2(Tag tag, const float *v)
283 {
284         uniform(tag, FLOAT_MAT2, 1, v);
285 }
286
287 void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
288 {
289         uniform(tag, FLOAT_MAT3x2, 1, v);
290 }
291
292 void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
293 {
294         uniform(tag, FLOAT_MAT4x2, 1, v);
295 }
296
297 void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
298 {
299         uniform(tag, FLOAT_MAT2x3, 1, v);
300 }
301
302 void ProgramData::uniform_matrix3(Tag tag, const float *v)
303 {
304         uniform(tag, FLOAT_MAT3, 1, v);
305 }
306
307 void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
308 {
309         uniform(tag, FLOAT_MAT4x3, 1, v);
310 }
311
312 void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
313 {
314         uniform(tag, FLOAT_MAT2x4, 1, v);
315 }
316
317 void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
318 {
319         uniform(tag, FLOAT_MAT3x4, 1, v);
320 }
321
322 void ProgramData::uniform(Tag tag, const Matrix &m)
323 {
324         uniform_matrix4(tag, m.data());
325 }
326
327 void ProgramData::uniform_matrix4(Tag tag, const float *v)
328 {
329         uniform(tag, FLOAT_MAT4, 1, v);
330 }
331
332 void ProgramData::uniform_array(Tag tag, unsigned n, const int *v)
333 {
334         uniform(tag, INT, n, v);
335 }
336
337 void ProgramData::uniform_array(Tag tag, unsigned n, const unsigned *v)
338 {
339         uniform(tag, UNSIGNED_INT, n, v);
340 }
341
342 void ProgramData::uniform_array(Tag tag, unsigned n, const float *v)
343 {
344         uniform(tag, FLOAT, n, v);
345 }
346
347 void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
348 {
349         uniform(tag, INT, n, v);
350 }
351
352 void ProgramData::uniform1_array(Tag tag, unsigned n, const unsigned *v)
353 {
354         uniform(tag, UNSIGNED_INT, n, v);
355 }
356
357 void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
358 {
359         uniform(tag, FLOAT, n, v);
360 }
361
362 void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
363 {
364         uniform(tag, INT_VEC2, n, v);
365 }
366
367 void ProgramData::uniform2_array(Tag tag, unsigned n, const unsigned *v)
368 {
369         uniform(tag, UINT_VEC2, n, v);
370 }
371
372 void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
373 {
374         uniform(tag, FLOAT_VEC2, n, v);
375 }
376
377 void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
378 {
379         uniform(tag, INT_VEC3, n, v);
380 }
381
382 void ProgramData::uniform3_array(Tag tag, unsigned n, const unsigned *v)
383 {
384         uniform(tag, INT_VEC3, n, v);
385 }
386
387 void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
388 {
389         uniform(tag, FLOAT_VEC3, n, v);
390 }
391
392 void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
393 {
394         uniform(tag, INT_VEC4, n, v);
395 }
396
397 void ProgramData::uniform4_array(Tag tag, unsigned n, const unsigned *v)
398 {
399         uniform(tag, UINT_VEC4, n, v);
400 }
401
402 void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
403 {
404         uniform(tag, FLOAT_VEC4, n, v);
405 }
406
407 void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
408 {
409         uniform(tag, FLOAT_MAT2, n, v);
410 }
411
412 void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
413 {
414         uniform(tag, FLOAT_MAT3x2, n, v);
415 }
416
417 void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
418 {
419         uniform(tag, FLOAT_MAT4x2, n, v);
420 }
421
422 void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
423 {
424         uniform(tag, FLOAT_MAT2x3, n, v);
425 }
426
427 void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
428 {
429         uniform(tag, FLOAT_MAT3, n, v);
430 }
431
432 void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
433 {
434         uniform(tag, FLOAT_MAT4x3, n, v);
435 }
436
437 void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
438 {
439         uniform(tag, FLOAT_MAT2x4, n, v);
440 }
441
442 void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
443 {
444         uniform(tag, FLOAT_MAT3x4, n, v);
445 }
446
447 void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
448 {
449         uniform(tag, FLOAT_MAT4, n, v);
450 }
451
452 void ProgramData::remove_uniform(Tag tag)
453 {
454         auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
455         if(i==uniforms.end() || i->tag!=tag)
456                 return;
457
458         uniform_data.erase(uniform_data.begin()+i->data_offset, uniform_data.begin()+i->data_offset+i->data_size);
459         for(TaggedUniform &u: uniforms)
460                 if(u.data_offset>i->data_offset)
461                         u.data_offset -= i->data_size;
462         uniforms.erase(i);
463
464         mark_dirty(ALL_ONES);
465 }
466
467 vector<Tag> ProgramData::get_uniform_tags() const
468 {
469         vector<Tag> tags;
470         tags.reserve(uniforms.size());
471         for(const TaggedUniform &u: uniforms)
472                 tags.push_back(u.tag);
473         return tags;
474 }
475
476 void ProgramData::copy_uniform(const ProgramData &source, Tag tag)
477 {
478         int i = source.find_uniform_index(tag);
479         if(i<0)
480                 throw key_error(tag);
481         const TaggedUniform &tu = source.uniforms[i];
482         uniform(tag, tu.type, tu.array_size, source.uniform_data.data()+tu.data_offset);
483 }
484
485 int ProgramData::find_uniform_index(Tag tag) const
486 {
487         auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
488         return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
489 }
490
491 vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
492 {
493         ReflectData::LayoutHash prog_hash = prog.get_uniform_layout_hash();
494         auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
495         if(i!=programs.end() && i->prog_hash==prog_hash)
496                 return i;
497
498         const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
499         unsigned index = i-programs.begin();
500         programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash));
501
502         /* Block indices may change if new shared blocks need to be inserted.  Store
503         the hashes so they can be matched up later. */
504         vector<ReflectData::LayoutHash> block_hashes;
505         block_hashes.reserve(programs.size());
506         for(const ProgramBlock &b: programs)
507                 block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
508
509         for(unsigned j=0; j<block_infos.size(); ++j)
510         {
511                 const ReflectData::UniformBlockInfo &info = block_infos[j];
512                 block_hashes[index+1+j] = info.layout_hash;
513                 programs[index+1+j].bind_point = info.bind_point;
514
515                 auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
516                 if(k==blocks.end() || k->block_hash!=info.layout_hash)
517                 {
518                         k = blocks.insert(k, SharedBlock(info.layout_hash));
519                         update_block_uniform_indices(*k, info);
520                 }
521         }
522
523         /* Reassign shared block indices from the stored hashes. */
524         for(unsigned j=0; j<programs.size(); ++j)
525         {
526                 unsigned hash = block_hashes[j];
527                 if(hash)
528                 {
529                         auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
530                         programs[j].block_index = k-blocks.begin();
531                 }
532                 else
533                         programs[j].block_index = -1;
534         }
535
536         return programs.begin()+index;
537 }
538
539 void ProgramData::update_block_uniform_indices(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
540 {
541         uint8_t *indices = block.indices.values;
542         if(info.uniforms.size()>16)
543         {
544                 if(block.indices.type_flag==0xFD)
545                 {
546                         block.indices.dynamic.values = new uint8_t[info.uniforms.size()];
547                         block.indices.type_flag = 0xFE;
548                 }
549                 indices = block.indices.dynamic.values;
550         }
551
552         bool any_missing = false;
553
554         block.used = 0;
555         for(unsigned i=0; i<info.uniforms.size(); ++i)
556         {
557                 int j = find_uniform_index(info.uniforms[i]->tag);
558                 if(j>=0)
559                 {
560                         indices[i] = j;
561                         if(static_cast<unsigned>(j)<MASK_BITS)
562                                 block.used |= 1<<j;
563                 }
564                 else
565                 {
566                         indices[i] = 0xFF;
567                         any_missing = true;
568                 }
569         }
570
571         if(block.used && any_missing && info.bind_point>=0)
572         {
573 #ifdef DEBUG
574                 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
575 #else
576                 throw incomplete_uniform_block(info.name);
577 #endif
578         }
579
580         block.dirty = block.used;
581
582         if(block.used && !block.block)
583         {
584                 block.block = new UniformBlock(info);
585                 if(info.bind_point>=0)
586                 {
587                         if(!buffer)
588                         {
589                                 buffer = new Buffer();
590
591 #ifdef DEBUG
592                                 if(!debug_name.empty())
593                                         buffer->set_debug_name(debug_name);
594 #endif
595                         }
596
597                         block.block->use_buffer(buffer, last_buffer_block);
598                         last_buffer_block = block.block;
599                 }
600         }
601 }
602
603 void ProgramData::update_block(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
604 {
605         const uint8_t *indices = block.get_uniform_indices();
606         for(unsigned i=0; i<info.uniforms.size(); ++i)
607         {
608                 if(is_image(info.uniforms[i]->type))
609                         ;  // Temporarily ignore deprecated use of sampler uniforms in ProgramData
610                 else if(indices[i]!=0xFF)
611                 {
612                         const TaggedUniform &tu = uniforms[indices[i]];
613                         block.block->store(*info.uniforms[i], tu.array_size, uniform_data.data()+tu.data_offset);
614                 }
615         }
616 }
617
618 vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
619 {
620         UniformBlock *old_last_block = last_buffer_block;
621         auto prog_begin = get_program(prog);
622
623         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
624         Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
625         if(affected|prog_begin->masks.dirty)
626         {
627                 /* If the global dirty flag affects this program, add it to per-block and
628                 per-program dirty flags and clear the global flag.  A previously unseen
629                 program will cause this to happen if there's any dirty uniforms. */
630                 if(affected)
631                 {
632                         for(SharedBlock &b: blocks)
633                                 b.dirty |= (dirty&b.used) | force_dirty;
634                         for(ProgramBlock &b: programs)
635                                 if(b.block_index<0)
636                                         b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
637                         dirty = 0;
638                 }
639
640                 const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
641
642                 if(prog_begin->masks.dirty==ALL_ONES)
643                 {
644                         /* The set of uniforms has changed since this program was last used.
645                         Refresh uniform indices within the program's blocks. */
646                         prog_begin->masks.used = 0;
647                         auto j = prog_begin+1;
648                         for(const ReflectData::UniformBlockInfo &b: block_infos)
649                         {
650                                 SharedBlock &shared = blocks[j->block_index];
651                                 if(shared.dirty==ALL_ONES)
652                                         update_block_uniform_indices(shared, b);
653                                 prog_begin->masks.used |= shared.used;
654                                 j->block = (shared.used ? shared.block : 0);
655                                 ++j;
656                         }
657                 }
658
659                 // Update the contents of all dirty blocks.
660                 bool buffered_blocks_updated = false;
661                 auto j = prog_begin+1;
662                 for(const ReflectData::UniformBlockInfo &b: block_infos)
663                 {
664                         SharedBlock &shared = blocks[j->block_index];
665                         if(shared.dirty)
666                         {
667                                 update_block(shared, b);
668                                 shared.dirty = 0;
669                                 buffered_blocks_updated |= (j->bind_point>=0);
670                         }
671                         ++j;
672                 }
673
674                 prog_begin->masks.dirty = 0;
675
676                 if(last_buffer_block!=old_last_block)
677                 {
678                         unsigned required_size = last_buffer_block->get_required_buffer_size();
679                         if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
680                         {
681                                 if(buffer->get_size()>0)
682                                 {
683                                         delete buffer;
684                                         buffer = new Buffer();
685                                         last_buffer_block->change_buffer(buffer);
686
687 #ifdef DEBUG
688                                         if(!debug_name.empty())
689                                                 buffer->set_debug_name(debug_name);
690 #endif
691                                 }
692
693                                 buffer->storage(required_size);
694                         }
695                 }
696         }
697
698         return prog_begin;
699 }
700
701 void ProgramData::apply(const Program &prog, PipelineState &state) const
702 {
703         auto prog_begin = prepare_program(prog);
704         ReflectData::LayoutHash prog_hash = prog_begin->prog_hash;
705         for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
706                 if(i->block)
707                 {
708                         state.set_uniform_block(i->bind_point, i->block);
709                         if(i->bind_point>=0)
710                                 i->block->refresh();
711                 }
712 }
713
714 void ProgramData::set_debug_name(const string &name)
715 {
716 #ifdef DEBUG
717         debug_name = name;
718         if(buffer)
719                 buffer->set_debug_name(name);
720 #else
721         (void)name;
722 #endif
723 }
724
725
726 ProgramData::SharedBlock::SharedBlock(ReflectData::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_t *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(ReflectData::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