ProgramData::ProgramData():
last_block(0),
buffer(0),
- modified(false)
+ changes(NO_CHANGES)
{
static RequireExtension _ext("GL_ARB_shader_objects");
}
uniforms(other.uniforms),
last_block(0),
buffer(0),
- modified(false)
+ changes(NO_CHANGES)
{
for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
i->second = i->second->clone();
{
delete i->second;
i->second = uni;
+ changes = VALUES_CHANGED;
}
else
+ {
uniforms[name] = uni;
-
- modified = true;
+ changes = KEYS_CHANGED;
+ }
}
void ProgramData::uniform(const string &name, int v)
uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
}
-const UniformBlock &ProgramData::get_block(const Program &prog, const Program::UniformBlockInfo *info) const
+void ProgramData::find_uniforms_for_block(Block &block, const Program::UniformBlockInfo &info) const
{
- if(modified)
+ block.uniforms.clear();
+ for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
+ {
+ // XXX individual array elements
+ UniformMap::const_iterator j = uniforms.find((*i)->name);
+ if(j!=uniforms.end())
+ block.uniforms[(*i)->location] = &j->second;
+ }
+}
+
+UniformBlock *ProgramData::create_block(const Program::UniformBlockInfo &info) const
+{
+ UniformBlock *block = new UniformBlock(info.data_size);
+ if(!buffer)
+ buffer = new Buffer(UNIFORM_BUFFER);
+ block->use_buffer(buffer, last_block);
+ last_block = block;
+ return block;
+}
+
+const UniformBlock *ProgramData::get_block(const Program &prog, const Program::UniformBlockInfo *info) const
+{
+ if(changes)
{
for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
- i->second.dirty = true;
- modified = false;
+ if(i->second.changes<changes)
+ i->second.changes = changes;
+ changes = NO_CHANGES;
}
unsigned layout_hash = (info ? info->layout_hash : prog.get_uniform_layout_hash());
if(i==blocks.end())
{
i = blocks.insert(BlockMap::value_type(layout_hash, Block())).first;
- i->second.dirty = true;
if(info)
{
- i->second.block = new UniformBlock(info->data_size);
- if(!buffer)
- buffer = new Buffer(UNIFORM_BUFFER);
- i->second.block->use_buffer(buffer, last_block);
- last_block = i->second.block;
+ find_uniforms_for_block(i->second, *info);
+
+ if(!i->second.uniforms.empty())
+ {
+ i->second.block = create_block(*info);
+ i->second.changes = VALUES_CHANGED;
+ }
}
else
+ {
i->second.block = new UniformBlock;
+ i->second.changes = VALUES_CHANGED;
+ }
}
+ else if(info && i->second.changes==KEYS_CHANGED)
+ {
+ find_uniforms_for_block(i->second, *info);
+ if(!i->second.uniforms.empty())
+ {
+ if(!i->second.block)
+ i->second.block = create_block(*info);
+ i->second.changes = VALUES_CHANGED;
+ }
+ else
+ i->second.changes = NO_CHANGES;
+ }
+
+ if(!i->second.block)
+ return 0;
UniformBlock &block = *i->second.block;
- if(i->second.dirty)
+ if(i->second.changes)
{
if(info)
{
- for(vector<const Program::UniformInfo *>::const_iterator j=info->uniforms.begin(); j!=info->uniforms.end(); ++j)
+ vector<const Program::UniformInfo *>::const_iterator j = info->uniforms.begin();
+ map<unsigned, const Uniform *const *>::const_iterator k = i->second.uniforms.begin();
+ while(j!=info->uniforms.end() && k!=i->second.uniforms.end())
{
- // XXX individual array elements
- UniformMap::const_iterator k = uniforms.find((*j)->name);
- if(k!=uniforms.end())
- block.attach(**j, *k->second);
+ if(k->first==(*j)->location)
+ {
+ block.attach(**j, **k->second);
+ ++k;
+ }
+ ++j;
}
}
else
block.attach(loc, *j->second);
}
}
- i->second.dirty = false;
+ i->second.changes = NO_CHANGES;
}
- return block;
+ return █
}
-const UniformBlock &ProgramData::get_block(const Program &prog, const string &name) const
+const UniformBlock *ProgramData::get_block(const Program &prog, const string &name) const
{
if(name.empty())
return get_block(prog, 0);
typedef pair<const UniformBlock *, unsigned> ApplyBlock;
list<ApplyBlock> apply_blocks;
for(Program::UniformBlockMap::const_iterator i=prog_blocks.begin(); i!=prog_blocks.end(); ++i)
- {
- const UniformBlock &block = get_block(*prog, &i->second);
- apply_blocks.push_back(make_pair(&block, i->second.bind_point));
- }
+ if(const UniformBlock *block = get_block(*prog, &i->second))
+ apply_blocks.push_back(make_pair(block, i->second.bind_point));
if(buffer)
buffer->bind();
i->first->apply(i->second);
}
- const UniformBlock &block = get_block(*prog, 0);
- block.apply(-1);
+ if(const UniformBlock *block = get_block(*prog, 0))
+ block->apply(-1);
}
ProgramData::Block::Block():
- dirty(false),
+ changes(NO_CHANGES),
block(0)
{ }