Skip to content

Commit

Permalink
Distant Horizons support
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 25, 2024
1 parent 4f8c4c4 commit 9aa584e
Show file tree
Hide file tree
Showing 30 changed files with 1,211 additions and 62 deletions.
2 changes: 1 addition & 1 deletion buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void getModDependencies(ModDependencyCollector d) {

jij(d.addMaven(Maven.MAVEN_CENTRAL, new MavenId("io.github.douira:glsl-transformer:2.0.0-pre13"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME));
jij(d.addMaven(Maven.MAVEN_CENTRAL, new MavenId("org.antlr:antlr4-runtime:4.11.1"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME));
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "distanthorizons", "2.0.0-a-1.18.2"), ModDependencyFlag.COMPILE);
d.add(new JavaJarDependency(getProjectDir().resolve("custom_sodium").resolve("DistantHorizons-fabric-2.0.2-a-dev-1.18.2.jar"), null, new MavenId("maven.modrinth", "distanthorizons", "2.0.0-a-1.20.4")), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);

if (SODIUM) {
d.addMaven(FabricMaven.URL, new MavenId(FabricMaven.GROUP_ID + ".fabric-api", "fabric-api-base", "0.4.3+d7c144a8d2"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
Expand Down
159 changes: 153 additions & 6 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompat.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,162 @@
package net.coderbot.iris.compat.dh;

import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import com.mojang.math.Matrix4f;
import net.coderbot.iris.Iris;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.shadows.Matrix4fAccess;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.fabricmc.loader.api.FabricLoader;

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

public class DHCompat {
private GlFramebuffer fb;
private static Field renderingEnabled;
private static MethodHandle renderingEnabledGet;
private static Object compatInternalInstance;
private static MethodHandle createNewPipeline;
private static MethodHandle deletePipeline;
private static MethodHandle getDepthTex;
private static MethodHandle getFarPlane;
private static MethodHandle getNearPlane;
private static MethodHandle getDepthTexNoTranslucent;
private static MethodHandle getRenderDistance;
private static MethodHandle renderShadowSolid;
private static MethodHandle renderShadowTranslucent;

static {
try {
renderingEnabled = Class.forName("com.seibel.distanthorizons.core.config.Config$Client").getField("quickEnableRendering");
renderingEnabledGet = MethodHandles.lookup().findVirtual(Class.forName("com.seibel.distanthorizons.core.config.types.ConfigEntry"), "get", MethodType.methodType(Object.class));
if (FabricLoader.getInstance().isModLoaded("distanthorizons")) {
compatInternalInstance = Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal").getField("INSTANCE").get(null);
createNewPipeline = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "prepareNewPipeline", MethodType.methodType(void.class, NewWorldRenderingPipeline.class, boolean.class));
deletePipeline = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "clear", MethodType.methodType(void.class));
getDepthTex = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getStoredDepthTex", MethodType.methodType(int.class));
getRenderDistance = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getRenderDistance", MethodType.methodType(int.class));
getFarPlane = MethodHandles.lookup().findVirtual(Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal"), "getFarPlane", MethodType.methodType(float.class));
getNearPlane = MethodHandles.lookup().findVirtual(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));
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));
}
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e) {
if (FabricLoader.getInstance().isModLoaded("distanthorizons")) {
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 Matrix4f getProjection() {
/*
Matrix4f projection = new Matrix4f(CapturedRenderingState.INSTANCE.getGbufferProjection());
return new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), DHCompat.getNearPlane(), DHCompat.getFarPlane());
*/
Matrix4f matrix = CapturedRenderingState.INSTANCE.getGbufferProjection().copy();
((Matrix4fAccess) (Object) matrix).changeClipPlanes(DHCompat.getNearPlane(), DHCompat.getFarPlane());
return matrix;
}

public static void connectNewPipeline(NewWorldRenderingPipeline pipeline, boolean renderDhShadow) {
if (compatInternalInstance == null) return;
try {
createNewPipeline.invoke(compatInternalInstance, pipeline, renderDhShadow);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

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

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

public static 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 static 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 static float getFarPlane() {
if (compatInternalInstance == null) return 0.01f;

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

public static float getNearPlane() {
if (compatInternalInstance == null) return 0.01f;

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

public static int getRenderDistance() {
if (compatInternalInstance == null) return 0;

public int getFramebuffer() {
return fb.getId();
try {
return (int) getRenderDistance.invoke(compatInternalInstance);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

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

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

public static void renderShadowTranslucent() {
if (compatInternalInstance == null) return;

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

public static boolean hasRenderingEnabled() {
if (renderingEnabledGet == null) return false;

try {
return (boolean) renderingEnabledGet.invoke(renderingEnabled.get(null));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
193 changes: 193 additions & 0 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package net.coderbot.iris.compat.dh;

import com.seibel.distanthorizons.core.api.internal.ClientApi;
import com.seibel.distanthorizons.core.util.RenderUtil;
import com.seibel.distanthorizons.coreapi.util.math.Vec3f;
import loaderCommon.fabric.com.seibel.distanthorizons.common.wrappers.McObjectConverter;
import loaderCommon.fabric.com.seibel.distanthorizons.common.wrappers.world.ClientLevelWrapper;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.gl.texture.DepthBufferFormat;
import net.coderbot.iris.pipeline.ShadowRenderer;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.rendertarget.DepthTexture;
import net.coderbot.iris.shaderpack.ProgramSource;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.minecraft.client.Minecraft;

public class DHCompatInternal {
public static DHCompatInternal INSTANCE = new DHCompatInternal();
public boolean shouldOverrideShadow;
public boolean shouldOverride;
private IrisLodRenderProgram solidProgram;
private IrisLodRenderProgram translucentProgram;
private IrisLodRenderProgram shadowProgram;
private GlFramebuffer dhTerrainFramebuffer;
private GlFramebuffer dhWaterFramebuffer;
private GlFramebuffer dhShadowFramebuffer;
private DepthTexture depthTexNoTranslucent;
private int storedDepthTex;

public void prepareNewPipeline(NewWorldRenderingPipeline pipeline, boolean dhShadowEnabled) {
if (solidProgram != null) {
solidProgram.free();
solidProgram = null;

shouldOverride = false;
}

if (translucentProgram != null) {
translucentProgram.free();

translucentProgram = null;
}

if (shadowProgram != null) {
shadowProgram.free();

shadowProgram = null;
}

if (pipeline.getDHTerrainShader().isEmpty() && pipeline.getDHWaterShader().isEmpty()) {
Iris.logger.warn("No DH shader found in this pack.");
return;
}

ProgramSource terrain = pipeline.getDHTerrainShader().get();
solidProgram = IrisLodRenderProgram.createProgram(terrain.getName(), false, terrain, pipeline.getCustomUniforms(), pipeline);

if (pipeline.getDHWaterShader().isPresent()) {
ProgramSource water = pipeline.getDHWaterShader().get();
translucentProgram = IrisLodRenderProgram.createProgram(water.getName(), false, water, pipeline.getCustomUniforms(), pipeline);
dhWaterFramebuffer = pipeline.createDHFramebuffer(water, true);
}

if (pipeline.getDHShadowShader().isPresent() && dhShadowEnabled) {
ProgramSource shadow = pipeline.getDHShadowShader().get();
shadowProgram = IrisLodRenderProgram.createProgram(shadow.getName(), true, shadow, pipeline.getCustomUniforms(), pipeline);
dhShadowFramebuffer = pipeline.createDHFramebufferShadow(shadow);
shouldOverrideShadow = true;
} else {
shouldOverrideShadow = false;
}

dhTerrainFramebuffer = pipeline.createDHFramebuffer(terrain, false);

if (translucentProgram == null) {
translucentProgram = solidProgram;
}

shouldOverride = true;
}

public void reconnectDHTextures(int depthTex) {
if (storedDepthTex != depthTex && dhTerrainFramebuffer != null) {
storedDepthTex = depthTex;
dhTerrainFramebuffer.addDepthAttachment(depthTex);
if (dhWaterFramebuffer != null) {
dhWaterFramebuffer.addDepthAttachment(depthTex);
}
}
}

public void createDepthTex(int width, int height) {
if (depthTexNoTranslucent != null) {
depthTexNoTranslucent.destroy();
depthTexNoTranslucent = null;
}

depthTexNoTranslucent = new DepthTexture(width, height, DepthBufferFormat.DEPTH32F);
}

public void renderShadowSolid() {
ClientApi.INSTANCE.renderLods(ClientLevelWrapper.getWrapper(Minecraft.getInstance().level),
McObjectConverter.Convert(ShadowRenderer.MODELVIEW),
McObjectConverter.Convert(ShadowRenderer.PROJECTION),
CapturedRenderingState.INSTANCE.getTickDelta());
}

public void renderShadowTranslucent() {
ClientApi.INSTANCE.renderDeferredLods(ClientLevelWrapper.getWrapper(Minecraft.getInstance().level),
McObjectConverter.Convert(ShadowRenderer.MODELVIEW),
McObjectConverter.Convert(ShadowRenderer.PROJECTION),
CapturedRenderingState.INSTANCE.getTickDelta());
}

public void clear() {
if (solidProgram != null) {
solidProgram.free();
solidProgram = null;
}
if (translucentProgram != null) {
translucentProgram.free();
translucentProgram = null;
}
if (shadowProgram != null) {
shadowProgram.free();
shadowProgram = null;
}
shouldOverrideShadow = false;
shouldOverride = false;
dhTerrainFramebuffer = null;
dhWaterFramebuffer = null;
dhShadowFramebuffer = null;
storedDepthTex = -1;
}

public void setModelPos(Vec3f modelPos) {
solidProgram.bind();
solidProgram.setModelPos(modelPos);
translucentProgram.bind();
translucentProgram.setModelPos(modelPos);
solidProgram.bind();
}

public IrisLodRenderProgram getSolidShader() {
return solidProgram;
}

public GlFramebuffer getSolidFB() {
return dhTerrainFramebuffer;
}

public IrisLodRenderProgram getShadowShader() {
return shadowProgram;
}

public GlFramebuffer getShadowFB() {
return dhShadowFramebuffer;
}

public IrisLodRenderProgram getTranslucentShader() {
if (translucentProgram == null) {
return solidProgram;
}
return translucentProgram;
}

public int getStoredDepthTex() {
return storedDepthTex;
}

public int getRenderDistance() {
return RenderUtil.getFarClipPlaneDistanceInBlocks();
}

public float getFarPlane() {
return (float) ((double) (RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));
}

public float getNearPlane() {
return RenderUtil.getNearClipPlaneDistanceInBlocks(CapturedRenderingState.INSTANCE.getTickDelta());
}

public GlFramebuffer getTranslucentFB() {
return dhWaterFramebuffer;
}

public int getDepthTexNoTranslucent() {
if (depthTexNoTranslucent == null) return 0;

return depthTexNoTranslucent.getTextureId();
}
}
Loading

0 comments on commit 9aa584e

Please sign in to comment.