Skip to content

Commit

Permalink
The Attribute Compactor
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 11, 2024
1 parent 95672a3 commit e7c5109
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ private void setupContextHolder(ChunkVertexType vertexType, CallbackInfo ci, @Lo
}

@Override
public void beginBlock(short block, short renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
public void beginBlock(int block, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
contextHolder.setBlockData(block, renderType, blockEmission, localPosX, localPosY, localPosZ);
}

@Override
public void endBlock() {
contextHolder.setBlockData((short) 0, (short) 0, (byte) 0, 0, 0, 0);
contextHolder.setBlockData(0, (byte) 0, (byte) 0, 0, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class MixinChunkMeshBuildTask {
private void iris$onRenderModel(ChunkBuildContext buildContext, CancellationToken cancellationToken, CallbackInfoReturnable<ChunkBuildOutput> cir, @Local ChunkBuildBuffers buffers, @Local BlockState blockState, @Local(ordinal = 0) BlockPos.MutableBlockPos blockPos) {
if (WorldRenderingSettings.INSTANCE.getBlockStateIds() == null) return;

((BlockSensitiveBufferBuilder) buffers).beginBlock((short) WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(blockState), (short) 0, (byte) blockState.getLightEmission(), blockPos.getX(), blockPos.getY(), blockPos.getZ());
((BlockSensitiveBufferBuilder) buffers).beginBlock(WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(blockState), (byte) 0, (byte) blockState.getLightEmission(), blockPos.getX(), blockPos.getY(), blockPos.getZ());
}

@Inject(method = "execute(Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/ChunkBuildContext;Lnet/caffeinemc/mods/sodium/client/util/task/CancellationToken;)Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/ChunkBuildOutput;", at = @At(value = "INVOKE", target = "Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/pipeline/FluidRenderer;render(Lnet/caffeinemc/mods/sodium/client/world/LevelSlice;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/material/FluidState;Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/BlockPos;Lnet/caffeinemc/mods/sodium/client/render/chunk/translucent_sorting/TranslucentGeometryCollector;Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/ChunkBuildBuffers;)V"))
private void iris$onRenderLiquid(ChunkBuildContext buildContext, CancellationToken cancellationToken, CallbackInfoReturnable<ChunkBuildOutput> cir, @Local ChunkBuildBuffers buffers, @Local BlockState blockState, @Local FluidState fluidState, @Local(ordinal = 0) BlockPos.MutableBlockPos blockPos) {
if (WorldRenderingSettings.INSTANCE.getBlockStateIds() == null) return;

((BlockSensitiveBufferBuilder) buffers).beginBlock((short) WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(fluidState.createLegacyBlock()), (short) 1, (byte) blockState.getLightEmission(), blockPos.getX(), blockPos.getY(), blockPos.getZ());
((BlockSensitiveBufferBuilder) buffers).beginBlock(WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(fluidState.createLegacyBlock()), (byte) 1, (byte) blockState.getLightEmission(), blockPos.getX(), blockPos.getY(), blockPos.getZ());
}

@Inject(method = "execute(Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/ChunkBuildContext;Lnet/caffeinemc/mods/sodium/client/util/task/CancellationToken;)Lnet/caffeinemc/mods/sodium/client/render/chunk/compile/ChunkBuildOutput;", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/BlockState;isSolidRender(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/core/BlockPos;)Z"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.GlConst;
import com.mojang.blaze3d.platform.GlStateManager;
import org.lwjgl.opengl.GL30C;
import org.lwjgl.opengl.GL46C;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -50,11 +51,11 @@ public class MixinGlStateManager_FramebufferBinding {

@Inject(method = "_glUseProgram", at = @At("HEAD"), cancellable = true, remap = false)
private static void iris$avoidRedundantBind2(int pInt0, CallbackInfo ci) {
if (iris$program == pInt0) {
if (iris$program == 0 && pInt0 == 0) {
ci.cancel();
} else {
iris$program = pInt0;
}

iris$program = pInt0;
}

@Inject(method = "_glDeleteFramebuffers(I)V", at = @At("HEAD"), remap = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public abstract class MixinBufferBuilder implements VertexConsumer, BlockSensiti
@Unique
private int iris$vertexCount;
@Unique
private short currentBlock = -1;
private int currentBlock = -1;
@Unique
private short currentRenderType = -1;
private byte currentRenderType = -1;
@Unique
private int currentLocalPosX;
@Unique
Expand Down Expand Up @@ -138,7 +138,7 @@ private void injectMidBlock(float x, float y, float z, CallbackInfoReturnable<Ve
if ((this.elementsToFill & IrisVertexFormats.ENTITY_ELEMENT.mask()) != 0) {
long offset = this.beginElement(IrisVertexFormats.ENTITY_ELEMENT);
// ENTITY_ELEMENT
MemoryUtil.memPutShort(offset, currentBlock);
MemoryUtil.memPutShort(offset, (short) currentBlock);
MemoryUtil.memPutShort(offset + 2, currentRenderType);
} else if ((this.elementsToFill & IrisVertexFormats.ENTITY_ID_ELEMENT.mask()) != 0) {
long offset = this.beginElement(IrisVertexFormats.ENTITY_ID_ELEMENT);
Expand Down Expand Up @@ -188,7 +188,7 @@ private void injectMidBlock(float x, float y, float z, CallbackInfoReturnable<Ve
}

@Override
public void beginBlock(short block, short renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
public void beginBlock(int block, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
this.currentBlock = block;
this.currentRenderType = renderType;
this.currentLocalPosX = localPosX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ public static void transform(
renameFunctionCall(root, "texture3D", "texture");
renameFunctionCall(root, "texture2DLod", "textureLod");
renameFunctionCall(root, "texture3DLod", "textureLod");
renameFunctionCall(root, "texture2DProj", "textureProj");
renameFunctionCall(root, "texture3DProj", "textureProj");
renameFunctionCall(root, "texture2DGrad", "textureGrad");
renameFunctionCall(root, "texture2DGradARB", "textureGrad");
renameFunctionCall(root, "texture3DGrad", "textureGrad");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void transform(

root.replaceReferenceExpressions(t, "textureMatrix", "mat4(1.0)");
SodiumTransformer.replaceMidTexCoord(t, tree, root, 1.0f / 32768.0f);
SodiumTransformer.replaceMCEntity(t, tree, root);

SodiumTransformer.injectVertInit(t, tree, root, parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static void transform(
CommonTransformer.transform(t, tree, root, parameters, false);

replaceMidTexCoord(t, tree, root, 1.0f / 32768.0f);
replaceMCEntity(t, tree, root);

root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix0, "mat4(1.0)");
root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix1, "iris_LightmapTextureMatrix");
Expand Down Expand Up @@ -175,6 +176,68 @@ vec2 _get_texcoord_bias() {
}


public static void replaceMCEntity(ASTParser t,
TranslationUnit tree, Root root) {
Type dimension = Type.BOOL;
for (Identifier id : root.identifierIndex.get("mc_Entity")) {
TypeAndInitDeclaration initDeclaration = (TypeAndInitDeclaration) id.getAncestor(
2, 0, TypeAndInitDeclaration.class::isInstance);
if (initDeclaration == null) {
continue;
}
DeclarationExternalDeclaration declaration = (DeclarationExternalDeclaration) initDeclaration.getAncestor(
1, 0, DeclarationExternalDeclaration.class::isInstance);
if (declaration == null) {
continue;
}
if (initDeclaration.getType().getTypeSpecifier() instanceof BuiltinNumericTypeSpecifier numeric) {
dimension = numeric.type;

declaration.detachAndDelete();
initDeclaration.detachAndDelete();
id.detachAndDelete();
break;
}
}


root.replaceReferenceExpressions(t, "mc_Entity", "iris_Entity");

switch (dimension) {
case BOOL:
return;
case FLOAT32:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "float iris_Entity = int(mc_Entity >> 1u) - 1;");
break;
case F32VEC2:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec2 iris_Entity = vec2(int(mc_Entity >> 1u) - 1, mc_Entity & 1u);");
break;
case F32VEC3:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec3 iris_Entity = vec3(int(mc_Entity >> 1u) - 1, mc_Entity & 1u, 0.0);");
break;
case F32VEC4:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec4 iris_Entity = vec4(int(mc_Entity >> 1u) - 1, mc_Entity & 1u, 0.0, 1.0);");
break;
case INT32:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "uint iris_Entity = int(mc_Entity >> 1u) - 1;");
break;
case I32VEC2:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "ivec2 iris_Entity = ivec2(int(mc_Entity >> 1u) - 1, mc_Entity & 1u);");
break;
case I32VEC3:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "ivec3 iris_Entity = ivec3(int(mc_Entity >> 1u) - 1, mc_Entity & 1u, 0);");
break;
case I32VEC4:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "ivec4 iris_Entity = ivec4(int(mc_Entity >> 1u) - 1, mc_Entity & 1u, 0, 1);");
break;
default:
throw new IllegalStateException("Got an invalid format mc_Entity (" + dimension.getCompactName() + ").");
}

tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "in uint mc_Entity;");
}


public static void replaceMidTexCoord(ASTParser t,
TranslationUnit tree, Root root, float textureScale) {
Type dimension = Type.BOOL;
Expand Down Expand Up @@ -206,7 +269,7 @@ public static void replaceMidTexCoord(ASTParser t,
case BOOL:
return;
case FLOAT32:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "float iris_MidTex = (mc_midTexCoord.x * " + textureScale + ").x;");
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "float iris_MidTex = (mc_midTexCoord.x * " + textureScale + ");");
break;
case F32VEC2:
tree.parseAndInjectNode(t, ASTInjectionPoint.BEFORE_DECLARATIONS, "vec2 iris_MidTex = (mc_midTexCoord.xy * " + textureScale + ").xy;");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.irisshaders.iris.vertices;

public interface BlockSensitiveBufferBuilder {
void beginBlock(short block, short renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ);
void beginBlock(int block, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ);

void endBlock();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

public class BlockContextHolder {
private byte blockEmission;
private short blockId;
private short renderType;
private int blockId;
private byte renderType;
private int localPosX, localPosY, localPosZ;

public short getBlockId() {
public int getBlockId() {
return blockId;
}

public short getRenderType() {
public byte getRenderType() {
return renderType;
}

Expand All @@ -30,7 +30,7 @@ public int getLocalPosZ() {
return localPosZ;
}

public void setBlockData(short blockId, short renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
public void setBlockData(int blockId, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
this.blockId = blockId;
this.renderType = renderType;
this.blockEmission = blockEmission;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class IrisChunkMeshAttributes {
public static final VertexFormatAttribute MID_TEX_COORD = new VertexFormatAttribute("midTexCoord", GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false);
public static final VertexFormatAttribute TANGENT = new VertexFormatAttribute("TANGENT", GlVertexAttributeFormat.BYTE, 4, true, false);
public static final VertexFormatAttribute NORMAL = new VertexFormatAttribute("NORMAL", GlVertexAttributeFormat.BYTE, 3, true, false);
public static final VertexFormatAttribute BLOCK_ID = new VertexFormatAttribute("BLOCK_ID", GlVertexAttributeFormat.SHORT, 2, false, false);
public static final VertexFormatAttribute BLOCK_ID = new VertexFormatAttribute("BLOCK_ID", GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true);
public static final VertexFormatAttribute MID_BLOCK = new VertexFormatAttribute("MID_BLOCK", GlVertexAttributeFormat.BYTE, 4, false, false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public long write(long ptr,
MemoryUtil.memPutInt(ptr + 16L, packLightAndData(light, material, section));

if (blockIdOffset != 0) {
MemoryUtil.memPutShort(ptr + blockIdOffset, contextHolder.getBlockId());
MemoryUtil.memPutShort(ptr + blockIdOffset + 2, contextHolder.getRenderType());
MemoryUtil.memPutInt(ptr + blockIdOffset, packBlockId(contextHolder));
}

if (midBlockOffset != 0) {
Expand All @@ -177,4 +176,8 @@ public long write(long ptr,

return ptr;
}

private int packBlockId(BlockContextHolder contextHolder) {
return ((contextHolder.getBlockId() + 1) << 1) | (contextHolder.getRenderType() & 1);
}
}

0 comments on commit e7c5109

Please sign in to comment.