1 #include <msp/core/maputils.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
8 #include "programdata.h"
10 #include "uniformblock.h"
18 ProgramData::ProgramData(const Program *p):
25 // Blocks are intentionally left uncopied
26 ProgramData::ProgramData(const ProgramData &other):
28 uniforms(other.uniforms),
33 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
34 i->value = i->value->clone();
37 ProgramData &ProgramData::operator=(const ProgramData &other)
39 tied_program = other.tied_program;
41 uniforms = other.uniforms;
42 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
43 i->value = i->value->clone();
45 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
46 delete i->second.block;
56 ProgramData::~ProgramData()
58 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
60 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
62 if(i->second.indices.type_flag==0xFE)
63 delete i->second.indices.dynamic.values;
64 delete i->second.block;
69 void ProgramData::uniform(const string &name, Uniform *uni)
74 tied_program->get_uniform_info(name);
75 else if(name[name.size()-1]==']')
76 throw invalid_argument("ProgramData::uniform");
84 int i = find_uniform_index(name);
87 uniforms[i].replace_value(uni);
89 if(static_cast<unsigned>(i)<MASK_BITS)
91 else // Force a full update if the mask isn't wide enough
97 vector<NamedUniform>::iterator j = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
102 uniforms.insert(j, nu);
107 void ProgramData::uniform(const string &name, const Uniform &u)
109 uniform(name, u.clone());
112 void ProgramData::uniform(const string &name, int v)
114 uniform(name, new Uniform1i(v));
117 void ProgramData::uniform(const string &name, float v)
119 uniform(name, new Uniform1f(v));
122 void ProgramData::uniform(const string &name, int v0, int v1)
124 int va[2] = { v0, v1 };
128 void ProgramData::uniform(const string &name, float v0, float v1)
130 float va[2] = { v0, v1 };
134 void ProgramData::uniform2(const string &name, const int *v)
136 uniform(name, new Uniform2i(v));
139 void ProgramData::uniform2(const string &name, const float *v)
141 uniform(name, new Uniform2f(v));
144 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
146 int va[3] = { v0, v1, v2 };
150 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
152 float va[3] = { v0, v1, v2 };
156 void ProgramData::uniform(const string &name, const Vector3 &v)
158 uniform(name, v.x, v.y, v.z);
161 void ProgramData::uniform3(const string &name, const int *v)
163 uniform(name, new Uniform3i(v));
166 void ProgramData::uniform3(const string &name, const float *v)
168 uniform(name, new Uniform3f(v));
171 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
173 int va[4] = { v0, v1, v2, v3 };
177 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
179 float va[4] = { v0, v1, v2, v3 };
183 void ProgramData::uniform(const string &name, const Vector4 &v)
185 uniform(name, v.x, v.y, v.z, v.w);
188 void ProgramData::uniform(const string &name, const Color &c)
190 uniform(name, c.r, c.g, c.b, c.a);
193 void ProgramData::uniform4(const string &name, const int *v)
195 uniform(name, new Uniform4i(v));
198 void ProgramData::uniform4(const string &name, const float *v)
200 uniform(name, new Uniform4f(v));
203 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
205 uniform_matrix2(name, &m(0, 0));
208 void ProgramData::uniform_matrix2(const string &name, const float *v)
210 uniform(name, new UniformMatrix2x2f(v));
213 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 3> &m)
215 uniform_matrix3x2(name, &m(0, 0));
218 void ProgramData::uniform_matrix3x2(const string &name, const float *v)
220 uniform(name, new UniformMatrix3x2f(v));
223 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 4> &m)
225 uniform_matrix4x2(name, &m(0, 0));
228 void ProgramData::uniform_matrix4x2(const string &name, const float *v)
230 uniform(name, new UniformMatrix4x2f(v));
233 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 2> &m)
235 uniform_matrix2x3(name, &m(0, 0));
238 void ProgramData::uniform_matrix2x3(const string &name, const float *v)
240 uniform(name, new UniformMatrix2x3f(v));
243 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
245 uniform_matrix3(name, &m(0, 0));
248 void ProgramData::uniform_matrix3(const string &name, const float *v)
250 uniform(name, new UniformMatrix3x3f(v));
253 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 4> &m)
255 uniform_matrix4x3(name, &m(0, 0));
258 void ProgramData::uniform_matrix4x3(const string &name, const float *v)
260 uniform(name, new UniformMatrix4x3f(v));
263 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 2> &m)
265 uniform_matrix2x4(name, &m(0, 0));
268 void ProgramData::uniform_matrix2x4(const string &name, const float *v)
270 uniform(name, new UniformMatrix2x4f(v));
273 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 3> &m)
275 uniform_matrix3x4(name, &m(0, 0));
278 void ProgramData::uniform_matrix3x4(const string &name, const float *v)
280 uniform(name, new UniformMatrix3x4f(v));
283 void ProgramData::uniform(const string &name, const Matrix &m)
285 uniform_matrix4(name, m.data());
288 void ProgramData::uniform_matrix4(const string &name, const float *v)
290 uniform(name, new UniformMatrix4x4f(v));
293 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
295 uniform(name, new UniformArray<Uniform1i>(n, v));
298 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
300 uniform(name, new UniformArray<Uniform1f>(n, v));
303 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
305 uniform(name, new UniformArray<Uniform2i>(n, v));
308 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
310 uniform(name, new UniformArray<Uniform2f>(n, v));
313 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
315 uniform(name, new UniformArray<Uniform3i>(n, v));
318 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
320 uniform(name, new UniformArray<Uniform3f>(n, v));
323 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
325 uniform(name, new UniformArray<Uniform4i>(n, v));
328 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
330 uniform(name, new UniformArray<Uniform4f>(n, v));
333 void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v)
335 uniform(name, new UniformArray<UniformMatrix2x2f>(n, v));
338 void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v)
340 uniform(name, new UniformArray<UniformMatrix3x2f>(n, v));
343 void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v)
345 uniform(name, new UniformArray<UniformMatrix4x2f>(n, v));
348 void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v)
350 uniform(name, new UniformArray<UniformMatrix2x3f>(n, v));
353 void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
355 uniform(name, new UniformArray<UniformMatrix3x3f>(n, v));
358 void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v)
360 uniform(name, new UniformArray<UniformMatrix4x3f>(n, v));
363 void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v)
365 uniform(name, new UniformArray<UniformMatrix2x4f>(n, v));
368 void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v)
370 uniform(name, new UniformArray<UniformMatrix3x4f>(n, v));
373 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
375 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
378 void ProgramData::remove_uniform(const string &name)
380 vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
381 if(i==uniforms.end() || i->name!=name)
390 vector<string> ProgramData::get_uniform_names() const
392 vector<string> names;
393 names.reserve(uniforms.size());
394 for(vector<NamedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
395 names.push_back(i->name);
399 const Uniform &ProgramData::get_uniform(const string &name) const
401 int i = find_uniform_index(name);
403 throw key_error(name);
404 return *uniforms[i].value;
407 bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
412 int ProgramData::find_uniform_index(const string &name) const
414 vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
415 return ((i!=uniforms.end() && i->name==name) ? i-uniforms.begin() : -1);
418 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
420 UInt8 *indices = block.indices.values;
421 if(info.uniforms.size()>16)
423 if(block.indices.type_flag==0xFD)
425 block.indices.dynamic.values = new UInt8[info.uniforms.size()];
426 block.indices.type_flag = 0xFE;
428 indices = block.indices.dynamic.values;
432 for(unsigned i=0; i<info.uniforms.size(); ++i)
434 int j = find_uniform_index(info.uniforms[i]->name);
438 if(static_cast<unsigned>(j)<MASK_BITS)
445 block.dirty = block.used;
448 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
450 const UInt8 *indices = block.get_uniform_indices();
451 for(unsigned i=0; i<info.uniforms.size(); ++i)
453 block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
456 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
458 BlockMap::iterator i = blocks.find(info.layout_hash);
461 bool any_found = false;
462 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); (!any_found && j!=info.uniforms.end()); ++j)
463 any_found = (find_uniform_index((*j)->name)>=0);
465 // TODO throw if all uniforms for a buffer-backed block are not found
470 if(info.bind_point>=0)
474 buffer = new Buffer(UNIFORM_BUFFER);
475 buffer->set_usage(STREAM_DRAW);
478 block = new UniformBlock(info.data_size);
479 block->use_buffer(buffer, last_block);
483 block = new UniformBlock;
485 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
486 update_block_uniform_indices(i->second, info);
492 void ProgramData::apply() const
494 const Program *prog = Program::current();
496 throw invalid_operation("ProgramData::apply");
498 Program::LayoutHash layout = prog->get_uniform_layout_hash();
499 ProgramUniforms &pu = programs[layout];
501 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
502 Mask affected = (dirty&pu.used) | force_dirty;
503 if(affected|pu.dirty)
505 /* If the global dirty flag affects this program, add it to per-program
506 dirty flags and clear the global flag. A previously unseen program will
507 always cause this to happen. */
510 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
511 i->second.dirty |= (dirty&i->second.used) | force_dirty;
512 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
513 i->second.dirty |= (dirty&i->second.used) | force_dirty;
517 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
519 if(pu.dirty==ALL_ONES)
521 /* The set of uniforms has changed since this program was last used.
522 Regenerate the list of uniform blocks. */
524 pu.blocks.reserve(prog_blocks.size());
527 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
529 SharedBlock *shared = get_shared_block(i->second);
532 if(shared->dirty==ALL_ONES)
533 update_block_uniform_indices(*shared, i->second);
534 pu.used |= shared->used;
537 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
541 // Update the contents of all dirty blocks.
542 bool buffered_blocks_updated = false;
543 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
544 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
546 if(!j->shared || !j->shared->dirty)
549 update_block(*j->shared, i->second);
550 j->shared->dirty = 0;
551 buffered_blocks_updated |= (j->bind_point>=0);
556 /* If any blocks stored in the buffer were updated, bind the buffer here
557 to avoid state thrashing. */
558 if(buffered_blocks_updated && !ARB_direct_state_access)
562 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
564 i->block->apply(i->bind_point);
568 ProgramData::NamedUniform::NamedUniform():
572 void ProgramData::NamedUniform::replace_value(Uniform *v)
574 /* UniformBlock does not copy the uniforms, so existing default blocks
575 will be left with stale pointers. This is not a problem as long as no
576 one stores pointers to the blocks and expects them to stay valid. */
582 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
587 indices.type_flag = 0xFD;
590 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
592 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
596 ProgramData::ProgramBlock::ProgramBlock():
602 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
604 block((b && b->used) ? b->block : 0),
609 ProgramData::ProgramUniforms::ProgramUniforms():
615 ProgramData::Loader::Loader(ProgramData &pd):
616 DataFile::ObjectLoader<ProgramData>(pd)
618 add("uniform", &Loader::uniform1i);
619 add("uniform1i", &Loader::uniform1i);
620 add("uniform", &Loader::uniform1f);
621 add("uniform1f", &Loader::uniform1f);
622 add("uniform", &Loader::uniform2i);
623 add("uniform2i", &Loader::uniform2i);
624 add("uniform", &Loader::uniform2f);
625 add("uniform2f", &Loader::uniform2f);
626 add("uniform", &Loader::uniform3i);
627 add("uniform3i", &Loader::uniform3i);
628 add("uniform", &Loader::uniform3f);
629 add("uniform3f", &Loader::uniform3f);
630 add("uniform", &Loader::uniform4i);
631 add("uniform4i", &Loader::uniform4i);
632 add("uniform", &Loader::uniform4f);
633 add("uniform4f", &Loader::uniform4f);
634 add("uniform1i_array", &Loader::uniform1i_array);
635 add("uniform1f_array", &Loader::uniform1f_array);
636 add("uniform2f_array", &Loader::uniform2f_array);
637 add("uniform3f_array", &Loader::uniform3f_array);
638 add("uniform4f_array", &Loader::uniform4f_array);
639 add("uniform_array", &Loader::uniform_array);
642 void ProgramData::Loader::uniform1i(const string &n, int v)
647 void ProgramData::Loader::uniform1f(const string &n, float v)
652 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
654 obj.uniform(n, v0, v1);
657 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
659 obj.uniform(n, v0, v1);
662 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
664 obj.uniform(n, v0, v1, v2);
667 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
669 obj.uniform(n, v0, v1, v2);
672 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
674 obj.uniform(n, v0, v1, v2, v3);
677 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
679 obj.uniform(n, v0, v1, v2, v3);
682 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
684 ArrayLoader ldr(t, e);
686 unsigned size = ldr.get_size();
688 throw logic_error("empty uniform array");
690 DataType type = ldr.get_data_type();
691 unsigned elem_size = ldr.get_element_size();
694 const int *data = reinterpret_cast<const int *>(ldr.get_data());
696 obj.uniform1_array(n, size, data);
697 else if(elem_size==2)
698 obj.uniform2_array(n, size, data);
699 else if(elem_size==3)
700 obj.uniform3_array(n, size, data);
701 else if(elem_size==4)
702 obj.uniform4_array(n, size, data);
704 throw logic_error("unsupported combination of array type and element size");
708 const float *data = reinterpret_cast<const float *>(ldr.get_data());
710 obj.uniform1_array(n, size, data);
711 else if(elem_size==2)
712 obj.uniform2_array(n, size, data);
713 else if(elem_size==3)
714 obj.uniform3_array(n, size, data);
715 else if(elem_size==4)
716 obj.uniform4_array(n, size, data);
718 throw logic_error("unsupported combination of array type and element size");
721 throw logic_error("unsupported array type");
724 void ProgramData::Loader::uniform1i_array(const string &n)
726 uniform_array_(n, INT, 1);
729 void ProgramData::Loader::uniform1f_array(const string &n)
731 uniform_array_(n, FLOAT, 1);
734 void ProgramData::Loader::uniform2i_array(const string &n)
736 uniform_array_(n, INT, 2);
739 void ProgramData::Loader::uniform2f_array(const string &n)
741 uniform_array_(n, FLOAT, 2);
744 void ProgramData::Loader::uniform3i_array(const string &n)
746 uniform_array_(n, INT, 3);
749 void ProgramData::Loader::uniform3f_array(const string &n)
751 uniform_array_(n, FLOAT, 3);
754 void ProgramData::Loader::uniform4i_array(const string &n)
756 uniform_array_(n, INT, 4);
759 void ProgramData::Loader::uniform4f_array(const string &n)
761 uniform_array_(n, FLOAT, 4);
764 void ProgramData::Loader::uniform_array(const string &n)
766 uniform_array_(n, static_cast<DataType>(0), 0);
770 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
774 add("uniform", &ArrayLoader::uniform1i);
775 add("uniform1i", &ArrayLoader::uniform1i);
776 add("uniform", &ArrayLoader::uniform1f);
777 add("uniform1f", &ArrayLoader::uniform1f);
778 add("uniform", &ArrayLoader::uniform2f);
779 add("uniform2f", &ArrayLoader::uniform2f);
780 add("uniform", &ArrayLoader::uniform3f);
781 add("uniform3f", &ArrayLoader::uniform3f);
782 add("uniform", &ArrayLoader::uniform4f);
783 add("uniform4f", &ArrayLoader::uniform4f);
786 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
788 if(element_size && (t!=type || e!=element_size))
789 throw logic_error("heterogeneous array contents");
797 const char *cv = reinterpret_cast<const char *>(v);
798 data.insert(data.end(), cv, cv+element_size*4);
801 void ProgramData::ArrayLoader::uniform1i(int v)
806 void ProgramData::ArrayLoader::uniform1f(float v)
808 uniform(FLOAT, 1, &v);
811 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
813 int va[2] = { v0, v1 };
817 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
819 float va[2] = { v0, v1 };
820 uniform(FLOAT, 2, va);
823 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
825 int va[3] = { v0, v1, v2 };
829 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
831 float va[3] = { v0, v1, v2 };
832 uniform(FLOAT, 3, va);
835 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
837 int va[4] = { v0, v1, v2, v3 };
841 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
843 float va[4] = { v0, v1, v2, v3 };
844 uniform(FLOAT, 4, va);