Skip to content

Commit

Permalink
Working way better
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 12, 2024
1 parent 9ef7d58 commit caef1ae
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 25 deletions.
46 changes: 46 additions & 0 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
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;
import org.joml.Matrix4f;

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 {
private GlFramebuffer fb;

public static Supplier<Matrix4f> getProjection() {
return () -> {
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();
}
Expand All @@ -29,6 +39,9 @@ public void setFramebuffer(GlFramebuffer fb) {
private static Object compatInternalInstance;
private static MethodHandle createNewPipeline;
private static MethodHandle deletePipeline;
private static MethodHandle getDepthTex;
private static MethodHandle getFarPlane;
private static MethodHandle getRenderDistance;

static {
try {
Expand All @@ -38,6 +51,9 @@ public void setFramebuffer(GlFramebuffer fb) {
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));
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));
}
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e) {
if (FabricLoader.getInstance().isModLoaded("distanthorizons")) {
Expand Down Expand Up @@ -67,6 +83,36 @@ public static void clearPipeline() {
}
}

public static int getDepthTex() {
if (compatInternalInstance == null) return -1;

try {
return (int) getDepthTex.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 int getRenderDistance() {
if (compatInternalInstance == null) return 0;

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

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

Expand Down
43 changes: 36 additions & 7 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.coderbot.iris.compat.dh;

import com.mojang.blaze3d.systems.RenderSystem;
import com.seibel.distanthorizons.core.util.RenderUtil;
import com.seibel.distanthorizons.coreapi.util.math.Vec3f;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
Expand All @@ -22,12 +23,16 @@ public class DHCompatInternal {
public void prepareNewPipeline(NewWorldRenderingPipeline pipeline) {
if (solidProgram != null) {
solidProgram.free();
translucentProgram.free();
solidProgram = null;
translucentProgram = null;

shouldOverride = false;
}

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

translucentProgram = null;
}

if (pipeline.getDHTerrainShader().isEmpty() && pipeline.getDHWaterShader().isEmpty()) {
Iris.logger.warn("No DH shader found in this pack.");
Expand All @@ -43,21 +48,30 @@ public void prepareNewPipeline(NewWorldRenderingPipeline pipeline) {
dhTerrainFramebuffer = pipeline.createDHFramebuffer(terrain);
dhWaterFramebuffer = pipeline.createDHFramebuffer(water);

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

shouldOverride = true;
}

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

public void clear() {
solidProgram.free();
solidProgram = null;
translucentProgram.free();
translucentProgram = null;
if (solidProgram != null) {
solidProgram.free();
solidProgram = null;
}
if (translucentProgram != null) {
translucentProgram.free();
translucentProgram = null;
}
shouldOverride = false;
dhTerrainFramebuffer = null;
dhWaterFramebuffer = null;
Expand All @@ -81,9 +95,24 @@ public GlFramebuffer getSolidFB() {
}

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 GlFramebuffer getTranslucentFB() {
return dhWaterFramebuffer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.coderbot.iris.compat.dh.mixin;

import com.seibel.distanthorizons.core.render.renderer.shaders.AbstractShaderRenderer;
import com.seibel.distanthorizons.core.render.renderer.shaders.DhApplyShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.FogShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.SSAOApplyShader;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import net.coderbot.iris.Iris;
Expand All @@ -9,6 +11,7 @@
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.irisshaders.iris.api.v0.IrisApi;
import org.lwjgl.opengl.GL32;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -17,9 +20,10 @@

@Mixin(value = {
DhApplyShader.class,
SSAOApplyShader.class
SSAOApplyShader.class,
FogShader.class
}, remap = false)
public class MixinDHApplyShader {
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) {
Expand All @@ -33,4 +37,18 @@ private int changeFB(IMinecraftRenderWrapper instance) {
private void onRender2(CallbackInfo ci) {
if (DHCompatInternal.INSTANCE.shouldOverride) ci.cancel();
}

@Override
public void render(float partialTicks) {
if (DHCompatInternal.INSTANCE.shouldOverride) return;

this.init();
this.shader.bind();
this.onApplyUniforms(partialTicks);
int width = MC_RENDER.getTargetFrameBufferViewportWidth();
int height = MC_RENDER.getTargetFrameBufferViewportHeight();
GL32.glViewport(0, 0, width, height);
this.onRender();
this.shader.unbind();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -36,30 +37,36 @@ public class MixinLodRenderer {
@Final
private static IMinecraftClientWrapper MC;

@Unique
private boolean atTranslucent;

@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) {
DHCompatInternal.INSTANCE.getTranslucentShader().bind();
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
float nearClip = 0.1f;
float farClip = (float)((double)(RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));

if (DHCompatInternal.INSTANCE.shouldOverride) {
DHCompatInternal.INSTANCE.getTranslucentShader().bind();
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
float nearClip = 0.1f;
float farClip = (float) ((double) (RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));

DHCompatInternal.INSTANCE.getTranslucentShader().fillUniformData(new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), nearClip, farClip), CapturedRenderingState.INSTANCE.getGbufferModelView(), MC.getWrappedClientLevel().getMinHeight(), partialTicks);

DHCompatInternal.INSTANCE.getTranslucentFB().bind();
DHCompatInternal.INSTANCE.getTranslucentShader().fillUniformData(new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), nearClip, farClip), CapturedRenderingState.INSTANCE.getGbufferModelView(), MC.getWrappedClientLevel().getMinHeight(), partialTicks);

DHCompatInternal.INSTANCE.getTranslucentFB().bind();
}
atTranslucent = true;
}

@Redirect(method = "drawLODs", 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();
DHCompatInternal.INSTANCE.getSolidShader().bind();
atTranslucent = false;
} else {
instance.bind();
}
Expand Down Expand Up @@ -96,14 +103,12 @@ private int changeFramebuffer2(DhFramebuffer instance) {
@Redirect(method = "drawLODs", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/renderer/LodRenderProgram;fillUniformData(Lcom/seibel/distanthorizons/coreapi/util/math/Mat4f;III)V"))
private void fillUniformDataSolid(LodRenderProgram instance, Mat4f combinedMatrix, int lightmapBindPoint, int worldYOffset, int vanillaDrawDistance, IClientLevelWrapper clientLevelWrapper, Mat4f baseModelViewMatrix, Mat4f baseProjectionMatrix, float partialTicks) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
try (MemoryStack stack = MemoryStack.stackPush()) {
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
float nearClip = 0.1f;
float farClip = (float)((double)(RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
float nearClip = 0.1f;
float farClip = (float)((double)(RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));


DHCompatInternal.INSTANCE.getSolidShader().fillUniformData(new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), nearClip, farClip), CapturedRenderingState.INSTANCE.getGbufferModelView(), worldYOffset, partialTicks);
}
DHCompatInternal.INSTANCE.getSolidShader().fillUniformData(new Matrix4f().setPerspective(projection.perspectiveFov(), projection.m11() / projection.m00(), nearClip, farClip), CapturedRenderingState.INSTANCE.getGbufferModelView(), worldYOffset, partialTicks);
} else {
instance.fillUniformData(combinedMatrix, lightmapBindPoint, worldYOffset, vanillaDrawDistance);
}
Expand All @@ -115,7 +120,14 @@ private void override1(DhBlockPos pos, CallbackInfo ci) {
ci.cancel();
Vec3d cam = MC_RENDER.getCameraExactPosition();
Vec3f modelPos = new Vec3f((float)((double)pos.x - cam.x), (float)((double)pos.y - cam.y), (float)((double)pos.z - cam.z));
DHCompatInternal.INSTANCE.setModelPos(modelPos);
if (atTranslucent) {
DHCompatInternal.INSTANCE.getTranslucentShader().bind();
DHCompatInternal.INSTANCE.getTranslucentShader().setModelPos(modelPos);
} else {
DHCompatInternal.INSTANCE.getSolidShader().bind();
DHCompatInternal.INSTANCE.getSolidShader().setModelPos(modelPos);

}
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/coderbot/iris/samplers/IrisSamplers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.coderbot.iris.compat.dh.DHCompat;
import net.coderbot.iris.gbuffer_overrides.matching.InputAvailability;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.image.GlImage;
Expand Down Expand Up @@ -96,6 +97,9 @@ public static void addRenderTargetSamplers(SamplerHolder samplers, Supplier<Immu
samplers.addDynamicSampler(texture, name);
}
}

// Add the DH texture here, to make sure it's always visible.
//samplers.addDynamicSampler(TextureType.TEXTURE_2D, DHCompat::getDepthTex, null, "dhDepthTex");
}

public static void addNoiseSampler(SamplerHolder samplers, TextureAccess sampler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum ProgramId {
Water(ProgramGroup.Gbuffers, "water", Terrain),
HandWater(ProgramGroup.Gbuffers, "hand_water", Hand),
DhTerrain(ProgramGroup.Dh, "terrain"),
DhWater(ProgramGroup.Dh, "water"),
DhWater(ProgramGroup.Dh, "water", DhTerrain),

Final(ProgramGroup.Final, ""),
;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/coderbot/iris/uniforms/CommonUniforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.coderbot.iris.JomlConversions;
import net.coderbot.iris.compat.dh.DHCompat;
import net.coderbot.iris.gl.state.StateUpdateNotifiers;
import net.coderbot.iris.gl.uniform.DynamicUniformHolder;
import net.coderbot.iris.gl.uniform.UniformHolder;
Expand Down Expand Up @@ -154,7 +155,10 @@ public static void generalCommonUniforms(UniformHolder uniforms, FrameUpdateNoti
})
.uniform1f(PER_TICK, "rainStrength", CommonUniforms::getRainStrength)
.uniform1f(PER_TICK, "wetness", new SmoothedFloat(directives.getWetnessHalfLife(), directives.getDrynessHalfLife(), CommonUniforms::getRainStrength, updateNotifier))
.uniform3d(PER_FRAME, "skyColor", CommonUniforms::getSkyColor);
.uniform3d(PER_FRAME, "skyColor", CommonUniforms::getSkyColor)
// DH
.uniform1f(PER_FRAME, "dhFarPlane", DHCompat::getFarPlane)
.uniform1i(PER_FRAME, "dhRenderDistance", DHCompat::getRenderDistance);
}

private static boolean isOnGround() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/coderbot/iris/uniforms/MatrixUniforms.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.coderbot.iris.uniforms;

import net.coderbot.iris.compat.dh.DHCompat;
import org.joml.Matrix4f;
import net.coderbot.iris.gl.uniform.UniformHolder;
import net.coderbot.iris.pipeline.ShadowRenderer;
Expand All @@ -20,6 +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());
addShadowMatrix(uniforms, "ModelView", () ->
new Matrix4f(ShadowRenderer.createShadowModelView(directives.getSunPathRotation(), directives.getShadowDirectives().getIntervalSize()).last().pose()));
addShadowMatrix(uniforms, "Projection", () -> ShadowMatrices.createOrthoMatrix(directives.getShadowDirectives().getDistance()));
Expand All @@ -32,6 +34,13 @@ private static void addMatrix(UniformHolder uniforms, String name, Supplier<Matr
.uniformMatrix(PER_FRAME, "gbufferPrevious" + name, new Previous(supplier));
}

private static void addDHMatrix(UniformHolder uniforms, String name, Supplier<Matrix4f> supplier) {
uniforms
.uniformMatrix(PER_FRAME, "dh" + name, supplier)
.uniformMatrix(PER_FRAME, "dh" + name + "Inverse", new Inverted(supplier))
.uniformMatrix(PER_FRAME, "dhPrevious" + name, new Previous(supplier));
}

private static void addShadowMatrix(UniformHolder uniforms, String name, Supplier<Matrix4f> supplier) {
uniforms
.uniformMatrix(PER_FRAME, "shadow" + name, supplier)
Expand Down

0 comments on commit caef1ae

Please sign in to comment.