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_matrix2_array(const string &name, unsigned n, const float *v)
276 uniform(name, new UniformArray<UniformMatrix2x2f>(n, v));
279 void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
281 uniform(name, new UniformArray<UniformMatrix3x3f>(n, v));
284 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
286 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
289 void ProgramData::remove_uniform(const string &name)
291 SlotMap::iterator i = uniform_slots.find(name);
292 if(i!=uniform_slots.end())
294 vector<Uniform *>::iterator j = uniforms.begin()+i->second;
298 for(SlotMap::iterator k=uniform_slots.begin(); k!=uniform_slots.end(); ++k)
299 if(k->second>i->second)
302 uniform_slots.erase(i);
308 vector<string> ProgramData::get_uniform_names() const
310 vector<string> names;
311 for(SlotMap::const_iterator i=uniform_slots.begin(); i!=uniform_slots.end(); ++i)
312 names.push_back(i->first);
316 const Uniform &ProgramData::get_uniform(const string &name) const
318 return *uniforms[get_item(uniform_slots, name)];
321 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
324 for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
326 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
327 /* TODO issue a warning (or even error?) either here or in update_block
328 if all uniforms for a buffer-backed block are not found */
329 if(j!=uniform_slots.end() && j->second<MASK_BITS)
330 mask |= 1<<j->second;
336 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
338 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
340 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
341 if(j!=uniform_slots.end())
342 block.attach(**i, *uniforms[j->second]);
346 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
348 BlockMap::iterator i = blocks.find(info.layout_hash);
351 unsigned used = compute_slot_mask(info);
356 if(info.bind_point>=0)
360 buffer = new Buffer(UNIFORM_BUFFER);
361 buffer->set_usage(STREAM_DRAW);
364 block = new UniformBlock(info.data_size);
365 block->use_buffer(buffer, last_block);
369 block = new UniformBlock;
371 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
377 void ProgramData::apply() const
379 const Program *prog = Program::current();
381 throw invalid_operation("ProgramData::apply");
383 Program::LayoutHash layout = prog->get_uniform_layout_hash();
384 ProgramUniforms &pu = programs[layout];
386 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
387 Mask affected = (dirty&pu.used) | force_dirty;
388 if(affected|pu.dirty)
390 /* If the global dirty flag affects this program, add it to per-program
391 dirty flags and clear the global flag. A previously unseen program will
392 always cause this to happen. */
395 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
396 i->second.dirty |= (dirty&i->second.used) | force_dirty;
397 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
398 i->second.dirty |= (dirty&i->second.used) | force_dirty;
402 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
404 if(pu.dirty==ALL_ONES)
406 /* The set of uniforms has changed since this program was last used.
407 Regenerate the list of uniform blocks. */
409 pu.blocks.reserve(prog_blocks.size());
412 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
414 SharedBlock *shared = get_shared_block(i->second);
417 if(shared->dirty==ALL_ONES)
418 shared->used = compute_slot_mask(i->second);
419 pu.used |= shared->used;
422 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
426 // Update the contents of all dirty blocks.
427 bool buffered_blocks_updated = false;
428 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
429 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
431 if(!j->shared || !j->shared->dirty)
434 update_block(*j->block, i->second);
435 j->shared->dirty = 0;
436 buffered_blocks_updated |= (j->bind_point>=0);
441 /* If any blocks stored in the buffer were updated, bind the buffer here
442 to avoid state thrashing. */
443 if(buffered_blocks_updated && !ARB_direct_state_access)
447 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
449 i->block->apply(i->bind_point);
453 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
460 ProgramData::ProgramBlock::ProgramBlock():
466 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
468 block((b && b->used) ? b->block : 0),
473 ProgramData::ProgramUniforms::ProgramUniforms():
479 ProgramData::Loader::Loader(ProgramData &pd):
480 DataFile::ObjectLoader<ProgramData>(pd)
482 add("uniform", &Loader::uniform1i);
483 add("uniform1i", &Loader::uniform1i);
484 add("uniform", &Loader::uniform1f);
485 add("uniform1f", &Loader::uniform1f);
486 add("uniform", &Loader::uniform2i);
487 add("uniform2i", &Loader::uniform2i);
488 add("uniform", &Loader::uniform2f);
489 add("uniform2f", &Loader::uniform2f);
490 add("uniform", &Loader::uniform3i);
491 add("uniform3i", &Loader::uniform3i);
492 add("uniform", &Loader::uniform3f);
493 add("uniform3f", &Loader::uniform3f);
494 add("uniform", &Loader::uniform4i);
495 add("uniform4i", &Loader::uniform4i);
496 add("uniform", &Loader::uniform4f);
497 add("uniform4f", &Loader::uniform4f);
498 add("uniform1i_array", &Loader::uniform1i_array);
499 add("uniform1f_array", &Loader::uniform1f_array);
500 add("uniform2f_array", &Loader::uniform2f_array);
501 add("uniform3f_array", &Loader::uniform3f_array);
502 add("uniform4f_array", &Loader::uniform4f_array);
503 add("uniform_array", &Loader::uniform_array);
506 void ProgramData::Loader::uniform1i(const string &n, int v)
511 void ProgramData::Loader::uniform1f(const string &n, float v)
516 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
518 obj.uniform(n, v0, v1);
521 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
523 obj.uniform(n, v0, v1);
526 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
528 obj.uniform(n, v0, v1, v2);
531 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
533 obj.uniform(n, v0, v1, v2);
536 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
538 obj.uniform(n, v0, v1, v2, v3);
541 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
543 obj.uniform(n, v0, v1, v2, v3);
546 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
548 ArrayLoader ldr(t, e);
550 unsigned size = ldr.get_size();
552 throw logic_error("empty uniform array");
554 DataType type = ldr.get_data_type();
555 unsigned elem_size = ldr.get_element_size();
558 const int *data = reinterpret_cast<const int *>(ldr.get_data());
560 obj.uniform1_array(n, size, data);
561 else if(elem_size==2)
562 obj.uniform2_array(n, size, data);
563 else if(elem_size==3)
564 obj.uniform3_array(n, size, data);
565 else if(elem_size==4)
566 obj.uniform4_array(n, size, data);
568 throw logic_error("unsupported combination of array type and element size");
572 const float *data = reinterpret_cast<const float *>(ldr.get_data());
574 obj.uniform1_array(n, size, data);
575 else if(elem_size==2)
576 obj.uniform2_array(n, size, data);
577 else if(elem_size==3)
578 obj.uniform3_array(n, size, data);
579 else if(elem_size==4)
580 obj.uniform4_array(n, size, data);
582 throw logic_error("unsupported combination of array type and element size");
585 throw logic_error("unsupported array type");
588 void ProgramData::Loader::uniform1i_array(const string &n)
590 uniform_array_(n, INT, 1);
593 void ProgramData::Loader::uniform1f_array(const string &n)
595 uniform_array_(n, FLOAT, 1);
598 void ProgramData::Loader::uniform2i_array(const string &n)
600 uniform_array_(n, INT, 2);
603 void ProgramData::Loader::uniform2f_array(const string &n)
605 uniform_array_(n, FLOAT, 2);
608 void ProgramData::Loader::uniform3i_array(const string &n)
610 uniform_array_(n, INT, 3);
613 void ProgramData::Loader::uniform3f_array(const string &n)
615 uniform_array_(n, FLOAT, 3);
618 void ProgramData::Loader::uniform4i_array(const string &n)
620 uniform_array_(n, INT, 4);
623 void ProgramData::Loader::uniform4f_array(const string &n)
625 uniform_array_(n, FLOAT, 4);
628 void ProgramData::Loader::uniform_array(const string &n)
630 uniform_array_(n, static_cast<DataType>(0), 0);
634 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
638 add("uniform", &ArrayLoader::uniform1i);
639 add("uniform1i", &ArrayLoader::uniform1i);
640 add("uniform", &ArrayLoader::uniform1f);
641 add("uniform1f", &ArrayLoader::uniform1f);
642 add("uniform", &ArrayLoader::uniform2f);
643 add("uniform2f", &ArrayLoader::uniform2f);
644 add("uniform", &ArrayLoader::uniform3f);
645 add("uniform3f", &ArrayLoader::uniform3f);
646 add("uniform", &ArrayLoader::uniform4f);
647 add("uniform4f", &ArrayLoader::uniform4f);
650 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
652 if(element_size && (t!=type || e!=element_size))
653 throw logic_error("heterogeneous array contents");
661 const char *cv = reinterpret_cast<const char *>(v);
662 data.insert(data.end(), cv, cv+element_size*4);
665 void ProgramData::ArrayLoader::uniform1i(int v)
670 void ProgramData::ArrayLoader::uniform1f(float v)
672 uniform(FLOAT, 1, &v);
675 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
677 int va[2] = { v0, v1 };
681 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
683 float va[2] = { v0, v1 };
684 uniform(FLOAT, 2, va);
687 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
689 int va[3] = { v0, v1, v2 };
693 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
695 float va[3] = { v0, v1, v2 };
696 uniform(FLOAT, 3, va);
699 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
701 int va[4] = { v0, v1, v2, v3 };
705 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
707 float va[4] = { v0, v1, v2, v3 };
708 uniform(FLOAT, 4, va);