1 #include <msp/core/algorithm.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/io/print.h>
8 #include "pipelinestate.h"
10 #include "programdata.h"
11 #include "uniformblock.h"
19 ProgramData::ProgramData(const Program *p):
23 // Blocks are intentionally left uncopied
24 ProgramData::ProgramData(const ProgramData &other):
25 tied_program(other.tied_program),
26 uniforms(other.uniforms),
27 uniform_data(other.uniform_data),
28 generation(other.generation)
31 ProgramData::ProgramData(const ProgramData &other, const Program *p):
36 for(const TaggedUniform &u: other.uniforms)
40 uniforms = other.uniforms;
41 uniform_data = other.uniform_data;
44 ProgramData &ProgramData::operator=(const ProgramData &other)
46 tied_program = other.tied_program;
48 uniforms = other.uniforms;
49 uniform_data = other.uniform_data;
51 for(SharedBlock &b: blocks)
56 last_buffer_block = 0;
63 ProgramData::~ProgramData()
65 for(SharedBlock &b: blocks)
67 if(b.indices.type_flag==0xFE)
68 delete[] b.indices.dynamic.values;
74 void ProgramData::uniform(Tag tag, DataType type, unsigned array_size, const void *value)
76 if(!validate_tag(tag))
79 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
80 if(i==uniforms.end() || i->tag!=tag)
82 if(uniforms.size()>=MASK_BITS)
83 throw too_many_uniforms(tag.str());
88 tu.array_size = array_size;
89 tu.data_offset = uniform_data.size();
90 tu.data_size = array_size*get_type_size(type);
91 i = uniforms.insert(i, tu);
92 uniform_data.resize(tu.data_offset+tu.data_size);
96 else if(type!=i->type)
97 throw invalid_operation("ProgramData::uniform");
98 else if(array_size>i->array_size)
100 unsigned add_bytes = (array_size-i->array_size)*get_type_size(type);
101 uniform_data.insert(uniform_data.begin()+i->data_offset+i->data_size, add_bytes, 0);
102 for(TaggedUniform &u: uniforms)
103 if(u.data_offset>i->data_offset)
104 u.data_offset += add_bytes;
105 i->array_size = array_size;
106 i->data_size = array_size*get_type_size(type);
109 const char *val_begin = static_cast<const char *>(value);
110 const char *val_end = val_begin+array_size*get_type_size(type);
111 char *store_begin = uniform_data.data()+i->data_offset;
112 copy(val_begin, val_end, store_begin);
114 mark_dirty(1<<(i-uniforms.begin()));
117 bool ProgramData::validate_tag(Tag tag) const
125 const ReflectData::UniformInfo &info = tied_program->get_uniform_info(tag);
126 if(is_image(info.type))
127 throw invalid_operation("ProgramData::uniform");
132 catch(const exception &e)
134 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
140 void ProgramData::mark_dirty(Mask bits)
147 void ProgramData::uniform(Tag tag, int v)
149 uniform(tag, INT, 1, &v);
152 void ProgramData::uniform(Tag tag, float v)
154 uniform(tag, FLOAT, 1, &v);
157 void ProgramData::uniform(Tag tag, int v0, int v1)
159 int va[2] = { v0, v1 };
163 void ProgramData::uniform(Tag tag, float v0, float v1)
165 float va[2] = { v0, v1 };
169 void ProgramData::uniform2(Tag tag, const int *v)
171 uniform(tag, INT_VEC2, 1, v);
174 void ProgramData::uniform2(Tag tag, const float *v)
176 uniform(tag, FLOAT_VEC2, 1, v);
179 void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
181 int va[3] = { v0, v1, v2 };
185 void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
187 float va[3] = { v0, v1, v2 };
191 void ProgramData::uniform3(Tag tag, const int *v)
193 uniform(tag, INT_VEC3, 1, v);
196 void ProgramData::uniform3(Tag tag, const float *v)
198 uniform(tag, FLOAT_VEC3, 1, v);
201 void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
203 int va[4] = { v0, v1, v2, v3 };
207 void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
209 float va[4] = { v0, v1, v2, v3 };
213 void ProgramData::uniform(Tag tag, const Color &c)
215 uniform(tag, c.r, c.g, c.b, c.a);
218 void ProgramData::uniform4(Tag tag, const int *v)
220 uniform(tag, INT_VEC4, 1, v);
223 void ProgramData::uniform4(Tag tag, const float *v)
225 uniform(tag, FLOAT_VEC4, 1, v);
228 void ProgramData::uniform_matrix2(Tag tag, const float *v)
230 uniform(tag, FLOAT_MAT2, 1, v);
233 void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
235 uniform(tag, FLOAT_MAT3x2, 1, v);
238 void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
240 uniform(tag, FLOAT_MAT4x2, 1, v);
243 void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
245 uniform(tag, FLOAT_MAT2x3, 1, v);
248 void ProgramData::uniform_matrix3(Tag tag, const float *v)
250 uniform(tag, FLOAT_MAT3, 1, v);
253 void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
255 uniform(tag, FLOAT_MAT4x3, 1, v);
258 void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
260 uniform(tag, FLOAT_MAT2x4, 1, v);
263 void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
265 uniform(tag, FLOAT_MAT3x4, 1, v);
268 void ProgramData::uniform(Tag tag, const Matrix &m)
270 uniform_matrix4(tag, m.data());
273 void ProgramData::uniform_matrix4(Tag tag, const float *v)
275 uniform(tag, FLOAT_MAT4, 1, v);
278 void ProgramData::uniform_array(Tag tag, unsigned n, const int *v)
280 uniform(tag, INT, n, v);
283 void ProgramData::uniform_array(Tag tag, unsigned n, const float *v)
285 uniform(tag, FLOAT, n, v);
288 void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
290 uniform(tag, INT, n, v);
293 void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
295 uniform(tag, FLOAT, n, v);
298 void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
300 uniform(tag, INT_VEC2, n, v);
303 void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
305 uniform(tag, FLOAT_VEC2, n, v);
308 void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
310 uniform(tag, INT_VEC3, n, v);
313 void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
315 uniform(tag, FLOAT_VEC3, n, v);
318 void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
320 uniform(tag, INT_VEC4, n, v);
323 void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
325 uniform(tag, FLOAT_VEC4, n, v);
328 void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
330 uniform(tag, FLOAT_MAT2, n, v);
333 void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
335 uniform(tag, FLOAT_MAT3x2, n, v);
338 void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
340 uniform(tag, FLOAT_MAT4x2, n, v);
343 void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
345 uniform(tag, FLOAT_MAT2x3, n, v);
348 void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
350 uniform(tag, FLOAT_MAT3, n, v);
353 void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
355 uniform(tag, FLOAT_MAT4x3, n, v);
358 void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
360 uniform(tag, FLOAT_MAT2x4, n, v);
363 void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
365 uniform(tag, FLOAT_MAT3x4, n, v);
368 void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
370 uniform(tag, FLOAT_MAT4, n, v);
373 void ProgramData::remove_uniform(Tag tag)
375 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
376 if(i==uniforms.end() || i->tag!=tag)
379 uniform_data.erase(uniform_data.begin()+i->data_offset, uniform_data.begin()+i->data_offset+i->data_size);
380 for(TaggedUniform &u: uniforms)
381 if(u.data_offset>i->data_offset)
382 u.data_offset -= i->data_size;
385 mark_dirty(ALL_ONES);
388 vector<Tag> ProgramData::get_uniform_tags() const
391 tags.reserve(uniforms.size());
392 for(const TaggedUniform &u: uniforms)
393 tags.push_back(u.tag);
397 void ProgramData::copy_uniform(const ProgramData &source, Tag tag)
399 int i = source.find_uniform_index(tag);
401 throw key_error(tag);
402 const TaggedUniform &tu = source.uniforms[i];
403 uniform(tag, tu.type, tu.array_size, source.uniform_data.data()+tu.data_offset);
406 int ProgramData::find_uniform_index(Tag tag) const
408 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
409 return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
412 vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
414 ReflectData::LayoutHash prog_hash = prog.get_uniform_layout_hash();
415 auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
416 if(i!=programs.end() && i->prog_hash==prog_hash)
419 const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
420 unsigned index = i-programs.begin();
421 programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash));
423 /* Block indices may change if new shared blocks need to be inserted. Store
424 the hashes so they can be matched up later. */
425 vector<ReflectData::LayoutHash> block_hashes;
426 block_hashes.reserve(programs.size());
427 for(const ProgramBlock &b: programs)
428 block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
430 for(unsigned j=0; j<block_infos.size(); ++j)
432 const ReflectData::UniformBlockInfo &info = block_infos[j];
433 block_hashes[index+1+j] = info.layout_hash;
434 programs[index+1+j].bind_point = info.bind_point;
436 auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
437 if(k==blocks.end() || k->block_hash!=info.layout_hash)
439 k = blocks.insert(k, SharedBlock(info.layout_hash));
440 update_block_uniform_indices(*k, info);
444 /* Reassign shared block indices from the stored hashes. */
445 for(unsigned j=0; j<programs.size(); ++j)
447 unsigned hash = block_hashes[j];
450 auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
451 programs[j].block_index = k-blocks.begin();
454 programs[j].block_index = -1;
457 return programs.begin()+index;
460 void ProgramData::update_block_uniform_indices(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
462 uint8_t *indices = block.indices.values;
463 if(info.uniforms.size()>16)
465 if(block.indices.type_flag==0xFD)
467 block.indices.dynamic.values = new uint8_t[info.uniforms.size()];
468 block.indices.type_flag = 0xFE;
470 indices = block.indices.dynamic.values;
473 bool any_missing = false;
476 for(unsigned i=0; i<info.uniforms.size(); ++i)
478 int j = find_uniform_index(info.uniforms[i]->tag);
482 if(static_cast<unsigned>(j)<MASK_BITS)
492 if(block.used && any_missing && info.bind_point>=0)
495 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
497 throw incomplete_uniform_block(info.name);
501 block.dirty = block.used;
503 if(block.used && !block.block)
505 block.block = new UniformBlock(info);
506 if(info.bind_point>=0)
510 buffer = new Buffer();
513 if(!debug_name.empty())
514 buffer->set_debug_name(debug_name);
518 block.block->use_buffer(buffer, last_buffer_block);
519 last_buffer_block = block.block;
524 void ProgramData::update_block(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
526 const uint8_t *indices = block.get_uniform_indices();
527 for(unsigned i=0; i<info.uniforms.size(); ++i)
529 if(is_image(info.uniforms[i]->type))
530 ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData
531 else if(indices[i]!=0xFF)
533 const TaggedUniform &tu = uniforms[indices[i]];
534 block.block->store(*info.uniforms[i], tu.array_size, uniform_data.data()+tu.data_offset);
539 vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
541 UniformBlock *old_last_block = last_buffer_block;
542 auto prog_begin = get_program(prog);
544 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
545 Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
546 if(affected|prog_begin->masks.dirty)
548 /* If the global dirty flag affects this program, add it to per-block and
549 per-program dirty flags and clear the global flag. A previously unseen
550 program will cause this to happen if there's any dirty uniforms. */
553 for(SharedBlock &b: blocks)
554 b.dirty |= (dirty&b.used) | force_dirty;
555 for(ProgramBlock &b: programs)
557 b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
561 const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
563 if(prog_begin->masks.dirty==ALL_ONES)
565 /* The set of uniforms has changed since this program was last used.
566 Refresh uniform indices within the program's blocks. */
567 prog_begin->masks.used = 0;
568 auto j = prog_begin+1;
569 for(const ReflectData::UniformBlockInfo &b: block_infos)
571 SharedBlock &shared = blocks[j->block_index];
572 if(shared.dirty==ALL_ONES)
573 update_block_uniform_indices(shared, b);
574 prog_begin->masks.used |= shared.used;
575 j->block = (shared.used ? shared.block : 0);
580 // Update the contents of all dirty blocks.
581 bool buffered_blocks_updated = false;
582 auto j = prog_begin+1;
583 for(const ReflectData::UniformBlockInfo &b: block_infos)
585 SharedBlock &shared = blocks[j->block_index];
588 update_block(shared, b);
590 buffered_blocks_updated |= (j->bind_point>=0);
595 prog_begin->masks.dirty = 0;
597 if(last_buffer_block!=old_last_block)
599 unsigned required_size = last_buffer_block->get_required_buffer_size();
600 if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
602 if(buffer->get_size()>0)
605 buffer = new Buffer();
606 last_buffer_block->change_buffer(buffer);
609 if(!debug_name.empty())
610 buffer->set_debug_name(debug_name);
614 buffer->storage(required_size);
622 void ProgramData::apply(const Program &prog, PipelineState &state) const
624 auto prog_begin = prepare_program(prog);
625 ReflectData::LayoutHash prog_hash = prog_begin->prog_hash;
626 for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
629 state.set_uniform_block(i->bind_point, i->block);
635 void ProgramData::set_debug_name(const string &name)
640 buffer->set_debug_name(name);
647 ProgramData::SharedBlock::SharedBlock(ReflectData::LayoutHash h):
653 indices.type_flag = 0xFD;
656 const uint8_t *ProgramData::SharedBlock::get_uniform_indices() const
658 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
662 ProgramData::ProgramBlock::ProgramBlock(ReflectData::LayoutHash h):
667 masks.used = ALL_ONES;
668 masks.dirty = ALL_ONES;
672 ProgramData::Loader::Loader(ProgramData &pd):
673 DataFile::ObjectLoader<ProgramData>(pd)
675 add("uniform", &Loader::uniform1i);
676 add("uniform1i", &Loader::uniform1i);
677 add("uniform", &Loader::uniform1f);
678 add("uniform1f", &Loader::uniform1f);
679 add("uniform", &Loader::uniform2i);
680 add("uniform2i", &Loader::uniform2i);
681 add("uniform", &Loader::uniform2f);
682 add("uniform2f", &Loader::uniform2f);
683 add("uniform", &Loader::uniform3i);
684 add("uniform3i", &Loader::uniform3i);
685 add("uniform", &Loader::uniform3f);
686 add("uniform3f", &Loader::uniform3f);
687 add("uniform", &Loader::uniform4i);
688 add("uniform4i", &Loader::uniform4i);
689 add("uniform", &Loader::uniform4f);
690 add("uniform4f", &Loader::uniform4f);
691 add("uniform1i_array", &Loader::uniform1i_array);
692 add("uniform1f_array", &Loader::uniform1f_array);
693 add("uniform2f_array", &Loader::uniform2f_array);
694 add("uniform3f_array", &Loader::uniform3f_array);
695 add("uniform4f_array", &Loader::uniform4f_array);
696 add("uniform_array", &Loader::uniform_array);
699 void ProgramData::Loader::uniform1i(const string &n, int v)
704 void ProgramData::Loader::uniform1f(const string &n, float v)
709 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
711 obj.uniform(n, v0, v1);
714 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
716 obj.uniform(n, v0, v1);
719 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
721 obj.uniform(n, v0, v1, v2);
724 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
726 obj.uniform(n, v0, v1, v2);
729 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
731 obj.uniform(n, v0, v1, v2, v3);
734 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
736 obj.uniform(n, v0, v1, v2, v3);
739 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
741 ArrayLoader ldr(t, e);
743 unsigned size = ldr.get_size();
745 throw logic_error("empty uniform array");
747 DataType type = ldr.get_data_type();
748 unsigned elem_size = ldr.get_element_size();
751 const int *data = reinterpret_cast<const int *>(ldr.get_data());
753 obj.uniform1_array(n, size, data);
754 else if(elem_size==2)
755 obj.uniform2_array(n, size, data);
756 else if(elem_size==3)
757 obj.uniform3_array(n, size, data);
758 else if(elem_size==4)
759 obj.uniform4_array(n, size, data);
761 throw logic_error("unsupported combination of array type and element size");
765 const float *data = reinterpret_cast<const float *>(ldr.get_data());
767 obj.uniform1_array(n, size, data);
768 else if(elem_size==2)
769 obj.uniform2_array(n, size, data);
770 else if(elem_size==3)
771 obj.uniform3_array(n, size, data);
772 else if(elem_size==4)
773 obj.uniform4_array(n, size, data);
775 throw logic_error("unsupported combination of array type and element size");
778 throw logic_error("unsupported array type");
781 void ProgramData::Loader::uniform1i_array(const string &n)
783 uniform_array_(n, INT, 1);
786 void ProgramData::Loader::uniform1f_array(const string &n)
788 uniform_array_(n, FLOAT, 1);
791 void ProgramData::Loader::uniform2i_array(const string &n)
793 uniform_array_(n, INT, 2);
796 void ProgramData::Loader::uniform2f_array(const string &n)
798 uniform_array_(n, FLOAT, 2);
801 void ProgramData::Loader::uniform3i_array(const string &n)
803 uniform_array_(n, INT, 3);
806 void ProgramData::Loader::uniform3f_array(const string &n)
808 uniform_array_(n, FLOAT, 3);
811 void ProgramData::Loader::uniform4i_array(const string &n)
813 uniform_array_(n, INT, 4);
816 void ProgramData::Loader::uniform4f_array(const string &n)
818 uniform_array_(n, FLOAT, 4);
821 void ProgramData::Loader::uniform_array(const string &n)
823 uniform_array_(n, static_cast<DataType>(0), 0);
827 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
831 add("uniform", &ArrayLoader::uniform1i);
832 add("uniform1i", &ArrayLoader::uniform1i);
833 add("uniform", &ArrayLoader::uniform1f);
834 add("uniform1f", &ArrayLoader::uniform1f);
835 add("uniform", &ArrayLoader::uniform2f);
836 add("uniform2f", &ArrayLoader::uniform2f);
837 add("uniform", &ArrayLoader::uniform3f);
838 add("uniform3f", &ArrayLoader::uniform3f);
839 add("uniform", &ArrayLoader::uniform4f);
840 add("uniform4f", &ArrayLoader::uniform4f);
843 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
845 if(element_size && (t!=type || e!=element_size))
846 throw logic_error("heterogeneous array contents");
854 const char *cv = reinterpret_cast<const char *>(v);
855 data.insert(data.end(), cv, cv+element_size*4);
858 void ProgramData::ArrayLoader::uniform1i(int v)
863 void ProgramData::ArrayLoader::uniform1f(float v)
865 uniform(FLOAT, 1, &v);
868 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
870 int va[2] = { v0, v1 };
874 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
876 float va[2] = { v0, v1 };
877 uniform(FLOAT, 2, va);
880 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
882 int va[3] = { v0, v1, v2 };
886 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
888 float va[3] = { v0, v1, v2 };
889 uniform(FLOAT, 3, va);
892 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
894 int va[4] = { v0, v1, v2, v3 };
898 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
900 float va[4] = { v0, v1, v2, v3 };
901 uniform(FLOAT, 4, va);