From 07ce020247568dc7a9f171a0c29f988e9299e230 Mon Sep 17 00:00:00 2001 From: JellySquid Date: Sun, 29 Dec 2024 17:07:50 -0600 Subject: [PATCH] Do not apply optimizations to sprites with special tickers --- .../chunk/compile/pipeline/BlockRenderer.java | 19 ++++++++--- .../pipeline/TextureAtlasSpriteExtension.java | 5 +++ .../scan/TextureAtlasSpriteMixin.java | 33 +++++++++++++++++++ .../main/resources/sodium-common.mixins.json | 1 + 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/TextureAtlasSpriteExtension.java create mode 100644 common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/textures/scan/TextureAtlasSpriteMixin.java diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/BlockRenderer.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/BlockRenderer.java index e042e129d7..911525ba7c 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/BlockRenderer.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/BlockRenderer.java @@ -257,14 +257,23 @@ private boolean validateQuadUVs(TextureAtlasSprite atlasSprite) { } private static TerrainRenderPass getDowngradedPass(TextureAtlasSprite sprite, TerrainRenderPass pass) { - if (sprite.contents() instanceof SpriteContentsExtension contents) { - if (pass == DefaultTerrainRenderPasses.TRANSLUCENT && !contents.sodium$hasTranslucentPixels()) { - pass = DefaultTerrainRenderPasses.CUTOUT; + if (sprite instanceof TextureAtlasSpriteExtension spriteExt) { + // Some mods may use a custom ticker which we cannot look into. To avoid problems with these mods, + // do not attempt to downgrade the render pass. + if (spriteExt.sodium$hasUnknownImageContents()) { + return pass; } - if (pass == DefaultTerrainRenderPasses.CUTOUT && !contents.sodium$hasTransparentPixels()) { - pass = DefaultTerrainRenderPasses.SOLID; + + if (sprite.contents() instanceof SpriteContentsExtension contentsExt) { + if (pass == DefaultTerrainRenderPasses.TRANSLUCENT && !contentsExt.sodium$hasTranslucentPixels()) { + pass = DefaultTerrainRenderPasses.CUTOUT; + } + if (pass == DefaultTerrainRenderPasses.CUTOUT && !contentsExt.sodium$hasTransparentPixels()) { + pass = DefaultTerrainRenderPasses.SOLID; + } } } + return pass; } } \ No newline at end of file diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/TextureAtlasSpriteExtension.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/TextureAtlasSpriteExtension.java new file mode 100644 index 0000000000..462de88a24 --- /dev/null +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/TextureAtlasSpriteExtension.java @@ -0,0 +1,5 @@ +package net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline; + +public interface TextureAtlasSpriteExtension { + boolean sodium$hasUnknownImageContents(); +} diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/textures/scan/TextureAtlasSpriteMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/textures/scan/TextureAtlasSpriteMixin.java new file mode 100644 index 0000000000..521c67bc60 --- /dev/null +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/textures/scan/TextureAtlasSpriteMixin.java @@ -0,0 +1,33 @@ +package net.caffeinemc.mods.sodium.mixin.features.textures.scan; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.TextureAtlasSpriteExtension; +import net.minecraft.client.renderer.texture.SpriteContents; +import net.minecraft.client.renderer.texture.SpriteTicker; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(TextureAtlasSprite.class) +public class TextureAtlasSpriteMixin implements TextureAtlasSpriteExtension { + @Unique + private boolean hasUnknownImageContents; + + @WrapOperation(method = "createTicker", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/SpriteContents;createTicker()Lnet/minecraft/client/renderer/texture/SpriteTicker;")) + private SpriteTicker hookTickerInstantiation(SpriteContents instance, Operation original) { + var ticker = original.call(instance); + + if (ticker != null && !(ticker instanceof SpriteContents.Ticker)) { + this.hasUnknownImageContents = true; + } + + return ticker; + } + + @Override + public boolean sodium$hasUnknownImageContents() { + return this.hasUnknownImageContents; + } +} diff --git a/common/src/main/resources/sodium-common.mixins.json b/common/src/main/resources/sodium-common.mixins.json index 59d85915e3..5261c22829 100644 --- a/common/src/main/resources/sodium-common.mixins.json +++ b/common/src/main/resources/sodium-common.mixins.json @@ -87,6 +87,7 @@ "features.textures.mipmaps.MipmapGeneratorMixin", "features.textures.mipmaps.SpriteContentsMixin", "features.textures.scan.SpriteContentsMixin", + "features.textures.scan.TextureAtlasSpriteMixin", "workarounds.context_creation.WindowMixin", "workarounds.event_loop.RenderSystemMixin" ]