diff --git a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index 2a974854..703c858f 100644 --- a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -75,6 +75,7 @@ public class ModernFixEarlyConfig { this.addMixinRule("perf.datapack_reload_exceptions", true); this.addMixinRule("perf.faster_texture_stitching", true); this.addMixinRule("perf.faster_texture_loading", true); + this.addMixinRule("perf.faster_font_loading", true); /* off by default in 1.18 because it doesn't work as well */ this.addMixinRule("perf.faster_singleplayer_load", false); /* Keep this off if JEI/REI isn't installed to prevent breaking vanilla gameplay */ diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/faster_font_loading/LegacyUnicodeBitmapsProviderMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/faster_font_loading/LegacyUnicodeBitmapsProviderMixin.java new file mode 100644 index 00000000..beb327b7 --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/faster_font_loading/LegacyUnicodeBitmapsProviderMixin.java @@ -0,0 +1,64 @@ +package org.embeddedt.modernfix.mixin.perf.faster_font_loading; + +import com.mojang.blaze3d.platform.NativeImage; +import net.minecraft.client.gui.font.providers.LegacyUnicodeBitmapsProvider; +import net.minecraft.resources.ResourceLocation; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +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; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +/** + * Objective: avoid recomputing locations many times, as well as loading all the font sheets in the constructor + * only to do it again later. + */ +@Mixin(LegacyUnicodeBitmapsProvider.class) +public abstract class LegacyUnicodeBitmapsProviderMixin { + @Shadow protected abstract ResourceLocation getSheetLocation(int i); + + @Shadow @Final private Map textures; + private final ResourceLocation[] glyphLocations = new ResourceLocation[256]; + + private ResourceLocation currentCharIdx; + + @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/font/providers/LegacyUnicodeBitmapsProvider;getSheetLocation(I)Lnet/minecraft/resources/ResourceLocation;")) + private ResourceLocation storeCurrentCharIdx(LegacyUnicodeBitmapsProvider provider, int i) { + ResourceLocation location = getSheetLocation(i); + currentCharIdx = location; + return location; + } + + @Redirect(method = "", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/platform/NativeImage;read(Lcom/mojang/blaze3d/platform/NativeImage$Format;Ljava/io/InputStream;)Lcom/mojang/blaze3d/platform/NativeImage;")) + private NativeImage storeLoadedFontSheet(NativeImage.Format format, InputStream stream) throws IOException { + NativeImage image = NativeImage.read(format, stream); + textures.put(currentCharIdx, image); + return image; + } + + @Inject(method = "", at = @At("RETURN")) + private void clearLocation(CallbackInfo ci) { + currentCharIdx = null; + } + + @Inject(method = "getSheetLocation", at = @At("HEAD"), cancellable = true) + private void useCachedLocation(int idx, CallbackInfoReturnable cir) { + int cachedIdx = idx / 256; + if(cachedIdx >= 0 && cachedIdx < glyphLocations.length && glyphLocations[cachedIdx] != null) + cir.setReturnValue(glyphLocations[cachedIdx]); + } + + @Inject(method = "getSheetLocation", at = @At("RETURN")) + private void saveCachedLocation(int idx, CallbackInfoReturnable cir) { + int cachedIdx = idx / 256; + if(cachedIdx >= 0 && cachedIdx < glyphLocations.length) + glyphLocations[cachedIdx] = cir.getReturnValue(); + } +} diff --git a/src/main/resources/modernfix.mixins.json b/src/main/resources/modernfix.mixins.json index 4f2fc6e1..807b91d2 100644 --- a/src/main/resources/modernfix.mixins.json +++ b/src/main/resources/modernfix.mixins.json @@ -79,6 +79,7 @@ "perf.cache_model_materials.MultipartMixin", "perf.faster_texture_stitching.StitcherMixin", "perf.faster_texture_loading.TextureAtlasMixin", + "perf.faster_font_loading.LegacyUnicodeBitmapsProviderMixin", "perf.skip_first_datapack_reload.CreateWorldScreenMixin", "devenv.MinecraftMixin", "devenv.NarratorMixin"