Skip to content

Commit

Permalink
Add full support for Cable Facades
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Dec 24, 2024
1 parent f8ab5ee commit c772d68
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.irisshaders.iris.compat.sodium.mixin;

import net.caffeinemc.mods.sodium.client.render.chunk.compile.ChunkBuildBuffers;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(BlockRenderer.class)
public interface BlockRendererAccessor {
@Accessor
ChunkBuildBuffers getBuffers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.irisshaders.iris.compat.sodium.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
import net.caffeinemc.mods.sodium.client.render.frapi.render.AbstractBlockRenderContext;
import net.irisshaders.iris.platform.IrisPlatformHelpers;
import net.irisshaders.iris.shaderpack.materialmap.WorldRenderingSettings;
import net.irisshaders.iris.vertices.BlockSensitiveBufferBuilder;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(AbstractBlockRenderContext.class)
public class MixinAbstractBlockRenderContext {
@Shadow
protected BlockPos pos;

@Shadow
protected BlockAndTintGetter level;

@Inject(method = "bufferDefaultModel", at = @At(value = "INVOKE", target = "Lnet/caffeinemc/mods/sodium/client/services/PlatformModelAccess;getQuads(Lnet/minecraft/world/level/BlockAndTintGetter;Lnet/minecraft/core/BlockPos;Lnet/minecraft/client/resources/model/BakedModel;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/Direction;Lnet/minecraft/util/RandomSource;Lnet/minecraft/client/renderer/RenderType;Lnet/caffeinemc/mods/sodium/client/services/SodiumModelData;)Ljava/util/List;"))
private void checkDirectionNeo(BakedModel model, BlockState state, CallbackInfo ci, @Local Direction cullFace) {
if ((Object) this instanceof BlockRenderer r && WorldRenderingSettings.INSTANCE.getBlockStateIds() != null && cullFace != null) {
BlockState appearance = IrisPlatformHelpers.getInstance().getBlockAppearance(this.level, state, cullFace, this.pos);
((BlockSensitiveBufferBuilder) ((BlockRendererAccessor) r).getBuffers()).overrideBlock(WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(appearance));
}
}

@Inject(method = "bufferDefaultModel", at = @At(value = "TAIL"))
private void checkDirectionNeo(BakedModel model, BlockState state, CallbackInfo ci) {
if ((Object) this instanceof BlockRenderer r && WorldRenderingSettings.INSTANCE.getBlockStateIds() != null) {
((BlockSensitiveBufferBuilder) ((BlockRendererAccessor) r).getBuffers()).restoreBlock();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.irisshaders.iris.compat.sodium.mixin;

import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.BakedChunkModelBuilder;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.builder.ChunkMeshBufferBuilder;
import net.irisshaders.iris.vertices.BlockSensitiveBufferBuilder;
import net.irisshaders.iris.vertices.sodium.terrain.BlockContextHolder;
import net.irisshaders.iris.vertices.sodium.terrain.VertexEncoderInterface;
import org.spongepowered.asm.mixin.Mixin;
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(value = BakedChunkModelBuilder.class, remap = false)
public class MixinBakedChunkModelBuilder implements BlockSensitiveBufferBuilder {
@Unique
private final BlockContextHolder contextHolder = new BlockContextHolder();

@Inject(method = "<init>", at = @At(value = "TAIL"))
private void setupContextHolder(ChunkMeshBufferBuilder[] vertexBuffers, CallbackInfo ci) {
for (ChunkMeshBufferBuilder vertexBuffer : vertexBuffers) {
((VertexEncoderInterface) vertexBuffer).iris$setContextHolder(contextHolder);
}
}

@Override
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 overrideBlock(int block) {
contextHolder.overrideBlock(block);
}

@Override
public void restoreBlock() {
contextHolder.restoreBlock();
}

@Override
public void endBlock() {
contextHolder.setBlockData(0, (byte) 0, (byte) 0, 0, 0, 0);
}

@Override
public void ignoreMidBlock(boolean b) {
contextHolder.setIgnoreMidBlock(b);
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
package net.irisshaders.iris.compat.sodium.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.ChunkBuildBuffers;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.BakedChunkModelBuilder;
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.builder.ChunkMeshBufferBuilder;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import net.irisshaders.iris.vertices.BlockSensitiveBufferBuilder;
import net.irisshaders.iris.vertices.sodium.terrain.BlockContextHolder;
import net.irisshaders.iris.vertices.sodium.terrain.VertexEncoderInterface;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
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 org.spongepowered.asm.mixin.Shadow;

@Mixin(value = ChunkBuildBuffers.class, remap = false)
@Mixin(ChunkBuildBuffers.class)
public class MixinChunkBuildBuffers implements BlockSensitiveBufferBuilder {
@Unique
private final BlockContextHolder contextHolder = new BlockContextHolder();

@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/Reference2ReferenceOpenHashMap;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"))
private void setupContextHolder(ChunkVertexType vertexType, CallbackInfo ci, @Local TerrainRenderPass pass, @Local ChunkMeshBufferBuilder[] vertexBuffers) {
for (ChunkMeshBufferBuilder vertexBuffer : vertexBuffers) {
((VertexEncoderInterface) vertexBuffer).iris$setContextHolder(contextHolder);
@Shadow
@Final
private Reference2ReferenceOpenHashMap<TerrainRenderPass, BakedChunkModelBuilder> builders;

@Override
public void beginBlock(int block, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
for (BakedChunkModelBuilder value : builders.values()) {
((BlockSensitiveBufferBuilder) value).beginBlock(block, renderType, blockEmission, localPosX, localPosY, localPosZ);
}
}

@Override
public void beginBlock(int block, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ) {
contextHolder.setBlockData(block, renderType, blockEmission, localPosX, localPosY, localPosZ);
public void overrideBlock(int block) {
for (BakedChunkModelBuilder value : builders.values()) {
((BlockSensitiveBufferBuilder) value).overrideBlock(block);
}
}

@Override
public void restoreBlock() {
for (BakedChunkModelBuilder value : builders.values()) {
((BlockSensitiveBufferBuilder) value).restoreBlock();
}
}

@Override
public void endBlock() {
contextHolder.setBlockData(0, (byte) 0, (byte) 0, 0, 0, 0);
for (BakedChunkModelBuilder value : builders.values()) {
((BlockSensitiveBufferBuilder) value).endBlock();
}
}

@Override
public void ignoreMidBlock(boolean b) {
contextHolder.setIgnoreMidBlock(b);
for (BakedChunkModelBuilder value : builders.values()) {
((BlockSensitiveBufferBuilder) value).ignoreMidBlock(b);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.irisshaders.iris.compat.sodium.mixin;

import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.ChunkVertexConsumer;
import net.irisshaders.iris.vertices.BlockSensitiveBufferBuilder;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = ChunkVertexConsumer.class, remap = false)
public class MixinChunkVertexConsumer implements BlockSensitiveBufferBuilder {
@Shadow
@Final
private ChunkModelBuilder modelBuilder;

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

@Override
public void overrideBlock(int block) {
((BlockSensitiveBufferBuilder) modelBuilder).overrideBlock(block);
}

@Override
public void restoreBlock() {
((BlockSensitiveBufferBuilder) modelBuilder).restoreBlock();
}

@Override
public void endBlock() {
((BlockSensitiveBufferBuilder) modelBuilder).endBlock();
}

@Override
public void ignoreMidBlock(boolean b) {
((BlockSensitiveBufferBuilder) modelBuilder).ignoreMidBlock(b);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.irisshaders.iris.platform;

import net.minecraft.client.KeyMapping;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;

import java.nio.file.Path;
import java.util.ServiceLoader;
Expand All @@ -27,4 +31,6 @@ static IrisPlatformHelpers getInstance() {
KeyMapping registerKeyBinding(KeyMapping keyMapping);

boolean useELS();

BlockState getBlockAppearance(BlockAndTintGetter level, BlockState state, Direction cullFace, BlockPos pos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
public interface BlockSensitiveBufferBuilder {
void beginBlock(int block, byte renderType, byte blockEmission, int localPosX, int localPosY, int localPosZ);

void overrideBlock(int block);

void restoreBlock();

void endBlock();

void ignoreMidBlock(boolean b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class BlockContextHolder {
private byte renderType;
private int localPosX, localPosY, localPosZ;
private boolean ignoreMidBlock;
private int oldId = -1;

public int getBlockId() {
return blockId;
Expand Down Expand Up @@ -47,4 +48,20 @@ public boolean ignoreMidBlock() {
public void setIgnoreMidBlock(boolean ignoreMidBlock) {
this.ignoreMidBlock = ignoreMidBlock;
}

public void overrideBlock(int block) {
if (this.blockId == block) return;

if (this.oldId == -1) {
this.oldId = blockId;
}

this.blockId = block;
}

public void restoreBlock() {
if (this.oldId == -1) return;
this.blockId = oldId;
this.oldId = -1;
}
}
4 changes: 4 additions & 0 deletions common/src/main/resources/mixins.iris.compat.sodium.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
"plugin": "net.irisshaders.iris.mixin.IrisMixinPlugin",
"compatibilityLevel": "JAVA_8",
"client": [
"BlockRendererAccessor",
"CloudRendererAccessor",
"MixinAbstractBlockRenderContext",
"MixinBlockRenderer",
"MixinBakedChunkModelBuilder",
"MixinChunkBuildBuffers",
"MixinChunkVertexConsumer",
"MixinChunkMeshBufferBuilder",
"MixinChunkMeshBuildTask",
"MixinCloudRenderer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import net.fabricmc.loader.api.SemanticVersion;
import net.fabricmc.loader.api.VersionParsingException;
import net.minecraft.client.KeyMapping;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;

import java.nio.file.Path;
import java.text.ParseException;
Expand Down Expand Up @@ -54,4 +58,9 @@ public KeyMapping registerKeyBinding(KeyMapping keyMapping) {
public boolean useELS() {
return false;
}

@Override
public BlockState getBlockAppearance(BlockAndTintGetter level, BlockState state, Direction cullFace, BlockPos pos) {
return state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.irisshaders.iris.mixin.forge;

import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.irisshaders.iris.shaderpack.materialmap.WorldRenderingSettings;
import net.irisshaders.iris.vertices.BlockSensitiveBufferBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.client.event.AddSectionGeometryEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Pseudo
@Mixin(targets = "com/portingdeadmods/cable_facades/events/GameClientEvents$2", remap = true)
public class MixinGameClientEvents {
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/block/BlockRenderDispatcher;renderBatched(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/BlockAndTintGetter;Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;ZLnet/minecraft/util/RandomSource;Lnet/neoforged/neoforge/client/model/data/ModelData;Lnet/minecraft/client/renderer/RenderType;)V"))
private void iris$setId(AddSectionGeometryEvent.SectionRenderingContext sectionRenderingContext, CallbackInfo ci, @Local BlockState state, @Local VertexConsumer buffer, @Local BlockPos pos) {
if (WorldRenderingSettings.INSTANCE.getBlockStateIds() == null) return;
((BlockSensitiveBufferBuilder) buffer).beginBlock(WorldRenderingSettings.INSTANCE.getBlockStateIds().getInt(state), (byte) 0, (byte) state.getLightEmission(), pos.getX(), pos.getY(), pos.getZ());
}
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/block/BlockRenderDispatcher;renderBatched(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/BlockAndTintGetter;Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;ZLnet/minecraft/util/RandomSource;Lnet/neoforged/neoforge/client/model/data/ModelData;Lnet/minecraft/client/renderer/RenderType;)V", shift = At.Shift.AFTER))
private void iris$removeId(AddSectionGeometryEvent.SectionRenderingContext sectionRenderingContext, CallbackInfo ci, @Local BlockState state, @Local VertexConsumer buffer, @Local BlockPos pos) {
if (WorldRenderingSettings.INSTANCE.getBlockStateIds() == null) return;
((BlockSensitiveBufferBuilder) buffer).endBlock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import net.irisshaders.iris.Iris;
import net.minecraft.client.KeyMapping;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.fml.loading.LoadingModList;
Expand Down Expand Up @@ -51,4 +55,9 @@ public KeyMapping registerKeyBinding(KeyMapping keyMapping) {
public boolean useELS() {
return true;
}

@Override
public BlockState getBlockAppearance(BlockAndTintGetter level, BlockState state, Direction cullFace, BlockPos pos) {
return state.getAppearance(level, pos, cullFace, null, null);
}
}
1 change: 1 addition & 0 deletions neoforge/src/main/resources/mixins.iris.forge.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compatibilityLevel": "JAVA_8",
"client": [
"MixinHumanoidArmorLayer",
"MixinGameClientEvents",
"MixinItemBlockRenderTypes",
"MixinShaderInstance",
"MixinRenderFlame",
Expand Down

0 comments on commit c772d68

Please sign in to comment.