Create new deferred overlay system to fix z-fighting issues (#1605)

This commit is contained in:
Eclipse-Sol authored and GitHub committed 2026-03-30 15:50:18 -07:00
1 parent 2645c803f9
commit d3022a40d2
8 files changed
+414 -7

No files matched your search

@@ -37,9 +37,13 @@ public enum Mixins implements IMixins {
, "angelica.MixinMinecraft_FrameHook"
, "angelica.MixinMinecraftServer"
, "angelica.bugfixes.MixinItemRenderer_EdgeDepth"
, "angelica.bugfixes.MixinModelCreeper_AuraBodyInflate"
, "angelica.bugfixes.MixinModelWither_ArmorCentering"
, "angelica.bugfixes.MixinRenderBlocks_CrossedSquaresNormal"
, "angelica.bugfixes.MixinRenderCreeper_AuraDepth"
, "angelica.bugfixes.MixinRenderGlobal_DeferredEntityOverlay"
, "angelica.bugfixes.MixinRenderGlobal_DestroyBlock"
, "angelica.bugfixes.MixinRendererLivingEntity_DeferredEntityOverlay"
, "angelica.bugfixes.MixinRenderWither_ArmorCentering"
, "angelica.bugfixes.MixinRendererLivingEntity_EyeDepth"
, "angelica.debug.MixinMinecraft_FPSCap"
@@ -0,0 +1,217 @@
package com.gtnewhorizons.angelica.rendering;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import lombok.Getter;
import net.coderbot.iris.layer.GbufferPrograms;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.EntityIdHelper;
import net.coderbot.iris.uniforms.ItemIdManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.entity.RendererLivingEntity;
import net.minecraft.entity.EntityLivingBase;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Defers entity overlay rendering (auras, armor effects, etc.) to after all entities have been drawn.
* This lets us fix z-fighting between overlay's own coplanar faces by disabling the depth mask,
* and ensures the overlay composites correctly on top of all opaque geometry.
*
* Used by both the charged creeper aura and the Wither armor overlay.
*
* The deferred render replays shouldRenderPass on the original renderer instance so that any
* mod mixins targeting that method still fire during the deferred pass.
*/
public class DeferredEntityOverlay {
@FunctionalInterface
public interface ShouldRenderPassFn {
int invoke(EntityLivingBase entity, int pass, float partialTick);
}
private static final int INITIAL_CAPACITY = 4;
private static final List<DeferredEntry> deferred = new ArrayList<>(INITIAL_CAPACITY);
private static final List<DeferredEntry> pool = new ArrayList<>(INITIAL_CAPACITY);
private static final FloatBuffer MATRIX_BUF = BufferUtils.createFloatBuffer(16);
// Set by the shouldRenderPass inject, consumed by the doRender WrapOperation.
@Getter
private static boolean overlayPassActive = false;
// Set during deferred flush to prevent re-deferring when shouldRenderPass is replayed.
@Getter
private static boolean replaying = false;
// Pending context from the shouldRenderPass inject, consumed by deferRender.
private static ShouldRenderPassFn pendingShouldRenderPass;
private static RendererLivingEntity pendingRenderer;
private static EntityLivingBase pendingEntity;
private static float pendingPartialTick;
/**
* Called from shouldRenderPass HEAD injects. Marks that the upcoming renderPassModel.render()
* should be deferred. If a previous mark was not consumed (shouldRenderPass returned <= 0,
* so render() was never called).
*/
public static void markOverlayPass(ShouldRenderPassFn shouldRenderPass, RendererLivingEntity renderer,
EntityLivingBase entity, float partialTick) {
overlayPassActive = true;
pendingShouldRenderPass = shouldRenderPass;
pendingRenderer = renderer;
pendingEntity = entity;
pendingPartialTick = partialTick;
}
public static void clearStaleOverlayFlag() {
overlayPassActive = false;
}
/**
* Called from the WrapOperation on renderPassModel.render() in doRender.
* Captures the modelview matrix and animation params, skipping the actual render.
*/
public static void deferRender(float limbSwing, float limbSwingAmount, float ageInTicks,
float headYaw, float headPitch, float scale) {
overlayPassActive = false;
DeferredEntry entry = acquireEntry();
entry.set(pendingShouldRenderPass, pendingRenderer, pendingEntity, pendingPartialTick,
limbSwing, limbSwingAmount, ageInTicks, headYaw, headPitch, scale);
MATRIX_BUF.clear();
GLStateManager.glGetFloat(GL11.GL_MODELVIEW_MATRIX, MATRIX_BUF);
MATRIX_BUF.get(entry.matrix);
deferred.add(entry);
pendingShouldRenderPass = null;
pendingRenderer = null;
pendingEntity = null;
}
private static DeferredEntry acquireEntry() {
int size = pool.size();
if (size > 0) {
return pool.remove(size - 1);
}
return new DeferredEntry();
}
private static void recycle() {
for (DeferredEntry entry : deferred) {
entry.shouldRenderPass = null;
entry.renderer = null;
entry.entity = null;
pool.add(entry);
}
deferred.clear();
}
public static void clear() {
recycle();
overlayPassActive = false;
replaying = false;
}
public static void renderAll() {
if (deferred.isEmpty()) return;
GbufferPrograms.beginEntities();
try {
for (DeferredEntry entry : deferred) {
CapturedRenderingState.INSTANCE.setCurrentEntity(EntityIdHelper.getEntityId(entry.entity));
ItemIdManager.resetItemId();
renderOverlay(entry);
}
} finally {
CapturedRenderingState.INSTANCE.setCurrentEntity(-1);
GbufferPrograms.endEntities();
}
recycle();
}
private static void renderOverlay(DeferredEntry entry) {
EntityLivingBase entity = entry.entity;
// Restore the entity's lightmap coords
int brightness = entity.getBrightnessForRender(entry.partialTick);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit,
brightness % 65536, (float) brightness / 65536);
// Restore the saved modelview matrix
GLStateManager.glMatrixMode(GL11.GL_MODELVIEW);
GLStateManager.glPushMatrix();
boolean depthMaskDisabled = false;
try {
MATRIX_BUF.clear();
MATRIX_BUF.put(entry.matrix);
MATRIX_BUF.flip();
GLStateManager.glLoadMatrix(MATRIX_BUF);
// Replay shouldRenderPass pass 1
replaying = true;
int result = entry.shouldRenderPass.invoke(entity, 1, entry.partialTick);
if (result > 0) {
// Disable depth writes so coplanar faces don't z-fight each other
GLStateManager.glDepthMask(false);
depthMaskDisabled = true;
// Disable backface culling so both sides of the overlay shell are visible
GLStateManager.glDisable(GL11.GL_CULL_FACE);
// Render using the model set by shouldRenderPass
entry.renderer.renderPassModel.setLivingAnimations(entity,
entry.limbSwing, entry.limbSwingAmount, entry.partialTick);
entry.renderer.renderPassModel.render(entity,
entry.limbSwing, entry.limbSwingAmount, entry.ageInTicks,
entry.headYaw, entry.headPitch, entry.scale);
}
// Replay shouldRenderPass pass 2
entry.shouldRenderPass.invoke(entity, 2, entry.partialTick);
} finally {
replaying = false;
if (depthMaskDisabled) {
GLStateManager.glDepthMask(true);
GLStateManager.glEnable(GL11.GL_CULL_FACE);
}
GLStateManager.glPopMatrix();
}
}
private static class DeferredEntry {
final float[] matrix = new float[16];
ShouldRenderPassFn shouldRenderPass;
RendererLivingEntity renderer;
EntityLivingBase entity;
float partialTick;
float limbSwing;
float limbSwingAmount;
float ageInTicks;
float headYaw;
float headPitch;
float scale;
void set(ShouldRenderPassFn shouldRenderPass, RendererLivingEntity renderer,
EntityLivingBase entity, float partialTick,
float limbSwing, float limbSwingAmount, float ageInTicks,
float headYaw, float headPitch, float scale) {
this.shouldRenderPass = shouldRenderPass;
this.renderer = renderer;
this.entity = entity;
this.partialTick = partialTick;
this.limbSwing = limbSwing;
this.limbSwingAmount = limbSwingAmount;
this.ageInTicks = ageInTicks;
this.headYaw = headYaw;
this.headPitch = headPitch;
this.scale = scale;
}
}
}
@@ -0,0 +1,26 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.bugfixes;
import net.minecraft.client.model.ModelCreeper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
@Mixin(ModelCreeper.class)
public class MixinModelCreeper_AuraBodyInflate {
/**
* Reduce the body inflate by 2^20 ULPs so the aura body doesn't intersect the base model's head.
* Only applies when inflate > 0 (the aura model), leaving the base model untouched.
* The body addBox is the 3rd call (ordinal 2) in the constructor, and inflate is parameter index 6.
*/
@ModifyArg(
method = "<init>(F)V", at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/model/ModelRenderer;addBox(FFFIIIF)V", ordinal = 2), index = 6
)
private float angelica$shrinkAuraBody(float inflate) {
if (inflate > 0.0F) {
return Float.intBitsToFloat(Float.floatToIntBits(inflate) - 1048576);
}
return inflate;
}
}
@@ -5,6 +5,7 @@ import net.minecraft.client.model.ModelRenderer;
import net.minecraft.client.model.ModelWither;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -26,8 +27,17 @@ public class MixinModelWither_ArmorCentering {
// Heads: [0] center, [1] left, [2] right
@Shadow private ModelRenderer[] field_82904_b;
@Unique
private static final float ARMOR_INFLATE = 0.5F;
// Ribs armor size is reduced by 2^22 ULPs to sit just inside the base spine, preventing z-fighting.
@Unique
private static final float NO_RIBS_Z_INFLATE = Float.intBitsToFloat(Float.floatToIntBits(ARMOR_INFLATE) - 4194304);
// Side head armor is inflated by 2^22 ULPs to make the armor sit above the base shoulder, preventing z-fighting.
@Unique
private static final float NO_HEADS_Z_INFLATE = Float.intBitsToFloat(Float.floatToIntBits(ARMOR_INFLATE) + 4194304);
// If the armor inflate flag is set, rebuild all boxes with uniform inflate.
@Inject(method = "<init>", at = @At("TAIL"))
private void angelica$applyInflate(CallbackInfo ci) {
@@ -44,9 +54,9 @@ public class MixinModelWither_ArmorCentering {
field_82905_a[1] = (new ModelRenderer(self)).setTextureSize(self.textureWidth, self.textureHeight);
field_82905_a[1].setRotationPoint(-2.0F, 6.9F, -0.5F);
field_82905_a[1].setTextureOffset(0, 22).addBox(0.0F, 0.0F, 0.0F, 3, 10, 3, ARMOR_INFLATE);
field_82905_a[1].setTextureOffset(24, 22).addBox(-4.0F, 1.5F, 0.5F, 11, 2, 2, ARMOR_INFLATE);
field_82905_a[1].setTextureOffset(24, 22).addBox(-4.0F, 4.0F, 0.5F, 11, 2, 2, ARMOR_INFLATE);
field_82905_a[1].setTextureOffset(24, 22).addBox(-4.0F, 6.5F, 0.5F, 11, 2, 2, ARMOR_INFLATE);
field_82905_a[1].setTextureOffset(24, 22).addBox(-4.0F, 1.5F, 0.5F, 11, 2, 2, NO_RIBS_Z_INFLATE);
field_82905_a[1].setTextureOffset(24, 22).addBox(-4.0F, 4.0F, 0.5F, 11, 2, 2, NO_RIBS_Z_INFLATE);
field_82905_a[1].setTextureOffset(24, 22).addBox(-4.0F, 6.5F, 0.5F, 11, 2, 2, NO_RIBS_Z_INFLATE);
// [2] Tail
field_82905_a[2] = new ModelRenderer(self, 12, 22);
@@ -58,13 +68,13 @@ public class MixinModelWither_ArmorCentering {
// [1] Left head
field_82904_b[1] = new ModelRenderer(self, 32, 0);
field_82904_b[1].addBox(-4.0F, -4.0F, -4.0F, 6, 6, 6, ARMOR_INFLATE);
field_82904_b[1].addBox(-4.0F, -4.0F, -4.0F, 6, 6, 6, NO_HEADS_Z_INFLATE);
field_82904_b[1].rotationPointX = -8.0F;
field_82904_b[1].rotationPointY = 4.0F;
// [2] Right head
field_82904_b[2] = new ModelRenderer(self, 32, 0);
field_82904_b[2].addBox(-4.0F, -4.0F, -4.0F, 6, 6, 6, ARMOR_INFLATE);
field_82904_b[2].addBox(-4.0F, -4.0F, -4.0F, 6, 6, 6, NO_HEADS_Z_INFLATE);
field_82904_b[2].rotationPointX = 10.0F;
field_82904_b[2].rotationPointY = 4.0F;
}
@@ -0,0 +1,43 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.bugfixes;
import com.gtnewhorizons.angelica.rendering.DeferredEntityOverlay;
import net.minecraft.client.renderer.entity.RenderCreeper;
import net.minecraft.client.renderer.entity.RendererLivingEntity;
import net.minecraft.entity.monster.EntityCreeper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(RenderCreeper.class)
public abstract class MixinRenderCreeper_AuraDepth {
@Shadow
protected abstract int shouldRenderPass(EntityCreeper entity, int pass, float partialTick);
@Unique
private DeferredEntityOverlay.ShouldRenderPassFn angelica$cachedPassFn;
/**
* Mark that a charged creeper aura pass is about to execute. The method runs normally
* so any mixins added by mods within shouldRenderPass still fire. The flag is consumed by the
* WrapOperation on renderPassModel.render() in doRender, which defers the actual render.
* See MixinRendererLivingEntity_DeferredEntityOverlay
*/
@Inject(method = "shouldRenderPass(Lnet/minecraft/entity/monster/EntityCreeper;IF)I",
at = @At("HEAD"))
private void angelica$markOverlayPass(EntityCreeper creeper, int pass, float partialTick, CallbackInfoReturnable<Integer> cir) {
if (pass == 1 && creeper.getPowered() && !DeferredEntityOverlay.isReplaying()) {
if (angelica$cachedPassFn == null) {
angelica$cachedPassFn = (entity, pass2, tick) -> this.shouldRenderPass((EntityCreeper) entity, pass2, tick);
}
DeferredEntityOverlay.markOverlayPass(
angelica$cachedPassFn,
(RendererLivingEntity) (Object) this,
creeper, partialTick
);
}
}
}
@@ -0,0 +1,30 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.bugfixes;
import com.gtnewhorizons.angelica.rendering.DeferredEntityOverlay;
import net.minecraft.client.renderer.RenderGlobal;
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(RenderGlobal.class)
public class MixinRenderGlobal_DeferredEntityOverlay {
/**
* Clear stale deferred entries before collecting new ones.
*/
@Inject(method = "renderEntities", at = @At("HEAD"))
private void angelica$clearStaleDeferredOverlays(CallbackInfo ci) {
DeferredEntityOverlay.clear();
}
/**
* Render deferred entity overlays after all entities have been drawn,
* so additive-blended overlays composite correctly without depth issues.
*/
@Inject(method = "renderEntities", at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/renderer/RenderHelper;enableStandardItemLighting()V"))
private void angelica$renderDeferredOverlays(CallbackInfo ci) {
DeferredEntityOverlay.renderAll();
}
}
@@ -1,15 +1,20 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.bugfixes;
import com.gtnewhorizons.angelica.rendering.DeferredEntityOverlay;
import com.gtnewhorizons.angelica.rendering.WitherArmorState;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelWither;
import net.minecraft.client.renderer.entity.RenderWither;
import net.minecraft.client.renderer.entity.RendererLivingEntity;
import net.minecraft.entity.boss.EntityWither;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/**
* Replaces the Wither's glScalef(1.1) armor approach with a per-box inflated model.
@@ -17,11 +22,17 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
* Not exactly mod friendly but we can figure that out later.
*/
@Mixin(RenderWither.class)
public class MixinRenderWither_ArmorCentering {
public abstract class MixinRenderWither_ArmorCentering {
@Shadow
protected abstract int shouldRenderPass(EntityWither entity, int pass, float partialTick);
@Unique
private ModelWither angelica$armorModel;
@Unique
private DeferredEntityOverlay.ShouldRenderPassFn angelica$cachedPassFn;
@Inject(method = "<init>", at = @At("TAIL"))
private void angelica$createArmorModel(CallbackInfo ci) {
WitherArmorState.pendingInflate = true;
@@ -38,7 +49,7 @@ public class MixinRenderWither_ArmorCentering {
((RenderWither) (Object) this).setRenderPassModel(angelica$armorModel);
}
// No-op glTranslatef
// No-op the model offset translate (ordinal 1)
@Redirect(
method = "shouldRenderPass(Lnet/minecraft/entity/boss/EntityWither;IF)I",
at = @At(value = "INVOKE",
@@ -55,4 +66,20 @@ public class MixinRenderWither_ArmorCentering {
)
private void angelica$skipScale(float x, float y, float z) {
}
// Mark the Wither's armor pass for deferred rendering.
@Inject(method = "shouldRenderPass(Lnet/minecraft/entity/boss/EntityWither;IF)I",
at = @At("HEAD"))
private void angelica$markArmorPass(EntityWither wither, int pass, float partialTick, CallbackInfoReturnable<Integer> cir) {
if (pass == 1 && wither.isArmored() && !DeferredEntityOverlay.isReplaying()) {
if (angelica$cachedPassFn == null) {
angelica$cachedPassFn = (entity, pass2, tick) -> this.shouldRenderPass((EntityWither) entity, pass2, tick);
}
DeferredEntityOverlay.markOverlayPass(
angelica$cachedPassFn,
(RendererLivingEntity) (Object) this,
wither, partialTick
);
}
}
}
@@ -0,0 +1,50 @@
package com.gtnewhorizons.angelica.mixins.early.angelica.bugfixes;
import com.gtnewhorizons.angelica.rendering.DeferredEntityOverlay;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.entity.RendererLivingEntity;
import net.minecraft.entity.Entity;
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(value = RendererLivingEntity.class, priority = 1200)
public class MixinRendererLivingEntity_DeferredEntityOverlay {
/**
* Intercepts the first renderPassModel.render() call in the shouldRenderPass loop.
* When an entity overlay pass is active, captures the animation params and modelview
* matrix for deferred rendering, skipping the immediate render.
*/
@WrapOperation(
method = "doRender(Lnet/minecraft/entity/EntityLivingBase;DDDFF)V", at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/model/ModelBase;render(Lnet/minecraft/entity/Entity;FFFFFF)V", ordinal = 0)
)
private void angelica$maybeDeferOverlay(ModelBase model, Entity entity,
float limbSwing, float limbSwingAmount, float ageInTicks,
float headYaw, float headPitch, float scale,
Operation<Void> original) {
if (DeferredEntityOverlay.isOverlayPassActive()) {
DeferredEntityOverlay.deferRender(limbSwing, limbSwingAmount, ageInTicks, headYaw, headPitch, scale);
} else {
original.call(model, entity, limbSwing, limbSwingAmount, ageInTicks, headYaw, headPitch, scale);
}
}
/**
* Clear a stale overlayPassActive flag after the shouldRenderPass loop.
* Handles the case where markOverlayPass fired but shouldRenderPass returned <= 0,
* so renderPassModel.render() was never called and deferRender never consumed the flag.
*/
@Inject(
method = "doRender(Lnet/minecraft/entity/EntityLivingBase;DDDFF)V",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/renderer/entity/RendererLivingEntity;renderEquippedItems(Lnet/minecraft/entity/EntityLivingBase;F)V")
)
private void angelica$clearStaleOverlayFlag(CallbackInfo ci) {
DeferredEntityOverlay.clearStaleOverlayFlag();
}
}