X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Fprogramdata.cpp;h=1ef013ddac86e5a6be72643fe711764f01428d30;hb=1fba50491957cdd28cff4082a32764691d8ec473;hp=b21c395cf04c27f1084492294dbd2635993af5df;hpb=f1244e29afd2a36aafc2373d485457b4cb0411ff;p=libs%2Fgl.git diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index b21c395c..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" @@ -35,8 +36,8 @@ ProgramData::ProgramData(const ProgramData &other): 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): @@ -47,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) @@ -61,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; @@ -77,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; } @@ -180,7 +182,7 @@ void ProgramData::add_uniform(Tag tag, Uniform *uni) throw too_many_uniforms(tag.str()); } - vector::iterator j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); + auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); TaggedUniform nu; nu.tag = tag; @@ -430,7 +432,7 @@ 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; @@ -444,8 +446,8 @@ 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; } @@ -465,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; @@ -484,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)); @@ -507,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 @@ -519,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; @@ -586,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)) @@ -596,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; @@ -615,44 +612,46 @@ 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; @@ -679,9 +678,25 @@ 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) @@ -719,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); }