Merge Fade apply shaders

This commit is contained in:
James Seibel
2025-10-25 11:54:32 -05:00
parent dcaf334828
commit 3e7f160fcd
5 changed files with 30 additions and 148 deletions
@@ -23,23 +23,22 @@ import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.logging.DhLogger;
import com.seibel.distanthorizons.core.logging.DhLoggerBuilder;
import com.seibel.distanthorizons.core.render.glObject.GLState;
import com.seibel.distanthorizons.core.render.renderer.shaders.DhFadeApplyShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.DhFadeShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.FadeApplyShader;
import com.seibel.distanthorizons.core.util.math.Mat4f;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper;
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 org.lwjgl.opengl.GL32;
import java.nio.ByteBuffer;
/**
* Handles fading MC and DH together via {@link DhFadeShader} and {@link DhFadeApplyShader}. <br><br>
* Handles fading MC and DH together via {@link DhFadeShader} and {@link FadeApplyShader}. <br><br>
*
* {@link DhFadeShader} - draws the Fade to a texture. <br>
* {@link DhFadeApplyShader} - draws the Fade texture to MC's FrameBuffer. <br>
* {@link FadeApplyShader} - draws the Fade texture to DH's framebuffer. <br>
*/
public class DhFadeRenderer
{
@@ -74,7 +73,7 @@ public class DhFadeRenderer
this.init = true;
DhFadeShader.INSTANCE.init();
DhFadeApplyShader.INSTANCE.init();
FadeApplyShader.INSTANCE.init();
}
private void createFramebuffer(int width, int height)
@@ -139,8 +138,10 @@ public class DhFadeRenderer
profiler.popPush("Fade Apply");
DhFadeApplyShader.INSTANCE.fadeTexture = this.fadeTexture;
DhFadeApplyShader.INSTANCE.render(partialTicks);
FadeApplyShader.INSTANCE.fadeTexture = this.fadeTexture;
FadeApplyShader.INSTANCE.readFramebuffer = DhFadeShader.INSTANCE.frameBuffer;
FadeApplyShader.INSTANCE.drawFramebuffer = LodRenderer.INSTANCE.getActiveFramebufferId();
FadeApplyShader.INSTANCE.render(partialTicks);
}
catch (Exception e)
{
@@ -22,7 +22,8 @@ package com.seibel.distanthorizons.core.render.renderer;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.logging.DhLoggerBuilder;
import com.seibel.distanthorizons.core.render.glObject.GLState;
import com.seibel.distanthorizons.core.render.renderer.shaders.VanillaFadeApplyShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.DhFadeShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.FadeApplyShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.VanillaFadeShader;
import com.seibel.distanthorizons.core.util.math.Mat4f;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
@@ -36,10 +37,10 @@ import org.lwjgl.opengl.GL32;
import java.nio.ByteBuffer;
/**
* Handles fading MC and DH together via {@link VanillaFadeShader} and {@link VanillaFadeApplyShader}. <br><br>
* Handles fading MC and DH together via {@link VanillaFadeShader} and {@link FadeApplyShader}. <br><br>
*
* {@link VanillaFadeShader} - draws the Fade to a texture. <br>
* {@link VanillaFadeApplyShader} - draws the Fade texture to MC's FrameBuffer. <br>
* {@link FadeApplyShader} - draws the Fade texture to MC's FrameBuffer. <br>
*/
public class VanillaFadeRenderer
{
@@ -74,7 +75,7 @@ public class VanillaFadeRenderer
this.init = true;
VanillaFadeShader.INSTANCE.init();
VanillaFadeApplyShader.INSTANCE.init();
FadeApplyShader.INSTANCE.init();
}
private void createFramebuffer(int width, int height)
@@ -149,14 +150,16 @@ public class VanillaFadeRenderer
VanillaFadeShader.INSTANCE.setLevelMaxHeight(level.getMaxHeight());
VanillaFadeShader.INSTANCE.render(partialTicks);
profiler.popPush("Vanilla Fade Apply");
// Applying the fade texture is only needed if MC is drawing to their own frame buffer,
// otherwise we can directly render to their texture
if (MC_RENDER.mcRendersToFrameBuffer())
{
VanillaFadeApplyShader.INSTANCE.fadeTexture = this.fadeTexture;
VanillaFadeApplyShader.INSTANCE.render(partialTicks);
profiler.popPush("Vanilla Fade Apply");
FadeApplyShader.INSTANCE.fadeTexture = this.fadeTexture;
FadeApplyShader.INSTANCE.readFramebuffer = DhFadeShader.INSTANCE.frameBuffer;
FadeApplyShader.INSTANCE.drawFramebuffer = LodRenderer.INSTANCE.getActiveFramebufferId();
FadeApplyShader.INSTANCE.render(partialTicks);
}
profiler.pop();
@@ -176,7 +179,7 @@ public class VanillaFadeRenderer
public void free()
{
VanillaFadeShader.INSTANCE.free();
VanillaFadeApplyShader.INSTANCE.free();
FadeApplyShader.INSTANCE.free();
}
}
@@ -100,8 +100,6 @@ public class DhFadeShader extends AbstractShaderRenderer
float dhFarClipDistance = RenderUtil.getFarClipPlaneDistanceInBlocks();
// measured in blocks
float fadeStartDistance = dhFarClipDistance * 0.5f;
float fadeEndDistance = dhFarClipDistance * 0.9f;
@@ -21,8 +21,7 @@ package com.seibel.distanthorizons.core.render.renderer.shaders;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram;
import com.seibel.distanthorizons.core.render.renderer.DhFadeRenderer;
import com.seibel.distanthorizons.core.render.renderer.LodRenderer;
import com.seibel.distanthorizons.core.render.renderer.VanillaFadeRenderer;
import com.seibel.distanthorizons.core.render.renderer.ScreenQuad;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
@@ -32,12 +31,12 @@ import org.lwjgl.opengl.GL32;
* Draws the Fade texture onto Minecraft's FrameBuffer. <br><br>
*
* See Also: <br>
* {@link DhFadeRenderer} - Parent to this shader. <br>
* {@link DhFadeShader} - draws the Fade texture. <br>
* {@link VanillaFadeRenderer} - Parent to this shader. <br>
* {@link VanillaFadeShader} - draws the Fade texture. <br>
*/
public class DhFadeApplyShader extends AbstractShaderRenderer
public class FadeApplyShader extends AbstractShaderRenderer
{
public static DhFadeApplyShader INSTANCE = new DhFadeApplyShader();
public static FadeApplyShader INSTANCE = new FadeApplyShader();
private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
private static final IMinecraftGLWrapper GLMC = SingletonInjector.INSTANCE.get(IMinecraftGLWrapper.class);
@@ -46,6 +45,9 @@ public class DhFadeApplyShader extends AbstractShaderRenderer
public int fadeTexture;
public int readFramebuffer;
public int drawFramebuffer;
// uniforms
public int uFadeColorTextureUniform = -1;
@@ -102,8 +104,8 @@ public class DhFadeApplyShader extends AbstractShaderRenderer
// apply the rendered Fade to Minecraft's framebuffer
GLMC.glBindFramebuffer(GL32.GL_READ_FRAMEBUFFER, DhFadeShader.INSTANCE.frameBuffer);
GLMC.glBindFramebuffer(GL32.GL_DRAW_FRAMEBUFFER, LodRenderer.INSTANCE.getActiveFramebufferId());
GLMC.glBindFramebuffer(GL32.GL_READ_FRAMEBUFFER, this.readFramebuffer);
GLMC.glBindFramebuffer(GL32.GL_DRAW_FRAMEBUFFER, this.drawFramebuffer);
ScreenQuad.INSTANCE.render();
@@ -1,122 +0,0 @@
/*
* This file is part of the Distant Horizons mod
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.distanthorizons.core.render.renderer.shaders;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram;
import com.seibel.distanthorizons.core.render.renderer.VanillaFadeRenderer;
import com.seibel.distanthorizons.core.render.renderer.LodRenderer;
import com.seibel.distanthorizons.core.render.renderer.ScreenQuad;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import org.lwjgl.opengl.GL32;
/**
* Draws the Fade texture onto Minecraft's FrameBuffer. <br><br>
*
* See Also: <br>
* {@link VanillaFadeRenderer} - Parent to this shader. <br>
* {@link VanillaFadeShader} - draws the Fade texture. <br>
*/
public class VanillaFadeApplyShader extends AbstractShaderRenderer
{
public static VanillaFadeApplyShader INSTANCE = new VanillaFadeApplyShader();
private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
private static final IMinecraftGLWrapper GLMC = SingletonInjector.INSTANCE.get(IMinecraftGLWrapper.class);
public int fadeTexture;
// uniforms
public int uFadeColorTextureUniform = -1;
//=============//
// constructor //
//=============//
@Override
public void onInit()
{
this.shader = new ShaderProgram(
"shaders/normal.vert",
"shaders/fade/apply.frag",
"fragColor",
new String[]{ "vPosition" });
// uniform setup
this.uFadeColorTextureUniform = this.shader.getUniformLocation("uFadeColorTextureUniform");
}
//=============//
// render prep //
//=============//
@Override
protected void onApplyUniforms(float partialTicks)
{
GLMC.glActiveTexture(GL32.GL_TEXTURE0);
GLMC.glBindTexture(this.fadeTexture);
GL32.glUniform1i(this.uFadeColorTextureUniform, 0);
}
//========//
// render //
//========//
@Override
protected void onRender()
{
if (!MC_RENDER.mcRendersToFrameBuffer())
{
throw new IllegalStateException("If Minecraft is directly rendering to a texture the apply shader isn't needed, just draw the fade directly to the MC color texture.");
}
GLMC.disableBlend();
// Depth testing must be disabled otherwise this application shader won't apply anything.
// setting this isn't necessary in vanilla, but some mods may change this, requiring it to be set manually,
// it should be automatically restored after rendering is complete.
GLMC.disableDepthTest();
// apply the rendered Fade to Minecraft's framebuffer
GLMC.glBindFramebuffer(GL32.GL_READ_FRAMEBUFFER, VanillaFadeShader.INSTANCE.frameBuffer);
GLMC.glBindFramebuffer(GL32.GL_DRAW_FRAMEBUFFER, MC_RENDER.getTargetFramebuffer());
ScreenQuad.INSTANCE.render();
GLMC.enableDepthTest();
}
}