From 1ca23c514f978cd69d0f950cd15b75d099a4c94b Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 22 Jul 2021 14:07:02 -0700 Subject: [PATCH] Instance stealing --- .../backend/instancing/InstanceData.java | 2 +- .../backend/instancing/Instancer.java | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceData.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceData.java index cfd7b5c8f..21eaed753 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceData.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceData.java @@ -4,7 +4,7 @@ import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; public abstract class InstanceData { - protected final Instancer owner; + Instancer owner; boolean dirty; boolean removed; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java index e18c6cefc..cbfc9373c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java @@ -71,12 +71,14 @@ public class Instancer { } public D createInstance() { - D instanceData = factory.create(this); - instanceData.dirty = true; - anyToUpdate = true; - data.add(instanceData); + return _add(factory.create(this)); + } - return instanceData; + public void stealInstance(D inOther) { + if (inOther.owner == this) return; + + inOther.owner.anyToRemove = true; + _add(inOther); } public boolean empty() { @@ -105,6 +107,16 @@ public class Instancer { vao.delete(); } + private D _add(D instanceData) { + instanceData.owner = this; + + instanceData.dirty = true; + anyToUpdate = true; + data.add(instanceData); + + return instanceData; + } + protected void renderSetup() { if (anyToRemove) { removeDeletedInstances(); @@ -215,7 +227,7 @@ public class Instancer { final BitSet removeSet = new BitSet(oldSize); for (int i = 0; i < oldSize; i++) { final D element = this.data.get(i); - if (element.removed) { + if (element.removed || element.owner != this) { removeSet.set(i); removeCount++; }