From 48fdcdb75130d1576b89fe6576a09b7fd850d3cf Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 28 Sep 2024 14:26:15 -0700 Subject: [PATCH] DeDelleetete - Process instancer deletions in parallel - Give DrawManagers a frame plan --- .../flywheel/backend/engine/AbstractInstancer.java | 2 +- .../engine_room/flywheel/backend/engine/DrawManager.java | 8 ++++++++ .../engine_room/flywheel/backend/engine/EngineImpl.java | 3 ++- .../backend/engine/indirect/IndirectCullingGroup.java | 5 ++--- .../backend/engine/indirect/IndirectInstancer.java | 6 +----- .../backend/engine/instancing/InstancedDrawManager.java | 4 +--- .../backend/engine/instancing/InstancedInstancer.java | 7 +------ 7 files changed, 16 insertions(+), 19 deletions(-) diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/AbstractInstancer.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/AbstractInstancer.java index 1911ca1c2..3215d12d2 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/AbstractInstancer.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/AbstractInstancer.java @@ -160,7 +160,7 @@ public abstract class AbstractInstancer implements Instancer deleted.set(index); } - protected void removeDeletedInstances() { + public void removeDeletedInstances() { if (deleted.isEmpty()) { return; } diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/DrawManager.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/DrawManager.java index 20e27976c..f34887f00 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/DrawManager.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/DrawManager.java @@ -10,14 +10,17 @@ import java.util.concurrent.ConcurrentLinkedQueue; import com.mojang.datafixers.util.Pair; +import dev.engine_room.flywheel.api.RenderContext; import dev.engine_room.flywheel.api.backend.Engine; import dev.engine_room.flywheel.api.instance.Instance; import dev.engine_room.flywheel.api.instance.InstanceType; import dev.engine_room.flywheel.api.model.Model; +import dev.engine_room.flywheel.api.task.Plan; import dev.engine_room.flywheel.api.visualization.VisualType; import dev.engine_room.flywheel.backend.FlwBackend; import dev.engine_room.flywheel.backend.engine.embed.Environment; import dev.engine_room.flywheel.backend.engine.embed.EnvironmentStorage; +import dev.engine_room.flywheel.lib.task.ForEachPlan; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.client.resources.model.ModelBakery; @@ -45,6 +48,11 @@ public abstract class DrawManager> { return (AbstractInstancer) instancers.computeIfAbsent(key, this::createAndDeferInit); } + public Plan createFramePlan() { + // Go wide on instancers to process deletions in parallel. + return ForEachPlan.of(() -> new ArrayList<>(instancers.values()), AbstractInstancer::removeDeletedInstances); + } + public void flush(LightStorage lightStorage, EnvironmentStorage environmentStorage) { // Thread safety: flush is called from the render thread after all visual updates have been made, // so there are no:tm: threads we could be racing with. diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/EngineImpl.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/EngineImpl.java index 3a96c4c59..2be107cac 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/EngineImpl.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/EngineImpl.java @@ -50,7 +50,8 @@ public class EngineImpl implements Engine { @Override public Plan createFramePlan() { - return lightStorage.createFramePlan(); + return drawManager.createFramePlan() + .and(lightStorage.createFramePlan()); } @Override diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectCullingGroup.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectCullingGroup.java index 42511d84a..bce48dfbc 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectCullingGroup.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectCullingGroup.java @@ -64,7 +64,6 @@ public class IndirectCullingGroup { int modelIndex = 0; for (var iterator = instancers.iterator(); iterator.hasNext(); ) { var instancer = iterator.next(); - instancer.update(); var instanceCount = instancer.instanceCount(); if (instanceCount == 0) { @@ -73,7 +72,7 @@ public class IndirectCullingGroup { continue; } - instancer.postUpdate(modelIndex, instanceCountThisFrame); + instancer.update(modelIndex, instanceCountThisFrame); instanceCountThisFrame += instanceCount; modelIndex++; @@ -172,7 +171,7 @@ public class IndirectCullingGroup { public void add(IndirectInstancer instancer, InstancerKey key, MeshPool meshPool) { instancer.mapping = buffers.objectStorage.createMapping(); - instancer.postUpdate(instancers.size(), -1); + instancer.update(instancers.size(), -1); instancers.add(instancer); diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java index d3ae8999e..ee11999a8 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java @@ -55,11 +55,7 @@ public class IndirectInstancer extends AbstractInstancer return associatedDraws; } - public void update() { - removeDeletedInstances(); - } - - public void postUpdate(int modelIndex, int baseInstance) { + public void update(int modelIndex, int baseInstance) { this.modelIndex = modelIndex; this.baseInstance = baseInstance; mapping.update(modelIndex, instanceCount()); diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedDrawManager.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedDrawManager.java index eab6c811c..a3740ee56 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedDrawManager.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedDrawManager.java @@ -65,13 +65,11 @@ public class InstancedDrawManager extends DrawManager> { this.instancers.values() .removeIf(instancer -> { - // Update the instancers and remove any that are empty. - instancer.update(); - if (instancer.instanceCount() == 0) { instancer.delete(); return true; } else { + instancer.updateBuffer(); return false; } }); diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedInstancer.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedInstancer.java index 1f1de8f9d..2895195a5 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedInstancer.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedInstancer.java @@ -44,12 +44,7 @@ public class InstancedInstancer extends AbstractInstancer vbo = new GlBuffer(GlBufferUsage.DYNAMIC_DRAW); } - public void update() { - removeDeletedInstances(); - updateBuffer(); - } - - private void updateBuffer() { + public void updateBuffer() { if (changed.isEmpty() || vbo == null) { return; }