diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/render/vertex/VertexConsumerUtils.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/render/vertex/VertexConsumerUtils.java index 6118d3df41..978da79efd 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/render/vertex/VertexConsumerUtils.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/render/vertex/VertexConsumerUtils.java @@ -3,6 +3,8 @@ import com.mojang.blaze3d.vertex.VertexConsumer; import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; +import javax.annotation.Nullable; + public class VertexConsumerUtils { /** * Attempt to convert a {@link VertexConsumer} into a {@link VertexBufferWriter}. If this fails, return null @@ -10,7 +12,7 @@ public class VertexConsumerUtils { * @param consumer the consumer to convert * @return a {@link VertexBufferWriter}, or null if the consumer does not support this */ - public static VertexBufferWriter convertOrLog(VertexConsumer consumer) { + public static @Nullable VertexBufferWriter convertOrLog(VertexConsumer consumer) { VertexBufferWriter writer = VertexBufferWriter.tryOf(consumer); if (writer == null) { diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/model/item/ItemRendererMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/model/item/ItemRendererMixin.java index 12ca36097e..425480f504 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/model/item/ItemRendererMixin.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/model/item/ItemRendererMixin.java @@ -1,25 +1,26 @@ package net.caffeinemc.mods.sodium.mixin.features.render.model.item; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import net.caffeinemc.mods.sodium.api.util.ColorARGB; +import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; import net.caffeinemc.mods.sodium.client.model.quad.BakedQuadView; import net.caffeinemc.mods.sodium.client.render.immediate.model.BakedModelEncoder; import net.caffeinemc.mods.sodium.client.render.texture.SpriteUtil; import net.caffeinemc.mods.sodium.client.render.vertex.VertexConsumerUtils; import net.caffeinemc.mods.sodium.client.util.DirectionUtil; -import net.caffeinemc.mods.sodium.api.util.ColorARGB; -import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; import net.minecraft.client.renderer.block.model.BakedQuad; import net.minecraft.client.renderer.entity.ItemRenderer; -import net.minecraft.client.resources.model.BakedModel; import net.minecraft.core.Direction; import net.minecraft.util.RandomSource; -import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.levelgen.SingleThreadedRandomSource; -import org.spongepowered.asm.mixin.*; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.vertex.VertexConsumer; + import java.util.List; @Mixin(ItemRenderer.class) @@ -33,38 +34,39 @@ private static int getLayerColorSafe(int[] is, int i) { } /** - * @reason Avoid allocations - * @author JellySquid + * @reason Avoid Allocations + * @return JellySquid + */ + @WrapOperation(method = "renderModelLists", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/RandomSource;create()Lnet/minecraft/util/RandomSource;")) + private static RandomSource renderModelFastRandom(Operation original) { + return ItemRendererMixin.random.get(); + } + + /** + * @reason Avoid Allocations + * @return JellySquid */ - @Inject(method = "renderModelLists", at = @At("HEAD"), cancellable = true) - private static void renderModelFast(BakedModel model, int[] colors, int light, int overlay, PoseStack poseStack, VertexConsumer vertexConsumer, CallbackInfo ci) { + @WrapOperation(method = "renderModelLists", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/Direction;values()[Lnet/minecraft/core/Direction;")) + private static Direction[] renderModelFastDirections(Operation original) { + return DirectionUtil.ALL_DIRECTIONS; + } + + /** + * @reason Avoid Allocations + * @return JellySquid + */ + @WrapOperation(method = "renderModelLists", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/entity/ItemRenderer;renderQuadList(Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;Ljava/util/List;[III)V")) + private static void renderModelFast(PoseStack poseStack, VertexConsumer vertexConsumer, List quads, int[] colors, int light, int overlay, Operation original) { var writer = VertexConsumerUtils.convertOrLog(vertexConsumer); if (writer == null) { + original.call(poseStack, vertexConsumer, quads, colors, light, overlay); return; } - ci.cancel(); - - RandomSource random = ItemRendererMixin.random.get(); - long seed = 42L; - - PoseStack.Pose matrices = poseStack.last(); - - for (Direction direction : DirectionUtil.ALL_DIRECTIONS) { - random.setSeed(42L); - List quads = model.getQuads(null, direction, random); - - if (!quads.isEmpty()) { - renderBakedItemQuads(matrices, writer, quads, colors, light, overlay); - } - } - - random.setSeed(42L); - List quads = model.getQuads(null, null, random); - + // TODO/NOTE: Should .last be a LocalRef? if (!quads.isEmpty()) { - renderBakedItemQuads(matrices, writer, quads, colors, light, overlay); + renderBakedItemQuads(poseStack.last(), writer, quads, colors, light, overlay); } } @@ -85,7 +87,7 @@ private static void renderBakedItemQuads(PoseStack.Pose matrices, VertexBufferWr if (bakedQuad.isTinted()) { color = ColorARGB.toABGR(getLayerColorSafe(colors, bakedQuad.getTintIndex())); } - + BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay, BakedModelEncoder.shouldMultiplyAlpha()); SpriteUtil.markSpriteActive(quad.getSprite());