diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index ce368a913..95958d162 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -30,7 +30,7 @@ import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber(Dist.CLIENT) public class InstancedRenderDispatcher { - private static final WorldAttached> materialManagers = new WorldAttached<>($ -> new MaterialManager<>(Contexts.WORLD)); + private static final WorldAttached> materialManagers = new WorldAttached<>($ -> MaterialManager.builder(Contexts.WORLD).build()); private static final WorldAttached> entityInstanceManager = new WorldAttached<>(world -> new EntityInstanceManager(materialManagers.get(world))); private static final WorldAttached> tileInstanceManager = new WorldAttached<>(world -> new TileInstanceManager(materialManagers.get(world))); diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroup.java index bbcace0e0..d9d84e5af 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroup.java @@ -6,10 +6,8 @@ import java.util.Map; import com.jozufozu.flywheel.backend.instancing.InstanceData; import com.jozufozu.flywheel.backend.state.IRenderState; -import com.jozufozu.flywheel.core.shader.IProgramCallback; import com.jozufozu.flywheel.core.shader.WorldProgram; -import net.minecraft.client.renderer.RenderType; import net.minecraft.util.math.vector.Matrix4f; public class MaterialGroup

{ @@ -26,12 +24,16 @@ public class MaterialGroup

{ this.state = state; } - public void render(Matrix4f viewProjection, double camX, double camY, double camZ, IProgramCallback

callback) { + public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { for (MaterialRenderer

renderer : renderers) { - renderer.render(viewProjection, camX, camY, camZ, callback); + renderer.render(viewProjection, camX, camY, camZ); } } + public void setup(P program) { + + } + @SuppressWarnings("unchecked") public InstanceMaterial material(MaterialSpec spec) { return (InstanceMaterial) materials.computeIfAbsent(spec, this::createInstanceMaterial); @@ -40,7 +42,7 @@ public class MaterialGroup

{ private InstanceMaterial createInstanceMaterial(MaterialSpec type) { InstanceMaterial material = new InstanceMaterial<>(owner::getOriginCoordinate, type); - this.renderers.add(new MaterialRenderer<>(owner.getProgram(type.getProgramName()), material)); + this.renderers.add(new MaterialRenderer<>(owner.getProgram(type.getProgramName()), material, this::setup)); return material; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java index f7c76d711..d9436c117 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManager.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend.material; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; @@ -12,7 +13,6 @@ import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.materials.ModelData; import com.jozufozu.flywheel.core.materials.OrientedData; -import com.jozufozu.flywheel.core.shader.IProgramCallback; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.util.WeakHashSet; @@ -29,23 +29,32 @@ public class MaterialManager

{ public static int MAX_ORIGIN_DISTANCE = 100; + protected BlockPos originCoordinate = BlockPos.ZERO; + protected final WorldContext

context; - protected GroupFactory

groupFactory; - protected boolean ignoreOriginCoordinate; + protected final GroupFactory

groupFactory; + protected final boolean ignoreOriginCoordinate; protected final Map>> layers; - protected BlockPos originCoordinate = BlockPos.ZERO; - private final WeakHashSet listeners; public MaterialManager(WorldContext

context) { + this(context, MaterialGroup::new, false); + } + + public static

Builder

builder(WorldContext

context) { + return new Builder<>(context); + } + + public MaterialManager(WorldContext

context, GroupFactory

groupFactory, boolean ignoreOriginCoordinate) { this.context = context; + this.ignoreOriginCoordinate = ignoreOriginCoordinate; - this.layers = new HashMap<>(); this.listeners = new WeakHashSet<>(); - this.groupFactory = MaterialGroup::new; + this.groupFactory = groupFactory; + this.layers = new EnumMap<>(RenderLayer.class); for (RenderLayer value : RenderLayer.values()) { layers.put(value, new HashMap<>()); } @@ -53,20 +62,10 @@ public class MaterialManager

{ /** * Render every model for every material. - * @param layer Which vanilla {@link RenderType} is being drawn? + * @param layer Which vanilla {@link RenderType} is being drawn? * @param viewProjection How do we get from camera space to clip space? */ public void render(RenderLayer layer, Matrix4f viewProjection, double camX, double camY, double camZ) { - render(layer, viewProjection, camX, camY, camZ, null); - } - - /** - * Render every model for every material. - * @param layer Which vanilla {@link RenderType} is being drawn? - * @param viewProjection How do we get from camera space to clip space? - * @param callback Provide additional uniforms or state here. - */ - public void render(RenderLayer layer, Matrix4f viewProjection, double camX, double camY, double camZ, IProgramCallback

callback) { if (!ignoreOriginCoordinate) { camX -= originCoordinate.getX(); camY -= originCoordinate.getY(); @@ -84,7 +83,7 @@ public class MaterialManager

{ MaterialGroup

group = entry.getValue(); state.bind(); - group.render(viewProjection, camX, camY, camZ, callback); + group.render(viewProjection, camX, camY, camZ); state.unbind(); } } @@ -148,16 +147,6 @@ public class MaterialManager

{ return context.getProgramSupplier(name); } - public MaterialManager

setIgnoreOriginCoordinate(boolean ignoreOriginCoordinate) { - this.ignoreOriginCoordinate = ignoreOriginCoordinate; - return this; - } - - public MaterialManager

setGroupFactory(GroupFactory

factory) { - this.groupFactory = factory; - return this; - } - public Vector3i getOriginCoordinate() { return originCoordinate; } @@ -200,4 +189,28 @@ public class MaterialManager

{ public interface GroupFactory

{ MaterialGroup

create(MaterialManager

materialManager, IRenderState state); } + + public static class Builder

{ + protected final WorldContext

context; + protected GroupFactory

groupFactory = MaterialGroup::new; + protected boolean ignoreOriginCoordinate; + + public Builder(WorldContext

context) { + this.context = context; + } + + public Builder

setGroupFactory(GroupFactory

groupFactory) { + this.groupFactory = groupFactory; + return this; + } + + public Builder

setIgnoreOriginCoordinate(boolean ignoreOriginCoordinate) { + this.ignoreOriginCoordinate = ignoreOriginCoordinate; + return this; + } + + public MaterialManager

build() { + return new MaterialManager<>(context, groupFactory, ignoreOriginCoordinate); + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialRenderer.java index ff52f00e5..bf4c54b0b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialRenderer.java @@ -1,9 +1,9 @@ package com.jozufozu.flywheel.backend.material; +import java.util.function.Consumer; import java.util.function.Supplier; import com.jozufozu.flywheel.backend.instancing.Instancer; -import com.jozufozu.flywheel.core.shader.IProgramCallback; import com.jozufozu.flywheel.core.shader.WorldProgram; import net.minecraft.util.math.vector.Matrix4f; @@ -13,12 +13,15 @@ public class MaterialRenderer

{ protected final Supplier

program; protected final InstanceMaterial material; - public MaterialRenderer(Supplier

programSupplier, InstanceMaterial material) { + protected final Consumer

setupFunc; + + public MaterialRenderer(Supplier

programSupplier, InstanceMaterial material, Consumer

setupFunc) { this.program = programSupplier; this.material = material; + this.setupFunc = setupFunc; } - public void render(Matrix4f viewProjection, double camX, double camY, double camZ, IProgramCallback

setup) { + public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { if (material.nothingToRender()) return; P program = this.program.get(); @@ -27,7 +30,7 @@ public class MaterialRenderer

{ program.uploadViewProjection(viewProjection); program.uploadCameraPos(camX, camY, camZ); - if (setup != null) setup.call(program); + setupFunc.accept(program); material.forEachInstancer(Instancer::render); } diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java index ee361a15d..81159c784 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -1,15 +1,13 @@ package com.jozufozu.flywheel.core.crumbling; import com.jozufozu.flywheel.backend.gl.GlTextureUnit; -import com.jozufozu.flywheel.backend.material.MaterialManager; import com.jozufozu.flywheel.backend.material.MaterialGroup; +import com.jozufozu.flywheel.backend.material.MaterialManager; import com.jozufozu.flywheel.backend.state.IRenderState; import com.jozufozu.flywheel.core.atlas.AtlasInfo; import com.jozufozu.flywheel.core.atlas.SheetData; -import com.jozufozu.flywheel.core.shader.IProgramCallback; import net.minecraft.util.ResourceLocation; -import net.minecraft.util.math.vector.Matrix4f; public class CrumblingGroup

extends MaterialGroup

{ @@ -32,11 +30,7 @@ public class CrumblingGroup

extends MaterialGroup

} @Override - public void render(Matrix4f viewProjection, double camX, double camY, double camZ, IProgramCallback

callback) { - super.render(viewProjection, camX, camY, camZ, ((IProgramCallback

) this::setup).andThen(callback)); - } - - private void setup(P p) { + public void setup(P p) { p.setAtlasSize(width, height); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java index 941e2d44b..b95741afe 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java @@ -148,7 +148,9 @@ public class CrumblingRenderer { private final InstanceManager instanceManager; private State() { - materialManager = new MaterialManager<>(Contexts.CRUMBLING).setGroupFactory(CrumblingGroup::new); + materialManager = MaterialManager.builder(Contexts.CRUMBLING) + .setGroupFactory(CrumblingGroup::new) + .build(); instanceManager = new CrumblingInstanceManager(materialManager); } diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/IProgramCallback.java b/src/main/java/com/jozufozu/flywheel/core/shader/IProgramCallback.java deleted file mode 100644 index af1f3b650..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/shader/IProgramCallback.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.jozufozu.flywheel.core.shader; - -import com.jozufozu.flywheel.backend.gl.shader.GlProgram; - -/** - * Used to define shader uniforms. - */ -@FunctionalInterface -public interface IProgramCallback

{ - - void call(P program); - - default IProgramCallback

andThen(IProgramCallback

other) { - if (other == null) return this; - - return program -> { - call(program); - other.call(program); - }; - } -}