diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/StateSet.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/StateSet.java index 439a0900b..3bfc47fe9 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/StateSet.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/StateSet.java @@ -6,6 +6,8 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import org.lwjgl.opengl.GL11; +import java.util.Arrays; + public final class StateSet { public static final int R_DEPTH = 1; @@ -79,28 +81,47 @@ public final class StateSet { public static final StateSet CUTOUT = buildCutout(); public static final StateSet MASKS = buildMasks(); public static final StateSet CULL = buildCull(); + public static final StateSet BLOCK_ENTITY_PASS = buildBlockEntityPass(); + + private static CowStateStack[] batchMembers() { + return new CowStateStack[] { + GLSMBlend.getBlendState(), GLSMDepthStencilAlpha.getAlphaState(), GLSMDepthStencilAlpha.getDepthState(), + GLSMBlend.getColorMask(), GLSMRasterizer.getPolygonState(), + GLSMTextures.getTextures().getTextureUnitBindings(0), GLSMTextures.getTextures().getTextureUnitBindings(1), + GLSMTextures.getActiveTextureUnitStack(), + GLSMBlend.getBlendMode(), GLSMDepthStencilAlpha.getAlphaTest(), GLSMDepthStencilAlpha.getDepthTest(), + GLSMLighting.getLightingState(), GLSMRasterizer.getCullState(), GLSMRasterizer.getPolygonOffsetFillState(), + GLSMTextures.getTextures().getTextureUnitStates(0), GLSMTextures.getTextures().getTextureUnitStates(1), + GLSMLighting.getLightStates()[0], GLSMLighting.getLightStates()[1], + GLSMLighting.getLightDataStates()[0], GLSMLighting.getLightDataStates()[1], + GLSMLighting.getColorMaterial(), GLSMLighting.getColorMaterialFace(), GLSMLighting.getColorMaterialParameter(), + GLSMLighting.getLightModel(), GLSMLighting.getFrontMaterial(), GLSMLighting.getBackMaterial(), + GLSMLighting.getNormalizeState(), GLSMLighting.getRescaleNormalState() + }; + } private static StateSet buildBatch() { return new StateSet(0, - ids( - GLSMBlend.getBlendState(), GLSMDepthStencilAlpha.getAlphaState(), GLSMDepthStencilAlpha.getDepthState(), - GLSMBlend.getColorMask(), GLSMRasterizer.getPolygonState(), - GLSMTextures.getTextures().getTextureUnitBindings(0), GLSMTextures.getTextures().getTextureUnitBindings(1), - GLSMTextures.getActiveTextureUnitStack(), - GLSMBlend.getBlendMode(), GLSMDepthStencilAlpha.getAlphaTest(), GLSMDepthStencilAlpha.getDepthTest(), - GLSMLighting.getLightingState(), GLSMRasterizer.getCullState(), GLSMRasterizer.getPolygonOffsetFillState(), - GLSMTextures.getTextures().getTextureUnitStates(0), GLSMTextures.getTextures().getTextureUnitStates(1), - GLSMLighting.getLightStates()[0], GLSMLighting.getLightStates()[1], - GLSMLighting.getLightDataStates()[0], GLSMLighting.getLightDataStates()[1], - GLSMLighting.getColorMaterial(), GLSMLighting.getColorMaterialFace(), GLSMLighting.getColorMaterialParameter(), - GLSMLighting.getLightModel(), GLSMLighting.getFrontMaterial(), GLSMLighting.getBackMaterial(), - GLSMLighting.getNormalizeState(), GLSMLighting.getRescaleNormalState() - ), + ids(batchMembers()), R_BLEND | R_DEPTH | R_COLOR_MASK | R_POLYGON | R_TEXTURE | R_ACTIVE_UNIT, B_FRAG_COLORBUF | B_LIGHTING, true); } + private static StateSet buildBlockEntityPass() { + final CowStateStack[] batch = batchMembers(); + final CowStateStack[] all = Arrays.copyOf(batch, batch.length + 4); + all[batch.length] = GLSMCurrentAttribs.getColor(); + all[batch.length + 1] = GLSMFog.getFogState(); + all[batch.length + 2] = GLSMFog.getFogMode(); + all[batch.length + 3] = GLSMRasterizer.getShadeModelState(); + return new StateSet(0, + ids(all), + R_BLEND | R_DEPTH | R_COLOR_MASK | R_POLYGON | R_TEXTURE | R_ACTIVE_UNIT, + B_FRAG_COLORBUF | B_LIGHTING | B_CURRENT | B_FRAG_FOG, + true); + } + private static StateSet buildFont() { return new StateSet(0, ids( diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_StateSet_GLTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_StateSet_GLTest.java index d422ac9c8..863371b9f 100644 --- a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_StateSet_GLTest.java +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_StateSet_GLTest.java @@ -167,6 +167,33 @@ public class GLSM_StateSet_GLTest { || GLSMBlend.getColorMask().blue || GLSMBlend.getColorMask().alpha, "cache color mask - not a HAND member, must stay toggled"); } + @Test + void blockEntityPassRestoresColorFogAndShadeModelOnTopOfBatchButNotStencil() { + GLSMCurrentAttribs.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLSMFog.disableFog(); + GLSMFog.glFogf(GL11.GL_FOG_END, 64.0f); + GLSMRasterizer.glShadeModel(GL11.GL_SMOOTH); + GLSMDepthStencilAlpha.glDepthMask(true); + GLSMEnable.glDisable(GL11.GL_STENCIL_TEST); + + final int d = GLSMAttribStack.pushState(StateSet.BLOCK_ENTITY_PASS); + GLSMCurrentAttribs.glColor4f(0.2f, 0.4f, 0.6f, 0.8f); + GLSMFog.enableFog(); + GLSMFog.glFogf(GL11.GL_FOG_END, 8.0f); + GLSMRasterizer.glShadeModel(GL11.GL_FLAT); + GLSMDepthStencilAlpha.glDepthMask(false); + GLSMEnable.glEnable(GL11.GL_STENCIL_TEST); + GLSMAttribStack.popStateTo(d); + + assertEquals(1.0f, GLSMCurrentAttribs.getColor().getRed(), "cache color red"); + assertEquals(1.0f, GLSMCurrentAttribs.getColor().getAlpha(), "cache color alpha"); + assertFalse(GLSMFog.getFogMode().isEnabled(), "fog enable restored by BLOCK_ENTITY_PASS pop"); + assertEquals(64.0f, GLSMFog.getFogState().getEnd(), "cache fog end"); + assertEquals(GL11.GL_SMOOTH, GLSMRasterizer.getShadeModelState().getValue(), "cache shade model"); + assertTrue(GLSMDepthStencilAlpha.getDepthState().isEnabled(), "depth mask restored, as in BATCH"); + verifyIsEnabled(GL11.GL_STENCIL_TEST, true, "Stencil test - not a BLOCK_ENTITY_PASS member, must stay toggled"); + } + @Test void masksRestoresDepthMaskAndColorMaskButNotDepthTestEnable() { GLSMDepthStencilAlpha.glDepthMask(true); 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 f5a37d485..f84701d96 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java @@ -7,11 +7,13 @@ import com.gtnewhorizons.angelica.compat.bop.FogBiomeCache; import com.gtnewhorizons.angelica.render.CloudRenderer; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.entity.Entity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; +import net.minecraftforge.client.ForgeHooksClient; import net.minecraftforge.client.MinecraftForgeClient; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -35,15 +37,21 @@ import org.embeddedt.embeddium.impl.render.viewport.frustum.SimpleFrustum; import org.joml.FrustumIntersection; import org.joml.Matrix4f; import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL13; import com.gtnewhorizons.angelica.compat.ModStatus; 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.GLSMAttribStack; +import com.gtnewhorizons.angelica.glsm.GLSMBlend; import com.gtnewhorizons.angelica.glsm.GLSMCurrentAttribs; +import com.gtnewhorizons.angelica.glsm.GLSMDepthStencilAlpha; import com.gtnewhorizons.angelica.glsm.GLSMMatrices; import com.gtnewhorizons.angelica.glsm.GLSMRasterizer; +import com.gtnewhorizons.angelica.glsm.GLSMTextures; +import com.gtnewhorizons.angelica.glsm.StateSet; import com.gtnewhorizons.angelica.glsm.ffp.FfpExtendedAttribs; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.mixins.interfaces.ITileEntityBoundingBoxCache; @@ -64,6 +72,8 @@ 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 net.coderbot.iris.Iris; +import net.coderbot.iris.layer.GbufferPrograms; import net.coderbot.iris.pipeline.ShadowRenderer; public class CeleritasWorldRenderer extends SimpleWorldRenderer implements IDynamicLightWorldRenderer { @@ -73,6 +83,7 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer frameOverride = new ObjectArrayList<>(); private final TileEntityDepthOrder depthOrder = new TileEntityDepthOrder(); + private final Matrix4f savedTextureMatrix = new Matrix4f(); + private PassOne passOne = PassOne.NONE; + + private enum PassOne { NONE, PENDING, DRAWN_EARLY } private CeleritasWorldRenderer(Minecraft mc) { // Private constructor for singleton @@ -403,6 +418,58 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer opts.performance.blockEntitiesBeforeTranslucentTerrain = value, opts -> opts.performance.blockEntitiesBeforeTranslucentTerrain) + .setImpact(OptionImpact.LOW) + .build() + ) .add(OptionImpl.createBuilder(boolean.class, sodiumOpts) .setName(I18n.format("sodium.options.use_entity_culling.name")) .setTooltip(I18n.format("sodium.options.use_entity_culling.tooltip")) diff --git a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptions.java b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptions.java index f36a8df2c..a9cd44fda 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptions.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptions.java @@ -234,6 +234,7 @@ public class SodiumGameOptions { public boolean useBlockFaceCulling = true; public boolean useCompactVertexFormat = true; public boolean translucencySorting = true; + public boolean blockEntitiesBeforeTranslucentTerrain = true; public boolean useRenderPassOptimization = true; public int cpuRenderAheadLimit = 3; } diff --git a/src/main/resources/assets/angelica/lang/en_US.lang b/src/main/resources/assets/angelica/lang/en_US.lang index ce823994e..f36a96d37 100644 --- a/src/main/resources/assets/angelica/lang/en_US.lang +++ b/src/main/resources/assets/angelica/lang/en_US.lang @@ -91,6 +91,8 @@ sodium.options.animate_only_visible_textures.name=Animate Only Visible Textures sodium.options.animate_only_visible_textures.tooltip=If enabled, only animated textures determined to be visible will be updated. This can provide a significant boost to frame rates on some hardware, especially with heavier resource packs. If you experience issues with some textures not being animated, try disabling this option. sodium.options.translucency_sorting.name=Translucency Sorting sodium.options.translucency_sorting.tooltip=If enabled, translucent terrain (water, stained glass, ice) and translucent tile entities are sorted back to front so overlapping layers draw correctly. Disabling improves performance in scenes with lots of translucent blocks but causes layering artifacts. Changing this reloads chunks. +sodium.options.block_entities_before_translucent_terrain.name=Tile Entities Before Translucent Terrain +sodium.options.block_entities_before_translucent_terrain.tooltip=If enabled, translucent tile entities are drawn before translucent terrain (water, stained glass, ice), so that the latter does not hide the former behind it. Tile Entities in front of terrain may hide the terrain, and glows in front of terrain may appear behind it. sodium.options.cpu_render_ahead_limit.name=CPU Render-Ahead Limit sodium.options.cpu_render_ahead_limit.tooltip=How many frames the CPU may run ahead of the GPU. Lower values cut input latency, higher values smooth out uneven frame times. Very low or high values may create frame rate instability. 0 disables it; the SDL-GPU backend ignores it and applies its own backpressure. sodium.options.cpu_render_ahead_limit.value=%s frame(s) diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderGlobal.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderGlobal.java index ea53028bc..7aeb8a25d 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderGlobal.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderGlobal.java @@ -9,6 +9,7 @@ import com.gtnewhorizons.angelica.glsm.GLSMEnable; import com.gtnewhorizons.angelica.glsm.GLSMTextures; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.mixins.interfaces.IRenderGlobalExt; +import com.gtnewhorizons.angelica.proxy.ClientProxy; import com.gtnewhorizons.angelica.rendering.AngelicaRenderQueue; import com.gtnewhorizons.angelica.rendering.RenderingState; import com.gtnewhorizons.angelica.rendering.celeritas.BlockRenderLayer; @@ -18,7 +19,6 @@ import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher; import com.gtnewhorizons.angelica.rendering.tesr.TesrBatchRenderer; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import net.coderbot.iris.Iris; -import net.coderbot.iris.layer.GbufferPrograms; import net.coderbot.iris.pipeline.HandRenderer; import net.coderbot.iris.pipeline.WorldRenderingPhase; import net.coderbot.iris.pipeline.WorldRenderingPipeline; @@ -34,6 +34,7 @@ import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.client.MinecraftForgeClient; import org.embeddedt.embeddium.impl.gl.device.RenderDevice; import org.embeddedt.embeddium.impl.render.terrain.SimpleWorldRenderer; import org.embeddedt.embeddium.impl.render.viewport.Viewport; @@ -63,7 +64,6 @@ import static org.joml.Math.lerp; */ @Mixin(value = RenderGlobal.class, priority = 900) public class MixinRenderGlobal implements IRenderGlobalExt { - @Unique private static final long P_BLOCK_ENTITIES_RENDERED = Tracy.plotHandle("blockEntitiesRendered"); @Unique private static final Tracy.ZoneId Z_MT_QUEUE = Tracy.zoneId("mtQueue", Tracy.COLOR_TERRAIN); @Shadow public Minecraft mc; @@ -146,10 +146,13 @@ public class MixinRenderGlobal implements IRenderGlobalExt { pipeline = Iris.getPipelineManager().getPipelineNullable(); if (pass == 0) { pipeline.setPhase(WorldRenderingPhase.TERRAIN_SOLID); - } else if (pass == 1) { - if (!ShadowRenderingState.areShadowsCurrentlyBeingRendered()) { - iris$beginTranslucents(pipeline, Camera.INSTANCE); - } + } else if (pass == 1 && !ShadowRenderingState.areShadowsCurrentlyBeingRendered()) { + iris$beginTranslucents(pipeline, Camera.INSTANCE); + } + } + if (pass == 1) { + celeritas$renderBlockEntitiesBeforeTranslucent(partialTicks); + if (pipeline != null) { pipeline.setPhase(WorldRenderingPhase.TERRAIN_TRANSLUCENT); this.renderEngine.bindTexture(TextureMap.locationBlocksTexture); } @@ -276,17 +279,18 @@ public class MixinRenderGlobal implements IRenderGlobalExt { at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/RenderHelper;enableStandardItemLighting()V", ordinal = 0)) private void celeritas$enterBlockEntities() {} + @Unique + private void celeritas$renderBlockEntitiesBeforeTranslucent(double partialTicks) { + if (ShadowRenderingState.areShadowsCurrentlyBeingRendered()) return; + if (!ClientProxy.options().performance.blockEntitiesBeforeTranslucentTerrain) return; + if (this.mc.entityRenderer.debugViewDirection != 0) return; + this.celeritas$renderer.renderBlockEntitiesBeforeTranslucent((float) partialTicks); + } + @Surround.Return("blockEntities") private void celeritas$renderTileEntities(@Surround.Local(argsOnly = true) float partialTicks) { - if (Iris.enabled) { - GbufferPrograms.beginBlockEntities(); - GbufferPrograms.setBlockEntityDefaults(); - } - final int blockEntitiesRendered = this.celeritas$renderer.renderBlockEntities(partialTicks); - if (Tracy.ENABLED) Tracy.plotInt(P_BLOCK_ENTITIES_RENDERED, blockEntitiesRendered); - if (Iris.enabled) { - GbufferPrograms.endBlockEntities(); - } + if (MinecraftForgeClient.getRenderPass() == 1 && !this.celeritas$renderer.takeLatePassOne()) return; + this.celeritas$renderer.renderBlockEntitiesInPhase(partialTicks); } @ModifyExpressionValue(method = "renderEntities", at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/RenderGlobal;tileEntities:Ljava/util/List;", opcode = Opcodes.GETFIELD))