Skip to content

Commit

Permalink
Rewrite injections in ItemRendererMixin to be less invasive (#2968)
Browse files Browse the repository at this point in the history
  • Loading branch information
lowercasebtw authored Jan 4, 2025
1 parent 614e928 commit 62fad40
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
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
* and log a message.
* @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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<RandomSource> 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<RandomSource> 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<BakedQuad> quads, int[] colors, int light, int overlay, Operation<Void> 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<BakedQuad> quads = model.getQuads(null, direction, random);

if (!quads.isEmpty()) {
renderBakedItemQuads(matrices, writer, quads, colors, light, overlay);
}
}

random.setSeed(42L);
List<BakedQuad> 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);
}
}

Expand All @@ -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());
Expand Down

0 comments on commit 62fad40

Please sign in to comment.