1 #include <msp/core/maputils.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/io/print.h>
10 #include "programdata.h"
12 #include "uniformblock.h"
20 ProgramData::ProgramData(const Program *p):
27 // Blocks are intentionally left uncopied
28 ProgramData::ProgramData(const ProgramData &other):
29 tied_program(other.tied_program),
30 uniforms(other.uniforms),
35 for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
36 i->value = i->value->clone();
39 ProgramData::ProgramData(const ProgramData &other, const Program *p):
47 for(vector<TaggedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
51 uniforms = other.uniforms;
52 for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
53 i->value = i->value->clone();
56 ProgramData &ProgramData::operator=(const ProgramData &other)
58 tied_program = other.tied_program;
60 uniforms = other.uniforms;
61 for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
62 i->value = i->value->clone();
64 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
65 delete i->second.block;
75 ProgramData::~ProgramData()
77 for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
79 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
81 if(i->second.indices.type_flag==0xFE)
82 delete[] i->second.indices.dynamic.values;
83 delete i->second.block;
88 void ProgramData::uniform(Tag tag, Uniform *uni)
92 if(!validate_tag(tag))
104 int i = find_uniform_index(tag);
106 return add_uniform(tag, uni);
108 uniforms[i].replace_value(uni);
112 template<typename T, typename V>
113 void ProgramData::uniform(Tag tag, V value)
115 if(!validate_tag(tag))
118 int i = find_uniform_index(tag);
120 return add_uniform(tag, new T(value));
122 if(T *uni = dynamic_cast<T *>(uniforms[i].value))
125 uniforms[i].replace_value(new T(value));
130 template<typename T, typename V>
131 void ProgramData::uniform_array(Tag tag, unsigned n, V value)
133 if(!validate_tag(tag))
136 int i = find_uniform_index(tag);
138 return add_uniform(tag, new UniformArray<T>(n, value));
140 UniformArray<T> *uni = dynamic_cast<UniformArray<T> *>(uniforms[i].value);
141 if(uni && n==uni->size())
144 uniforms[i].replace_value(new UniformArray<T>(n, value));
149 bool ProgramData::validate_tag(Tag tag) const
156 tied_program->get_uniform_info(tag);
160 catch(const exception &e)
162 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
168 void ProgramData::add_uniform(Tag tag, Uniform *uni)
170 if(uniforms.size()>=MASK_BITS)
173 throw too_many_uniforms(tag.str());
176 vector<TaggedUniform>::iterator j = lower_bound(uniforms.begin(), uniforms.end(), tag, uniform_tag_compare);
181 uniforms.insert(j, nu);
186 void ProgramData::uniform(Tag tag, const Uniform &u)
188 uniform(tag, u.clone());
191 void ProgramData::uniform(Tag tag, int v)
193 uniform<Uniform1i>(tag, v);
196 void ProgramData::uniform(Tag tag, float v)
198 uniform<Uniform1f>(tag, v);
201 void ProgramData::uniform(Tag tag, int v0, int v1)
203 int va[2] = { v0, v1 };
207 void ProgramData::uniform(Tag tag, float v0, float v1)
209 float va[2] = { v0, v1 };
213 void ProgramData::uniform2(Tag tag, const int *v)
215 uniform<Uniform2i>(tag, v);
218 void ProgramData::uniform2(Tag tag, const float *v)
220 uniform<Uniform2f>(tag, v);
223 void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
225 int va[3] = { v0, v1, v2 };
229 void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
231 float va[3] = { v0, v1, v2 };
235 void ProgramData::uniform(Tag tag, const Vector3 &v)
237 uniform(tag, v.x, v.y, v.z);
240 void ProgramData::uniform3(Tag tag, const int *v)
242 uniform<Uniform3i>(tag, v);
245 void ProgramData::uniform3(Tag tag, const float *v)
247 uniform<Uniform3f>(tag, v);
250 void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
252 int va[4] = { v0, v1, v2, v3 };
256 void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
258 float va[4] = { v0, v1, v2, v3 };
262 void ProgramData::uniform(Tag tag, const Vector4 &v)
264 uniform(tag, v.x, v.y, v.z, v.w);
267 void ProgramData::uniform(Tag tag, const Color &c)
269 uniform(tag, c.r, c.g, c.b, c.a);
272 void ProgramData::uniform4(Tag tag, const int *v)
274 uniform<Uniform4i>(tag, v);
277 void ProgramData::uniform4(Tag tag, const float *v)
279 uniform<Uniform4f>(tag, v);
282 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 2, 2> &m)
284 uniform_matrix2(tag, &m(0, 0));
287 void ProgramData::uniform_matrix2(Tag tag, const float *v)
289 uniform<UniformMatrix2x2f>(tag, v);
292 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 2, 3> &m)
294 uniform_matrix3x2(tag, &m(0, 0));
297 void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
299 uniform<UniformMatrix3x2f>(tag, v);
302 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 2, 4> &m)
304 uniform_matrix4x2(tag, &m(0, 0));
307 void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
309 uniform<UniformMatrix4x2f>(tag, v);
312 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 3, 2> &m)
314 uniform_matrix2x3(tag, &m(0, 0));
317 void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
319 uniform<UniformMatrix2x3f>(tag, v);
322 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 3, 3> &m)
324 uniform_matrix3(tag, &m(0, 0));
327 void ProgramData::uniform_matrix3(Tag tag, const float *v)
329 uniform<UniformMatrix3x3f>(tag, v);
332 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 3, 4> &m)
334 uniform_matrix4x3(tag, &m(0, 0));
337 void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
339 uniform<UniformMatrix4x3f>(tag, v);
342 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 4, 2> &m)
344 uniform_matrix2x4(tag, &m(0, 0));
347 void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
349 uniform<UniformMatrix2x4f>(tag, v);
352 void ProgramData::uniform(Tag tag, const LinAl::Matrix<float, 4, 3> &m)
354 uniform_matrix3x4(tag, &m(0, 0));
357 void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
359 uniform<UniformMatrix3x4f>(tag, v);
362 void ProgramData::uniform(Tag tag, const Matrix &m)
364 uniform_matrix4(tag, m.data());
367 void ProgramData::uniform_matrix4(Tag tag, const float *v)
369 uniform<UniformMatrix4x4f>(tag, v);
372 void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
374 uniform_array<Uniform1i>(tag, n, v);
377 void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
379 uniform_array<Uniform1f>(tag, n, v);
382 void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
384 uniform_array<Uniform2i>(tag, n, v);
387 void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
389 uniform_array<Uniform2f>(tag, n, v);
392 void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
394 uniform_array<Uniform3i>(tag, n, v);
397 void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
399 uniform_array<Uniform3f>(tag, n, v);
402 void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
404 uniform_array<Uniform4i>(tag, n, v);
407 void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
409 uniform_array<Uniform4f>(tag, n, v);
412 void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
414 uniform_array<UniformMatrix2x2f>(tag, n, v);
417 void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
419 uniform_array<UniformMatrix3x2f>(tag, n, v);
422 void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
424 uniform_array<UniformMatrix4x2f>(tag, n, v);
427 void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
429 uniform_array<UniformMatrix2x3f>(tag, n, v);
432 void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
434 uniform_array<UniformMatrix3x3f>(tag, n, v);
437 void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
439 uniform_array<UniformMatrix4x3f>(tag, n, v);
442 void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
444 uniform_array<UniformMatrix2x4f>(tag, n, v);
447 void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
449 uniform_array<UniformMatrix3x4f>(tag, n, v);
452 void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
454 uniform_array<UniformMatrix4x4f>(tag, n, v);
457 void ProgramData::remove_uniform(Tag tag)
459 vector<TaggedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), tag, uniform_tag_compare);
460 if(i==uniforms.end() || i->tag!=tag)
469 vector<Tag> ProgramData::get_uniform_tags() const
472 tags.reserve(uniforms.size());
473 for(vector<TaggedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
474 tags.push_back(i->tag);
478 const Uniform &ProgramData::get_uniform(Tag tag) const
480 int i = find_uniform_index(tag);
482 throw key_error(tag);
483 return *uniforms[i].value;
486 const Uniform *ProgramData::find_uniform(Tag tag) const
488 int i = find_uniform_index(tag);
489 return (i>=0 ? uniforms[i].value : 0);
492 bool ProgramData::uniform_tag_compare(const TaggedUniform &tu, Tag tag)
497 int ProgramData::find_uniform_index(Tag tag) const
499 vector<TaggedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), tag, uniform_tag_compare);
500 return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
503 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
505 UInt8 *indices = block.indices.values;
506 if(info.uniforms.size()>16)
508 if(block.indices.type_flag==0xFD)
510 block.indices.dynamic.values = new UInt8[info.uniforms.size()];
511 block.indices.type_flag = 0xFE;
513 indices = block.indices.dynamic.values;
517 for(unsigned i=0; i<info.uniforms.size(); ++i)
519 int j = find_uniform_index(info.uniforms[i]->tag);
523 if(static_cast<unsigned>(j)<MASK_BITS)
530 block.dirty = block.used;
533 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
535 const UInt8 *indices = block.get_uniform_indices();
536 for(unsigned i=0; i<info.uniforms.size(); ++i)
538 block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
541 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
543 BlockMap::iterator i = blocks.find(info.layout_hash);
546 bool any_found = false;
547 bool all_found = true;
548 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); j!=info.uniforms.end(); ++j)
550 if(find_uniform_index((*j)->name)>=0)
558 else if(!all_found && info.bind_point>=0)
561 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
563 throw incomplete_uniform_block(info.name);
568 if(info.bind_point>=0)
571 buffer = new Buffer(UNIFORM_BUFFER);
573 block = new UniformBlock(info.data_size);
574 block->use_buffer(buffer, last_block);
578 block = new UniformBlock;
580 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
581 update_block_uniform_indices(i->second, info);
587 void ProgramData::apply() const
589 const Program *prog = Program::current();
591 throw invalid_operation("ProgramData::apply");
593 Program::LayoutHash layout = prog->get_uniform_layout_hash();
594 ProgramUniforms &pu = programs[layout];
596 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
597 Mask affected = (dirty&pu.used) | force_dirty;
598 if(affected|pu.dirty)
600 /* If the global dirty flag affects this program, add it to per-program
601 dirty flags and clear the global flag. A previously unseen program will
602 cause this to happen if there's any dirty uniforms. */
605 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
606 i->second.dirty |= (dirty&i->second.used) | force_dirty;
607 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
608 i->second.dirty |= (dirty&i->second.used) | force_dirty;
612 const vector<Program::UniformBlockInfo> &prog_blocks = prog->get_uniform_blocks();
614 UniformBlock *old_last_block = last_block;
615 if(pu.dirty==ALL_ONES)
617 /* The set of uniforms has changed since this program was last used.
618 Regenerate the list of uniform blocks. */
620 pu.blocks.reserve(prog_blocks.size());
623 for(vector<Program::UniformBlockInfo>::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
625 SharedBlock *shared = get_shared_block(*i);
628 if(shared->dirty==ALL_ONES)
629 update_block_uniform_indices(*shared, *i);
630 pu.used |= shared->used;
633 pu.blocks.push_back(ProgramBlock(i->bind_point, shared));
637 // Update the contents of all dirty blocks.
638 bool buffered_blocks_updated = false;
639 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
640 for(vector<Program::UniformBlockInfo>::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
642 if(!j->shared || !j->shared->dirty)
645 update_block(*j->shared, *i);
646 j->shared->dirty = 0;
647 buffered_blocks_updated |= (j->bind_point>=0);
652 /* If any blocks stored in the buffer were updated, bind the buffer here
653 to avoid state thrashing. */
654 if(buffered_blocks_updated && !ARB_direct_state_access)
657 if(last_block!=old_last_block)
659 unsigned required_size = last_block->get_required_buffer_size();
660 if(last_block->get_required_buffer_size()>buffer->get_size())
662 if(buffer->get_size()>0)
665 buffer = new Buffer(UNIFORM_BUFFER);
666 last_block->change_buffer(buffer);
669 buffer->storage(required_size);
674 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
676 i->block->apply(i->bind_point);
680 ProgramData::TaggedUniform::TaggedUniform():
684 void ProgramData::TaggedUniform::replace_value(Uniform *v)
686 /* UniformBlock does not copy the uniforms, so existing default blocks
687 will be left with stale pointers. This is not a problem as long as no
688 one stores pointers to the blocks and expects them to stay valid. */
694 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
699 indices.type_flag = 0xFD;
702 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
704 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
708 ProgramData::ProgramBlock::ProgramBlock():
714 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
716 block((b && b->used) ? b->block : 0),
721 ProgramData::ProgramUniforms::ProgramUniforms():
727 ProgramData::Loader::Loader(ProgramData &pd):
728 DataFile::ObjectLoader<ProgramData>(pd)
730 add("uniform", &Loader::uniform1i);
731 add("uniform1i", &Loader::uniform1i);
732 add("uniform", &Loader::uniform1f);
733 add("uniform1f", &Loader::uniform1f);
734 add("uniform", &Loader::uniform2i);
735 add("uniform2i", &Loader::uniform2i);
736 add("uniform", &Loader::uniform2f);
737 add("uniform2f", &Loader::uniform2f);
738 add("uniform", &Loader::uniform3i);
739 add("uniform3i", &Loader::uniform3i);
740 add("uniform", &Loader::uniform3f);
741 add("uniform3f", &Loader::uniform3f);
742 add("uniform", &Loader::uniform4i);
743 add("uniform4i", &Loader::uniform4i);
744 add("uniform", &Loader::uniform4f);
745 add("uniform4f", &Loader::uniform4f);
746 add("uniform1i_array", &Loader::uniform1i_array);
747 add("uniform1f_array", &Loader::uniform1f_array);
748 add("uniform2f_array", &Loader::uniform2f_array);
749 add("uniform3f_array", &Loader::uniform3f_array);
750 add("uniform4f_array", &Loader::uniform4f_array);
751 add("uniform_array", &Loader::uniform_array);
754 void ProgramData::Loader::uniform1i(const string &n, int v)
759 void ProgramData::Loader::uniform1f(const string &n, float v)
764 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
766 obj.uniform(n, v0, v1);
769 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
771 obj.uniform(n, v0, v1);
774 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
776 obj.uniform(n, v0, v1, v2);
779 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
781 obj.uniform(n, v0, v1, v2);
784 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
786 obj.uniform(n, v0, v1, v2, v3);
789 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
791 obj.uniform(n, v0, v1, v2, v3);
794 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
796 ArrayLoader ldr(t, e);
798 unsigned size = ldr.get_size();
800 throw logic_error("empty uniform array");
802 DataType type = ldr.get_data_type();
803 unsigned elem_size = ldr.get_element_size();
806 const int *data = reinterpret_cast<const int *>(ldr.get_data());
808 obj.uniform1_array(n, size, data);
809 else if(elem_size==2)
810 obj.uniform2_array(n, size, data);
811 else if(elem_size==3)
812 obj.uniform3_array(n, size, data);
813 else if(elem_size==4)
814 obj.uniform4_array(n, size, data);
816 throw logic_error("unsupported combination of array type and element size");
820 const float *data = reinterpret_cast<const float *>(ldr.get_data());
822 obj.uniform1_array(n, size, data);
823 else if(elem_size==2)
824 obj.uniform2_array(n, size, data);
825 else if(elem_size==3)
826 obj.uniform3_array(n, size, data);
827 else if(elem_size==4)
828 obj.uniform4_array(n, size, data);
830 throw logic_error("unsupported combination of array type and element size");
833 throw logic_error("unsupported array type");
836 void ProgramData::Loader::uniform1i_array(const string &n)
838 uniform_array_(n, INT, 1);
841 void ProgramData::Loader::uniform1f_array(const string &n)
843 uniform_array_(n, FLOAT, 1);
846 void ProgramData::Loader::uniform2i_array(const string &n)
848 uniform_array_(n, INT, 2);
851 void ProgramData::Loader::uniform2f_array(const string &n)
853 uniform_array_(n, FLOAT, 2);
856 void ProgramData::Loader::uniform3i_array(const string &n)
858 uniform_array_(n, INT, 3);
861 void ProgramData::Loader::uniform3f_array(const string &n)
863 uniform_array_(n, FLOAT, 3);
866 void ProgramData::Loader::uniform4i_array(const string &n)
868 uniform_array_(n, INT, 4);
871 void ProgramData::Loader::uniform4f_array(const string &n)
873 uniform_array_(n, FLOAT, 4);
876 void ProgramData::Loader::uniform_array(const string &n)
878 uniform_array_(n, static_cast<DataType>(0), 0);
882 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
886 add("uniform", &ArrayLoader::uniform1i);
887 add("uniform1i", &ArrayLoader::uniform1i);
888 add("uniform", &ArrayLoader::uniform1f);
889 add("uniform1f", &ArrayLoader::uniform1f);
890 add("uniform", &ArrayLoader::uniform2f);
891 add("uniform2f", &ArrayLoader::uniform2f);
892 add("uniform", &ArrayLoader::uniform3f);
893 add("uniform3f", &ArrayLoader::uniform3f);
894 add("uniform", &ArrayLoader::uniform4f);
895 add("uniform4f", &ArrayLoader::uniform4f);
898 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
900 if(element_size && (t!=type || e!=element_size))
901 throw logic_error("heterogeneous array contents");
909 const char *cv = reinterpret_cast<const char *>(v);
910 data.insert(data.end(), cv, cv+element_size*4);
913 void ProgramData::ArrayLoader::uniform1i(int v)
918 void ProgramData::ArrayLoader::uniform1f(float v)
920 uniform(FLOAT, 1, &v);
923 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
925 int va[2] = { v0, v1 };
929 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
931 float va[2] = { v0, v1 };
932 uniform(FLOAT, 2, va);
935 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
937 int va[3] = { v0, v1, v2 };
941 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
943 float va[3] = { v0, v1, v2 };
944 uniform(FLOAT, 3, va);
947 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
949 int va[4] = { v0, v1, v2, v3 };
953 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
955 float va[4] = { v0, v1, v2, v3 };
956 uniform(FLOAT, 4, va);