Skip to content

Commit

Permalink
Fix #65
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Nov 13, 2023
1 parent 3f4b02b commit 862ede1
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +24,7 @@
@ApiStatus.Internal
public final class ItemDecoratorHandler {
private final List<IItemDecorator> itemDecorators;
private final GlStateBackup stateBackup = new GlStateBackup();

private static Map<Item, ItemDecoratorHandler> DECORATOR_LOOKUP = ImmutableMap.of();

Expand Down Expand Up @@ -50,16 +53,79 @@ 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() {
RenderSystem.enableDepthTest();
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);
}
}
23 changes: 23 additions & 0 deletions items/src/main/resources/porting_lib_items.accesswidener
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 862ede1

Please sign in to comment.