]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Add const overload for AnimatedObject::get_shader_data
[libs/gl.git] / source / programdata.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include "buffer.h"
3 #include "color.h"
4 #include "error.h"
5 #include "matrix.h"
6 #include "program.h"
7 #include "programdata.h"
8 #include "uniform.h"
9 #include "uniformblock.h"
10 #include "vector.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 ProgramData::ProgramData(const Program *p):
18         tied_program(p),
19         last_block(0),
20         buffer(0),
21         dirty(0)
22 { }
23
24 // Blocks are intentionally left uncopied
25 ProgramData::ProgramData(const ProgramData &other):
26         tied_program(0),
27         uniform_slots(other.uniform_slots),
28         uniforms(other.uniforms),
29         last_block(0),
30         buffer(0),
31         dirty(0)
32 {
33         for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
34                 *i = (*i)->clone();
35 }
36
37 ProgramData &ProgramData::operator=(const ProgramData &other)
38 {
39         for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
40                 delete *i;
41         uniforms.clear();
42
43         tied_program = other.tied_program;
44
45         uniform_slots = other.uniform_slots;
46         for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
47                 uniforms.push_back((*i)->clone());
48
49         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
50                 delete i->second.block;
51         programs.clear();
52
53         last_block = 0;
54         buffer = 0;
55         dirty = 0;
56
57         return *this;
58 }
59
60 ProgramData::~ProgramData()
61 {
62         for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
63                 delete *i;
64         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
65                 delete i->second.block;
66         delete buffer;
67 }
68
69 void ProgramData::uniform(const string &name, Uniform *uni)
70 {
71         try
72         {
73                 if(tied_program)
74                         tied_program->get_uniform_info(name);
75                 else if(name[name.size()-1]==']')
76                         throw invalid_argument("ProgramData::uniform");
77         }
78         catch(...)
79         {
80                 delete uni;
81                 throw;
82         }
83
84         SlotMap::iterator i = uniform_slots.find(name);
85         if(i!=uniform_slots.end())
86         {
87                 Uniform *&slot = uniforms[i->second];
88                 /* UniformBlock does not copy the uniforms, so existing default blocks
89                 will be left with stale pointers.  This is not a problem as long as no
90                 one stores pointers to the blocks and expects them to stay valid. */
91                 delete slot;
92                 slot = uni;
93
94                 if(i->second<MASK_BITS)
95                         dirty |= 1<<i->second;
96                 else  // Force a full update if the mask isn't wide enough
97                         dirty = ALL_ONES;
98         }
99         else
100         {
101                 uniform_slots[name] = uniforms.size();
102                 uniforms.push_back(uni);
103                 dirty = ALL_ONES;
104         }
105 }
106
107 void ProgramData::uniform(const string &name, int v)
108 {
109         uniform(name, new Uniform1i(v));
110 }
111
112 void ProgramData::uniform(const string &name, float v)
113 {
114         uniform(name, new Uniform1f(v));
115 }
116
117 void ProgramData::uniform(const string &name, int v0, int v1)
118 {
119         int va[2] = { v0, v1 };
120         uniform2(name, va);
121 }
122
123 void ProgramData::uniform(const string &name, float v0, float v1)
124 {
125         float va[2] = { v0, v1 };
126         uniform2(name, va);
127 }
128
129 void ProgramData::uniform2(const string &name, const int *v)
130 {
131         uniform(name, new Uniform2i(v));
132 }
133
134 void ProgramData::uniform2(const string &name, const float *v)
135 {
136         uniform(name, new Uniform2f(v));
137 }
138
139 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
140 {
141         int va[3] = { v0, v1, v2 };
142         uniform3(name, va);
143 }
144
145 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
146 {
147         float va[3] = { v0, v1, v2 };
148         uniform3(name, va);
149 }
150
151 void ProgramData::uniform(const string &name, const Vector3 &v)
152 {
153         uniform(name, v.x, v.y, v.z);
154 }
155
156 void ProgramData::uniform3(const string &name, const int *v)
157 {
158         uniform(name, new Uniform3i(v));
159 }
160
161 void ProgramData::uniform3(const string &name, const float *v)
162 {
163         uniform(name, new Uniform3f(v));
164 }
165
166 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
167 {
168         int va[4] = { v0, v1, v2, v3 };
169         uniform4(name, va);
170 }
171
172 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
173 {
174         float va[4] = { v0, v1, v2, v3 };
175         uniform4(name, va);
176 }
177
178 void ProgramData::uniform(const string &name, const Vector4 &v)
179 {
180         uniform(name, v.x, v.y, v.z, v.w);
181 }
182
183 void ProgramData::uniform(const string &name, const Color &c)
184 {
185         uniform(name, c.r, c.g, c.b, c.a);
186 }
187
188 void ProgramData::uniform4(const string &name, const int *v)
189 {
190         uniform(name, new Uniform4i(v));
191 }
192
193 void ProgramData::uniform4(const string &name, const float *v)
194 {
195         uniform(name, new Uniform4f(v));
196 }
197
198 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
199 {
200         uniform_matrix2(name, &m(0, 0));
201 }
202
203 void ProgramData::uniform_matrix2(const string &name, const float *v)
204 {
205         uniform(name, new UniformMatrix2x2f(v));
206 }
207
208 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
209 {
210         uniform_matrix3(name, &m(0, 0));
211 }
212
213 void ProgramData::uniform_matrix3(const string &name, const float *v)
214 {
215         uniform(name, new UniformMatrix3x3f(v));
216 }
217
218 void ProgramData::uniform(const string &name, const Matrix &m)
219 {
220         uniform_matrix4(name, m.data());
221 }
222
223 void ProgramData::uniform_matrix4(const string &name, const float *v)
224 {
225         uniform(name, new UniformMatrix4x4f(v));
226 }
227
228 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
229 {
230         uniform(name, new UniformArray<Uniform1i>(n, v));
231 }
232
233 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
234 {
235         uniform(name, new UniformArray<Uniform1f>(n, v));
236 }
237
238 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
239 {
240         uniform(name, new UniformArray<Uniform2i>(n, v));
241 }
242
243 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
244 {
245         uniform(name, new UniformArray<Uniform2f>(n, v));
246 }
247
248 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
249 {
250         uniform(name, new UniformArray<Uniform3i>(n, v));
251 }
252
253 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
254 {
255         uniform(name, new UniformArray<Uniform3f>(n, v));
256 }
257
258 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
259 {
260         uniform(name, new UniformArray<Uniform4i>(n, v));
261 }
262
263 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
264 {
265         uniform(name, new UniformArray<Uniform4f>(n, v));
266 }
267
268 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
269 {
270         uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
271 }
272
273 void ProgramData::remove_uniform(const string &name)
274 {
275         SlotMap::iterator i = uniform_slots.find(name);
276         if(i!=uniform_slots.end())
277         {
278                 vector<Uniform *>::iterator j = uniforms.begin()+i->second;
279                 delete *j;
280                 uniforms.erase(j);
281
282                 for(SlotMap::iterator k=uniform_slots.begin(); k!=uniform_slots.end(); ++k)
283                         if(k->second>i->second)
284                                 --k->second;
285
286                 uniform_slots.erase(i);
287
288                 dirty = ALL_ONES;
289         }
290 }
291
292 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
293 {
294         unsigned mask = 0;
295         for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
296         {
297                 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
298                 /* TODO issue a warning (or even error?) either here or in update_block
299                 if all uniforms for a buffer-backed block are not found */
300                 if(j!=uniform_slots.end() && j->second<MASK_BITS)
301                         mask |= 1<<j->second;
302         }
303
304         return mask;
305 }
306
307 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
308 {
309         for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
310         {
311                 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
312                 if(j!=uniform_slots.end())
313                         block.attach(**i, *uniforms[j->second]);
314         }
315 }
316
317 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
318 {
319         BlockMap::iterator i = blocks.find(info.layout_hash);
320         if(i==blocks.end())
321         {
322                 unsigned used = compute_slot_mask(info);
323                 if(!used)
324                         return 0;
325
326                 UniformBlock *block;
327                 if(info.bind_point>=0)
328                 {
329                         if(!buffer)
330                         {
331                                 buffer = new Buffer(UNIFORM_BUFFER);
332                                 buffer->set_usage(STREAM_DRAW);
333                         }
334
335                         block = new UniformBlock(info.data_size);
336                         block->use_buffer(buffer, last_block);
337                         last_block = block;
338                 }
339                 else
340                         block = new UniformBlock;
341
342                 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
343         }
344
345         return &i->second;
346 }
347
348 void ProgramData::apply() const
349 {
350         const Program *prog = Program::current();
351         if(!prog)
352                 throw invalid_operation("ProgramData::apply");
353
354         Program::LayoutHash layout = prog->get_uniform_layout_hash();
355         ProgramUniforms &pu = programs[layout];
356
357         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
358         Mask affected = (dirty&pu.used) | force_dirty;
359         if(affected|pu.dirty)
360         {
361                 /* If the global dirty flag affects this program, add it to per-program
362                 dirty flags and clear the global flag.  A previously unseen program will
363                 always cause this to happen. */
364                 if(affected)
365                 {
366                         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
367                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
368                         for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
369                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
370                         dirty = 0;
371                 }
372
373                 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
374
375                 if(pu.dirty==ALL_ONES)
376                 {
377                         /* The set of uniforms has changed since this program was last used.
378                         Regenerate the list of uniform blocks. */
379                         pu.blocks.clear();
380                         pu.blocks.reserve(prog_blocks.size());
381
382                         pu.used = 0;
383                         for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
384                         {
385                                 SharedBlock *shared = get_shared_block(i->second);
386                                 if(shared)
387                                 {
388                                         if(shared->dirty==ALL_ONES)
389                                                 shared->used = compute_slot_mask(i->second);
390                                         pu.used |= shared->used;
391                                 }
392
393                                 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
394                         }
395                 }
396
397                 // Update the contents of all dirty blocks.
398                 bool buffered_blocks_updated = false;
399                 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
400                 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
401                 {
402                         if(!j->shared || !j->shared->dirty)
403                                 continue;
404
405                         update_block(*j->block, i->second);
406                         j->shared->dirty = 0;
407                         buffered_blocks_updated |= (j->bind_point>=0);
408                 }
409
410                 pu.dirty = 0;
411
412                 /* If any blocks stored in the buffer were updated, bind the buffer here
413                 to avoid state thrashing. */
414                 if(buffered_blocks_updated && !ARB_direct_state_access)
415                         buffer->bind();
416         }
417
418         for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
419                 if(i->block)
420                         i->block->apply(i->bind_point);
421 }
422
423
424 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
425         used(u),
426         dirty(u),
427         block(b)
428 { }
429
430
431 ProgramData::ProgramBlock::ProgramBlock():
432         bind_point(-1),
433         block(0),
434         shared(0)
435 { }
436
437 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
438         bind_point(p),
439         block(b ? b->block : 0),
440         shared(b)
441 { }
442
443
444 ProgramData::ProgramUniforms::ProgramUniforms():
445         used(ALL_ONES),
446         dirty(ALL_ONES)
447 { }
448
449
450 ProgramData::Loader::Loader(ProgramData &pd):
451         DataFile::ObjectLoader<ProgramData>(pd)
452 {
453         add("uniform", &Loader::uniform1i);
454         add("uniform1i", &Loader::uniform1i);
455         add("uniform", &Loader::uniform1f);
456         add("uniform1f", &Loader::uniform1f);
457         add("uniform", &Loader::uniform2i);
458         add("uniform2i", &Loader::uniform2i);
459         add("uniform", &Loader::uniform2f);
460         add("uniform2f", &Loader::uniform2f);
461         add("uniform", &Loader::uniform3i);
462         add("uniform3i", &Loader::uniform3i);
463         add("uniform", &Loader::uniform3f);
464         add("uniform3f", &Loader::uniform3f);
465         add("uniform", &Loader::uniform4i);
466         add("uniform4i", &Loader::uniform4i);
467         add("uniform", &Loader::uniform4f);
468         add("uniform4f", &Loader::uniform4f);
469         add("uniform1i_array", &Loader::uniform1i_array);
470         add("uniform1f_array", &Loader::uniform1f_array);
471         add("uniform2f_array", &Loader::uniform2f_array);
472         add("uniform3f_array", &Loader::uniform3f_array);
473         add("uniform4f_array", &Loader::uniform4f_array);
474         add("uniform_array", &Loader::uniform_array);
475 }
476
477 void ProgramData::Loader::uniform1i(const string &n, int v)
478 {
479         obj.uniform(n, v);
480 }
481
482 void ProgramData::Loader::uniform1f(const string &n, float v)
483 {
484         obj.uniform(n, v);
485 }
486
487 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
488 {
489         obj.uniform(n, v0, v1);
490 }
491
492 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
493 {
494         obj.uniform(n, v0, v1);
495 }
496
497 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
498 {
499         obj.uniform(n, v0, v1, v2);
500 }
501
502 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
503 {
504         obj.uniform(n, v0, v1, v2);
505 }
506
507 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
508 {
509         obj.uniform(n, v0, v1, v2, v3);
510 }
511
512 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
513 {
514         obj.uniform(n, v0, v1, v2, v3);
515 }
516
517 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
518 {
519         ArrayLoader ldr(t, e);
520         load_sub_with(ldr);
521         unsigned size = ldr.get_size();
522         if(!size)
523                 throw logic_error("empty uniform array");
524
525         DataType type = ldr.get_data_type();
526         unsigned elem_size = ldr.get_element_size();
527         if(type==INT)
528         {
529                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
530                 if(elem_size==1)
531                         obj.uniform1_array(n, size, data);
532                 else if(elem_size==2)
533                         obj.uniform2_array(n, size, data);
534                 else if(elem_size==3)
535                         obj.uniform3_array(n, size, data);
536                 else if(elem_size==4)
537                         obj.uniform4_array(n, size, data);
538                 else
539                         throw logic_error("unsupported combination of array type and element size");
540         }
541         else if(type==FLOAT)
542         {
543                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
544                 if(elem_size==1)
545                         obj.uniform1_array(n, size, data);
546                 else if(elem_size==2)
547                         obj.uniform2_array(n, size, data);
548                 else if(elem_size==3)
549                         obj.uniform3_array(n, size, data);
550                 else if(elem_size==4)
551                         obj.uniform4_array(n, size, data);
552                 else
553                         throw logic_error("unsupported combination of array type and element size");
554         }
555         else
556                 throw logic_error("unsupported array type");
557 }
558
559 void ProgramData::Loader::uniform1i_array(const string &n)
560 {
561         uniform_array_(n, INT, 1);
562 }
563
564 void ProgramData::Loader::uniform1f_array(const string &n)
565 {
566         uniform_array_(n, FLOAT, 1);
567 }
568
569 void ProgramData::Loader::uniform2i_array(const string &n)
570 {
571         uniform_array_(n, INT, 2);
572 }
573
574 void ProgramData::Loader::uniform2f_array(const string &n)
575 {
576         uniform_array_(n, FLOAT, 2);
577 }
578
579 void ProgramData::Loader::uniform3i_array(const string &n)
580 {
581         uniform_array_(n, INT, 3);
582 }
583
584 void ProgramData::Loader::uniform3f_array(const string &n)
585 {
586         uniform_array_(n, FLOAT, 3);
587 }
588
589 void ProgramData::Loader::uniform4i_array(const string &n)
590 {
591         uniform_array_(n, INT, 4);
592 }
593
594 void ProgramData::Loader::uniform4f_array(const string &n)
595 {
596         uniform_array_(n, FLOAT, 4);
597 }
598
599 void ProgramData::Loader::uniform_array(const string &n)
600 {
601         uniform_array_(n, static_cast<DataType>(0), 0);
602 }
603
604
605 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
606         type(t),
607         element_size(e)
608 {
609         add("uniform", &ArrayLoader::uniform1i);
610         add("uniform1i", &ArrayLoader::uniform1i);
611         add("uniform", &ArrayLoader::uniform1f);
612         add("uniform1f", &ArrayLoader::uniform1f);
613         add("uniform", &ArrayLoader::uniform2f);
614         add("uniform2f", &ArrayLoader::uniform2f);
615         add("uniform", &ArrayLoader::uniform3f);
616         add("uniform3f", &ArrayLoader::uniform3f);
617         add("uniform", &ArrayLoader::uniform4f);
618         add("uniform4f", &ArrayLoader::uniform4f);
619 }
620
621 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
622 {
623         if(element_size && (t!=type || e!=element_size))
624                 throw logic_error("heterogeneous array contents");
625
626         if(!element_size)
627         {
628                 type = t;
629                 element_size = e;
630         }
631
632         const char *cv = reinterpret_cast<const char *>(v);
633         data.insert(data.end(), cv, cv+element_size*4);
634 }
635
636 void ProgramData::ArrayLoader::uniform1i(int v)
637 {
638         uniform(INT, 1, &v);
639 }
640
641 void ProgramData::ArrayLoader::uniform1f(float v)
642 {
643         uniform(FLOAT, 1, &v);
644 }
645
646 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
647 {
648         int va[2] = { v0, v1 };
649         uniform(INT, 2, va);
650 }
651
652 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
653 {
654         float va[2] = { v0, v1 };
655         uniform(FLOAT, 2, va);
656 }
657
658 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
659 {
660         int va[3] = { v0, v1, v2 };
661         uniform(INT, 3, va);
662 }
663
664 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
665 {
666         float va[3] = { v0, v1, v2 };
667         uniform(FLOAT, 3, va);
668 }
669
670 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
671 {
672         int va[4] = { v0, v1, v2, v3 };
673         uniform(INT, 4, va);
674 }
675
676 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
677 {
678         float va[4] = { v0, v1, v2, v3 };
679         uniform(FLOAT, 4, va);
680 }
681
682 } // namespace GL
683 } // namespace Msp