From c20b0142e9c00a0e3f018f0471df5cad649479dc Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Tue, 7 Dec 2021 23:40:45 -0800 Subject: [PATCH] Groups store their types again - Fix crash rendering breaking overlay --- .../instancing/InstancedMaterialGroup.java | 6 +- .../material/instancing/InstancingEngine.java | 59 +++++++++++-------- .../material/instancing/package-info.java | 6 ++ .../core/crumbling/CrumblingGroup.java | 6 +- 4 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/material/instancing/package-info.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancedMaterialGroup.java index 5933a97d0..5716034d0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancedMaterialGroup.java @@ -22,13 +22,15 @@ import net.minecraft.client.renderer.RenderType; public class InstancedMaterialGroup

implements MaterialGroup { protected final InstancingEngine

owner; + protected final RenderType type; protected final ArrayList> renderers = new ArrayList<>(); private final Map, InstancedMaterial> materials = new HashMap<>(); - public InstancedMaterialGroup(InstancingEngine

owner) { + public InstancedMaterialGroup(InstancingEngine

owner, RenderType type) { this.owner = owner; + this.type = type; } /** @@ -43,7 +45,7 @@ public class InstancedMaterialGroup

implements MaterialG return (InstancedMaterial) materials.computeIfAbsent(spec, this::createInstanceMaterial); } - public void render(RenderType type, Matrix4f viewProjection, double camX, double camY, double camZ) { + public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { type.setupRenderState(); TextureBinder.bindActiveTextures(); for (InstancedMaterialRenderer

renderer : renderers) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancingEngine.java index 25fa707c9..05a8de155 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/instancing/InstancingEngine.java @@ -4,6 +4,9 @@ import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; +import java.util.stream.Stream; + +import javax.annotation.Nullable; import com.jozufozu.flywheel.backend.gl.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; @@ -60,15 +63,15 @@ public class InstancingEngine

implements Engine { } /** - * Get a material group that will render in the given layer with the given state. + * Get a material group that will render in the given layer with the given type. * * @param layer The {@link RenderLayer} you want to draw in. - * @param state The {@link RenderType} you need to draw with. + * @param type The {@link RenderType} you need to draw with. * @return A material group whose children will */ @Override - public MaterialGroup state(RenderLayer layer, RenderType state) { - return layers.get(layer).computeIfAbsent(state, $ -> groupFactory.create(this)); + public MaterialGroup state(RenderLayer layer, RenderType type) { + return layers.get(layer).computeIfAbsent(type, t -> groupFactory.create(this, t)); } /** @@ -76,34 +79,44 @@ public class InstancingEngine

implements Engine { */ @Override public void render(RenderLayerEvent event, MultiBufferSource buffers) { - double camX = event.camX; - double camY = event.camY; - double camZ = event.camZ; - Matrix4f viewProjection = event.viewProjection; + double camX; + double camY; + double camZ; + Matrix4f viewProjection; if (!ignoreOriginCoordinate) { - camX -= originCoordinate.getX(); - camY -= originCoordinate.getY(); - camZ -= originCoordinate.getZ(); + camX = event.camX - originCoordinate.getX(); + camY = event.camY - originCoordinate.getY(); + camZ = event.camZ - originCoordinate.getZ(); - Matrix4f translate = Matrix4f.createTranslateMatrix((float) -camX, (float) -camY, (float) -camZ); - - translate.multiplyBackward(viewProjection); - - viewProjection = translate; + viewProjection = Matrix4f.createTranslateMatrix((float) -camX, (float) -camY, (float) -camZ); + viewProjection.multiplyBackward(event.viewProjection); + } else { + camX = event.camX; + camY = event.camY; + camZ = event.camZ; + viewProjection = event.viewProjection; } - for (Map.Entry> entry : layers.get(event.getLayer()).entrySet()) { - RenderType state = entry.getKey(); - InstancedMaterialGroup

group = entry.getValue(); - - group.render(state, viewProjection, camX, camY, camZ); - } + getGroupsToRender(event.getLayer()).forEach(group -> group.render(viewProjection, camX, camY, camZ)); GlBufferType.ELEMENT_ARRAY_BUFFER.unbind(); GlBufferType.ARRAY_BUFFER.unbind(); GlVertexArray.unbind(); } + private Stream> getGroupsToRender(@Nullable RenderLayer layer) { + if (layer != null) { + return layers.get(layer) + .values() + .stream(); + } else { + return layers.values() + .stream() + .flatMap(it -> it.values() + .stream()); + } + } + @Override public void delete() { for (Map> groups : layers.values()) { @@ -159,7 +172,7 @@ public class InstancingEngine

implements Engine { @FunctionalInterface public interface GroupFactory

{ - InstancedMaterialGroup

create(InstancingEngine

materialManager); + InstancedMaterialGroup

create(InstancingEngine

engine, RenderType type); } public static class Builder

{ diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/instancing/package-info.java b/src/main/java/com/jozufozu/flywheel/backend/material/instancing/package-info.java new file mode 100644 index 000000000..42283d047 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/material/instancing/package-info.java @@ -0,0 +1,6 @@ +@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault +package com.jozufozu.flywheel.backend.material.instancing; + +import javax.annotation.ParametersAreNonnullByDefault; + +import net.minecraft.MethodsReturnNonnullByDefault; 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 58f6c4d88..0250bf51d 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -18,12 +18,12 @@ public class CrumblingGroup

extends InstancedMateria private int width; private int height; - public CrumblingGroup(InstancingEngine

owner) { - super(owner); + public CrumblingGroup(InstancingEngine

owner, RenderType type) { + super(owner, type); } @Override - public void render(RenderType type, Matrix4f viewProjection, double camX, double camY, double camZ) { + public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { type.setupRenderState(); int renderTex = RenderSystem.getShaderTexture(0);