Skip to content

Commit

Permalink
Port Distant Horizons compatibility
Browse files Browse the repository at this point in the history
Co-Authored-By: IMS <[email protected]>
  • Loading branch information
Asek3 and IMS212 committed Apr 10, 2024
1 parent 22f576e commit abe6d56
Show file tree
Hide file tree
Showing 40 changed files with 1,770 additions and 270 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'dev.architectury.loom' version '1.4-SNAPSHOT'
id 'dev.architectury.loom' version '1.6-SNAPSHOT'
id 'maven-publish'
}

Expand All @@ -19,7 +19,6 @@ loom {
"oculus-batched-entity-rendering.mixins.json",
"mixins.oculus.compat.sodium.json",
"mixins.oculus.compat.indigo.json",
"mixins.oculus.compat.dh.json",
"mixins.oculus.compat.indium.json",
"mixins.oculus.compat.pixelmon.json"
]
Expand Down Expand Up @@ -106,12 +105,13 @@ dependencies {
forge "net.neoforged:forge:${minecraft_version}-${forge_version}"

modImplementation "org.embeddedt:embeddium-${minecraft_version}:${embeddium_version}"
compileOnly "maven.modrinth:distanthorizons:2.0.1-a-1.20.1"

forgeRuntimeLibrary(implementation(shadow(project(path: ":glsl-relocated", configuration: "bundledJar")))) {
transitive = false
}
forgeRuntimeLibrary(implementation(include("org.anarres:jcpp:1.4.14")))

modImplementation fileTree(dir: 'libs', include: ['*.jar'])
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Binary file added libs/DistantHorizons-2.0.3-a-dev-1.20.1.jar
Binary file not shown.
27 changes: 15 additions & 12 deletions src/main/java/net/coderbot/iris/Iris.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.base.Throwables;
import com.mojang.blaze3d.platform.GlDebug;
import com.mojang.blaze3d.platform.InputConstants;
import com.sun.jna.platform.unix.LibC;
import net.coderbot.iris.compat.dh.DHCompat;
import net.coderbot.iris.config.IrisConfig;
import net.coderbot.iris.gl.GLDebug;
import net.coderbot.iris.gl.shader.ShaderCompileException;
Expand All @@ -15,7 +15,6 @@
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.shaderpack.DimensionId;
import net.coderbot.iris.shaderpack.IrisDefines;
import net.coderbot.iris.shaderpack.OptionalBoolean;
import net.coderbot.iris.shaderpack.ProgramSet;
import net.coderbot.iris.shaderpack.ShaderPack;
Expand All @@ -28,11 +27,9 @@
import net.coderbot.iris.texture.pbr.PBRTextureManager;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;

import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.ConfigScreenHandler;
Expand All @@ -48,18 +45,11 @@
import net.minecraftforge.network.NetworkConstants;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.Configuration;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -122,6 +112,10 @@ public Iris() {
}
}

public static boolean loadedIncompatiblePack() {
return DHCompat.lastPackIncompatible();
}

