Skip to content

Commit

Permalink
Use vanilla render path and log warning when incompatible VertexConsu…
Browse files Browse the repository at this point in the history
…mer is detected (#1842)

Co-authored-by: JellySquid <[email protected]>
  • Loading branch information
embeddedt and jellysquid3 authored Jul 22, 2023
1 parent 1ad18d2 commit 5b57419
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.caffeinemc.mods.sodium.api.memory.MemoryIntrinsics;
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatDescription;
import net.minecraft.client.render.VertexConsumer;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.system.MemoryStack;

public interface VertexBufferWriter {
Expand All @@ -21,6 +22,22 @@ static VertexBufferWriter of(VertexConsumer consumer) {
throw createUnsupportedVertexConsumerThrowable(consumer);
}

/**
* Converts a {@link VertexConsumer} into a {@link VertexBufferWriter} if possible.
*
* @param consumer The vertex consumer to create a writer for
* @return An implementation of {@link VertexBufferWriter} which will write vertices into {@param consumer}, or null
* if the vertex consumer does not implement the necessary interface
*/
@Nullable
static VertexBufferWriter tryOf(VertexConsumer consumer) {
if (consumer instanceof VertexBufferWriter writer) {
return writer;
}

return null;
}

private static RuntimeException createUnsupportedVertexConsumerThrowable(VertexConsumer consumer) {
var clazz = consumer.getClass();
var name = clazz.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.jellysquid.mods.sodium.client.render.vertex;

import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import it.unimi.dsi.fastutil.objects.ReferenceSet;
import me.jellysquid.mods.sodium.client.SodiumClientMod;
import net.minecraft.client.render.VertexConsumer;

public class VertexConsumerTracker {
private static final ReferenceSet<Class<? extends VertexConsumer>> BAD_CONSUMERS = new ReferenceOpenHashSet<>();

public static void logBadConsumer(VertexConsumer consumer) {
if(BAD_CONSUMERS.add(consumer.getClass())) {
SodiumClientMod.logger().warn("Some Sodium optimizations are being bypassed to prevent crashes with a mod that is implementing " + consumer.getClass().getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.jellysquid.mods.sodium.client.render.vertex;

import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter;
import net.minecraft.client.render.VertexConsumer;

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) {
VertexBufferWriter writer = VertexBufferWriter.tryOf(consumer);
if(writer == null) {
VertexConsumerTracker.logBadConsumer(consumer);
}
return writer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.jellysquid.mods.sodium.client.model.quad.BakedQuadView;
import me.jellysquid.mods.sodium.client.render.immediate.model.BakedModelEncoder;
import me.jellysquid.mods.sodium.client.render.texture.SpriteUtil;
import me.jellysquid.mods.sodium.client.render.vertex.VertexConsumerUtils;
import me.jellysquid.mods.sodium.common.util.DirectionUtil;
import net.caffeinemc.mods.sodium.api.util.ColorABGR;
import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter;
Expand All @@ -19,6 +20,9 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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 java.util.List;

Expand All @@ -30,9 +34,14 @@ public class MixinBlockModelRenderer {
* @reason Use optimized vertex writer intrinsics, avoid allocations
* @author JellySquid
*/
@Overwrite
public void render(MatrixStack.Entry entry, VertexConsumer vertexConsumer, BlockState blockState, BakedModel bakedModel, float red, float green, float blue, int light, int overlay) {
var writer = VertexBufferWriter.of(vertexConsumer);
@Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack$Entry;Lnet/minecraft/client/render/VertexConsumer;Lnet/minecraft/block/BlockState;Lnet/minecraft/client/render/model/BakedModel;FFFII)V", at = @At("HEAD"), cancellable = true)
private void renderFast(MatrixStack.Entry entry, VertexConsumer vertexConsumer, BlockState blockState, BakedModel bakedModel, float red, float green, float blue, int light, int overlay, CallbackInfo ci) {
var writer = VertexConsumerUtils.convertOrLog(vertexConsumer);
if(writer == null) {
return;
}

ci.cancel();

Random random = this.random;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.jellysquid.mods.sodium.mixin.features.entity.fast_render;

import me.jellysquid.mods.sodium.client.model.ModelCuboidAccessor;
import me.jellysquid.mods.sodium.client.render.vertex.VertexConsumerUtils;
import net.caffeinemc.mods.sodium.api.render.immediate.RenderImmediate;
import me.jellysquid.mods.sodium.client.render.immediate.model.ModelCuboid;
import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter;
Expand Down Expand Up @@ -53,9 +54,15 @@ private void onInit(List<ModelPart.Cuboid> cuboids, Map<String, ModelPart> child
* @author JellySquid
* @reason Use optimized vertex writer, avoid allocations, use quick matrix transformations
*/
@Overwrite
private void renderCuboids(MatrixStack.Entry matrices, VertexConsumer vertexConsumer, int light, int overlay, float red, float green, float blue, float alpha) {
var writer = VertexBufferWriter.of(vertexConsumer);
@Inject(method = "renderCuboids", at = @At("HEAD"), cancellable = true)
private void renderCuboidsFast(MatrixStack.Entry matrices, VertexConsumer vertexConsumer, int light, int overlay, float red, float green, float blue, float alpha, CallbackInfo ci) {
var writer = VertexConsumerUtils.convertOrLog(vertexConsumer);
if(writer == null) {
return;
}

ci.cancel();

int color = ColorABGR.pack(red, green, blue, alpha);

for (ModelCuboid cuboid : this.sodium$cuboids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.jellysquid.mods.sodium.client.model.quad.BakedQuadView;
import me.jellysquid.mods.sodium.client.render.immediate.model.BakedModelEncoder;
import me.jellysquid.mods.sodium.client.render.texture.SpriteUtil;
import me.jellysquid.mods.sodium.client.render.vertex.VertexConsumerUtils;
import me.jellysquid.mods.sodium.client.model.color.interop.ItemColorsExtended;
import me.jellysquid.mods.sodium.common.util.DirectionUtil;
import net.caffeinemc.mods.sodium.api.util.ColorARGB;
Expand All @@ -19,6 +20,9 @@
import net.minecraft.util.math.random.LocalRandom;
import net.minecraft.util.math.random.Random;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

Expand All @@ -34,9 +38,14 @@ public class MixinItemRenderer {
* @reason Avoid allocations
* @author JellySquid
*/
@Overwrite
private void renderBakedItemModel(BakedModel model, ItemStack itemStack, int light, int overlay, MatrixStack matrixStack, VertexConsumer vertexConsumer) {
var writer = VertexBufferWriter.of(vertexConsumer);
@Inject(method = "renderBakedItemModel", at = @At("HEAD"), cancellable = true)
private void renderModelFast(BakedModel model, ItemStack itemStack, int light, int overlay, MatrixStack matrixStack, VertexConsumer vertexConsumer, CallbackInfo ci) {
var writer = VertexConsumerUtils.convertOrLog(vertexConsumer);
if(writer == null) {
return;
}

ci.cancel();

Random random = this.random;
MatrixStack.Entry matrices = matrixStack.peek();
Expand Down

0 comments on commit 5b57419

Please sign in to comment.