Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 14, 2024
1 parent 1409b0a commit 3af14ee
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 3 deletions.
12 changes: 12 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 @@ -28,6 +28,7 @@ public static Matrix4f getProjection() {
private static MethodHandle getDepthTex;
private static MethodHandle getFarPlane;
private static MethodHandle getNearPlane;
private static MethodHandle getDepthTexNoTranslucent;
private static MethodHandle getRenderDistance;

static {
Expand All @@ -42,6 +43,7 @@ public static Matrix4f getProjection() {
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));
}
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e) {
if (FabricLoader.getInstance().isModLoaded("distanthorizons")) {
Expand Down Expand Up @@ -81,6 +83,16 @@ public static int getDepthTex() {
}
}

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;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import net.coderbot.iris.Iris;
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.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.rendertarget.DepthTexture;
import net.coderbot.iris.shaderpack.ProgramSource;
import net.coderbot.iris.uniforms.CapturedRenderingState;

Expand All @@ -17,6 +19,7 @@ public class DHCompatInternal {
private IrisLodRenderProgram translucentProgram;
private GlFramebuffer dhTerrainFramebuffer;
private GlFramebuffer dhWaterFramebuffer;
private DepthTexture depthTexNoTranslucent;

private int storedDepthTex;
public boolean shouldOverride;
Expand Down Expand Up @@ -68,6 +71,15 @@ public void reconnectDHTextures(int depthTex) {
}
}

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

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

public void clear() {
if (solidProgram != null) {
solidProgram.free();
Expand Down Expand Up @@ -125,4 +137,10 @@ public float getNearPlane() {
public GlFramebuffer getTranslucentFB() {
return dhWaterFramebuffer;
}

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

return depthTexNoTranslucent.getTextureId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private IrisLodRenderProgram(String name, BlendModeOverride override, String ver

GL32.glBindAttribLocation(this.id, 0, "vPosition");
GL32.glBindAttribLocation(this.id, 1, "color");
GL32.glBindAttribLocation(this.id, 2, "irisExtra");

GlShader vert = new GlShader(ShaderType.VERTEX, name + ".vsh", vertex);
GL43C.glAttachShader(id, vert.getHandle());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package net.coderbot.iris.compat.dh.mixin;

import com.seibel.distanthorizons.core.level.ClientLevelModule;
import com.seibel.distanthorizons.core.level.IDhClientLevel;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper;
import net.irisshaders.iris.api.v0.IrisApi;
import net.minecraft.client.gui.screens.Screen;
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.callback.CallbackInfo;

import java.util.concurrent.atomic.AtomicReference;

@Mixin(ClientLevelModule.class)
public class MixinClientLevelModule {
@Shadow
@Final
public AtomicReference<ClientLevelModule.ClientRenderState> ClientRenderStateRef;
@Shadow
@Final
private IDhClientLevel parentClientLevel;
@Unique
private boolean hasEnabledShaders;

@Inject(method = "clientTick", at = @At("HEAD"), remap = false)
private void markDirtyIfChanged(CallbackInfo ci) {
if (IrisApi.getInstance().isShaderPackInUse() != hasEnabledShaders) {
hasEnabledShaders = IrisApi.getInstance().isShaderPackInUse();
if (this.ClientRenderStateRef.get() != null) {
IClientLevelWrapper clientLevelWrapper = this.parentClientLevel.getClientLevelWrapper();
if (clientLevelWrapper == null) {
return;
}

this.ClientRenderStateRef.get().quadtree.nodeIterator().forEachRemaining(node -> {
node.value.disposeBufferForRecreate();
});
this.ClientRenderStateRef.set(new ClientLevelModule.ClientRenderState(this.parentClientLevel, clientLevelWrapper, this.parentClientLevel.getFileHandler(), this.parentClientLevel.getSaveStructure()));

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.seibel.distanthorizons.core.pos.DhBlockPos;
import com.seibel.distanthorizons.core.render.RenderBufferHandler;
import com.seibel.distanthorizons.core.render.glObject.texture.DHDepthTexture;
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,7 @@
import com.seibel.distanthorizons.coreapi.util.math.Vec3d;
import com.seibel.distanthorizons.coreapi.util.math.Vec3f;
import net.coderbot.iris.compat.dh.DHCompatInternal;
import net.coderbot.iris.gl.texture.DepthCopyStrategy;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.irisshaders.iris.api.v0.IrisApi;
import net.minecraft.client.gui.screens.Screen;
Expand Down Expand Up @@ -43,6 +45,14 @@ public class MixinLodRenderer {

@Shadow
private boolean deferWaterRendering;
@Shadow
private DhFramebuffer framebuffer;
@Shadow
private DHDepthTexture depthTexture;
@Shadow
private int cachedWidth;
@Shadow
private int cachedHeight;
@Unique
private boolean atTranslucent;

Expand All @@ -56,8 +66,14 @@ private void reloadDepth(int depthTextureId, CallbackInfo ci) {
DHCompatInternal.INSTANCE.reconnectDHTextures(depthTextureId);
}

@Inject(method = "createColorAndDepthTextures", at = @At("TAIL"))
private void createDepthTex(int width, int height, CallbackInfo ci) {
DHCompatInternal.INSTANCE.createDepthTex(width, height);
}

@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) {
DepthCopyStrategy.fastest(false).copy(DHCompatInternal.INSTANCE.getSolidFB(), depthTexture.getTextureId(), null, DHCompatInternal.INSTANCE.getDepthTexNoTranslucent(), cachedWidth, cachedHeight);
if (DHCompatInternal.INSTANCE.shouldOverride && DHCompatInternal.INSTANCE.getTranslucentFB() != null) {
DHCompatInternal.INSTANCE.getTranslucentShader().bind();
Matrix4f projection = CapturedRenderingState.INSTANCE.getGbufferProjection();
Expand All @@ -73,7 +89,7 @@ private void onTransparent(IProfilerWrapper profiler, float partialTicks, Callba
}

@Redirect(method = {
"drawLODs",
"setupGLState",
}, at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL32;glClear(I)V"))
private void properClear(int i) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/net/coderbot/iris/compat/dh/mixin/MixinLodUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.coderbot.iris.compat.dh.mixin;

import com.seibel.distanthorizons.core.render.vertexFormat.LodVertexFormat;
import com.seibel.distanthorizons.core.util.LodUtil;
import net.coderbot.iris.compat.dh.DHCompatInternal;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(LodUtil.class)
public class MixinLodUtil {
@Shadow
@Final
public static LodVertexFormat LOD_VERTEX_FORMAT_EXTENDED;

@Shadow
@Final
public static LodVertexFormat LOD_VERTEX_FORMAT;

@Overwrite(remap = false)
public static LodVertexFormat getPreferredVertexFormat() {
return DHCompatInternal.INSTANCE.shouldOverride ? LOD_VERTEX_FORMAT_EXTENDED : LOD_VERTEX_FORMAT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void transform(
root.rename("gl_Color", "_vert_color");

if (parameters.type.glShaderType == ShaderType.VERTEX) {
root.replaceReferenceExpressions(t, "gl_Normal", "sorryYouCantUseNormals");
root.replaceReferenceExpressions(t, "gl_Normal", "_vert_normal");

}

Expand Down Expand Up @@ -115,10 +115,12 @@ public static void injectVertInit(
// translated from sodium's chunk_vertex.glsl
"vec3 _vert_position;",
"vec2 _vert_tex_light_coord;",
"int dhMaterialId;",
"vec4 _vert_color;",
"vec3 _vert_normal;",
"uniform float mircoOffset;",
"uniform vec3 modelOffset;",
"const vec3 irisNormals[6] = vec3[](vec3(0,-1,0),vec3(0,1,0),vec3(0,0,-1),vec3(0,0,1),vec3(-1,0,0),vec3(1,0,0));",
"void _vert_init() {" +
" uint meta = vPosition.a;\n"+
"uint mirco = (meta & 0xFF00u) >> 8u; // mirco offset which is a xyz 2bit value\n" +
Expand All @@ -134,10 +136,13 @@ public static void injectVertInit(
" mz = (mirco & 32u)!=0u ? -mz : mz;\n" +
" uint lights = meta & 0xFFu;\n" +
"_vert_position = (vPosition.xyz + vec3(mx, 0, mz));" +
"_vert_normal = irisNormals[irisExtra.y];" +
"dhMaterialId = int(irisExtra.x);" +
"_vert_tex_light_coord = vec2((float(lights/16u)+0.5) / 16.0, (mod(float(lights), 16.0)+0.5) / 16.0);" +
"_vert_color = color; }");
addIfNotExists(root, t, tree, "color", Type.F32VEC4, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "vPosition", Type.U32VEC4, StorageQualifier.StorageType.IN);
addIfNotExists(root, t, tree, "irisExtra", Type.U32VEC4, StorageQualifier.StorageType.IN);
tree.prependMainFunctionBody(t, "_vert_init();");
}
}
1 change: 1 addition & 0 deletions src/main/java/net/coderbot/iris/samplers/IrisSamplers.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static void addRenderTargetSamplers(SamplerHolder samplers, Supplier<Immu

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

public static void addNoiseSampler(SamplerHolder samplers, TextureAccess sampler) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/mixins.iris.compat.dh.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"package": "net.coderbot.iris.compat.dh.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"MixinClientLevelModule",
"MixinDHApplyShader",
"MixinLodRenderer"
"MixinLodRenderer",
"MixinLodUtil"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 3af14ee

Please sign in to comment.