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