Skip to content

Commit

Permalink
TODO: depth tex access, far uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 12, 2024
1 parent aa3db2b commit 9ef7d58
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
21 changes: 15 additions & 6 deletions src/main/java/net/coderbot/iris/compat/dh/DHCompatInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ public void prepareNewPipeline(NewWorldRenderingPipeline pipeline) {
}

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

Expand All @@ -62,13 +59,17 @@ public void clear() {
translucentProgram.free();
translucentProgram = null;
shouldOverride = false;
dhTerrainFramebuffer = null;
dhWaterFramebuffer = null;
storedDepthTex = -1;
}

public void setModelPos(Vec3f modelPos) {
solidProgram.bind();
solidProgram.setModelPos(modelPos);
translucentProgram.bind();
translucentProgram.setModelPos(modelPos);
solidProgram.bind();
}

public IrisLodRenderProgram getSolidShader() {
Expand All @@ -78,4 +79,12 @@ public IrisLodRenderProgram getSolidShader() {
public GlFramebuffer getSolidFB() {
return dhTerrainFramebuffer;
}

public IrisLodRenderProgram getTranslucentShader() {
return translucentProgram;
}

public GlFramebuffer getTranslucentFB() {
return dhWaterFramebuffer;
}
}
28 changes: 17 additions & 11 deletions src/main/java/net/coderbot/iris/compat/dh/IrisLodRenderProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import net.coderbot.iris.pipeline.transform.TransformPatcher;
import net.coderbot.iris.samplers.IrisSamplers;
import net.coderbot.iris.shaderpack.ProgramSource;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.CommonUniforms;
import net.coderbot.iris.uniforms.builtin.BuiltinReplacementUniforms;
import net.coderbot.iris.uniforms.custom.CustomUniforms;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL32;
import org.lwjgl.system.MemoryStack;

import java.nio.FloatBuffer;
Expand Down Expand Up @@ -84,6 +86,12 @@ public static IrisLodRenderProgram createProgram(String name, ProgramSource sour
return new IrisLodRenderProgram(name, vertex2, fragment2, uniforms, pipeline);
}

public int tryGetUniformLocation2(CharSequence name) {
int i = GL32.glGetUniformLocation(this.id, name);
if (i == -1) Iris.logger.warn("Couldn't find " + name);
return i;
}

// Noise Uniforms

// This will bind AbstractVertexAttribute
Expand All @@ -106,17 +114,17 @@ private IrisLodRenderProgram(String name, String vertex, String fragment, Custom
samplers = samplerBuilder.build();
images = builder.build();

modelOffsetUniform = tryGetUniformLocation("modelOffset");
worldYOffsetUniform = tryGetUniformLocation("worldYOffset");
mircoOffsetUniform = tryGetUniformLocation("mircoOffset");
projectionUniform = tryGetUniformLocation("iris_ProjectionMatrix");
projectionInverseUniform = tryGetUniformLocation("iris_ProjectionMatrixInverse");
modelViewUniform = tryGetUniformLocation("iris_ModelViewMatrix");
modelViewInverseUniform = tryGetUniformLocation("iris_ModelViewMatrixInverse");
normalMatrix3fUniform = tryGetUniformLocation("iris_NormalMatrix");
modelOffsetUniform = tryGetUniformLocation2("modelOffset");
worldYOffsetUniform = tryGetUniformLocation2("worldYOffset");
mircoOffsetUniform = tryGetUniformLocation2("mircoOffset");
projectionUniform = tryGetUniformLocation2("iris_ProjectionMatrix");
projectionInverseUniform = tryGetUniformLocation2("iris_ProjectionMatrixInverse");
modelViewUniform = tryGetUniformLocation2("iris_ModelViewMatrix");
modelViewInverseUniform = tryGetUniformLocation2("iris_ModelViewMatrixInverse");
normalMatrix3fUniform = tryGetUniformLocation2("iris_NormalMatrix");

// Fog/Clip Uniforms
clipDistanceUniform = tryGetUniformLocation("clipDistance");
clipDistanceUniform = tryGetUniformLocation2("clipDistance");

// TODO: Add better use of the LODFormat thing
int vertexByteCount = LodUtil.LOD_VERTEX_FORMAT.getByteSize();
Expand Down Expand Up @@ -167,13 +175,11 @@ public void setUniform(int index, Matrix3f matrix) {
public void bind()
{
super.bind();
vao.bind();
}
// Override ShaderProgram.unbind()
public void unbind()
{
super.unbind();
vao.unbind();
ProgramUniforms.clearActiveUniforms();
ProgramSamplers.clearActiveSamplers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import com.seibel.distanthorizons.core.render.renderer.LodRenderProgram;
import com.seibel.distanthorizons.core.render.renderer.LodRenderer;
import com.seibel.distanthorizons.core.util.RenderUtil;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IProfilerWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper;
import com.seibel.distanthorizons.coreapi.util.math.Mat4f;
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.uniforms.CapturedRenderingState;
import org.joml.Matrix4f;
import org.lwjgl.system.MemoryStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -21,33 +24,41 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LodRenderer.class)
import java.nio.FloatBuffer;

@Mixin(value = LodRenderer.class, remap = false)
public class MixinLodRenderer {
@Shadow
@Final
private static IMinecraftRenderWrapper MC_RENDER;

@Shadow
@Final
private static IMinecraftClientWrapper MC;

@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"), cancellable = true)
@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) {
}
@Redirect(method = "drawVbo", at = @At(value = "INVOKE", target = "Lcom/seibel/distanthorizons/core/render/renderer/LodRenderProgram;bindVertexBuffer(I)V"))
private void changeVbo(LodRenderProgram instance, int vbo) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
DHCompatInternal.INSTANCE.getSolidShader().bindVertexBuffer(vbo);
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();

} else {
instance.bindVertexBuffer(vbo);
}
}

@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();
} else {
instance.bind();
Expand All @@ -58,6 +69,7 @@ private void bindSolid(LodRenderProgram instance) {
private void unbindSolid(LodRenderProgram instance) {
if (DHCompatInternal.INSTANCE.shouldOverride) {
DHCompatInternal.INSTANCE.getSolidShader().unbind();
instance.unbind();
} else {
instance.unbind();
}
Expand All @@ -84,14 +96,14 @@ 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) {
Matrix4f projection = new Matrix4f().set(baseProjectionMatrix.getValuesAsArray());
float nearClip = 0.1f;
float farClip = (float)((double)(RenderUtil.getFarClipPlaneDistanceInBlocks() + 512) * Math.sqrt(2.0));
float matNearClip = -((farClip + nearClip) / (farClip - nearClip));
float matFarClip = -(2.0F * farClip * nearClip / (farClip - nearClip));
projection.m22(matNearClip);
projection.m23(matFarClip);
DHCompatInternal.INSTANCE.getSolidShader().fillUniformData(projection, new Matrix4f().set(baseModelViewMatrix.getValuesAsArray()), worldYOffset, partialTicks);
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));


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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public static void transform(
"uniform mat4 iris_ProjectionMatrix;",
"uniform mat4 iris_ModelViewMatrix;",
// _draw_translation replaced with Chunks[_draw_id].offset.xyz
"vec4 getVertexPosition() { return vec4(modelOffset + _vert_position, 1.0); }");
"vec4 getVertexPosition() { return vec4(modelOffset + _vert_position, 1.0); }",
"vec4 getPos() { return vPosition; }");
root.replaceReferenceExpressions(t, "gl_Vertex", "getVertexPosition()");

// inject here so that _vert_position is available to the above. (injections
Expand Down

0 comments on commit 9ef7d58

Please sign in to comment.