1 #include <msp/core/maputils.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/io/print.h>
10 #include "programdata.h"
12 #include "uniformblock.h"
20 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),
35 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
36 i->value = i->value->clone();
39 ProgramData::ProgramData(const ProgramData &other, const Program *p):
47 for(vector<NamedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
48 tied_program->get_uniform_info(i->name);
51 uniforms = other.uniforms;
52 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
53 i->value = i->value->clone();
56 ProgramData &ProgramData::operator=(const ProgramData &other)
58 tied_program = other.tied_program;
60 uniforms = other.uniforms;
61 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
62 i->value = i->value->clone();
64 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
65 delete i->second.block;
75 ProgramData::~ProgramData()
77 for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
79 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
81 if(i->second.indices.type_flag==0xFE)
82 delete i->second.indices.dynamic.values;
83 delete i->second.block;
88 void ProgramData::uniform(const string &name, Uniform *uni)
93 tied_program->get_uniform_info(name);
94 else if(name[name.size()-1]==']')
95 throw invalid_argument("ProgramData::uniform");
97 catch(const exception &e)
101 IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what());
108 int i = find_uniform_index(name);
111 uniforms[i].replace_value(uni);
113 if(static_cast<unsigned>(i)<MASK_BITS)
115 else // Force a full update if the mask isn't wide enough
121 vector<NamedUniform>::iterator j = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
126 uniforms.insert(j, nu);
131 void ProgramData::uniform(const string &name, const Uniform &u)
133 uniform(name, u.clone());
136 void ProgramData::uniform(const string &name, int v)
138 uniform(name, new Uniform1i(v));
141 void ProgramData::uniform(const string &name, float v)
143 uniform(name, new Uniform1f(v));
146 void ProgramData::uniform(const string &name, int v0, int v1)
148 int va[2] = { v0, v1 };
152 void ProgramData::uniform(const string &name, float v0, float v1)
154 float va[2] = { v0, v1 };
158 void ProgramData::uniform2(const string &name, const int *v)
160 uniform(name, new Uniform2i(v));
163 void ProgramData::uniform2(const string &name, const float *v)
165 uniform(name, new Uniform2f(v));
168 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
170 int va[3] = { v0, v1, v2 };
174 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
176 float va[3] = { v0, v1, v2 };
180 void ProgramData::uniform(const string &name, const Vector3 &v)
182 uniform(name, v.x, v.y, v.z);
185 void ProgramData::uniform3(const string &name, const int *v)
187 uniform(name, new Uniform3i(v));
190 void ProgramData::uniform3(const string &name, const float *v)
192 uniform(name, new Uniform3f(v));
195 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
197 int va[4] = { v0, v1, v2, v3 };
201 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
203 float va[4] = { v0, v1, v2, v3 };
207 void ProgramData::uniform(const string &name, const Vector4 &v)
209 uniform(name, v.x, v.y, v.z, v.w);
212 void ProgramData::uniform(const string &name, const Color &c)
214 uniform(name, c.r, c.g, c.b, c.a);
217 void ProgramData::uniform4(const string &name, const int *v)
219 uniform(name, new Uniform4i(v));
222 void ProgramData::uniform4(const string &name, const float *v)
224 uniform(name, new Uniform4f(v));
227 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
229 uniform_matrix2(name, &m(0, 0));
232 void ProgramData::uniform_matrix2(const string &name, const float *v)
234 uniform(name, new UniformMatrix2x2f(v));
237 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 3> &m)
239 uniform_matrix3x2(name, &m(0, 0));
242 void ProgramData::uniform_matrix3x2(const string &name, const float *v)
244 uniform(name, new UniformMatrix3x2f(v));
247 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 4> &m)
249 uniform_matrix4x2(name, &m(0, 0));
252 void ProgramData::uniform_matrix4x2(const string &name, const float *v)
254 uniform(name, new UniformMatrix4x2f(v));
257 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 2> &m)
259 uniform_matrix2x3(name, &m(0, 0));
262 void ProgramData::uniform_matrix2x3(const string &name, const float *v)
264 uniform(name, new UniformMatrix2x3f(v));
267 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
269 uniform_matrix3(name, &m(0, 0));
272 void ProgramData::uniform_matrix3(const string &name, const float *v)
274 uniform(name, new UniformMatrix3x3f(v));
277 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 4> &m)
279 uniform_matrix4x3(name, &m(0, 0));
282 void ProgramData::uniform_matrix4x3(const string &name, const float *v)
284 uniform(name, new UniformMatrix4x3f(v));
287 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 2> &m)
289 uniform_matrix2x4(name, &m(0, 0));
292 void ProgramData::uniform_matrix2x4(const string &name, const float *v)
294 uniform(name, new UniformMatrix2x4f(v));
297 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 3> &m)
299 uniform_matrix3x4(name, &m(0, 0));
302 void ProgramData::uniform_matrix3x4(const string &name, const float *v)
304 uniform(name, new UniformMatrix3x4f(v));
307 void ProgramData::uniform(const string &name, const Matrix &m)
309 uniform_matrix4(name, m.data());
312 void ProgramData::uniform_matrix4(const string &name, const float *v)
314 uniform(name, new UniformMatrix4x4f(v));
317 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
319 uniform(name, new UniformArray<Uniform1i>(n, v));
322 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
324 uniform(name, new UniformArray<Uniform1f>(n, v));
327 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
329 uniform(name, new UniformArray<Uniform2i>(n, v));
332 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
334 uniform(name, new UniformArray<Uniform2f>(n, v));
337 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
339 uniform(name, new UniformArray<Uniform3i>(n, v));
342 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
344 uniform(name, new UniformArray<Uniform3f>(n, v));
347 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
349 uniform(name, new UniformArray<Uniform4i>(n, v));
352 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
354 uniform(name, new UniformArray<Uniform4f>(n, v));
357 void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v)
359 uniform(name, new UniformArray<UniformMatrix2x2f>(n, v));
362 void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v)
364 uniform(name, new UniformArray<UniformMatrix3x2f>(n, v));
367 void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v)
369 uniform(name, new UniformArray<UniformMatrix4x2f>(n, v));
372 void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v)
374 uniform(name, new UniformArray<UniformMatrix2x3f>(n, v));
377 void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
379 uniform(name, new UniformArray<UniformMatrix3x3f>(n, v));
382 void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v)
384 uniform(name, new UniformArray<UniformMatrix4x3f>(n, v));
387 void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v)
389 uniform(name, new UniformArray<UniformMatrix2x4f>(n, v));
392 void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v)
394 uniform(name, new UniformArray<UniformMatrix3x4f>(n, v));
397 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
399 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
402 void ProgramData::remove_uniform(const string &name)
404 vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
405 if(i==uniforms.end() || i->name!=name)
414 vector<string> ProgramData::get_uniform_names() const
416 vector<string> names;
417 names.reserve(uniforms.size());
418 for(vector<NamedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
419 names.push_back(i->name);
423 const Uniform &ProgramData::get_uniform(const string &name) const
425 int i = find_uniform_index(name);
427 throw key_error(name);
428 return *uniforms[i].value;
431 const Uniform *ProgramData::find_uniform(const string &name) const
433 int i = find_uniform_index(name);
434 return (i>=0 ? uniforms[i].value : 0);
437 bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
442 int ProgramData::find_uniform_index(const string &name) const
444 vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
445 return ((i!=uniforms.end() && i->name==name) ? i-uniforms.begin() : -1);
448 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
450 UInt8 *indices = block.indices.values;
451 if(info.uniforms.size()>16)
453 if(block.indices.type_flag==0xFD)
455 block.indices.dynamic.values = new UInt8[info.uniforms.size()];
456 block.indices.type_flag = 0xFE;
458 indices = block.indices.dynamic.values;
462 for(unsigned i=0; i<info.uniforms.size(); ++i)
464 int j = find_uniform_index(info.uniforms[i]->name);
468 if(static_cast<unsigned>(j)<MASK_BITS)
475 block.dirty = block.used;
478 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
480 const UInt8 *indices = block.get_uniform_indices();
481 for(unsigned i=0; i<info.uniforms.size(); ++i)
483 block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
486 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
488 BlockMap::iterator i = blocks.find(info.layout_hash);
491 bool any_found = false;
492 bool all_found = true;
493 for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); j!=info.uniforms.end(); ++j)
495 if(find_uniform_index((*j)->name)>=0)
503 else if(!all_found && info.bind_point>=0)
506 IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
508 throw incomplete_uniform_block(info.name);
513 if(info.bind_point>=0)
517 buffer = new Buffer(UNIFORM_BUFFER);
518 buffer->set_usage(STREAM_DRAW);
521 block = new UniformBlock(info.data_size);
522 block->use_buffer(buffer, last_block);
526 block = new UniformBlock;
528 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
529 update_block_uniform_indices(i->second, info);
535 void ProgramData::apply() const
537 const Program *prog = Program::current();
539 throw invalid_operation("ProgramData::apply");
541 Program::LayoutHash layout = prog->get_uniform_layout_hash();
542 ProgramUniforms &pu = programs[layout];
544 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
545 Mask affected = (dirty&pu.used) | force_dirty;
546 if(affected|pu.dirty)
548 /* If the global dirty flag affects this program, add it to per-program
549 dirty flags and clear the global flag. A previously unseen program will
550 always cause this to happen. */
553 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
554 i->second.dirty |= (dirty&i->second.used) | force_dirty;
555 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
556 i->second.dirty |= (dirty&i->second.used) | force_dirty;
560 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
562 if(pu.dirty==ALL_ONES)
564 /* The set of uniforms has changed since this program was last used.
565 Regenerate the list of uniform blocks. */
567 pu.blocks.reserve(prog_blocks.size());
570 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
572 SharedBlock *shared = get_shared_block(i->second);
575 if(shared->dirty==ALL_ONES)
576 update_block_uniform_indices(*shared, i->second);
577 pu.used |= shared->used;
580 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
584 // Update the contents of all dirty blocks.
585 bool buffered_blocks_updated = false;
586 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
587 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
589 if(!j->shared || !j->shared->dirty)
592 update_block(*j->shared, i->second);
593 j->shared->dirty = 0;
594 buffered_blocks_updated |= (j->bind_point>=0);
599 /* If any blocks stored in the buffer were updated, bind the buffer here
600 to avoid state thrashing. */
601 if(buffered_blocks_updated && !ARB_direct_state_access)
605 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
607 i->block->apply(i->bind_point);
611 ProgramData::NamedUniform::NamedUniform():
615 void ProgramData::NamedUniform::replace_value(Uniform *v)
617 /* UniformBlock does not copy the uniforms, so existing default blocks
618 will be left with stale pointers. This is not a problem as long as no
619 one stores pointers to the blocks and expects them to stay valid. */
625 ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
630 indices.type_flag = 0xFD;
633 const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
635 return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
639 ProgramData::ProgramBlock::ProgramBlock():
645 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
647 block((b && b->used) ? b->block : 0),
652 ProgramData::ProgramUniforms::ProgramUniforms():
658 ProgramData::Loader::Loader(ProgramData &pd):
659 DataFile::ObjectLoader<ProgramData>(pd)
661 add("uniform", &Loader::uniform1i);
662 add("uniform1i", &Loader::uniform1i);
663 add("uniform", &Loader::uniform1f);
664 add("uniform1f", &Loader::uniform1f);
665 add("uniform", &Loader::uniform2i);
666 add("uniform2i", &Loader::uniform2i);
667 add("uniform", &Loader::uniform2f);
668 add("uniform2f", &Loader::uniform2f);
669 add("uniform", &Loader::uniform3i);
670 add("uniform3i", &Loader::uniform3i);
671 add("uniform", &Loader::uniform3f);
672 add("uniform3f", &Loader::uniform3f);
673 add("uniform", &Loader::uniform4i);
674 add("uniform4i", &Loader::uniform4i);
675 add("uniform", &Loader::uniform4f);
676 add("uniform4f", &Loader::uniform4f);
677 add("uniform1i_array", &Loader::uniform1i_array);
678 add("uniform1f_array", &Loader::uniform1f_array);
679 add("uniform2f_array", &Loader::uniform2f_array);
680 add("uniform3f_array", &Loader::uniform3f_array);
681 add("uniform4f_array", &Loader::uniform4f_array);
682 add("uniform_array", &Loader::uniform_array);
685 void ProgramData::Loader::uniform1i(const string &n, int v)
690 void ProgramData::Loader::uniform1f(const string &n, float v)
695 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
697 obj.uniform(n, v0, v1);
700 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
702 obj.uniform(n, v0, v1);
705 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
707 obj.uniform(n, v0, v1, v2);
710 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
712 obj.uniform(n, v0, v1, v2);
715 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
717 obj.uniform(n, v0, v1, v2, v3);
720 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
722 obj.uniform(n, v0, v1, v2, v3);
725 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
727 ArrayLoader ldr(t, e);
729 unsigned size = ldr.get_size();
731 throw logic_error("empty uniform array");
733 DataType type = ldr.get_data_type();
734 unsigned elem_size = ldr.get_element_size();
737 const int *data = reinterpret_cast<const int *>(ldr.get_data());
739 obj.uniform1_array(n, size, data);
740 else if(elem_size==2)
741 obj.uniform2_array(n, size, data);
742 else if(elem_size==3)
743 obj.uniform3_array(n, size, data);
744 else if(elem_size==4)
745 obj.uniform4_array(n, size, data);
747 throw logic_error("unsupported combination of array type and element size");
751 const float *data = reinterpret_cast<const float *>(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");
764 throw logic_error("unsupported array type");
767 void ProgramData::Loader::uniform1i_array(const string &n)
769 uniform_array_(n, INT, 1);
772 void ProgramData::Loader::uniform1f_array(const string &n)
774 uniform_array_(n, FLOAT, 1);
777 void ProgramData::Loader::uniform2i_array(const string &n)
779 uniform_array_(n, INT, 2);
782 void ProgramData::Loader::uniform2f_array(const string &n)
784 uniform_array_(n, FLOAT, 2);
787 void ProgramData::Loader::uniform3i_array(const string &n)
789 uniform_array_(n, INT, 3);
792 void ProgramData::Loader::uniform3f_array(const string &n)
794 uniform_array_(n, FLOAT, 3);
797 void ProgramData::Loader::uniform4i_array(const string &n)
799 uniform_array_(n, INT, 4);
802 void ProgramData::Loader::uniform4f_array(const string &n)
804 uniform_array_(n, FLOAT, 4);
807 void ProgramData::Loader::uniform_array(const string &n)
809 uniform_array_(n, static_cast<DataType>(0), 0);
813 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
817 add("uniform", &ArrayLoader::uniform1i);
818 add("uniform1i", &ArrayLoader::uniform1i);
819 add("uniform", &ArrayLoader::uniform1f);
820 add("uniform1f", &ArrayLoader::uniform1f);
821 add("uniform", &ArrayLoader::uniform2f);
822 add("uniform2f", &ArrayLoader::uniform2f);
823 add("uniform", &ArrayLoader::uniform3f);
824 add("uniform3f", &ArrayLoader::uniform3f);
825 add("uniform", &ArrayLoader::uniform4f);
826 add("uniform4f", &ArrayLoader::uniform4f);
829 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
831 if(element_size && (t!=type || e!=element_size))
832 throw logic_error("heterogeneous array contents");
840 const char *cv = reinterpret_cast<const char *>(v);
841 data.insert(data.end(), cv, cv+element_size*4);
844 void ProgramData::ArrayLoader::uniform1i(int v)
849 void ProgramData::ArrayLoader::uniform1f(float v)
851 uniform(FLOAT, 1, &v);
854 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
856 int va[2] = { v0, v1 };
860 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
862 float va[2] = { v0, v1 };
863 uniform(FLOAT, 2, va);
866 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
868 int va[3] = { v0, v1, v2 };
872 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
874 float va[3] = { v0, v1, v2 };
875 uniform(FLOAT, 3, va);
878 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
880 int va[4] = { v0, v1, v2, v3 };
884 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
886 float va[4] = { v0, v1, v2, v3 };
887 uniform(FLOAT, 4, va);