Skip to content

Commit

Permalink
Sodium 0.5.6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 26, 2024
1 parent a994da4 commit a3f561b
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 93 deletions.
4 changes: 2 additions & 2 deletions buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@

public class Buildscript extends SimpleFabricProject {
static final boolean SODIUM = true;
static final boolean CUSTOM_SODIUM = false;
static final boolean CUSTOM_SODIUM = true;
static final String MC_VERSION = "1.20.4";
static final String customSodiumName = "sodium-fabric-mc23w45a-0.5.3git.86b69d2-dirty.jar";
static final String customSodiumName = "sodium-fabric-mc1.20.3-0.5.6git.7a62284.jar";

private static final String[] SOURCE_SETS = new String[] {
"main",
Expand Down
106 changes: 49 additions & 57 deletions src/main/java/net/coderbot/iris/mixin/vertices/MixinBufferBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexFormatElement;
import net.caffeinemc.mods.sodium.api.vertex.attributes.CommonVertexAttribute;
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatDescription;
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatRegistry;
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
import net.coderbot.iris.compat.sodium.impl.vertex_format.IrisCommonVertexAttributes;
import org.joml.Vector3f;
import net.coderbot.iris.vertices.NormI8;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,7 +19,6 @@
import net.coderbot.iris.vertices.IrisVertexFormats;
import net.coderbot.iris.vertices.NormalHelper;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.system.MemoryUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand Down Expand Up @@ -99,9 +94,15 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
@Shadow
public abstract void begin(VertexFormat.Mode drawMode, VertexFormat vertexFormat);

@Shadow
public abstract void putShort(int i, short s);

@Shadow
protected abstract void switchFormat(VertexFormat arg);

@Shadow
public abstract void nextElement();

@Override
public void iris$beginWithoutExtending(VertexFormat.Mode drawMode, VertexFormat vertexFormat) {
iris$shouldNotExtend = true;
Expand Down Expand Up @@ -138,6 +139,12 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
}
}

@Override
public @NotNull VertexConsumer uv2(int pBufferVertexConsumer0, int pInt1) {

return BufferVertexConsumer.super.uv2(pBufferVertexConsumer0, pInt1);
}

@ModifyArg(method = "begin", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/BufferBuilder;switchFormat(Lcom/mojang/blaze3d/vertex/VertexFormat;)V"))
private VertexFormat iris$afterBeginSwitchFormat(VertexFormat arg) {
if (extending) {
Expand All @@ -164,39 +171,12 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
vertexCount = 0;
}

@Unique
private int uv1Offset, normalOffset, tangentOffset, midTexOffset, midBlockOffset, blockIdOffset, entityIdOffset;

@Inject(method = "switchFormat", at = @At("RETURN"))
private void iris$preventHardcodedVertexWriting(VertexFormat format, CallbackInfo ci) {
if (!extending) {
return;
}

VertexFormatDescription formatDescription = VertexFormatRegistry.instance()
.get(format);

if (formatDescription.containsElement(CommonVertexAttribute.OVERLAY)) {
this.uv1Offset = formatDescription.getElementOffset(CommonVertexAttribute.OVERLAY);
}
if (formatDescription.containsElement(CommonVertexAttribute.NORMAL)) {
this.normalOffset = formatDescription.getElementOffset(CommonVertexAttribute.NORMAL);
}
if (formatDescription.containsElement(IrisCommonVertexAttributes.TANGENT)) {
this.tangentOffset = formatDescription.getElementOffset(IrisCommonVertexAttributes.TANGENT);
}
if (formatDescription.containsElement(IrisCommonVertexAttributes.MID_TEX_COORD)) {
this.midTexOffset = formatDescription.getElementOffset(IrisCommonVertexAttributes.MID_TEX_COORD);
}
if (formatDescription.containsElement(IrisCommonVertexAttributes.BLOCK_ID)) {
this.blockIdOffset = formatDescription.getElementOffset(IrisCommonVertexAttributes.BLOCK_ID);
}
if (formatDescription.containsElement(IrisCommonVertexAttributes.ENTITY_ID)) {
this.entityIdOffset = formatDescription.getElementOffset(IrisCommonVertexAttributes.ENTITY_ID);
}
if (formatDescription.containsElement(IrisCommonVertexAttributes.MID_BLOCK)) {
this.midBlockOffset = formatDescription.getElementOffset(IrisCommonVertexAttributes.MID_BLOCK);
}
fastFormat = false;
fullFormat = false;
}
Expand All @@ -207,30 +187,39 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
return;
}

if (injectNormalAndUV1) {
this.normal(0, 0, 0);
if (injectNormalAndUV1 && currentElement == DefaultVertexFormat.ELEMENT_NORMAL) {
this.putInt(0, 0);
this.nextElement();
}

if (iris$isTerrain) {
// ENTITY_ELEMENT
long offset = MemoryUtil.memAddress(buffer, nextElementByte + blockIdOffset);
MemoryUtil.memPutShort(offset, currentBlock);
MemoryUtil.memPutShort(offset + 2, currentRenderType);
this.putShort(0, currentBlock);
this.putShort(2, currentRenderType);
} else {
// ENTITY_ELEMENT
long offset = MemoryUtil.memAddress(buffer, nextElementByte + entityIdOffset);
MemoryUtil.memPutShort(offset, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedEntity());
MemoryUtil.memPutShort(offset + 2, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedBlockEntity());
MemoryUtil.memPutShort(offset + 4, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedItem());
this.putShort(0, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedEntity());
this.putShort(2, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedBlockEntity());
this.putShort(4, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedItem());
}

this.nextElement();

// MID_TEXTURE_ELEMENT
this.putFloat(0, 0);
this.putFloat(4, 0);
this.nextElement();
// TANGENT_ELEMENT
this.putInt(0, 0);
this.nextElement();
if (iris$isTerrain) {
// MID_BLOCK_ELEMENT
long bufferIndex = MemoryUtil.memAddress(buffer, nextElementByte);
float x = MemoryUtil.memGetFloat(bufferIndex);
float y = MemoryUtil.memGetFloat(bufferIndex + 4);
float z = MemoryUtil.memGetFloat(bufferIndex + 8);
MemoryUtil.memPutInt(MemoryUtil.memAddress(buffer, nextElementByte + midBlockOffset), ExtendedDataHelper.computeMidBlock(x, y, z, currentLocalPosX, currentLocalPosY, currentLocalPosZ));
int posIndex = this.nextElementByte - 48;
float x = buffer.getFloat(posIndex);
float y = buffer.getFloat(posIndex + 4);
float z = buffer.getFloat(posIndex + 8);
this.putInt(0, ExtendedDataHelper.computeMidBlock(x, y, z, currentLocalPosX, currentLocalPosY, currentLocalPosZ));
this.nextElement();
}

vertexCount++;
Expand All @@ -246,9 +235,7 @@ private void fillExtendedData(int vertexAmount) {

int stride = format.getVertexSize();

int offset = nextElementByte + stride;

polygon.setup(buffer, offset, stride, vertexAmount);
polygon.setup(buffer, nextElementByte, stride, vertexAmount);

float midU = 0;
float midV = 0;
Expand Down Expand Up @@ -281,24 +268,24 @@ private void fillExtendedData(int vertexAmount) {
// NormalHelper.computeFaceNormalTri(normal, polygon); // Removed to enable smooth shaded triangles. Mods rendering triangles with bad normals need to recalculate their normals manually or otherwise shading might be inconsistent.

for (int vertex = 0; vertex < vertexAmount; vertex++) {
int packedNormal = buffer.getInt(offset - normalOffset - stride * vertex); // retrieve per-vertex normal
int packedNormal = buffer.getInt(nextElementByte - normalOffset - stride * vertex); // retrieve per-vertex normal

int tangent = NormalHelper.computeTangentSmooth(NormI8.unpackX(packedNormal), NormI8.unpackY(packedNormal), NormI8.unpackZ(packedNormal), polygon);

buffer.putFloat(offset - midUOffset - stride * vertex, midU);
buffer.putFloat(offset - midVOffset - stride * vertex, midV);
buffer.putInt(offset - tangentOffset - stride * vertex, tangent);
buffer.putFloat(nextElementByte - midUOffset - stride * vertex, midU);
buffer.putFloat(nextElementByte - midVOffset - stride * vertex, midV);
buffer.putInt(nextElementByte - tangentOffset - stride * vertex, tangent);
}
} else {
NormalHelper.computeFaceNormal(normal, polygon);
int packedNormal = NormI8.pack(normal.x, normal.y, normal.z, 0.0f);
int tangent = NormalHelper.computeTangent(normal.x, normal.y, normal.z, polygon);

for (int vertex = 0; vertex < vertexAmount; vertex++) {
buffer.putFloat(offset - midUOffset - stride * vertex, midU);
buffer.putFloat(offset - midVOffset - stride * vertex, midV);
buffer.putInt(offset - normalOffset - stride * vertex, packedNormal);
buffer.putInt(offset - tangentOffset - stride * vertex, tangent);
buffer.putFloat(nextElementByte - midUOffset - stride * vertex, midU);
buffer.putFloat(nextElementByte - midVOffset - stride * vertex, midV);
buffer.putInt(nextElementByte - normalOffset - stride * vertex, packedNormal);
buffer.putInt(nextElementByte - tangentOffset - stride * vertex, tangent);
}
}
}
Expand All @@ -320,4 +307,9 @@ public void endBlock() {
this.currentLocalPosY = 0;
this.currentLocalPosZ = 0;
}

@Unique
private void putInt(int i, int value) {
this.buffer.putInt(this.nextElementByte + i, value);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"depends": {
"fabricloader": ">=0.12.3",
"minecraft": ["1.20.3", "1.20.4"],
"sodium": "0.5.5"
"sodium": "0.5.6"
},

"breaks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private GlProgram<IrisChunkShaderInterface> createShader(IrisTerrainPass pass, S
int handle = ((GlObject) shader).handle();
ShaderBindingContextExt contextExt = (ShaderBindingContextExt) shader;

return new IrisChunkShaderInterface(handle, contextExt, pipeline, new ChunkShaderOptions(ChunkFogMode.SMOOTH, pass.toTerrainPass()),
return new IrisChunkShaderInterface(handle, contextExt, pipeline, new ChunkShaderOptions(ChunkFogMode.SMOOTH, pass.toTerrainPass(), vertexType),
tessCShader != null || tessEShader != null, pass == IrisTerrainPass.SHADOW || pass == IrisTerrainPass.SHADOW_CUTOUT, blendOverride, bufferOverrides, alpha, pipeline.getCustomUniforms());
});
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
* Initialized by {@link net.coderbot.iris.compat.sodium.mixin.vertex_format.MixinChunkMeshAttribute}
*/
public class IrisChunkMeshAttributes {
public static ChunkMeshAttribute POSITION_MATERIAL_MESH;
public static ChunkMeshAttribute COLOR_SHADE;
public static ChunkMeshAttribute BLOCK_TEXTURE;
public static ChunkMeshAttribute LIGHT_TEXTURE;
public static ChunkMeshAttribute NORMAL;
public static ChunkMeshAttribute TANGENT;
public static ChunkMeshAttribute MID_TEX_COORD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
public class XHFPModelVertexType implements ChunkVertexType {
public static final int STRIDE = 40;
public static final GlVertexFormat<ChunkMeshAttribute> VERTEX_FORMAT = GlVertexFormat.builder(ChunkMeshAttribute.class, STRIDE)
.addElement(IrisChunkMeshAttributes.POSITION_MATERIAL_MESH, 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 4, false, true)
.addElement(IrisChunkMeshAttributes.COLOR_SHADE, 8, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false)
.addElement(IrisChunkMeshAttributes.BLOCK_TEXTURE, 12, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
.addElement(IrisChunkMeshAttributes.LIGHT_TEXTURE, 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, true)
.addElement(ChunkMeshAttribute.POSITION_MATERIAL_MESH, 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 4, false, true)
.addElement(ChunkMeshAttribute.COLOR_SHADE, 8, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false)
.addElement(ChunkMeshAttribute.BLOCK_TEXTURE, 12, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
.addElement(ChunkMeshAttribute.LIGHT_TEXTURE, 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, true)
.addElement(IrisChunkMeshAttributes.MID_TEX_COORD, 20, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
.addElement(IrisChunkMeshAttributes.TANGENT, 24, IrisGlVertexAttributeFormat.BYTE, 4, true, false)
.addElement(IrisChunkMeshAttributes.NORMAL, 28, IrisGlVertexAttributeFormat.BYTE, 3, true, false)
.addElement(IrisChunkMeshAttributes.BLOCK_ID, 32, IrisGlVertexAttributeFormat.SHORT, 2, false, false)
.addElement(IrisChunkMeshAttributes.MID_BLOCK, 36, IrisGlVertexAttributeFormat.BYTE, 3, false, false)
.addElement(ChunkMeshAttribute.VERTEX_DATA, 40, IrisGlVertexAttributeFormat.BYTE, 0, false, false)
.build();

private static final int POSITION_MAX_VALUE = 65536;
Expand All @@ -37,6 +36,21 @@ public class XHFPModelVertexType implements ChunkVertexType {

private static final float TEXTURE_SCALE = (1.0f / TEXTURE_MAX_VALUE);

@Override
public float getTextureScale() {
return TEXTURE_SCALE;
}

@Override
public float getPositionScale() {
return MODEL_SCALE;
}

@Override
public float getPositionOffset() {
return -MODEL_ORIGIN;
}

@Override
public GlVertexFormat<ChunkMeshAttribute> getVertexFormat() {
return VERTEX_FORMAT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
@Mixin(SodiumGameOptions.class)
public class MixinSodiumGameOptions {
@Inject(method = "writeChanges()V", at = @At("RETURN"), remap = false)
public void iris$writeIrisConfig(CallbackInfo ci) {
@Inject(method = "writeToDisk", at = @At("RETURN"), remap = false)
private static void iris$writeIrisConfig(CallbackInfo ci) {
try {
if (Iris.getIrisConfig() != null) {
Iris.getIrisConfig().save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,18 @@ public class MixinChunkMeshAttribute {
static {
int baseOrdinal = $VALUES.length;

IrisChunkMeshAttributes.POSITION_MATERIAL_MESH
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("POSITION_MATERIAL_MESH", baseOrdinal);
IrisChunkMeshAttributes.COLOR_SHADE
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("COLOR_SHADE", baseOrdinal + 1);
IrisChunkMeshAttributes.BLOCK_TEXTURE
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("BLOCK_TEXTURE", baseOrdinal + 2);
IrisChunkMeshAttributes.LIGHT_TEXTURE
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("LIGHT_TEXTURE", baseOrdinal + 3);
IrisChunkMeshAttributes.NORMAL
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("NORMAL", baseOrdinal + 4);
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("NORMAL", baseOrdinal);
IrisChunkMeshAttributes.TANGENT
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("TANGENT", baseOrdinal + 5);
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("TANGENT", baseOrdinal + 1);
IrisChunkMeshAttributes.MID_TEX_COORD
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_TEX_COORD", baseOrdinal + 6);
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_TEX_COORD", baseOrdinal + 2);
IrisChunkMeshAttributes.BLOCK_ID
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("BLOCK_ID", baseOrdinal + 7);
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("BLOCK_ID", baseOrdinal + 3);
IrisChunkMeshAttributes.MID_BLOCK
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_BLOCK", baseOrdinal + 8);
= ChunkMeshAttributeAccessor.createChunkMeshAttribute("MID_BLOCK", baseOrdinal + 4);

$VALUES = ArrayUtils.addAll($VALUES,
IrisChunkMeshAttributes.POSITION_MATERIAL_MESH,
IrisChunkMeshAttributes.COLOR_SHADE,
IrisChunkMeshAttributes.BLOCK_TEXTURE,
IrisChunkMeshAttributes.LIGHT_TEXTURE,
IrisChunkMeshAttributes.NORMAL,
IrisChunkMeshAttributes.TANGENT,
IrisChunkMeshAttributes.MID_TEX_COORD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.jellysquid.mods.sodium.client.gl.tessellation.TessellationBinding;
import me.jellysquid.mods.sodium.client.render.chunk.DefaultChunkRenderer;
import me.jellysquid.mods.sodium.client.render.chunk.ShaderChunkRenderer;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkMeshAttribute;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
import net.coderbot.iris.compat.sodium.impl.IrisChunkShaderBindingPoints;
Expand Down Expand Up @@ -36,13 +37,13 @@ public MixinRegionChunkRenderer(RenderDevice device, ChunkVertexType vertexType)

attributes = new GlVertexAttributeBinding[]{
new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_POSITION_ID,
vertexFormat.getAttribute(IrisChunkMeshAttributes.POSITION_MATERIAL_MESH)),
vertexFormat.getAttribute(ChunkMeshAttribute.POSITION_MATERIAL_MESH)),
new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_COLOR,
vertexFormat.getAttribute(IrisChunkMeshAttributes.COLOR_SHADE)),
vertexFormat.getAttribute(ChunkMeshAttribute.COLOR_SHADE)),
new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_BLOCK_TEXTURE,
vertexFormat.getAttribute(IrisChunkMeshAttributes.BLOCK_TEXTURE)),
vertexFormat.getAttribute(ChunkMeshAttribute.BLOCK_TEXTURE)),
new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.ATTRIBUTE_LIGHT_TEXTURE,
vertexFormat.getAttribute(IrisChunkMeshAttributes.LIGHT_TEXTURE)),
vertexFormat.getAttribute(ChunkMeshAttribute.LIGHT_TEXTURE)),
new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.MID_BLOCK,
vertexFormat.getAttribute(IrisChunkMeshAttributes.MID_BLOCK)),
new GlVertexAttributeBinding(IrisChunkShaderBindingPoints.BLOCK_ID,
Expand Down

0 comments on commit a3f561b

Please sign in to comment.