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):
27 tied_program(other.tied_program),
28 uniforms(other.uniforms),
33 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
34 i->value = i->value->clone();
37 ProgramData::ProgramData(const ProgramData &other, const Program *p):
45 for(vector<NamedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
46 tied_program->get_uniform_info(i->name);
49 uniforms = other.uniforms;
50 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
51 i->value = i->value->clone();
54 ProgramData &ProgramData::operator=(const ProgramData &other)
56 tied_program = other.tied_program;
58 uniforms = other.uniforms;
59 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
60 i->value = i->value->clone();
62 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
63 delete i->second.block;
73 ProgramData::~ProgramData()
75 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
77 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
79 if(i->second.indices.type_flag==0xFE)
80 delete i->second.indices.dynamic.values;
81 delete i->second.block;
86 void ProgramData::uniform(const string &name, Uniform *uni)
91 tied_program->get_uniform_info(name);
92 else if(name[name.size()-1]==']')
93 throw invalid_argument("ProgramData::uniform");
101 int i = find_uniform_index(name);
104 uniforms[i].replace_value(uni);
106 if(static_cast<unsigned>(i)<MASK_BITS)
108 else // Force a full update if the mask isn't wide enough
114 vector<NamedUniform>::iterator j = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
119 uniforms.insert(j, nu);
124 void ProgramData::uniform(const string &name, const Uniform &u)
126 uniform(name, u.clone());
129 void ProgramData::uniform(const string &name, int v)
131 uniform(name, new Uniform1i(v));
134 void ProgramData::uniform(const string &name, float v)
136 uniform(name, new Uniform1f(v));
139 void ProgramData::uniform(const string &name, int v0, int v1)
141 int va[2] = { v0, v1 };
145 void ProgramData::uniform(const string &name, float v0, float v1)
147 float va[2] = { v0, v1 };
151 void ProgramData::uniform2(const string &name, const int *v)
153 uniform(name, new Uniform2i(v));
156 void ProgramData::uniform2(const string &name, const float *v)
158 uniform(name, new Uniform2f(v));
161 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
163 int va[3] = { v0, v1, v2 };
167 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
169 float va[3] = { v0, v1, v2 };
173 void ProgramData::uniform(const string &name, const Vector3 &v)
175 uniform(name, v.x, v.y, v.z);
178 void ProgramData::uniform3(const string &name, const int *v)
180 uniform(name, new Uniform3i(v));
183 void ProgramData::uniform3(const string &name, const float *v)
185 uniform(name, new Uniform3f(v));
188 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
190 int va[4] = { v0, v1, v2, v3 };
194 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
196 float va[4] = { v0, v1, v2, v3 };
200 void ProgramData::uniform(const string &name, const Vector4 &v)
202 uniform(name, v.x, v.y, v.z, v.w);
205 void ProgramData::uniform(const string &name, const Color &c)
207 uniform(name, c.r, c.g, c.b, c.a);
210 void ProgramData::uniform4(const string &name, const int *v)
212 uniform(name, new Uniform4i(v));
215 void ProgramData::uniform4(const string &name, const float *v)
217 uniform(name, new Uniform4f(v));
220 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
222 uniform_matrix2(name, &m(0, 0));
225 void ProgramData::uniform_matrix2(const string &name, const float *v)
227 uniform(name, new UniformMatrix2x2f(v));
230 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 3> &m)
232 uniform_matrix3x2(name, &m(0, 0));
235 void ProgramData::uniform_matrix3x2(const string &name, const float *v)
237 uniform(name, new UniformMatrix3x2f(v));
240 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 4> &m)
242 uniform_matrix4x2(name, &m(0, 0));
245 void ProgramData::uniform_matrix4x2(const string &name, const float *v)
247 uniform(name, new UniformMatrix4x2f(v));
250 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 2> &m)
252 uniform_matrix2x3(name, &m(0, 0));
255 void ProgramData::uniform_matrix2x3(const string &name, const float *v)
257 uniform(name, new UniformMatrix2x3f(v));
260 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
262 uniform_matrix3(name, &m(0, 0));
265 void ProgramData::uniform_matrix3(const string &name, const float *v)
267 uniform(name, new UniformMatrix3x3f(v));
270 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 4> &m)
272 uniform_matrix4x3(name, &m(0, 0));
275 void ProgramData::uniform_matrix4x3(const string &name, const float *v)
277 uniform(name, new UniformMatrix4x3f(v));
280 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 2> &m)
282 uniform_matrix2x4(name, &m(0, 0));
285 void ProgramData::uniform_matrix2x4(const string &name, const float *v)
287 uniform(name, new UniformMatrix2x4f(v));
290 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 3> &m)
292 uniform_matrix3x4(name, &m(0, 0));
295 void ProgramData::uniform_matrix3x4(const string &name, const float *v)
297 uniform(name, new UniformMatrix3x4f(v));
300 void ProgramData::uniform(const string &name, const Matrix &m)
302 uniform_matrix4(name, m.data());
305 void ProgramData::uniform_matrix4(const string &name, const float *v)
307 uniform(name, new UniformMatrix4x4f(v));
310 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
312 uniform(name, new UniformArray<Uniform1i>(n, v));
315 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
317 uniform(name, new UniformArray<Uniform1f>(n, v));
320 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
322 uniform(name, new UniformArray<Uniform2i>(n, v));
325 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
327 uniform(name, new UniformArray<Uniform2f>(n, v));
330 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
332 uniform(name, new UniformArray<Uniform3i>(n, v));
335 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
337 uniform(name, new UniformArray<Uniform3f>(n, v));
340 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
342 uniform(name, new UniformArray<Uniform4i>(n, v));
345 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
347 uniform(name, new UniformArray<Uniform4f>(n, v));
350 void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v)
352 uniform(name, new UniformArray<UniformMatrix2x2f>(n, v));
355 void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v)
357 uniform(name, new UniformArray<UniformMatrix3x2f>(n, v));
360 void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v)
362 uniform(name, new UniformArray<UniformMatrix4x2f>(n, v));
365 void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v)
367 uniform(name, new UniformArray<UniformMatrix2x3f>(n, v));
370 void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
372 uniform(name, new UniformArray<UniformMatrix3x3f>(n, v));
375 void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v)
377 uniform(name, new UniformArray<UniformMatrix4x3f>(n, v));
380 void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v)
382 uniform(name, new UniformArray<UniformMatrix2x4f>(n, v));
385 void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v)
387 uniform(name, new UniformArray<UniformMatrix3x4f>(n, v));
390 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
392 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
395 void ProgramData::remove_uniform(const string &name)
397 vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
398 if(i==uniforms.end() || i->name!=name)
407 vector<string> ProgramData::get_uniform_names() const
409 vector<string> names;
410 names.reserve(uniforms.size());
411 for(vector<NamedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
412 names.push_back(i->name);
416 const Uniform &ProgramData::get_uniform(const string &name) const
418 int i = find_uniform_index(name);
420 throw key_error(name);
421 return *uniforms[i].value;
424 bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
429 int ProgramData::find_uniform_index(const string &name) const
431 vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
432 return ((i!=uniforms.end() && i->name==name) ? i-uniforms.begin() : -1);
435 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
437 UInt8 *indices = block.indices.values;
438 if(info.uniforms.size()>16)
440 if(block.indices.type_flag==0xFD)
442 block.indices.dynamic.values = new UInt8[info.uniforms.size()];
443 block.indices.type_flag = 0xFE;
445 indices = block.indices.dynamic.values;
449 for(unsigned i=0; i<info.uniforms.size(); ++i)
451 int j = find_uniform_index(info.uniforms[i]->name);
455 if(static_cast<unsigned>(j)<MASK_BITS)
462 block.dirty = block.used;
465 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
467 const UInt8 *indices = block.get_uniform_indices();
468 for(unsigned i=0; i<info.uniforms.size(); ++i)
470 block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
473 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
475 BlockMap::iterator i = blocks.find(info.layout_hash);
478 bool any_found = false;
479 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); (!any_found && j!=info.uniforms.end()); ++j)
480 any_found = (find_uniform_index((*j)->name)>=0);
482 // TODO throw if all uniforms for a buffer-backed block are not found
487 if(info.bind_point>=0)
491 buffer = new Buffer(UNIFORM_BUFFER);
492 buffer->set_usage(STREAM_DRAW);
495 block = new UniformBlock(info.data_size);
496 block->use_buffer(buffer, last_block);
500 block = new UniformBlock;
502 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
503 update_block_uniform_indices(i->second, info);
509 void ProgramData::apply() const
511 const Program *prog = Program::current();
513 throw invalid_operation("ProgramData::apply");
515 Program::LayoutHash layout = prog->get_uniform_layout_hash();
516 ProgramUniforms &pu = programs[layout];
518 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
519 Mask affected = (dirty&pu.used) | force_dirty;
520 if(affected|pu.dirty)
522 /* If the global dirty flag affects this program, add it to per-program
523 dirty flags and clear the global flag. A previously unseen program will
524 always cause this to happen. */
527 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
528 i->second.dirty |= (dirty&i->second.used) | force_dirty;
529 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
530 i->second.dirty |= (dirty&i->second.used) | force_dirty;
534 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
536 if(pu.dirty==ALL_ONES)
538 /* The set of uniforms has changed since this program was last used.
539 Regenerate the list of uniform blocks. */
541 pu.blocks.reserve(prog_blocks.size());
544 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
546 SharedBlock *shared = get_shared_block(i->second);
549 if(shared->dirty==ALL_ONES)
550 update_block_uniform_indices(*shared, i->second);
551 pu.used |= shared->used;
554 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
558 // Update the contents of all dirty blocks.
559 bool buffered_blocks_updated = false;
560 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
561 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
563 if(!j->shared || !j->shared->dirty)
566 update_block(*j->shared, i->second);
567 j->shared->dirty = 0;
568 buffered_blocks_updated |= (j->bind_point>=0);
573 /* If any blocks stored in the buffer were updated, bind the buffer here
574 to avoid state thrashing. */
575 if(buffered_blocks_updated && !ARB_direct_state_access)
579 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
581 i->block->apply(i->bind_point);
585 ProgramData::NamedUniform::NamedUniform():
589 void ProgramData::NamedUniform::replace_value(Uniform *v)
591 /* UniformBlock does not copy the uniforms, so existing default blocks
592 will be left with stale pointers. This is not a problem as long as no
593 one stores pointers to the blocks and expects them to stay valid. */
599 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
604 indices.type_flag = 0xFD;
607 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
609 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
613 ProgramData::ProgramBlock::ProgramBlock():
619 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
621 block((b && b->used) ? b->block : 0),
626 ProgramData::ProgramUniforms::ProgramUniforms():
632 ProgramData::Loader::Loader(ProgramData &pd):
633 DataFile::ObjectLoader<ProgramData>(pd)
635 add("uniform", &Loader::uniform1i);
636 add("uniform1i", &Loader::uniform1i);
637 add("uniform", &Loader::uniform1f);
638 add("uniform1f", &Loader::uniform1f);
639 add("uniform", &Loader::uniform2i);
640 add("uniform2i", &Loader::uniform2i);
641 add("uniform", &Loader::uniform2f);
642 add("uniform2f", &Loader::uniform2f);
643 add("uniform", &Loader::uniform3i);
644 add("uniform3i", &Loader::uniform3i);
645 add("uniform", &Loader::uniform3f);
646 add("uniform3f", &Loader::uniform3f);
647 add("uniform", &Loader::uniform4i);
648 add("uniform4i", &Loader::uniform4i);
649 add("uniform", &Loader::uniform4f);
650 add("uniform4f", &Loader::uniform4f);
651 add("uniform1i_array", &Loader::uniform1i_array);
652 add("uniform1f_array", &Loader::uniform1f_array);
653 add("uniform2f_array", &Loader::uniform2f_array);
654 add("uniform3f_array", &Loader::uniform3f_array);
655 add("uniform4f_array", &Loader::uniform4f_array);
656 add("uniform_array", &Loader::uniform_array);
659 void ProgramData::Loader::uniform1i(const string &n, int v)
664 void ProgramData::Loader::uniform1f(const string &n, float v)
669 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
671 obj.uniform(n, v0, v1);
674 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
676 obj.uniform(n, v0, v1);
679 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
681 obj.uniform(n, v0, v1, v2);
684 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
686 obj.uniform(n, v0, v1, v2);
689 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
691 obj.uniform(n, v0, v1, v2, v3);
694 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
696 obj.uniform(n, v0, v1, v2, v3);
699 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
701 ArrayLoader ldr(t, e);
703 unsigned size = ldr.get_size();
705 throw logic_error("empty uniform array");
707 DataType type = ldr.get_data_type();
708 unsigned elem_size = ldr.get_element_size();
711 const int *data = reinterpret_cast<const int *>(ldr.get_data());
713 obj.uniform1_array(n, size, data);
714 else if(elem_size==2)
715 obj.uniform2_array(n, size, data);
716 else if(elem_size==3)
717 obj.uniform3_array(n, size, data);
718 else if(elem_size==4)
719 obj.uniform4_array(n, size, data);
721 throw logic_error("unsupported combination of array type and element size");
725 const float *data = reinterpret_cast<const float *>(ldr.get_data());
727 obj.uniform1_array(n, size, data);
728 else if(elem_size==2)
729 obj.uniform2_array(n, size, data);
730 else if(elem_size==3)
731 obj.uniform3_array(n, size, data);
732 else if(elem_size==4)
733 obj.uniform4_array(n, size, data);
735 throw logic_error("unsupported combination of array type and element size");
738 throw logic_error("unsupported array type");
741 void ProgramData::Loader::uniform1i_array(const string &n)
743 uniform_array_(n, INT, 1);
746 void ProgramData::Loader::uniform1f_array(const string &n)
748 uniform_array_(n, FLOAT, 1);
751 void ProgramData::Loader::uniform2i_array(const string &n)
753 uniform_array_(n, INT, 2);
756 void ProgramData::Loader::uniform2f_array(const string &n)
758 uniform_array_(n, FLOAT, 2);
761 void ProgramData::Loader::uniform3i_array(const string &n)
763 uniform_array_(n, INT, 3);
766 void ProgramData::Loader::uniform3f_array(const string &n)
768 uniform_array_(n, FLOAT, 3);
771 void ProgramData::Loader::uniform4i_array(const string &n)
773 uniform_array_(n, INT, 4);
776 void ProgramData::Loader::uniform4f_array(const string &n)
778 uniform_array_(n, FLOAT, 4);
781 void ProgramData::Loader::uniform_array(const string &n)
783 uniform_array_(n, static_cast<DataType>(0), 0);
787 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
791 add("uniform", &ArrayLoader::uniform1i);
792 add("uniform1i", &ArrayLoader::uniform1i);
793 add("uniform", &ArrayLoader::uniform1f);
794 add("uniform1f", &ArrayLoader::uniform1f);
795 add("uniform", &ArrayLoader::uniform2f);
796 add("uniform2f", &ArrayLoader::uniform2f);
797 add("uniform", &ArrayLoader::uniform3f);
798 add("uniform3f", &ArrayLoader::uniform3f);
799 add("uniform", &ArrayLoader::uniform4f);
800 add("uniform4f", &ArrayLoader::uniform4f);
803 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
805 if(element_size && (t!=type || e!=element_size))
806 throw logic_error("heterogeneous array contents");
814 const char *cv = reinterpret_cast<const char *>(v);
815 data.insert(data.end(), cv, cv+element_size*4);
818 void ProgramData::ArrayLoader::uniform1i(int v)
823 void ProgramData::ArrayLoader::uniform1f(float v)
825 uniform(FLOAT, 1, &v);
828 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
830 int va[2] = { v0, v1 };
834 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
836 float va[2] = { v0, v1 };
837 uniform(FLOAT, 2, va);
840 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
842 int va[3] = { v0, v1, v2 };
846 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
848 float va[3] = { v0, v1, v2 };
849 uniform(FLOAT, 3, va);
852 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
854 int va[4] = { v0, v1, v2, v3 };
858 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
860 float va[4] = { v0, v1, v2, v3 };
861 uniform(FLOAT, 4, va);