From c26d3ecfe9779e9cce13edfd981246c146c6f3a1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 22 Apr 2023 12:14:26 +0100 Subject: Streamline the instancing maintenance --- gfx/gl/instanceVertices.h | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/instanceVertices.h b/gfx/gl/instanceVertices.h index 036b48e..cf4f0ae 100644 --- a/gfx/gl/instanceVertices.h +++ b/gfx/gl/instanceVertices.h @@ -25,7 +25,7 @@ public: ~InstanceProxy() { if (instances) { - instances->release(*this); + instances->release(index); } } @@ -33,7 +33,7 @@ public: operator=(InstanceProxy && other) { if (instances) { - instances->release(*this); + instances->release(index); } instances = std::exchange(other.instances, nullptr); index = other.index; @@ -87,8 +87,6 @@ public: } private: - friend InstanceVertices; - InstanceVertices * instances; std::size_t index; }; @@ -131,23 +129,19 @@ protected: friend InstanceProxy; void - release(const InstanceProxy & p) + release(const size_t pidx) { // Destroy p's object - at(p.index).~T(); - if (next-- > index[p.index]) { + at(pidx).~T(); + if (next-- > index[pidx]) { // Remember p.index is free index now - unused.push_back(p.index); + unused.push_back(pidx); // Move last object into p's slot - new (&at(p.index)) T {std::move(data[next])}; + new (&at(pidx)) T {std::move(data[next])}; (data[next]).~T(); - for (auto & i : index) { - if (i == next) { - i = index[p.index]; - break; - } - } - index[p.index] = 100000; + *std::find(index.begin(), index.end(), next) = index[pidx]; + // Not strictly required, but needed for uniqueness unit test assertion + index[pidx] = static_cast(-1); } else { index.pop_back(); -- cgit v1.2.3