From 9f019fc72fb32a1c9ffa2e1e176a2b3ba79241c0 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 23 Nov 2023 22:48:29 -0800 Subject: [PATCH] Loading visualization - Fix up/comment out some touchy mixins. - Get chests to render. - There's something wrong with the projection matrix. - Add comment to DebugMemoryBlockImpl's stack walker usage. --- .../java/com/jozufozu/flywheel/Flywheel.java | 11 +++++-- .../lib/memory/DebugMemoryBlockImpl.java | 6 ++++ .../flywheel/mixin/LevelRendererMixin.java | 2 +- .../mixin/fix/FixNormalScalingMixin.java | 8 ++--- .../visualmanage/ChunkRebuildHooksMixin.java | 31 ++++++++++++++++--- 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index f9065081d..f249a6be4 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -49,6 +49,7 @@ import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.IExtensionPoint; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @@ -116,6 +117,10 @@ public class Flywheel { ShadersModHandler.init(); + modEventBus.addListener(Flywheel::lateInit); + } + + private static void lateInit(final FMLClientSetupEvent event) { VertexTypes.init(); InstanceTypes.init(); Materials.init(); @@ -124,12 +129,12 @@ public class Flywheel { MaterialIndices.init(); VanillaVisuals.init(); + + RegistryImpl.freezeAll(); + IdRegistryImpl.freezeAll(); } private static void setup(final FMLCommonSetupEvent event) { - RegistryImpl.freezeAll(); - IdRegistryImpl.freezeAll(); - ArgumentTypeInfos.registerByClass(BackendArgument.class, SingletonArgumentInfo.contextFree(() -> BackendArgument.INSTANCE)); } diff --git a/src/main/java/com/jozufozu/flywheel/lib/memory/DebugMemoryBlockImpl.java b/src/main/java/com/jozufozu/flywheel/lib/memory/DebugMemoryBlockImpl.java index fb8bd8571..004a4ba62 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/memory/DebugMemoryBlockImpl.java +++ b/src/main/java/com/jozufozu/flywheel/lib/memory/DebugMemoryBlockImpl.java @@ -23,6 +23,12 @@ class DebugMemoryBlockImpl extends MemoryBlockImpl { } static StackWalker.StackFrame[] getStackTrace() { + // skip 4 frames to get to the caller: + // - this method + // - DebugMemoryBlockImpl::new + // - DebugMemoryBlockImpl::malloc/calloc + // - MemoryBlock::malloc/calloc + // - {caller is here} return StackWalker.getInstance().walk(s -> s.skip(4).toArray(StackWalker.StackFrame[]::new)); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java index 25caa920d..12419bcad 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java @@ -128,7 +128,7 @@ public class LevelRendererMixin { flywheel$dispatch(RenderStage.AFTER_FINAL_END_BATCH); } - @Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;renderChunkLayer(Lnet/minecraft/client/renderer/RenderType;Lcom/mojang/blaze3d/vertex/PoseStack;DDDLcom/mojang/math/Matrix4f;)V", ordinal = 6, shift = Shift.AFTER)) + @Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;renderChunkLayer(Lnet/minecraft/client/renderer/RenderType;Lcom/mojang/blaze3d/vertex/PoseStack;DDDLorg/joml/Matrix4f;)V", ordinal = 6, shift = Shift.AFTER)) private void flywheel$onStage$afterTranslucentTerrain(CallbackInfo ci) { flywheel$dispatch(RenderStage.AFTER_TRANSLUCENT_TERRAIN); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/fix/FixNormalScalingMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/fix/FixNormalScalingMixin.java index 144d6efa9..aad3d8eb2 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/fix/FixNormalScalingMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/fix/FixNormalScalingMixin.java @@ -17,10 +17,10 @@ public class FixNormalScalingMixin { * applied, which negates the matrix again, resulting in the matrix being the * same as in the beginning. */ - @Inject(method = "scale(FFF)V", at = @At(value = "INVOKE", target = "Lcom/mojang/math/Matrix3f;mul(F)V", shift = Shift.AFTER), cancellable = true) - private void flywheel$returnAfterNegate(float x, float y, float z, CallbackInfo ci) { - ci.cancel(); - } +// @Inject(method = "scale(FFF)V", at = @At(value = "INVOKE", target = "Lcom/mojang/math/Matrix3f;mul(F)V", shift = Shift.AFTER), cancellable = true) +// private void flywheel$returnAfterNegate(float x, float y, float z, CallbackInfo ci) { +// ci.cancel(); +// } /** * Minecraft takes the inverse cube root of the product of all scales to provide a diff --git a/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java index 9a6e092cf..90874ba5a 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java @@ -5,18 +5,39 @@ import java.util.Set; 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.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.impl.visualization.VisualizationHelper; +import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher; +import net.minecraft.client.renderer.chunk.RenderChunkRegion; +import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.entity.BlockEntity; -@Mixin(targets = "net.minecraft.client.renderer.chunk.SectionRenderDispatcher$RenderSection$RebuildTask") +@Mixin(targets = "net.minecraft.client.renderer.chunk.ChunkRenderDispatcher$RenderChunk$RebuildTask") public class ChunkRebuildHooksMixin { - @Inject(method = "handleBlockEntity(Lnet/minecraft/client/renderer/chunk/SectionRenderDispatcher$RenderSection$RebuildTask$CompileResults;Lnet/minecraft/world/level/block/entity/BlockEntity;)V", at = @At("HEAD"), cancellable = true) - private void flywheel$tryAddBlockEntity(Object pCompileResults, E pBlockEntity, CallbackInfo ci) { - if (VisualizationHelper.tryAddBlockEntity(pBlockEntity)) { - ci.cancel(); + +// FIXME: use this instead of the redirect if there's a clean way to reference the CompileResults +// @Inject(method = "handleBlockEntity(Lnet/minecraft/client/renderer/chunk/ChunkRenderDispatcher$RenderChunk$RebuildTask$CompileResults;Lnet/minecraft/world/level/block/entity/BlockEntity;)V", at = @At("HEAD"), cancellable = true) +// private void flywheel$tryAddBlockEntity(Object pCompileResults, E pBlockEntity, CallbackInfo ci) { +// if (VisualizationHelper.tryAddBlockEntity(pBlockEntity)) { +// ci.cancel(); +// } +// } + + @Redirect(method = "compile", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/chunk/RenderChunkRegion;getBlockEntity(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/level/block/entity/BlockEntity;")) + private BlockEntity flywheel$interceptGetBlockEntity(RenderChunkRegion region, BlockPos pos) { + BlockEntity blockEntity = region.getBlockEntity(pos); + + if (blockEntity == null) { + return null; } + + if (VisualizationHelper.tryAddBlockEntity(blockEntity)) { + return null; + } + + return blockEntity; } }