X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Finstancearray.h;h=f2a9b2c00be5930c281a8341851d838af949ba1f;hb=274b6fb1b02692cc422bad9040d1abe5d545505a;hp=0965499fe66f98de6268619a189f80d253d91800;hpb=8544116298d7a1be176f52be48c9952529f5c446;p=libs%2Fgl.git diff --git a/source/render/instancearray.h b/source/render/instancearray.h index 0965499f..f2a9b2c0 100644 --- a/source/render/instancearray.h +++ b/source/render/instancearray.h @@ -2,6 +2,7 @@ #define MSP_GL_INSTANCEARRAY_H_ #include +#include #include "programdata.h" #include "renderable.h" #include "vertexarray.h" @@ -14,67 +15,142 @@ class Buffer; class Object; class ObjectInstance; -/** -Renders multiple instances of an Object in an efficient manner. If instanced -rendering is supported, only one draw call per Batch needs to be issued. - -Changing the Mesh of the Object while an InstanceArray exists is not supported. -*/ -class InstanceArray: public Renderable +class InstanceArrayBase: public Renderable, public NonCopyable { -public: - template - class Instance: public T +private: + struct Block { - private: - InstanceArray &array; - unsigned index; - - public: - Instance(const Object &o, InstanceArray &a, unsigned i): T(o), array(a), index(i) { } + char *begin = 0; + char *end = 0; + }; - virtual void set_matrix(const Matrix &); + struct Slot + { + bool used = false; + union + { + std::uint16_t array_index; + std::uint16_t next_free; + }; + std::uint16_t block_index; + std::uint16_t index_in_block; }; -private: const Object &object; - std::vector instances; VertexArray instance_data; Buffer *instance_buffer = 0; VertexSetup vtx_setup; int matrix_location = -1; unsigned matrix_offset = 0; + std::size_t instance_size; + std::size_t default_count; + std::vector storage; + std::vector slots; + std::vector array_order; + int first_free = -1; + int last_free = -1; + std::size_t instance_count = 0; + +protected: + InstanceArrayBase(const Object &, std::size_t); + ~InstanceArrayBase(); -public: - InstanceArray(const Object &); - ~InstanceArray(); +private: + void add_block(std::size_t); + std::size_t allocate(); + char *get_address(std::size_t) const; + std::size_t find_index(char *) const; + void release(std::size_t); +protected: + template + T *create(A &); - void set_matrix_attribute(const std::string &); + template + void destroy(T *); + + template + void destroy_all(); + + void update_instance_matrix(std::size_t, const Matrix &); - template - T &append(); -private: - void append(ObjectInstance *); - void update_instance_matrix(unsigned); public: - void remove(ObjectInstance &); + std::size_t size() const { return instance_count; } virtual void render(Renderer &, Tag) const; }; +template +inline T *InstanceArrayBase::create(A &array) +{ + size_t index = allocate(); + return new(get_address(index)) T(object, array, index); +} + template -T &InstanceArray::append() +inline void InstanceArrayBase::destroy(T *obj) { - Instance *inst = new Instance(object, *this, instances.size()); - append(inst); - return *inst; + char *addr = reinterpret_cast(obj); + obj->~T(); + release(find_index(addr)); } template -void InstanceArray::Instance::set_matrix(const Matrix &m) +inline void InstanceArrayBase::destroy_all() +{ + for(unsigned i=0; i(get_address(i))->~T(); +} + + +/** +Stores and renders multiple instances of an Object in an efficient manner. + +The instance specific transform is passed to the shader in an attribute with +the name instance_transform. The attribute should have the type vec4[3]. Each +elements of the array corresponds to a row of the transform matrix. + +If the Mesh or Technique of the Object is changed during the lifetime of the +InstanceArray, behaviour is undefined. + +The instance type must have a constructor accepting a const Object &. If it +has a virtual function with the signature void set_matrix(const Matrix &), it +will be used to update the instance matrix. The original function is also +called. + +Instance created by the array have stable addresses. However after an instance +is removed, its address may later be reused for another instance. +*/ +template +class InstanceArray: public InstanceArrayBase +{ +private: + class Instance: public T + { + private: + InstanceArray &array; + unsigned index; + + public: + Instance(const Object &o, InstanceArray &a, unsigned i): T(o), array(a), index(i) { } + + virtual void set_matrix(const Matrix &); + }; + +public: + InstanceArray(const Object &o): InstanceArrayBase(o, sizeof(Instance)) { } + ~InstanceArray() { destroy_all(); } + + T &append() { return *create(*this); } + void remove(T &obj) { destroy(&obj); } +}; + + +template +inline void InstanceArray::Instance::set_matrix(const Matrix &m) { T::set_matrix(m); - array.update_instance_matrix(index); + array.update_instance_matrix(index, *this->get_matrix()); } } // namespace GL