Added caching for baked vanilla models, fixes #3594.

This commit is contained in:
RainWarrior 2017-01-07 02:58:36 +03:00
parent d2872ec7a2
commit cea922432a
2 changed files with 77 additions and 1 deletions

View file

@ -31,6 +31,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
@ -103,8 +104,12 @@ import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@ -512,7 +517,12 @@ public final class ModelLoader extends ModelBakery
return builder.build();
}
public IBakedModel bake(IModelState state, final VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
public IBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
{
return VanillaLoader.INSTANCE.modelCache.getUnchecked(new BakedModelCacheKey(this, state, format, bakedTextureGetter));
}
public IBakedModel bakeImpl(IModelState state, final VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
{
if(!Attributes.moreSpecific(format, Attributes.DEFAULT_BAKED_FORMAT))
{
@ -829,11 +839,55 @@ public final class ModelLoader extends ModelBakery
return missingModel;
}
protected final class BakedModelCacheKey
{
private final VanillaModelWrapper model;
private final IModelState state;
private final VertexFormat format;
private final Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter;
public BakedModelCacheKey(VanillaModelWrapper model, IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
{
this.model = model;
this.state = state;
this.format = format;
this.bakedTextureGetter = bakedTextureGetter;
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
BakedModelCacheKey that = (BakedModelCacheKey) o;
return Objects.equal(model, that.model) && Objects.equal(state, that.state) && Objects.equal(format, that.format) && Objects.equal(bakedTextureGetter, that.bakedTextureGetter);
}
@Override
public int hashCode()
{
return Objects.hashCode(model, state, format, bakedTextureGetter);
}
}
protected static enum VanillaLoader implements ICustomModelLoader
{
INSTANCE;
private ModelLoader loader;
private LoadingCache<BakedModelCacheKey, IBakedModel> modelCache = CacheBuilder.newBuilder().maximumSize(50).expireAfterWrite(100, TimeUnit.MILLISECONDS).build(new CacheLoader<BakedModelCacheKey, IBakedModel>() {
@Override
public IBakedModel load(BakedModelCacheKey key) throws Exception
{
return key.model.bakeImpl(key.state, key.format, key.bakedTextureGetter);
}
});
void setLoader(ModelLoader loader)
{

View file

@ -19,6 +19,7 @@
package net.minecraftforge.client.model;
import com.google.common.base.Objects;
import net.minecraftforge.common.model.IModelPart;
import net.minecraftforge.common.model.IModelState;
import net.minecraftforge.common.model.TRSRTransformation;
@ -45,4 +46,25 @@ public class ModelStateComposition implements IModelState
}
return f.or(s);
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
ModelStateComposition that = (ModelStateComposition) o;
return Objects.equal(first, that.first) && Objects.equal(second, that.second);
}
@Override
public int hashCode()
{
return Objects.hashCode(first, second);
}
}