X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Fprogramdata.cpp;h=43bcbf3febea57702fb6be343818b01d9d472325;hp=d934b48782c23ce181a5022b5fcfeb6009ad83b4;hb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14;hpb=4124807fcacedc8317bd109f056d48e077d0c12f diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index d934b487..43bcbf3f 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -1,15 +1,13 @@ #include -#include #include -#include #include #include "buffer.h" #include "color.h" #include "error.h" #include "matrix.h" +#include "pipelinestate.h" #include "program.h" #include "programdata.h" -#include "uniform.h" #include "uniformblock.h" #include "vector.h" @@ -20,6 +18,7 @@ namespace GL { ProgramData::ProgramData(const Program *p): tied_program(p), + generation(0), last_buffer_block(0), buffer(0), dirty(0) @@ -29,13 +28,12 @@ ProgramData::ProgramData(const Program *p): ProgramData::ProgramData(const ProgramData &other): tied_program(other.tied_program), uniforms(other.uniforms), + uniform_data(other.uniform_data), + 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(); -} +{ } ProgramData::ProgramData(const ProgramData &other, const Program *p): tied_program(p), @@ -45,13 +43,12 @@ 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(); + uniform_data = other.uniform_data; } ProgramData &ProgramData::operator=(const ProgramData &other) @@ -59,11 +56,11 @@ 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(); + uniform_data = other.uniform_data; - 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,76 +72,56 @@ 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(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; } -void ProgramData::uniform(Tag tag, Uniform *uni) +void ProgramData::uniform(Tag tag, DataType type, unsigned array_size, const void *value) { - try + if(!validate_tag(tag)) + return; + + auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag); + if(i==uniforms.end() || i->tag!=tag) { - if(!validate_tag(tag)) - { - delete uni; - return; - } + if(uniforms.size()>=MASK_BITS) + throw too_many_uniforms(tag.str()); + + TaggedUniform tu; + tu.tag = tag; + tu.type = type; + tu.array_size = array_size; + tu.data_offset = uniform_data.size(); + tu.data_size = array_size*get_type_size(type); + i = uniforms.insert(i, tu); + uniform_data.resize(tu.data_offset+tu.data_size); + + mark_dirty(ALL_ONES); } - catch(...) + else if(type!=i->type) + throw invalid_operation("ProgramData::uniform"); + else if(array_size>i->array_size) { - delete uni; - throw; + unsigned add_bytes = (array_size-i->array_size)*get_type_size(type); + uniform_data.insert(uniform_data.begin()+i->data_offset+i->data_size, add_bytes, 0); + for(TaggedUniform &u: uniforms) + if(u.data_offset>i->data_offset) + u.data_offset += add_bytes; + i->array_size = array_size; + i->data_size = array_size*get_type_size(type); } - int i = find_uniform_index(tag); - if(i<0) - return add_uniform(tag, uni); + const char *val_begin = static_cast(value); + const char *val_end = val_begin+array_size*get_type_size(type); + char *store_begin = uniform_data.data()+i->data_offset; + copy(val_begin, val_end, store_begin); - uniforms[i].replace_value(uni); - dirty |= 1< -void ProgramData::uniform(Tag tag, V value) -{ - if(!validate_tag(tag)) - return; - - int i = find_uniform_index(tag); - if(i<0) - return add_uniform(tag, new T(value)); - - if(T *uni = dynamic_cast(uniforms[i].value)) - uni->set(value); - else - uniforms[i].replace_value(new T(value)); - - dirty |= 1< -void ProgramData::uniform_array(Tag tag, unsigned n, V value) -{ - if(!validate_tag(tag)) - return; - - int i = find_uniform_index(tag); - if(i<0) - return add_uniform(tag, new UniformArray(n, value)); - - UniformArray *uni = dynamic_cast *>(uniforms[i].value); - if(uni && n==uni->size()) - uni->set(value); - else - uniforms[i].replace_value(new UniformArray(n, value)); - - dirty |= 1<get_uniform_info(tag); + const ReflectData::UniformInfo &info = tied_program->get_uniform_info(tag); if(is_image(info.type)) throw invalid_operation("ProgramData::uniform"); } @@ -170,37 +147,21 @@ bool ProgramData::validate_tag(Tag tag) const #endif } -void ProgramData::add_uniform(Tag tag, Uniform *uni) -{ - if(uniforms.size()>=MASK_BITS) - { - delete uni; - throw too_many_uniforms(tag.str()); - } - - vector::iterator j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); - - TaggedUniform nu; - nu.tag = tag; - nu.value = uni; - uniforms.insert(j, nu); - - dirty = ALL_ONES; -} - -void ProgramData::uniform(Tag tag, const Uniform &u) +void ProgramData::mark_dirty(Mask bits) { - uniform(tag, u.clone()); + if(!dirty) + ++generation; + dirty |= bits; } void ProgramData::uniform(Tag tag, int v) { - uniform(tag, v); + uniform(tag, INT, 1, &v); } void ProgramData::uniform(Tag tag, float v) { - uniform(tag, v); + uniform(tag, FLOAT, 1, &v); } void ProgramData::uniform(Tag tag, int v0, int v1) @@ -217,12 +178,12 @@ void ProgramData::uniform(Tag tag, float v0, float v1) void ProgramData::uniform2(Tag tag, const int *v) { - uniform(tag, v); + uniform(tag, INT_VEC2, 1, v); } void ProgramData::uniform2(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_VEC2, 1, v); } void ProgramData::uniform(Tag tag, int v0, int v1, int v2) @@ -239,12 +200,12 @@ void ProgramData::uniform(Tag tag, float v0, float v1, float v2) void ProgramData::uniform3(Tag tag, const int *v) { - uniform(tag, v); + uniform(tag, INT_VEC3, 1, v); } void ProgramData::uniform3(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_VEC3, 1, v); } void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3) @@ -266,52 +227,52 @@ void ProgramData::uniform(Tag tag, const Color &c) void ProgramData::uniform4(Tag tag, const int *v) { - uniform(tag, v); + uniform(tag, INT_VEC4, 1, v); } void ProgramData::uniform4(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_VEC4, 1, v); } void ProgramData::uniform_matrix2(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT2, 1, v); } void ProgramData::uniform_matrix3x2(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT3x2, 1, v); } void ProgramData::uniform_matrix4x2(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT4x2, 1, v); } void ProgramData::uniform_matrix2x3(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT2x3, 1, v); } void ProgramData::uniform_matrix3(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT3, 1, v); } void ProgramData::uniform_matrix4x3(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT4x3, 1, v); } void ProgramData::uniform_matrix2x4(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT2x4, 1, v); } void ProgramData::uniform_matrix3x4(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT3x4, 1, v); } void ProgramData::uniform(Tag tag, const Matrix &m) @@ -321,170 +282,168 @@ void ProgramData::uniform(Tag tag, const Matrix &m) void ProgramData::uniform_matrix4(Tag tag, const float *v) { - uniform(tag, v); + uniform(tag, FLOAT_MAT4, 1, v); } void ProgramData::uniform_array(Tag tag, unsigned n, const int *v) { - uniform_array(tag, n, v); + uniform(tag, INT, n, v); } void ProgramData::uniform_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT, n, v); } void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v) { - uniform_array(tag, n, v); + uniform(tag, INT, n, v); } void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT, n, v); } void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v) { - uniform_array(tag, n, v); + uniform(tag, INT_VEC2, n, v); } void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_VEC2, n, v); } void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v) { - uniform_array(tag, n, v); + uniform(tag, INT_VEC3, n, v); } void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_VEC3, n, v); } void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v) { - uniform_array(tag, n, v); + uniform(tag, INT_VEC4, n, v); } void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_VEC4, n, v); } void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT2, n, v); } void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT3x2, n, v); } void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT4x2, n, v); } void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT2x3, n, v); } void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT3, n, v); } void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT4x3, n, v); } void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT2x4, n, v); } void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT3x4, n, v); } void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v) { - uniform_array(tag, n, v); + uniform(tag, FLOAT_MAT4, n, 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; + uniform_data.erase(uniform_data.begin()+i->data_offset, uniform_data.begin()+i->data_offset+i->data_size); + for(TaggedUniform &u: uniforms) + if(u.data_offset>i->data_offset) + u.data_offset -= i->data_size; 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; } -const Uniform &ProgramData::get_uniform(Tag tag) const +void ProgramData::copy_uniform(const ProgramData &source, Tag tag) { - int i = find_uniform_index(tag); + int i = source.find_uniform_index(tag); if(i<0) throw key_error(tag); - return *uniforms[i].value; -} - -const Uniform *ProgramData::find_uniform(Tag tag) const -{ - int i = find_uniform_index(tag); - return (i>=0 ? uniforms[i].value : 0); + const TaggedUniform &tu = source.uniforms[i]; + uniform(tag, tu.type, tu.array_size, source.uniform_data.data()+tu.data_offset); } 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); + ReflectData::LayoutHash prog_hash = prog.get_uniform_layout_hash(); + auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash); if(i!=programs.end() && i->prog_hash==prog_hash) return i; - const vector &block_infos = prog.get_uniform_blocks(); + const vector &block_infos = prog.get_uniform_blocks(); unsigned index = i-programs.begin(); programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash)); /* Block indices may change if new shared blocks need to be inserted. Store the hashes so they can be matched up later. */ - vector block_hashes; + 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 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 +457,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 @@ -508,14 +467,14 @@ vector::iterator ProgramData::get_program(const Progr return programs.begin()+index; } -void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const +void ProgramData::update_block_uniform_indices(SharedBlock &block, const ReflectData::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; @@ -553,42 +512,44 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program if(block.used && !block.block) { + block.block = new UniformBlock(info); 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; - bb_block->use_buffer(buffer, last_buffer_block); - last_buffer_block = bb_block; + block.block->use_buffer(buffer, last_buffer_block); + last_buffer_block = block.block; } - else - block.block = new DefaultUniformBlock; } } -void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const +void ProgramData::update_block(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const { - const UInt8 *indices = block.get_uniform_indices(); + const uint8_t *indices = block.get_uniform_indices(); for(unsigned i=0; itype)) ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData else if(indices[i]!=0xFF) - block.block->attach(*info.uniforms[i], *uniforms[indices[i]].value); + { + const TaggedUniform &tu = uniforms[indices[i]]; + block.block->store(*info.uniforms[i], tu.array_size, uniform_data.data()+tu.data_offset); + } } } -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(); + UniformBlock *old_last_block = last_buffer_block; + 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 +560,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 ReflectData::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 ReflectData::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 +612,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,27 +626,35 @@ void ProgramData::apply() const } } - for(vector::iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i) - if(i->block) - i->block->apply(i->bind_point); + return prog_begin; } +void ProgramData::apply(const Program &prog, PipelineState &state) const +{ + auto prog_begin = prepare_program(prog); + ReflectData::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) + { + state.set_uniform_block(i->bind_point, i->block); + if(i->bind_point>=0) + i->block->refresh(); + } +} -ProgramData::TaggedUniform::TaggedUniform(): - value(0) -{ } - -void ProgramData::TaggedUniform::replace_value(Uniform *v) +void ProgramData::set_debug_name(const string &name) { - /* 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; +#ifdef DEBUG + debug_name = name; + if(buffer) + buffer->set_debug_name(name); +#else + (void)name; +#endif } -ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h): +ProgramData::SharedBlock::SharedBlock(ReflectData::LayoutHash h): block_hash(h), used(0), dirty(0), @@ -692,13 +663,13 @@ 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); } -ProgramData::ProgramBlock::ProgramBlock(Program::LayoutHash h): +ProgramData::ProgramBlock::ProgramBlock(ReflectData::LayoutHash h): prog_hash(h), bind_point(-1), block_index(-1)