Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizes player heads by adding a cache #2348

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private MixinConfig() {
this.addMixinRule("features.render.model", true);
this.addMixinRule("features.render.model.block", true);
this.addMixinRule("features.render.model.item", true);
this.addMixinRule("features.render.model.player_head", true);

this.addMixinRule("features.render.particle", true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package net.caffeinemc.mods.sodium.mixin.features.render.model.player_head;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Maps;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.PropertyMap;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.SkullBlockRenderer;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.SkinManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.SkullBlock;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.*;

import java.time.Duration;
import java.util.Map;

@Mixin(SkullBlockRenderer.class)
public class SkullBlockRendererMixin {

@Shadow
@Final
private static Map<SkullBlock.Type, ResourceLocation> SKIN_BY_TYPE;
@Unique
private static final Map<SkullBlock.Type, RenderType> SKULLS_RENDER_TYPE = Util.make(Maps.newHashMap(), (hashMap) -> {
for (SkullBlock.Type type : SKIN_BY_TYPE.keySet()) {
hashMap.put(type, RenderType.entityCutoutNoCullZOffset(SKIN_BY_TYPE.get(type)));
}
});

// Use the PropertyMap as the key instead of the UUID since there can be many skins with the same UUID,
// for example, a head with a previous skin and the current one from the same player.
// Instead, in the PropertyMap, the skin's URL is stored.
@Unique
private static final Cache<PropertyMap, RenderType> RENDER_TYPE_CACHE = CacheBuilder.newBuilder()
.expireAfterAccess(Duration.ofMillis(500L)).build();

/**
* @author Zailer43
* @reason Adds cache to the RenderType as it is CPU expensive
* and minecraft gets it all the time inside the render method
*/
@Overwrite
public static RenderType getRenderType(SkullBlock.Type type, @Nullable GameProfile profile) {
if (type == SkullBlock.Types.PLAYER && profile != null) {
PropertyMap profileProperties = profile.getProperties();
RenderType renderType = RENDER_TYPE_CACHE.getIfPresent(profileProperties);
if (renderType != null) {
return renderType;
}

SkinManager skinManager = Minecraft.getInstance().getSkinManager();
ResourceLocation skin = skinManager.getInsecureSkin(profile).texture();
renderType = RenderType.entityTranslucent(skin);

// before the skin is loaded a default skin is used, we do not want to cache that
if (skin != DefaultPlayerSkin.get(profile).texture()) {
RENDER_TYPE_CACHE.put(profileProperties, renderType);
}

return renderType;
}

return SKULLS_RENDER_TYPE.get(type);
}

}
1 change: 1 addition & 0 deletions src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"features.render.model.ItemBlockRenderTypesMixin",
"features.render.model.block.ModelBlockRendererMixin",
"features.render.model.item.ItemRendererMixin",
"features.render.model.player_head.SkullBlockRendererMixin",
"features.render.particle.SingleQuadParticleMixin",
"features.render.world.clouds.LevelRendererMixin",
"features.render.world.sky.FogRendererMixin",
Expand Down