1 #include <msp/gl/extensions/arb_direct_state_access.h>
7 #include "programdata.h"
9 #include "uniformblock.h"
17 ProgramData::ProgramData():
23 // Blocks are intentionally left uncopied
24 ProgramData::ProgramData(const ProgramData &other):
25 uniform_slots(other.uniform_slots),
26 uniforms(other.uniforms),
31 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
35 ProgramData &ProgramData::operator=(const ProgramData &other)
37 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
41 uniform_slots = other.uniform_slots;
42 for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
43 uniforms.push_back((*i)->clone());
45 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
46 delete i->second.block;
56 ProgramData::~ProgramData()
58 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
60 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
61 delete i->second.block;
65 void ProgramData::uniform(const string &name, Uniform *uni)
67 if(name[name.size()-1]==']')
68 throw invalid_argument("ProgramData::uniform");
70 SlotMap::iterator i = uniform_slots.find(name);
71 if(i!=uniform_slots.end())
73 Uniform *&slot = uniforms[i->second];
74 /* UniformBlock does not copy the uniforms, so existing default blocks
75 will be left with stale pointers. This is not a problem as long as no
76 one stores pointers to the blocks and expects them to stay valid. */
80 if(i->second<MASK_BITS)
81 dirty |= 1<<i->second;
82 else // Force a full update if the mask isn't wide enough
87 uniform_slots[name] = uniforms.size();
88 uniforms.push_back(uni);
93 void ProgramData::uniform(const string &name, int v)
95 uniform(name, new Uniform1i(v));
98 void ProgramData::uniform(const string &name, float v)
100 uniform(name, new Uniform1f(v));
103 void ProgramData::uniform(const string &name, int v0, int v1)
105 int va[2] = { v0, v1 };
109 void ProgramData::uniform(const string &name, float v0, float v1)
111 float va[2] = { v0, v1 };
115 void ProgramData::uniform2(const string &name, const int *v)
117 uniform(name, new Uniform2i(v));
120 void ProgramData::uniform2(const string &name, const float *v)
122 uniform(name, new Uniform2f(v));
125 void ProgramData::uniform(const string &name, int v0, int v1, int v2)
127 int va[3] = { v0, v1, v2 };
131 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
133 float va[3] = { v0, v1, v2 };
137 void ProgramData::uniform(const string &name, const Vector3 &v)
139 uniform(name, v.x, v.y, v.z);
142 void ProgramData::uniform3(const string &name, const int *v)
144 uniform(name, new Uniform3i(v));
147 void ProgramData::uniform3(const string &name, const float *v)
149 uniform(name, new Uniform3f(v));
152 void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
154 int va[4] = { v0, v1, v2, v3 };
158 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
160 float va[4] = { v0, v1, v2, v3 };
164 void ProgramData::uniform(const string &name, const Vector4 &v)
166 uniform(name, v.x, v.y, v.z, v.w);
169 void ProgramData::uniform(const string &name, const Color &c)
171 uniform(name, c.r, c.g, c.b, c.a);
174 void ProgramData::uniform4(const string &name, const int *v)
176 uniform(name, new Uniform4i(v));
179 void ProgramData::uniform4(const string &name, const float *v)
181 uniform(name, new Uniform4f(v));
184 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
186 uniform_matrix2(name, &m(0, 0));
189 void ProgramData::uniform_matrix2(const string &name, const float *v)
191 uniform(name, new UniformMatrix2x2f(v));
194 void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
196 uniform_matrix3(name, &m(0, 0));
199 void ProgramData::uniform_matrix3(const string &name, const float *v)
201 uniform(name, new UniformMatrix3x3f(v));
204 void ProgramData::uniform(const string &name, const Matrix &m)
206 uniform_matrix4(name, m.data());
209 void ProgramData::uniform_matrix4(const string &name, const float *v)
211 uniform(name, new UniformMatrix4x4f(v));
214 void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
216 uniform(name, new UniformArray<Uniform1i>(n, v));
219 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
221 uniform(name, new UniformArray<Uniform1f>(n, v));
224 void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
226 uniform(name, new UniformArray<Uniform2i>(n, v));
229 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
231 uniform(name, new UniformArray<Uniform2f>(n, v));
234 void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
236 uniform(name, new UniformArray<Uniform3i>(n, v));
239 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
241 uniform(name, new UniformArray<Uniform3f>(n, v));
244 void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
246 uniform(name, new UniformArray<Uniform4i>(n, v));
249 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
251 uniform(name, new UniformArray<Uniform4f>(n, v));
254 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
256 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
259 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
262 for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
264 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
265 if(j!=uniform_slots.end() && j->second<MASK_BITS)
266 mask |= 1<<j->second;
272 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
274 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
276 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
277 if(j!=uniform_slots.end())
278 block.attach(**i, *uniforms[j->second]);
282 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
284 BlockMap::iterator i = blocks.find(info.layout_hash);
287 unsigned used = compute_slot_mask(info);
292 if(info.bind_point>=0)
296 buffer = new Buffer(UNIFORM_BUFFER);
297 buffer->set_usage(STREAM_DRAW);
300 block = new UniformBlock(info.data_size);
301 block->use_buffer(buffer, last_block);
305 block = new UniformBlock;
307 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
313 void ProgramData::apply() const
315 const Program *prog = Program::current();
317 throw invalid_operation("ProgramData::apply");
319 Program::LayoutHash layout = prog->get_uniform_layout_hash();
320 ProgramUniforms &pu = programs[layout];
322 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
323 Mask affected = (dirty&pu.used) | force_dirty;
324 if(affected|pu.dirty)
326 /* If the global dirty flag affects this program, add it to per-program
327 dirty flags and clear the global flag. A previously unseen program will
328 always cause this to happen. */
331 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
332 i->second.dirty |= (dirty&i->second.used) | force_dirty;
333 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
334 i->second.dirty |= (dirty&i->second.used) | force_dirty;
338 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
340 if(pu.dirty==ALL_ONES)
342 /* The set of uniforms has changed since this program was last used.
343 Regenerate the list of uniform blocks. */
345 pu.blocks.reserve(prog_blocks.size());
348 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
350 SharedBlock *shared = get_shared_block(i->second);
353 if(shared->dirty==ALL_ONES)
354 shared->used = compute_slot_mask(i->second);
355 pu.used |= shared->used;
358 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
362 // Update the contents of all dirty blocks.
363 bool buffered_blocks_updated = false;
364 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
365 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
367 if(!j->shared || !j->shared->dirty)
370 update_block(*j->block, i->second);
371 j->shared->dirty = 0;
372 buffered_blocks_updated |= (j->bind_point>=0);
377 /* If any blocks stored in the buffer were updated, bind the buffer here
378 to avoid state thrashing. */
379 if(buffered_blocks_updated && !ARB_direct_state_access)
383 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
385 i->block->apply(i->bind_point);
389 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
396 ProgramData::ProgramBlock::ProgramBlock():
402 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
404 block(b ? b->block : 0),
409 ProgramData::ProgramUniforms::ProgramUniforms():
415 ProgramData::Loader::Loader(ProgramData &pd):
416 DataFile::ObjectLoader<ProgramData>(pd)
418 add("uniform", &Loader::uniform1i);
419 add("uniform1i", &Loader::uniform1i);
420 add("uniform", &Loader::uniform1f);
421 add("uniform1f", &Loader::uniform1f);
422 add("uniform", &Loader::uniform2i);
423 add("uniform2i", &Loader::uniform2i);
424 add("uniform", &Loader::uniform2f);
425 add("uniform2f", &Loader::uniform2f);
426 add("uniform", &Loader::uniform3i);
427 add("uniform3i", &Loader::uniform3i);
428 add("uniform", &Loader::uniform3f);
429 add("uniform3f", &Loader::uniform3f);
430 add("uniform", &Loader::uniform4i);
431 add("uniform4i", &Loader::uniform4i);
432 add("uniform", &Loader::uniform4f);
433 add("uniform4f", &Loader::uniform4f);
434 add("uniform1i_array", &Loader::uniform1i_array);
435 add("uniform1f_array", &Loader::uniform1f_array);
436 add("uniform2f_array", &Loader::uniform2f_array);
437 add("uniform3f_array", &Loader::uniform3f_array);
438 add("uniform4f_array", &Loader::uniform4f_array);
439 add("uniform_array", &Loader::uniform_array);
442 void ProgramData::Loader::uniform1i(const string &n, int v)
447 void ProgramData::Loader::uniform1f(const string &n, float v)
452 void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
454 obj.uniform(n, v0, v1);
457 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
459 obj.uniform(n, v0, v1);
462 void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
464 obj.uniform(n, v0, v1, v2);
467 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
469 obj.uniform(n, v0, v1, v2);
472 void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
474 obj.uniform(n, v0, v1, v2, v3);
477 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
479 obj.uniform(n, v0, v1, v2, v3);
482 void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
484 ArrayLoader ldr(t, e);
486 unsigned size = ldr.get_size();
488 throw logic_error("empty uniform array");
490 DataType type = ldr.get_data_type();
491 unsigned elem_size = ldr.get_element_size();
494 const int *data = reinterpret_cast<const int *>(ldr.get_data());
496 obj.uniform1_array(n, size, data);
497 else if(elem_size==2)
498 obj.uniform2_array(n, size, data);
499 else if(elem_size==3)
500 obj.uniform3_array(n, size, data);
501 else if(elem_size==4)
502 obj.uniform4_array(n, size, data);
504 throw logic_error("unsupported combination of array type and element size");
508 const float *data = reinterpret_cast<const float *>(ldr.get_data());
510 obj.uniform1_array(n, size, data);
511 else if(elem_size==2)
512 obj.uniform2_array(n, size, data);
513 else if(elem_size==3)
514 obj.uniform3_array(n, size, data);
515 else if(elem_size==4)
516 obj.uniform4_array(n, size, data);
518 throw logic_error("unsupported combination of array type and element size");
521 throw logic_error("unsupported array type");
524 void ProgramData::Loader::uniform1i_array(const string &n)
526 uniform_array_(n, INT, 1);
529 void ProgramData::Loader::uniform1f_array(const string &n)
531 uniform_array_(n, FLOAT, 1);
534 void ProgramData::Loader::uniform2i_array(const string &n)
536 uniform_array_(n, INT, 2);
539 void ProgramData::Loader::uniform2f_array(const string &n)
541 uniform_array_(n, FLOAT, 2);
544 void ProgramData::Loader::uniform3i_array(const string &n)
546 uniform_array_(n, INT, 3);
549 void ProgramData::Loader::uniform3f_array(const string &n)
551 uniform_array_(n, FLOAT, 3);
554 void ProgramData::Loader::uniform4i_array(const string &n)
556 uniform_array_(n, INT, 4);
559 void ProgramData::Loader::uniform4f_array(const string &n)
561 uniform_array_(n, FLOAT, 4);
564 void ProgramData::Loader::uniform_array(const string &n)
566 uniform_array_(n, static_cast<DataType>(0), 0);
570 ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
574 add("uniform", &ArrayLoader::uniform1i);
575 add("uniform1i", &ArrayLoader::uniform1i);
576 add("uniform", &ArrayLoader::uniform1f);
577 add("uniform1f", &ArrayLoader::uniform1f);
578 add("uniform", &ArrayLoader::uniform2f);
579 add("uniform2f", &ArrayLoader::uniform2f);
580 add("uniform", &ArrayLoader::uniform3f);
581 add("uniform3f", &ArrayLoader::uniform3f);
582 add("uniform", &ArrayLoader::uniform4f);
583 add("uniform4f", &ArrayLoader::uniform4f);
586 void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
588 if(element_size && (t!=type || e!=element_size))
589 throw logic_error("heterogeneous array contents");
597 const char *cv = reinterpret_cast<const char *>(v);
598 data.insert(data.end(), cv, cv+element_size*4);
601 void ProgramData::ArrayLoader::uniform1i(int v)
606 void ProgramData::ArrayLoader::uniform1f(float v)
608 uniform(FLOAT, 1, &v);
611 void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
613 int va[2] = { v0, v1 };
617 void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
619 float va[2] = { v0, v1 };
620 uniform(FLOAT, 2, va);
623 void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
625 int va[3] = { v0, v1, v2 };
629 void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
631 float va[3] = { v0, v1, v2 };
632 uniform(FLOAT, 3, va);
635 void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
637 int va[4] = { v0, v1, v2, v3 };
641 void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
643 float va[4] = { v0, v1, v2, v3 };
644 uniform(FLOAT, 4, va);