/**
* Called very early on in Minecraft initialization. At this point we *cannot* safely access OpenGL, but we can do
* some very basic setup, config loading, and environment checks.
Expand All @@ -132,6 +126,8 @@ public Iris() {
* <p>This is called right before options are loaded, so we can add key bindings here.</p>
*/
public static void onEarlyInitialize() {
DHCompat.run();

try {
if (!Files.exists(getShaderpacksDirectory())) {
Files.createDirectories(getShaderpacksDirectory());
Expand Down Expand Up @@ -590,6 +586,13 @@ public static void reload() throws IOException {
if (Minecraft.getInstance().level != null) {
Iris.getPipelineManager().preparePipeline(Iris.getCurrentDimension());
}

if (loadedIncompatiblePack() && Minecraft.getInstance().player != null) {
Minecraft.getInstance().gui.setTimes(10, 70, 140);
Iris.logger.warn("Incompatible pack for DH!");
Minecraft.getInstance().gui.setTitle(Component.literal("This pack doesn't have DH support").withStyle(ChatFormatting.BOLD, ChatFormatting.RED));
Minecraft.getInstance().gui.setSubtitle(Component.literal("Distant Horizons (DH) chunks won't show up. This isn't a bug, get another shader.").withStyle(ChatFormatting.RED));
}
}


Expand Down
210 changes: 203 additions & 7 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompat.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,211 @@
package net.coderbot.iris.compat.dh;

import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.shader.ShaderCompileException;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.loading.LoadingModList;
import org.joml.Matrix4f;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;

public class DHCompat {
private GlFramebuffer fb;
private static boolean dhPresent = true;
private static boolean lastIncompatible;
private static MethodHandle setupEventHandlers;
private static MethodHandle deletePipeline;
private static MethodHandle incompatible;
private static MethodHandle getDepthTex;
private static MethodHandle getFarPlane;
private static MethodHandle getNearPlane;
private static MethodHandle getDepthTexNoTranslucent;
private static MethodHandle checkFrame;
private static MethodHandle getRenderDistance;
private static MethodHandle renderShadowSolid;
private static MethodHandle renderShadowTranslucent;
private static MethodHandle onResolutionChanged;
private Object compatInternalInstance;

public DHCompat(NewWorldRenderingPipeline pipeline, boolean renderDHShadow) {
try {
if (dhPresent) {
compatInternalInstance = Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal").getDeclaredConstructor(pipeline.getClass(), boolean.class).newInstance(pipeline, renderDHShadow);
lastIncompatible = (boolean) incompatible.invoke(compatInternalInstance);
}
} catch (Throwable e) {
lastIncompatible = false;
if (e.getCause() instanceof ShaderCompileException sce) {
throw sce;
} else if (e instanceof InvocationTargetException ite) {
throw new RuntimeException("Unknown error loading Distant Horizons compatibility.", ite.getCause());
} else {
throw new RuntimeException("Unknown error loading Distant Horizons compatibility.", e);
}
}

}

public static Matrix4f getProjection() {
if (!dhPresent) {
return new Matrix4f(CapturedRenderingState.INSTANCE.getGbufferProjection());
}

Matrix4f projection = new Matrix4f(CapturedRenderingState.INSTANCE.getGbufferProjection());
return new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), DHCompat.getNearPlane(), DHCompat.getFarPlane());
}

public static void run() {
try {
if (LoadingModList.get().getModFileById("distanthorizons") != null) {
deletePipeline = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "clear", MethodType.methodType(void.class));
setupEventHandlers = MethodHandles.lookup().findStatic(Class.forName("net.coderbot.iris.compat.dh.LodRendererEvents"), "setupEventHandlers", MethodType.methodType(void.class));
getDepthTex = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getStoredDepthTex", MethodType.methodType(int.class));
getRenderDistance = MethodHandles.lookup().findStatic(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getRenderDistance", MethodType.methodType(int.class));
incompatible = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "incompatiblePack", MethodType.methodType(boolean.class));
getFarPlane = MethodHandles.lookup().findStatic(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getFarPlane", MethodType.methodType(float.class));
getNearPlane = MethodHandles.lookup().findStatic(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getNearPlane", MethodType.methodType(float.class));
getDepthTexNoTranslucent = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getDepthTexNoTranslucent", MethodType.methodType(int.class));
checkFrame = MethodHandles.lookup().findStatic(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "checkFrame", MethodType.methodType(boolean.class));
renderShadowSolid = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "renderShadowSolid", MethodType.methodType(void.class));
renderShadowTranslucent = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "renderShadowTranslucent", MethodType.methodType(void.class));
onResolutionChanged = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "onResolutionChanged", MethodType.methodType(void.class));

setupEventHandlers.invoke();
} else {
dhPresent = false;
}
} catch (Throwable e) {
dhPresent = false;

if (LoadingModList.get().getModFileById("distanthorizons") != null) {
if (e instanceof ExceptionInInitializerError eiie) {
throw new RuntimeException("Failure loading DH compat.", eiie.getCause());
} else {
throw new RuntimeException("DH 2.0 not found, yet Fabric claims it's there. Curious.", e);
}
} else {
Iris.logger.info("DH not found, and classes not found.");
}
}
}

public static boolean lastPackIncompatible() {
return dhPresent && hasRenderingEnabled() && lastIncompatible;
}

public static float getFarPlane() {
if (!dhPresent) return 0.01f;

try {
return (float) getFarPlane.invoke();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public static float getNearPlane() {
if (!dhPresent) return 0.01f;

try {
return (float) getNearPlane.invoke();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public static int getRenderDistance() {
if (!dhPresent) return Minecraft.getInstance().options.getEffectiveRenderDistance();

try {
return (int) getRenderDistance.invoke();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public int getFramebuffer() {
return fb.getId();
public static boolean checkFrame() {
if (!dhPresent) return false;

try {
return (boolean) checkFrame.invoke();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public static boolean hasRenderingEnabled() {
if (!dhPresent) {
return false;
}

return checkFrame();
}

public void clearPipeline() {
if (compatInternalInstance == null) return;

try {
deletePipeline.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public int getDepthTex() {
if (compatInternalInstance == null) throw new IllegalStateException("Couldn't find DH depth texture");

try {
return (int) getDepthTex.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public int getDepthTexNoTranslucent() {
if (compatInternalInstance == null) throw new IllegalStateException("Couldn't find DH depth texture");

try {
return (int) getDepthTexNoTranslucent.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public void renderShadowSolid() {
if (compatInternalInstance == null) return;

try {
renderShadowSolid.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public void setFramebuffer(GlFramebuffer fb) {
this.fb = fb;
public void renderShadowTranslucent() {
if (compatInternalInstance == null) return;

try {
renderShadowTranslucent.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public Object getInstance() {
return compatInternalInstance;
}

public void onResolutionChanged() {
if (compatInternalInstance == null) return;

try {
onResolutionChanged.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
}
Loading

0 comments on commit abe6d56

Please sign in to comment.