From 862ede1a4dd6c4e55408c99d042ee176317e8df5 Mon Sep 17 00:00:00 2001 From: alpha Date: Sun, 12 Nov 2023 23:15:04 -0600 Subject: [PATCH] Fix #65 --- .../item/impl/client/GlStateBackup.java | 40 +++++++++++ .../impl/client/ItemDecoratorHandler.java | 66 +++++++++++++++++++ .../resources/porting_lib_items.accesswidener | 23 +++++++ 3 files changed, 129 insertions(+) create mode 100644 items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/GlStateBackup.java create mode 100644 items/src/main/resources/porting_lib_items.accesswidener diff --git a/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/GlStateBackup.java b/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/GlStateBackup.java new file mode 100644 index 000000000..1b145b21a --- /dev/null +++ b/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/GlStateBackup.java @@ -0,0 +1,40 @@ +package io.github.fabricators_of_create.porting_lib.item.impl.client; + +import com.mojang.blaze3d.systems.RenderSystem; + +/** + * Backup of the OpenGL render state, for use in GUI rendering that needs to be able to go back to the previous + * render state after calling third-party renderers which may apply arbitrary modifications to the render state. + * + *

Create a backup before changing the global render state with {@link RenderSystem#backupGlState(GlStateBackup)}, + * and apply the backup with {@link RenderSystem#restoreGlState(GlStateBackup)}. + */ +public final class GlStateBackup { + public boolean blendEnabled; + public int blendSrcRgb; + public int blendDestRgb; + public int blendSrcAlpha; + public int blendDestAlpha; + public boolean depthEnabled; + public boolean depthMask; + public int depthFunc; + public boolean cullEnabled; + public boolean polyOffsetFillEnabled; + public boolean polyOffsetLineEnabled; + public float polyOffsetFactor; + public float polyOffsetUnits; + public boolean colorLogicEnabled; + public int colorLogicOp; + public int stencilFuncFunc; + public int stencilFuncRef; + public int stencilFuncMask; + public int stencilMask; + public int stencilFail; + public int stencilZFail; + public int stencilZPass; + public boolean scissorEnabled; + public boolean colorMaskRed; + public boolean colorMaskGreen; + public boolean colorMaskBlue; + public boolean colorMaskAlpha; +} diff --git a/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/ItemDecoratorHandler.java b/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/ItemDecoratorHandler.java index d875d5174..ff8445322 100644 --- a/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/ItemDecoratorHandler.java +++ b/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/client/ItemDecoratorHandler.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.Map; +import com.mojang.blaze3d.platform.GlStateManager; + import io.github.fabricators_of_create.porting_lib.item.api.client.IItemDecorator; import io.github.fabricators_of_create.porting_lib.item.api.client.callbacks.ItemDecorationsCallback; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; @@ -22,6 +24,7 @@ @ApiStatus.Internal public final class ItemDecoratorHandler { private final List itemDecorators; + private final GlStateBackup stateBackup = new GlStateBackup(); private static Map DECORATOR_LOOKUP = ImmutableMap.of(); @@ -50,11 +53,15 @@ public static ItemDecoratorHandler of(ItemStack stack) { } public void render(GuiGraphics guiGraphics, Font font, ItemStack stack, int xOffset, int yOffset) { + backupGlState(stateBackup); + resetRenderState(); for (IItemDecorator itemDecorator : itemDecorators) { if (itemDecorator.render(guiGraphics, font, stack, xOffset, yOffset)) resetRenderState(); } + + restoreGlState(stateBackup); } private void resetRenderState() { @@ -62,4 +69,63 @@ private void resetRenderState() { RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); } + + public static void backupGlState(GlStateBackup state) { + RenderSystem.assertOnRenderThread(); + _backupGlState(state); + } + + public static void restoreGlState(GlStateBackup state) { + RenderSystem.assertOnRenderThread(); + _restoreGlState(state); + } + + public static void _backupGlState(GlStateBackup state) { + state.blendEnabled = GlStateManager.BLEND.mode.enabled; + state.blendSrcRgb = GlStateManager.BLEND.srcRgb; + state.blendDestRgb = GlStateManager.BLEND.dstRgb; + state.blendSrcAlpha = GlStateManager.BLEND.srcAlpha; + state.blendDestAlpha = GlStateManager.BLEND.dstAlpha; + state.depthEnabled = GlStateManager.DEPTH.mode.enabled; + state.depthMask = GlStateManager.DEPTH.mask; + state.depthFunc = GlStateManager.DEPTH.func; + state.cullEnabled = GlStateManager.CULL.enable.enabled; + state.polyOffsetFillEnabled = GlStateManager.POLY_OFFSET.fill.enabled; + state.polyOffsetLineEnabled = GlStateManager.POLY_OFFSET.line.enabled; + state.polyOffsetFactor = GlStateManager.POLY_OFFSET.factor; + state.polyOffsetUnits = GlStateManager.POLY_OFFSET.units; + state.colorLogicEnabled = GlStateManager.COLOR_LOGIC.enable.enabled; + state.colorLogicOp = GlStateManager.COLOR_LOGIC.op; + state.stencilFuncFunc = GlStateManager.STENCIL.func.func; + state.stencilFuncRef = GlStateManager.STENCIL.func.ref; + state.stencilFuncMask = GlStateManager.STENCIL.func.mask; + state.stencilMask = GlStateManager.STENCIL.mask; + state.stencilFail = GlStateManager.STENCIL.fail; + state.stencilZFail = GlStateManager.STENCIL.zfail; + state.stencilZPass = GlStateManager.STENCIL.zpass; + state.scissorEnabled = GlStateManager.SCISSOR.mode.enabled; + state.colorMaskRed = GlStateManager.COLOR_MASK.red; + state.colorMaskGreen = GlStateManager.COLOR_MASK.green; + state.colorMaskBlue = GlStateManager.COLOR_MASK.blue; + state.colorMaskAlpha = GlStateManager.COLOR_MASK.alpha; + } + + public static void _restoreGlState(GlStateBackup state) { + GlStateManager.BLEND.mode.setEnabled(state.blendEnabled); + GlStateManager._blendFuncSeparate(state.blendSrcRgb, state.blendDestRgb, state.blendSrcAlpha, state.blendDestAlpha); + GlStateManager.DEPTH.mode.setEnabled(state.depthEnabled); + GlStateManager._depthMask(state.depthMask); + GlStateManager._depthFunc(state.depthFunc); + GlStateManager.CULL.enable.setEnabled(state.cullEnabled); + GlStateManager.POLY_OFFSET.fill.setEnabled(state.polyOffsetFillEnabled); + GlStateManager.POLY_OFFSET.line.setEnabled(state.polyOffsetLineEnabled); + GlStateManager._polygonOffset(state.polyOffsetFactor, state.polyOffsetUnits); + GlStateManager.COLOR_LOGIC.enable.setEnabled(state.colorLogicEnabled); + GlStateManager._logicOp(state.colorLogicOp); + GlStateManager._stencilFunc(state.stencilFuncFunc, state.stencilFuncRef, state.stencilFuncMask); + GlStateManager._stencilMask(state.stencilMask); + GlStateManager._stencilOp(state.stencilFail, state.stencilZFail, state.stencilZPass); + GlStateManager.SCISSOR.mode.setEnabled(state.scissorEnabled); + GlStateManager._colorMask(state.colorMaskRed, state.colorMaskGreen, state.colorMaskBlue, state.colorMaskAlpha); + } } diff --git a/items/src/main/resources/porting_lib_items.accesswidener b/items/src/main/resources/porting_lib_items.accesswidener new file mode 100644 index 000000000..055c73a1c --- /dev/null +++ b/items/src/main/resources/porting_lib_items.accesswidener @@ -0,0 +1,23 @@ +accessWidener v2 named +accessible field com/mojang/blaze3d/platform/GlStateManager BLEND Lcom/mojang/blaze3d/platform/GlStateManager$BlendState; +accessible field com/mojang/blaze3d/platform/GlStateManager DEPTH Lcom/mojang/blaze3d/platform/GlStateManager$DepthState; +accessible field com/mojang/blaze3d/platform/GlStateManager CULL Lcom/mojang/blaze3d/platform/GlStateManager$CullState; +accessible field com/mojang/blaze3d/platform/GlStateManager POLY_OFFSET Lcom/mojang/blaze3d/platform/GlStateManager$PolygonOffsetState; +accessible field com/mojang/blaze3d/platform/GlStateManager COLOR_LOGIC Lcom/mojang/blaze3d/platform/GlStateManager$ColorLogicState; +accessible field com/mojang/blaze3d/platform/GlStateManager STENCIL Lcom/mojang/blaze3d/platform/GlStateManager$StencilState; +accessible field com/mojang/blaze3d/platform/GlStateManager SCISSOR Lcom/mojang/blaze3d/platform/GlStateManager$ScissorState; +accessible field com/mojang/blaze3d/platform/GlStateManager COLOR_MASK Lcom/mojang/blaze3d/platform/GlStateManager$ColorMask; + +accessible class com/mojang/blaze3d/platform/GlStateManager$BlendState +accessible class com/mojang/blaze3d/platform/GlStateManager$DepthState +accessible class com/mojang/blaze3d/platform/GlStateManager$CullState +accessible class com/mojang/blaze3d/platform/GlStateManager$PolygonOffsetState +accessible class com/mojang/blaze3d/platform/GlStateManager$ColorLogicState +accessible class com/mojang/blaze3d/platform/GlStateManager$StencilState +accessible class com/mojang/blaze3d/platform/GlStateManager$ScissorState +accessible class com/mojang/blaze3d/platform/GlStateManager$ColorMask + +accessible class com/mojang/blaze3d/platform/GlStateManager$BooleanState +accessible field com/mojang/blaze3d/platform/GlStateManager$BooleanState enabled Z + +accessible class com/mojang/blaze3d/platform/GlStateManager$StencilFunc