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