From 5c4ff9ca2a6021880803ed4e796a79e77154cf7b Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Wed, 5 Jan 2022 22:01:03 -0800 Subject: [PATCH] Fix visual artifacts with contraptions while using opfine. I think the buffers from the shadow pass were bleeding into the color pass. --- .../jozufozu/flywheel/backend/Backend.java | 8 +-- .../flywheel/backend/OptifineHandler.java | 25 +++++++ .../backend/instancing/InstanceWorld.java | 6 +- .../flywheel/core/LastActiveCamera.java | 16 +++++ .../flywheel/event/BeginFrameEvent.java | 20 +++--- .../jozufozu/flywheel/mixin/CameraMixin.java | 21 ++++++ .../jozufozu/flywheel/mixin/FrustumMixin.java | 25 +++++++ ...ooksMixin.java => LevelRendererMixin.java} | 4 +- src/main/resources/flywheel.mixins.json | 66 ++++++++++--------- 9 files changed, 138 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/core/LastActiveCamera.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/CameraMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java rename src/main/java/com/jozufozu/flywheel/mixin/{RenderHooksMixin.java => LevelRendererMixin.java} (95%) diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index bffbddc46..2285f5684 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -1,10 +1,6 @@ package com.jozufozu.flywheel.backend; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import javax.annotation.Nullable; @@ -55,7 +51,7 @@ public class Backend { * (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use. */ public String getBackendDescriptor() { - return engine.getProperName(); + return engine == null ? "" : engine.getProperName(); } public FlwEngine getEngine() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java b/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java index f003208fe..51e60eebe 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java @@ -4,7 +4,11 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.lang.reflect.Field; import java.util.Optional; +import java.util.function.BooleanSupplier; + +import com.jozufozu.flywheel.util.Lazy; import net.minecraft.client.Minecraft; @@ -15,6 +19,23 @@ public class OptifineHandler { private static Package optifine; private static OptifineHandler handler; + private static final Lazy isShadowPass = Lazy.of(() -> { + try { + Class ofShaders = Class.forName("net.optifine.shaders.Shaders"); + Field field = ofShaders.getDeclaredField("isShadowPass"); + field.setAccessible(true); + return () -> { + try { + return field.getBoolean(null); + } catch (IllegalAccessException ignored) { + return false; + } + }; + } catch (Exception ignored) { + return () -> false; + } + }); + public final boolean usingShaders; public OptifineHandler(boolean usingShaders) { @@ -40,6 +61,10 @@ public class OptifineHandler { .orElse(false); } + public static boolean isShadowPass() { + return isShadowPass.get().getAsBoolean(); + } + public static void init() { optifine = Package.getPackage(OPTIFINE_ROOT_PACKAGE); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java index 1f4c7200c..8274a2e06 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -90,12 +90,12 @@ public class InstanceWorld { *

*/ public void beginFrame(BeginFrameEvent event) { - engine.beginFrame(event.getInfo()); + engine.beginFrame(event.getCamera()); taskEngine.syncPoint(); - blockEntityInstanceManager.beginFrame(taskEngine, event.getInfo()); - entityInstanceManager.beginFrame(taskEngine, event.getInfo()); + blockEntityInstanceManager.beginFrame(taskEngine, event.getCamera()); + entityInstanceManager.beginFrame(taskEngine, event.getCamera()); } /** diff --git a/src/main/java/com/jozufozu/flywheel/core/LastActiveCamera.java b/src/main/java/com/jozufozu/flywheel/core/LastActiveCamera.java new file mode 100644 index 000000000..0d47d3c66 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/LastActiveCamera.java @@ -0,0 +1,16 @@ +package com.jozufozu.flywheel.core; + +import net.minecraft.client.Camera; + +public class LastActiveCamera { + + private static Camera camera; + + public static void _setActiveCamera(Camera camera) { + LastActiveCamera.camera = camera; + } + + public static Camera getActiveCamera() { + return camera; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/event/BeginFrameEvent.java b/src/main/java/com/jozufozu/flywheel/event/BeginFrameEvent.java index 2db6271ee..db4d6fd1f 100644 --- a/src/main/java/com/jozufozu/flywheel/event/BeginFrameEvent.java +++ b/src/main/java/com/jozufozu/flywheel/event/BeginFrameEvent.java @@ -8,28 +8,28 @@ import net.minecraftforge.eventbus.api.Event; public class BeginFrameEvent extends Event { private final ClientLevel world; - private final Camera info; - private final Frustum clippingHelper; + private final Camera camera; + private final Frustum frustum; - public BeginFrameEvent(ClientLevel world, Camera info, Frustum clippingHelper) { + public BeginFrameEvent(ClientLevel world, Camera camera, Frustum frustum) { this.world = world; - this.info = info; - this.clippingHelper = clippingHelper; + this.camera = camera; + this.frustum = frustum; } public ClientLevel getWorld() { return world; } - public Camera getInfo() { - return info; + public Camera getCamera() { + return camera; } - public Frustum getClippingHelper() { - return clippingHelper; + public Frustum getFrustum() { + return frustum; } public Vec3 getCameraPos() { - return info.getPosition(); + return camera.getPosition(); } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CameraMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CameraMixin.java new file mode 100644 index 000000000..52e6043ef --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/CameraMixin.java @@ -0,0 +1,21 @@ +package com.jozufozu.flywheel.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.jozufozu.flywheel.core.LastActiveCamera; + +import net.minecraft.client.Camera; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.BlockGetter; + +@Mixin(Camera.class) +public class CameraMixin { + + @Inject(method = "setup", at = @At("TAIL")) + private void setup(BlockGetter level, Entity entity, boolean is3rdPerson, boolean isMirrored, float pt, CallbackInfo ci) { + LastActiveCamera._setActiveCamera((Camera)(Object) this); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java new file mode 100644 index 000000000..c0edf6bf0 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/FrustumMixin.java @@ -0,0 +1,25 @@ +package com.jozufozu.flywheel.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.jozufozu.flywheel.backend.OptifineHandler; +import com.jozufozu.flywheel.core.LastActiveCamera; +import com.jozufozu.flywheel.event.BeginFrameEvent; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.culling.Frustum; +import net.minecraftforge.common.MinecraftForge; + +@Mixin(Frustum.class) +public class FrustumMixin { + + @Inject(method = "prepare", at = @At("TAIL")) + private void onPrepare(double x, double y, double z, CallbackInfo ci) { + if (OptifineHandler.isShadowPass()) { + MinecraftForge.EVENT_BUS.post(new BeginFrameEvent(Minecraft.getInstance().level, LastActiveCamera.getActiveCamera(), (Frustum) (Object) this)); + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java similarity index 95% rename from src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java index a60ffd45d..3dbe19701 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java @@ -33,7 +33,7 @@ import net.minecraftforge.common.MinecraftForge; @OnlyIn(Dist.CLIENT) @Mixin(LevelRenderer.class) -public class RenderHooksMixin { +public class LevelRendererMixin { @Shadow private ClientLevel level; @@ -52,7 +52,7 @@ public class RenderHooksMixin { * layer-correct custom rendering. RenderWorldLast is not refined enough for rendering world objects. * This should probably be a forge event. */ - @Inject(at = @At("TAIL"), method = "renderChunkLayer") + @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/ShaderInstance;clear()V"), method = "renderChunkLayer") private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) { RenderBuffers renderBuffers = this.renderBuffers; diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index bcc9860ab..01959c480 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -1,34 +1,36 @@ { - "required": true, - "minVersion": "0.8", - "package": "com.jozufozu.flywheel.mixin", - "compatibilityLevel": "JAVA_17", - "refmap": "flywheel.refmap.json", - "client": [ - "BlockEntityTypeMixin", - "BufferBuilderMixin", - "BufferUploaderAccessor", - "CancelEntityRenderMixin", - "ChunkRebuildHooksMixin", - "EntityTypeMixin", - "FixFabulousDepthMixin", - "InstanceAddMixin", - "InstanceRemoveMixin", - "LevelRendererAccessor", - "PausedPartialTickAccessor", - "RenderHooksMixin", - "RenderTexturesMixin", - "ShaderCloseMixin", - "ShaderInstanceAccessor", - "atlas.AtlasDataMixin", - "atlas.SheetDataAccessor", - "light.LightUpdateMixin", - "light.NetworkLightUpdateMixin", - "matrix.Matrix3fMixin", - "matrix.Matrix4fMixin", - "matrix.PoseStackMixin" - ], - "injectors": { - "defaultRequire": 1 - } + "required": true, + "minVersion": "0.8", + "package": "com.jozufozu.flywheel.mixin", + "compatibilityLevel": "JAVA_17", + "refmap": "flywheel.refmap.json", + "client": [ + "BlockEntityTypeMixin", + "BufferBuilderMixin", + "BufferUploaderAccessor", + "CameraMixin", + "CancelEntityRenderMixin", + "ChunkRebuildHooksMixin", + "EntityTypeMixin", + "FixFabulousDepthMixin", + "FrustumMixin", + "InstanceAddMixin", + "InstanceRemoveMixin", + "LevelRendererAccessor", + "LevelRendererMixin", + "PausedPartialTickAccessor", + "RenderTexturesMixin", + "ShaderCloseMixin", + "ShaderInstanceAccessor", + "atlas.AtlasDataMixin", + "atlas.SheetDataAccessor", + "light.LightUpdateMixin", + "light.NetworkLightUpdateMixin", + "matrix.Matrix3fMixin", + "matrix.Matrix4fMixin", + "matrix.PoseStackMixin" + ], + "injectors": { + "defaultRequire": 1 + } }