diff --git a/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrMaterial.java b/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrMaterial.java index 7ca17dbca..daaa0fdac 100644 --- a/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrMaterial.java +++ b/src/main/java/com/gtnewhorizons/angelica/api/tesr/TesrMaterial.java @@ -17,7 +17,11 @@ public final class TesrMaterial { public enum Transparency { OPAQUE, TRANSLUCENT, ADDITIVE, ADDITIVE_ALPHA, GLINT } - public enum SpecialRender { NONE, BEACON_BEAM, GLINT } + public enum SpecialRender { + NONE, BEACON_BEAM, GLINT, ITEM_GLINT; + + public boolean isGlint() { return this == GLINT || this == ITEM_GLINT; } + } private static final ConcurrentHashMap INTERNED = new ConcurrentHashMap<>(); diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/mojang/RenderLayer.java b/src/main/java/com/gtnewhorizons/angelica/compat/mojang/RenderLayer.java index 7a5553cfa..c75cad785 100644 --- a/src/main/java/com/gtnewhorizons/angelica/compat/mojang/RenderLayer.java +++ b/src/main/java/com/gtnewhorizons/angelica/compat/mojang/RenderLayer.java @@ -140,7 +140,7 @@ public final class RenderLayer implements BlendingStateHolder { private static Hook hookFor(TesrMaterial material, PassOverride pass, int glintSlot, boolean lightmap) { switch (material.special()) { - case GLINT -> { + case GLINT, ITEM_GLINT -> { return glintHook(glintSlot); } case BEACON_BEAM -> { diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/SkippedGlintBlock.java b/src/main/java/com/gtnewhorizons/angelica/rendering/SkippedGlintBlock.java index 4792eef43..c200a2577 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/SkippedGlintBlock.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/SkippedGlintBlock.java @@ -20,7 +20,15 @@ public final class SkippedGlintBlock { } public static void applyHeldItemExitState() { - applyExitState(0.5F * 0.76F, 0.25F * 0.76F, 0.8F * 0.76F, 1.0F, GL11.GL_SRC_COLOR, GL11.GL_ONE, GL11.GL_ONE, GL11.GL_ZERO, false, false); + applyItemExitState(GL11.GL_ONE, GL11.GL_ZERO); + } + + public static void applyDroppedItemExitState() { + applyItemExitState(GL11.GL_SRC_COLOR, GL11.GL_ONE); + } + + private static void applyItemExitState(int srcAlpha, int dstAlpha) { + applyExitState(0.5F * 0.76F, 0.25F * 0.76F, 0.8F * 0.76F, 1.0F, GL11.GL_SRC_COLOR, GL11.GL_ONE, srcAlpha, dstAlpha, false, false); } private static void applyExitState(float red, float green, float blue, float alpha, int srcRgb, int dstRgb, int srcAlpha, int dstAlpha, boolean resetTextureMatrix, boolean restoreDepthMask) { diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/items/DroppedItemInstancer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/items/DroppedItemInstancer.java index 8f5c74cf1..3cd0c0ab1 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/items/DroppedItemInstancer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/items/DroppedItemInstancer.java @@ -40,8 +40,7 @@ public final class DroppedItemInstancer { private static boolean glintSeen; private static final ItemPropCache.ItemProp glintProperties = new ItemPropCache.ItemProp(); private static final ItemPropCache.ItemProp currentGlintProperties = new ItemPropCache.ItemProp(); - private static TemplateBuffer glintTemplate; - private static int glintTemplateWidth, glintTemplateHeight; + private static final GlintMesh glintMesh = new GlintMesh(); private static long instanced; private static long glintInstanced; @@ -183,16 +182,14 @@ public final class DroppedItemInstancer { return drawsBefore; } - public static boolean queueSkippedGlint(int iconWidth, int iconHeight) { - if (!basePart || !BatchEligibility.batchingAllowed()) return false; - if (glintTemplate == null || glintTemplateWidth != iconWidth || glintTemplateHeight != iconHeight) { - final IconMesh hit = icons.get(0.0F, 0.0F, 1.0F, 1.0F, iconWidth, iconHeight, 0.0625F); - if (hit == null || hit.template == null) return false; - glintTemplate = hit.template; - glintTemplateWidth = iconWidth; - glintTemplateHeight = iconHeight; - } - if (!ModelPartBatcher.INSTANCE.queueSkippedHeldGlint(glintTemplate)) return false; + public static boolean canQueueSkippedGlint() { + return basePart && BatchEligibility.batchingAllowed(); + } + + public static boolean queueSkippedGlint(int stripsWide, int stripsHigh, TesrMaterial material) { + if (!canQueueSkippedGlint()) return false; + final TemplateBuffer template = glintMesh.get(stripsWide, stripsHigh); + if (template == null || !ModelPartBatcher.INSTANCE.queueSkippedItemGlint(template, material)) return false; glintInstanced += 2; BatchEligibility.onPartQueued(); BatchEligibility.onPartQueued(); @@ -276,13 +273,27 @@ public final class DroppedItemInstancer { basePart = false; glintCapture.reset(); glintSeen = false; - glintTemplate = null; + glintMesh.template = null; if (blockBackend != null) { blockBackend.delete(); blockBackend = null; } } + private static final class GlintMesh { + TemplateBuffer template; + int stripsWide, stripsHigh; + + TemplateBuffer get(int stripsWide, int stripsHigh) { + if (template == null || this.stripsWide != stripsWide || this.stripsHigh != stripsHigh) { + template = iconTemplate(0.0F, 0.0F, 1.0F, 1.0F, stripsWide, stripsHigh, 0.0625F); + this.stripsWide = stripsWide; + this.stripsHigh = stripsHigh; + } + return template; + } + } + private static TemplateBuffer iconTemplate(float maxU, float minV, float minU, float maxV, int width, int height, float thickness) { final IconMesh hit = icons.get(maxU, minV, minU, maxV, width, height, thickness); if (hit != null) return hit.template; diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/items/HeldItemGlint.java b/src/main/java/com/gtnewhorizons/angelica/rendering/items/ItemGlint.java similarity index 96% rename from src/main/java/com/gtnewhorizons/angelica/rendering/items/HeldItemGlint.java rename to src/main/java/com/gtnewhorizons/angelica/rendering/items/ItemGlint.java index 8fb7e406e..0a5dc99c9 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/items/HeldItemGlint.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/items/ItemGlint.java @@ -16,15 +16,15 @@ import org.lwjgl.opengl.GL11; import javax.imageio.ImageIO; import java.io.IOException; -/** One draw for the two vanilla held-item layers; modified glint sequences retain their original draws. */ -public final class HeldItemGlint { +/** One draw for the two vanilla held and dropped item layers; modified glint sequences retain their original draws. */ +public final class ItemGlint { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/misc/enchanted_item_glint.png"); private static final Matrix4f first = new Matrix4f(); private static final Matrix4f second = new Matrix4f(); private static long matrixTime = Long.MIN_VALUE; private static boolean listening, checked, opaque; - private HeldItemGlint() {} + private ItemGlint() {} private static boolean batchedGlintGuarded() { return ModelPartBatcher.INSTANCE.entityPassActive() && ModelPartBatcher.INSTANCE.canQueueGlint() && canResetStencil(); diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterials.java b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterials.java index 6d78ae259..8e48df613 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterials.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterials.java @@ -30,8 +30,8 @@ public final class EntityMaterials { static final TesrMaterial DEPTH_ONLY = TesrMaterial.builder().depthOnly().stream().build(); static final TesrMaterial DEPTH_ONLY_CUTOUT = TesrMaterial.builder().depthOnly().cutout(0.1f).stream().build(); static final TesrMaterial OVERLAY = TesrMaterial.builder().translucent().depthEqual().stream().build(); - public static final TesrMaterial GLINT = TesrMaterial.builder().glint().depthEqual().noDepthWrite().unlit().stream().special(TesrMaterial.SpecialRender.GLINT).build(); - public static final TesrMaterial ITEM_GLINT = TesrMaterial.builder().glint().depthEqual().noDepthWrite().unlit().stream().special(TesrMaterial.SpecialRender.GLINT).build(); + public static final TesrMaterial GLINT = glintMaterial(TesrMaterial.SpecialRender.GLINT); + public static final TesrMaterial ITEM_GLINT = glintMaterial(TesrMaterial.SpecialRender.ITEM_GLINT); private static final BlendState effectiveBlend = new BlendState(); private static final AlphaState effectiveAlpha = new AlphaState(); @@ -39,6 +39,10 @@ public final class EntityMaterials { private EntityMaterials() {} + private static TesrMaterial glintMaterial(TesrMaterial.SpecialRender special) { + return TesrMaterial.builder().glint().depthEqual().noDepthWrite().unlit().stream().special(special).build(); + } + static TesrMaterial fromCurrentState(boolean texIdentity) { final boolean textured = GLStateManager.getTextures().getTextureUnitStates(0).isEnabled(); final AlphaState alpha = GLStateManager.getEffectiveAlphaState(effectiveAlpha); diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/ModelPartBatcher.java b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/ModelPartBatcher.java index 4494d527e..506c36202 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/ModelPartBatcher.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/ModelPartBatcher.java @@ -26,7 +26,7 @@ import com.gtnewhorizons.angelica.glsm.states.PolygonState; import com.gtnewhorizons.angelica.mixins.interfaces.ModelBoxData; import com.gtnewhorizons.angelica.profiling.BailClassCounts; import com.gtnewhorizons.angelica.rendering.GlintClock; -import com.gtnewhorizons.angelica.rendering.items.HeldItemGlint; +import com.gtnewhorizons.angelica.rendering.items.ItemGlint; import com.gtnewhorizons.angelica.shadercompat.ShaderGlint; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectIterator; @@ -454,14 +454,14 @@ public final class ModelPartBatcher { return true; } - public boolean queueSkippedHeldGlint(TemplateBuffer template) { - if (!canQueueGlint() || GLStateManager.getActiveTextureUnit() != 0 || !activeGroups.canCopyInstances(EntityMaterials.ITEM_GLINT) || !MatrixHelper.isIdentity(GLStateManager.getTextures().getTextureUnitMatrix(0))) return false; + public boolean queueSkippedItemGlint(TemplateBuffer template, TesrMaterial material) { + if (!canQueueGlint() || GLStateManager.getActiveTextureUnit() != 0 || !activeGroups.canCopyInstances(material) || !MatrixHelper.isIdentity(GLStateManager.getTextures().getTextureUnitMatrix(0))) return false; final boolean offset = GLStateManager.glIsEnabled(GL11.GL_POLYGON_OFFSET_FILL); final PolygonState polygon = GLStateManager.getPolygonState(); - final int cullCode = DrawState.liveCull(EntityMaterials.ITEM_GLINT); - final RenderLayer layer = layerFor(HeldItemGlint.texture(), EntityMaterials.ITEM_GLINT, PassOverride.NONE, offset ? polygon.getOffsetFactor() : 0, offset ? polygon.getOffsetUnits() : 0, ShaderGlint.NO_TINT, cullCode, false, DrawState.liveLightmap()); - activeGroups.queueGlintLayers(template, layer, EntityMaterials.ITEM_GLINT, currentModelView(), GLSMConfig.packedLastBrightness(), VANILLA_GLINT_COLOR, currentOverlay(), currentEntityInfo(0), - HeldItemGlint.firstMatrix(), HeldItemGlint.secondMatrix()); + final int cullCode = DrawState.liveCull(material); + final RenderLayer layer = layerFor(ItemGlint.texture(), material, PassOverride.NONE, offset ? polygon.getOffsetFactor() : 0, offset ? polygon.getOffsetUnits() : 0, ShaderGlint.NO_TINT, cullCode, false, DrawState.liveLightmap()); + activeGroups.queueGlintLayers(template, layer, material, currentModelView(), GLSMConfig.packedLastBrightness(), VANILLA_GLINT_COLOR, currentOverlay(), currentEntityInfo(0), + ItemGlint.firstMatrix(), ItemGlint.secondMatrix()); parts++; glintPassReuses++; return true; @@ -625,7 +625,7 @@ public final class ModelPartBatcher { final PolygonState polygon = GLStateManager.getPolygonState(); final float offsetFactor = offset ? polygon.getOffsetFactor() : 0.0f; final float offsetUnits = offset ? polygon.getOffsetUnits() : 0.0f; - final int glintSlot = material.special() == TesrMaterial.SpecialRender.GLINT ? lastBoundGlintSlot : ShaderGlint.NO_TINT; + final int glintSlot = material.special().isGlint() ? lastBoundGlintSlot : ShaderGlint.NO_TINT; final int cullCode = DrawState.liveCull(material); final boolean lit = DrawState.liveLit(material); final EntityTextureAtlas.Slot atlasSlot = texMatrix == null && cubesInsideTexture ? atlasSlotFor(material) : null; diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/RetainedTesrGroups.java b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/RetainedTesrGroups.java index 75ab1c2dd..9f751a208 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/RetainedTesrGroups.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/tesr/RetainedTesrGroups.java @@ -1136,10 +1136,10 @@ final class RetainedTesrGroups implements AngelicaBufferSource.LayerDrawHook { if (!live(group) || !queuedInstanced(group)) continue; if (group.templateColumns.hasInstances()) { final boolean fixedFunction = deferred == null && GLStateManager.getActiveProgram() == 0; - drawTemplateRuns(group.templateColumns, group.material.special() == TesrMaterial.SpecialRender.GLINT && fixedFunction, group.material == EntityMaterials.ITEM_GLINT); + drawTemplateRuns(group.templateColumns, group.material.special().isGlint() && fixedFunction, group.material == EntityMaterials.ITEM_GLINT); } if (!cubeAvailable && group.cubeColumns.hasInstances() && !group.cubeColumns.anyAtlasSlot) { - drawTemplateRuns(group.cubeColumns, group.material == EntityMaterials.GLINT && deferred == null, false); + drawTemplateRuns(group.cubeColumns, group.material.special().isGlint() && deferred == null, false); } } restoreTexMatrix(); @@ -1281,7 +1281,7 @@ final class RetainedTesrGroups implements AngelicaBufferSource.LayerDrawHook { for (int r = 0, n = cols.runs.size(); r < n; r++) { final TexRun run = cols.runs.get(r); applyTexMatrix(run); - final boolean combined = group.material == EntityMaterials.GLINT && deferred == null && r + 1 < n && combineRuns(cols, run, cols.runs.get(r + 1)); + final boolean combined = group.material.special().isGlint() && deferred == null && r + 1 < n && combineRuns(cols, run, cols.runs.get(r + 1)); try { if (canRedraw(run)) { instanced.redrawCubes(run.sourceRun.uploadOffset, run.instances, run.color); diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/bugfixes/MixinItemRenderer_EdgeDepth.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/bugfixes/MixinItemRenderer_EdgeDepth.java index cc4ad7f3e..afd00bbde 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/bugfixes/MixinItemRenderer_EdgeDepth.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/bugfixes/MixinItemRenderer_EdgeDepth.java @@ -6,7 +6,7 @@ import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.ffp.CombinedGlint; import com.gtnewhorizons.angelica.glsm.hooks.BatchStateGuard; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; -import com.gtnewhorizons.angelica.rendering.items.HeldItemGlint; +import com.gtnewhorizons.angelica.rendering.items.ItemGlint; import net.coderbot.iris.pipeline.ShadowRenderer; import net.minecraft.client.renderer.ItemRenderer; import net.minecraft.client.renderer.Tessellator; @@ -62,7 +62,7 @@ public class MixinItemRenderer_EdgeDepth { final boolean preparing = !ShadowRenderer.ACTIVE && !GLStateManager.isRecordingDisplayList() && !TessellatorManager.isCurrentlyCapturing() && !TessellatorManager.shouldInterceptDraw(tess) && GLStateManager.getDepthTest().isEnabled() && GLStateManager.getDepthState().isEnabled() - && HeldItemGlint.canResetStencil() && HeldItemGlint.eligible() && HeldItemGlint.needsImmediateBase(stack, pass); + && ItemGlint.canResetStencil() && ItemGlint.eligible() && ItemGlint.needsImmediateBase(stack, pass); if (preparing) { BatchStateGuard.suspend(); try { @@ -124,7 +124,7 @@ public class MixinItemRenderer_EdgeDepth { final boolean secondLayer = angelica$firstLayerMarked; BatchStateGuard.suspend(); try { - final boolean combined = !secondLayer && HeldItemGlint.begin(); + final boolean combined = !secondLayer && ItemGlint.begin(); // Depth selects the surface, stencil limits it to one blend. // The base draw's stencil reset or the first layer's marks avoid a masked full-screen clear. GLStateManager.glPushAttrib(GL11.GL_STENCIL_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); @@ -150,7 +150,7 @@ public class MixinItemRenderer_EdgeDepth { if (combined) CombinedGlint.end(); } angelica$combinedGlint = combined; - angelica$firstLayerMarked = !combined && !secondLayer && HeldItemGlint.layersCoverSamePixels(); + angelica$firstLayerMarked = !combined && !secondLayer && ItemGlint.layersCoverSamePixels(); } finally { BatchStateGuard.resume(); } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinItemRenderer_Instanced.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinItemRenderer_Instanced.java index d832c07ce..7544a3744 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinItemRenderer_Instanced.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinItemRenderer_Instanced.java @@ -5,7 +5,7 @@ import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.rendering.GlintClock; import com.gtnewhorizons.angelica.rendering.SkippedGlintBlock; import com.gtnewhorizons.angelica.rendering.items.DroppedItemInstancer; -import com.gtnewhorizons.angelica.rendering.items.HeldItemGlint; +import com.gtnewhorizons.angelica.rendering.items.ItemGlint; import com.gtnewhorizons.angelica.rendering.tesr.EntityMaterials; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import net.minecraft.block.Block; @@ -78,7 +78,7 @@ public abstract class MixinItemRenderer_Instanced { try { // A later pass without glint covers the deferred glint of an earlier one, as vanilla draws it after that glint. final boolean afterGlint = angelica$deferredGlintStack != null && stack == angelica$deferredGlintStack && !stack.hasEffect(angelica$pass); - return DroppedItemInstancer.icon(angelica$batchable(angelica$type) && !HeldItemGlint.needsImmediateBase(stack, angelica$pass) ? stack : null, afterGlint, maxU, minV, minU, maxV, width, height, thickness); + return DroppedItemInstancer.icon(angelica$batchable(angelica$type) && !ItemGlint.needsImmediateBase(stack, angelica$pass) ? stack : null, afterGlint, maxU, minV, minU, maxV, width, height, thickness); } catch (Throwable t) { if (Tracy.FINE_ZONES) Tracy.endZone(); throw t; @@ -91,7 +91,7 @@ public abstract class MixinItemRenderer_Instanced { remap = false ) private boolean angelica$queueHeldGlintBlock(boolean hasEffect) { - if (!hasEffect || !HeldItemGlint.eligible() || !DroppedItemInstancer.queueSkippedGlint(angelica$iconWidth, angelica$iconHeight)) return hasEffect; + if (!hasEffect || !ItemGlint.eligible() || !DroppedItemInstancer.queueSkippedGlint(angelica$iconWidth, angelica$iconHeight, EntityMaterials.ITEM_GLINT)) return hasEffect; angelica$deferredGlintStack = angelica$stack; angelica$deferredGlintPass = angelica$pass; SkippedGlintBlock.applyHeldItemExitState(); diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinRenderItem_Instanced.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinRenderItem_Instanced.java index c2e38eafb..de32eefda 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinRenderItem_Instanced.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/entity/MixinRenderItem_Instanced.java @@ -3,21 +3,47 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.entity; import com.gtnewhorizons.angelica.experimental.surround.Surround; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.rendering.GlintClock; +import com.gtnewhorizons.angelica.rendering.SkippedGlintBlock; import com.gtnewhorizons.angelica.rendering.items.DroppedItemInstancer; +import com.gtnewhorizons.angelica.rendering.items.ItemGlint; import com.gtnewhorizons.angelica.rendering.tesr.BatchEligibility; import com.gtnewhorizons.angelica.rendering.tesr.EntityMaterials; +import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher; import net.minecraft.block.Block; import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.entity.RenderItem; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.IIcon; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(RenderItem.class) public abstract class MixinRenderItem_Instanced { + @Unique private static final int NO_DEFERRED_GLINT = -1; + @Unique private static final byte GLINT_PASSES_UNKNOWN = 0, ONE_GLINT_PASS = 1, SEVERAL_GLINT_PASSES = 2; + + @Unique private int angelica$deferredGlintPass; + @Unique private byte angelica$glintPasses; + + @Surround(method = "doRender(Lnet/minecraft/entity/item/EntityItem;DDDFF)V", id = "glintState") + private void angelica$beginGlintState() { + @Surround.Carry("pass") final int previousPass = angelica$deferredGlintPass; + @Surround.Carry("glintPasses") final byte previousGlintPasses = angelica$glintPasses; + angelica$deferredGlintPass = NO_DEFERRED_GLINT; + angelica$glintPasses = GLINT_PASSES_UNKNOWN; + } + + @Surround.Finally("glintState") + private void angelica$restoreGlintState(@Surround.Carry("pass") int previousPass, @Surround.Carry("glintPasses") byte previousGlintPasses) { + angelica$deferredGlintPass = previousPass; + angelica$glintPasses = previousGlintPasses; + } + @Surround( method = "renderDroppedItem(Lnet/minecraft/entity/item/EntityItem;Lnet/minecraft/util/IIcon;IFFFFI)V", at = @At( @@ -29,9 +55,11 @@ public abstract class MixinRenderItem_Instanced { id = "icon", remap = false ) - private void angelica$instanceDroppedIcon(Tessellator tessellator, float maxU, float minV, float minU, float maxV, int width, int height, float thickness, @Surround.Local ItemStack stack) { + private void angelica$instanceDroppedIcon(Tessellator tessellator, float maxU, float minV, float minU, float maxV, int width, int height, float thickness, + @Surround.Local ItemStack stack, @Surround.Local(argsOnly = true, ordinal = 1) int pass) { @Surround.Carry - final long part = DroppedItemInstancer.icon(stack, false, maxU, minV, minU, maxV, width, height, thickness); + final long part = DroppedItemInstancer.icon(stack, angelica$deferredGlintPass >= 0 && pass > angelica$deferredGlintPass, + maxU, minV, minU, maxV, width, height, thickness); @Surround.Skip final boolean queued = part == DroppedItemInstancer.SKIP; } @@ -41,6 +69,44 @@ public abstract class MixinRenderItem_Instanced { DroppedItemInstancer.endPart(part); } + @Surround( + method = "renderDroppedItem(Lnet/minecraft/entity/item/EntityItem;Lnet/minecraft/util/IIcon;IFFFFI)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;hasEffect(I)Z"), + id = "glintBlock", + remap = false + ) + private void angelica$beginDroppedGlintBlock() {} + + @Surround.Return("glintBlock") + private boolean angelica$queueDroppedGlintBlock(boolean hasEffect, ItemStack stack, int pass, @Surround.Local(argsOnly = true) IIcon icon) { + if (!hasEffect || !ItemGlint.eligible() || !DroppedItemInstancer.canQueueSkippedGlint() || !angelica$oneGlintPass(stack) + || !DroppedItemInstancer.queueSkippedGlint(icon.getIconWidth(), icon.getIconHeight(), EntityMaterials.GLINT)) return hasEffect; + angelica$deferredGlintPass = pass; + SkippedGlintBlock.applyDroppedItemExitState(); + return false; + } + + @Unique + private boolean angelica$oneGlintPass(ItemStack stack) { + // Depends only on the stack, so it holds for every pass and copy of this doRender. + if (angelica$glintPasses == GLINT_PASSES_UNKNOWN) { + angelica$glintPasses = angelica$countOneGlintPass(stack) ? ONE_GLINT_PASS : SEVERAL_GLINT_PASSES; + } + return angelica$glintPasses == ONE_GLINT_PASS; + } + + @Unique + private static boolean angelica$countOneGlintPass(ItemStack stack) { + final Item item = stack.getItem(); + assert item != null; + if (!item.requiresMultipleRenderPasses()) return true; + int glintPasses = 0; + for (int pass = 0, passes = item.getRenderPasses(stack.getItemDamage()); pass < passes; pass++) { + if (stack.hasEffect(pass) && ++glintPasses > 1) return false; + } + return true; + } + @Surround( method = "renderDroppedItem(Lnet/minecraft/entity/item/EntityItem;Lnet/minecraft/util/IIcon;IFFFFI)V", at = { @@ -60,11 +126,13 @@ public abstract class MixinRenderItem_Instanced { id = "glint", remap = false ) - private void angelica$instanceDroppedGlint(Tessellator tessellator, float maxU, float minV, float minU, float maxV, int width, int height, float thickness) { + private void angelica$instanceDroppedGlint(Tessellator tessellator, float maxU, float minV, float minU, float maxV, int width, int height, float thickness, + @Surround.Local ItemStack stack, @Surround.Local(argsOnly = true, ordinal = 1) int pass, @Surround.Local(argsOnly = true) IIcon icon) { @Surround.Carry - final long part = DroppedItemInstancer.glint(maxU, minV, minU, maxV, width, height, thickness, EntityMaterials.GLINT); + final long part = DroppedItemInstancer.glint(maxU, minV, minU, maxV, icon.getIconWidth(), icon.getIconHeight(), thickness, EntityMaterials.GLINT); @Surround.Skip final boolean queued = part == DroppedItemInstancer.SKIP; + if (queued && !ModelPartBatcher.INSTANCE.isShadowPass() && angelica$oneGlintPass(stack)) angelica$deferredGlintPass = pass; } @Surround.Finally("glint") diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterialsTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterialsTest.java index 7c29f9f93..f494ec69e 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterialsTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/EntityMaterialsTest.java @@ -1,6 +1,7 @@ package com.gtnewhorizons.angelica.rendering.tesr; import com.gtnewhorizons.angelica.api.tesr.TesrMaterial; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -9,6 +10,7 @@ import org.lwjgl.opengl.GL11; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -17,6 +19,12 @@ class EntityMaterialsTest { private static final int SA = GL11.GL_SRC_ALPHA; private static final int OMSA = GL11.GL_ONE_MINUS_SRC_ALPHA; + @Test + void armorAndHeldGlintRemainDistinctWhenMaterialsAreInterned() { + assertNotSame(EntityMaterials.GLINT, EntityMaterials.ITEM_GLINT, + "only held glint uses the per-item stencil guard"); + } + private static Arguments row(String name, TesrMaterial expected, boolean textured, boolean blend, Boolean texAnimated, int srcRgb, int dstRgb, boolean alphaTest, int alphaFunc, float alphaRef, int depthFunc, boolean depthMask) { return Arguments.of(name, expected, textured, blend, texAnimated, srcRgb, dstRgb, alphaTest, alphaFunc, alphaRef, depthFunc, depthMask); } diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/TesrLayerStateTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/TesrLayerStateTest.java index c7ef88963..970955449 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/TesrLayerStateTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/tesr/TesrLayerStateTest.java @@ -12,6 +12,7 @@ import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; import com.gtnewhorizons.angelica.glsm.hooks.TextureUtilHooks; import com.gtnewhorizons.angelica.glsm.testutil.Reflect; import com.gtnewhorizons.angelica.glsm.texture.TextureInfoCache; +import com.gtnewhorizons.angelica.rendering.SkippedGlintBlock; import com.gtnewhorizons.angelica.shadercompat.ShaderGlint; import net.coderbot.batchedentityrendering.impl.AngelicaBufferSource; import net.coderbot.batchedentityrendering.impl.TransparencyType; @@ -33,6 +34,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL13; @@ -112,20 +114,23 @@ class TesrLayerStateTest { return RenderLayer.tesr(null, material, PassOverride.NONE, 0f, 0f, ShaderGlint.NO_TINT, DrawState.CULL_BACK, true, true); } - @Test - void batchBracketRestoresCallerDepthAfterGlint() { + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void batchBracketRestoresCallerDepthAfterGlint(boolean held) { for (boolean enabled : new boolean[] { true, false }) { GLStateManager.getDepthTest().setEnabled(enabled); GLStateManager.glDepthFunc(GL11.GL_GREATER); final int depth = GLStateManager.pushState(StateSet.BATCH); final RenderLayer base = layer(EntityMaterials.DROPPED_ITEM_CUTOUT); - final RenderLayer glint = glint(ShaderGlint.NO_TINT); + final RenderLayer glint = layer(held ? EntityMaterials.ITEM_GLINT : EntityMaterials.GLINT); base.startDrawing(); assertDepth(true, GL11.GL_LEQUAL); base.endDrawing(); glint.startDrawing(); assertDepth(true, GL11.GL_EQUAL); + assertSame(SpecialCondition.GLINT, GbufferPrograms.getSpecialCondition()); glint.endDrawing(); + assertNull(GbufferPrograms.getSpecialCondition()); GLStateManager.popStateTo(depth); assertDepth(enabled, GL11.GL_GREATER); } @@ -138,6 +143,22 @@ class TesrLayerStateTest { assertEquals(function, GL11.glGetInteger(GL11.GL_DEPTH_FUNC), "driver depth function"); } + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void skippedDroppedGlintPreservesDepthMaskAndTextureMatrix(boolean depthWrite) { + final Matrix4f textureMatrix = new Matrix4f().translation(0.13f, 0.27f, 0); + GLStateManager.setTextureMatrix(0, textureMatrix); + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glDepthMask(depthWrite); + GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO); + SkippedGlintBlock.applyDroppedItemExitState(); + assertEquals(depthWrite, GLStateManager.isEffectiveDepthMaskEnabled()); + assertEquals(textureMatrix, GLStateManager.getTextures().getTextureUnitMatrix(0)); + assertEquals(GL11.GL_MODELVIEW, GLStateManager.getMatrixMode().getMode()); + assertEquals(GL11.GL_SRC_COLOR, GLStateManager.getBlendState().getSrcAlpha(), "dropped glint blends alpha like color, unlike held"); + assertEquals(GL11.GL_ONE, GLStateManager.getBlendState().getDstAlpha()); + } + @Test void afterGlintLayerDrawsAfterEveryGlintLayer() { final AngelicaBufferSource source = new AngelicaBufferSource();