1 #include <msp/gl/extensions/arb_direct_state_access.h>
7 #include "programdata.h"
9 #include "uniformblock.h"
17 ProgramData::ProgramData():
23 // Blocks are intentionally left uncopied
24 ProgramData::ProgramData(const ProgramData &other):
25 uniform_slots(other.uniform_slots),
26 uniforms(other.uniforms),
31 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
35 ProgramData &ProgramData::operator=(const ProgramData &other)
37 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
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());
45 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
46 delete i->second.block;
56 ProgramData::~ProgramData()
58 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
60 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
61 delete i->second.block;
65 void ProgramData::uniform(const string &name, Uniform *uni)
67 if(name[name.size()-1]==']')
70 throw invalid_argument("ProgramData::uniform");
73 SlotMap::iterator i = uniform_slots.find(name);
74 if(i!=uniform_slots.end())
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. */
83 if(i->second<MASK_BITS)
84 dirty |= 1<<i->second;
85 else // Force a full update if the mask isn't wide enough
90 uniform_slots[name] = uniforms.size();
91 uniforms.push_back(uni);
96 void ProgramData::uniform(const string &name, int v)
98 uniform(name, new Uniform1i(v));
101 void ProgramData::uniform(const string &name, float v)
103 uniform(name, new Uniform1f(v));
106 void ProgramData::uniform(const string &name, int v0, int v1)
108 int va[2] = { v0, v1 };
112 void ProgramData::uniform(const string &name, float v0, float v1)
114 float va[2] = { v0, v1 };
118 void ProgramData::uniform2(const string &name, const int *v)
120 uniform(name, new Uniform2i(v));
123 void ProgramData::uniform2(const string &name, const float *v)
125 uniform(name, new Uniform2f(v));
128 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
130 int va[3] = { v0, v1, v2 };
134 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
136 float va[3] = { v0, v1, v2 };
140 void ProgramData::uniform(const string &name, const Vector3 &v)
142 uniform(name, v.x, v.y, v.z);
145 void ProgramData::uniform3(const string &name, const int *v)
147 uniform(name, new Uniform3i(v));
150 void ProgramData::uniform3(const string &name, const float *v)
152 uniform(name, new Uniform3f(v));
155 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
157 int va[4] = { v0, v1, v2, v3 };
161 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
163 float va[4] = { v0, v1, v2, v3 };
167 void ProgramData::uniform(const string &name, const Vector4 &v)
169 uniform(name, v.x, v.y, v.z, v.w);
172 void ProgramData::uniform(const string &name, const Color &c)
174 uniform(name, c.r, c.g, c.b, c.a);
177 void ProgramData::uniform4(const string &name, const int *v)
179 uniform(name, new Uniform4i(v));
182 void ProgramData::uniform4(const string &name, const float *v)
184 uniform(name, new Uniform4f(v));
187 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
189 uniform_matrix2(name, &m(0, 0));
192 void ProgramData::uniform_matrix2(const string &name, const float *v)
194 uniform(name, new UniformMatrix2x2f(v));
197 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
199 uniform_matrix3(name, &m(0, 0));
202 void ProgramData::uniform_matrix3(const string &name, const float *v)
204 uniform(name, new UniformMatrix3x3f(v));
207 void ProgramData::uniform(const string &name, const Matrix &m)
209 uniform_matrix4(name, m.data());
212 void ProgramData::uniform_matrix4(const string &name, const float *v)
214 uniform(name, new UniformMatrix4x4f(v));
217 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
219 uniform(name, new UniformArray<Uniform1i>(n, v));
222 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
224 uniform(name, new UniformArray<Uniform1f>(n, v));
227 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
229 uniform(name, new UniformArray<Uniform2i>(n, v));
232 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
234 uniform(name, new UniformArray<Uniform2f>(n, v));
237 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
239 uniform(name, new UniformArray<Uniform3i>(n, v));
242 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
244 uniform(name, new UniformArray<Uniform3f>(n, v));
247 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
249 uniform(name, new UniformArray<Uniform4i>(n, v));
252 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
254 uniform(name, new UniformArray<Uniform4f>(n, v));
257 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
259 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
262 void ProgramData::remove_uniform(const string &name)
264 SlotMap::iterator i = uniform_slots.find(name);
265 if(i!=uniform_slots.end())
267 vector<Uniform *>::iterator j = uniforms.begin()+i->second;
271 for(SlotMap::iterator k=uniform_slots.begin(); k!=uniform_slots.end(); ++k)
272 if(k->second>i->second)
275 uniform_slots.erase(i);
281 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
284 for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
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;
294 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
296 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
298 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
299 if(j!=uniform_slots.end())
300 block.attach(**i, *uniforms[j->second]);
304 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
306 BlockMap::iterator i = blocks.find(info.layout_hash);
309 unsigned used = compute_slot_mask(info);
314 if(info.bind_point>=0)
318 buffer = new Buffer(UNIFORM_BUFFER);
319 buffer->set_usage(STREAM_DRAW);
322 block = new UniformBlock(info.data_size);
323 block->use_buffer(buffer, last_block);
327 block = new UniformBlock;
329 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
335 void ProgramData::apply() const
337 const Program *prog = Program::current();
339 throw invalid_operation("ProgramData::apply");
341 Program::LayoutHash layout = prog->get_uniform_layout_hash();
342 ProgramUniforms &pu = programs[layout];
344 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
345 Mask affected = (dirty&pu.used) | force_dirty;
346 if(affected|pu.dirty)
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. */
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;
360 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
362 if(pu.dirty==ALL_ONES)
364 /* The set of uniforms has changed since this program was last used.
365 Regenerate the list of uniform blocks. */
367 pu.blocks.reserve(prog_blocks.size());
370 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
372 SharedBlock *shared = get_shared_block(i->second);
375 if(shared->dirty==ALL_ONES)
376 shared->used = compute_slot_mask(i->second);
377 pu.used |= shared->used;
380 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
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)
389 if(!j->shared || !j->shared->dirty)
392 update_block(*j->block, i->second);
393 j->shared->dirty = 0;
394 buffered_blocks_updated |= (j->bind_point>=0);
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)
405 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
407 i->block->apply(i->bind_point);
411 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
418 ProgramData::ProgramBlock::ProgramBlock():
424 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
426 block(b ? b->block : 0),
431 ProgramData::ProgramUniforms::ProgramUniforms():
437 ProgramData::Loader::Loader(ProgramData &pd):
438 DataFile::ObjectLoader<ProgramData>(pd)
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);
464 void ProgramData::Loader::uniform1i(const string &n, int v)
469 void ProgramData::Loader::uniform1f(const string &n, float v)
474 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
476 obj.uniform(n, v0, v1);
479 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
481 obj.uniform(n, v0, v1);
484 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
486 obj.uniform(n, v0, v1, v2);
489 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
491 obj.uniform(n, v0, v1, v2);
494 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
496 obj.uniform(n, v0, v1, v2, v3);
499 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
501 obj.uniform(n, v0, v1, v2, v3);
504 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
506 ArrayLoader ldr(t, e);
508 unsigned size = ldr.get_size();
510 throw logic_error("empty uniform array");
512 DataType type = ldr.get_data_type();
513 unsigned elem_size = ldr.get_element_size();
516 const int *data = reinterpret_cast<const int *>(ldr.get_data());
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);
526 throw logic_error("unsupported combination of array type and element size");
530 const float *data = reinterpret_cast<const float *>(ldr.get_data());
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);
540 throw logic_error("unsupported combination of array type and element size");
543 throw logic_error("unsupported array type");
546 void ProgramData::Loader::uniform1i_array(const string &n)
548 uniform_array_(n, INT, 1);
551 void ProgramData::Loader::uniform1f_array(const string &n)
553 uniform_array_(n, FLOAT, 1);
556 void ProgramData::Loader::uniform2i_array(const string &n)
558 uniform_array_(n, INT, 2);
561 void ProgramData::Loader::uniform2f_array(const string &n)
563 uniform_array_(n, FLOAT, 2);
566 void ProgramData::Loader::uniform3i_array(const string &n)
568 uniform_array_(n, INT, 3);
571 void ProgramData::Loader::uniform3f_array(const string &n)
573 uniform_array_(n, FLOAT, 3);
576 void ProgramData::Loader::uniform4i_array(const string &n)
578 uniform_array_(n, INT, 4);
581 void ProgramData::Loader::uniform4f_array(const string &n)
583 uniform_array_(n, FLOAT, 4);
586 void ProgramData::Loader::uniform_array(const string &n)
588 uniform_array_(n, static_cast<DataType>(0), 0);
592 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
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);
608 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
610 if(element_size && (t!=type || e!=element_size))
611 throw logic_error("heterogeneous array contents");
619 const char *cv = reinterpret_cast<const char *>(v);
620 data.insert(data.end(), cv, cv+element_size*4);
623 void ProgramData::ArrayLoader::uniform1i(int v)
628 void ProgramData::ArrayLoader::uniform1f(float v)
630 uniform(FLOAT, 1, &v);
633 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
635 int va[2] = { v0, v1 };
639 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
641 float va[2] = { v0, v1 };
642 uniform(FLOAT, 2, va);
645 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
647 int va[3] = { v0, v1, v2 };
651 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
653 float va[3] = { v0, v1, v2 };
654 uniform(FLOAT, 3, va);
657 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
659 int va[4] = { v0, v1, v2, v3 };
663 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
665 float va[4] = { v0, v1, v2, v3 };
666 uniform(FLOAT, 4, va);