6 #include "programdata.h"
8 #include "uniformblock.h"
16 ProgramData::ProgramData():
22 // Blocks are intentionally left uncopied
23 ProgramData::ProgramData(const ProgramData &other):
24 uniforms(other.uniforms),
29 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
33 ProgramData &ProgramData::operator=(const ProgramData &other)
35 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
39 for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
40 uniforms.push_back((*i)->clone());
42 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
43 delete i->second.block;
53 ProgramData::~ProgramData()
55 for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
57 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
58 delete i->second.block;
62 void ProgramData::uniform(const string &name, Uniform *uni)
64 if(name[name.size()-1]==']')
65 throw invalid_argument("ProgramData::uniform");
67 SlotMap::iterator i = uniform_slots.find(name);
68 if(i!=uniform_slots.end())
70 Uniform *&slot = uniforms[i->second];
71 /* UniformBlock does not copy the uniforms, so existing default blocks
72 will be left with stale pointers. This is not a problem as long as no
73 one stores pointers to the blocks and expects them to stay valid. */
77 if(i->second<MASK_BITS)
78 dirty |= 1<<i->second;
79 else // Force a full update if the mask isn't wide enough
84 uniform_slots[name] = uniforms.size();
85 uniforms.push_back(uni);
90 void ProgramData::uniform(const string &name, int v)
92 uniform(name, new Uniform1i(v));
95 void ProgramData::uniform(const string &name, float v)
97 uniform(name, new Uniform1f(v));
100 void ProgramData::uniform(const string &name, float v0, float v1)
102 float va[2] = { v0, v1 };
106 void ProgramData::uniform2(const string &name, const float *v)
108 uniform(name, new Uniform2f(v));
111 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
113 float va[3] = { v0, v1, v2 };
117 void ProgramData::uniform(const string &name, const Vector3 &v)
119 uniform(name, v.x, v.y, v.z);
122 void ProgramData::uniform3(const string &name, const float *v)
124 uniform(name, new Uniform3f(v));
127 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
129 float va[4] = { v0, v1, v2, v3 };
133 void ProgramData::uniform(const string &name, const Vector4 &v)
135 uniform(name, v.x, v.y, v.z, v.w);
138 void ProgramData::uniform(const string &name, const Color &c)
140 uniform(name, c.r, c.g, c.b, c.a);
143 void ProgramData::uniform4(const string &name, const float *v)
145 uniform(name, new Uniform4f(v));
148 void ProgramData::uniform_matrix2(const string &name, const float *v)
150 uniform(name, new UniformMatrix2x2f(v));
153 void ProgramData::uniform_matrix3(const string &name, const float *v)
155 uniform(name, new UniformMatrix3x3f(v));
158 void ProgramData::uniform(const string &name, const Matrix &m)
160 uniform_matrix4(name, m.data());
163 void ProgramData::uniform_matrix4(const string &name, const float *v)
165 uniform(name, new UniformMatrix4x4f(v));
168 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
170 uniform(name, new UniformArray<Uniform1f>(n, v));
173 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
175 uniform(name, new UniformArray<Uniform2f>(n, v));
178 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
180 uniform(name, new UniformArray<Uniform3f>(n, v));
183 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
185 uniform(name, new UniformArray<Uniform4f>(n, v));
188 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
190 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
193 unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
196 for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
198 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
199 if(j!=uniform_slots.end() && j->second<MASK_BITS)
200 mask |= 1<<j->second;
206 void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
208 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
210 SlotMap::const_iterator j = uniform_slots.find((*i)->name);
211 if(j!=uniform_slots.end())
212 block.attach(**i, *uniforms[j->second]);
216 ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
218 BlockMap::iterator i = blocks.find(info.layout_hash);
221 unsigned used = compute_slot_mask(info);
226 if(info.bind_point>=0)
230 buffer = new Buffer(UNIFORM_BUFFER);
231 buffer->set_usage(STREAM_DRAW);
234 block = new UniformBlock(info.data_size);
235 block->use_buffer(buffer, last_block);
239 block = new UniformBlock;
241 i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
247 void ProgramData::apply() const
249 const Program *prog = Program::current();
251 throw invalid_operation("ProgramData::apply");
253 Program::LayoutHash layout = prog->get_uniform_layout_hash();
254 ProgramUniforms &pu = programs[layout];
256 if((dirty&pu.used)|pu.dirty)
258 /* If the global dirty flag affects this program, add it to per-program
259 dirty flags and clear the global flag. A previously unseen program will
260 always cause this to happen. */
263 Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
264 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
265 i->second.dirty |= (dirty&i->second.used) | force_dirty;
266 for(ProgramMap::iterator i=programs.begin(); i!=programs.end(); ++i)
267 i->second.dirty |= (dirty&i->second.used) | force_dirty;
271 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
273 if(pu.dirty==ALL_ONES)
275 /* The set of uniforms has changed since this program was last used.
276 Regenerate the list of uniform blocks. */
278 pu.blocks.reserve(prog_blocks.size());
281 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
283 SharedBlock *shared = get_shared_block(i->second);
286 if(shared->dirty==ALL_ONES)
287 shared->used = compute_slot_mask(i->second);
288 pu.used |= shared->used;
291 pu.blocks.push_back(ProgramBlock(i->second.bind_point, shared));
295 // Update the contents of all dirty blocks.
296 bool buffered_blocks_updated = false;
297 std::vector<ProgramBlock>::iterator j = pu.blocks.begin();
298 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i, ++j)
300 if(!j->shared || !j->shared->dirty)
303 update_block(*j->block, i->second);
304 j->shared->dirty = 0;
305 buffered_blocks_updated |= (j->bind_point>=0);
310 /* If any blocks stored in the buffer were updated, bind the buffer here
311 to avoid state thrashing. */
312 if(buffered_blocks_updated)
316 for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
318 i->block->apply(i->bind_point);
322 ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
329 ProgramData::ProgramBlock::ProgramBlock():
335 ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
337 block(b ? b->block : 0),
342 ProgramData::ProgramUniforms::ProgramUniforms():
348 ProgramData::Loader::Loader(ProgramData &pd):
349 DataFile::ObjectLoader<ProgramData>(pd)
351 add("uniform1i", &Loader::uniform1i);
352 add("uniform1f", &Loader::uniform1f);
353 add("uniform2f", &Loader::uniform2f);
354 add("uniform3f", &Loader::uniform3f);
355 add("uniform4f", &Loader::uniform4f);
358 void ProgramData::Loader::uniform1i(const string &n, int v)
363 void ProgramData::Loader::uniform1f(const string &n, float v)
368 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
370 obj.uniform(n, v0, v1);
373 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
375 obj.uniform(n, v0, v1, v2);
378 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
380 obj.uniform(n, v0, v1, v2, v3);