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