The botched batching plan

- Directly port over submitTasks to use Plan
- Could probably make better use of the plan primitives, but oh well
- Remove Engine#beginFrame
This commit is contained in:
Jozufozu 2023-04-09 14:36:35 -07:00
parent 1627874e33
commit 76dcf3fed6
7 changed files with 20 additions and 34 deletions

View file

@ -12,7 +12,6 @@ import net.minecraft.client.Camera;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
public interface Engine extends InstancerProvider { public interface Engine extends InstancerProvider {
void beginFrame(TaskExecutor executor, RenderContext context);
void renderStage(TaskExecutor executor, RenderContext context, RenderStage stage); void renderStage(TaskExecutor executor, RenderContext context, RenderStage stage);

View file

@ -1,5 +1,6 @@
package com.jozufozu.flywheel.backend.engine.batching; package com.jozufozu.flywheel.backend.engine.batching;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.jozufozu.flywheel.api.backend.Engine; 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.struct.StructType;
import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.task.Plan;
import com.jozufozu.flywheel.api.task.TaskExecutor; 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.lib.task.PlanUtil;
import com.jozufozu.flywheel.util.FlwUtil; import com.jozufozu.flywheel.util.FlwUtil;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Camera; import net.minecraft.client.Camera;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
@ -30,19 +30,6 @@ public class BatchingEngine implements Engine {
return transformManager.getInstancer(type, model, stage); 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 @Override
public Plan planThisFrame(RenderContext context) { public Plan planThisFrame(RenderContext context) {
return PlanUtil.of(transformManager::flush) return PlanUtil.of(transformManager::flush)
@ -55,11 +42,11 @@ public class BatchingEngine implements Engine {
var stack = FlwUtil.copyPoseStack(context.stack()); var stack = FlwUtil.copyPoseStack(context.stack());
stack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z); stack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z);
// TODO: planify batching engine var matrices = stack.last();
return PlanUtil.of(); var level = context.level();
}
var plans = new ArrayList<Plan>();
private void submitTasks(TaskExecutor executor, PoseStack.Pose matrices, ClientLevel level) {
for (var transformSetEntry : transformManager.getTransformSetsView() for (var transformSetEntry : transformManager.getTransformSetsView()
.entrySet()) { .entrySet()) {
var stage = transformSetEntry.getKey(); var stage = transformSetEntry.getKey();
@ -84,11 +71,13 @@ public class BatchingEngine implements Engine {
int startVertex = 0; int startVertex = 0;
for (var transformCall : transformCalls) { for (var transformCall : transformCalls) {
transformCall.submitTasks(executor, buffer, startVertex, matrices, level); plans.add(transformCall.getPlan(buffer, startVertex, matrices, level));
startVertex += transformCall.getTotalVertexCount(); startVertex += transformCall.getTotalVertexCount();
} }
} }
} }
return new NestedPlan(plans);
} }
@Override @Override

View file

@ -1,13 +1,15 @@
package com.jozufozu.flywheel.backend.engine.batching; package com.jozufozu.flywheel.backend.engine.batching;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.struct.InstancePart; import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructVertexTransformer; 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.MutableVertexList;
import com.jozufozu.flywheel.api.vertex.ReusableVertexList; import com.jozufozu.flywheel.api.vertex.ReusableVertexList;
import com.jozufozu.flywheel.lib.task.SimplePlan;
import com.jozufozu.flywheel.lib.vertex.VertexTransformations; import com.jozufozu.flywheel.lib.vertex.VertexTransformations;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Matrix3f; import com.mojang.math.Matrix3f;
@ -40,9 +42,11 @@ public class TransformCall<P extends InstancePart> {
instancer.update(); 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(); int instances = instancer.getInstanceCount();
var out = new ArrayList<Runnable>();
while (instances > 0) { while (instances > 0) {
int end = instances; int end = instances;
instances -= 512; instances -= 512;
@ -52,8 +56,9 @@ public class TransformCall<P extends InstancePart> {
ReusableVertexList sub = buffer.slice(startVertex, vertexCount); ReusableVertexList sub = buffer.slice(startVertex, vertexCount);
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) { public void transformRange(ReusableVertexList vertexList, int from, int to, PoseStack.Pose matrices, ClientLevel level) {

View file

@ -40,11 +40,6 @@ public class IndirectEngine implements Engine {
return drawManager.getInstancer(type, model, stage); return drawManager.getInstancer(type, model, stage);
} }
@Override
public void beginFrame(TaskExecutor executor, RenderContext context) {
flushDrawManager();
}
@Override @Override
public Plan planThisFrame(RenderContext context) { public Plan planThisFrame(RenderContext context) {
return PlanUtil.onMainThread(this::flushDrawManager); return PlanUtil.onMainThread(this::flushDrawManager);

View file

@ -47,11 +47,6 @@ public class InstancingEngine implements Engine {
return drawManager.getInstancer(type, model, stage); return drawManager.getInstancer(type, model, stage);
} }
@Override
public void beginFrame(TaskExecutor executor, RenderContext context) {
flushDrawManager();
}
@Override @Override
public Plan planThisFrame(RenderContext context) { public Plan planThisFrame(RenderContext context) {
return PlanUtil.onMainThread(this::flushDrawManager); return PlanUtil.onMainThread(this::flushDrawManager);

View file

@ -75,6 +75,7 @@ public class InstanceWorld implements AutoCloseable {
var effectPlan = effects.planThisTick(cameraX, cameraY, cameraZ); var effectPlan = effects.planThisTick(cameraX, cameraY, cameraZ);
PlanUtil.of(blockEntityPlan, entityPlan, effectPlan) PlanUtil.of(blockEntityPlan, entityPlan, effectPlan)
.maybeSimplify()
.execute(taskExecutor); .execute(taskExecutor);
} }
@ -90,6 +91,7 @@ public class InstanceWorld implements AutoCloseable {
taskExecutor.syncPoint(); taskExecutor.syncPoint();
getManagerPlan(context).then(engine.planThisFrame(context)) getManagerPlan(context).then(engine.planThisFrame(context))
.maybeSimplify()
.execute(taskExecutor); .execute(taskExecutor);
} }

View file

@ -10,6 +10,7 @@ import com.jozufozu.flywheel.api.task.TaskExecutor;
public record RunOnAllPlan<T>(Supplier<List<T>> listSupplier, Consumer<T> action) implements Plan { public record RunOnAllPlan<T>(Supplier<List<T>> listSupplier, Consumer<T> action) implements Plan {
@Override @Override
public void execute(TaskExecutor taskExecutor, Runnable onCompletion) { public void execute(TaskExecutor taskExecutor, Runnable onCompletion) {
// TODO: unit tests, fix CME?
taskExecutor.execute(() -> { taskExecutor.execute(() -> {
var list = listSupplier.get(); var list = listSupplier.get();
final int size = list.size(); final int size = list.size();