From 1758f3be1a543f253159bda6cf8ec7f7c15eee87 Mon Sep 17 00:00:00 2001 From: Ruling-0 <47913168+Ruling-0@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:44:35 -0600 Subject: [PATCH] Translucency Glow Depth (#2333) * Replay translucent-pass block entities into a glow depth so glows sort against translucent terrain * Let block entities opt out of the glow depth replay * Keep glow fragments that add no color out of the echoed glow depth --- .../angelica/glsm/CoverageDiscard.java | 41 +++++ .../angelica/glsm/DepthEcho.java | 140 +++++++++++++++ .../angelica/glsm/GLSMDraws.java | 12 ++ .../angelica/glsm/GLSMFramebuffer.java | 2 + .../angelica/glsm/GLSMQueries.java | 4 +- .../glsm/backend/Lwjgl2GLRenderBackend.java | 24 +-- .../angelica/glsm/backend/RenderBackend.java | 170 ++++++++++++++++-- .../angelica/glsm/ffp/FragmentKey.java | 58 +++--- .../glsm/ffp/FragmentShaderGenerator.java | 4 + .../angelica/glsm/ffp/Program.java | 9 + .../angelica/glsm/ffp/ShaderManager.java | 2 + .../angelica/shaders/combined_glint.frag.glsl | 3 + .../lwjgl3/Lwjgl3GLRenderBackend.java | 24 +-- .../angelica/sdlgpu/SDLGPURenderBackend.java | 18 +- .../angelica/api/tesr/TesrGlowDepth.java | 15 ++ .../angelica/api/tesr/package-info.java | 2 +- .../angelica/config/AngelicaConfig.java | 5 + .../gtnewhorizons/angelica/mixins/Mixins.java | 9 + .../celeritas/CeleritasWorldRenderer.java | 47 +++++ .../rendering/tesr/GlowReplayExclusions.java | 42 +++++ .../rendering/tesr/PendingTesrDraws.java | 8 +- .../rendering/tesr/TesrBatchRenderer.java | 9 + .../translucency/TranslucencyLayers.java | 140 ++++++++++++--- .../shaders/translucency_glow_depth.frag | 9 + .../MixinEffectRenderer_GlowDepthReplay.java | 18 ++ .../MixinSoundHandler_GlowDepthReplay.java | 23 +++ .../TranslucencyCompositeGLTest.java | 130 ++++++++++++++ 27 files changed, 867 insertions(+), 101 deletions(-) create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CoverageDiscard.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/DepthEcho.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrGlowDepth.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/rendering/tesr/GlowReplayExclusions.java create mode 100644 src/main/resources/assets/angelica/shaders/translucency_glow_depth.frag create mode 100644 src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinEffectRenderer_GlowDepthReplay.java create mode 100644 src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinSoundHandler_GlowDepthReplay.java diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CoverageDiscard.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CoverageDiscard.java new file mode 100644 index 000000000..428119b96 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CoverageDiscard.java @@ -0,0 +1,41 @@ +package com.gtnewhorizons.angelica.glsm; + +import com.gtnewhorizons.angelica.glsm.stacks.BlendStateStack; +import org.lwjgl.opengl.GL11; + +/// Discards fragments that would not change the target under the draw's blend, so the depth [DepthEcho] keeps for a +/// glow covers only what the glow shows. A fragment it drops changes the draw's own color by less than one 8-bit step. +public final class CoverageDiscard { + + private static final int OFF = 0; + private static final int ALPHA = 1; + private static final int RGB = 2; + private static final int RGB_ALPHA = 3; + + public static final String GLSL = "#define COVER_OFF " + OFF + "\n" + + "#define COVER_RGB " + RGB + "\n" + + "#define COVER_RGB_ALPHA " + RGB_ALPHA + "\n" + + "#define COVER_MIN 0.004\n" + + """ + uniform int u_CoverMode; + void coverageTest(vec4 c) { + if (u_CoverMode == COVER_OFF) return; + float rgb = max(c.r, max(c.g, c.b)); + float coverage = u_CoverMode == COVER_RGB ? rgb : u_CoverMode == COVER_RGB_ALPHA ? rgb * c.a : c.a; + if (coverage < COVER_MIN) discard; + } + """; + + private CoverageDiscard() {} + + public static boolean active() { + return DepthEcho.discardsCoverage(); + } + + public static int mode() { + if (!GLSMBlend.getBlendMode().isEnabled()) return OFF; + final BlendStateStack blend = GLStateManager.ctx().blendState; + if (blend.getDstRgb() != GL11.GL_ONE) return ALPHA; + return blend.getSrcRgb() == GL11.GL_SRC_ALPHA ? RGB_ALPHA : RGB; + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/DepthEcho.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/DepthEcho.java new file mode 100644 index 000000000..fd30d77bb --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/DepthEcho.java @@ -0,0 +1,140 @@ +package com.gtnewhorizons.angelica.glsm; + +import com.gtnewhorizons.angelica.glsm.hooks.BatchStateGuard; +import com.gtnewhorizons.angelica.glsm.stacks.ColorMaskStack; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL30; + +import static com.gtnewhorizons.angelica.glsm.backend.BackendManager.RENDER_BACKEND; + +/// A draw between [#beginTracking] and [#endTracking] is left to the caller's replay instead of repeated. A replay +/// ignores the depth mask, which a previous renderer may leak differently into the pass and the replay, and repeats +/// set GL state through the backend, never the GLSM cache. +public final class DepthEcho { + + private static int framebuffer; + private static int source; + private static boolean echoed; + private static boolean tracking; + private static boolean hit; + private static int immediate; + private static boolean replaying; + private static boolean replayDepthWrite; + + private DepthEcho() {} + + public static void arm(int framebuffer, int source) { + DepthEcho.framebuffer = framebuffer; + DepthEcho.source = source; + } + + public static void disarm() { + framebuffer = 0; + tracking = false; + hit = false; + endReplay(); + } + + public static boolean armed() { + return framebuffer != 0; + } + + public static void beginTracking() { + tracking = framebuffer != 0; + hit = false; + } + + public static boolean endTracking() { + tracking = false; + final boolean was = hit; + hit = false; + return was; + } + + public static void beginImmediate() { + immediate++; + } + + public static void endImmediate() { + immediate--; + } + + public static void beginReplay() { + replaying = true; + } + + public static void endReplay() { + replaying = false; + restoreReplayDepthMask(); + } + + public static boolean replaying() { + return replaying; + } + + public static boolean takeEchoed() { + final boolean was = echoed; + echoed = false; + return was; + } + + public static boolean skipsCall() { + return replaySkips() && !DisplayListManager.isRecording(); + } + + public static boolean skipsDraw() { + if (!replaying) return false; + if (replaySkips()) return true; + if (!GLSMDepthStencilAlpha.getDepthState().isEnabled()) { + RENDER_BACKEND.depthMask(true); + replayDepthWrite = true; + } + return false; + } + + private static boolean replaySkips() { + return replaying && !(blendsVisibly() && GLSMFramebuffer.getDrawFramebuffer() == framebuffer); + } + + public static boolean discardsCoverage() { + return replaying || echoes(); + } + + public static boolean begin() { + if (replayDepthWrite) { + restoreReplayDepthMask(); + return false; + } + if (!echoes()) return false; + echoed = true; + if (tracking && immediate == 0 && !BatchStateGuard.isSuspended()) { + hit = true; + return false; + } + RENDER_BACKEND.bindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, framebuffer); + RENDER_BACKEND.depthMask(true); + return true; + } + + public static void end() { + RENDER_BACKEND.depthMask(false); + RENDER_BACKEND.bindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, source); + } + + private static void restoreReplayDepthMask() { + if (!replayDepthWrite) return; + replayDepthWrite = false; + RENDER_BACKEND.depthMask(GLSMDepthStencilAlpha.getDepthState().isEnabled()); + } + + private static boolean echoes() { + return framebuffer != 0 && !GLSMDepthStencilAlpha.getDepthState().isEnabled() && blendsVisibly() && GLSMFramebuffer.getDrawFramebuffer() == source; + } + + private static boolean blendsVisibly() { + if (!GLSMDepthStencilAlpha.getDepthTest().isEnabled() || !GLSMBlend.getBlendMode().isEnabled()) return false; + if (GLSMDepthStencilAlpha.getDepthState().getFunc() == GL11.GL_EQUAL) return false; + final ColorMaskStack mask = GLSMBlend.getColorMask(); + return mask.red || mask.green || mask.blue || mask.alpha; + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMDraws.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMDraws.java index d222a88a7..5dd7699f6 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMDraws.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMDraws.java @@ -141,6 +141,7 @@ public final class GLSMDraws { } public static void glDrawElements(int mode, ByteBuffer indices) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; CommandRecorder savedRecorder = null; @@ -174,6 +175,7 @@ public final class GLSMDraws { } public static void glDrawElements(int mode, IntBuffer indices) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; CommandRecorder savedRecorder = null; @@ -207,6 +209,7 @@ public final class GLSMDraws { } public static void glDrawElements(int mode, ShortBuffer indices) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; CommandRecorder savedRecorder = null; @@ -240,6 +243,7 @@ public final class GLSMDraws { } public static void glDrawElements(int mode, int count, int type, ByteBuffer indices) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; CommandRecorder savedRecorder = null; @@ -273,6 +277,7 @@ public final class GLSMDraws { } public static void glDrawElements(int mode, int indices_count, int type, long indices_buffer_offset) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); final GLContextState glCtx = ctx(); drawCalls++; @@ -329,12 +334,14 @@ public final class GLSMDraws { } public static void glDrawElementsBaseVertex(int mode, int count, int type, long indices, int basevertex) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; RENDER_BACKEND.drawElementsBaseVertex(mode, count, type, indices, basevertex); } public static void glDrawElementsInstanced(int mode, int count, int type, long indices, int primcount) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; final GLContextState glCtx = ctx(); @@ -353,6 +360,7 @@ public final class GLSMDraws { } public static void glDrawArraysInstanced(int mode, int first, int count, int primcount) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; final GLContextState glCtx = ctx(); @@ -377,6 +385,7 @@ public final class GLSMDraws { } public static void glDrawRangeElements(int mode, int start, int end, int count, int type, long indices) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); final GLContextState glCtx = ctx(); final boolean locked = acquireDrawLock(); @@ -393,11 +402,13 @@ public final class GLSMDraws { } public static void glDrawRangeElementsBaseVertex(int mode, int start, int end, int count, int type, long indices, int baseVertex) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); RENDER_BACKEND.drawRangeElementsBaseVertex(mode, start, end, count, type, indices, baseVertex); } public static void glMultiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); final GLContextState glCtx = ctx(); final boolean locked = acquireDrawLock(); @@ -423,6 +434,7 @@ public final class GLSMDraws { } public static void glDrawArrays(int mode, int first, int count) { + if (DepthEcho.skipsCall()) return; rejectExitCapture(); drawCalls++; CommandRecorder savedRecorder = null; diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMFramebuffer.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMFramebuffer.java index c5e7b9579..204b27158 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMFramebuffer.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMFramebuffer.java @@ -64,6 +64,7 @@ public final class GLSMFramebuffer { private static long firstClearBlockNanos; public static void glClear(int mask) { + if (DepthEcho.replaying() && !DisplayListManager.isRecording()) return; rejectExitCapture(); final RecordMode mode = DisplayListManager.getRecordMode(); if (mode != RecordMode.NONE) { @@ -315,6 +316,7 @@ public final class GLSMFramebuffer { } public static void glBlitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) { + if (DepthEcho.replaying() && !DisplayListManager.isRecording()) return; rejectExitCapture(); RENDER_BACKEND.blitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMQueries.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMQueries.java index a48b6e789..69222c1f2 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMQueries.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLSMQueries.java @@ -503,8 +503,8 @@ public final class GLSMQueries { return RENDER_BACKEND.genQueries(); } public static void glDeleteQueries(int id) { rejectExitCapture(); RENDER_BACKEND.deleteQueries(id); } - public static void glBeginQuery(int target, int id) { rejectExitCapture(); RENDER_BACKEND.beginQuery(target, id); } - public static void glEndQuery(int target) { rejectExitCapture(); RENDER_BACKEND.endQuery(target); } + public static void glBeginQuery(int target, int id) { rejectExitCapture(); if (!DepthEcho.replaying()) RENDER_BACKEND.beginQuery(target, id); } + public static void glEndQuery(int target) { rejectExitCapture(); if (!DepthEcho.replaying()) RENDER_BACKEND.endQuery(target); } public static void glGetQueryObjectui(int id, int pname, IntBuffer params) { rejectExitCapture(); RENDER_BACKEND.getQueryObjectui(id, pname, params); } public static int glGetQueryObjecti(int id, int pname) { rejectExitCapture(); return RENDER_BACKEND.getQueryObjecti(id, pname); } public static void glQueryCounter(int id, int target) { rejectExitCapture(); RENDER_BACKEND.queryCounter(id, target); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java index 7ed88b33c..559e6ff91 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java @@ -323,42 +323,42 @@ public final class Lwjgl2GLRenderBackend extends RenderBackend { } @Override - public void drawArrays(int mode, int first, int count) { + protected void issueDrawArrays(int mode, int first, int count) { GL11.glDrawArrays(mode, first, count); } @Override - public void drawElements(int mode, int count, int type, long indices) { + protected void issueDrawElements(int mode, int count, int type, long indices) { GL11.glDrawElements(mode, count, type, indices); } @Override - public void drawElements(int mode, ByteBuffer indices) { + protected void issueDrawElements(int mode, ByteBuffer indices) { GL11.glDrawElements(mode, indices); } @Override - public void drawElements(int mode, IntBuffer indices) { + protected void issueDrawElements(int mode, IntBuffer indices) { GL11.glDrawElements(mode, indices); } @Override - public void drawElements(int mode, ShortBuffer indices) { + protected void issueDrawElements(int mode, ShortBuffer indices) { GL11.glDrawElements(mode, indices); } @Override - public void drawElements(int mode, int count, int type, ByteBuffer indices) { + protected void issueDrawElements(int mode, int count, int type, ByteBuffer indices) { GL11.glDrawElements(mode, count, type, indices); } @Override - public void drawElementsInstanced(int mode, int count, int type, long indices, int primcount) { + protected void issueDrawElementsInstanced(int mode, int count, int type, long indices, int primcount) { GL31.glDrawElementsInstanced(mode, count, type, indices, primcount); } @Override - public void drawArraysInstanced(int mode, int first, int count, int primcount) { + protected void issueDrawArraysInstanced(int mode, int first, int count, int primcount) { GL31.glDrawArraysInstanced(mode, first, count, primcount); } @@ -368,7 +368,7 @@ public final class Lwjgl2GLRenderBackend extends RenderBackend { } @Override - public void multiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { + protected void issueMultiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { GL43.glMultiDrawElementsIndirect(mode, type, indirect, drawcount, stride); } @@ -378,12 +378,12 @@ public final class Lwjgl2GLRenderBackend extends RenderBackend { } @Override - public void drawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { + protected void issueDrawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { GL32.glDrawElementsBaseVertex(mode, count, type, indices, baseVertex); } @Override - public void multiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { + protected void issueMultiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { for (int i = 0; i < drawcount; i++) { GL32.glDrawElementsBaseVertex(mode, memGetInt(pCount + 4L * i), type, memGetAddress(pIndices + (long) i * POINTER_SIZE), memGetInt(pBaseVertex + 4L * i)); } @@ -1715,7 +1715,7 @@ public final class Lwjgl2GLRenderBackend extends RenderBackend { } @Override - public void multiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { + protected void issueMultiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { GL14.glMultiDrawArrays(mode, firsts, counts); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java index cc488f055..1816d7a18 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java @@ -1,6 +1,7 @@ package com.gtnewhorizons.angelica.glsm.backend; import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; +import com.gtnewhorizons.angelica.glsm.DepthEcho; import com.gtnewhorizons.angelica.glsm.GLESFormatRemap; import com.gtnewhorizons.angelica.glsm.GLSMAttribStack; import com.gtnewhorizons.angelica.glsm.GLStateManager; @@ -321,28 +322,173 @@ public abstract class RenderBackend { public abstract void logicOp(int opcode); public abstract void hint(int target, int mode); - public abstract void drawArrays(int mode, int first, int count); - public abstract void drawElements(int mode, int count, int type, long indices); - public void drawElements(int mode, ByteBuffer indices) {} - public void drawElements(int mode, IntBuffer indices) {} - public void drawElements(int mode, ShortBuffer indices) {} - public void drawElements(int mode, int count, int type, ByteBuffer indices) {} - public abstract void multiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride); public void multiDrawArraysIndirectCount(int mode, long indirect, long drawcount, int maxdrawcount, int stride) { throw new UnsupportedOperationException(getName() + ": indirect draw-count buffers unsupported"); } public void multiDrawElementsIndirectCount(int mode, int type, long indirect, long drawcount, int maxdrawcount, int stride) { throw new UnsupportedOperationException(getName() + ": indirect draw-count buffers unsupported"); } public abstract void copyBufferSubData(int readTarget, int writeTarget, long readOffset, long writeOffset, long size); - public abstract void drawElementsInstanced(int mode, int count, int type, long indices, int primcount); - public abstract void drawArraysInstanced(int mode, int first, int count, int primcount); public void provokingVertex(int provokeMode) {} - public abstract void drawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex); - public abstract void multiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex); + + public final void drawArrays(int mode, int first, int count) { + if (DepthEcho.skipsDraw()) return; + issueDrawArrays(mode, first, count); + if (DepthEcho.begin()) { + try { + issueDrawArrays(mode, first, count); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElements(int mode, int count, int type, long indices) { + if (DepthEcho.skipsDraw()) return; + issueDrawElements(mode, count, type, indices); + if (DepthEcho.begin()) { + try { + issueDrawElements(mode, count, type, indices); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElements(int mode, ByteBuffer indices) { + if (DepthEcho.skipsDraw()) return; + issueDrawElements(mode, indices); + if (DepthEcho.begin()) { + try { + issueDrawElements(mode, indices); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElements(int mode, IntBuffer indices) { + if (DepthEcho.skipsDraw()) return; + issueDrawElements(mode, indices); + if (DepthEcho.begin()) { + try { + issueDrawElements(mode, indices); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElements(int mode, ShortBuffer indices) { + if (DepthEcho.skipsDraw()) return; + issueDrawElements(mode, indices); + if (DepthEcho.begin()) { + try { + issueDrawElements(mode, indices); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElements(int mode, int count, int type, ByteBuffer indices) { + if (DepthEcho.skipsDraw()) return; + issueDrawElements(mode, count, type, indices); + if (DepthEcho.begin()) { + try { + issueDrawElements(mode, count, type, indices); + } finally { + DepthEcho.end(); + } + } + } + + public final void multiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { + if (DepthEcho.skipsDraw()) return; + issueMultiDrawElementsIndirect(mode, type, indirect, drawcount, stride); + if (DepthEcho.begin()) { + try { + issueMultiDrawElementsIndirect(mode, type, indirect, drawcount, stride); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElementsInstanced(int mode, int count, int type, long indices, int primcount) { + if (DepthEcho.skipsDraw()) return; + issueDrawElementsInstanced(mode, count, type, indices, primcount); + if (DepthEcho.begin()) { + try { + issueDrawElementsInstanced(mode, count, type, indices, primcount); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawArraysInstanced(int mode, int first, int count, int primcount) { + if (DepthEcho.skipsDraw()) return; + issueDrawArraysInstanced(mode, first, count, primcount); + if (DepthEcho.begin()) { + try { + issueDrawArraysInstanced(mode, first, count, primcount); + } finally { + DepthEcho.end(); + } + } + } + + public final void drawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { + if (DepthEcho.skipsDraw()) return; + issueDrawElementsBaseVertex(mode, count, type, indices, baseVertex); + if (DepthEcho.begin()) { + try { + issueDrawElementsBaseVertex(mode, count, type, indices, baseVertex); + } finally { + DepthEcho.end(); + } + } + } + + public final void multiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { + if (DepthEcho.skipsDraw()) return; + issueMultiDrawElementsBaseVertex(mode, pCount, type, pIndices, drawcount, pBaseVertex); + if (DepthEcho.begin()) { + try { + issueMultiDrawElementsBaseVertex(mode, pCount, type, pIndices, drawcount, pBaseVertex); + } finally { + DepthEcho.end(); + } + } + } + + public final void multiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { + if (DepthEcho.skipsDraw()) return; + issueMultiDrawArrays(mode, firsts, counts); + if (DepthEcho.begin()) { + try { + issueMultiDrawArrays(mode, firsts, counts); + } finally { + DepthEcho.end(); + } + } + } + + protected abstract void issueDrawArrays(int mode, int first, int count); + protected abstract void issueDrawElements(int mode, int count, int type, long indices); + protected void issueDrawElements(int mode, ByteBuffer indices) {} + protected void issueDrawElements(int mode, IntBuffer indices) {} + protected void issueDrawElements(int mode, ShortBuffer indices) {} + protected void issueDrawElements(int mode, int count, int type, ByteBuffer indices) {} + protected abstract void issueMultiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride); + protected abstract void issueDrawElementsInstanced(int mode, int count, int type, long indices, int primcount); + protected abstract void issueDrawArraysInstanced(int mode, int first, int count, int primcount); + protected abstract void issueDrawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex); + protected abstract void issueMultiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex); + protected abstract void issueMultiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts); public void drawRangeElements(int mode, int start, int end, int count, int type, long indices) { drawElements(mode, count, type, indices); } public void drawRangeElementsBaseVertex(int mode, int start, int end, int count, int type, long indices, int baseVertex) { drawElementsBaseVertex(mode, count, type, indices, baseVertex); } - public abstract void multiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts); public abstract void primitiveRestartIndex(int index); public abstract void pointParameterf(int pname, float param); public abstract void pointParameteri(int pname, int param); diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentKey.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentKey.java index 82bf93bab..238e154e0 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentKey.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentKey.java @@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.glsm.ffp; import java.util.Arrays; +import com.gtnewhorizons.angelica.glsm.CoverageDiscard; import com.gtnewhorizons.angelica.glsm.GLContextState; import com.gtnewhorizons.angelica.glsm.GLSMRasterizer; import com.gtnewhorizons.angelica.glsm.GLStateManager; @@ -16,24 +17,24 @@ import org.lwjgl.opengl.GL13; * Packed-long fragment shader permutation key for FFP emulation. * * Layout: - * long[0]: global bits (14) + unit 0 (47 bits at offset 14) - * long[1..3]: unit 1..3 (47 bits each, low-aligned) + * long[0]: global bits (14) + unit 0 (45 bits at offset 14), coverage discard (60), combined glint (61), glint alpha (62), clip discard (63) + * long[1..3]: unit 1..3 (45 bits each, low-aligned) * Only max(1, nrEnabledUnits) longs are significant. * - * Per-unit packing (47 bits): + * Per-unit packing (45 bits): * 0: enabled * 1-3: mode (MODULATE=0..COMBINE=5) * 4: needsEnvColor - * 5-8: combineRgb (encoded 0-7) - * 9-12: combineAlpha (encoded 0-7) - * 13-14: scaleShiftRgb - * 15-16: scaleShiftAlpha - * 17-21: arg0 RGB (source:3 + operand:2) - * 22-26: arg1 RGB - * 27-31: arg2 RGB - * 32-36: arg0 Alpha - * 37-41: arg1 Alpha - * 42-46: arg2 Alpha + * 5-7: combineRgb (encoded 0-7) + * 8-10: combineAlpha (encoded 0-7) + * 11-12: scaleShiftRgb + * 13-14: scaleShiftAlpha + * 15-19: arg0 RGB (source:3 + operand:2) + * 20-24: arg1 RGB + * 25-29: arg2 RGB + * 30-34: arg0 Alpha + * 35-39: arg1 Alpha + * 40-44: arg2 Alpha */ public final class FragmentKey { public static final int TEX_ENV_MODULATE = 0; @@ -78,6 +79,7 @@ public final class FragmentKey { } private static final int GLOBAL_BITS = 14; + private static final int BIT_COVERAGE_DISCARD = 60; private static final int BIT_COMBINED_GLINT = 61; private static final int BIT_GLINT_REPLACE_ALPHA = 62; private static final int BIT_CLIP_DISCARD = 63; @@ -94,16 +96,16 @@ public final class FragmentKey { private static final int U_ENABLED = 0; private static final int U_MODE = 1; // 3 bits private static final int U_NEEDS_ENV_COLOR = 4; // 1 bit - private static final int U_COMBINE_RGB = 5; // 4 bits - private static final int U_COMBINE_ALPHA = 9; // 4 bits - private static final int U_SCALE_RGB = 13; // 2 bits - private static final int U_SCALE_ALPHA = 15; // 2 bits - private static final int U_ARG0_RGB = 17; // 5 bits (source:3 + operand:2) - private static final int U_ARG1_RGB = 22; // 5 bits - private static final int U_ARG2_RGB = 27; // 5 bits - private static final int U_ARG0_ALPHA = 32; // 5 bits - private static final int U_ARG1_ALPHA = 37; // 5 bits - private static final int U_ARG2_ALPHA = 42; // 5 bits + private static final int U_COMBINE_RGB = 5; // 3 bits + private static final int U_COMBINE_ALPHA = 8; // 3 bits + private static final int U_SCALE_RGB = 11; // 2 bits + private static final int U_SCALE_ALPHA = 13; // 2 bits + private static final int U_ARG0_RGB = 15; // 5 bits (source:3 + operand:2) + private static final int U_ARG1_RGB = 20; // 5 bits + private static final int U_ARG2_RGB = 25; // 5 bits + private static final int U_ARG0_ALPHA = 30; // 5 bits + private static final int U_ARG1_ALPHA = 35; // 5 bits + private static final int U_ARG2_ALPHA = 40; // 5 bits static final int MAX_UNITS = 4; @@ -127,6 +129,7 @@ public final class FragmentKey { public static int packFromState(long[] out, GLContextState glCtx) { long global = 0; if (CombinedGlint.isActive()) global |= 1L << BIT_COMBINED_GLINT; + if (CoverageDiscard.active()) global |= 1L << BIT_COVERAGE_DISCARD; if (CombinedGlint.replacesAlpha()) global |= 1L << BIT_GLINT_REPLACE_ALPHA; if (GLSMRasterizer.clipEmulationActive(glCtx)) global |= 1L << BIT_CLIP_DISCARD; @@ -200,8 +203,8 @@ public final class FragmentKey { if (mode == TEX_ENV_COMBINE) { final int combRgb = encodeCombineFunc(envState.combineRgb); final int combAlpha = encodeCombineFunc(envState.combineAlpha); - bits |= ((long) (combRgb & 0xF)) << U_COMBINE_RGB; - bits |= ((long) (combAlpha & 0xF)) << U_COMBINE_ALPHA; + bits |= ((long) (combRgb & 0x7)) << U_COMBINE_RGB; + bits |= ((long) (combAlpha & 0x7)) << U_COMBINE_ALPHA; bits |= ((long) (encodeScale(envState.scaleRgb) & 0x3)) << U_SCALE_RGB; bits |= ((long) (encodeScale(envState.scaleAlpha) & 0x3)) << U_SCALE_ALPHA; @@ -264,6 +267,7 @@ public final class FragmentKey { public boolean combinedGlint() { return (packed[0] & (1L << BIT_COMBINED_GLINT)) != 0; } public boolean clipDiscard() { return (packed[0] & (1L << BIT_CLIP_DISCARD)) != 0; } public boolean glintReplaceAlpha() { return (packed[0] & (1L << BIT_GLINT_REPLACE_ALPHA)) != 0; } + public boolean coverageDiscard() { return (packed[0] & (1L << BIT_COVERAGE_DISCARD)) != 0; } public int textureSupersamplingSamples() { return 1 << (int) ((packed[0] >> BIT_TEXTURE_SUPERSAMPLING) & 0x7); } public boolean alphaTestEnabled() { return ((packed[0] >> BIT_ALPHA_TEST) & 1) != 0; } public int alphaTestFunc() { return (int) ((packed[0] >> BIT_ALPHA_FUNC) & 0x7); } @@ -281,8 +285,8 @@ public final class FragmentKey { public boolean unitEnabled(int i) { return (unitBits(i) & 1) != 0; } public int unitMode(int i) { return (int) ((unitBits(i) >> U_MODE) & 0x7); } public boolean unitNeedsEnvColor(int i) { return ((unitBits(i) >> U_NEEDS_ENV_COLOR) & 1) != 0; } - public int unitCombineRgb(int i) { return (int) ((unitBits(i) >> U_COMBINE_RGB) & 0xF); } - public int unitCombineAlpha(int i) { return (int) ((unitBits(i) >> U_COMBINE_ALPHA) & 0xF); } + public int unitCombineRgb(int i) { return (int) ((unitBits(i) >> U_COMBINE_RGB) & 0x7); } + public int unitCombineAlpha(int i) { return (int) ((unitBits(i) >> U_COMBINE_ALPHA) & 0x7); } public int unitScaleShiftRgb(int i) { return (int) ((unitBits(i) >> U_SCALE_RGB) & 0x3); } public int unitScaleShiftAlpha(int i) { return (int) ((unitBits(i) >> U_SCALE_ALPHA) & 0x3); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentShaderGenerator.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentShaderGenerator.java index 7540aafdb..77a22556e 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentShaderGenerator.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/FragmentShaderGenerator.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.glsm.ffp; +import com.gtnewhorizons.angelica.glsm.CoverageDiscard; import org.lwjgl.opengl.GL11; import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; @@ -24,6 +25,7 @@ public final class FragmentShaderGenerator { sb.append("#define GLINT_ALPHA_FUNC ").append(key.alphaTestEnabled() ? key.alphaTestFunc() : 7).append('\n'); sb.append("#define GLINT_OVERLAY ").append(key.overlayInstanced() ? 2 : key.overlayEnabled() ? 1 : 0).append('\n'); sb.append("#define GLINT_LIGHTMAP ").append(key.nrEnabledUnits() > 1 && key.unitEnabled(1) ? 1 : 0).append('\n'); + if (key.coverageDiscard()) sb.append("#define COVERAGE_DISCARD 1\n"); sb.append(ShaderLoader.getShaderSource("angelica:combined_glint.frag.glsl")); return sb.toString(); } @@ -43,6 +45,7 @@ public final class FragmentShaderGenerator { emitSpecularAdd(sb, key); emitAlphaTest(sb, key); emitFog(sb, key); + if (key.coverageDiscard()) sb.append(" coverageTest(color);\n"); sb.append(" fragColor = color;\n"); sb.append("}\n"); @@ -89,6 +92,7 @@ public final class FragmentShaderGenerator { sb.append("uniform sampler2D u_Sampler").append(i).append(";\n"); } } + if (key.coverageDiscard()) sb.append(CoverageDiscard.GLSL); sb.append(FFPUniformBlock.GLSL_DECL); sb.append('\n'); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/Program.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/Program.java index c6433c045..671a6417c 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/Program.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/Program.java @@ -35,6 +35,8 @@ public class Program { @Getter private final FragmentKey fragmentKey; public final int[] locSampler = { -1, -1, -1, -1 }; + private final int locCoverMode; + private int coverMode = -1; Program(int programId, VertexKey vertexKey, FragmentKey fragmentKey) { this.programId = programId; @@ -43,6 +45,13 @@ public class Program { for (int i = 0; i < 4; i++) { locSampler[i] = RENDER_BACKEND.getUniformLocation(programId, "u_Sampler" + i); } + locCoverMode = RENDER_BACKEND.getUniformLocation(programId, "u_CoverMode"); + } + + void syncCoverMode(int mode) { + if (locCoverMode == -1 || coverMode == mode) return; + RENDER_BACKEND.uniform1i(locCoverMode, mode); + coverMode = mode; } public void destroy() { diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java index 81a306f0c..9202b3d49 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/ShaderManager.java @@ -4,6 +4,7 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; import com.gtnewhorizons.angelica.config.SystemProperties; import com.gtnewhorizons.angelica.glsm.CompatUniformManager; +import com.gtnewhorizons.angelica.glsm.CoverageDiscard; import com.gtnewhorizons.angelica.glsm.GLContextState; import com.gtnewhorizons.angelica.glsm.GLSMCurrentAttribs; import com.gtnewhorizons.angelica.glsm.GLStateManager; @@ -188,6 +189,7 @@ public final class ShaderManager { private void uploadUniforms(GLContextState glCtx) { if (currentProgram != null) { uniforms.upload(glCtx); + if (currentProgram.getFragmentKey().coverageDiscard()) currentProgram.syncCoverMode(CoverageDiscard.mode()); } } diff --git a/glsm/src/main/resources/assets/angelica/shaders/combined_glint.frag.glsl b/glsm/src/main/resources/assets/angelica/shaders/combined_glint.frag.glsl index 2902c73b5..0554bc7a4 100644 --- a/glsm/src/main/resources/assets/angelica/shaders/combined_glint.frag.glsl +++ b/glsm/src/main/resources/assets/angelica/shaders/combined_glint.frag.glsl @@ -53,4 +53,7 @@ void main() { #if GLINT_REPLACE_ALPHA fragColor.a = visible1 ? b.a : a.a; #endif +#ifdef COVERAGE_DISCARD + coverageTest(fragColor); +#endif } diff --git a/lwjgl3-backend/src/main/java/com/gtnewhorizons/angelica/lwjgl3/Lwjgl3GLRenderBackend.java b/lwjgl3-backend/src/main/java/com/gtnewhorizons/angelica/lwjgl3/Lwjgl3GLRenderBackend.java index b1999ebc6..64c6a77fa 100644 --- a/lwjgl3-backend/src/main/java/com/gtnewhorizons/angelica/lwjgl3/Lwjgl3GLRenderBackend.java +++ b/lwjgl3-backend/src/main/java/com/gtnewhorizons/angelica/lwjgl3/Lwjgl3GLRenderBackend.java @@ -493,42 +493,42 @@ public final class Lwjgl3GLRenderBackend extends RenderBackend { } @Override - public void drawArrays(int mode, int first, int count) { + protected void issueDrawArrays(int mode, int first, int count) { GL11C.glDrawArrays(mode, first, count); } @Override - public void drawElements(int mode, int count, int type, long indices) { + protected void issueDrawElements(int mode, int count, int type, long indices) { GL11C.glDrawElements(mode, count, type, indices); } @Override - public void drawElements(int mode, ByteBuffer indices) { + protected void issueDrawElements(int mode, ByteBuffer indices) { GL11C.glDrawElements(mode, indices); } @Override - public void drawElements(int mode, IntBuffer indices) { + protected void issueDrawElements(int mode, IntBuffer indices) { GL11C.glDrawElements(mode, indices); } @Override - public void drawElements(int mode, ShortBuffer indices) { + protected void issueDrawElements(int mode, ShortBuffer indices) { GL11C.glDrawElements(mode, indices); } @Override - public void drawElements(int mode, int count, int type, ByteBuffer indices) { + protected void issueDrawElements(int mode, int count, int type, ByteBuffer indices) { GL11C.glDrawElements(mode, count, type, MemoryUtil.memAddress(indices)); } @Override - public void drawElementsInstanced(int mode, int count, int type, long indices, int primcount) { + protected void issueDrawElementsInstanced(int mode, int count, int type, long indices, int primcount) { GL31C.glDrawElementsInstanced(mode, count, type, indices, primcount); } @Override - public void drawArraysInstanced(int mode, int first, int count, int primcount) { + protected void issueDrawArraysInstanced(int mode, int first, int count, int primcount) { GL31C.glDrawArraysInstanced(mode, first, count, primcount); } @@ -539,7 +539,7 @@ public final class Lwjgl3GLRenderBackend extends RenderBackend { } @Override - public void multiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { + protected void issueMultiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { GL43C.glMultiDrawElementsIndirect(mode, type, indirect, drawcount, stride); } @@ -549,12 +549,12 @@ public final class Lwjgl3GLRenderBackend extends RenderBackend { } @Override - public void drawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { + protected void issueDrawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { GL32C.glDrawElementsBaseVertex(mode, count, type, indices, baseVertex); } @Override - public void multiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { + protected void issueMultiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { GL32C.nglMultiDrawElementsBaseVertex(mode, pCount, type, pIndices, drawcount, pBaseVertex); } @@ -1884,7 +1884,7 @@ public final class Lwjgl3GLRenderBackend extends RenderBackend { } @Override - public void multiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { + protected void issueMultiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { GL14C.glMultiDrawArrays(mode, firsts, counts); } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java index ceefa04d4..56c0eb62e 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java @@ -1228,7 +1228,7 @@ public class SDLGPURenderBackend extends RenderBackend { } } - @Override public void drawArrays(int mode, int first, int count) { + @Override protected void issueDrawArrays(int mode, int first, int count) { if (count <= 0) return; final ContextState st = s(); if (st.rasterizerDiscard) return; @@ -1250,7 +1250,7 @@ public class SDLGPURenderBackend extends RenderBackend { captureVertexWrites(st, first, count, 1, 0, 0, 0); } - @Override public void drawElements(int mode, int count, int type, long indices) { + @Override protected void issueDrawElements(int mode, int count, int type, long indices) { final ContextState st = s(); final long rp = prepareIndexedDrawBind(st, mode, type, "drawElements"); if (rp == 0) return; @@ -1260,7 +1260,7 @@ public class SDLGPURenderBackend extends RenderBackend { captureVertexWrites(st, 0, count, 1, type, indices, 0); } - @Override public void drawElementsInstanced(int mode, int count, int type, long indices, int primcount) { + @Override protected void issueDrawElementsInstanced(int mode, int count, int type, long indices, int primcount) { final ContextState st = s(); final long rp = prepareIndexedDrawBind(st, mode, type, "drawElementsInstanced"); if (rp == 0) return; @@ -1272,7 +1272,7 @@ public class SDLGPURenderBackend extends RenderBackend { } - @Override public void drawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { + @Override protected void issueDrawElementsBaseVertex(int mode, int count, int type, long indices, int baseVertex) { final ContextState st = s(); final long rp = prepareIndexedDrawBind(st, mode, type, "drawElementsBaseVertex"); if (rp == 0) return; @@ -1297,7 +1297,7 @@ public class SDLGPURenderBackend extends RenderBackend { return rp; } - @Override public void multiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { + @Override protected void issueMultiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { if (drawcount <= 0) return; final ContextState st = s(); final long rp = prepareIndexedDrawBind(st, mode, type, "multiDrawElementsBaseVertex"); @@ -1317,7 +1317,7 @@ public class SDLGPURenderBackend extends RenderBackend { } } - @Override public void multiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { + @Override protected void issueMultiDrawElementsIndirect(int mode, int type, long indirect, int drawcount, int stride) { final ContextState cs = s(); if (drawcount <= 0) return; Tracy.beginZone(Z_SDL_INDIRECT_DRAW); @@ -4670,7 +4670,7 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override - public void drawArraysInstanced(int mode, int first, int count, int primcount) { + protected void issueDrawArraysInstanced(int mode, int first, int count, int primcount) { if (count <= 0 || primcount <= 0) return; final ContextState st = s(); if (st.rasterizerDiscard) return; @@ -4688,11 +4688,11 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override - public void multiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { + protected void issueMultiDrawArrays(int mode, IntBuffer firsts, IntBuffer counts) { if (firsts == null || counts == null) return; final int n = Math.min(firsts.remaining(), counts.remaining()); for (int i = 0; i < n; i++) { - drawArrays(mode, firsts.get(firsts.position() + i), counts.get(counts.position() + i)); + issueDrawArrays(mode, firsts.get(firsts.position() + i), counts.get(counts.position() + i)); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrGlowDepth.java b/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrGlowDepth.java new file mode 100644 index 000000000..4493e1918 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrGlowDepth.java @@ -0,0 +1,15 @@ +package com.gtnewhorizons.angelica.api.tesr; + +import com.gtnewhorizons.angelica.rendering.tesr.GlowReplayExclusions; + +/// Under Fancy translucency, block entities drawing glows (blended draws without depth writes) in the translucent +/// pass are rendered a second time. Renderers that advance state per call opt out here. +public final class TesrGlowDepth { + + private TesrGlowDepth() {} + + /// Excludes block entities of `type`, or rendered by a renderer of `type`, including subclasses. + public static void excludeFromReplay(Class type) { + GlowReplayExclusions.exclude(type); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/tesr/package-info.java b/src/main/java/com/gtnewhorizons/angelica/api/tesr/package-info.java index 39aeb50d5..1db639178 100644 --- a/src/main/java/com/gtnewhorizons/angelica/api/tesr/package-info.java +++ b/src/main/java/com/gtnewhorizons/angelica/api/tesr/package-info.java @@ -1,5 +1,5 @@ /** TESR mesh cache/batch API (Render thread only) */ -@API(owner = "angelica", provides = "angelica|tesr", apiVersion = "3") +@API(owner = "angelica", provides = "angelica|tesr", apiVersion = "4") package com.gtnewhorizons.angelica.api.tesr; import cpw.mods.fml.common.API; diff --git a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java index d72117b5f..48c541f29 100644 --- a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java +++ b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java @@ -177,6 +177,11 @@ public class AngelicaConfig { @Config.RequiresMcRestart public static String[] entityShadowReplayExcludedClasses; + @Config.Comment("Tile entity or renderer classes whose glows Fancy translucency records as they draw instead of rendering the tile entity a second time. For renderers that advance an animation or other state on each render.") + @Config.DefaultStringList({}) + @Config.RequiresMcRestart + public static String[] glowDepthReplayExcludedClasses; + @Config.Comment("Skip the end-of-frame shader buffer copy by ping-ponging buffers. Disable if a shader pack misrenders.") @Config.DefaultBoolean(true) @Config.RequiresMcRestart diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java index 72c8cd288..8d03c3a5f 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java @@ -301,6 +301,15 @@ public enum Mixins implements IMixins { ) ), + ANGELICA_GLOW_DEPTH_REPLAY(new MixinBuilder("Drop particles and sounds a block entity renderer starts while Fancy translucency replays it into the glow depth") + .setPhase(Phase.EARLY) + .setApplyIf(() -> AngelicaConfig.enableIris) + .addClientMixins( + "angelica.tesr.MixinEffectRenderer_GlowDepthReplay", + "angelica.tesr.MixinSoundHandler_GlowDepthReplay" + ) + ), + ANGELICA_TESR_PROVIDER_DISPATCH(new MixinBuilder("Route TesrMeshProvider renderers through the batched mesh cache, and observe renderers that mix model parts with their own draws") .setPhase(Phase.EARLY) .setApplyIf(() -> AngelicaConfig.enableTESRProviderDispatch || AngelicaConfig.enableEntityBatching) diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java index ebe85cd26..4ed7f3492 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java @@ -44,6 +44,7 @@ import com.gtnewhorizons.angelica.compat.cubicchunks.CubicChunksAPI; import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import com.gtnewhorizons.angelica.dynamiclights.IDynamicLightWorldRenderer; +import com.gtnewhorizons.angelica.glsm.DepthEcho; import com.gtnewhorizons.angelica.glsm.GLSMAttribStack; import com.gtnewhorizons.angelica.glsm.GLSMBlend; import com.gtnewhorizons.angelica.glsm.GLSMCurrentAttribs; @@ -64,6 +65,7 @@ import com.gtnewhorizons.angelica.rendering.culling.GpuCulling; import com.gtnewhorizons.angelica.render.WeatherRenderer; import com.gtnewhorizons.angelica.rendering.culling.GpuTerrainCuller; import com.gtnewhorizons.angelica.rendering.tesr.AngelicaTesrMeshCache; +import com.gtnewhorizons.angelica.rendering.tesr.GlowReplayExclusions; import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher; import com.gtnewhorizons.angelica.rendering.tesr.PendingTesrDraws; import com.gtnewhorizons.angelica.rendering.tesr.SectionBuiltBlockEntity; @@ -72,6 +74,7 @@ import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHold import com.gtnewhorizons.angelica.rendering.tesr.TesrAttribution; import com.gtnewhorizons.angelica.rendering.tesr.TesrBatchRenderer; import com.gtnewhorizons.angelica.rendering.tesr.TesrLifecycle; +import com.gtnewhorizons.angelica.rendering.tesr.TesrProviderDispatch; import com.gtnewhorizons.angelica.rendering.translucency.TranslucencyLayers; import com.gtnewhorizons.angelica.rendering.translucency.TranslucentLayer; import net.coderbot.iris.Iris; @@ -105,6 +108,7 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer frameTEs = new ObjectArrayList<>(); private final ObjectArrayList frameOverride = new ObjectArrayList<>(); + private final ObjectArrayList glowReplay = new ObjectArrayList<>(); private final TileEntityDepthOrder depthOrder = new TileEntityDepthOrder(); private final Matrix4f savedTextureMatrix = new Matrix4f(); @@ -432,6 +436,7 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer> registered = new ReferenceOpenHashSet<>(); + private static final Reference2ObjectOpenHashMap, Boolean> excluded = new Reference2ObjectOpenHashMap<>(); + + private GlowReplayExclusions() {} + + public static void exclude(Class type) { + if (registered.add(type)) excluded.clear(); + } + + public static boolean excludes(TileEntity tileEntity) { + final Class type = tileEntity.getClass(); + Boolean result = excluded.get(type); + if (result == null) { + final TileEntitySpecialRenderer renderer = TileEntityRendererDispatcher.instance.getSpecialRendererByClass(type); + result = matches(type) || renderer != null && matches(renderer.getClass()); + excluded.put(type, result); + } + return result; + } + + private static boolean matches(Class type) { + final String[] names = AngelicaConfig.glowDepthReplayExcludedClasses; + for (Class c = type; c != null && c != Object.class; c = c.getSuperclass()) { + if (registered.contains(c)) return true; + if (names != null) for (String name : names) { + if (c.getName().equals(name)) return true; + } + } + return false; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/PendingTesrDraws.java b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/PendingTesrDraws.java index dc2e9d556..798167585 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/PendingTesrDraws.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/PendingTesrDraws.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.rendering.tesr; +import com.gtnewhorizons.angelica.glsm.DepthEcho; import net.minecraft.tileentity.TileEntity; /** @@ -40,7 +41,12 @@ public final class PendingTesrDraws { final Pending owner = pending; if (owner == null) return; pending = null; - owner.flush(); + DepthEcho.beginImmediate(); + try { + owner.flush(); + } finally { + DepthEcho.endImmediate(); + } } public interface Pending { diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/TesrBatchRenderer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/TesrBatchRenderer.java index e7a9fb3d0..37547afa5 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/TesrBatchRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/TesrBatchRenderer.java @@ -7,6 +7,7 @@ import com.gtnewhorizons.angelica.api.tesr.TesrMaterial; import com.gtnewhorizons.angelica.api.tesr.TesrShader; import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; import com.gtnewhorizons.angelica.compat.mojang.RenderLayer; +import com.gtnewhorizons.angelica.glsm.DepthEcho; import com.gtnewhorizons.angelica.glsm.GLSMAttribStack; import com.gtnewhorizons.angelica.glsm.GLSMBlend; import com.gtnewhorizons.angelica.glsm.GLSMCurrentAttribs; @@ -194,6 +195,7 @@ public final class TesrBatchRenderer { } public void queue(TemplateBuffer template, ResourceLocation texture, TesrMaterial material) { + if (DepthEcho.replaying() && cannotBlend(material)) return; modelView.set(GLSMMatrices.getModelViewMatrix()); final int packedLight = currentPackedLight(material); final int colorABGR = resolveColor(material); @@ -216,6 +218,13 @@ public final class TesrBatchRenderer { } } + private static boolean cannotBlend(TesrMaterial material) { + if (material == TesrMaterial.CURRENT_STATE) return false; + final TesrMaterial.Transparency transparency = material.transparency(); + return transparency == TesrMaterial.Transparency.OPAQUE || transparency == TesrMaterial.Transparency.GLINT || material.special().isGlint() + || material.isDepthEqual() || material.isDepthOnly(); + } + private static Matrix4f captureTextureMatrix() { final Matrix4f texMatrix = GLSMTextures.getTextures().getTextureUnitMatrix(0); return MatrixHelper.isIdentity(texMatrix) ? null : texMatrix; diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyLayers.java b/src/main/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyLayers.java index 556be44f1..425c2ce39 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyLayers.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyLayers.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.rendering.translucency; +import com.gtnewhorizons.angelica.glsm.DepthEcho; import com.gtnewhorizons.angelica.glsm.GLSMAttribStack; import com.gtnewhorizons.angelica.glsm.GLSMBlend; import com.gtnewhorizons.angelica.glsm.GLSMDepthStencilAlpha; @@ -39,6 +40,10 @@ import java.nio.ByteBuffer; /// /// The terrain layer has a second sheet for fragments behind the block-entity layer, so a tile entity inside a /// block's own translucent model is covered only by the faces in front of it. +/// +/// Glows (blended block-entity draws that write no depth) would sort at the opaque depth behind them, so +/// [DepthEcho] records their depth separately, and it is merged into the block-entity layer before anything +/// reads that depth. public final class TranslucencyLayers { private static final int LAYERS = TranslucentLayer.values().length; @@ -78,11 +83,18 @@ public final class TranslucencyLayers { private boolean splitting; private boolean behindDrawn; + private Target glowDepth; + private boolean replayingGlows; + private CompositeProgram program; private CompositeProgram behindProgram; + private GlProgram glowMergeProgram; private int vao; private StateSet prepareState; private StateSet compositeState; + private StateSet clearDepthState; + private final Runnable compositePass = this::drawComposite; + private final Runnable glowMergePass = this::drawGlowMerge; TranslucencyLayers(MainTarget main) { this.main = main; @@ -91,15 +103,19 @@ public final class TranslucencyLayers { /// Whether layering applies is decided on the first [#active] call, once Iris has prepared the frame's pipeline. public void beginFrame() { decided = false; - drawnMask = 0; - behindDrawn = false; + resetFrame(); } void beginFrame(boolean active) { this.active = active; decided = true; + resetFrame(); + } + + private void resetFrame() { drawnMask = 0; behindDrawn = false; + DepthEcho.takeEchoed(); } public boolean active() { @@ -123,13 +139,16 @@ public final class TranslucencyLayers { savedDraw = GLSMFramebuffer.getDrawFramebuffer(); savedRead = GLSMFramebuffer.getReadFramebuffer(); syncSize(); + if (layer == TranslucentLayer.TERRAIN) mergeGlowDepth(); final Target target = layer(layer); final int bit = 1 << layer.ordinal(); if ((drawnMask & bit) == 0) { drawnMask |= bit; prepare(target); + if (layer == TranslucentLayer.BLOCK_ENTITIES) clearDepth(glowDepth()); } target.framebuffer.bind(); + if (layer == TranslucentLayer.BLOCK_ENTITIES) DepthEcho.arm(glowDepth.framebuffer.getId(), target.framebuffer.getId()); open = layer; GLSMBlend.setLayerBlending(true); final ColorMaskStack mask = GLSMBlend.getColorMask(); @@ -139,6 +158,7 @@ public final class TranslucencyLayers { public void end() { BatchStateGuard.beforeChange(); + DepthEcho.disarm(); if (alphaMasked) { final ColorMaskStack mask = GLSMBlend.getColorMask(); GLSMBlend.glColorMask(mask.red, mask.green, mask.blue, false); @@ -154,6 +174,22 @@ public final class TranslucencyLayers { } public void bindOpenLayer() { + (replayingGlows ? glowDepth : layers[open.ordinal()]).framebuffer.bind(); + } + + public boolean beginGlowReplay() { + if (!DepthEcho.armed()) return false; + BatchStateGuard.beforeChange(); + glowDepth.framebuffer.bind(); + replayingGlows = true; + DepthEcho.beginReplay(); + return true; + } + + public void endGlowReplay() { + BatchStateGuard.beforeChange(); + DepthEcho.endReplay(); + replayingGlows = false; layers[open.ordinal()].framebuffer.bind(); } @@ -187,36 +223,64 @@ public final class TranslucencyLayers { public void composite() { if (drawnMask == 0) return; BatchStateGuard.beforeChange(); + syncSize(); + if (drawnMask == 0) return; + try { + mergeGlowDepth(); + fullscreenPass(compositePass); + } finally { + drawnMask = 0; + behindDrawn = false; + } + } + + private void drawComposite() { + final Target snapshot = depthSnapshot(); + blit(main.framebuffer(), snapshot.framebuffer.getId(), GL11.GL_DEPTH_BUFFER_BIT); + GLSMFramebuffer.glBindFramebuffer(GL30.GL_FRAMEBUFFER, main.framebuffer()); + GLSMBlend.enableBlend(); + GLSMBlend.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ZERO, GL11.GL_ONE); + GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_ALWAYS); + bindTexture(0, snapshot.depth); + for (int i = 0; i < LAYERS; i++) { + if ((drawnMask & (1 << i)) == 0) continue; + bindTexture(FIRST_LAYER_UNIT + i, layers[i].color); + bindTexture(FIRST_LAYER_UNIT + LAYERS + i, layers[i].depth); + } + if (behindDrawn) bindTexture(TERRAIN_BEHIND_UNIT, layers[TranslucentLayer.TERRAIN.ordinal()].behind); + final CompositeProgram composite = behindDrawn ? behindProgram : program; + GLSMPrograms.glUseProgram(composite.program.handle()); + GLSMPrograms.glUniform1i(composite.maskUniform, drawnMask); + } + + private void mergeGlowDepth() { + if (!DepthEcho.takeEchoed() || (drawnMask & (1 << TranslucentLayer.BLOCK_ENTITIES.ordinal())) == 0) return; + fullscreenPass(glowMergePass); + } + + private void drawGlowMerge() { + layers[TranslucentLayer.BLOCK_ENTITIES.ordinal()].framebuffer.bind(); + GLSMBlend.disableBlend(); + GLSMBlend.glColorMask(false, false, false, false); + GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_LESS); + bindTexture(0, glowDepth.depth); + GLSMPrograms.glUseProgram(glowMergeProgram.handle()); + } + + private void fullscreenPass(Runnable setup) { final int draw = GLSMFramebuffer.getDrawFramebuffer(); final int read = GLSMFramebuffer.getReadFramebuffer(); final int activeProgram = GLSMPrograms.getActiveProgram(); final int activeVao = GLSMQueries.glGetInteger(GL30.GL_VERTEX_ARRAY_BINDING); if (program == null) initProgram(); - syncSize(); - if (drawnMask == 0) return; - final Target snapshot = depthSnapshot(); final int depth = GLSMAttribStack.pushState(compositeState); try { GLSMRasterizer.disableScissorTest(); - blit(main.framebuffer(), snapshot.framebuffer.getId(), GL11.GL_DEPTH_BUFFER_BIT); - GLSMFramebuffer.glBindFramebuffer(GL30.GL_FRAMEBUFFER, main.framebuffer()); - GLSMRasterizer.glViewport(0, 0, width, height); - GLSMBlend.enableBlend(); - GLSMBlend.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ZERO, GL11.GL_ONE); GLSMRasterizer.disableCull(); GLSMDepthStencilAlpha.enableDepthTest(); - GLSMDepthStencilAlpha.glDepthFunc(GL11.GL_ALWAYS); GLSMDepthStencilAlpha.glDepthMask(true); - bindTexture(0, snapshot.depth); - for (int i = 0; i < LAYERS; i++) { - if ((drawnMask & (1 << i)) == 0) continue; - bindTexture(FIRST_LAYER_UNIT + i, layers[i].color); - bindTexture(FIRST_LAYER_UNIT + LAYERS + i, layers[i].depth); - } - if (behindDrawn) bindTexture(TERRAIN_BEHIND_UNIT, layers[TranslucentLayer.TERRAIN.ordinal()].behind); - final CompositeProgram composite = behindDrawn ? behindProgram : program; - GLSMPrograms.glUseProgram(composite.program.handle()); - GLSMPrograms.glUniform1i(composite.maskUniform, drawnMask); + setup.run(); + GLSMRasterizer.glViewport(0, 0, width, height); GLSMVertexInput.glBindVertexArray(vao); GLSMDraws.glDrawArrays(GL11.GL_TRIANGLES, 0, 3); } finally { @@ -225,8 +289,6 @@ public final class TranslucencyLayers { GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, draw); GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, read); GLSMAttribStack.popStateTo(depth); - drawnMask = 0; - behindDrawn = false; } } @@ -235,15 +297,22 @@ public final class TranslucencyLayers { if (program != null) { program.program.delete(); behindProgram.program.delete(); + glowMergeProgram.delete(); GLSMVertexInput.glDeleteVertexArrays(vao); program = null; behindProgram = null; + glowMergeProgram = null; } } private void initProgram() { program = new CompositeProgram("angelica:translucency_composite", ShaderConstants.EMPTY); behindProgram = new CompositeProgram("angelica:translucency_composite_behind", ShaderConstants.builder().add("TERRAIN_BEHIND").build()); + glowMergeProgram = PanoramaRenderer.loadProgram("angelica:translucency_glow_depth", "angelica:panorama_blur", "angelica:translucency_glow_depth", ctx -> ctx); + final int merge = glowMergeProgram.handle(); + GLSMPrograms.glUseProgram(merge); + GLSMPrograms.glUniform1i(GLSMPrograms.glGetUniformLocation(merge, "u_GlowDepth"), 0); + GLSMPrograms.glUseProgram(0); vao = GLSMVertexInput.glGenVertexArrays(); compositeState = StateSet.forMask(GL11.GL_ALL_ATTRIB_BITS); } @@ -267,6 +336,20 @@ public final class TranslucencyLayers { } } + private void clearDepth(Target target) { + if (clearDepthState == null) clearDepthState = StateSet.forMask(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_SCISSOR_BIT); + final int depth = GLSMAttribStack.pushState(clearDepthState); + try { + GLSMRasterizer.disableScissorTest(); + GLSMDepthStencilAlpha.glDepthMask(true); + GLSMDepthStencilAlpha.glClearDepth(1.0); + target.framebuffer.bind(); + GLSMFramebuffer.glClear(GL11.GL_DEPTH_BUFFER_BIT); + } finally { + GLSMAttribStack.popStateTo(depth); + } + } + private void blit(int from, int to, int mask) { GLSMFramebuffer.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, from); GLSMFramebuffer.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, to); @@ -293,6 +376,11 @@ public final class TranslucencyLayers { return layers[i]; } + private Target glowDepth() { + if (glowDepth == null) glowDepth = new Target(width, height, depthFormat, false); + return glowDepth; + } + private Target depthSnapshot() { if (depthSnapshot == null) depthSnapshot = new Target(width, height, depthFormat, false); return depthSnapshot; @@ -305,8 +393,10 @@ public final class TranslucencyLayers { } if (depthSnapshot != null) depthSnapshot.destroy(); depthSnapshot = null; - drawnMask = 0; - behindDrawn = false; + if (glowDepth != null) glowDepth.destroy(); + glowDepth = null; + DepthEcho.disarm(); + resetFrame(); } private static final class CompositeProgram { diff --git a/src/main/resources/assets/angelica/shaders/translucency_glow_depth.frag b/src/main/resources/assets/angelica/shaders/translucency_glow_depth.frag new file mode 100644 index 000000000..3c8ff3f5b --- /dev/null +++ b/src/main/resources/assets/angelica/shaders/translucency_glow_depth.frag @@ -0,0 +1,9 @@ +#version 330 core + +in vec2 v_TexCoord; + +uniform sampler2D u_GlowDepth; + +void main() { + gl_FragDepth = texture(u_GlowDepth, v_TexCoord).r; +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinEffectRenderer_GlowDepthReplay.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinEffectRenderer_GlowDepthReplay.java new file mode 100644 index 000000000..eb937d2b0 --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinEffectRenderer_GlowDepthReplay.java @@ -0,0 +1,18 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.tesr; + +import com.gtnewhorizons.angelica.glsm.DepthEcho; +import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.client.particle.EntityFX; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(EffectRenderer.class) +public class MixinEffectRenderer_GlowDepthReplay { + + @Inject(method = "addEffect", at = @At("HEAD"), cancellable = true) + private void angelica$dropDuringGlowReplay(EntityFX effect, CallbackInfo ci) { + if (DepthEcho.replaying()) ci.cancel(); + } +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinSoundHandler_GlowDepthReplay.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinSoundHandler_GlowDepthReplay.java new file mode 100644 index 000000000..2d417b206 --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/tesr/MixinSoundHandler_GlowDepthReplay.java @@ -0,0 +1,23 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.tesr; + +import com.gtnewhorizons.angelica.glsm.DepthEcho; +import net.minecraft.client.audio.ISound; +import net.minecraft.client.audio.SoundHandler; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(SoundHandler.class) +public class MixinSoundHandler_GlowDepthReplay { + + @Inject(method = "playSound", at = @At("HEAD"), cancellable = true) + private void angelica$dropDuringGlowReplay(ISound sound, CallbackInfo ci) { + if (DepthEcho.replaying()) ci.cancel(); + } + + @Inject(method = "playDelayedSound", at = @At("HEAD"), cancellable = true) + private void angelica$dropDelayedDuringGlowReplay(ISound sound, int delay, CallbackInfo ci) { + if (DepthEcho.replaying()) ci.cancel(); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyCompositeGLTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyCompositeGLTest.java index 13639bcb3..ae7c5efba 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyCompositeGLTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/translucency/TranslucencyCompositeGLTest.java @@ -1,21 +1,33 @@ package com.gtnewhorizons.angelica.rendering.translucency; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.glsm.DepthEcho; import com.gtnewhorizons.angelica.glsm.GLCoreTest; import com.gtnewhorizons.angelica.glsm.GLSMBlend; +import com.gtnewhorizons.angelica.glsm.GLSMBuffers; import com.gtnewhorizons.angelica.glsm.GLSMDepthStencilAlpha; import com.gtnewhorizons.angelica.glsm.GLSMDraws; +import com.gtnewhorizons.angelica.glsm.GLSMEnable; import com.gtnewhorizons.angelica.glsm.GLSMFramebuffer; +import com.gtnewhorizons.angelica.glsm.GLSMMatrices; import com.gtnewhorizons.angelica.glsm.GLSMPrograms; import com.gtnewhorizons.angelica.glsm.GLSMRasterizer; import com.gtnewhorizons.angelica.glsm.GLSMTextures; import com.gtnewhorizons.angelica.glsm.GLSMVertexInput; import com.gtnewhorizons.angelica.glsm.GlTestSupport; +import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; +import com.gtnewhorizons.angelica.glsm.ffp.VAOManager; +import com.gtnewhorizons.angelica.glsm.hooks.BatchStateGuard; import net.coderbot.iris.gl.framebuffer.GlFramebuffer; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL30; import java.nio.ByteBuffer; @@ -23,6 +35,7 @@ import java.nio.FloatBuffer; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; @GLCoreTest class TranslucencyCompositeGLTest { @@ -227,4 +240,121 @@ class TranslucencyCompositeGLTest { assertColumn(color, 0, 159, 159, 191); assertColumn(color, 1, 64, 96, 159); } + + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void glowsCompositeAtTheirOwnDepthWithoutRejectingEachOther(boolean replayed) { + layers.beginFrame(true); + layers.begin(TranslucentLayer.BLOCK_ENTITIES); + if (replayed) DepthEcho.beginTracking(); + drawGlows(); + BatchStateGuard.suspend(); + drawGlow(2, 0.3f, 0f, 0f, 1f); + BatchStateGuard.resume(); + assertEquals(replayed, DepthEcho.endTracking()); + if (replayed) { + assertTrue(layers.beginGlowReplay()); + drawGlows(); + layers.endGlowReplay(); + } + layers.end(); + assertEquals(GLSMDepthStencilAlpha.getDepthState().isEnabled(), GL11.glGetBoolean(GL11.GL_DEPTH_WRITEMASK)); + drawInto(TranslucentLayer.TERRAIN, 0, 0.6f, 0f, 1f, 0f, 0.5f); + drawInto(TranslucentLayer.TERRAIN, 2, 0.6f, 0f, 1f, 0f, 0.5f); + layers.composite(); + + final ByteBuffer color = readColor(); + assertColumn(color, 0, 64, 64, 128); + assertColumn(color, 1, 64, 128, 64); + assertColumn(color, 2, 64, 64, 128); + } + + private void drawGlows() { + drawGlow(0, 0.3f, 0f, 0f, 1f); + drawGlow(1, 0.3f, 0f, 0f, 1f); + drawGlow(1, 0.5f, 0f, 1f, 0f); + } + + private void drawGlow(int column, float depth, float r, float g, float b) { + GLSMDepthStencilAlpha.glDepthMask(false); + drawBlended(quadProgram, column, depth, r, g, b, 0.5f, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + GLSMDepthStencilAlpha.glDepthMask(true); + } + + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void transparentGlowTexelsLeaveBlockEntitiesBehindThemAtTheirOwnDepth(boolean replayed) { + final boolean ffpEnabled = ShaderManager.isEnabled(); + final int glowVao = GLSMVertexInput.glGenVertexArrays(); + final int glowVbo = GLSMBuffers.glGenBuffers(); + try { + ShaderManager.enable(); + buildFfpGlow(glowVao, glowVbo, 0.3f); + layers.beginFrame(true); + drawInto(TranslucentLayer.BLOCK_ENTITIES, 0, 0.6f, 0f, 0f, 1f, 0.5f); + layers.begin(TranslucentLayer.BLOCK_ENTITIES); + if (replayed) DepthEcho.beginTracking(); + drawFfpGlow(glowVao, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + drawFfpGlow(glowVao, GL11.GL_ONE, GL11.GL_ONE); + assertEquals(replayed, DepthEcho.endTracking()); + if (replayed) { + assertTrue(layers.beginGlowReplay()); + drawFfpGlow(glowVao, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + drawFfpGlow(glowVao, GL11.GL_ONE, GL11.GL_ONE); + layers.endGlowReplay(); + } + layers.end(); + drawInto(TranslucentLayer.TERRAIN, 0, 0.5f, 0f, 1f, 0f, 0.5f); + layers.composite(); + + assertColumn(readColor(), 0, 64, 128, 64); + } finally { + if (ShaderManager.getInstance().isActive()) ShaderManager.getInstance().deactivate(); + if (!ffpEnabled) ShaderManager.disable(); + GLSMVertexInput.glBindVertexArray(0); + GLSMBuffers.glDeleteBuffers(glowVbo); + GLSMVertexInput.glDeleteVertexArrays(glowVao); + } + } + + private static void buildFfpGlow(int vao, int vbo, float depth) { + final ByteBuffer data = BufferUtils.createByteBuffer(3 * 16); + final float z = depth * 2f - 1f; + data.putFloat(-1f).putFloat(-1f).putFloat(z).putInt(0x00000000); + data.putFloat(3f).putFloat(-1f).putFloat(z).putInt(0x00000000); + data.putFloat(-1f).putFloat(3f).putFloat(z).putInt(0x00000000); + data.flip(); + GLSMVertexInput.glBindVertexArray(vao); + GLSMBuffers.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + GLSMBuffers.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW); + final int position = VertexFormatElement.Usage.POSITION.getAttributeLocation(); + final int color = VertexFormatElement.Usage.COLOR.getAttributeLocation(); + GLSMVertexInput.glEnableVertexAttribArray(position); + GLSMVertexInput.glVertexAttribPointer(position, 3, GL11.GL_FLOAT, false, 16, 0L); + GLSMVertexInput.glEnableVertexAttribArray(color); + GLSMVertexInput.glVertexAttribPointer(color, 4, GL11.GL_UNSIGNED_BYTE, true, 16, 12L); + GLSMVertexInput.glBindVertexArray(0); + } + + private static void drawFfpGlow(int vao, int srcRgb, int dstRgb) { + GLSMRasterizer.glViewport(0, 0, 1, 1); + GLSMMatrices.glMatrixMode(GL11.GL_PROJECTION); + GLSMMatrices.glLoadIdentity(); + GLSMMatrices.glMatrixMode(GL11.GL_MODELVIEW); + GLSMMatrices.glLoadIdentity(); + GLSMEnable.glDisable(GL11.GL_LIGHTING); + GLSMEnable.glDisable(GL11.GL_FOG); + GLSMEnable.glDisable(GL11.GL_ALPHA_TEST); + GLSMTextures.disableTexture(); + GLSMDepthStencilAlpha.glDepthMask(false); + GLSMBlend.enableBlend(); + GLSMBlend.glBlendFunc(srcRgb, dstRgb); + GLSMVertexInput.glBindVertexArray(vao); + VAOManager.setCurrentVertexFlags(VertexFlags.COLOR_BIT); + GLSMDraws.glDrawArrays(GL11.GL_TRIANGLES, 0, 3); + GLSMVertexInput.glBindVertexArray(0); + GLSMBlend.disableBlend(); + GLSMDepthStencilAlpha.glDepthMask(true); + GLSMRasterizer.glViewport(0, 0, WIDTH, 1); + } }