Skip to content

Commit

Permalink
Refractor it all
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Feb 2, 2024
1 parent 048da1e commit 523efe8
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 325 deletions.
115 changes: 49 additions & 66 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

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

public class DHCompat {
private static boolean dhPresent = true;

private static Object compatInternalInstance;
private static MethodHandle createNewPipeline;
private static MethodHandle deletePipeline;
Expand All @@ -32,12 +24,14 @@ public static Matrix4f getProjection()
private static MethodHandle renderShadowSolid;
private static MethodHandle renderShadowTranslucent;

public static void run()
{
try
{
if (FabricLoader.getInstance().isModLoaded("distanthorizons"))
{
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());
}

public static void run() {
try {
if (FabricLoader.getInstance().isModLoaded("distanthorizons")) {
LodRendererEvents.setupEventHandlers();

compatInternalInstance = Class.forName("net.coderbot.iris.compat.dh.DHCompatInternal").getField("INSTANCE").get(null);
Expand All @@ -51,132 +45,121 @@ public static void run()
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)
{
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e) {
dhPresent = false;

if (FabricLoader.getInstance().isModLoaded("distanthorizons"))
{
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;
try {
createNewPipeline.invoke(compatInternalInstance, pipeline, renderDhShadow);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
throw new RuntimeException(e);
}
}

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

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

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

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

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

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


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


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

return DhApi.Delayed.configs.graphics().renderingEnabled().getValue();
}
catch (NoClassDefFoundError e)
{
} 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
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@
import com.seibel.distanthorizons.coreapi.util.math.Vec3f;
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;
public boolean shouldOverride;
private IrisLodRenderProgram solidProgram;
private IrisLodRenderProgram translucentProgram;
private IrisLodRenderProgram shadowProgram;
private GlFramebuffer dhTerrainFramebuffer;
Expand All @@ -34,11 +31,16 @@ public class DHCompatInternal
private DhFrameBufferWrapper dhShadowFramebufferWrapper;
private DepthTexture depthTexNoTranslucent;
private boolean translucentDepthDirty;

private int storedDepthTex;
public boolean shouldOverride;

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 prepareNewPipeline(NewWorldRenderingPipeline pipeline, boolean dhShadowEnabled) {
if (solidProgram != null) {
Expand Down Expand Up @@ -183,6 +185,7 @@ public IrisLodRenderProgram getSolidShader() {
public GlFramebuffer getSolidFB() {
return dhTerrainFramebuffer;
}

public DhFrameBufferWrapper getSolidFBWrapper() {
return dhTerrainFramebufferWrapper;
}
Expand All @@ -194,6 +197,7 @@ public IrisLodRenderProgram getShadowShader() {
public GlFramebuffer getShadowFB() {
return dhShadowFramebuffer;
}

public DhFrameBufferWrapper getShadowFBWrapper() {
return dhShadowFramebufferWrapper;
}
Expand All @@ -212,15 +216,6 @@ public int getStoredDepthTex() {
public int getRenderDistance() {
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) {
Expand All @@ -234,7 +229,7 @@ public void copyTranslucents(int width, int height) {
}

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

public float getNearPlane() {
Expand Down
32 changes: 13 additions & 19 deletions src/main/java/net/coderbot/iris/compat/dh/DhFrameBufferWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,49 @@
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import org.lwjgl.opengl.GL32;

public class DhFrameBufferWrapper implements IDhApiFramebuffer
{
public class DhFrameBufferWrapper implements IDhApiFramebuffer {
private final GlFramebuffer framebuffer;


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



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

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

@Override
public void addDepthAttachment(int i, boolean b)
{
public void addDepthAttachment(int i, boolean b) {
// ignore
}

@Override
public int getId()
{
public int getId() {
return this.framebuffer.getId();
}

@Override
public int getStatus()
{
public int getStatus() {
this.bind();
int status = GL32.glCheckFramebufferStatus(GL32.GL_FRAMEBUFFER);
return status;
}

@Override
public void addColorAttachment(int i, int i1)
{
public void addColorAttachment(int i, int i1) {
// ignore
}

@Override
public void destroy()
{
public void destroy() {
// ignore
//this.framebuffer.destroy();
}
Expand Down
Loading

0 comments on commit 523efe8

Please sign in to comment.