1 #include <msp/core/maputils.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
8 #include "programdata.h"
10 #include "uniformblock.h"
18 ProgramData::ProgramData(const Program *p):
25 // Blocks are intentionally left uncopied
26 ProgramData::ProgramData(const ProgramData &other):
28 uniform_slots(other.uniform_slots),
29 uniforms(other.uniforms),
34 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
38 ProgramData &ProgramData::operator=(const ProgramData &other)
40 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
44 tied_program = other.tied_program;
46 uniform_slots = other.uniform_slots;
47 for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
48 uniforms.push_back((*i)->clone());
50 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
51 delete i->second.block;
61 ProgramData::~ProgramData()
63 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
65 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
66 delete i->second.block;
70 void ProgramData::uniform(const string &name, Uniform *uni)
75 tied_program->get_uniform_info(name);
76 else if(name[name.size()-1]==']')
77 throw invalid_argument("ProgramData::uniform");
85 SlotMap::iterator i = uniform_slots.find(name);
86 if(i!=uniform_slots.end())
88 Uniform *&slot = uniforms[i->second];
89 /* UniformBlock does not copy the uniforms, so existing default blocks
90 will be left with stale pointers. This is not a problem as long as no
91 one stores pointers to the blocks and expects them to stay valid. */
95 if(i->second<MASK_BITS)
96 dirty |= 1<<i->second;
97 else // Force a full update if the mask isn't wide enough
102 uniform_slots[name] = uniforms.size();
103 uniforms.push_back(uni);
108 void ProgramData::uniform(const string &name, const Uniform &u)
110 uniform(name, u.clone());
113 void ProgramData::uniform(const string &name, int v)
115 uniform(name, new Uniform1i(v));
118 void ProgramData::uniform(const string &name, float v)
120 uniform(name, new Uniform1f(v));
123 void ProgramData::uniform(const string &name, int v0, int v1)
125 int va[2] = { v0, v1 };
129 void ProgramData::uniform(const string &name, float v0, float v1)
131 float va[2] = { v0, v1 };
135 void ProgramData::uniform2(const string &name, const int *v)
137 uniform(name, new Uniform2i(v));
140 void ProgramData::uniform2(const string &name, const float *v)
142 uniform(name, new Uniform2f(v));
145 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
147 int va[3] = { v0, v1, v2 };
151 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
153 float va[3] = { v0, v1, v2 };
157 void ProgramData::uniform(const string &name, const Vector3 &v)
159 uniform(name, v.x, v.y, v.z);
162 void ProgramData::uniform3(const string &name, const int *v)
164 uniform(name, new Uniform3i(v));
167 void ProgramData::uniform3(const string &name, const float *v)
169 uniform(name, new Uniform3f(v));
172 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
174 int va[4] = { v0, v1, v2, v3 };
178 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
180 float va[4] = { v0, v1, v2, v3 };
184 void ProgramData::uniform(const string &name, const Vector4 &v)
186 uniform(name, v.x, v.y, v.z, v.w);
189 void ProgramData::uniform(const string &name, const Color &c)
191 uniform(name, c.r, c.g, c.b, c.a);
194 void ProgramData::uniform4(const string &name, const int *v)
196 uniform(name, new Uniform4i(v));
199 void ProgramData::uniform4(const string &name, const float *v)
201 uniform(name, new Uniform4f(v));
204 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
206 uniform_matrix2(name, &m(0, 0));
209 void ProgramData::uniform_matrix2(const string &name, const float *v)
211 uniform(name, new UniformMatrix2x2f(v));
214 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
216 uniform_matrix3(name, &m(0, 0));
219 void ProgramData::uniform_matrix3(const string &name, const float *v)
221 uniform(name, new UniformMatrix3x3f(v));
224 void ProgramData::uniform(const string &name, const Matrix &m)
226 uniform_matrix4(name, m.data());
229 void ProgramData::uniform_matrix4(const string &name, const float *v)
231 uniform(name, new UniformMatrix4x4f(v));
234 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
236 uniform(name, new UniformArray<Uniform1i>(n, v));
239 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
241 uniform(name, new UniformArray<Uniform1f>(n, v));
244 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
246 uniform(name, new UniformArray<Uniform2i>(n, v));
249 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
251 uniform(name, new UniformArray<Uniform2f>(n, v));
254 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
256 uniform(name, new UniformArray<Uniform3i>(n, v));
259 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
261 uniform(name, new UniformArray<Uniform3f>(n, v));
264 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
266 uniform(name, new UniformArray<Uniform4i>(n, v));
269 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
271 uniform(name, new UniformArray<Uniform4f>(n, v));
274 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
276 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
279 void ProgramData::remove_uniform(const string &name)
281 SlotMap::iterator i = uniform_slots.find(name);
282 if(i!=uniform_slots.end())
284 vector<Uniform *>::iterator j = uniforms.begin()+i->second;
288 for(SlotMap::iterator k=uniform_slots.begin(); k!=uniform_slots.end(); ++k)
289 if(k->second>i->second)
292 uniform_slots.erase(i);
298 vector<string> ProgramData::get_uniform_names() const
300 vector<string> names;
301 for(SlotMap::const_iterator i=uniform_slots.begin(); i!=uniform_slots.end(); ++i)
302 names.push_back(i->first);
306 const Uniform &ProgramData::get_uniform(const string &name) const
308 return *uniforms[get_item(uniform_slots, name)];
311 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
314 for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
316 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
317 /* TODO issue a warning (or even error?) either here or in update_block
318 if all uniforms for a buffer-backed block are not found */
319 if(j!=uniform_slots.end() && j->second<MASK_BITS)
320 mask |= 1<<j->second;
326 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
328 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
330 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
331 if(j!=uniform_slots.end())
332 block.attach(**i, *uniforms[j->second]);
336 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
338 BlockMap::iterator i = blocks.find(info.layout_hash);
341 unsigned used = compute_slot_mask(info);
346 if(info.bind_point>=0)
350 buffer = new Buffer(UNIFORM_BUFFER);
351 buffer->set_usage(STREAM_DRAW);
354 block = new UniformBlock(info.data_size);
355 block->use_buffer(buffer, last_block);
359 block = new UniformBlock;
361 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
367 void ProgramData::apply() const
369 const Program *prog = Program::current();
371 throw invalid_operation("ProgramData::apply");
373 Program::LayoutHash layout = prog->get_uniform_layout_hash();
374 ProgramUniforms &pu = programs[layout];
376 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
377 Mask affected = (dirty&pu.used) | force_dirty;
378 if(affected|pu.dirty)
380 /* If the global dirty flag affects this program, add it to per-program
381 dirty flags and clear the global flag. A previously unseen program will
382 always cause this to happen. */
385 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
386 i->second.dirty |= (dirty&i->second.used) | force_dirty;
387 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
388 i->second.dirty |= (dirty&i->second.used) | force_dirty;
392 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
394 if(pu.dirty==ALL_ONES)
396 /* The set of uniforms has changed since this program was last used.
397 Regenerate the list of uniform blocks. */
399 pu.blocks.reserve(prog_blocks.size());
402 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
404 SharedBlock *shared = get_shared_block(i->second);
407 if(shared->dirty==ALL_ONES)
408 shared->used = compute_slot_mask(i->second);
409 pu.used |= shared->used;
412 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
416 // Update the contents of all dirty blocks.
417 bool buffered_blocks_updated = false;
418 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
419 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
421 if(!j->shared || !j->shared->dirty)
424 update_block(*j->block, i->second);
425 j->shared->dirty = 0;
426 buffered_blocks_updated |= (j->bind_point>=0);
431 /* If any blocks stored in the buffer were updated, bind the buffer here
432 to avoid state thrashing. */
433 if(buffered_blocks_updated && !ARB_direct_state_access)
437 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
439 i->block->apply(i->bind_point);
443 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
450 ProgramData::ProgramBlock::ProgramBlock():
456 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
458 block((b && b->used) ? b->block : 0),
463 ProgramData::ProgramUniforms::ProgramUniforms():
469 ProgramData::Loader::Loader(ProgramData &pd):
470 DataFile::ObjectLoader<ProgramData>(pd)
472 add("uniform", &Loader::uniform1i);
473 add("uniform1i", &Loader::uniform1i);
474 add("uniform", &Loader::uniform1f);
475 add("uniform1f", &Loader::uniform1f);
476 add("uniform", &Loader::uniform2i);
477 add("uniform2i", &Loader::uniform2i);
478 add("uniform", &Loader::uniform2f);
479 add("uniform2f", &Loader::uniform2f);
480 add("uniform", &Loader::uniform3i);
481 add("uniform3i", &Loader::uniform3i);
482 add("uniform", &Loader::uniform3f);
483 add("uniform3f", &Loader::uniform3f);
484 add("uniform", &Loader::uniform4i);
485 add("uniform4i", &Loader::uniform4i);
486 add("uniform", &Loader::uniform4f);
487 add("uniform4f", &Loader::uniform4f);
488 add("uniform1i_array", &Loader::uniform1i_array);
489 add("uniform1f_array", &Loader::uniform1f_array);
490 add("uniform2f_array", &Loader::uniform2f_array);
491 add("uniform3f_array", &Loader::uniform3f_array);
492 add("uniform4f_array", &Loader::uniform4f_array);
493 add("uniform_array", &Loader::uniform_array);
496 void ProgramData::Loader::uniform1i(const string &n, int v)
501 void ProgramData::Loader::uniform1f(const string &n, float v)
506 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
508 obj.uniform(n, v0, v1);
511 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
513 obj.uniform(n, v0, v1);
516 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
518 obj.uniform(n, v0, v1, v2);
521 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
523 obj.uniform(n, v0, v1, v2);
526 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
528 obj.uniform(n, v0, v1, v2, v3);
531 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
533 obj.uniform(n, v0, v1, v2, v3);
536 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
538 ArrayLoader ldr(t, e);
540 unsigned size = ldr.get_size();
542 throw logic_error("empty uniform array");
544 DataType type = ldr.get_data_type();
545 unsigned elem_size = ldr.get_element_size();
548 const int *data = reinterpret_cast<const int *>(ldr.get_data());
550 obj.uniform1_array(n, size, data);
551 else if(elem_size==2)
552 obj.uniform2_array(n, size, data);
553 else if(elem_size==3)
554 obj.uniform3_array(n, size, data);
555 else if(elem_size==4)
556 obj.uniform4_array(n, size, data);
558 throw logic_error("unsupported combination of array type and element size");
562 const float *data = reinterpret_cast<const float *>(ldr.get_data());
564 obj.uniform1_array(n, size, data);
565 else if(elem_size==2)
566 obj.uniform2_array(n, size, data);
567 else if(elem_size==3)
568 obj.uniform3_array(n, size, data);
569 else if(elem_size==4)
570 obj.uniform4_array(n, size, data);
572 throw logic_error("unsupported combination of array type and element size");
575 throw logic_error("unsupported array type");
578 void ProgramData::Loader::uniform1i_array(const string &n)
580 uniform_array_(n, INT, 1);
583 void ProgramData::Loader::uniform1f_array(const string &n)
585 uniform_array_(n, FLOAT, 1);
588 void ProgramData::Loader::uniform2i_array(const string &n)
590 uniform_array_(n, INT, 2);
593 void ProgramData::Loader::uniform2f_array(const string &n)
595 uniform_array_(n, FLOAT, 2);
598 void ProgramData::Loader::uniform3i_array(const string &n)
600 uniform_array_(n, INT, 3);
603 void ProgramData::Loader::uniform3f_array(const string &n)
605 uniform_array_(n, FLOAT, 3);
608 void ProgramData::Loader::uniform4i_array(const string &n)
610 uniform_array_(n, INT, 4);
613 void ProgramData::Loader::uniform4f_array(const string &n)
615 uniform_array_(n, FLOAT, 4);
618 void ProgramData::Loader::uniform_array(const string &n)
620 uniform_array_(n, static_cast<DataType>(0), 0);
624 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
628 add("uniform", &ArrayLoader::uniform1i);
629 add("uniform1i", &ArrayLoader::uniform1i);
630 add("uniform", &ArrayLoader::uniform1f);
631 add("uniform1f", &ArrayLoader::uniform1f);
632 add("uniform", &ArrayLoader::uniform2f);
633 add("uniform2f", &ArrayLoader::uniform2f);
634 add("uniform", &ArrayLoader::uniform3f);
635 add("uniform3f", &ArrayLoader::uniform3f);
636 add("uniform", &ArrayLoader::uniform4f);
637 add("uniform4f", &ArrayLoader::uniform4f);
640 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
642 if(element_size && (t!=type || e!=element_size))
643 throw logic_error("heterogeneous array contents");
651 const char *cv = reinterpret_cast<const char *>(v);
652 data.insert(data.end(), cv, cv+element_size*4);
655 void ProgramData::ArrayLoader::uniform1i(int v)
660 void ProgramData::ArrayLoader::uniform1f(float v)
662 uniform(FLOAT, 1, &v);
665 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
667 int va[2] = { v0, v1 };
671 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
673 float va[2] = { v0, v1 };
674 uniform(FLOAT, 2, va);
677 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
679 int va[3] = { v0, v1, v2 };
683 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
685 float va[3] = { v0, v1, v2 };
686 uniform(FLOAT, 3, va);
689 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
691 int va[4] = { v0, v1, v2, v3 };
695 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
697 float va[4] = { v0, v1, v2, v3 };
698 uniform(FLOAT, 4, va);