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)
72 last_buffer_block = 0;
79 ProgramData::~ProgramData()
81 for(TaggedUniform &u: uniforms)
83 for(SharedBlock &b: blocks)
85 if(b.indices.type_flag==0xFE)
86 delete[] b.indices.dynamic.values;
92 void ProgramData::uniform(Tag tag, Uniform *uni)
96 if(!validate_tag(tag))
108 int i = find_uniform_index(tag);
110 return add_uniform(tag, uni);
112 uniforms[i].replace_value(uni);
116 template<typename T, typename V>
117 void ProgramData::uniform(Tag tag, V value)
119 if(!validate_tag(tag))
122 int i = find_uniform_index(tag);
124 return add_uniform(tag, new T(value));
126 if(T *uni = dynamic_cast<T *>(uniforms[i].value))
129 uniforms[i].replace_value(new T(value));
134 template<typename T, typename V>
135 void ProgramData::uniform_array(Tag tag, unsigned n, V value)
137 if(!validate_tag(tag))
140 int i = find_uniform_index(tag);
142 return add_uniform(tag, new UniformArray<T>(n, value));
144 UniformArray<T> *uni = dynamic_cast<UniformArray<T> *>(uniforms[i].value);
145 if(uni && n==uni->size())
148 uniforms[i].replace_value(new UniformArray<T>(n, value));
153 bool ProgramData::validate_tag(Tag tag) const
161 const Program::UniformInfo &info = tied_program->get_uniform_info(tag);
162 if(is_image(info.type))
163 throw invalid_operation("ProgramData::uniform");
168 catch(const exception &e)
170 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
176 void ProgramData::add_uniform(Tag tag, Uniform *uni)
178 if(uniforms.size()>=MASK_BITS)
181 throw too_many_uniforms(tag.str());
184 auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
189 uniforms.insert(j, nu);
191 mark_dirty(ALL_ONES);
194 void ProgramData::mark_dirty(Mask bits)
201 void ProgramData::uniform(Tag tag, const Uniform &u)
203 uniform(tag, u.clone());
206 void ProgramData::uniform(Tag tag, int v)
208 uniform<Uniform1i>(tag, v);
211 void ProgramData::uniform(Tag tag, float v)
213 uniform<Uniform1f>(tag, v);
216 void ProgramData::uniform(Tag tag, int v0, int v1)
218 int va[2] = { v0, v1 };
222 void ProgramData::uniform(Tag tag, float v0, float v1)
224 float va[2] = { v0, v1 };
228 void ProgramData::uniform2(Tag tag, const int *v)
230 uniform<Uniform2i>(tag, v);
233 void ProgramData::uniform2(Tag tag, const float *v)
235 uniform<Uniform2f>(tag, v);
238 void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
240 int va[3] = { v0, v1, v2 };
244 void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
246 float va[3] = { v0, v1, v2 };
250 void ProgramData::uniform3(Tag tag, const int *v)
252 uniform<Uniform3i>(tag, v);
255 void ProgramData::uniform3(Tag tag, const float *v)
257 uniform<Uniform3f>(tag, v);
260 void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
262 int va[4] = { v0, v1, v2, v3 };
266 void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
268 float va[4] = { v0, v1, v2, v3 };
272 void ProgramData::uniform(Tag tag, const Color &c)
274 uniform(tag, c.r, c.g, c.b, c.a);
277 void ProgramData::uniform4(Tag tag, const int *v)
279 uniform<Uniform4i>(tag, v);
282 void ProgramData::uniform4(Tag tag, const float *v)
284 uniform<Uniform4f>(tag, v);
287 void ProgramData::uniform_matrix2(Tag tag, const float *v)
289 uniform<UniformMatrix2x2f>(tag, v);
292 void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
294 uniform<UniformMatrix3x2f>(tag, v);
297 void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
299 uniform<UniformMatrix4x2f>(tag, v);
302 void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
304 uniform<UniformMatrix2x3f>(tag, v);
307 void ProgramData::uniform_matrix3(Tag tag, const float *v)
309 uniform<UniformMatrix3x3f>(tag, v);
312 void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
314 uniform<UniformMatrix4x3f>(tag, v);
317 void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
319 uniform<UniformMatrix2x4f>(tag, v);
322 void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
324 uniform<UniformMatrix3x4f>(tag, v);
327 void ProgramData::uniform(Tag tag, const Matrix &m)
329 uniform_matrix4(tag, m.data());
332 void ProgramData::uniform_matrix4(Tag tag, const float *v)
334 uniform<UniformMatrix4x4f>(tag, v);
337 void ProgramData::uniform_array(Tag tag, unsigned n, const int *v)
339 uniform_array<Uniform1i>(tag, n, v);
342 void ProgramData::uniform_array(Tag tag, unsigned n, const float *v)
344 uniform_array<Uniform1f>(tag, n, v);
347 void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
349 uniform_array<Uniform1i>(tag, n, v);
352 void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
354 uniform_array<Uniform1f>(tag, n, v);
357 void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
359 uniform_array<Uniform2i>(tag, n, v);
362 void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
364 uniform_array<Uniform2f>(tag, n, v);
367 void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
369 uniform_array<Uniform3i>(tag, n, v);
372 void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
374 uniform_array<Uniform3f>(tag, n, v);
377 void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
379 uniform_array<Uniform4i>(tag, n, v);
382 void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
384 uniform_array<Uniform4f>(tag, n, v);
387 void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
389 uniform_array<UniformMatrix2x2f>(tag, n, v);
392 void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
394 uniform_array<UniformMatrix3x2f>(tag, n, v);
397 void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
399 uniform_array<UniformMatrix4x2f>(tag, n, v);
402 void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
404 uniform_array<UniformMatrix2x3f>(tag, n, v);
407 void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
409 uniform_array<UniformMatrix3x3f>(tag, n, v);
412 void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
414 uniform_array<UniformMatrix4x3f>(tag, n, v);
417 void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
419 uniform_array<UniformMatrix2x4f>(tag, n, v);
422 void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
424 uniform_array<UniformMatrix3x4f>(tag, n, v);
427 void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
429 uniform_array<UniformMatrix4x4f>(tag, n, v);
432 void ProgramData::remove_uniform(Tag tag)
434 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
435 if(i==uniforms.end() || i->tag!=tag)
441 mark_dirty(ALL_ONES);
444 vector<Tag> ProgramData::get_uniform_tags() const
447 tags.reserve(uniforms.size());
448 for(const TaggedUniform &u: uniforms)
449 tags.push_back(u.tag);
453 const Uniform &ProgramData::get_uniform(Tag tag) const
455 int i = find_uniform_index(tag);
457 throw key_error(tag);
458 return *uniforms[i].value;
461 const Uniform *ProgramData::find_uniform(Tag tag) const
463 int i = find_uniform_index(tag);
464 return (i>=0 ? uniforms[i].value : 0);
467 int ProgramData::find_uniform_index(Tag tag) const
469 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
470 return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
473 vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
475 Program::LayoutHash prog_hash = prog.get_uniform_layout_hash();
476 auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
477 if(i!=programs.end() && i->prog_hash==prog_hash)
480 const vector<Program::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
481 unsigned index = i-programs.begin();
482 programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash));
484 /* Block indices may change if new shared blocks need to be inserted. Store
485 the hashes so they can be matched up later. */
486 vector<Program::LayoutHash> block_hashes;
487 block_hashes.reserve(programs.size());
488 for(const ProgramBlock &b: programs)
489 block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
491 for(unsigned j=0; j<block_infos.size(); ++j)
493 const Program::UniformBlockInfo &info = block_infos[j];
494 block_hashes[index+1+j] = info.layout_hash;
495 programs[index+1+j].bind_point = info.bind_point;
497 auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
498 if(k==blocks.end() || k->block_hash!=info.layout_hash)
500 k = blocks.insert(k, SharedBlock(info.layout_hash));
501 update_block_uniform_indices(*k, info);
505 /* Reassign shared block indices from the stored hashes. */
506 for(unsigned j=0; j<programs.size(); ++j)
508 unsigned hash = block_hashes[j];
511 auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
512 programs[j].block_index = k-blocks.begin();
515 programs[j].block_index = -1;
518 return programs.begin()+index;
521 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
523 UInt8 *indices = block.indices.values;
524 if(info.uniforms.size()>16)
526 if(block.indices.type_flag==0xFD)
528 block.indices.dynamic.values = new UInt8[info.uniforms.size()];
529 block.indices.type_flag = 0xFE;
531 indices = block.indices.dynamic.values;
534 bool any_missing = false;
537 for(unsigned i=0; i<info.uniforms.size(); ++i)
539 int j = find_uniform_index(info.uniforms[i]->tag);
543 if(static_cast<unsigned>(j)<MASK_BITS)
553 if(block.used && any_missing && info.bind_point>=0)
556 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
558 throw incomplete_uniform_block(info.name);
562 block.dirty = block.used;
564 if(block.used && !block.block)
566 if(info.bind_point>=0)
570 buffer = new Buffer();
573 if(!debug_name.empty())
574 buffer->set_debug_name(debug_name);
578 BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size);
579 block.block = bb_block;
580 bb_block->use_buffer(buffer, last_buffer_block);
581 last_buffer_block = bb_block;
584 block.block = new DefaultUniformBlock;
588 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
590 const UInt8 *indices = block.get_uniform_indices();
591 for(unsigned i=0; i<info.uniforms.size(); ++i)
593 if(is_image(info.uniforms[i]->type))
594 ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData
595 else if(indices[i]!=0xFF)
596 block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
600 vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
602 BufferBackedUniformBlock *old_last_block = last_buffer_block;
603 auto prog_begin = get_program(prog);
605 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
606 Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
607 if(affected|prog_begin->masks.dirty)
609 /* If the global dirty flag affects this program, add it to per-block and
610 per-program dirty flags and clear the global flag. A previously unseen
611 program will cause this to happen if there's any dirty uniforms. */
614 for(SharedBlock &b: blocks)
615 b.dirty |= (dirty&b.used) | force_dirty;
616 for(ProgramBlock &b: programs)
618 b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
622 const vector<Program::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
624 if(prog_begin->masks.dirty==ALL_ONES)
626 /* The set of uniforms has changed since this program was last used.
627 Refresh uniform indices within the program's blocks. */
628 prog_begin->masks.used = 0;
629 auto j = prog_begin+1;
630 for(const Program::UniformBlockInfo &b: block_infos)
632 SharedBlock &shared = blocks[j->block_index];
633 if(shared.dirty==ALL_ONES)
634 update_block_uniform_indices(shared, b);
635 prog_begin->masks.used |= shared.used;
636 j->block = (shared.used ? shared.block : 0);
641 // Update the contents of all dirty blocks.
642 bool buffered_blocks_updated = false;
643 auto j = prog_begin+1;
644 for(const Program::UniformBlockInfo &b: block_infos)
646 SharedBlock &shared = blocks[j->block_index];
649 update_block(shared, b);
651 buffered_blocks_updated |= (j->bind_point>=0);
656 prog_begin->masks.dirty = 0;
658 if(last_buffer_block!=old_last_block)
660 unsigned required_size = last_buffer_block->get_required_buffer_size();
661 if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
663 if(buffer->get_size()>0)
666 buffer = new Buffer();
667 last_buffer_block->change_buffer(buffer);
670 if(!debug_name.empty())
671 buffer->set_debug_name(debug_name);
675 buffer->storage(required_size);
683 void ProgramData::apply(const Program &prog, PipelineState &state) const
685 auto prog_begin = prepare_program(prog);
686 Program::LayoutHash prog_hash = prog_begin->prog_hash;
687 for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
691 state.set_uniforms(static_cast<const DefaultUniformBlock *>(i->block));
694 const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
696 state.set_uniform_block(i->bind_point, block);
701 void ProgramData::set_debug_name(const string &name)
706 buffer->set_debug_name(name);
713 ProgramData::TaggedUniform::TaggedUniform():
717 void ProgramData::TaggedUniform::replace_value(Uniform *v)
719 /* UniformBlock does not copy the uniforms, so existing default blocks
720 will be left with stale pointers. This is not a problem as long as no
721 one stores pointers to the blocks and expects them to stay valid. */
727 ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h):
733 indices.type_flag = 0xFD;
736 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
738 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
742 ProgramData::ProgramBlock::ProgramBlock(Program::LayoutHash h):
747 masks.used = ALL_ONES;
748 masks.dirty = ALL_ONES;
752 ProgramData::Loader::Loader(ProgramData &pd):
753 DataFile::ObjectLoader<ProgramData>(pd)
755 add("uniform", &Loader::uniform1i);
756 add("uniform1i", &Loader::uniform1i);
757 add("uniform", &Loader::uniform1f);
758 add("uniform1f", &Loader::uniform1f);
759 add("uniform", &Loader::uniform2i);
760 add("uniform2i", &Loader::uniform2i);
761 add("uniform", &Loader::uniform2f);
762 add("uniform2f", &Loader::uniform2f);
763 add("uniform", &Loader::uniform3i);
764 add("uniform3i", &Loader::uniform3i);
765 add("uniform", &Loader::uniform3f);
766 add("uniform3f", &Loader::uniform3f);
767 add("uniform", &Loader::uniform4i);
768 add("uniform4i", &Loader::uniform4i);
769 add("uniform", &Loader::uniform4f);
770 add("uniform4f", &Loader::uniform4f);
771 add("uniform1i_array", &Loader::uniform1i_array);
772 add("uniform1f_array", &Loader::uniform1f_array);
773 add("uniform2f_array", &Loader::uniform2f_array);
774 add("uniform3f_array", &Loader::uniform3f_array);
775 add("uniform4f_array", &Loader::uniform4f_array);
776 add("uniform_array", &Loader::uniform_array);
779 void ProgramData::Loader::uniform1i(const string &n, int v)
784 void ProgramData::Loader::uniform1f(const string &n, float v)
789 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
791 obj.uniform(n, v0, v1);
794 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
796 obj.uniform(n, v0, v1);
799 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
801 obj.uniform(n, v0, v1, v2);
804 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
806 obj.uniform(n, v0, v1, v2);
809 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
811 obj.uniform(n, v0, v1, v2, v3);
814 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
816 obj.uniform(n, v0, v1, v2, v3);
819 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
821 ArrayLoader ldr(t, e);
823 unsigned size = ldr.get_size();
825 throw logic_error("empty uniform array");
827 DataType type = ldr.get_data_type();
828 unsigned elem_size = ldr.get_element_size();
831 const int *data = reinterpret_cast<const int *>(ldr.get_data());
833 obj.uniform1_array(n, size, data);
834 else if(elem_size==2)
835 obj.uniform2_array(n, size, data);
836 else if(elem_size==3)
837 obj.uniform3_array(n, size, data);
838 else if(elem_size==4)
839 obj.uniform4_array(n, size, data);
841 throw logic_error("unsupported combination of array type and element size");
845 const float *data = reinterpret_cast<const float *>(ldr.get_data());
847 obj.uniform1_array(n, size, data);
848 else if(elem_size==2)
849 obj.uniform2_array(n, size, data);
850 else if(elem_size==3)
851 obj.uniform3_array(n, size, data);
852 else if(elem_size==4)
853 obj.uniform4_array(n, size, data);
855 throw logic_error("unsupported combination of array type and element size");
858 throw logic_error("unsupported array type");
861 void ProgramData::Loader::uniform1i_array(const string &n)
863 uniform_array_(n, INT, 1);
866 void ProgramData::Loader::uniform1f_array(const string &n)
868 uniform_array_(n, FLOAT, 1);
871 void ProgramData::Loader::uniform2i_array(const string &n)
873 uniform_array_(n, INT, 2);
876 void ProgramData::Loader::uniform2f_array(const string &n)
878 uniform_array_(n, FLOAT, 2);
881 void ProgramData::Loader::uniform3i_array(const string &n)
883 uniform_array_(n, INT, 3);
886 void ProgramData::Loader::uniform3f_array(const string &n)
888 uniform_array_(n, FLOAT, 3);
891 void ProgramData::Loader::uniform4i_array(const string &n)
893 uniform_array_(n, INT, 4);
896 void ProgramData::Loader::uniform4f_array(const string &n)
898 uniform_array_(n, FLOAT, 4);
901 void ProgramData::Loader::uniform_array(const string &n)
903 uniform_array_(n, static_cast<DataType>(0), 0);
907 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
911 add("uniform", &ArrayLoader::uniform1i);
912 add("uniform1i", &ArrayLoader::uniform1i);
913 add("uniform", &ArrayLoader::uniform1f);
914 add("uniform1f", &ArrayLoader::uniform1f);
915 add("uniform", &ArrayLoader::uniform2f);
916 add("uniform2f", &ArrayLoader::uniform2f);
917 add("uniform", &ArrayLoader::uniform3f);
918 add("uniform3f", &ArrayLoader::uniform3f);
919 add("uniform", &ArrayLoader::uniform4f);
920 add("uniform4f", &ArrayLoader::uniform4f);
923 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
925 if(element_size && (t!=type || e!=element_size))
926 throw logic_error("heterogeneous array contents");
934 const char *cv = reinterpret_cast<const char *>(v);
935 data.insert(data.end(), cv, cv+element_size*4);
938 void ProgramData::ArrayLoader::uniform1i(int v)
943 void ProgramData::ArrayLoader::uniform1f(float v)
945 uniform(FLOAT, 1, &v);
948 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
950 int va[2] = { v0, v1 };
954 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
956 float va[2] = { v0, v1 };
957 uniform(FLOAT, 2, va);
960 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
962 int va[3] = { v0, v1, v2 };
966 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
968 float va[3] = { v0, v1, v2 };
969 uniform(FLOAT, 3, va);
972 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
974 int va[4] = { v0, v1, v2, v3 };
978 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
980 float va[4] = { v0, v1, v2, v3 };
981 uniform(FLOAT, 4, va);