From 377c729355f2e3a24f3989ae89a9d414c1f3f932 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 24 Sep 2022 22:44:42 -0700 Subject: [PATCH] Yeetrium - Mixin to sodium to steal block entity rendering duties - Fixes z-fighting and inconsistent BE rendering --- build.gradle | 2 +- .../flywheel/mixin/LevelRendererMixin.java | 29 +-------- .../SodiumChunkRenderDataMixin.java | 60 +++++++++++++++++++ src/main/resources/flywheel.mixins.json | 1 + 4 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/instancemanage/SodiumChunkRenderDataMixin.java diff --git a/build.gradle b/build.gradle index 3bcd74b6e..bc973f5da 100644 --- a/build.gradle +++ b/build.gradle @@ -112,7 +112,7 @@ dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" // switch to implementation for debugging - compileOnly fg.deobf("maven.modrinth:starlight-forge:1.0.2+1.18.2") + compileOnly fg.deobf("maven.modrinth:starlight-forge:1.1.1+1.19") compileOnly fg.deobf("maven.modrinth:rubidium:0.6.1") compileOnly fg.deobf("maven.modrinth:oculus:1.19-1.2.5a") diff --git a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java index f3838d4bc..9daf1d971 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java @@ -3,9 +3,7 @@ package com.jozufozu.flywheel.mixin; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; 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.Group; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -48,33 +46,8 @@ public class LevelRendererMixin { MinecraftForge.EVENT_BUS.post(new BeginFrameEvent(level, camera, frustum)); } - @Unique - private boolean flywheel$LayerRendered; - - /** - * This only gets injected if renderChunkLayer is not Overwritten - */ - @Group(name = "flywheel$renderLayer", min = 1, max = 2) - @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiling/ProfilerFiller;pop()V", ordinal = 1), method = "renderChunkLayer") - private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) { - flywheel$renderLayer(type, stack, camX, camY, camZ); - flywheel$LayerRendered = true; - } - - /** - * This always gets injected. - */ - @Group(name = "flywheel$renderLayer") @Inject(at = @At("TAIL"), method = "renderChunkLayer") - private void renderLayerSodium(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) { - if (!flywheel$LayerRendered) { - flywheel$renderLayer(type, stack, camX, camY, camZ); - } - flywheel$LayerRendered = false; - } - - @Unique - private void flywheel$renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ) { + private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) { MinecraftForge.EVENT_BUS.post(new RenderLayerEvent(level, type, stack, renderBuffers, camX, camY, camZ)); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/SodiumChunkRenderDataMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/SodiumChunkRenderDataMixin.java new file mode 100644 index 000000000..55e7340f2 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/SodiumChunkRenderDataMixin.java @@ -0,0 +1,60 @@ +package com.jozufozu.flywheel.mixin.instancemanage; + +import java.util.ArrayList; +import java.util.List; + +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.injection.callback.CallbackInfoReturnable; + +import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; +import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; + +import me.jellysquid.mods.sodium.client.render.chunk.data.ChunkRenderData; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; + +@OnlyIn(Dist.CLIENT) +@Mixin(targets = "me.jellysquid.mods.sodium.client.render.chunk.data.ChunkRenderData$Builder", remap = false) +public class SodiumChunkRenderDataMixin { + + @Unique + private List flywheel$blockEntities; + @Unique + private Level flywheel$level; + + @Inject(method = "addBlockEntity", at = @At("HEAD"), cancellable = true, require = 0) + private void flywheel$onAddBlockEntity(BlockEntity be, boolean cull, CallbackInfo ci) { + if (InstancedRenderRegistry.canInstance(be.getType())) { + if (flywheel$blockEntities == null) { + flywheel$blockEntities = new ArrayList<>(); + } + + if (flywheel$level == null) { + flywheel$level = be.getLevel(); + } + + // Collect BEs in a temporary list to avoid excessive synchronization in InstancedRenderDispatcher. + flywheel$blockEntities.add(be); + } + + if (InstancedRenderRegistry.shouldSkipRender(be)) { + ci.cancel(); + } + } + + @Inject(method = "build", at = @At("HEAD")) + private void flywheel$onBuild(CallbackInfoReturnable cir) { + if (flywheel$level == null || flywheel$blockEntities == null) { + return; + } + + InstancedRenderDispatcher.getBlockEntities(flywheel$level) + .queueAddAll(flywheel$blockEntities); + } +} diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 71d3f7495..149c7bd63 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -28,6 +28,7 @@ "instancemanage.InstanceRemoveMixin", "instancemanage.RenderChunkAccessor", "instancemanage.RenderChunkMixin", + "instancemanage.SodiumChunkRenderDataMixin", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin", "matrix.Matrix3fMixin",