X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Finstancearray.cpp;h=0a3d6b685ad3ffb98b9ee6265db0eba2c3328431;hp=a03fbf20dff9ca2766c15067087389e0d2902fd3;hb=274b6fb1b02692cc422bad9040d1abe5d545505a;hpb=c737d292d49d2d39b44e1cd903c1f8b79174f0d7 diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index a03fbf20..0a3d6b68 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -14,8 +14,10 @@ using namespace std; namespace Msp { namespace GL { -InstanceArray::InstanceArray(const Object &o): - object(o) +InstanceArrayBase::InstanceArrayBase(const Object &o, size_t s): + object(o), + instance_size(s), + default_count(max(4096U/instance_size, 8U)) { const Technique *tech = object.get_technique(); for(const auto &kvp: tech->get_methods()) @@ -46,53 +48,133 @@ InstanceArray::InstanceArray(const Object &o): vtx_setup.set_instance_array(instance_data); } -InstanceArray::~InstanceArray() +InstanceArrayBase::~InstanceArrayBase() { - for(ObjectInstance *i: instances) - delete i; delete instance_buffer; + for(Block &b: storage) + delete[] b.begin; } -void InstanceArray::append(ObjectInstance *inst) +void InstanceArrayBase::add_block(size_t count) { - instances.push_back(inst); - if(instance_data.size()0) + slots[last_free].next_free = base; + else + { + first_free = base; + last_free = slots.size()-1; + } + + slots.back().next_free = first_free; +} + +size_t InstanceArrayBase::allocate() +{ + if(first_free<0) + add_block(default_count); + + size_t index = first_free; + Slot &slot = slots[index]; + if(first_free==last_free) + { + first_free = -1; + last_free = -1; + } + else + { + first_free = slot.next_free; + slots[last_free].next_free = first_free; + } + + slot.used = true; + slot.array_index = instance_count++; + if(instance_data.size()get_size()>0 && instance_buffer->get_size()=b.begin && addrget_matrix(); + Slot &slot = slots[index]; + + --instance_count; + if(slot.array_index(instance_data.modify(index)+matrix_offset); + slot.used = false; + if(first_free>=0) + { + slot.next_free = first_free; + slots[last_free].next_free = index; + } + else + { + slot.next_free = index; + first_free = index; + } + last_free = index; +} + +void InstanceArrayBase::update_instance_matrix(size_t index, const Matrix &matrix) +{ + float *d = reinterpret_cast(instance_data.modify(slots[index].array_index)+matrix_offset); for(unsigned i=0; i<12; ++i) - d[i] = m(i/4, i%4); + d[i] = matrix(i/4, i%4); } -void InstanceArray::render(Renderer &renderer, Tag tag) const +void InstanceArrayBase::render(Renderer &renderer, Tag tag) const { - if(instances.empty()) + if(!instance_count) return; const Technique *tech = object.get_technique(); @@ -109,7 +191,7 @@ void InstanceArray::render(Renderer &renderer, Tag tag) const Renderer::Push push(renderer); renderer.set_pipeline_key(this, tag.id); method->apply(renderer); - mesh->draw_instanced(renderer, vtx_setup, instances.size()); + mesh->draw_instanced(renderer, vtx_setup, instance_count); } } // namespace GL