Skip to content

Commit

Permalink
optimize transformations (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: ev chang <[email protected]>
  • Loading branch information
MicrocontrollersDev and Wyvest authored Jul 16, 2024
1 parent 58ac567 commit 397b146
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ This work, "PolyPatcher", is adapted from ["Patcher"](https://sk1er.club/mods/pa
- Boost performance by optimizing adding normals to vertex formats
- Boost performance by unloading tile entities quickly
- Improve speed when changing language, mipmap level, and anisotropic filtering level
- Reduce memory usage of model transformations
- Fix Forge held item lighting to match vanilla
- Fix vanilla bug where entering an entity in spectator mode while in third person applies shaders
- Fix vanilla bug where enchantment glint takes up the whole slot
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/club/sk1er/patcher/hooks/TRSRTransformationHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package club.sk1er.patcher.hooks;

import java.util.EnumMap;
import java.util.Map;

import net.minecraft.client.renderer.block.model.ItemTransformVec3f;
import net.minecraft.client.resources.model.ModelRotation;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.TRSRTransformation;

import static net.minecraftforge.client.model.TRSRTransformation.identity;

public class TRSRTransformationHook {
@SuppressWarnings("deprecation")
public static TRSRTransformation from(ItemTransformVec3f transform) {
return transform.equals(ItemTransformVec3f.DEFAULT) ? identity() : new TRSRTransformation(transform);
}

public static TRSRTransformation from(ModelRotation rotation) {
return Cache.get(rotation);
}

public static TRSRTransformation from(EnumFacing facing) {
return Cache.get(getRotation(facing));
}

public static ModelRotation getRotation(EnumFacing facing) {
switch (facing) {
case DOWN: return ModelRotation.X90_Y0;
case UP: return ModelRotation.X270_Y0;
case NORTH: return ModelRotation.X0_Y0;
case SOUTH: return ModelRotation.X0_Y180;
case WEST: return ModelRotation.X0_Y270;
case EAST: return ModelRotation.X0_Y90;
}
throw new IllegalArgumentException(String.valueOf(facing));
}

private static final class Cache {
private static final Map<ModelRotation, TRSRTransformation> rotations = new EnumMap<>(ModelRotation.class);

static {
rotations.put(ModelRotation.X0_Y0, identity());
}

static TRSRTransformation get(ModelRotation rotation) {
return rotations.computeIfAbsent(rotation, r -> new TRSRTransformation(ForgeHooksClient.getMatrix(r)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package club.sk1er.patcher.mixins.performance.forge;

import club.sk1er.patcher.hooks.TRSRTransformationHook;
import net.minecraft.client.resources.model.ModelRotation;
import net.minecraftforge.client.model.ForgeBlockStateV1;
import net.minecraftforge.client.model.TRSRTransformation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(value = ForgeBlockStateV1.Variant.Deserializer.class, remap = false)
public class ForgeBlockStateV1Mixin_OptimizeTransforms {
//#if MC==10809
@Redirect(method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraftforge/client/model/ForgeBlockStateV1$Variant;", at = @At(value = "NEW", target = "net/minecraftforge/client/model/TRSRTransformation"))
private TRSRTransformation patcher$from(ModelRotation rotation) {
return TRSRTransformationHook.from(rotation);
}
//#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package club.sk1er.patcher.mixins.performance.forge;

import club.sk1er.patcher.hooks.TRSRTransformationHook;
import net.minecraft.client.renderer.block.model.ItemTransformVec3f;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.TRSRTransformation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(value = ForgeHooksClient.class, remap = false)
public abstract class ForgeHooksClientMixin_OptimizeTransforms {
//#if MC==10809
@SuppressWarnings("deprecation")
@Redirect(method = "applyTransform(Lnet/minecraft/client/renderer/block/model/ItemTransformVec3f;Lcom/google/common/base/Optional;)Lcom/google/common/base/Optional;", at = @At(value = "NEW", target = "net/minecraftforge/client/model/TRSRTransformation"))
private static TRSRTransformation patcher$from(ItemTransformVec3f transform) {
return TRSRTransformationHook.from(transform);
}
//#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package club.sk1er.patcher.mixins.performance.forge;

import net.minecraftforge.client.model.TRSRTransformation;
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.callback.CallbackInfoReturnable;

@Mixin(value = TRSRTransformation.class, remap = false)
public class TRSRTransformationMixin_OptimizeTransforms {
//#if MC==10809
@Shadow
@Final
private static TRSRTransformation identity;

@Inject(method = "compose", at = @At("HEAD"), cancellable = true)
private void patcher$earlyExitCompose(TRSRTransformation b, CallbackInfoReturnable<TRSRTransformation> cir) {
TRSRTransformation thiz = (TRSRTransformation) (Object) this;
if (thiz == identity) {
cir.setReturnValue(b);
} else if (b == identity) {
cir.setReturnValue(thiz);
}
}

@Inject(method = "blockCenterToCorner", at = @At("HEAD"), cancellable = true)
private static void patcher$earlyExitBlockCenter(TRSRTransformation transform, CallbackInfoReturnable<TRSRTransformation> cir) {
if (transform == identity) {
cir.setReturnValue(transform);
}
}

@Inject(method = "blockCornerToCenter", at = @At("HEAD"), cancellable = true)
private static void patcher$earlyExitBlockCorner(TRSRTransformation transform, CallbackInfoReturnable<TRSRTransformation> cir) {
if (transform == identity) {
cir.setReturnValue(transform);
}
}
//#endif
}
3 changes: 3 additions & 0 deletions src/main/resources/mixins.patcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,14 @@
"performance.forge.FluidRegistryMixin_Optimization",
"performance.forge.FMLClientHandlerMixin_Optimization",
"performance.forge.FMLControlledNamespacedRegistryMixin_TrimAvailabilityMap",
"performance.forge.ForgeBlockStateV1Mixin_OptimizeTransforms",
"performance.forge.ForgeHooksClientMixin_OptimizeTransforms",
"performance.forge.ItemLayerModelMixin_ReduceQuadCount",
"performance.forge.JarDiscovererMixin_ReplaceRegex",
"performance.forge.LightUtilMixin_OptimizeNormals",
"performance.forge.ModDiscovererMixin_Logging",
"performance.forge.ModDiscovererMixin_ReplaceRegex",
"performance.forge.TRSRTransformationMixin_OptimizeTransforms",
"performance.forge.VertexLighterFlatMixin_OptimizeNormals",
"performance.hudcaching.EntityRendererMixin_HUDCaching",
"performance.hudcaching.FramebufferMixin_HUDCaching",
Expand Down

0 comments on commit 397b146

Please sign in to comment.