Speed up FontManager loading

This commit is contained in:
embeddedt 2023-04-27 12:03:38 -04:00
parent 934da3660f
commit c861c99c79
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
3 changed files with 66 additions and 0 deletions

View File

@ -89,6 +89,7 @@ public class ModernFixEarlyConfig {
this.addMixinRule("perf.async_locator", true);
this.addMixinRule("perf.faster_texture_stitching", true);
this.addMixinRule("perf.faster_texture_loading", true);
this.addMixinRule("perf.faster_font_loading", true);
this.addMixinRule("perf.kubejs", modPresent("kubejs"));
this.addMixinRule("perf.faster_singleplayer_load", false);
/* Keep this off if JEI isn't installed to prevent breaking vanilla gameplay */

View File

@ -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<ResourceLocation, NativeImage> textures;
private final ResourceLocation[] glyphLocations = new ResourceLocation[256];
private ResourceLocation currentCharIdx;
@Redirect(method = "<init>", 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 = "<init>", 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 = "<init>", at = @At("RETURN"))
private void clearLocation(CallbackInfo ci) {
currentCharIdx = null;
}
@Inject(method = "getSheetLocation", at = @At("HEAD"), cancellable = true)
private void useCachedLocation(int idx, CallbackInfoReturnable<ResourceLocation> 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<ResourceLocation> cir) {
int cachedIdx = idx / 256;
if(cachedIdx >= 0 && cachedIdx < glyphLocations.length)
glyphLocations[cachedIdx] = cir.getReturnValue();
}
}

View File

@ -113,6 +113,7 @@
"perf.cache_model_materials.MultipartMixin",
"perf.faster_texture_stitching.StitcherMixin",
"perf.faster_texture_loading.TextureAtlasMixin",
"perf.faster_font_loading.LegacyUnicodeBitmapsProviderMixin",
"bugfix.packet_leak.ClientPlayNetHandlerMixin",
"bugfix.packet_leak.SCustomPayloadPlayPacketMixin",
"perf.reuse_datapacks.MinecraftMixin",