1 #include <msp/gl/extensions/arb_shader_objects.h>
7 #include "programdata.h"
9 #include "uniformblock.h"
17 ProgramData::ProgramData():
22 static Require _req(ARB_shader_objects);
25 // Blocks are intentionally left uncopied
26 ProgramData::ProgramData(const ProgramData &other):
27 uniforms(other.uniforms),
32 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
33 i->second = i->second->clone();
36 ProgramData &ProgramData::operator=(const ProgramData &other)
38 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
42 for(UniformMap::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
43 uniforms.insert(uniforms.end(), UniformMap::value_type(i->first, i->second->clone()));
45 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
46 delete i->second.block;
54 ProgramData::~ProgramData()
56 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
58 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
59 delete i->second.block;
63 void ProgramData::uniform(const string &name, Uniform *uni)
65 UniformMap::iterator i = uniforms.find(name);
68 /* UniformBlock does not copy the uniforms, so existing blocks will be
69 left with stale pointers. This is not a problem as long as no one stores
70 pointers to the blocks and expects them to stay valid. */
73 changes = VALUES_CHANGED;
78 changes = KEYS_CHANGED;
82 void ProgramData::uniform(const string &name, int v)
84 uniform(name, new Uniform1i(v));
87 void ProgramData::uniform(const string &name, float v)
89 uniform(name, new Uniform1f(v));
92 void ProgramData::uniform(const string &name, float v0, float v1)
94 float va[2] = { v0, v1 };
98 void ProgramData::uniform2(const string &name, const float *v)
100 uniform(name, new Uniform2f(v));
103 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
105 float va[3] = { v0, v1, v2 };
109 void ProgramData::uniform(const string &name, const Vector3 &v)
111 uniform(name, v.x, v.y, v.z);
114 void ProgramData::uniform3(const string &name, const float *v)
116 uniform(name, new Uniform3f(v));
119 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
121 float va[4] = { v0, v1, v2, v3 };
125 void ProgramData::uniform(const string &name, const Vector4 &v)
127 uniform(name, v.x, v.y, v.z, v.w);
130 void ProgramData::uniform(const string &name, const Color &c)
132 uniform(name, c.r, c.g, c.b, c.a);
135 void ProgramData::uniform4(const string &name, const float *v)
137 uniform(name, new Uniform4f(v));
140 void ProgramData::uniform_matrix2(const string &name, const float *v)
142 uniform(name, new UniformMatrix2x2f(v));
145 void ProgramData::uniform_matrix3(const string &name, const float *v)
147 uniform(name, new UniformMatrix3x3f(v));
150 void ProgramData::uniform(const string &name, const Matrix &m)
152 uniform_matrix4(name, m.data());
155 void ProgramData::uniform_matrix4(const string &name, const float *v)
157 uniform(name, new UniformMatrix4x4f(v));
160 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
162 uniform(name, new UniformArray<Uniform1f>(n, v));
165 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
167 uniform(name, new UniformArray<Uniform2f>(n, v));
170 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
172 uniform(name, new UniformArray<Uniform3f>(n, v));
175 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
177 uniform(name, new UniformArray<Uniform4f>(n, v));
180 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
182 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
185 void ProgramData::find_uniforms_for_block(Block &block, const Program::UniformBlockInfo &info) const
187 block.uniforms.clear();
188 for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
190 // XXX individual array elements
191 UniformMap::const_iterator j = uniforms.find((*i)->name);
192 if(j!=uniforms.end())
193 block.uniforms[(*i)->location] = &j->second;
197 UniformBlock *ProgramData::create_block(const Program::UniformBlockInfo &info) const
199 UniformBlock *block = new UniformBlock(info.data_size);
202 buffer = new Buffer(UNIFORM_BUFFER);
203 buffer->set_usage(STREAM_DRAW);
205 block->use_buffer(buffer, last_block);
210 const UniformBlock *ProgramData::get_block(const Program &prog, const Program::UniformBlockInfo *info) const
214 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
215 if(i->second.changes<changes)
216 i->second.changes = changes;
217 changes = NO_CHANGES;
220 unsigned layout_hash = (info ? info->layout_hash : prog.get_uniform_layout_hash());
222 map<unsigned, Block>::iterator i = blocks.find(layout_hash);
225 i = blocks.insert(BlockMap::value_type(layout_hash, Block())).first;
228 find_uniforms_for_block(i->second, *info);
230 if(!i->second.uniforms.empty())
232 i->second.block = create_block(*info);
233 i->second.changes = VALUES_CHANGED;
238 i->second.block = new UniformBlock;
239 i->second.changes = VALUES_CHANGED;
242 else if(info && i->second.changes==KEYS_CHANGED)
244 find_uniforms_for_block(i->second, *info);
245 if(!i->second.uniforms.empty())
248 i->second.block = create_block(*info);
249 i->second.changes = VALUES_CHANGED;
252 i->second.changes = NO_CHANGES;
258 UniformBlock &block = *i->second.block;
259 if(i->second.changes)
263 vector<const Program::UniformInfo *>::const_iterator j = info->uniforms.begin();
264 map<unsigned, const Uniform *const *>::const_iterator k = i->second.uniforms.begin();
265 while(j!=info->uniforms.end() && k!=i->second.uniforms.end())
267 if(k->first==(*j)->location)
269 block.attach(**j, **k->second);
277 for(UniformMap::const_iterator j=uniforms.begin(); j!=uniforms.end(); ++j)
279 int loc = prog.get_uniform_location(j->first);
281 block.attach(loc, *j->second);
284 i->second.changes = NO_CHANGES;
290 const UniformBlock *ProgramData::get_block(const Program &prog, const string &name) const
293 return get_block(prog, 0);
295 return get_block(prog, &prog.get_uniform_block_info(name));
298 void ProgramData::apply() const
300 const Program *prog = Program::current();
302 throw invalid_operation("ProgramData::apply");
304 const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
305 if(!prog_blocks.empty())
307 typedef pair<const UniformBlock *, unsigned> ApplyBlock;
308 list<ApplyBlock> apply_blocks;
309 for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
310 if(const UniformBlock *block = get_block(*prog, &i->second))
311 apply_blocks.push_back(make_pair(block, i->second.bind_point));
316 for(list<ApplyBlock>::const_iterator i=apply_blocks.begin(); i!=apply_blocks.end(); ++i)
317 i->first->apply(i->second);
320 if(const UniformBlock *block = get_block(*prog, 0))
325 ProgramData::Block::Block():
331 ProgramData::Loader::Loader(ProgramData &pd):
332 DataFile::ObjectLoader<ProgramData>(pd)
334 add("uniform1i", &Loader::uniform1i);
335 add("uniform1f", &Loader::uniform1f);
336 add("uniform2f", &Loader::uniform2f);
337 add("uniform3f", &Loader::uniform3f);
338 add("uniform4f", &Loader::uniform4f);
341 void ProgramData::Loader::uniform1i(const string &n, int v)
346 void ProgramData::Loader::uniform1f(const string &n, float v)
351 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
353 obj.uniform(n, v0, v1);
356 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
358 obj.uniform(n, v0, v1, v2);
361 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
363 obj.uniform(n, v0, v1, v2, v3);