Skip to content

Commit

Permalink
Insane steps
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 14, 2024
1 parent 91904b6 commit 1409b0a
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 61 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.19.4"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
d.add(new JavaJarDependency(getProjectDir().resolve("custom_sodium").resolve("DistantHorizons-fabric-2.0.2-a-dev-1.19.4.jar"), null, new MavenId("maven.modrinth", "distanthorizons", "2.0.0-a-1.19.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
30 changes: 14 additions & 16 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.coderbot.iris.compat.dh;

import com.seibel.distanthorizons.api.DhApi;
import com.seibel.distanthorizons.core.config.Config;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
Expand All @@ -17,21 +15,9 @@
import java.util.function.Supplier;

public class DHCompat {
private GlFramebuffer fb;

public static Supplier<Matrix4f> getProjection() {
return () -> {
public static Matrix4f getProjection() {
Matrix4f projection = new Matrix4f(CapturedRenderingState.INSTANCE.getGbufferProjection());
return new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), 0.01f, DHCompat.getFarPlane());
};
}

public int getFramebuffer() {
return fb.getId();
}

public void setFramebuffer(GlFramebuffer fb) {
this.fb = fb;
return new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), DHCompat.getNearPlane(), DHCompat.getFarPlane());
}

private static Field renderingEnabled;
Expand All @@ -41,6 +27,7 @@ public void setFramebuffer(GlFramebuffer fb) {
private static MethodHandle deletePipeline;
private static MethodHandle getDepthTex;
private static MethodHandle getFarPlane;
private static MethodHandle getNearPlane;
private static MethodHandle getRenderDistance;

static {
Expand All @@ -54,6 +41,7 @@ public void setFramebuffer(GlFramebuffer fb) {
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));
}
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e) {
if (FabricLoader.getInstance().isModLoaded("distanthorizons")) {
Expand Down Expand Up @@ -103,6 +91,16 @@ public static float getFarPlane() {
}
}

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;

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.shaderpack.ProgramSource;
import net.coderbot.iris.uniforms.CapturedRenderingState;

public class DHCompatInternal {
public static DHCompatInternal INSTANCE = new DHCompatInternal();
Expand Down Expand Up @@ -45,10 +46,10 @@ public void prepareNewPipeline(NewWorldRenderingPipeline pipeline) {
if (pipeline.getDHWaterShader().isPresent()) {
ProgramSource water = pipeline.getDHWaterShader().get();
translucentProgram = IrisLodRenderProgram.createProgram(water.getName(), water, pipeline.getCustomUniforms(), pipeline);
dhWaterFramebuffer = pipeline.createDHFramebuffer(water);
dhWaterFramebuffer = pipeline.createDHFramebuffer(water, true);
}

dhTerrainFramebuffer = pipeline.createDHFramebuffer(terrain);
dhTerrainFramebuffer = pipeline.createDHFramebuffer(terrain, false);

if (translucentProgram == null) {
translucentProgram = solidProgram;
Expand All @@ -61,7 +62,9 @@ public void reconnectDHTextures(int depthTex) {
if (storedDepthTex != depthTex && dhTerrainFramebuffer != null) {
storedDepthTex = depthTex;
dhTerrainFramebuffer.addDepthAttachment(depthTex);
dhWaterFramebuffer.addDepthAttachment(depthTex);
if (dhWaterFramebuffer != null) {
dhWaterFramebuffer.addDepthAttachment(depthTex);
}
}
}

Expand Down Expand Up @@ -115,6 +118,10 @@ 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@
FogShader.class
}, remap = false)
public class MixinDHApplyShader extends AbstractShaderRenderer {
//@Redirect(method = "onRender", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/wrapperInterfaces/minecraft/IMinecraftRenderWrapper;getTargetFrameBuffer()I"))
private int changeFB(IMinecraftRenderWrapper instance) {
if (Iris.getPipelineManager().getPipelineNullable() instanceof NewWorldRenderingPipeline pipeline) {
return pipeline.getDHCompat().getFramebuffer();
} else {
return instance.getTargetFrameBuffer();
}
}

@Inject(method = "onRender", at = @At("HEAD"), cancellable = true)
private void onRender2(CallbackInfo ci) {
if (DHCompatInternal.INSTANCE.shouldOverride) ci.cancel();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.coderbot.iris.compat.dh.mixin;

import com.seibel.distanthorizons.core.pos.DhBlockPos;
import com.seibel.distanthorizons.core.render.RenderBufferHandler;
import com.seibel.distanthorizons.core.render.glObject.texture.DhFramebuffer;
import com.seibel.distanthorizons.core.render.renderer.LodRenderProgram;
import com.seibel.distanthorizons.core.render.renderer.LodRenderer;
Expand All @@ -14,6 +15,8 @@
import com.seibel.distanthorizons.coreapi.util.math.Vec3f;
import net.coderbot.iris.compat.dh.DHCompatInternal;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.irisshaders.iris.api.v0.IrisApi;
import net.minecraft.client.gui.screens.Screen;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL43C;
import org.lwjgl.system.MemoryStack;
Expand All @@ -38,20 +41,27 @@ public class MixinLodRenderer {
@Final
private static IMinecraftClientWrapper MC;

@Shadow
private boolean deferWaterRendering;
@Unique
private boolean atTranslucent;

@Inject(method = "drawLODs", at = @At("TAIL"))
private void setDeferred(IClientLevelWrapper clientLevelWrapper, Mat4f baseModelViewMatrix, Mat4f baseProjectionMatrix, float partialTicks, IProfilerWrapper profiler, CallbackInfo ci) {
this.deferWaterRendering = IrisApi.getInstance().isShaderPackInUse();
}

@Inject(method = "setActiveDepthTextureId", at = @At("TAIL"))
private void reloadDepth(int depthTextureId, CallbackInfo ci) {
DHCompatInternal.INSTANCE.reconnectDHTextures(depthTextureId);
}

@Inject(method = "drawLODs", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/RenderBufferHandler;renderTransparent(Lcom/seibel/distanthorizons/core/render/renderer/LodRenderer;)V"))
private void onTransparent(IClientLevelWrapper clientLevelWrapper, Mat4f baseModelViewMatrix, Mat4f baseProjectionMatrix, float partialTicks, IProfilerWrapper profiler, CallbackInfo ci) {
@Inject(method = "renderWaterOnly", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/RenderBufferHandler;renderTransparent(Lcom/seibel/distanthorizons/core/render/renderer/LodRenderer;)V"))
private void onTransparent(IProfilerWrapper profiler, float partialTicks, CallbackInfo ci) {
if (DHCompatInternal.INSTANCE.shouldOverride && DHCompatInternal.INSTANCE.getTranslucentFB() != null) {
DHCompatInternal.INSTANCE.getTranslucentShader().bind();
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
float nearClip = 0.1f;
float nearClip = RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks);
float farClip = (float) ((double) (RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));


Expand All @@ -62,15 +72,17 @@ private void onTransparent(IClientLevelWrapper clientLevelWrapper, Mat4f baseMod
atTranslucent = true;
}

@Redirect(method = "drawLODs", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL32;glClear(I)V"))
@Redirect(method = {
"drawLODs",
}, at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL32;glClear(I)V"))
private void properClear(int i) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
GL43C.glClear(GL43C.GL_DEPTH_BUFFER_BIT);
} else {
GL43C.glClear(i);
}
}
@Redirect(method = "drawLODs", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/renderer/LodRenderProgram;bind()V"))
@Redirect(method = "setupGLState", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/renderer/LodRenderProgram;bind()V"))
private void bindSolid(LodRenderProgram instance) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
instance.bind();
Expand All @@ -81,7 +93,10 @@ private void bindSolid(LodRenderProgram instance) {
}
}

@Redirect(method = "drawLODs", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/renderer/LodRenderProgram;unbind()V"))
@Redirect(method = {
"drawLODs",
"drawTranslucentLODs"
}, at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/renderer/LodRenderProgram;unbind()V"))
private void unbindSolid(LodRenderProgram instance) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
DHCompatInternal.INSTANCE.getSolidShader().unbind();
Expand All @@ -91,7 +106,7 @@ private void unbindSolid(LodRenderProgram instance) {
}
}

@Redirect(method = "drawLODs", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/glObject/texture/DhFramebuffer;bind()V"))
@Redirect(method = "setupGLState", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/glObject/texture/DhFramebuffer;bind()V"))
private void changeFramebuffer(DhFramebuffer instance) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
DHCompatInternal.INSTANCE.getSolidFB().bind();
Expand All @@ -100,7 +115,7 @@ private void changeFramebuffer(DhFramebuffer instance) {
}
}

@Redirect(method = "drawLODs", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/glObject/texture/DhFramebuffer;getId()I"))
@Redirect(method = "setupGLState", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/glObject/texture/DhFramebuffer;getId()I"))
private int changeFramebuffer2(DhFramebuffer instance) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
return DHCompatInternal.INSTANCE.getSolidFB().getId();
Expand All @@ -113,7 +128,7 @@ private int changeFramebuffer2(DhFramebuffer instance) {
private void fillUniformDataSolid(IClientLevelWrapper clientLevelWrapper, Mat4f baseModelViewMatrix, Mat4f baseProjectionMatrix, float partialTicks, IProfilerWrapper profiler, CallbackInfo ci) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
float nearClip = 0.1f;
float nearClip = RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks);
float farClip = (float)((double)(RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,6 @@ public float getSunPathRotation() {
return sunPathRotation;
}

@Override
public DHCompat getDHCompat() {
return null;
}

private RenderCondition getCondition(WorldRenderingPhase phase) {
if (isRenderingShadow) {
return RenderCondition.SHADOW;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,4 @@ public float getSunPathRotation() {
// No sun tilt
return 0;
}

@Override
public DHCompat getDHCompat() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,4 @@ public interface WorldRenderingPipeline {
boolean hasFeature(FeatureFlags flags);

float getSunPathRotation();

DHCompat getDHCompat();
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ public class NewWorldRenderingPipeline implements WorldRenderingPipeline, CoreWo
private final ShaderPack pack;
private PackShadowDirectives shadowDirectives;
private ColorSpace currentColorSpace;
private DHCompat dhCompat;

public NewWorldRenderingPipeline(ProgramSet programSet) throws IOException {
ShaderPrinter.resetPrintState();
Expand All @@ -205,7 +204,6 @@ public NewWorldRenderingPipeline(ProgramSet programSet) throws IOException {
this.shouldRenderMoon = programSet.getPackDirectives().shouldRenderMoon();
this.allowConcurrentCompute = programSet.getPackDirectives().getConcurrentCompute();
this.frustumCulling = programSet.getPackDirectives().shouldUseFrustumCulling();
this.dhCompat = new DHCompat();

this.resolver = new ProgramFallbackResolver(programSet);
this.pack = programSet.getPack();
Expand Down Expand Up @@ -485,7 +483,6 @@ public NewWorldRenderingPipeline(ProgramSet programSet) throws IOException {
this.shadowRenderer = null;
}

dhCompat.setFramebuffer(renderTargets.createGbufferFramebuffer(ImmutableSet.of(), new int[] { 0 }));
// TODO: Create fallback Sodium shaders if the pack doesn't provide terrain shaders
// Currently we use Sodium's shaders but they don't support EXP2 fog underwater.
this.sodiumTerrainPipeline = new SodiumTerrainPipeline(this, programSet, createTerrainSamplers,
Expand Down Expand Up @@ -1265,11 +1262,6 @@ public float getSunPathRotation() {
return sunPathRotation;
}

@Override
public DHCompat getDHCompat() {
return dhCompat;
}

protected AbstractTexture getWhitePixel() {
return whitePixel;
}
Expand Down Expand Up @@ -1301,8 +1293,8 @@ public CustomUniforms getCustomUniforms() {
return customUniforms;
}

public GlFramebuffer createDHFramebuffer(ProgramSource sources) {
return renderTargets.createDHFramebuffer(flippedAfterPrepare,
public GlFramebuffer createDHFramebuffer(ProgramSource sources, boolean trans) {
return renderTargets.createDHFramebuffer(trans ? flippedAfterTranslucent : flippedAfterPrepare,
sources.getDirectives().getDrawBuffers());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public static void generalCommonUniforms(UniformHolder uniforms, FrameUpdateNoti
.uniform3d(PER_FRAME, "skyColor", CommonUniforms::getSkyColor)
// DH
.uniform1f(PER_FRAME, "dhFarPlane", DHCompat::getFarPlane)
.uniform1f(PER_FRAME, "dhNearPlane", DHCompat::getNearPlane)
.uniform1i(PER_FRAME, "dhRenderDistance", DHCompat::getRenderDistance);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void addMatrixUniforms(UniformHolder uniforms, PackDirectives dire
// TODO: In some cases, gbufferProjectionInverse takes on a value much different than OptiFine...
// We need to audit Mojang's linear algebra.
addMatrix(uniforms, "Projection", CapturedRenderingState.INSTANCE::getGbufferProjection);
addDHMatrix(uniforms, "Projection", DHCompat.getProjection());
addDHMatrix(uniforms, "Projection", DHCompat::getProjection);
addShadowMatrix(uniforms, "ModelView", () ->
new Matrix4f(ShadowRenderer.createShadowModelView(directives.getSunPathRotation(), directives.getShadowDirectives().getIntervalSize()).last().pose()));
addShadowMatrix(uniforms, "Projection", () -> ShadowMatrices.createOrthoMatrix(directives.getShadowDirectives().getDistance()));
Expand Down

0 comments on commit 1409b0a

Please sign in to comment.