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