diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/SingleQuadParticleMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/SingleQuadParticleMixin.java index edfd303de7..6dd1a3e2cc 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/SingleQuadParticleMixin.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/particle/SingleQuadParticleMixin.java @@ -1,9 +1,9 @@ package net.caffeinemc.mods.sodium.mixin.features.render.particle; import net.caffeinemc.mods.sodium.api.vertex.format.common.ParticleVertex; -import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; import com.mojang.blaze3d.vertex.VertexConsumer; import net.caffeinemc.mods.sodium.api.util.ColorABGR; +import net.caffeinemc.mods.sodium.client.render.vertex.VertexConsumerUtils; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.particle.Particle; import net.minecraft.client.particle.SingleQuadParticle; @@ -11,9 +11,11 @@ import org.joml.Vector3f; import org.lwjgl.system.MemoryStack; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; 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; @Mixin(SingleQuadParticle.class) public abstract class SingleQuadParticleMixin extends Particle { @@ -32,9 +34,6 @@ public abstract class SingleQuadParticleMixin extends Particle { @Shadow protected abstract float getV1(); - @Unique - private Vector3f transferVector = new Vector3f(); - protected SingleQuadParticleMixin(ClientLevel level, double x, double y, double z) { super(level, x, y, z); } @@ -43,8 +42,16 @@ protected SingleQuadParticleMixin(ClientLevel level, double x, double y, double * @reason Optimize function * @author JellySquid */ - @Overwrite - protected void renderRotatedQuad(VertexConsumer vertexConsumer, Quaternionf quaternionf, float x, float y, float z, float tickDelta) { + @Inject(method = "renderRotatedQuad(Lcom/mojang/blaze3d/vertex/VertexConsumer;Lorg/joml/Quaternionf;FFFF)V", at = @At("HEAD"), cancellable = true) + protected void renderRotatedQuad(VertexConsumer vertexConsumer, Quaternionf quaternionf, float x, float y, float z, float tickDelta, CallbackInfo ci) { + final var writer = VertexConsumerUtils.convertOrLog(vertexConsumer); + + if (writer == null) { + return; + } + + ci.cancel(); + float size = this.getQuadSize(tickDelta); float minU = this.getU0(); float maxU = this.getU1(); @@ -52,24 +59,22 @@ protected void renderRotatedQuad(VertexConsumer vertexConsumer, Quaternionf quat float maxV = this.getV1(); int light = this.getLightColor(tickDelta); - var writer = VertexBufferWriter.of(vertexConsumer); - int color = ColorABGR.pack(this.rCol, this.gCol, this.bCol, this.alpha); try (MemoryStack stack = MemoryStack.stackPush()) { long buffer = stack.nmalloc(4 * ParticleVertex.STRIDE); long ptr = buffer; - this.writeVertex(ptr, quaternionf, x, y, z, 1.0F, -1.0F, size, maxU, maxV, color, light); + this.sodium$writeVertex(ptr, quaternionf, x, y, z, 1.0F, -1.0F, size, maxU, maxV, color, light); ptr += ParticleVertex.STRIDE; - this.writeVertex(ptr, quaternionf, x, y, z, 1.0F, 1.0F, size, maxU, minV, color, light); + this.sodium$writeVertex(ptr, quaternionf, x, y, z, 1.0F, 1.0F, size, maxU, minV, color, light); ptr += ParticleVertex.STRIDE; - this.writeVertex(ptr, quaternionf, x, y, z, -1.0F, 1.0F, size, minU, minV, color, light); + this.sodium$writeVertex(ptr, quaternionf, x, y, z, -1.0F, 1.0F, size, minU, minV, color, light); ptr += ParticleVertex.STRIDE; - this.writeVertex(ptr, quaternionf, x, y, z, -1.0F, -1.0F, size, minU, maxV, color, light); + this.sodium$writeVertex(ptr, quaternionf, x, y, z, -1.0F, -1.0F, size, minU, maxV, color, light); ptr += ParticleVertex.STRIDE; writer.push(stack, buffer, 4, ParticleVertex.FORMAT); @@ -77,12 +82,16 @@ protected void renderRotatedQuad(VertexConsumer vertexConsumer, Quaternionf quat } @Unique - private void writeVertex(long ptr, Quaternionf quaternionf, float originX, float originY, float originZ, float posX, float posY, float size, float u, float v, int color, int light) { - transferVector.set(posX, posY, 0.0f); - transferVector.rotate(quaternionf); - transferVector.mul(size); - transferVector.add(originX, originY, originZ); + private final Vector3f sodium$scratchVertex = new Vector3f(); // not thread-safe - ParticleVertex.put(ptr, transferVector.x(), transferVector.y(), transferVector.z(), u, v, color, light); + @Unique + private void sodium$writeVertex(long ptr, Quaternionf quaternionf, float originX, float originY, float originZ, float posX, float posY, float size, float u, float v, int color, int light) { + final var vertex = this.sodium$scratchVertex; + vertex.set(posX, posY, 0.0f); + vertex.rotate(quaternionf); + vertex.mul(size); + vertex.add(originX, originY, originZ); + + ParticleVertex.put(ptr, vertex.x(), vertex.y(), vertex.z(), u, v, color, light); } }