From 8f42acbd65cec9ab856af9643a1bc91dd1734bc8 Mon Sep 17 00:00:00 2001 From: NULL511 Date: Sun, 10 Sep 2023 04:15:35 -0400 Subject: [PATCH] simplify matrix usage; uniform cleanup --- .../core/render/renderer/LodRenderer.java | 11 ++- .../core/render/renderer/SSAORenderer.java | 4 +- .../render/renderer/shaders/FogShader.java | 14 +--- .../renderer/shaders/SSAOApplyShader.java | 53 ++++++------ .../render/renderer/shaders/SSAOShader.java | 83 +++++++++---------- 5 files changed, 76 insertions(+), 89 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/LodRenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/LodRenderer.java index dee4ede60..2d9609681 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/LodRenderer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/LodRenderer.java @@ -243,16 +243,23 @@ public class LodRenderer { this.shaderProgram.free(); this.shaderProgram = new LodRenderProgram(newFogConfig); + FogShader.INSTANCE.free(); FogShader.INSTANCE = new FogShader(newFogConfig); } this.shaderProgram.bind(); } + GL32.glActiveTexture(GL32.GL_TEXTURE0); /*---------Get required data--------*/ int vanillaBlockRenderedDistance = MC_RENDER.getRenderDistance() * LodUtil.CHUNK_WIDTH; - Mat4f modelViewProjectionMatrix = RenderUtil.createCombinedModelViewProjectionMatrix(baseProjectionMatrix, baseModelViewMatrix, partialTicks); + //Mat4f modelViewProjectionMatrix = RenderUtil.createCombinedModelViewProjectionMatrix(baseProjectionMatrix, baseModelViewMatrix, partialTicks); + + Mat4f projectionMatrix = RenderUtil.createLodProjectionMatrix(baseProjectionMatrix, partialTicks); + + Mat4f modelViewProjectionMatrix = new Mat4f(projectionMatrix); + modelViewProjectionMatrix.multiply(RenderUtil.createLodModelViewMatrix(baseModelViewMatrix)); /*---------Fill uniform data--------*/ this.shaderProgram.fillUniformData(modelViewProjectionMatrix, /*Light map = GL_TEXTURE0*/ 0, @@ -283,12 +290,12 @@ public class LodRenderer if (Config.Client.Advanced.Graphics.Ssao.enabled.get()) { profiler.popPush("LOD SSAO"); + SSAOShader.INSTANCE.setProjectionMatrix(projectionMatrix); SSAORenderer.INSTANCE.render(minecraftGlState, partialTicks); } profiler.popPush("LOD Fog"); - // TODO add the model view/projection matrices to the render() function FogShader.INSTANCE.setModelViewProjectionMatrix(modelViewProjectionMatrix); FogShader.INSTANCE.render(partialTicks); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/SSAORenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/SSAORenderer.java index 0cbb038d1..9a52a753a 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/SSAORenderer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/SSAORenderer.java @@ -106,12 +106,12 @@ public class SSAORenderer this.createFramebuffer(width, height); } - SSAOShader.INSTANCE.ssaoFramebuffer = this.ssaoFramebuffer; + SSAOShader.INSTANCE.FrameBuffer = this.ssaoFramebuffer; SSAOShader.INSTANCE.render(partialTicks); primaryState.RestoreFrameBuffer(); - SSAOApplyShader.INSTANCE.ssaoTexture = this.ssaoTexture; + SSAOApplyShader.INSTANCE.BufferTexture = this.ssaoTexture; SSAOApplyShader.INSTANCE.render(partialTicks); state.restore(); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java index 22b762b4f..d66b06eb8 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java @@ -45,6 +45,7 @@ public class FogShader extends AbstractShaderRenderer private final LodFogConfig fogConfig; + private Mat4f inverseMvmProjMatrix; public int gInvertedModelViewProjectionUniform; public int gDepthMapUniform; @@ -92,7 +93,7 @@ public class FogShader extends AbstractShaderRenderer @Override protected void onApplyUniforms(float partialTicks) { - this.shader.bind(); + this.shader.setUniform(this.gInvertedModelViewProjectionUniform, this.inverseMvmProjMatrix); int lodDrawDistance = RenderUtil.getFarClipPlaneDistanceInBlocks(); int vanillaDrawDistance = MC_RENDER.getRenderDistance() * LodUtil.CHUNK_WIDTH; @@ -138,15 +139,8 @@ public class FogShader extends AbstractShaderRenderer public void setModelViewProjectionMatrix(Mat4f combinedModelViewProjectionMatrix) { - this.init(); - - this.shader.bind(); - - Mat4f inverseMvmProjMatrix = new Mat4f(combinedModelViewProjectionMatrix); - inverseMvmProjMatrix.invert(); - this.shader.setUniform(this.gInvertedModelViewProjectionUniform, inverseMvmProjMatrix); - - this.shader.unbind(); + this.inverseMvmProjMatrix = new Mat4f(combinedModelViewProjectionMatrix); + this.inverseMvmProjMatrix.invert(); } @Override diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOApplyShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOApplyShader.java index 44776d154..66d1dbfbe 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOApplyShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOApplyShader.java @@ -30,20 +30,15 @@ public class SSAOApplyShader extends AbstractShaderRenderer { public static SSAOApplyShader INSTANCE = new SSAOApplyShader(); - public int ssaoTexture; + public int BufferTexture; - // apply uniforms - private final ApplyShaderUniforms applyShaderUniforms = new ApplyShaderUniforms(); - - private static class ApplyShaderUniforms - { - public int gSSAOMapUniform; - public int gDepthMapUniform; - public int gViewSizeUniform; - public int gBlurRadiusUniform; - public int gNearUniform; - public int gFarUniform; - } + // uniforms + public int gSSAOMapUniform; + public int gDepthMapUniform; + public int gViewSizeUniform; + public int gBlurRadiusUniform; + public int gNearUniform; + public int gFarUniform; @Override @@ -56,12 +51,12 @@ public class SSAOApplyShader extends AbstractShaderRenderer new String[]{"vPosition"}); // uniform setup - this.applyShaderUniforms.gSSAOMapUniform = this.shader.getUniformLocation("gSSAOMap"); - this.applyShaderUniforms.gDepthMapUniform = this.shader.getUniformLocation("gDepthMap"); - this.applyShaderUniforms.gViewSizeUniform = this.shader.tryGetUniformLocation("gViewSize"); - this.applyShaderUniforms.gBlurRadiusUniform = this.shader.tryGetUniformLocation("gBlurRadius"); - this.applyShaderUniforms.gNearUniform = this.shader.tryGetUniformLocation("gNear"); - this.applyShaderUniforms.gFarUniform = this.shader.tryGetUniformLocation("gFar"); + this.gSSAOMapUniform = this.shader.getUniformLocation("gSSAOMap"); + this.gDepthMapUniform = this.shader.getUniformLocation("gDepthMap"); + this.gViewSizeUniform = this.shader.tryGetUniformLocation("gViewSize"); + this.gBlurRadiusUniform = this.shader.tryGetUniformLocation("gBlurRadius"); + this.gNearUniform = this.shader.tryGetUniformLocation("gNear"); + this.gFarUniform = this.shader.tryGetUniformLocation("gFar"); } @Override @@ -69,32 +64,32 @@ public class SSAOApplyShader extends AbstractShaderRenderer { GL32.glActiveTexture(GL32.GL_TEXTURE0); GL32.glBindTexture(GL32.GL_TEXTURE_2D, MC_RENDER.getDepthTextureId()); - GL32.glUniform1i(this.applyShaderUniforms.gDepthMapUniform, 0); + GL32.glUniform1i(this.gDepthMapUniform, 0); GL32.glActiveTexture(GL32.GL_TEXTURE1); - GL32.glBindTexture(GL32.GL_TEXTURE_2D, this.ssaoTexture); - GL32.glUniform1i(this.applyShaderUniforms.gSSAOMapUniform, 1); + GL32.glBindTexture(GL32.GL_TEXTURE_2D, this.BufferTexture); + GL32.glUniform1i(this.gSSAOMapUniform, 1); - GL32.glUniform1i(this.applyShaderUniforms.gBlurRadiusUniform, + GL32.glUniform1i(this.gBlurRadiusUniform, Config.Client.Advanced.Graphics.Ssao.blurRadius.get()); - if (this.applyShaderUniforms.gViewSizeUniform >= 0) + if (this.gViewSizeUniform >= 0) { - GL32.glUniform2f(this.applyShaderUniforms.gViewSizeUniform, + GL32.glUniform2f(this.gViewSizeUniform, MC_RENDER.getTargetFrameBufferViewportWidth(), MC_RENDER.getTargetFrameBufferViewportHeight()); } - if (this.applyShaderUniforms.gNearUniform >= 0) + if (this.gNearUniform >= 0) { - GL32.glUniform1f(this.applyShaderUniforms.gNearUniform, + GL32.glUniform1f(this.gNearUniform, RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks)); } - if (this.applyShaderUniforms.gFarUniform >= 0) + if (this.gFarUniform >= 0) { float far = (float) ((RenderUtil.getFarClipPlaneDistanceInBlocks() + LodUtil.REGION_WIDTH) * Math.sqrt(2)); - GL32.glUniform1f(this.applyShaderUniforms.gFarUniform, far); + GL32.glUniform1f(this.gFarUniform, far); } } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java index 88e02a7cb..34eee4b96 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/SSAOShader.java @@ -22,8 +22,6 @@ package com.seibel.distanthorizons.core.render.renderer.shaders; import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram; import com.seibel.distanthorizons.core.render.renderer.ScreenQuad; -import com.seibel.distanthorizons.core.util.LodUtil; -import com.seibel.distanthorizons.core.util.RenderUtil; import com.seibel.distanthorizons.coreapi.util.math.Mat4f; import org.lwjgl.opengl.GL32; @@ -31,22 +29,20 @@ public class SSAOShader extends AbstractShaderRenderer { public static SSAOShader INSTANCE = new SSAOShader(); - public int ssaoFramebuffer; + public int FrameBuffer; + + private Mat4f perspective; + private Mat4f invertedPerspective; // uniforms - private final SsaoShaderUniforms ssaoShaderUniforms = new SsaoShaderUniforms(); - - private static class SsaoShaderUniforms - { - public int gProjUniform; - public int gInvProjUniform; - public int gSampleCountUniform; - public int gRadiusUniform; - public int gStrengthUniform; - public int gMinLightUniform; - public int gBiasUniform; - public int gDepthMapUniform; - } + public int gProjUniform; + public int gInvProjUniform; + public int gSampleCountUniform; + public int gRadiusUniform; + public int gStrengthUniform; + public int gMinLightUniform; + public int gBiasUniform; + public int gDepthMapUniform; @Override @@ -56,55 +52,50 @@ public class SSAOShader extends AbstractShaderRenderer "fragColor", new String[]{"vPosition"}); // uniform setup - this.ssaoShaderUniforms.gProjUniform = this.shader.getUniformLocation("gProj"); - this.ssaoShaderUniforms.gInvProjUniform = this.shader.getUniformLocation("gInvProj"); - this.ssaoShaderUniforms.gSampleCountUniform = this.shader.getUniformLocation("gSampleCount"); - this.ssaoShaderUniforms.gRadiusUniform = this.shader.getUniformLocation("gRadius"); - this.ssaoShaderUniforms.gStrengthUniform = this.shader.getUniformLocation("gStrength"); - this.ssaoShaderUniforms.gMinLightUniform = this.shader.getUniformLocation("gMinLight"); - this.ssaoShaderUniforms.gBiasUniform = this.shader.getUniformLocation("gBias"); - this.ssaoShaderUniforms.gDepthMapUniform = this.shader.getUniformLocation("gDepthMap"); + this.gProjUniform = this.shader.getUniformLocation("gProj"); + this.gInvProjUniform = this.shader.getUniformLocation("gInvProj"); + this.gSampleCountUniform = this.shader.getUniformLocation("gSampleCount"); + this.gRadiusUniform = this.shader.getUniformLocation("gRadius"); + this.gStrengthUniform = this.shader.getUniformLocation("gStrength"); + this.gMinLightUniform = this.shader.getUniformLocation("gMinLight"); + this.gBiasUniform = this.shader.getUniformLocation("gBias"); + this.gDepthMapUniform = this.shader.getUniformLocation("gDepthMap"); + } + + public void setProjectionMatrix(Mat4f perspective) + { + this.perspective = perspective; + + this.invertedPerspective = new Mat4f(perspective); + this.invertedPerspective.invert(); } @Override protected void onApplyUniforms(float partialTicks) { - int width = MC_RENDER.getTargetFrameBufferViewportWidth(); - int height = MC_RENDER.getTargetFrameBufferViewportHeight(); - float near = RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks); - float far = (float) ((RenderUtil.getFarClipPlaneDistanceInBlocks() + LodUtil.REGION_WIDTH) * Math.sqrt(2)); + this.shader.setUniform(this.gProjUniform, this.perspective); - Mat4f perspective = Mat4f.perspective( - (float) MC_RENDER.getFov(partialTicks), - width / (float) height, - near, far); + this.shader.setUniform(this.gInvProjUniform, this.invertedPerspective); - Mat4f invertedPerspective = new Mat4f(perspective); - invertedPerspective.invert(); - - this.shader.setUniform(this.ssaoShaderUniforms.gProjUniform, perspective); - - this.shader.setUniform(this.ssaoShaderUniforms.gInvProjUniform, invertedPerspective); - - this.shader.setUniform(this.ssaoShaderUniforms.gSampleCountUniform, + this.shader.setUniform(this.gSampleCountUniform, Config.Client.Advanced.Graphics.Ssao.sampleCount.get()); - this.shader.setUniform(this.ssaoShaderUniforms.gRadiusUniform, + this.shader.setUniform(this.gRadiusUniform, Config.Client.Advanced.Graphics.Ssao.radius.get().floatValue()); - this.shader.setUniform(this.ssaoShaderUniforms.gStrengthUniform, + this.shader.setUniform(this.gStrengthUniform, Config.Client.Advanced.Graphics.Ssao.strength.get().floatValue()); - this.shader.setUniform(this.ssaoShaderUniforms.gMinLightUniform, + this.shader.setUniform(this.gMinLightUniform, Config.Client.Advanced.Graphics.Ssao.minLight.get().floatValue()); - this.shader.setUniform(this.ssaoShaderUniforms.gBiasUniform, + this.shader.setUniform(this.gBiasUniform, Config.Client.Advanced.Graphics.Ssao.bias.get().floatValue()); GL32.glActiveTexture(GL32.GL_TEXTURE0); GL32.glBindTexture(GL32.GL_TEXTURE_2D, MC_RENDER.getDepthTextureId()); - GL32.glUniform1i(this.ssaoShaderUniforms.gDepthMapUniform, 0); + GL32.glUniform1i(this.gDepthMapUniform, 0); } @@ -115,7 +106,7 @@ public class SSAOShader extends AbstractShaderRenderer @Override protected void onRender() { - GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, this.ssaoFramebuffer); + GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, this.FrameBuffer); GL32.glDisable(GL32.GL_SCISSOR_TEST); GL32.glDisable(GL32.GL_DEPTH_TEST); GL32.glDisable(GL32.GL_BLEND);