diff --git a/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java b/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java index fa2202101..4246f1afd 100644 --- a/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java +++ b/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java @@ -12,7 +12,6 @@ import net.minecraft.client.Camera; import net.minecraft.core.Vec3i; public interface Engine extends InstancerProvider { - void beginFrame(TaskExecutor executor, RenderContext context); void renderStage(TaskExecutor executor, RenderContext context, RenderStage stage); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java index 444622b2b..9869da03d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend.engine.batching; +import java.util.ArrayList; import java.util.List; import com.jozufozu.flywheel.api.backend.Engine; @@ -11,12 +12,11 @@ import com.jozufozu.flywheel.api.struct.InstancePart; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.task.TaskExecutor; +import com.jozufozu.flywheel.lib.task.NestedPlan; import com.jozufozu.flywheel.lib.task.PlanUtil; import com.jozufozu.flywheel.util.FlwUtil; -import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.Camera; -import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.core.BlockPos; import net.minecraft.core.Vec3i; import net.minecraft.world.phys.Vec3; @@ -30,19 +30,6 @@ public class BatchingEngine implements Engine { return transformManager.getInstancer(type, model, stage); } - @Override - public void beginFrame(TaskExecutor executor, RenderContext context) { - transformManager.flush(); - - Vec3 cameraPos = context.camera() - .getPosition(); - var stack = FlwUtil.copyPoseStack(context.stack()); - stack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z); - - executor.syncPoint(); - submitTasks(executor, stack.last(), context.level()); - } - @Override public Plan planThisFrame(RenderContext context) { return PlanUtil.of(transformManager::flush) @@ -55,11 +42,11 @@ public class BatchingEngine implements Engine { var stack = FlwUtil.copyPoseStack(context.stack()); stack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z); - // TODO: planify batching engine - return PlanUtil.of(); - } + var matrices = stack.last(); + var level = context.level(); + + var plans = new ArrayList(); - private void submitTasks(TaskExecutor executor, PoseStack.Pose matrices, ClientLevel level) { for (var transformSetEntry : transformManager.getTransformSetsView() .entrySet()) { var stage = transformSetEntry.getKey(); @@ -84,11 +71,13 @@ public class BatchingEngine implements Engine { int startVertex = 0; for (var transformCall : transformCalls) { - transformCall.submitTasks(executor, buffer, startVertex, matrices, level); + plans.add(transformCall.getPlan(buffer, startVertex, matrices, level)); startVertex += transformCall.getTotalVertexCount(); } } } + + return new NestedPlan(plans); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/TransformCall.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/TransformCall.java index 61f1c61ba..6055e5e61 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/TransformCall.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/TransformCall.java @@ -1,13 +1,15 @@ package com.jozufozu.flywheel.backend.engine.batching; +import java.util.ArrayList; import java.util.List; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.struct.InstancePart; import com.jozufozu.flywheel.api.struct.StructVertexTransformer; -import com.jozufozu.flywheel.api.task.TaskExecutor; +import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.ReusableVertexList; +import com.jozufozu.flywheel.lib.task.SimplePlan; import com.jozufozu.flywheel.lib.vertex.VertexTransformations; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix3f; @@ -40,9 +42,11 @@ public class TransformCall

{ instancer.update(); } - public void submitTasks(TaskExecutor executor, DrawBuffer buffer, int startVertex, PoseStack.Pose matrices, ClientLevel level) { + public Plan getPlan(DrawBuffer buffer, int startVertex, PoseStack.Pose matrices, ClientLevel level) { int instances = instancer.getInstanceCount(); + var out = new ArrayList(); + while (instances > 0) { int end = instances; instances -= 512; @@ -52,8 +56,9 @@ public class TransformCall

{ ReusableVertexList sub = buffer.slice(startVertex, vertexCount); startVertex += vertexCount; - executor.execute(() -> transformRange(sub, start, end, matrices, level)); + out.add(() -> transformRange(sub, start, end, matrices, level)); } + return new SimplePlan(out); } public void transformRange(ReusableVertexList vertexList, int from, int to, PoseStack.Pose matrices, ClientLevel level) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java index 7a2a0e2bc..23665914e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java @@ -40,11 +40,6 @@ public class IndirectEngine implements Engine { return drawManager.getInstancer(type, model, stage); } - @Override - public void beginFrame(TaskExecutor executor, RenderContext context) { - flushDrawManager(); - } - @Override public Plan planThisFrame(RenderContext context) { return PlanUtil.onMainThread(this::flushDrawManager); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java index d5dff8b41..a55104daf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java @@ -47,11 +47,6 @@ public class InstancingEngine implements Engine { return drawManager.getInstancer(type, model, stage); } - @Override - public void beginFrame(TaskExecutor executor, RenderContext context) { - flushDrawManager(); - } - @Override public Plan planThisFrame(RenderContext context) { return PlanUtil.onMainThread(this::flushDrawManager); diff --git a/src/main/java/com/jozufozu/flywheel/impl/instancing/InstanceWorld.java b/src/main/java/com/jozufozu/flywheel/impl/instancing/InstanceWorld.java index 435d66a86..f184e0883 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/impl/instancing/InstanceWorld.java @@ -75,6 +75,7 @@ public class InstanceWorld implements AutoCloseable { var effectPlan = effects.planThisTick(cameraX, cameraY, cameraZ); PlanUtil.of(blockEntityPlan, entityPlan, effectPlan) + .maybeSimplify() .execute(taskExecutor); } @@ -90,6 +91,7 @@ public class InstanceWorld implements AutoCloseable { taskExecutor.syncPoint(); getManagerPlan(context).then(engine.planThisFrame(context)) + .maybeSimplify() .execute(taskExecutor); } diff --git a/src/main/java/com/jozufozu/flywheel/lib/task/RunOnAllPlan.java b/src/main/java/com/jozufozu/flywheel/lib/task/RunOnAllPlan.java index 3f36022e5..598af1a9b 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/task/RunOnAllPlan.java +++ b/src/main/java/com/jozufozu/flywheel/lib/task/RunOnAllPlan.java @@ -10,6 +10,7 @@ import com.jozufozu.flywheel.api.task.TaskExecutor; public record RunOnAllPlan(Supplier> listSupplier, Consumer action) implements Plan { @Override public void execute(TaskExecutor taskExecutor, Runnable onCompletion) { + // TODO: unit tests, fix CME? taskExecutor.execute(() -> { var list = listSupplier.get(); final int size = list.size();