Skip to content

Commit

Permalink
Port to 1.21-pre1 and fix lines
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed May 30, 2024
1 parent 57f7207 commit f62187e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
object Constants {
// https://fabricmc.net/develop/
const val MINECRAFT_VERSION: String = "24w21b"
const val MINECRAFT_VERSION: String = "1.21-pre1"
const val FABRIC_LOADER_VERSION: String = "0.15.11"
const val FABRIC_API_VERSION: String = "0.99.1+1.21"
const val FABRIC_API_VERSION: String = "0.99.2+1.21"

// https://semver.org/
const val MOD_VERSION: String = "0.6.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.caffeinemc.mods.sodium.client.render.vertex;

public interface BufferBuilderExtension {
void duplicateLastVertex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatDescription;
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatRegistry;
import net.caffeinemc.mods.sodium.api.vertex.serializer.VertexSerializerRegistry;
import net.caffeinemc.mods.sodium.client.render.vertex.BufferBuilderExtension;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.objectweb.asm.Opcodes;
Expand All @@ -22,7 +23,7 @@
import java.nio.ByteBuffer;

@Mixin(BufferBuilder.class)
public abstract class BufferBuilderMixin implements VertexConsumer, VertexBufferWriter {
public abstract class BufferBuilderMixin implements VertexConsumer, VertexBufferWriter, BufferBuilderExtension {
@Shadow
protected abstract void ensureBuilding();

Expand Down Expand Up @@ -52,6 +53,15 @@ private void onInit(ByteBufferBuilder byteBufferBuilder, VertexFormat.Mode mode,
.get(format);
}

@Override
public void duplicateLastVertex() {
if (this.vertices != 0) {
long l = this.buffer.reserve(this.vertexSize);
MemoryIntrinsics.copyMemory(l - this.vertexSize, l, this.vertexSize);
++this.vertices;
}
}

@Override
public boolean canUseIntrinsics() {
return this.formatDescription != null && this.formatDescription.isSimpleFormat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.AABB;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(EntityRenderer.class)
public abstract class EntityRendererMixin<T extends Entity> {
@Inject(method = "shouldRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/culling/Frustum;isVisible(Lnet/minecraft/world/phys/AABB;)Z", shift = At.Shift.AFTER), cancellable = true)
private void preShouldRender(T entity, Frustum frustum, double x, double y, double z, CallbackInfoReturnable<Boolean> cir) {
@Redirect(method = "shouldRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/culling/Frustum;isVisible(Lnet/minecraft/world/phys/AABB;)Z", ordinal = 0))
private boolean preShouldRender(Frustum frustum, AABB aABB, T entity) {
var renderer = SodiumWorldRenderer.instanceNullable();

if (renderer == null) {
return;
return frustum.isVisible(aABB);
}

// If the entity isn't culled already by other means, try to perform a second pass
if (cir.getReturnValue() && !renderer.isEntityVisible(entity)) {
cir.setReturnValue(false);
}
return frustum.isVisible(aABB) && renderer.isEntityVisible(entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.caffeinemc.mods.sodium.client.render.vertex.BufferBuilderExtension;
import net.caffeinemc.mods.sodium.client.render.vertex.VertexConsumerUtils;
import net.caffeinemc.mods.sodium.api.vertex.format.common.LineVertex;
import net.minecraft.client.renderer.LevelRenderer;
Expand Down Expand Up @@ -80,6 +81,10 @@ private static void drawBoxFast(PoseStack matrices, VertexConsumer vertexConsume
float v8y = Math.fma(position.m01(), x2f, Math.fma(position.m11(), y2f, Math.fma(position.m21(), z2f, position.m31())));
float v8z = Math.fma(position.m02(), x2f, Math.fma(position.m12(), y2f, Math.fma(position.m22(), z2f, position.m32())));

if (writer instanceof BufferBuilderExtension extension) {
extension.duplicateLastVertex();
}

writeLineVertices(writer, v1x, v1y, v1z, ColorABGR.pack(red, yAxisGreen, zAxisBlue, alpha), NormI8.pack(normal.m00(), normal.m01(), normal.m02()));
writeLineVertices(writer, v2x, v2y, v2z, ColorABGR.pack(red, yAxisGreen, zAxisBlue, alpha), NormI8.pack(normal.m00(), normal.m01(), normal.m02()));
writeLineVertices(writer, v1x, v1y, v1z, ColorABGR.pack(xAxisRed, green, zAxisBlue, alpha), NormI8.pack(normal.m10(), normal.m11(), normal.m12()));
Expand All @@ -103,7 +108,7 @@ private static void drawBoxFast(PoseStack matrices, VertexConsumer vertexConsume
writeLineVertices(writer, v7x, v7y, v7z, color, NormI8.pack(normal.m10(), normal.m11(), normal.m12()));
writeLineVertices(writer, v8x, v8y, v8z, color, NormI8.pack(normal.m10(), normal.m11(), normal.m12()));
writeLineVertices(writer, v5x, v5y, v5z, color, NormI8.pack(normal.m20(), normal.m21(), normal.m22()));
writeLineVertices(writer, v8x, v8y, v8z, color, NormI8.pack(normal.m20(), normal.m21(), normal.m22()));
writeSingleVertex(writer, v8x, v8y, v8z, color, NormI8.pack(normal.m20(), normal.m21(), normal.m22()));
}

@Unique
Expand All @@ -119,7 +124,17 @@ private static void writeLineVertices(VertexBufferWriter writer, float x, float

writer.push(stack, buffer, 2, LineVertex.FORMAT);
}
}

@Unique
private static void writeSingleVertex(VertexBufferWriter writer, float x, float y, float z, int color, int normal) {
try (MemoryStack stack = MemoryStack.stackPush()) {
long buffer = stack.nmalloc(LineVertex.STRIDE);

LineVertex.put(buffer, x, y, z, color, normal);

writer.push(stack, buffer, 1, LineVertex.FORMAT);
}
}

}

0 comments on commit f62187e

Please sign in to comment.