Skip to content

Commit

Permalink
Merge pull request #2227 from jeseibel/1.20.3-dh-api
Browse files Browse the repository at this point in the history
Replace Distant Horizons mixins with API calls
  • Loading branch information
IMS212 authored Feb 1, 2024
2 parents 2a5c585 + afddcee commit 28617df
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 402 deletions.
9 changes: 8 additions & 1 deletion buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ 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.add(new JavaJarDependency(getProjectDir().resolve("custom_sodium").resolve("DistantHorizons-fabric-2.0.2-a-dev-1.20.4.jar"), null, new MavenId("maven.modrinth", "distanthorizons", "2.0.0-a-1.20.4")), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
d.add(new JavaJarDependency(
// Note: using the full jar is NOT recommended since mixing into DH may cause issues when DH refactors or moves to different MC versions
// using the API should prevent these sorts of issues and improve mod compatibility
//getProjectDir().resolve("custom_sodium").resolve("DistantHorizons-fabric-2.0.2-a-dev-1.20.4.jar"), // DH full jar
getProjectDir().resolve("custom_sodium").resolve("DistantHorizons-api-2.0.2-a-dev-1.20.4.jar"),
//null, // DH full jar
getProjectDir().resolve("custom_sodium").resolve("DistantHorizons-api-2.0.2-a-dev-1.20.4-sources.jar"),
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.17+93d8cb8253"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
Expand Down
82 changes: 56 additions & 26 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.coderbot.iris.compat.dh;

import com.seibel.distanthorizons.api.DhApi;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -10,18 +10,17 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.Supplier;

public class DHCompat {
public static Matrix4f getProjection() {
public class DHCompat
{
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());
}

private static Field renderingEnabled;
private static MethodHandle renderingEnabledGet;
private static boolean dhPresent = true;

private static Object compatInternalInstance;
private static MethodHandle createNewPipeline;
private static MethodHandle deletePipeline;
Expand All @@ -33,11 +32,14 @@ public static Matrix4f getProjection() {
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")) {
static
{
try
{
if (FabricLoader.getInstance().isModLoaded("distanthorizons"))
{
LodRendererEvents.setupEventHandlers();

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));
Expand All @@ -49,14 +51,21 @@ public static Matrix4f getProjection() {
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")) {
}
catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e)
{
dhPresent = false;

if (FabricLoader.getInstance().isModLoaded("distanthorizons"))
{
throw new RuntimeException("DH 2.0 not found, yet Fabric claims it's there. Curious.", e);
} else {
}
else
{
Iris.logger.info("DH not found, and classes not found.");
}
}
}
}
}

public static void connectNewPipeline(NewWorldRenderingPipeline pipeline, boolean renderDhShadow) {
if (compatInternalInstance == null) return;
Expand Down Expand Up @@ -147,13 +156,34 @@ public static void renderShadowTranslucent() {
}
}

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

try {
return (boolean) renderingEnabledGet.invoke(renderingEnabled.get(null));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public static boolean hasRenderingEnabled()
{
if (!dhPresent)
{
return false;
}


try
{
if (DhApi.Delayed.configs == null)
{
// DH hasn't finished loading yet
return false;
}

return DhApi.Delayed.configs.graphics().renderingEnabled().getValue();
}
catch (NoClassDefFoundError e)
{
// if Distant Horizons isn't present the dhPresent
// variable should already be set to false,
// but this try-catch is present just in case

dhPresent = false;
return false;
}
}

}
80 changes: 63 additions & 17 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package net.coderbot.iris.compat.dh;

import com.mojang.blaze3d.systems.RenderSystem;
import com.seibel.distanthorizons.core.api.internal.ClientApi;
import com.seibel.distanthorizons.core.util.RenderUtil;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper;
import com.seibel.distanthorizons.api.DhApi;
import com.seibel.distanthorizons.api.interfaces.override.rendering.IDhApiFramebuffer;
import com.seibel.distanthorizons.coreapi.DependencyInjection.OverrideInjector;
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.IrisRenderSystem;
import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.gl.texture.DepthBufferFormat;
import net.coderbot.iris.gl.texture.DepthCopyStrategy;
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;
import org.lwjgl.opengl.GL20C;

public class DHCompatInternal {
public class DHCompatInternal
{
public static DHCompatInternal INSTANCE = new DHCompatInternal();
public boolean shouldOverrideShadow;

private IrisLodRenderProgram solidProgram;
private IrisLodRenderProgram translucentProgram;
private IrisLodRenderProgram shadowProgram;
private GlFramebuffer dhTerrainFramebuffer;
private DhFrameBufferWrapper dhTerrainFramebufferWrapper;
private GlFramebuffer dhWaterFramebuffer;
private GlFramebuffer dhShadowFramebuffer;
private DhFrameBufferWrapper dhShadowFramebufferWrapper;
private DepthTexture depthTexNoTranslucent;
private boolean translucentDepthDirty;

private int storedDepthTex;
public boolean shouldOverride;



public void prepareNewPipeline(NewWorldRenderingPipeline pipeline, boolean dhShadowEnabled) {
if (solidProgram != null) {
solidProgram.free();
Expand Down Expand Up @@ -72,13 +79,15 @@ public void prepareNewPipeline(NewWorldRenderingPipeline pipeline, boolean dhSha
shadowProgram = IrisLodRenderProgram.createProgram(shadow.getName(), true, false, shadow, pipeline.getCustomUniforms(), pipeline);
if (pipeline.hasShadowRenderTargets()) {
dhShadowFramebuffer = pipeline.createDHFramebufferShadow(shadow);
dhShadowFramebufferWrapper = new DhFrameBufferWrapper(dhShadowFramebuffer);
}
shouldOverrideShadow = true;
} else {
shouldOverrideShadow = false;
}

dhTerrainFramebuffer = pipeline.createDHFramebuffer(terrain, false);
dhTerrainFramebufferWrapper = new DhFrameBufferWrapper(dhTerrainFramebuffer);

if (translucentProgram == null) {
translucentProgram = solidProgram;
Expand All @@ -103,21 +112,25 @@ public void createDepthTex(int width, int height) {
depthTexNoTranslucent = null;
}

translucentDepthDirty = true;

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());
// FIXME doesn't appear to do anything
//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());
// FIXME doesn't appear to do anything
//ClientApi.INSTANCE.renderDeferredLods(ClientLevelWrapper.getWrapper(Minecraft.getInstance().level),
// McObjectConverter.Convert(ShadowRenderer.MODELVIEW),
// McObjectConverter.Convert(ShadowRenderer.PROJECTION),
// CapturedRenderingState.INSTANCE.getTickDelta());
}

public void clear() {
Expand All @@ -139,6 +152,12 @@ public void clear() {
dhWaterFramebuffer = null;
dhShadowFramebuffer = null;
storedDepthTex = -1;
translucentDepthDirty = true;

OverrideInjector.INSTANCE.unbind(IDhApiFramebuffer.class, dhTerrainFramebufferWrapper);
OverrideInjector.INSTANCE.unbind(IDhApiFramebuffer.class, dhShadowFramebufferWrapper);
dhTerrainFramebufferWrapper = null;
dhShadowFramebufferWrapper = null;
}

public void setModelPos(Vec3f modelPos) {
Expand All @@ -156,6 +175,9 @@ public IrisLodRenderProgram getSolidShader() {
public GlFramebuffer getSolidFB() {
return dhTerrainFramebuffer;
}
public DhFrameBufferWrapper getSolidFBWrapper() {
return dhTerrainFramebufferWrapper;
}

public IrisLodRenderProgram getShadowShader() {
return shadowProgram;
Expand All @@ -164,6 +186,9 @@ public IrisLodRenderProgram getShadowShader() {
public GlFramebuffer getShadowFB() {
return dhShadowFramebuffer;
}
public DhFrameBufferWrapper getShadowFBWrapper() {
return dhShadowFramebufferWrapper;
}

public IrisLodRenderProgram getTranslucentShader() {
if (translucentProgram == null) {
Expand All @@ -177,15 +202,35 @@ public int getStoredDepthTex() {
}

public int getRenderDistance() {
return RenderUtil.getFarClipPlaneDistanceInBlocks();
return getDhBlockRenderDistance();
}
public static int getDhBlockRenderDistance() {
if (DhApi.Delayed.configs == null)
{
// Called before DH has finished setup
return 0;
}

return DhApi.Delayed.configs.graphics().chunkRenderDistance().getValue() * 16;
}

public void copyTranslucents(int width, int height) {
if (translucentDepthDirty) {
translucentDepthDirty = false;
RenderSystem.bindTexture(depthTexNoTranslucent.getTextureId());
dhTerrainFramebuffer.bindAsReadBuffer();
IrisRenderSystem.copyTexImage2D(GL20C.GL_TEXTURE_2D, 0, DepthBufferFormat.DEPTH32F.getGlInternalFormat(), 0, 0, width, height, 0);
} else {
DepthCopyStrategy.fastest(false).copy(dhTerrainFramebuffer, storedDepthTex, null, depthTexNoTranslucent.getTextureId(), width, height);
}
}

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

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

public GlFramebuffer getTranslucentFB() {
Expand All @@ -197,4 +242,5 @@ public int getDepthTexNoTranslucent() {

return depthTexNoTranslucent.getTextureId();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.coderbot.iris.compat.dh;

import com.seibel.distanthorizons.api.interfaces.override.rendering.IDhApiFramebuffer;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import org.lwjgl.opengl.GL32;

public class DhFrameBufferWrapper implements IDhApiFramebuffer
{
private final GlFramebuffer framebuffer;


public DhFrameBufferWrapper(GlFramebuffer framebuffer)
{
this.framebuffer = framebuffer;
}



@Override
public boolean overrideThisFrame()
{
return true;
}

@Override
public void bind()
{
this.framebuffer.bind();
}

@Override
public void addDepthAttachment(int i, boolean b)
{
// ignore
}
@Override
public int getId()
{
return this.framebuffer.getId();
}
@Override
public int getStatus()
{
this.bind();
int status = GL32.glCheckFramebufferStatus(GL32.GL_FRAMEBUFFER);
return status;
}
@Override
public void addColorAttachment(int i, int i1)
{
// ignore
}
@Override
public void destroy()
{
// ignore
//this.framebuffer.destroy();
}

}
Loading

0 comments on commit 28617df

Please sign in to comment.