1 #include <msp/gl/extensions/arb_direct_state_access.h>
7 #include "programdata.h"
9 #include "uniformblock.h"
17 ProgramData::ProgramData(const Program *p):
24 // Blocks are intentionally left uncopied
25 ProgramData::ProgramData(const ProgramData &other):
27 uniform_slots(other.uniform_slots),
28 uniforms(other.uniforms),
33 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
37 ProgramData &ProgramData::operator=(const ProgramData &other)
39 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
43 tied_program = other.tied_program;
45 uniform_slots = other.uniform_slots;
46 for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
47 uniforms.push_back((*i)->clone());
49 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
50 delete i->second.block;
60 ProgramData::~ProgramData()
62 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
64 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
65 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 SlotMap::iterator i = uniform_slots.find(name);
85 if(i!=uniform_slots.end())
87 Uniform *&slot = uniforms[i->second];
88 /* UniformBlock does not copy the uniforms, so existing default blocks
89 will be left with stale pointers. This is not a problem as long as no
90 one stores pointers to the blocks and expects them to stay valid. */
94 if(i->second<MASK_BITS)
95 dirty |= 1<<i->second;
96 else // Force a full update if the mask isn't wide enough
101 uniform_slots[name] = uniforms.size();
102 uniforms.push_back(uni);
107 void ProgramData::uniform(const string &name, int v)
109 uniform(name, new Uniform1i(v));
112 void ProgramData::uniform(const string &name, float v)
114 uniform(name, new Uniform1f(v));
117 void ProgramData::uniform(const string &name, int v0, int v1)
119 int va[2] = { v0, v1 };
123 void ProgramData::uniform(const string &name, float v0, float v1)
125 float va[2] = { v0, v1 };
129 void ProgramData::uniform2(const string &name, const int *v)
131 uniform(name, new Uniform2i(v));
134 void ProgramData::uniform2(const string &name, const float *v)
136 uniform(name, new Uniform2f(v));
139 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
141 int va[3] = { v0, v1, v2 };
145 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
147 float va[3] = { v0, v1, v2 };
151 void ProgramData::uniform(const string &name, const Vector3 &v)
153 uniform(name, v.x, v.y, v.z);
156 void ProgramData::uniform3(const string &name, const int *v)
158 uniform(name, new Uniform3i(v));
161 void ProgramData::uniform3(const string &name, const float *v)
163 uniform(name, new Uniform3f(v));
166 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
168 int va[4] = { v0, v1, v2, v3 };
172 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
174 float va[4] = { v0, v1, v2, v3 };
178 void ProgramData::uniform(const string &name, const Vector4 &v)
180 uniform(name, v.x, v.y, v.z, v.w);
183 void ProgramData::uniform(const string &name, const Color &c)
185 uniform(name, c.r, c.g, c.b, c.a);
188 void ProgramData::uniform4(const string &name, const int *v)
190 uniform(name, new Uniform4i(v));
193 void ProgramData::uniform4(const string &name, const float *v)
195 uniform(name, new Uniform4f(v));
198 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
200 uniform_matrix2(name, &m(0, 0));
203 void ProgramData::uniform_matrix2(const string &name, const float *v)
205 uniform(name, new UniformMatrix2x2f(v));
208 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
210 uniform_matrix3(name, &m(0, 0));
213 void ProgramData::uniform_matrix3(const string &name, const float *v)
215 uniform(name, new UniformMatrix3x3f(v));
218 void ProgramData::uniform(const string &name, const Matrix &m)
220 uniform_matrix4(name, m.data());
223 void ProgramData::uniform_matrix4(const string &name, const float *v)
225 uniform(name, new UniformMatrix4x4f(v));
228 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
230 uniform(name, new UniformArray<Uniform1i>(n, v));
233 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
235 uniform(name, new UniformArray<Uniform1f>(n, v));
238 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
240 uniform(name, new UniformArray<Uniform2i>(n, v));
243 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
245 uniform(name, new UniformArray<Uniform2f>(n, v));
248 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
250 uniform(name, new UniformArray<Uniform3i>(n, v));
253 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
255 uniform(name, new UniformArray<Uniform3f>(n, v));
258 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
260 uniform(name, new UniformArray<Uniform4i>(n, v));
263 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
265 uniform(name, new UniformArray<Uniform4f>(n, v));
268 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
270 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
273 void ProgramData::remove_uniform(const string &name)
275 SlotMap::iterator i = uniform_slots.find(name);
276 if(i!=uniform_slots.end())
278 vector<Uniform *>::iterator j = uniforms.begin()+i->second;
282 for(SlotMap::iterator k=uniform_slots.begin(); k!=uniform_slots.end(); ++k)
283 if(k->second>i->second)
286 uniform_slots.erase(i);
292 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
295 for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
297 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
298 if(j!=uniform_slots.end() && j->second<MASK_BITS)
299 mask |= 1<<j->second;
305 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
307 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
309 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
310 if(j!=uniform_slots.end())
311 block.attach(**i, *uniforms[j->second]);
315 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
317 BlockMap::iterator i = blocks.find(info.layout_hash);
320 unsigned used = compute_slot_mask(info);
325 if(info.bind_point>=0)
329 buffer = new Buffer(UNIFORM_BUFFER);
330 buffer->set_usage(STREAM_DRAW);
333 block = new UniformBlock(info.data_size);
334 block->use_buffer(buffer, last_block);
338 block = new UniformBlock;
340 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
346 void ProgramData::apply() const
348 const Program *prog = Program::current();
350 throw invalid_operation("ProgramData::apply");
352 Program::LayoutHash layout = prog->get_uniform_layout_hash();
353 ProgramUniforms &pu = programs[layout];
355 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
356 Mask affected = (dirty&pu.used) | force_dirty;
357 if(affected|pu.dirty)
359 /* If the global dirty flag affects this program, add it to per-program
360 dirty flags and clear the global flag. A previously unseen program will
361 always cause this to happen. */
364 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
365 i->second.dirty |= (dirty&i->second.used) | force_dirty;
366 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
367 i->second.dirty |= (dirty&i->second.used) | force_dirty;
371 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
373 if(pu.dirty==ALL_ONES)
375 /* The set of uniforms has changed since this program was last used.
376 Regenerate the list of uniform blocks. */
378 pu.blocks.reserve(prog_blocks.size());
381 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
383 SharedBlock *shared = get_shared_block(i->second);
386 if(shared->dirty==ALL_ONES)
387 shared->used = compute_slot_mask(i->second);
388 pu.used |= shared->used;
391 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
395 // Update the contents of all dirty blocks.
396 bool buffered_blocks_updated = false;
397 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
398 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
400 if(!j->shared || !j->shared->dirty)
403 update_block(*j->block, i->second);
404 j->shared->dirty = 0;
405 buffered_blocks_updated |= (j->bind_point>=0);
410 /* If any blocks stored in the buffer were updated, bind the buffer here
411 to avoid state thrashing. */
412 if(buffered_blocks_updated && !ARB_direct_state_access)
416 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
418 i->block->apply(i->bind_point);
422 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
429 ProgramData::ProgramBlock::ProgramBlock():
435 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
437 block(b ? b->block : 0),
442 ProgramData::ProgramUniforms::ProgramUniforms():
448 ProgramData::Loader::Loader(ProgramData &pd):
449 DataFile::ObjectLoader<ProgramData>(pd)
451 add("uniform", &Loader::uniform1i);
452 add("uniform1i", &Loader::uniform1i);
453 add("uniform", &Loader::uniform1f);
454 add("uniform1f", &Loader::uniform1f);
455 add("uniform", &Loader::uniform2i);
456 add("uniform2i", &Loader::uniform2i);
457 add("uniform", &Loader::uniform2f);
458 add("uniform2f", &Loader::uniform2f);
459 add("uniform", &Loader::uniform3i);
460 add("uniform3i", &Loader::uniform3i);
461 add("uniform", &Loader::uniform3f);
462 add("uniform3f", &Loader::uniform3f);
463 add("uniform", &Loader::uniform4i);
464 add("uniform4i", &Loader::uniform4i);
465 add("uniform", &Loader::uniform4f);
466 add("uniform4f", &Loader::uniform4f);
467 add("uniform1i_array", &Loader::uniform1i_array);
468 add("uniform1f_array", &Loader::uniform1f_array);
469 add("uniform2f_array", &Loader::uniform2f_array);
470 add("uniform3f_array", &Loader::uniform3f_array);
471 add("uniform4f_array", &Loader::uniform4f_array);
472 add("uniform_array", &Loader::uniform_array);
475 void ProgramData::Loader::uniform1i(const string &n, int v)
480 void ProgramData::Loader::uniform1f(const string &n, float v)
485 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
487 obj.uniform(n, v0, v1);
490 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
492 obj.uniform(n, v0, v1);
495 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
497 obj.uniform(n, v0, v1, v2);
500 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
502 obj.uniform(n, v0, v1, v2);
505 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
507 obj.uniform(n, v0, v1, v2, v3);
510 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
512 obj.uniform(n, v0, v1, v2, v3);
515 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
517 ArrayLoader ldr(t, e);
519 unsigned size = ldr.get_size();
521 throw logic_error("empty uniform array");
523 DataType type = ldr.get_data_type();
524 unsigned elem_size = ldr.get_element_size();
527 const int *data = reinterpret_cast<const int *>(ldr.get_data());
529 obj.uniform1_array(n, size, data);
530 else if(elem_size==2)
531 obj.uniform2_array(n, size, data);
532 else if(elem_size==3)
533 obj.uniform3_array(n, size, data);
534 else if(elem_size==4)
535 obj.uniform4_array(n, size, data);
537 throw logic_error("unsupported combination of array type and element size");
541 const float *data = reinterpret_cast<const float *>(ldr.get_data());
543 obj.uniform1_array(n, size, data);
544 else if(elem_size==2)
545 obj.uniform2_array(n, size, data);
546 else if(elem_size==3)
547 obj.uniform3_array(n, size, data);
548 else if(elem_size==4)
549 obj.uniform4_array(n, size, data);
551 throw logic_error("unsupported combination of array type and element size");
554 throw logic_error("unsupported array type");
557 void ProgramData::Loader::uniform1i_array(const string &n)
559 uniform_array_(n, INT, 1);
562 void ProgramData::Loader::uniform1f_array(const string &n)
564 uniform_array_(n, FLOAT, 1);
567 void ProgramData::Loader::uniform2i_array(const string &n)
569 uniform_array_(n, INT, 2);
572 void ProgramData::Loader::uniform2f_array(const string &n)
574 uniform_array_(n, FLOAT, 2);
577 void ProgramData::Loader::uniform3i_array(const string &n)
579 uniform_array_(n, INT, 3);
582 void ProgramData::Loader::uniform3f_array(const string &n)
584 uniform_array_(n, FLOAT, 3);
587 void ProgramData::Loader::uniform4i_array(const string &n)
589 uniform_array_(n, INT, 4);
592 void ProgramData::Loader::uniform4f_array(const string &n)
594 uniform_array_(n, FLOAT, 4);
597 void ProgramData::Loader::uniform_array(const string &n)
599 uniform_array_(n, static_cast<DataType>(0), 0);
603 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
607 add("uniform", &ArrayLoader::uniform1i);
608 add("uniform1i", &ArrayLoader::uniform1i);
609 add("uniform", &ArrayLoader::uniform1f);
610 add("uniform1f", &ArrayLoader::uniform1f);
611 add("uniform", &ArrayLoader::uniform2f);
612 add("uniform2f", &ArrayLoader::uniform2f);
613 add("uniform", &ArrayLoader::uniform3f);
614 add("uniform3f", &ArrayLoader::uniform3f);
615 add("uniform", &ArrayLoader::uniform4f);
616 add("uniform4f", &ArrayLoader::uniform4f);
619 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
621 if(element_size && (t!=type || e!=element_size))
622 throw logic_error("heterogeneous array contents");
630 const char *cv = reinterpret_cast<const char *>(v);
631 data.insert(data.end(), cv, cv+element_size*4);
634 void ProgramData::ArrayLoader::uniform1i(int v)
639 void ProgramData::ArrayLoader::uniform1f(float v)
641 uniform(FLOAT, 1, &v);
644 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
646 int va[2] = { v0, v1 };
650 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
652 float va[2] = { v0, v1 };
653 uniform(FLOAT, 2, va);
656 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
658 int va[3] = { v0, v1, v2 };
662 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
664 float va[3] = { v0, v1, v2 };
665 uniform(FLOAT, 3, va);
668 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
670 int va[4] = { v0, v1, v2, v3 };
674 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
676 float va[4] = { v0, v1, v2, v3 };
677 uniform(FLOAT, 4, va);