]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Allow tying ProgramData to a Program for name-checking purposes
[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                 if(j!=uniform_slots.end() && j->second<MASK_BITS)
299                         mask |= 1<<j->second;
300         }
301
302         return mask;
303 }
304
305 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
306 {
307         for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
308         {
309                 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
310                 if(j!=uniform_slots.end())
311                         block.attach(**i, *uniforms[j->second]);
312         }
313 }
314
315 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
316 {
317         BlockMap::iterator i = blocks.find(info.layout_hash);
318         if(i==blocks.end())
319         {
320                 unsigned used = compute_slot_mask(info);
321                 if(!used)
322                         return 0;
323
324                 UniformBlock *block;
325                 if(info.bind_point>=0)
326                 {
327                         if(!buffer)
328                         {
329                                 buffer = new Buffer(UNIFORM_BUFFER);
330                                 buffer->set_usage(STREAM_DRAW);
331                         }
332
333                         block = new UniformBlock(info.data_size);
334                         block->use_buffer(buffer, last_block);
335                         last_block = block;
336                 }
337                 else
338                         block = new UniformBlock;
339
340                 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
341         }
342
343         return &i->second;
344 }
345
346 void ProgramData::apply() const
347 {
348         const Program *prog = Program::current();
349         if(!prog)
350                 throw invalid_operation("ProgramData::apply");
351
352         Program::LayoutHash layout = prog->get_uniform_layout_hash();
353         ProgramUniforms &pu = programs[layout];
354
355         Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
356         Mask affected = (dirty&pu.used) | force_dirty;
357         if(affected|pu.dirty)
358         {
359                 /* If the global dirty flag affects this program, add it to per-program
360                 dirty flags and clear the global flag.  A previously unseen program will
361                 always cause this to happen. */
362                 if(affected)
363                 {
364                         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
365                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
366                         for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
367                                 i->second.dirty |= (dirty&i->second.used) | force_dirty;
368                         dirty = 0;
369                 }
370
371                 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
372
373                 if(pu.dirty==ALL_ONES)
374                 {
375                         /* The set of uniforms has changed since this program was last used.
376                         Regenerate the list of uniform blocks. */
377                         pu.blocks.clear();
378                         pu.blocks.reserve(prog_blocks.size());
379
380                         pu.used = 0;
381                         for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
382                         {
383                                 SharedBlock *shared = get_shared_block(i->second);
384                                 if(shared)
385                                 {
386                                         if(shared->dirty==ALL_ONES)
387                                                 shared->used = compute_slot_mask(i->second);
388                                         pu.used |= shared->used;
389                                 }
390
391                                 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
392                         }
393                 }
394
395                 // Update the contents of all dirty blocks.
396                 bool buffered_blocks_updated = false;
397                 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
398                 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
399                 {
400                         if(!j->shared || !j->shared->dirty)
401                                 continue;
402
403                         update_block(*j->block, i->second);
404                         j->shared->dirty = 0;
405                         buffered_blocks_updated |= (j->bind_point>=0);
406                 }
407
408                 pu.dirty = 0;
409
410                 /* If any blocks stored in the buffer were updated, bind the buffer here
411                 to avoid state thrashing. */
412                 if(buffered_blocks_updated && !ARB_direct_state_access)
413                         buffer->bind();
414         }
415
416         for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
417                 if(i->block)
418                         i->block->apply(i->bind_point);
419 }
420
421
422 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
423         used(u),
424         dirty(u),
425         block(b)
426 { }
427
428
429 ProgramData::ProgramBlock::ProgramBlock():
430         bind_point(-1),
431         block(0),
432         shared(0)
433 { }
434
435 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
436         bind_point(p),
437         block(b ? b->block : 0),
438         shared(b)
439 { }
440
441
442 ProgramData::ProgramUniforms::ProgramUniforms():
443         used(ALL_ONES),
444         dirty(ALL_ONES)
445 { }
446
447
448 ProgramData::Loader::Loader(ProgramData &pd):
449         DataFile::ObjectLoader<ProgramData>(pd)
450 {
451         add("uniform", &Loader::uniform1i);
452         add("uniform1i", &Loader::uniform1i);
453         add("uniform", &Loader::uniform1f);
454         add("uniform1f", &Loader::uniform1f);
455         add("uniform", &Loader::uniform2i);
456         add("uniform2i", &Loader::uniform2i);
457         add("uniform", &Loader::uniform2f);
458         add("uniform2f", &Loader::uniform2f);
459         add("uniform", &Loader::uniform3i);
460         add("uniform3i", &Loader::uniform3i);
461         add("uniform", &Loader::uniform3f);
462         add("uniform3f", &Loader::uniform3f);
463         add("uniform", &Loader::uniform4i);
464         add("uniform4i", &Loader::uniform4i);
465         add("uniform", &Loader::uniform4f);
466         add("uniform4f", &Loader::uniform4f);
467         add("uniform1i_array", &Loader::uniform1i_array);
468         add("uniform1f_array", &Loader::uniform1f_array);
469         add("uniform2f_array", &Loader::uniform2f_array);
470         add("uniform3f_array", &Loader::uniform3f_array);
471         add("uniform4f_array", &Loader::uniform4f_array);
472         add("uniform_array", &Loader::uniform_array);
473 }
474
475 void ProgramData::Loader::uniform1i(const string &n, int v)
476 {
477         obj.uniform(n, v);
478 }
479
480 void ProgramData::Loader::uniform1f(const string &n, float v)
481 {
482         obj.uniform(n, v);
483 }
484
485 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
486 {
487         obj.uniform(n, v0, v1);
488 }
489
490 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
491 {
492         obj.uniform(n, v0, v1);
493 }
494
495 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
496 {
497         obj.uniform(n, v0, v1, v2);
498 }
499
500 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
501 {
502         obj.uniform(n, v0, v1, v2);
503 }
504
505 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
506 {
507         obj.uniform(n, v0, v1, v2, v3);
508 }
509
510 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
511 {
512         obj.uniform(n, v0, v1, v2, v3);
513 }
514
515 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
516 {
517         ArrayLoader ldr(t, e);
518         load_sub_with(ldr);
519         unsigned size = ldr.get_size();
520         if(!size)
521                 throw logic_error("empty uniform array");
522
523         DataType type = ldr.get_data_type();
524         unsigned elem_size = ldr.get_element_size();
525         if(type==INT)
526         {
527                 const int *data = reinterpret_cast<const int *>(ldr.get_data());
528                 if(elem_size==1)
529                         obj.uniform1_array(n, size, data);
530                 else if(elem_size==2)
531                         obj.uniform2_array(n, size, data);
532                 else if(elem_size==3)
533                         obj.uniform3_array(n, size, data);
534                 else if(elem_size==4)
535                         obj.uniform4_array(n, size, data);
536                 else
537                         throw logic_error("unsupported combination of array type and element size");
538         }
539         else if(type==FLOAT)
540         {
541                 const float *data = reinterpret_cast<const float *>(ldr.get_data());
542                 if(elem_size==1)
543                         obj.uniform1_array(n, size, data);
544                 else if(elem_size==2)
545                         obj.uniform2_array(n, size, data);
546                 else if(elem_size==3)
547                         obj.uniform3_array(n, size, data);
548                 else if(elem_size==4)
549                         obj.uniform4_array(n, size, data);
550                 else
551                         throw logic_error("unsupported combination of array type and element size");
552         }
553         else
554                 throw logic_error("unsupported array type");
555 }
556
557 void ProgramData::Loader::uniform1i_array(const string &n)
558 {
559         uniform_array_(n, INT, 1);
560 }
561
562 void ProgramData::Loader::uniform1f_array(const string &n)
563 {
564         uniform_array_(n, FLOAT, 1);
565 }
566
567 void ProgramData::Loader::uniform2i_array(const string &n)
568 {
569         uniform_array_(n, INT, 2);
570 }
571
572 void ProgramData::Loader::uniform2f_array(const string &n)
573 {
574         uniform_array_(n, FLOAT, 2);
575 }
576
577 void ProgramData::Loader::uniform3i_array(const string &n)
578 {
579         uniform_array_(n, INT, 3);
580 }
581
582 void ProgramData::Loader::uniform3f_array(const string &n)
583 {
584         uniform_array_(n, FLOAT, 3);
585 }
586
587 void ProgramData::Loader::uniform4i_array(const string &n)
588 {
589         uniform_array_(n, INT, 4);
590 }
591
592 void ProgramData::Loader::uniform4f_array(const string &n)
593 {
594         uniform_array_(n, FLOAT, 4);
595 }
596
597 void ProgramData::Loader::uniform_array(const string &n)
598 {
599         uniform_array_(n, static_cast<DataType>(0), 0);
600 }
601
602
603 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
604         type(t),
605         element_size(e)
606 {
607         add("uniform", &ArrayLoader::uniform1i);
608         add("uniform1i", &ArrayLoader::uniform1i);
609         add("uniform", &ArrayLoader::uniform1f);
610         add("uniform1f", &ArrayLoader::uniform1f);
611         add("uniform", &ArrayLoader::uniform2f);
612         add("uniform2f", &ArrayLoader::uniform2f);
613         add("uniform", &ArrayLoader::uniform3f);
614         add("uniform3f", &ArrayLoader::uniform3f);
615         add("uniform", &ArrayLoader::uniform4f);
616         add("uniform4f", &ArrayLoader::uniform4f);
617 }
618
619 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
620 {
621         if(element_size && (t!=type || e!=element_size))
622                 throw logic_error("heterogeneous array contents");
623
624         if(!element_size)
625         {
626                 type = t;
627                 element_size = e;
628         }
629
630         const char *cv = reinterpret_cast<const char *>(v);
631         data.insert(data.end(), cv, cv+element_size*4);
632 }
633
634 void ProgramData::ArrayLoader::uniform1i(int v)
635 {
636         uniform(INT, 1, &v);
637 }
638
639 void ProgramData::ArrayLoader::uniform1f(float v)
640 {
641         uniform(FLOAT, 1, &v);
642 }
643
644 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
645 {
646         int va[2] = { v0, v1 };
647         uniform(INT, 2, va);
648 }
649
650 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
651 {
652         float va[2] = { v0, v1 };
653         uniform(FLOAT, 2, va);
654 }
655
656 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
657 {
658         int va[3] = { v0, v1, v2 };
659         uniform(INT, 3, va);
660 }
661
662 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
663 {
664         float va[3] = { v0, v1, v2 };
665         uniform(FLOAT, 3, va);
666 }
667
668 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
669 {
670         int va[4] = { v0, v1, v2, v3 };
671         uniform(INT, 4, va);
672 }
673
674 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
675 {
676         float va[4] = { v0, v1, v2, v3 };
677         uniform(FLOAT, 4, va);
678 }
679
680 } // namespace GL
681 } // namespace Msp