X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Fprogramdata.cpp;h=e7c171754aed006dab1ecbdcedd272ddd894a396;hb=016f0f0dd51511f98d0bf398d99199d7dec1543c;hp=377b5ace09eb9632eadac8410bfa675bd5f9a13f;hpb=8366f8951984aa436938b1bb18a57067ff2260c1;p=libs%2Fgl.git diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index 377b5ace..e7c17175 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -1,7 +1,5 @@ #include -#include #include -#include #include #include "buffer.h" #include "color.h" @@ -10,7 +8,6 @@ #include "pipelinestate.h" #include "program.h" #include "programdata.h" -#include "uniform.h" #include "uniformblock.h" #include "vector.h" @@ -20,67 +17,27 @@ namespace Msp { namespace GL { ProgramData::ProgramData(const Program *p): - tied_program(p), - generation(0), - last_buffer_block(0), - buffer(0), - dirty(0) + tied_program(p) { } -// Blocks are intentionally left uncopied -ProgramData::ProgramData(const ProgramData &other): +ProgramData::ProgramData(ProgramData &&other): tied_program(other.tied_program), - uniforms(other.uniforms), + uniforms(move(other.uniforms)), + uniform_data(move(other.uniform_data)), generation(other.generation), - last_buffer_block(0), - buffer(0), - dirty(0) + blocks(move(other.blocks)), + programs(move(other.programs)), + last_buffer_block(other.last_buffer_block), + buffer(other.buffer), + dirty(other.dirty), + debug_name(move(other.debug_name)) { - for(TaggedUniform &u: uniforms) - u.value = u.value->clone(); -} - -ProgramData::ProgramData(const ProgramData &other, const Program *p): - tied_program(p), - last_buffer_block(0), - buffer(0), - dirty(0) -{ - if(tied_program) - { - for(const TaggedUniform &u: other.uniforms) - validate_tag(u.tag); - } - - uniforms = other.uniforms; - for(TaggedUniform &u: uniforms) - u.value = u.value->clone(); -} - -ProgramData &ProgramData::operator=(const ProgramData &other) -{ - tied_program = other.tied_program; - - uniforms = other.uniforms; - for(TaggedUniform &u: uniforms) - u.value = u.value->clone(); - - for(SharedBlock &b: blocks) - delete b.block; - blocks.clear(); - programs.clear(); - - last_buffer_block = 0; - buffer = 0; - dirty = 0; - - return *this; + other.blocks.clear(); + other.buffer = 0; } ProgramData::~ProgramData() { - for(TaggedUniform &u: uniforms) - delete u.value; for(SharedBlock &b: blocks) { if(b.indices.type_flag==0xFE) @@ -90,65 +47,47 @@ ProgramData::~ProgramData() 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); - - uniforms[i].replace_value(uni); - mark_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)); + 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); - mark_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)); - - mark_dirty(1<=MASK_BITS) - { - delete uni; - throw too_many_uniforms(tag.str()); - } - - auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); - - TaggedUniform nu; - nu.tag = tag; - nu.value = uni; - uniforms.insert(j, nu); - - mark_dirty(ALL_ONES); -} - void ProgramData::mark_dirty(Mask bits) { if(!dirty) + { + if(generation && !streaming) + { + streaming = true; + if(buffer && buffer->get_size()) + recreate_buffer(); + } ++generation; + } dirty |= bits; } -void ProgramData::uniform(Tag tag, const Uniform &u) +void ProgramData::uniform(Tag tag, int v) { - uniform(tag, u.clone()); + uniform(tag, INT, 1, &v); } -void ProgramData::uniform(Tag tag, int v) +void ProgramData::uniform(Tag tag, unsigned v) { - uniform(tag, v); + uniform(tag, UNSIGNED_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) @@ -220,6 +149,12 @@ void ProgramData::uniform(Tag tag, int v0, int v1) uniform2(tag, va); } +void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1) +{ + unsigned va[2] = { v0, v1 }; + uniform2(tag, va); +} + void ProgramData::uniform(Tag tag, float v0, float v1) { float va[2] = { v0, v1 }; @@ -228,12 +163,17 @@ 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 unsigned *v) +{ + uniform(tag, UINT_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) @@ -242,6 +182,12 @@ void ProgramData::uniform(Tag tag, int v0, int v1, int v2) uniform3(tag, va); } +void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1, unsigned v2) +{ + unsigned va[3] = { v0, v1, v2 }; + uniform3(tag, va); +} + void ProgramData::uniform(Tag tag, float v0, float v1, float v2) { float va[3] = { v0, v1, v2 }; @@ -250,12 +196,17 @@ 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 unsigned *v) +{ + uniform(tag, UINT_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) @@ -264,6 +215,12 @@ void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3) uniform4(tag, va); } +void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1, unsigned v2, unsigned v3) +{ + unsigned va[4] = { v0, v1, v2, v3 }; + uniform4(tag, va); +} + void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3) { float va[4] = { v0, v1, v2, v3 }; @@ -277,52 +234,57 @@ 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 unsigned *v) +{ + uniform(tag, UINT_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) @@ -332,102 +294,127 @@ 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 unsigned *v) +{ + uniform(tag, UNSIGNED_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 unsigned *v) +{ + uniform(tag, UNSIGNED_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 unsigned *v) +{ + uniform(tag, UINT_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 unsigned *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 unsigned *v) +{ + uniform(tag, UINT_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) @@ -436,7 +423,10 @@ void ProgramData::remove_uniform(Tag 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); mark_dirty(ALL_ONES); @@ -451,18 +441,19 @@ vector ProgramData::get_uniform_tags() const 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 TaggedUniform &tu = source.uniforms[i]; + uniform(tag, tu.type, tu.array_size, source.uniform_data.data()+tu.data_offset); } -const Uniform *ProgramData::find_uniform(Tag tag) const +void ProgramData::copy_uniforms(const ProgramData &source) { - int i = find_uniform_index(tag); - return (i>=0 ? uniforms[i].value : 0); + for(const TaggedUniform &u: source.uniforms) + uniform(u.tag, u.type, u.array_size, source.uniform_data.data()+u.data_offset); } int ProgramData::find_uniform_index(Tag tag) const @@ -519,6 +510,21 @@ vector::iterator ProgramData::get_program(const Progr return programs.begin()+index; } +void ProgramData::recreate_buffer() const +{ + Buffer *old_buffer = buffer; + // Create the new buffer first to ensure it has a different address + buffer = new Buffer; + delete old_buffer; + if(last_buffer_block) + last_buffer_block->change_buffer(buffer); + +#ifdef DEBUG + if(!debug_name.empty()) + buffer->set_debug_name(debug_name); +#endif +} + void ProgramData::update_block_uniform_indices(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const { uint8_t *indices = block.indices.values; @@ -568,14 +574,7 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Reflect if(info.bind_point>=0) { if(!buffer) - { - buffer = new Buffer(); - -#ifdef DEBUG - if(!debug_name.empty()) - buffer->set_debug_name(debug_name); -#endif - } + recreate_buffer(); block.block->use_buffer(buffer, last_buffer_block); last_buffer_block = block.block; @@ -591,7 +590,10 @@ void ProgramData::update_block(SharedBlock &block, const ReflectData::UniformBlo if(is_image(info.uniforms[i]->type)) ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData else if(indices[i]!=0xFF) - block.block->store(*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); + } } } @@ -653,24 +655,15 @@ vector::const_iterator ProgramData::prepare_program(c prog_begin->masks.dirty = 0; - if(last_buffer_block!=old_last_block) + if(last_buffer_block!=old_last_block || (buffer && !buffer->get_size())) { - unsigned required_size = last_buffer_block->get_required_buffer_size(); + unsigned required_size = last_buffer_block->get_required_buffer_size(streaming); if(last_buffer_block->get_required_buffer_size()>buffer->get_size()) { if(buffer->get_size()>0) - { - delete buffer; - buffer = new Buffer(); - last_buffer_block->change_buffer(buffer); - -#ifdef DEBUG - if(!debug_name.empty()) - buffer->set_debug_name(debug_name); -#endif - } + recreate_buffer(); - buffer->storage(required_size); + buffer->storage(required_size, (streaming ? STREAMING : STATIC)); } } } @@ -678,16 +671,17 @@ vector::const_iterator ProgramData::prepare_program(c return prog_begin; } -void ProgramData::apply(const Program &prog, PipelineState &state) const +void ProgramData::apply(const Program &prog, PipelineState &state, unsigned frame) 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(); + i->block->refresh(frame); } } @@ -703,20 +697,6 @@ void ProgramData::set_debug_name(const string &name) } -ProgramData::TaggedUniform::TaggedUniform(): - value(0) -{ } - -void ProgramData::TaggedUniform::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(ReflectData::LayoutHash h): block_hash(h), used(0),