1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include <msp/debug/demangle.h>
4 #include <msp/gl/extensions/arb_direct_state_access.h>
5 #include <msp/io/print.h>
10 #include "pipelinestate.h"
12 #include "programdata.h"
14 #include "uniformblock.h"
22 ProgramData::ProgramData(const Program *p):
30 // Blocks are intentionally left uncopied
31 ProgramData::ProgramData(const ProgramData &other):
32 tied_program(other.tied_program),
33 uniforms(other.uniforms),
34 generation(other.generation),
39 for(TaggedUniform &u: uniforms)
40 u.value = u.value->clone();
43 ProgramData::ProgramData(const ProgramData &other, const Program *p):
51 for(const TaggedUniform &u: other.uniforms)
55 uniforms = other.uniforms;
56 for(TaggedUniform &u: uniforms)
57 u.value = u.value->clone();
60 ProgramData &ProgramData::operator=(const ProgramData &other)
62 tied_program = other.tied_program;
64 uniforms = other.uniforms;
65 for(TaggedUniform &u: uniforms)
66 u.value = u.value->clone();
68 for(SharedBlock &b: blocks)
73 last_buffer_block = 0;
80 ProgramData::~ProgramData()
82 for(TaggedUniform &u: uniforms)
84 for(SharedBlock &b: blocks)
86 if(b.indices.type_flag==0xFE)
87 delete[] b.indices.dynamic.values;
93 void ProgramData::uniform(Tag tag, Uniform *uni)
97 if(!validate_tag(tag))
109 int i = find_uniform_index(tag);
111 return add_uniform(tag, uni);
113 uniforms[i].replace_value(uni);
117 template<typename T, typename V>
118 void ProgramData::uniform(Tag tag, V value)
120 if(!validate_tag(tag))
123 int i = find_uniform_index(tag);
125 return add_uniform(tag, new T(value));
127 if(T *uni = dynamic_cast<T *>(uniforms[i].value))
130 uniforms[i].replace_value(new T(value));
135 template<typename T, typename V>
136 void ProgramData::uniform_array(Tag tag, unsigned n, V value)
138 if(!validate_tag(tag))
141 int i = find_uniform_index(tag);
143 return add_uniform(tag, new UniformArray<T>(n, value));
145 UniformArray<T> *uni = dynamic_cast<UniformArray<T> *>(uniforms[i].value);
146 if(uni && n==uni->size())
149 uniforms[i].replace_value(new UniformArray<T>(n, value));
154 bool ProgramData::validate_tag(Tag tag) const
162 const Program::UniformInfo &info = tied_program->get_uniform_info(tag);
163 if(is_image(info.type))
164 throw invalid_operation("ProgramData::uniform");
169 catch(const exception &e)
171 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
177 void ProgramData::add_uniform(Tag tag, Uniform *uni)
179 if(uniforms.size()>=MASK_BITS)
182 throw too_many_uniforms(tag.str());
185 auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
190 uniforms.insert(j, nu);
192 mark_dirty(ALL_ONES);
195 void ProgramData::mark_dirty(Mask bits)
202 void ProgramData::uniform(Tag tag, const Uniform &u)
204 uniform(tag, u.clone());
207 void ProgramData::uniform(Tag tag, int v)
209 uniform<Uniform1i>(tag, v);
212 void ProgramData::uniform(Tag tag, float v)
214 uniform<Uniform1f>(tag, v);
217 void ProgramData::uniform(Tag tag, int v0, int v1)
219 int va[2] = { v0, v1 };
223 void ProgramData::uniform(Tag tag, float v0, float v1)
225 float va[2] = { v0, v1 };
229 void ProgramData::uniform2(Tag tag, const int *v)
231 uniform<Uniform2i>(tag, v);
234 void ProgramData::uniform2(Tag tag, const float *v)
236 uniform<Uniform2f>(tag, v);
239 void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
241 int va[3] = { v0, v1, v2 };
245 void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
247 float va[3] = { v0, v1, v2 };
251 void ProgramData::uniform3(Tag tag, const int *v)
253 uniform<Uniform3i>(tag, v);
256 void ProgramData::uniform3(Tag tag, const float *v)
258 uniform<Uniform3f>(tag, v);
261 void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
263 int va[4] = { v0, v1, v2, v3 };
267 void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
269 float va[4] = { v0, v1, v2, v3 };
273 void ProgramData::uniform(Tag tag, const Color &c)
275 uniform(tag, c.r, c.g, c.b, c.a);
278 void ProgramData::uniform4(Tag tag, const int *v)
280 uniform<Uniform4i>(tag, v);
283 void ProgramData::uniform4(Tag tag, const float *v)
285 uniform<Uniform4f>(tag, v);
288 void ProgramData::uniform_matrix2(Tag tag, const float *v)
290 uniform<UniformMatrix2x2f>(tag, v);
293 void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
295 uniform<UniformMatrix3x2f>(tag, v);
298 void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
300 uniform<UniformMatrix4x2f>(tag, v);
303 void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
305 uniform<UniformMatrix2x3f>(tag, v);
308 void ProgramData::uniform_matrix3(Tag tag, const float *v)
310 uniform<UniformMatrix3x3f>(tag, v);
313 void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
315 uniform<UniformMatrix4x3f>(tag, v);
318 void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
320 uniform<UniformMatrix2x4f>(tag, v);
323 void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
325 uniform<UniformMatrix3x4f>(tag, v);
328 void ProgramData::uniform(Tag tag, const Matrix &m)
330 uniform_matrix4(tag, m.data());
333 void ProgramData::uniform_matrix4(Tag tag, const float *v)
335 uniform<UniformMatrix4x4f>(tag, v);
338 void ProgramData::uniform_array(Tag tag, unsigned n, const int *v)
340 uniform_array<Uniform1i>(tag, n, v);
343 void ProgramData::uniform_array(Tag tag, unsigned n, const float *v)
345 uniform_array<Uniform1f>(tag, n, v);
348 void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
350 uniform_array<Uniform1i>(tag, n, v);
353 void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
355 uniform_array<Uniform1f>(tag, n, v);
358 void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
360 uniform_array<Uniform2i>(tag, n, v);
363 void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
365 uniform_array<Uniform2f>(tag, n, v);
368 void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
370 uniform_array<Uniform3i>(tag, n, v);
373 void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
375 uniform_array<Uniform3f>(tag, n, v);
378 void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
380 uniform_array<Uniform4i>(tag, n, v);
383 void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
385 uniform_array<Uniform4f>(tag, n, v);
388 void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
390 uniform_array<UniformMatrix2x2f>(tag, n, v);
393 void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
395 uniform_array<UniformMatrix3x2f>(tag, n, v);
398 void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
400 uniform_array<UniformMatrix4x2f>(tag, n, v);
403 void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
405 uniform_array<UniformMatrix2x3f>(tag, n, v);
408 void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
410 uniform_array<UniformMatrix3x3f>(tag, n, v);
413 void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
415 uniform_array<UniformMatrix4x3f>(tag, n, v);
418 void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
420 uniform_array<UniformMatrix2x4f>(tag, n, v);
423 void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
425 uniform_array<UniformMatrix3x4f>(tag, n, v);
428 void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
430 uniform_array<UniformMatrix4x4f>(tag, n, v);
433 void ProgramData::remove_uniform(Tag tag)
435 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
436 if(i==uniforms.end() || i->tag!=tag)
442 mark_dirty(ALL_ONES);
445 vector<Tag> ProgramData::get_uniform_tags() const
448 tags.reserve(uniforms.size());
449 for(const TaggedUniform &u: uniforms)
450 tags.push_back(u.tag);
454 const Uniform &ProgramData::get_uniform(Tag tag) const
456 int i = find_uniform_index(tag);
458 throw key_error(tag);
459 return *uniforms[i].value;
462 const Uniform *ProgramData::find_uniform(Tag tag) const
464 int i = find_uniform_index(tag);
465 return (i>=0 ? uniforms[i].value : 0);
468 int ProgramData::find_uniform_index(Tag tag) const
470 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
471 return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
474 vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
476 Program::LayoutHash prog_hash = prog.get_uniform_layout_hash();
477 auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
478 if(i!=programs.end() && i->prog_hash==prog_hash)
481 const vector<Program::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
482 unsigned index = i-programs.begin();
483 programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash));
485 /* Block indices may change if new shared blocks need to be inserted. Store
486 the hashes so they can be matched up later. */
487 vector<Program::LayoutHash> block_hashes;
488 block_hashes.reserve(programs.size());
489 for(const ProgramBlock &b: programs)
490 block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
492 for(unsigned j=0; j<block_infos.size(); ++j)
494 const Program::UniformBlockInfo &info = block_infos[j];
495 block_hashes[index+1+j] = info.layout_hash;
496 programs[index+1+j].bind_point = info.bind_point;
498 auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
499 if(k==blocks.end() || k->block_hash!=info.layout_hash)
501 k = blocks.insert(k, SharedBlock(info.layout_hash));
502 update_block_uniform_indices(*k, info);
506 /* Reassign shared block indices from the stored hashes. */
507 for(unsigned j=0; j<programs.size(); ++j)
509 unsigned hash = block_hashes[j];
512 auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
513 programs[j].block_index = k-blocks.begin();
516 programs[j].block_index = -1;
519 return programs.begin()+index;
522 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
524 uint8_t *indices = block.indices.values;
525 if(info.uniforms.size()>16)
527 if(block.indices.type_flag==0xFD)
529 block.indices.dynamic.values = new uint8_t[info.uniforms.size()];
530 block.indices.type_flag = 0xFE;
532 indices = block.indices.dynamic.values;
535 bool any_missing = false;
538 for(unsigned i=0; i<info.uniforms.size(); ++i)
540 int j = find_uniform_index(info.uniforms[i]->tag);
544 if(static_cast<unsigned>(j)<MASK_BITS)
554 if(block.used && any_missing && info.bind_point>=0)
557 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
559 throw incomplete_uniform_block(info.name);
563 block.dirty = block.used;
565 if(block.used && !block.block)
567 if(info.bind_point>=0)
571 buffer = new Buffer();
574 if(!debug_name.empty())
575 buffer->set_debug_name(debug_name);
579 BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size);
580 block.block = bb_block;
581 bb_block->use_buffer(buffer, last_buffer_block);
582 last_buffer_block = bb_block;
585 block.block = new DefaultUniformBlock;
589 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
591 const uint8_t *indices = block.get_uniform_indices();
592 for(unsigned i=0; i<info.uniforms.size(); ++i)
594 if(is_image(info.uniforms[i]->type))
595 ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData
596 else if(indices[i]!=0xFF)
597 block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
601 vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
603 BufferBackedUniformBlock *old_last_block = last_buffer_block;
604 auto prog_begin = get_program(prog);
606 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
607 Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
608 if(affected|prog_begin->masks.dirty)
610 /* If the global dirty flag affects this program, add it to per-block and
611 per-program dirty flags and clear the global flag. A previously unseen
612 program will cause this to happen if there's any dirty uniforms. */
615 for(SharedBlock &b: blocks)
616 b.dirty |= (dirty&b.used) | force_dirty;
617 for(ProgramBlock &b: programs)
619 b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
623 const vector<Program::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
625 if(prog_begin->masks.dirty==ALL_ONES)
627 /* The set of uniforms has changed since this program was last used.
628 Refresh uniform indices within the program's blocks. */
629 prog_begin->masks.used = 0;
630 auto j = prog_begin+1;
631 for(const Program::UniformBlockInfo &b: block_infos)
633 SharedBlock &shared = blocks[j->block_index];
634 if(shared.dirty==ALL_ONES)
635 update_block_uniform_indices(shared, b);
636 prog_begin->masks.used |= shared.used;
637 j->block = (shared.used ? shared.block : 0);
642 // Update the contents of all dirty blocks.
643 bool buffered_blocks_updated = false;
644 auto j = prog_begin+1;
645 for(const Program::UniformBlockInfo &b: block_infos)
647 SharedBlock &shared = blocks[j->block_index];
650 update_block(shared, b);
652 buffered_blocks_updated |= (j->bind_point>=0);
657 prog_begin->masks.dirty = 0;
659 if(last_buffer_block!=old_last_block)
661 unsigned required_size = last_buffer_block->get_required_buffer_size();
662 if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
664 if(buffer->get_size()>0)
667 buffer = new Buffer();
668 last_buffer_block->change_buffer(buffer);
671 if(!debug_name.empty())
672 buffer->set_debug_name(debug_name);
676 buffer->storage(required_size);
684 void ProgramData::apply(const Program &prog, PipelineState &state) const
686 auto prog_begin = prepare_program(prog);
687 Program::LayoutHash prog_hash = prog_begin->prog_hash;
688 for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
692 state.set_uniforms(static_cast<const DefaultUniformBlock *>(i->block));
695 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
697 state.set_uniform_block(i->bind_point, block);
702 void ProgramData::set_debug_name(const string &name)
707 buffer->set_debug_name(name);
714 ProgramData::TaggedUniform::TaggedUniform():
718 void ProgramData::TaggedUniform::replace_value(Uniform *v)
720 /* UniformBlock does not copy the uniforms, so existing default blocks
721 will be left with stale pointers. This is not a problem as long as no
722 one stores pointers to the blocks and expects them to stay valid. */
728 ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h):
734 indices.type_flag = 0xFD;
737 const uint8_t *ProgramData::SharedBlock::get_uniform_indices() const
739 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
743 ProgramData::ProgramBlock::ProgramBlock(Program::LayoutHash h):
748 masks.used = ALL_ONES;
749 masks.dirty = ALL_ONES;
753 ProgramData::Loader::Loader(ProgramData &pd):
754 DataFile::ObjectLoader<ProgramData>(pd)
756 add("uniform", &Loader::uniform1i);
757 add("uniform1i", &Loader::uniform1i);
758 add("uniform", &Loader::uniform1f);
759 add("uniform1f", &Loader::uniform1f);
760 add("uniform", &Loader::uniform2i);
761 add("uniform2i", &Loader::uniform2i);
762 add("uniform", &Loader::uniform2f);
763 add("uniform2f", &Loader::uniform2f);
764 add("uniform", &Loader::uniform3i);
765 add("uniform3i", &Loader::uniform3i);
766 add("uniform", &Loader::uniform3f);
767 add("uniform3f", &Loader::uniform3f);
768 add("uniform", &Loader::uniform4i);
769 add("uniform4i", &Loader::uniform4i);
770 add("uniform", &Loader::uniform4f);
771 add("uniform4f", &Loader::uniform4f);
772 add("uniform1i_array", &Loader::uniform1i_array);
773 add("uniform1f_array", &Loader::uniform1f_array);
774 add("uniform2f_array", &Loader::uniform2f_array);
775 add("uniform3f_array", &Loader::uniform3f_array);
776 add("uniform4f_array", &Loader::uniform4f_array);
777 add("uniform_array", &Loader::uniform_array);
780 void ProgramData::Loader::uniform1i(const string &n, int v)
785 void ProgramData::Loader::uniform1f(const string &n, float v)
790 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
792 obj.uniform(n, v0, v1);
795 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
797 obj.uniform(n, v0, v1);
800 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
802 obj.uniform(n, v0, v1, v2);
805 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
807 obj.uniform(n, v0, v1, v2);
810 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
812 obj.uniform(n, v0, v1, v2, v3);
815 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
817 obj.uniform(n, v0, v1, v2, v3);
820 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
822 ArrayLoader ldr(t, e);
824 unsigned size = ldr.get_size();
826 throw logic_error("empty uniform array");
828 DataType type = ldr.get_data_type();
829 unsigned elem_size = ldr.get_element_size();
832 const int *data = reinterpret_cast<const int *>(ldr.get_data());
834 obj.uniform1_array(n, size, data);
835 else if(elem_size==2)
836 obj.uniform2_array(n, size, data);
837 else if(elem_size==3)
838 obj.uniform3_array(n, size, data);
839 else if(elem_size==4)
840 obj.uniform4_array(n, size, data);
842 throw logic_error("unsupported combination of array type and element size");
846 const float *data = reinterpret_cast<const float *>(ldr.get_data());
848 obj.uniform1_array(n, size, data);
849 else if(elem_size==2)
850 obj.uniform2_array(n, size, data);
851 else if(elem_size==3)
852 obj.uniform3_array(n, size, data);
853 else if(elem_size==4)
854 obj.uniform4_array(n, size, data);
856 throw logic_error("unsupported combination of array type and element size");
859 throw logic_error("unsupported array type");
862 void ProgramData::Loader::uniform1i_array(const string &n)
864 uniform_array_(n, INT, 1);
867 void ProgramData::Loader::uniform1f_array(const string &n)
869 uniform_array_(n, FLOAT, 1);
872 void ProgramData::Loader::uniform2i_array(const string &n)
874 uniform_array_(n, INT, 2);
877 void ProgramData::Loader::uniform2f_array(const string &n)
879 uniform_array_(n, FLOAT, 2);
882 void ProgramData::Loader::uniform3i_array(const string &n)
884 uniform_array_(n, INT, 3);
887 void ProgramData::Loader::uniform3f_array(const string &n)
889 uniform_array_(n, FLOAT, 3);
892 void ProgramData::Loader::uniform4i_array(const string &n)
894 uniform_array_(n, INT, 4);
897 void ProgramData::Loader::uniform4f_array(const string &n)
899 uniform_array_(n, FLOAT, 4);
902 void ProgramData::Loader::uniform_array(const string &n)
904 uniform_array_(n, static_cast<DataType>(0), 0);
908 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
912 add("uniform", &ArrayLoader::uniform1i);
913 add("uniform1i", &ArrayLoader::uniform1i);
914 add("uniform", &ArrayLoader::uniform1f);
915 add("uniform1f", &ArrayLoader::uniform1f);
916 add("uniform", &ArrayLoader::uniform2f);
917 add("uniform2f", &ArrayLoader::uniform2f);
918 add("uniform", &ArrayLoader::uniform3f);
919 add("uniform3f", &ArrayLoader::uniform3f);
920 add("uniform", &ArrayLoader::uniform4f);
921 add("uniform4f", &ArrayLoader::uniform4f);
924 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
926 if(element_size && (t!=type || e!=element_size))
927 throw logic_error("heterogeneous array contents");
935 const char *cv = reinterpret_cast<const char *>(v);
936 data.insert(data.end(), cv, cv+element_size*4);
939 void ProgramData::ArrayLoader::uniform1i(int v)
944 void ProgramData::ArrayLoader::uniform1f(float v)
946 uniform(FLOAT, 1, &v);
949 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
951 int va[2] = { v0, v1 };
955 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
957 float va[2] = { v0, v1 };
958 uniform(FLOAT, 2, va);
961 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
963 int va[3] = { v0, v1, v2 };
967 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
969 float va[3] = { v0, v1, v2 };
970 uniform(FLOAT, 3, va);
973 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
975 int va[4] = { v0, v1, v2, v3 };
979 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
981 float va[4] = { v0, v1, v2, v3 };
982 uniform(FLOAT, 4, va);