+#include <msp/core/maputils.h>
+#include <msp/debug/demangle.h>
#include <msp/gl/extensions/arb_direct_state_access.h>
+#include <msp/io/print.h>
#include "buffer.h"
#include "color.h"
#include "error.h"
// Blocks are intentionally left uncopied
ProgramData::ProgramData(const ProgramData &other):
- tied_program(0),
- uniform_slots(other.uniform_slots),
+ tied_program(other.tied_program),
uniforms(other.uniforms),
last_block(0),
buffer(0),
dirty(0)
{
- for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- *i = (*i)->clone();
+ for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ i->value = i->value->clone();
}
-ProgramData &ProgramData::operator=(const ProgramData &other)
+ProgramData::ProgramData(const ProgramData &other, const Program *p):
+ tied_program(p),
+ last_block(0),
+ buffer(0),
+ dirty(0)
{
- for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- delete *i;
- uniforms.clear();
+ if(tied_program)
+ {
+ for(vector<NamedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
+ tied_program->get_uniform_info(i->name);
+ }
+
+ uniforms = other.uniforms;
+ for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ i->value = i->value->clone();
+}
+ProgramData &ProgramData::operator=(const ProgramData &other)
+{
tied_program = other.tied_program;
- uniform_slots = other.uniform_slots;
- for(vector<Uniform *>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
- uniforms.push_back((*i)->clone());
+ uniforms = other.uniforms;
+ for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ i->value = i->value->clone();
for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
delete i->second.block;
ProgramData::~ProgramData()
{
- for(vector<Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- delete *i;
+ for(vector<NamedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ delete i->value;
for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ {
+ if(i->second.indices.type_flag==0xFE)
+ delete i->second.indices.dynamic.values;
delete i->second.block;
+ }
delete buffer;
}
{
try
{
- if(tied_program)
- tied_program->get_uniform_info(name);
- else if(name[name.size()-1]==']')
- throw invalid_argument("ProgramData::uniform");
+ if(!validate_name(name))
+ {
+ delete uni;
+ return;
+ }
}
catch(...)
{
throw;
}
- SlotMap::iterator i = uniform_slots.find(name);
- if(i!=uniform_slots.end())
+ int i = find_uniform_index(name);
+ if(i<0)
+ return add_uniform(name, uni);
+
+ uniforms[i].replace_value(uni);
+ dirty |= 1<<i;
+}
+
+template<typename T, typename V>
+void ProgramData::uniform(const string &name, V value)
+{
+ if(!validate_name(name))
+ return;
+
+ int i = find_uniform_index(name);
+ if(i<0)
+ return add_uniform(name, new T(value));
+
+ if(T *uni = dynamic_cast<T *>(uniforms[i].value))
+ uni->set(value);
+ else
+ uniforms[i].replace_value(new T(value));
+
+ dirty |= 1<<i;
+}
+
+template<typename T, typename V>
+void ProgramData::uniform_array(const string &name, unsigned n, V value)
+{
+ if(!validate_name(name))
+ return;
+
+ int i = find_uniform_index(name);
+ if(i<0)
+ return add_uniform(name, new UniformArray<T>(n, value));
+
+ UniformArray<T> *uni = dynamic_cast<UniformArray<T> *>(uniforms[i].value);
+ if(uni && n==uni->size())
+ uni->set(value);
+ else
+ uniforms[i].replace_value(new UniformArray<T>(n, value));
+
+ dirty |= 1<<i;
+}
+
+bool ProgramData::validate_name(const string &name) const
+{
+#ifdef DEBUG
+ try
+#endif
{
- Uniform *&slot = uniforms[i->second];
- /* UniformBlock does not copy the uniforms, so existing default blocks
- will be left with stale pointers. This is not a problem as long as no
- one stores pointers to the blocks and expects them to stay valid. */
- delete slot;
- slot = uni;
-
- if(i->second<MASK_BITS)
- dirty |= 1<<i->second;
- else // Force a full update if the mask isn't wide enough
- dirty = ALL_ONES;
+ if(tied_program)
+ tied_program->get_uniform_info(name);
+ else if(name[name.size()-1]==']')
+ throw invalid_argument("ProgramData::uniform");
+ return true;
}
- else
+#ifdef DEBUG
+ catch(const exception &e)
{
- uniform_slots[name] = uniforms.size();
- uniforms.push_back(uni);
- dirty = ALL_ONES;
+ IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what());
+ return false;
}
+#endif
+}
+
+void ProgramData::add_uniform(const string &name, Uniform *uni)
+{
+ if(uniforms.size()>=MASK_BITS)
+ {
+ delete uni;
+ throw too_many_uniforms(name);
+ }
+
+ vector<NamedUniform>::iterator j = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
+
+ NamedUniform nu;
+ nu.name = name;
+ nu.value = uni;
+ uniforms.insert(j, nu);
+
+ dirty = ALL_ONES;
}
void ProgramData::uniform(const string &name, const Uniform &u)
void ProgramData::uniform(const string &name, int v)
{
- uniform(name, new Uniform1i(v));
+ uniform<Uniform1i>(name, v);
}
void ProgramData::uniform(const string &name, float v)
{
- uniform(name, new Uniform1f(v));
+ uniform<Uniform1f>(name, v);
}
void ProgramData::uniform(const string &name, int v0, int v1)
void ProgramData::uniform2(const string &name, const int *v)
{
- uniform(name, new Uniform2i(v));
+ uniform<Uniform2i>(name, v);
}
void ProgramData::uniform2(const string &name, const float *v)
{
- uniform(name, new Uniform2f(v));
+ uniform<Uniform2f>(name, v);
}
void ProgramData::uniform(const string &name, int v0, int v1, int v2)
void ProgramData::uniform3(const string &name, const int *v)
{
- uniform(name, new Uniform3i(v));
+ uniform<Uniform3i>(name, v);
}
void ProgramData::uniform3(const string &name, const float *v)
{
- uniform(name, new Uniform3f(v));
+ uniform<Uniform3f>(name, v);
}
void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
void ProgramData::uniform4(const string &name, const int *v)
{
- uniform(name, new Uniform4i(v));
+ uniform<Uniform4i>(name, v);
}
void ProgramData::uniform4(const string &name, const float *v)
{
- uniform(name, new Uniform4f(v));
+ uniform<Uniform4f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
void ProgramData::uniform_matrix2(const string &name, const float *v)
{
- uniform(name, new UniformMatrix2x2f(v));
+ uniform<UniformMatrix2x2f>(name, v);
+}
+
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 3> &m)
+{
+ uniform_matrix3x2(name, &m(0, 0));
+}
+
+void ProgramData::uniform_matrix3x2(const string &name, const float *v)
+{
+ uniform<UniformMatrix3x2f>(name, v);
+}
+
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 4> &m)
+{
+ uniform_matrix4x2(name, &m(0, 0));
+}
+
+void ProgramData::uniform_matrix4x2(const string &name, const float *v)
+{
+ uniform<UniformMatrix4x2f>(name, v);
+}
+
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 2> &m)
+{
+ uniform_matrix2x3(name, &m(0, 0));
+}
+
+void ProgramData::uniform_matrix2x3(const string &name, const float *v)
+{
+ uniform<UniformMatrix2x3f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
void ProgramData::uniform_matrix3(const string &name, const float *v)
{
- uniform(name, new UniformMatrix3x3f(v));
+ uniform<UniformMatrix3x3f>(name, v);
+}
+
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 4> &m)
+{
+ uniform_matrix4x3(name, &m(0, 0));
+}
+
+void ProgramData::uniform_matrix4x3(const string &name, const float *v)
+{
+ uniform<UniformMatrix4x3f>(name, v);
+}
+
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 2> &m)
+{
+ uniform_matrix2x4(name, &m(0, 0));
+}
+
+void ProgramData::uniform_matrix2x4(const string &name, const float *v)
+{
+ uniform<UniformMatrix2x4f>(name, v);
+}
+
+void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 3> &m)
+{
+ uniform_matrix3x4(name, &m(0, 0));
+}
+
+void ProgramData::uniform_matrix3x4(const string &name, const float *v)
+{
+ uniform<UniformMatrix3x4f>(name, v);
}
void ProgramData::uniform(const string &name, const Matrix &m)
void ProgramData::uniform_matrix4(const string &name, const float *v)
{
- uniform(name, new UniformMatrix4x4f(v));
+ uniform<UniformMatrix4x4f>(name, v);
}
void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform1i>(n, v));
+ uniform_array<Uniform1i>(name, n, v);
}
void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform1f>(n, v));
+ uniform_array<Uniform1f>(name, n, v);
}
void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform2i>(n, v));
+ uniform_array<Uniform2i>(name, n, v);
}
void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform2f>(n, v));
+ uniform_array<Uniform2f>(name, n, v);
}
void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform3i>(n, v));
+ uniform_array<Uniform3i>(name, n, v);
}
void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform3f>(n, v));
+ uniform_array<Uniform3f>(name, n, v);
}
void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform4i>(n, v));
+ uniform_array<Uniform4i>(name, n, v);
}
void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform4f>(n, v));
+ uniform_array<Uniform4f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix2x2f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix3x2f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix4x2f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix2x3f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix3x3f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix4x3f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix2x4f>(name, n, v);
+}
+
+void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v)
+{
+ uniform_array<UniformMatrix3x4f>(name, n, v);
}
void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
+ uniform_array<UniformMatrix4x4f>(name, n, v);
}
void ProgramData::remove_uniform(const string &name)
{
- SlotMap::iterator i = uniform_slots.find(name);
- if(i!=uniform_slots.end())
- {
- vector<Uniform *>::iterator j = uniforms.begin()+i->second;
- delete *j;
- uniforms.erase(j);
+ vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
+ if(i==uniforms.end() || i->name!=name)
+ return;
+
+ delete i->value;
+ uniforms.erase(i);
- for(SlotMap::iterator k=uniform_slots.begin(); k!=uniform_slots.end(); ++k)
- if(k->second>i->second)
- --k->second;
+ dirty = ALL_ONES;
+}
- uniform_slots.erase(i);
+vector<string> ProgramData::get_uniform_names() const
+{
+ vector<string> names;
+ names.reserve(uniforms.size());
+ for(vector<NamedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ names.push_back(i->name);
+ return names;
+}
- dirty = ALL_ONES;
- }
+const Uniform &ProgramData::get_uniform(const string &name) const
+{
+ int i = find_uniform_index(name);
+ if(i<0)
+ throw key_error(name);
+ return *uniforms[i].value;
}
-unsigned ProgramData::compute_slot_mask(const Program::UniformBlockInfo &block) const
+const Uniform *ProgramData::find_uniform(const string &name) const
{
- unsigned mask = 0;
- for(vector<const Program::UniformInfo *>::const_iterator i=block.uniforms.begin(); i!=block.uniforms.end(); ++i)
- {
- SlotMap::const_iterator j = uniform_slots.find((*i)->name);
- /* TODO issue a warning (or even error?) either here or in update_block
- if all uniforms for a buffer-backed block are not found */
- if(j!=uniform_slots.end() && j->second<MASK_BITS)
- mask |= 1<<j->second;
- }
+ int i = find_uniform_index(name);
+ return (i>=0 ? uniforms[i].value : 0);
+}
+
+bool ProgramData::uniform_name_compare(const NamedUniform &nu, const string &name)
+{
+ return nu.name<name;
+}
- return mask;
+int ProgramData::find_uniform_index(const string &name) const
+{
+ vector<NamedUniform>::const_iterator i = lower_bound(uniforms.begin(), uniforms.end(), name, uniform_name_compare);
+ return ((i!=uniforms.end() && i->name==name) ? i-uniforms.begin() : -1);
}
-void ProgramData::update_block(UniformBlock &block, const Program::UniformBlockInfo &info) const
+void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
{
- for(vector<const Program::UniformInfo *>::const_iterator i=info.uniforms.begin(); i!=info.uniforms.end(); ++i)
+ UInt8 *indices = block.indices.values;
+ if(info.uniforms.size()>16)
{
- SlotMap::const_iterator j = uniform_slots.find((*i)->name);
- if(j!=uniform_slots.end())
- block.attach(**i, *uniforms[j->second]);
+ if(block.indices.type_flag==0xFD)
+ {
+ block.indices.dynamic.values = new UInt8[info.uniforms.size()];
+ block.indices.type_flag = 0xFE;
+ }
+ indices = block.indices.dynamic.values;
}
+
+ block.used = 0;
+ for(unsigned i=0; i<info.uniforms.size(); ++i)
+ {
+ int j = find_uniform_index(info.uniforms[i]->name);
+ if(j>=0)
+ {
+ indices[i] = j;
+ if(static_cast<unsigned>(j)<MASK_BITS)
+ block.used |= 1<<j;
+ }
+ else
+ indices[i] = 0xFF;
+ }
+
+ block.dirty = block.used;
+}
+
+void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
+{
+ const UInt8 *indices = block.get_uniform_indices();
+ for(unsigned i=0; i<info.uniforms.size(); ++i)
+ if(indices[i]!=0xFF)
+ block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value);
}
ProgramData::SharedBlock *ProgramData::get_shared_block(const Program::UniformBlockInfo &info) const
BlockMap::iterator i = blocks.find(info.layout_hash);
if(i==blocks.end())
{
- unsigned used = compute_slot_mask(info);
- if(!used)
+ bool any_found = false;
+ bool all_found = true;
+ for(vector<const Program::UniformInfo *>::const_iterator j=info.uniforms.begin(); j!=info.uniforms.end(); ++j)
+ {
+ if(find_uniform_index((*j)->name)>=0)
+ any_found = true;
+ else
+ all_found = false;
+ }
+
+ if(!any_found)
return 0;
+ else if(!all_found && info.bind_point>=0)
+ {
+#ifdef DEBUG
+ IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
+#else
+ throw incomplete_uniform_block(info.name);
+#endif
+ }
UniformBlock *block;
if(info.bind_point>=0)
{
if(!buffer)
- {
buffer = new Buffer(UNIFORM_BUFFER);
- buffer->set_usage(STREAM_DRAW);
- }
block = new UniformBlock(info.data_size);
block->use_buffer(buffer, last_block);
else
block = new UniformBlock;
- i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(used, block))).first;
+ i = blocks.insert(BlockMap::value_type(info.layout_hash, SharedBlock(block))).first;
+ update_block_uniform_indices(i->second, info);
}
return &i->second;
{
/* If the global dirty flag affects this program, add it to per-program
dirty flags and clear the global flag. A previously unseen program will
- always cause this to happen. */
+ cause this to happen if there's any dirty uniforms. */
if(affected)
{
for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
const Program::UniformBlockMap &prog_blocks = prog->get_uniform_blocks();
+ UniformBlock *old_last_block = last_block;
if(pu.dirty==ALL_ONES)
{
/* The set of uniforms has changed since this program was last used.
if(shared)
{
if(shared->dirty==ALL_ONES)
- shared->used = compute_slot_mask(i->second);
+ update_block_uniform_indices(*shared, i->second);
pu.used |= shared->used;
}
if(!j->shared || !j->shared->dirty)
continue;
- update_block(*j->block, i->second);
+ update_block(*j->shared, i->second);
j->shared->dirty = 0;
buffered_blocks_updated |= (j->bind_point>=0);
}
to avoid state thrashing. */
if(buffered_blocks_updated && !ARB_direct_state_access)
buffer->bind();
+
+ if(last_block!=old_last_block)
+ {
+ unsigned required_size = last_block->get_required_buffer_size();
+ if(last_block->get_required_buffer_size()>buffer->get_size())
+ {
+ if(buffer->get_size()>0)
+ {
+ delete buffer;
+ buffer = new Buffer(UNIFORM_BUFFER);
+ last_block->change_buffer(buffer);
+ }
+
+ buffer->storage(required_size);
+ }
+ }
}
for(vector<ProgramBlock>::iterator i=pu.blocks.begin(); i!=pu.blocks.end(); ++i)
}
-ProgramData::SharedBlock::SharedBlock(unsigned u, UniformBlock *b):
- used(u),
- dirty(u),
- block(b)
+ProgramData::NamedUniform::NamedUniform():
+ value(0)
{ }
+void ProgramData::NamedUniform::replace_value(Uniform *v)
+{
+ /* UniformBlock does not copy the uniforms, so existing default blocks
+ will be left with stale pointers. This is not a problem as long as no
+ one stores pointers to the blocks and expects them to stay valid. */
+ delete value;
+ value = v;
+}
+
+
+ProgramData::SharedBlock::SharedBlock(UniformBlock *b):
+ used(0),
+ dirty(0),
+ block(b)
+{
+ indices.type_flag = 0xFD;
+}
+
+const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
+{
+ return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
+}
+
ProgramData::ProgramBlock::ProgramBlock():
bind_point(-1),
ProgramData::ProgramBlock::ProgramBlock(int p, SharedBlock *b):
bind_point(p),
- block(b ? b->block : 0),
+ block((b && b->used) ? b->block : 0),
shared(b)
{ }