Fix visual artifacts with contraptions while using opfine.

I think the buffers from the shadow pass were bleeding into the color pass.
This commit is contained in:
Jozufozu 2022-01-05 22:01:03 -08:00
parent e0342c4b78
commit 5c4ff9ca2a
9 changed files with 138 additions and 53 deletions

View file

@ -1,10 +1,6 @@
package com.jozufozu.flywheel.backend; package com.jozufozu.flywheel.backend;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable; 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. * (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use.
*/ */
public String getBackendDescriptor() { public String getBackendDescriptor() {
return engine.getProperName(); return engine == null ? "" : engine.getProperName();
} }
public FlwEngine getEngine() { public FlwEngine getEngine() {

View file

@ -4,7 +4,11 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Optional; import java.util.Optional;
import java.util.function.BooleanSupplier;
import com.jozufozu.flywheel.util.Lazy;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -15,6 +19,23 @@ public class OptifineHandler {
private static Package optifine; private static Package optifine;
private static OptifineHandler handler; private static OptifineHandler handler;
private static final Lazy<BooleanSupplier> 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 final boolean usingShaders;
public OptifineHandler(boolean usingShaders) { public OptifineHandler(boolean usingShaders) {
@ -40,6 +61,10 @@ public class OptifineHandler {
.orElse(false); .orElse(false);
} }
public static boolean isShadowPass() {
return isShadowPass.get().getAsBoolean();
}
public static void init() { public static void init() {
optifine = Package.getPackage(OPTIFINE_ROOT_PACKAGE); optifine = Package.getPackage(OPTIFINE_ROOT_PACKAGE);

View file

@ -90,12 +90,12 @@ public class InstanceWorld {
* </p> * </p>
*/ */
public void beginFrame(BeginFrameEvent event) { public void beginFrame(BeginFrameEvent event) {
engine.beginFrame(event.getInfo()); engine.beginFrame(event.getCamera());
taskEngine.syncPoint(); taskEngine.syncPoint();
blockEntityInstanceManager.beginFrame(taskEngine, event.getInfo()); blockEntityInstanceManager.beginFrame(taskEngine, event.getCamera());
entityInstanceManager.beginFrame(taskEngine, event.getInfo()); entityInstanceManager.beginFrame(taskEngine, event.getCamera());
} }
/** /**

View file

@ -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;
}
}

View file

@ -8,28 +8,28 @@ import net.minecraftforge.eventbus.api.Event;
public class BeginFrameEvent extends Event { public class BeginFrameEvent extends Event {
private final ClientLevel world; private final ClientLevel world;
private final Camera info; private final Camera camera;
private final Frustum clippingHelper; private final Frustum frustum;
public BeginFrameEvent(ClientLevel world, Camera info, Frustum clippingHelper) { public BeginFrameEvent(ClientLevel world, Camera camera, Frustum frustum) {
this.world = world; this.world = world;
this.info = info; this.camera = camera;
this.clippingHelper = clippingHelper; this.frustum = frustum;
} }
public ClientLevel getWorld() { public ClientLevel getWorld() {
return world; return world;
} }
public Camera getInfo() { public Camera getCamera() {
return info; return camera;
} }
public Frustum getClippingHelper() { public Frustum getFrustum() {
return clippingHelper; return frustum;
} }
public Vec3 getCameraPos() { public Vec3 getCameraPos() {
return info.getPosition(); return camera.getPosition();
} }
} }

View file

@ -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);
}
}

View file

@ -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));
}
}
}

View file

@ -33,7 +33,7 @@ import net.minecraftforge.common.MinecraftForge;
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
@Mixin(LevelRenderer.class) @Mixin(LevelRenderer.class)
public class RenderHooksMixin { public class LevelRendererMixin {
@Shadow @Shadow
private ClientLevel level; private ClientLevel level;
@ -52,7 +52,7 @@ public class RenderHooksMixin {
* layer-correct custom rendering. RenderWorldLast is not refined enough for rendering world objects. * layer-correct custom rendering. RenderWorldLast is not refined enough for rendering world objects.
* This should probably be a forge event. * 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) { private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) {
RenderBuffers renderBuffers = this.renderBuffers; RenderBuffers renderBuffers = this.renderBuffers;

View file

@ -1,34 +1,36 @@
{ {
"required": true, "required": true,
"minVersion": "0.8", "minVersion": "0.8",
"package": "com.jozufozu.flywheel.mixin", "package": "com.jozufozu.flywheel.mixin",
"compatibilityLevel": "JAVA_17", "compatibilityLevel": "JAVA_17",
"refmap": "flywheel.refmap.json", "refmap": "flywheel.refmap.json",
"client": [ "client": [
"BlockEntityTypeMixin", "BlockEntityTypeMixin",
"BufferBuilderMixin", "BufferBuilderMixin",
"BufferUploaderAccessor", "BufferUploaderAccessor",
"CancelEntityRenderMixin", "CameraMixin",
"ChunkRebuildHooksMixin", "CancelEntityRenderMixin",
"EntityTypeMixin", "ChunkRebuildHooksMixin",
"FixFabulousDepthMixin", "EntityTypeMixin",
"InstanceAddMixin", "FixFabulousDepthMixin",
"InstanceRemoveMixin", "FrustumMixin",
"LevelRendererAccessor", "InstanceAddMixin",
"PausedPartialTickAccessor", "InstanceRemoveMixin",
"RenderHooksMixin", "LevelRendererAccessor",
"RenderTexturesMixin", "LevelRendererMixin",
"ShaderCloseMixin", "PausedPartialTickAccessor",
"ShaderInstanceAccessor", "RenderTexturesMixin",
"atlas.AtlasDataMixin", "ShaderCloseMixin",
"atlas.SheetDataAccessor", "ShaderInstanceAccessor",
"light.LightUpdateMixin", "atlas.AtlasDataMixin",
"light.NetworkLightUpdateMixin", "atlas.SheetDataAccessor",
"matrix.Matrix3fMixin", "light.LightUpdateMixin",
"matrix.Matrix4fMixin", "light.NetworkLightUpdateMixin",
"matrix.PoseStackMixin" "matrix.Matrix3fMixin",
], "matrix.Matrix4fMixin",
"injectors": { "matrix.PoseStackMixin"
"defaultRequire": 1 ],
} "injectors": {
"defaultRequire": 1
}
} }