From d0598055c0adf41504d56df6c0b960c1ff513be4 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Wed, 29 May 2024 20:47:59 -0400 Subject: [PATCH] Do not load models for all of a block's blockstates at once --- .../BlockStateModelLoaderMixin.java | 24 ++++++++++++++++--- .../dynamic_resources/ModelBakeryMixin.java | 14 +++++++++-- .../duck/IBlockStateModelLoader.java | 4 ++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java index 76d3e406..b681d0c1 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java @@ -1,9 +1,11 @@ package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; +import com.google.common.collect.ImmutableList; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntMaps; import net.minecraft.client.color.block.BlockColors; import net.minecraft.client.resources.model.BlockStateModelLoader; +import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.core.DefaultedRegistry; import net.minecraft.core.registries.BuiltInRegistries; @@ -12,8 +14,10 @@ import net.minecraft.util.profiling.ProfilerFiller; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; +import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.duck.IBlockStateModelLoader; +import org.embeddedt.modernfix.dynamicresources.ModelBakeryHelpers; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -35,16 +39,25 @@ public abstract class BlockStateModelLoaderMixin implements IBlockStateModelLoad @Shadow @Mutable @Final private Object2IntMap modelGroups; + private ImmutableList filteredStates; + @Inject(method = "", at = @At("RETURN")) private void makeModelGroupsSynchronized(Map map, ProfilerFiller profilerFiller, UnbakedModel unbakedModel, BlockColors blockColors, BiConsumer biConsumer, CallbackInfo ci) { this.modelGroups = Object2IntMaps.synchronize(this.modelGroups); } @Override - public void loadSpecificBlock(ResourceLocation location) { - var optionalBlock = BuiltInRegistries.BLOCK.getOptional(location); + public void loadSpecificBlock(ModelResourceLocation location) { + var optionalBlock = BuiltInRegistries.BLOCK.getOptional(location.id()); if(optionalBlock.isPresent()) { - this.loadBlockStateDefinitions(location, optionalBlock.get().getStateDefinition()); + try { + filteredStates = ModelBakeryHelpers.getBlockStatesForMRL(optionalBlock.get().getStateDefinition(), location); + } catch(RuntimeException e) { + ModernFix.LOGGER.error("Exception filtering states on {}", location, e); + filteredStates = null; + } + this.loadBlockStateDefinitions(location.id(), optionalBlock.get().getStateDefinition()); + filteredStates = null; } } @@ -52,4 +65,9 @@ public abstract class BlockStateModelLoaderMixin implements IBlockStateModelLoad private Iterator skipIteratingBlocks(DefaultedRegistry instance) { return Collections.emptyIterator(); } + + @Redirect(method = "loadBlockStateDefinitions", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/StateDefinition;getPossibleStates()Lcom/google/common/collect/ImmutableList;")) + private ImmutableList getFilteredStates(StateDefinition instance) { + return this.filteredStates != null ? this.filteredStates : instance.getPossibleStates(); + } } diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelBakeryMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelBakeryMixin.java index b3c48f73..badcc2dd 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelBakeryMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelBakeryMixin.java @@ -53,6 +53,9 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery { @Shadow @Final private UnbakedModel missingModel; + @Unique + private static final boolean DEBUG_MODEL_LOADS = Boolean.getBoolean("modernfix.debugDynamicModelLoading"); + /** * Bake a model using the provided texture getter and location. The model is stored in {@link ModelBakeryMixin#bakedTopLevelModels}. */ @@ -74,10 +77,14 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery { private UnbakedModel loadUnbakedModelDynamic(ModelResourceLocation location) { if(location.equals(MISSING_MODEL_VARIANT)) { return missingModel; - } else if(location.variant().equals("inventory")) { + } + if(DEBUG_MODEL_LOADS) { + ModernFix.LOGGER.info("Loading model {}", location); + } + if(location.variant().equals("inventory")) { this.loadItemModelAndDependencies(location.id()); } else { - ((IBlockStateModelLoader)dynamicLoader).loadSpecificBlock(location.id()); + ((IBlockStateModelLoader)dynamicLoader).loadSpecificBlock(location); } return this.topLevelModels.getOrDefault(location, this.missingModel); } @@ -94,6 +101,9 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery { if(model == null) { UnbakedModel prototype = loadUnbakedModelDynamic(location); prototype.resolveParents(this::getModel); + if(DEBUG_MODEL_LOADS) { + ModernFix.LOGGER.info("Baking model {}", location); + } this.method_61072(this.textureGetter, location, prototype); model = bakedTopLevelModels.remove(location); if(model == null) { diff --git a/common/src/main/java/org/embeddedt/modernfix/duck/IBlockStateModelLoader.java b/common/src/main/java/org/embeddedt/modernfix/duck/IBlockStateModelLoader.java index 3d208ac6..87c96c66 100644 --- a/common/src/main/java/org/embeddedt/modernfix/duck/IBlockStateModelLoader.java +++ b/common/src/main/java/org/embeddedt/modernfix/duck/IBlockStateModelLoader.java @@ -1,7 +1,7 @@ package org.embeddedt.modernfix.duck; -import net.minecraft.resources.ResourceLocation; +import net.minecraft.client.resources.model.ModelResourceLocation; public interface IBlockStateModelLoader { - void loadSpecificBlock(ResourceLocation location); + void loadSpecificBlock(ModelResourceLocation location); }