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):
27 // Blocks are intentionally left uncopied
28 ProgramData::ProgramData(const ProgramData &other):
29 tied_program(other.tied_program),
30 uniforms(other.uniforms),
31 uniform_data(other.uniform_data),
32 generation(other.generation),
38 ProgramData::ProgramData(const ProgramData &other, const Program *p):
46 for(const TaggedUniform &u: other.uniforms)
50 uniforms = other.uniforms;
51 uniform_data = other.uniform_data;
54 ProgramData &ProgramData::operator=(const ProgramData &other)
56 tied_program = other.tied_program;
58 uniforms = other.uniforms;
59 uniform_data = other.uniform_data;
61 for(SharedBlock &b: blocks)
66 last_buffer_block = 0;
73 ProgramData::~ProgramData()
75 for(SharedBlock &b: blocks)
77 if(b.indices.type_flag==0xFE)
78 delete[] b.indices.dynamic.values;
84 void ProgramData::uniform(Tag tag, DataType type, unsigned array_size, const void *value)
86 if(!validate_tag(tag))
89 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
90 if(i==uniforms.end() || i->tag!=tag)
92 if(uniforms.size()>=MASK_BITS)
93 throw too_many_uniforms(tag.str());
98 tu.array_size = array_size;
99 tu.data_offset = uniform_data.size();
100 tu.data_size = array_size*get_type_size(type);
101 i = uniforms.insert(i, tu);
102 uniform_data.resize(tu.data_offset+tu.data_size);
104 mark_dirty(ALL_ONES);
106 else if(type!=i->type)
107 throw invalid_operation("ProgramData::uniform");
108 else if(array_size>i->array_size)
110 unsigned add_bytes = (array_size-i->array_size)*get_type_size(type);
111 uniform_data.insert(uniform_data.begin()+i->data_offset+i->data_size, add_bytes, 0);
112 for(TaggedUniform &u: uniforms)
113 if(u.data_offset>i->data_offset)
114 u.data_offset += add_bytes;
115 i->array_size = array_size;
116 i->data_size = array_size*get_type_size(type);
119 const char *val_begin = static_cast<const char *>(value);
120 const char *val_end = val_begin+array_size*get_type_size(type);
121 char *store_begin = uniform_data.data()+i->data_offset;
122 copy(val_begin, val_end, store_begin);
124 mark_dirty(1<<(i-uniforms.begin()));
127 bool ProgramData::validate_tag(Tag tag) const
135 const ReflectData::UniformInfo &info = tied_program->get_uniform_info(tag);
136 if(is_image(info.type))
137 throw invalid_operation("ProgramData::uniform");
142 catch(const exception &e)
144 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
150 void ProgramData::mark_dirty(Mask bits)
157 void ProgramData::uniform(Tag tag, int v)
159 uniform(tag, INT, 1, &v);
162 void ProgramData::uniform(Tag tag, float v)
164 uniform(tag, FLOAT, 1, &v);
167 void ProgramData::uniform(Tag tag, int v0, int v1)
169 int va[2] = { v0, v1 };
173 void ProgramData::uniform(Tag tag, float v0, float v1)
175 float va[2] = { v0, v1 };
179 void ProgramData::uniform2(Tag tag, const int *v)
181 uniform(tag, INT_VEC2, 1, v);
184 void ProgramData::uniform2(Tag tag, const float *v)
186 uniform(tag, FLOAT_VEC2, 1, v);
189 void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
191 int va[3] = { v0, v1, v2 };
195 void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
197 float va[3] = { v0, v1, v2 };
201 void ProgramData::uniform3(Tag tag, const int *v)
203 uniform(tag, INT_VEC3, 1, v);
206 void ProgramData::uniform3(Tag tag, const float *v)
208 uniform(tag, FLOAT_VEC3, 1, v);
211 void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
213 int va[4] = { v0, v1, v2, v3 };
217 void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
219 float va[4] = { v0, v1, v2, v3 };
223 void ProgramData::uniform(Tag tag, const Color &c)
225 uniform(tag, c.r, c.g, c.b, c.a);
228 void ProgramData::uniform4(Tag tag, const int *v)
230 uniform(tag, INT_VEC4, 1, v);
233 void ProgramData::uniform4(Tag tag, const float *v)
235 uniform(tag, FLOAT_VEC4, 1, v);
238 void ProgramData::uniform_matrix2(Tag tag, const float *v)
240 uniform(tag, FLOAT_MAT2, 1, v);
243 void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
245 uniform(tag, FLOAT_MAT3x2, 1, v);
248 void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
250 uniform(tag, FLOAT_MAT4x2, 1, v);
253 void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
255 uniform(tag, FLOAT_MAT2x3, 1, v);
258 void ProgramData::uniform_matrix3(Tag tag, const float *v)
260 uniform(tag, FLOAT_MAT3, 1, v);
263 void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
265 uniform(tag, FLOAT_MAT4x3, 1, v);
268 void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
270 uniform(tag, FLOAT_MAT2x4, 1, v);
273 void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
275 uniform(tag, FLOAT_MAT3x4, 1, v);
278 void ProgramData::uniform(Tag tag, const Matrix &m)
280 uniform_matrix4(tag, m.data());
283 void ProgramData::uniform_matrix4(Tag tag, const float *v)
285 uniform(tag, FLOAT_MAT4, 1, v);
288 void ProgramData::uniform_array(Tag tag, unsigned n, const int *v)
290 uniform(tag, INT, n, v);
293 void ProgramData::uniform_array(Tag tag, unsigned n, const float *v)
295 uniform(tag, FLOAT, n, v);
298 void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
300 uniform(tag, INT, n, v);
303 void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
305 uniform(tag, FLOAT, n, v);
308 void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
310 uniform(tag, INT_VEC2, n, v);
313 void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
315 uniform(tag, FLOAT_VEC2, n, v);
318 void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
320 uniform(tag, INT_VEC3, n, v);
323 void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
325 uniform(tag, FLOAT_VEC3, n, v);
328 void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
330 uniform(tag, INT_VEC4, n, v);
333 void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
335 uniform(tag, FLOAT_VEC4, n, v);
338 void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
340 uniform(tag, FLOAT_MAT2, n, v);
343 void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
345 uniform(tag, FLOAT_MAT3x2, n, v);
348 void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
350 uniform(tag, FLOAT_MAT4x2, n, v);
353 void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
355 uniform(tag, FLOAT_MAT2x3, n, v);
358 void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
360 uniform(tag, FLOAT_MAT3, n, v);
363 void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
365 uniform(tag, FLOAT_MAT4x3, n, v);
368 void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
370 uniform(tag, FLOAT_MAT2x4, n, v);
373 void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
375 uniform(tag, FLOAT_MAT3x4, n, v);
378 void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
380 uniform(tag, FLOAT_MAT4, n, v);
383 void ProgramData::remove_uniform(Tag tag)
385 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
386 if(i==uniforms.end() || i->tag!=tag)
389 uniform_data.erase(uniform_data.begin()+i->data_offset, uniform_data.begin()+i->data_offset+i->data_size);
390 for(TaggedUniform &u: uniforms)
391 if(u.data_offset>i->data_offset)
392 u.data_offset -= i->data_size;
395 mark_dirty(ALL_ONES);
398 vector<Tag> ProgramData::get_uniform_tags() const
401 tags.reserve(uniforms.size());
402 for(const TaggedUniform &u: uniforms)
403 tags.push_back(u.tag);
407 void ProgramData::copy_uniform(const ProgramData &source, Tag tag)
409 int i = source.find_uniform_index(tag);
411 throw key_error(tag);
412 const TaggedUniform &tu = source.uniforms[i];
413 uniform(tag, tu.type, tu.array_size, source.uniform_data.data()+tu.data_offset);
416 int ProgramData::find_uniform_index(Tag tag) const
418 auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
419 return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
422 vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
424 ReflectData::LayoutHash prog_hash = prog.get_uniform_layout_hash();
425 auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
426 if(i!=programs.end() && i->prog_hash==prog_hash)
429 const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
430 unsigned index = i-programs.begin();
431 programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash));
433 /* Block indices may change if new shared blocks need to be inserted. Store
434 the hashes so they can be matched up later. */
435 vector<ReflectData::LayoutHash> block_hashes;
436 block_hashes.reserve(programs.size());
437 for(const ProgramBlock &b: programs)
438 block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
440 for(unsigned j=0; j<block_infos.size(); ++j)
442 const ReflectData::UniformBlockInfo &info = block_infos[j];
443 block_hashes[index+1+j] = info.layout_hash;
444 programs[index+1+j].bind_point = info.bind_point;
446 auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
447 if(k==blocks.end() || k->block_hash!=info.layout_hash)
449 k = blocks.insert(k, SharedBlock(info.layout_hash));
450 update_block_uniform_indices(*k, info);
454 /* Reassign shared block indices from the stored hashes. */
455 for(unsigned j=0; j<programs.size(); ++j)
457 unsigned hash = block_hashes[j];
460 auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
461 programs[j].block_index = k-blocks.begin();
464 programs[j].block_index = -1;
467 return programs.begin()+index;
470 void ProgramData::update_block_uniform_indices(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
472 uint8_t *indices = block.indices.values;
473 if(info.uniforms.size()>16)
475 if(block.indices.type_flag==0xFD)
477 block.indices.dynamic.values = new uint8_t[info.uniforms.size()];
478 block.indices.type_flag = 0xFE;
480 indices = block.indices.dynamic.values;
483 bool any_missing = false;
486 for(unsigned i=0; i<info.uniforms.size(); ++i)
488 int j = find_uniform_index(info.uniforms[i]->tag);
492 if(static_cast<unsigned>(j)<MASK_BITS)
502 if(block.used && any_missing && info.bind_point>=0)
505 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
507 throw incomplete_uniform_block(info.name);
511 block.dirty = block.used;
513 if(block.used && !block.block)
515 block.block = new UniformBlock(info);
516 if(info.bind_point>=0)
520 buffer = new Buffer();
523 if(!debug_name.empty())
524 buffer->set_debug_name(debug_name);
528 block.block->use_buffer(buffer, last_buffer_block);
529 last_buffer_block = block.block;
534 void ProgramData::update_block(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
536 const uint8_t *indices = block.get_uniform_indices();
537 for(unsigned i=0; i<info.uniforms.size(); ++i)
539 if(is_image(info.uniforms[i]->type))
540 ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData
541 else if(indices[i]!=0xFF)
543 const TaggedUniform &tu = uniforms[indices[i]];
544 block.block->store(*info.uniforms[i], tu.array_size, uniform_data.data()+tu.data_offset);
549 vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
551 UniformBlock *old_last_block = last_buffer_block;
552 auto prog_begin = get_program(prog);
554 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
555 Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
556 if(affected|prog_begin->masks.dirty)
558 /* If the global dirty flag affects this program, add it to per-block and
559 per-program dirty flags and clear the global flag. A previously unseen
560 program will cause this to happen if there's any dirty uniforms. */
563 for(SharedBlock &b: blocks)
564 b.dirty |= (dirty&b.used) | force_dirty;
565 for(ProgramBlock &b: programs)
567 b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
571 const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
573 if(prog_begin->masks.dirty==ALL_ONES)
575 /* The set of uniforms has changed since this program was last used.
576 Refresh uniform indices within the program's blocks. */
577 prog_begin->masks.used = 0;
578 auto j = prog_begin+1;
579 for(const ReflectData::UniformBlockInfo &b: block_infos)
581 SharedBlock &shared = blocks[j->block_index];
582 if(shared.dirty==ALL_ONES)
583 update_block_uniform_indices(shared, b);
584 prog_begin->masks.used |= shared.used;
585 j->block = (shared.used ? shared.block : 0);
590 // Update the contents of all dirty blocks.
591 bool buffered_blocks_updated = false;
592 auto j = prog_begin+1;
593 for(const ReflectData::UniformBlockInfo &b: block_infos)
595 SharedBlock &shared = blocks[j->block_index];
598 update_block(shared, b);
600 buffered_blocks_updated |= (j->bind_point>=0);
605 prog_begin->masks.dirty = 0;
607 if(last_buffer_block!=old_last_block)
609 unsigned required_size = last_buffer_block->get_required_buffer_size();
610 if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
612 if(buffer->get_size()>0)
615 buffer = new Buffer();
616 last_buffer_block->change_buffer(buffer);
619 if(!debug_name.empty())
620 buffer->set_debug_name(debug_name);
624 buffer->storage(required_size);
632 void ProgramData::apply(const Program &prog, PipelineState &state) const
634 auto prog_begin = prepare_program(prog);
635 ReflectData::LayoutHash prog_hash = prog_begin->prog_hash;
636 for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
639 state.set_uniform_block(i->bind_point, i->block);
645 void ProgramData::set_debug_name(const string &name)
650 buffer->set_debug_name(name);
657 ProgramData::SharedBlock::SharedBlock(ReflectData::LayoutHash h):
663 indices.type_flag = 0xFD;
666 const uint8_t *ProgramData::SharedBlock::get_uniform_indices() const
668 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
672 ProgramData::ProgramBlock::ProgramBlock(ReflectData::LayoutHash h):
677 masks.used = ALL_ONES;
678 masks.dirty = ALL_ONES;
682 ProgramData::Loader::Loader(ProgramData &pd):
683 DataFile::ObjectLoader<ProgramData>(pd)
685 add("uniform", &Loader::uniform1i);
686 add("uniform1i", &Loader::uniform1i);
687 add("uniform", &Loader::uniform1f);
688 add("uniform1f", &Loader::uniform1f);
689 add("uniform", &Loader::uniform2i);
690 add("uniform2i", &Loader::uniform2i);
691 add("uniform", &Loader::uniform2f);
692 add("uniform2f", &Loader::uniform2f);
693 add("uniform", &Loader::uniform3i);
694 add("uniform3i", &Loader::uniform3i);
695 add("uniform", &Loader::uniform3f);
696 add("uniform3f", &Loader::uniform3f);
697 add("uniform", &Loader::uniform4i);
698 add("uniform4i", &Loader::uniform4i);
699 add("uniform", &Loader::uniform4f);
700 add("uniform4f", &Loader::uniform4f);
701 add("uniform1i_array", &Loader::uniform1i_array);
702 add("uniform1f_array", &Loader::uniform1f_array);
703 add("uniform2f_array", &Loader::uniform2f_array);
704 add("uniform3f_array", &Loader::uniform3f_array);
705 add("uniform4f_array", &Loader::uniform4f_array);
706 add("uniform_array", &Loader::uniform_array);
709 void ProgramData::Loader::uniform1i(const string &n, int v)
714 void ProgramData::Loader::uniform1f(const string &n, float v)
719 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
721 obj.uniform(n, v0, v1);
724 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
726 obj.uniform(n, v0, v1);
729 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
731 obj.uniform(n, v0, v1, v2);
734 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
736 obj.uniform(n, v0, v1, v2);
739 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
741 obj.uniform(n, v0, v1, v2, v3);
744 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
746 obj.uniform(n, v0, v1, v2, v3);
749 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
751 ArrayLoader ldr(t, e);
753 unsigned size = ldr.get_size();
755 throw logic_error("empty uniform array");
757 DataType type = ldr.get_data_type();
758 unsigned elem_size = ldr.get_element_size();
761 const int *data = reinterpret_cast<const int *>(ldr.get_data());
763 obj.uniform1_array(n, size, data);
764 else if(elem_size==2)
765 obj.uniform2_array(n, size, data);
766 else if(elem_size==3)
767 obj.uniform3_array(n, size, data);
768 else if(elem_size==4)
769 obj.uniform4_array(n, size, data);
771 throw logic_error("unsupported combination of array type and element size");
775 const float *data = reinterpret_cast<const float *>(ldr.get_data());
777 obj.uniform1_array(n, size, data);
778 else if(elem_size==2)
779 obj.uniform2_array(n, size, data);
780 else if(elem_size==3)
781 obj.uniform3_array(n, size, data);
782 else if(elem_size==4)
783 obj.uniform4_array(n, size, data);
785 throw logic_error("unsupported combination of array type and element size");
788 throw logic_error("unsupported array type");
791 void ProgramData::Loader::uniform1i_array(const string &n)
793 uniform_array_(n, INT, 1);
796 void ProgramData::Loader::uniform1f_array(const string &n)
798 uniform_array_(n, FLOAT, 1);
801 void ProgramData::Loader::uniform2i_array(const string &n)
803 uniform_array_(n, INT, 2);
806 void ProgramData::Loader::uniform2f_array(const string &n)
808 uniform_array_(n, FLOAT, 2);
811 void ProgramData::Loader::uniform3i_array(const string &n)
813 uniform_array_(n, INT, 3);
816 void ProgramData::Loader::uniform3f_array(const string &n)
818 uniform_array_(n, FLOAT, 3);
821 void ProgramData::Loader::uniform4i_array(const string &n)
823 uniform_array_(n, INT, 4);
826 void ProgramData::Loader::uniform4f_array(const string &n)
828 uniform_array_(n, FLOAT, 4);
831 void ProgramData::Loader::uniform_array(const string &n)
833 uniform_array_(n, static_cast<DataType>(0), 0);
837 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
841 add("uniform", &ArrayLoader::uniform1i);
842 add("uniform1i", &ArrayLoader::uniform1i);
843 add("uniform", &ArrayLoader::uniform1f);
844 add("uniform1f", &ArrayLoader::uniform1f);
845 add("uniform", &ArrayLoader::uniform2f);
846 add("uniform2f", &ArrayLoader::uniform2f);
847 add("uniform", &ArrayLoader::uniform3f);
848 add("uniform3f", &ArrayLoader::uniform3f);
849 add("uniform", &ArrayLoader::uniform4f);
850 add("uniform4f", &ArrayLoader::uniform4f);
853 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
855 if(element_size && (t!=type || e!=element_size))
856 throw logic_error("heterogeneous array contents");
864 const char *cv = reinterpret_cast<const char *>(v);
865 data.insert(data.end(), cv, cv+element_size*4);
868 void ProgramData::ArrayLoader::uniform1i(int v)
873 void ProgramData::ArrayLoader::uniform1f(float v)
875 uniform(FLOAT, 1, &v);
878 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
880 int va[2] = { v0, v1 };
884 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
886 float va[2] = { v0, v1 };
887 uniform(FLOAT, 2, va);
890 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
892 int va[3] = { v0, v1, v2 };
896 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
898 float va[3] = { v0, v1, v2 };
899 uniform(FLOAT, 3, va);
902 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
904 int va[4] = { v0, v1, v2, v3 };
908 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
910 float va[4] = { v0, v1, v2, v3 };
911 uniform(FLOAT, 4, va);