X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Fprogramdata.cpp;h=1ef013ddac86e5a6be72643fe711764f01428d30;hp=d934b48782c23ce181a5022b5fcfeb6009ad83b4;hb=3a1b9cbe2441ae670a97541dc8ccb0a2860c8302;hpb=4124807fcacedc8317bd109f056d48e077d0c12f diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index d934b487..1ef013dd 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -7,6 +7,7 @@ #include "color.h" #include "error.h" #include "matrix.h" +#include "pipelinestate.h" #include "program.h" #include "programdata.h" #include "uniform.h" @@ -20,6 +21,7 @@ namespace GL { ProgramData::ProgramData(const Program *p): tied_program(p), + generation(0), last_buffer_block(0), buffer(0), dirty(0) @@ -29,12 +31,13 @@ ProgramData::ProgramData(const Program *p): ProgramData::ProgramData(const ProgramData &other): tied_program(other.tied_program), uniforms(other.uniforms), + generation(other.generation), last_buffer_block(0), buffer(0), dirty(0) { - for(vector::iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - i->value = i->value->clone(); + for(TaggedUniform &u: uniforms) + u.value = u.value->clone(); } ProgramData::ProgramData(const ProgramData &other, const Program *p): @@ -45,13 +48,13 @@ ProgramData::ProgramData(const ProgramData &other, const Program *p): { if(tied_program) { - for(vector::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i) - validate_tag(i->tag); + for(const TaggedUniform &u: other.uniforms) + validate_tag(u.tag); } uniforms = other.uniforms; - for(vector::iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - i->value = i->value->clone(); + for(TaggedUniform &u: uniforms) + u.value = u.value->clone(); } ProgramData &ProgramData::operator=(const ProgramData &other) @@ -59,11 +62,12 @@ ProgramData &ProgramData::operator=(const ProgramData &other) tied_program = other.tied_program; uniforms = other.uniforms; - for(vector::iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - i->value = i->value->clone(); + for(TaggedUniform &u: uniforms) + u.value = u.value->clone(); - for(vector::iterator i=blocks.begin(); i!=blocks.end(); ++i) - delete i->block; + for(SharedBlock &b: blocks) + delete b.block; + blocks.clear(); programs.clear(); last_buffer_block = 0; @@ -75,13 +79,13 @@ ProgramData &ProgramData::operator=(const ProgramData &other) ProgramData::~ProgramData() { - for(vector::iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - delete i->value; - for(vector::iterator i=blocks.begin(); i!=blocks.end(); ++i) + for(TaggedUniform &u: uniforms) + delete u.value; + for(SharedBlock &b: blocks) { - if(i->indices.type_flag==0xFE) - delete[] i->indices.dynamic.values; - delete i->block; + if(b.indices.type_flag==0xFE) + delete[] b.indices.dynamic.values; + delete b.block; } delete buffer; } @@ -107,7 +111,7 @@ void ProgramData::uniform(Tag tag, Uniform *uni) return add_uniform(tag, uni); uniforms[i].replace_value(uni); - dirty |= 1< @@ -125,7 +129,7 @@ void ProgramData::uniform(Tag tag, V value) else uniforms[i].replace_value(new T(value)); - dirty |= 1< @@ -144,7 +148,7 @@ void ProgramData::uniform_array(Tag tag, unsigned n, V value) else uniforms[i].replace_value(new UniformArray(n, value)); - dirty |= 1<::iterator j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); + auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); TaggedUniform nu; nu.tag = tag; nu.value = uni; uniforms.insert(j, nu); - dirty = ALL_ONES; + mark_dirty(ALL_ONES); +} + +void ProgramData::mark_dirty(Mask bits) +{ + if(!dirty) + ++generation; + dirty |= bits; } void ProgramData::uniform(Tag tag, const Uniform &u) @@ -421,22 +432,22 @@ void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v) void ProgramData::remove_uniform(Tag tag) { - vector::const_iterator i = lower_bound_member(uniforms, tag, &TaggedUniform::tag); + auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag); if(i==uniforms.end() || i->tag!=tag) return; delete i->value; uniforms.erase(i); - dirty = ALL_ONES; + mark_dirty(ALL_ONES); } vector ProgramData::get_uniform_tags() const { vector tags; tags.reserve(uniforms.size()); - for(vector::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - tags.push_back(i->tag); + for(const TaggedUniform &u: uniforms) + tags.push_back(u.tag); return tags; } @@ -456,14 +467,14 @@ const Uniform *ProgramData::find_uniform(Tag tag) const int ProgramData::find_uniform_index(Tag tag) const { - vector::const_iterator i = lower_bound_member(uniforms, tag, &TaggedUniform::tag); + auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag); return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1); } vector::iterator ProgramData::get_program(const Program &prog) const { Program::LayoutHash prog_hash = prog.get_uniform_layout_hash(); - vector::iterator i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash); + auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash); if(i!=programs.end() && i->prog_hash==prog_hash) return i; @@ -475,8 +486,8 @@ vector::iterator ProgramData::get_program(const Progr the hashes so they can be matched up later. */ vector block_hashes; block_hashes.reserve(programs.size()); - for(vector::iterator j=programs.begin(); j!=programs.end(); ++j) - block_hashes.push_back(j->block_index>=0 ? blocks[j->block_index].block_hash : 0); + for(const ProgramBlock &b: programs) + block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0); for(unsigned j=0; j::iterator ProgramData::get_program(const Progr block_hashes[index+1+j] = info.layout_hash; programs[index+1+j].bind_point = info.bind_point; - vector::iterator k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash); + auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash); if(k==blocks.end() || k->block_hash!=info.layout_hash) { k = blocks.insert(k, SharedBlock(info.layout_hash)); @@ -498,7 +509,7 @@ vector::iterator ProgramData::get_program(const Progr unsigned hash = block_hashes[j]; if(hash) { - vector::const_iterator k = lower_bound_member(blocks, hash, &SharedBlock::block_hash); + auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash); programs[j].block_index = k-blocks.begin(); } else @@ -510,12 +521,12 @@ vector::iterator ProgramData::get_program(const Progr void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const { - UInt8 *indices = block.indices.values; + uint8_t *indices = block.indices.values; if(info.uniforms.size()>16) { if(block.indices.type_flag==0xFD) { - block.indices.dynamic.values = new UInt8[info.uniforms.size()]; + block.indices.dynamic.values = new uint8_t[info.uniforms.size()]; block.indices.type_flag = 0xFE; } indices = block.indices.dynamic.values; @@ -556,7 +567,14 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program if(info.bind_point>=0) { if(!buffer) - buffer = new Buffer(UNIFORM_BUFFER); + { + buffer = new Buffer(); + +#ifdef DEBUG + if(!debug_name.empty()) + buffer->set_debug_name(debug_name); +#endif + } BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size); block.block = bb_block; @@ -570,7 +588,7 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const { - const UInt8 *indices = block.get_uniform_indices(); + const uint8_t *indices = block.get_uniform_indices(); for(unsigned i=0; itype)) @@ -580,15 +598,10 @@ void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockIn } } -void ProgramData::apply() const +vector::const_iterator ProgramData::prepare_program(const Program &prog) const { - const Program *prog = Program::current(); - if(!prog) - throw invalid_operation("ProgramData::apply"); - BufferBackedUniformBlock *old_last_block = last_buffer_block; - vector::iterator prog_begin = get_program(*prog); - Program::LayoutHash prog_hash = prog->get_uniform_layout_hash(); + auto prog_begin = get_program(prog); Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U); Mask affected = (dirty&prog_begin->masks.used) | force_dirty; @@ -599,53 +612,50 @@ void ProgramData::apply() const program will cause this to happen if there's any dirty uniforms. */ if(affected) { - for(vector::iterator i=blocks.begin(); i!=blocks.end(); ++i) - i->dirty |= (dirty&i->used) | force_dirty; - for(vector::iterator i=programs.begin(); i!=programs.end(); ++i) - if(i->block_index<0) - i->masks.dirty |= (dirty&i->masks.used) | force_dirty; + for(SharedBlock &b: blocks) + b.dirty |= (dirty&b.used) | force_dirty; + for(ProgramBlock &b: programs) + if(b.block_index<0) + b.masks.dirty |= (dirty&b.masks.used) | force_dirty; dirty = 0; } - const vector &block_infos = prog->get_uniform_blocks(); + const vector &block_infos = prog.get_uniform_blocks(); if(prog_begin->masks.dirty==ALL_ONES) { /* The set of uniforms has changed since this program was last used. Refresh uniform indices within the program's blocks. */ prog_begin->masks.used = 0; - vector::iterator j = prog_begin+1; - for(vector::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j) + auto j = prog_begin+1; + for(const Program::UniformBlockInfo &b: block_infos) { SharedBlock &shared = blocks[j->block_index]; if(shared.dirty==ALL_ONES) - update_block_uniform_indices(shared, *i); + update_block_uniform_indices(shared, b); prog_begin->masks.used |= shared.used; j->block = (shared.used ? shared.block : 0); + ++j; } } // Update the contents of all dirty blocks. bool buffered_blocks_updated = false; - vector::iterator j = prog_begin+1; - for(vector::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j) + auto j = prog_begin+1; + for(const Program::UniformBlockInfo &b: block_infos) { SharedBlock &shared = blocks[j->block_index]; if(shared.dirty) { - update_block(shared, *i); + update_block(shared, b); shared.dirty = 0; buffered_blocks_updated |= (j->bind_point>=0); } + ++j; } prog_begin->masks.dirty = 0; - /* If any blocks stored in the buffer were updated, bind the buffer here - to avoid state thrashing. */ - if(buffered_blocks_updated && !ARB_direct_state_access) - buffer->bind(); - if(last_buffer_block!=old_last_block) { unsigned required_size = last_buffer_block->get_required_buffer_size(); @@ -654,8 +664,13 @@ void ProgramData::apply() const if(buffer->get_size()>0) { delete buffer; - buffer = new Buffer(UNIFORM_BUFFER); + buffer = new Buffer(); last_buffer_block->change_buffer(buffer); + +#ifdef DEBUG + if(!debug_name.empty()) + buffer->set_debug_name(debug_name); +#endif } buffer->storage(required_size); @@ -663,9 +678,36 @@ void ProgramData::apply() const } } - for(vector::iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i) + return prog_begin; +} + +void ProgramData::apply(const Program &prog, PipelineState &state) const +{ + auto prog_begin = prepare_program(prog); + Program::LayoutHash prog_hash = prog_begin->prog_hash; + for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i) if(i->block) - i->block->apply(i->bind_point); + { + if(i->bind_point<0) + state.set_uniforms(static_cast(i->block)); + else + { + const BufferBackedUniformBlock *block = static_cast(i->block); + block->refresh(); + state.set_uniform_block(i->bind_point, block); + } + } +} + +void ProgramData::set_debug_name(const string &name) +{ +#ifdef DEBUG + debug_name = name; + if(buffer) + buffer->set_debug_name(name); +#else + (void)name; +#endif } @@ -692,7 +734,7 @@ ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h): indices.type_flag = 0xFD; } -const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const +const uint8_t *ProgramData::SharedBlock::get_uniform_indices() const { return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values); }