From d53c9b0a32b6f6d1e91302a346832d23bb453862 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 16 Sep 2021 20:03:45 -0700 Subject: [PATCH] It renders stuff - The lightmap moved - Begin to move away from access transformers - .textureManager -> .getTextureManager() - RIP cutout, saw that coming --- build.gradle | 3 +- .../backend/instancing/InstanceWorld.java | 6 ++ .../backend/state/TextureRenderState.java | 2 +- .../flywheel/core/atlas/AtlasInfo.java | 2 +- .../core/crumbling/CrumblingRenderer.java | 7 +- .../flywheel/core/shader/WorldProgram.java | 2 +- .../mixin/CancelEntityRenderMixin.java | 31 +++--- .../mixin/CancelTileEntityRenderMixin.java | 3 - .../mixin/FastChunkProviderMixin.java | 94 ------------------- ...ldHookMixin.java => InstanceAddMixin.java} | 18 ++-- ...oveMixin.java => InstanceRemoveMixin.java} | 14 ++- .../flywheel/mixin/LevelRendererAccessor.java | 16 ++++ .../resources/META-INF/accesstransformer.cfg | 9 -- src/main/resources/flywheel.mixins.json | 8 +- 14 files changed, 71 insertions(+), 144 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java rename src/main/java/com/jozufozu/flywheel/mixin/{TileWorldHookMixin.java => InstanceAddMixin.java} (60%) rename src/main/java/com/jozufozu/flywheel/mixin/{TileRemoveMixin.java => InstanceRemoveMixin.java} (64%) create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/LevelRendererAccessor.java diff --git a/build.gradle b/build.gradle index 6497002e2..5942e4994 100644 --- a/build.gradle +++ b/build.gradle @@ -41,6 +41,7 @@ minecraft { property 'forge.logging.markers', '' property 'forge.logging.console.level', 'debug' property 'fml.earlyprogresswindow', 'false' + property 'mixin.debug.export', 'true' arg "-mixin.config=flywheel.mixins.json" @@ -100,7 +101,7 @@ dependencies { //implementation "org.joml:joml:1.10.1" - //annotationProcessor 'org.spongepowered:mixin:0.8.4:processor' + annotationProcessor 'org.spongepowered:mixin:0.8.4:processor' } // Example for how to get properties into the manifest for reading by the runtime.. diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java index 7321b7dce..994286d14 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -8,9 +8,11 @@ import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; +import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.renderer.ShaderInstance; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.block.entity.BlockEntity; @@ -99,6 +101,10 @@ public class InstanceWorld { public void renderLayer(RenderLayerEvent event) { event.type.setupRenderState(); + ShaderInstance shader = RenderSystem.getShader(); + if (shader != null) + shader.apply(); + materialManager.render(event.layer, event.viewProjection, event.camX, event.camY, event.camZ); event.type.clearRenderState(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java b/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java index 5e3063d93..b7c43aa75 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java +++ b/src/main/java/com/jozufozu/flywheel/backend/state/TextureRenderState.java @@ -34,7 +34,7 @@ public class TextureRenderState implements IRenderState { @Override public void bind() { unit.makeActive(); - Minecraft.getInstance().textureManager.bindForSetup(location); + Minecraft.getInstance().getTextureManager().bindForSetup(location); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java b/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java index 584d94a6a..afffa920a 100644 --- a/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java +++ b/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java @@ -16,7 +16,7 @@ public class AtlasInfo { private static final Map sheetData = new HashMap<>(); public static TextureAtlas getAtlas(ResourceLocation name) { - AbstractTexture texture = Minecraft.getInstance().textureManager.getTexture(name); + AbstractTexture texture = Minecraft.getInstance().getTextureManager().getTexture(name); if (texture instanceof TextureAtlas) return (TextureAtlas) texture; diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java index 55278e76f..04ebefdab 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java @@ -14,6 +14,7 @@ import com.jozufozu.flywheel.backend.material.MaterialManagerImpl; import com.jozufozu.flywheel.backend.state.RenderLayer; import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.event.ReloadRenderersEvent; +import com.jozufozu.flywheel.mixin.LevelRendererAccessor; import com.jozufozu.flywheel.util.Lazy; import com.jozufozu.flywheel.util.Pair; @@ -66,7 +67,7 @@ public class CrumblingRenderer { InstanceManager renderer = state.instanceManager; - TextureManager textureManager = Minecraft.getInstance().textureManager; + TextureManager textureManager = Minecraft.getInstance().getTextureManager(); Camera info = Minecraft.getInstance().gameRenderer.getMainCamera(); MaterialManagerImpl materials = state.materialManager; @@ -102,11 +103,11 @@ public class CrumblingRenderer { * Associate each breaking stage with a list of all tile entities at that stage. */ private static Int2ObjectMap> getActiveStageTiles(ClientLevel world) { - Long2ObjectMap> breakingProgressions = Minecraft.getInstance().levelRenderer.destructionProgress; Int2ObjectMap> breakingEntities = new Int2ObjectArrayMap<>(); - for (Long2ObjectMap.Entry> entry : breakingProgressions.long2ObjectEntrySet()) { + for (Long2ObjectMap.Entry> entry : ((LevelRendererAccessor) Minecraft.getInstance().levelRenderer).getDestructionProgress() + .long2ObjectEntrySet()) { BlockPos breakingPos = BlockPos.of(entry.getLongKey()); SortedSet progresses = entry.getValue(); diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java b/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java index a6b6a4037..6d065f21c 100644 --- a/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java +++ b/src/main/java/com/jozufozu/flywheel/core/shader/WorldProgram.java @@ -33,7 +33,7 @@ public class WorldProgram extends ExtensibleGlProgram { protected void registerSamplers() { uBlockAtlas = setSamplerBinding("uBlockAtlas", 0); - uLightMap = setSamplerBinding("uLightMap", 2); + uLightMap = setSamplerBinding("uLightMap", 1); } public void uploadViewProjection(Matrix4f viewProjection) { diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java index 3456690dd..1b45c59be 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java @@ -20,6 +20,7 @@ import net.minecraft.util.ClassInstanceMultiMap; @Mixin(LevelRenderer.class) public class CancelEntityRenderMixin { + // TODO: Don't use redirect @Group(name = "entityFilter", min = 1, max = 1) @Redirect(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientLevel;entitiesForRendering()Ljava/lang/Iterable;")) private Iterable filterEntities(ClientLevel world) { @@ -37,19 +38,19 @@ public class CancelEntityRenderMixin { return entities; } - @Group(name = "entityFilter") - @Redirect(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/ClassInstanceMultiMap;iterator()Ljava/util/Iterator;")) - private Iterator filterEntitiesOF(ClassInstanceMultiMap classInheritanceMultiMap) { - if (Backend.getInstance() - .canUseInstancing()) { - - ArrayList filtered = Lists.newArrayList(classInheritanceMultiMap); - - InstancedRenderRegistry r = InstancedRenderRegistry.getInstance(); - filtered.removeIf(r::shouldSkipRender); - - return filtered.iterator(); - } - return classInheritanceMultiMap.iterator(); - } +// @Group(name = "entityFilter") +// @Redirect(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/ClassInstanceMultiMap;iterator()Ljava/util/Iterator;")) +// private Iterator filterEntitiesOF(ClassInstanceMultiMap classInheritanceMultiMap) { +// if (Backend.getInstance() +// .canUseInstancing()) { +// +// ArrayList filtered = Lists.newArrayList(classInheritanceMultiMap); +// +// InstancedRenderRegistry r = InstancedRenderRegistry.getInstance(); +// filtered.removeIf(r::shouldSkipRender); +// +// return filtered.iterator(); +// } +// return classInheritanceMultiMap.iterator(); +// } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java index 4cc209091..21b6bc0ac 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java @@ -12,10 +12,7 @@ import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher; import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; -@OnlyIn(Dist.CLIENT) @Mixin(ChunkRenderDispatcher.CompiledChunk.class) public class CancelTileEntityRenderMixin { diff --git a/src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java deleted file mode 100644 index 27a10a6a2..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java +++ /dev/null @@ -1,94 +0,0 @@ -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.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.jozufozu.flywheel.backend.Backend; - -import net.minecraft.client.multiplayer.ClientChunkCache; -import net.minecraft.client.multiplayer.ClientLevel; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.world.level.chunk.ChunkBiomeContainer; -import net.minecraft.world.level.chunk.ChunkSource; -import net.minecraft.world.level.chunk.LevelChunk; -import net.minecraft.world.level.chunk.ChunkStatus; -import net.minecraft.world.level.chunk.ChunkAccess; - -import java.util.BitSet; - -@Mixin(ClientChunkCache.class) -public abstract class FastChunkProviderMixin extends ChunkSource { - - @Shadow - @Final - private ClientLevel level; - @Unique - private int lastX; - @Unique - private int lastZ; - - @Unique - private ChunkAccess lastChunk; - - @Inject(method = "getChunk", - at = @At("HEAD"), - cancellable = true) - public void returnCachedChunk(int x, int z, ChunkStatus status, boolean create, CallbackInfoReturnable cir) { - if (Backend.getInstance().chunkCachingEnabled && status.isOrAfter(ChunkStatus.FULL)) { - synchronized (level) { - if (lastChunk != null && x == lastX && z == lastZ) { - cir.setReturnValue(lastChunk); - } - } - } - } - - @Inject(method = "getChunk", - at = @At("RETURN")) - public void cacheChunk(int x, int z, ChunkStatus status, boolean create, CallbackInfoReturnable cir) { - if (Backend.getInstance().chunkCachingEnabled && status.isOrAfter(ChunkStatus.FULL)) { - synchronized (level) { - lastChunk = cir.getReturnValue(); - lastX = x; - lastZ = z; - } - } - } - - @Inject(method = "drop", at = @At("HEAD")) - public void invalidateOnDrop(int x, int z, CallbackInfo ci) { - if (Backend.getInstance().chunkCachingEnabled) { - synchronized (level) { - if (x == lastX && z == lastZ) lastChunk = null; - } - } - } - - @Inject(method = "replaceWithPacketData", at = @At("HEAD")) - public void invalidateOnPacket(int x, int z, ChunkBiomeContainer p_171618_, FriendlyByteBuf p_171619_, CompoundTag p_171620_, BitSet p_171621_, CallbackInfoReturnable cir) { - if (Backend.getInstance().chunkCachingEnabled) { - synchronized (level) { - if (x == lastX && z == lastZ) lastChunk = null; - } - } - } - - @Redirect(method = "isTickingChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientChunkProvider;hasChunk(II)Z")) - public boolean redirectTicking(ClientChunkCache clientChunkProvider, int x, int z) { - if (Backend.getInstance().chunkCachingEnabled) { - synchronized (level) { - if (lastChunk != null && x == lastX && z == lastZ) return true; - } - } - - return clientChunkProvider.hasChunk(x, z); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/InstanceAddMixin.java similarity index 60% rename from src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/InstanceAddMixin.java index f3d0b1a61..dabee25e5 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/InstanceAddMixin.java @@ -9,26 +9,24 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; -import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.chunk.LevelChunk; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @OnlyIn(Dist.CLIENT) -@Mixin(value = LevelChunk.class, priority = 1100) // this and create.mixins.json have high priority to load after Performant -public class TileWorldHookMixin { +@Mixin(LevelChunk.class) +public class InstanceAddMixin { @Shadow @Final Level level; - @Inject(at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/level/block/entity/BlockEntity;clearRemoved()V"), - method = "setBlockEntity") - private void onAddTile(BlockEntity te, CallbackInfo ci) { - if (level.isClientSide) { - InstancedRenderDispatcher.getTiles(level) - .queueAdd(te); - } + @Inject(method = "setBlockEntity", + at = @At(value = "INVOKE_ASSIGN", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")) + private void tileAdded(BlockEntity be, CallbackInfo ci) { + if (level.isClientSide) InstancedRenderDispatcher.getTiles(this.level) + .add(be); } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/TileRemoveMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/InstanceRemoveMixin.java similarity index 64% rename from src/main/java/com/jozufozu/flywheel/mixin/TileRemoveMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/InstanceRemoveMixin.java index 491079b29..188019ec5 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/TileRemoveMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/InstanceRemoveMixin.java @@ -15,15 +15,25 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.Level; @Mixin(BlockEntity.class) -public class TileRemoveMixin { +public class InstanceRemoveMixin { @Shadow @Nullable protected Level level; @Inject(at = @At("TAIL"), method = "setRemoved") - private void onRemove(CallbackInfo ci) { + private void removeInstance(CallbackInfo ci) { if (level instanceof ClientLevel) InstancedRenderDispatcher.getTiles(this.level) .remove((BlockEntity) (Object) this); } + +// /** +// * Don't do this. +// * It can cause infinite loops if an instance class tries to access another tile entity in its constructor. +// */ +// @Inject(at = @At("TAIL"), method = "clearRemoved") +// private void addInstance(CallbackInfo ci) { +// if (level.isClientSide) InstancedRenderDispatcher.getTiles(this.level) +// .add((BlockEntity) (Object) this); +// } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererAccessor.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererAccessor.java new file mode 100644 index 000000000..e348cebbe --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererAccessor.java @@ -0,0 +1,16 @@ +package com.jozufozu.flywheel.mixin; + +import java.util.SortedSet; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import net.minecraft.client.renderer.LevelRenderer; +import net.minecraft.server.level.BlockDestructionProgress; + +@Mixin(LevelRenderer.class) +public interface LevelRendererAccessor { + @Accessor + Long2ObjectMap> getDestructionProgress(); +} diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 44cf85f73..d4a263750 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -2,12 +2,6 @@ # Don't jitter when game is paused public net.minecraft.client.Minecraft f_91013_ #renderPartialTicksPaused -# Expose fog state for use in flywheel shaders -public com.mojang.blaze3d.platform.GlStateManager$FogState -public com.mojang.blaze3d.platform.GlStateManager f_84068_ #FOG -public com.mojang.blaze3d.platform.GlStateManager$BooleanState -public com.mojang.blaze3d.platform.GlStateManager$BooleanState f_84586_ #field_179201_b - # For uploading matrices as vertex attributes. public com.mojang.math.Matrix3f f_8134_ #a00 public com.mojang.math.Matrix3f f_8135_ #a01 @@ -35,6 +29,3 @@ public com.mojang.math.Matrix4f f_27615_ #a30 public com.mojang.math.Matrix4f f_27616_ #a31 public com.mojang.math.Matrix4f f_27617_ #a32 public com.mojang.math.Matrix4f f_27618_ #a33 - -# For fancy breaking overlay rendering -public net.minecraft.client.renderer.LevelRenderer f_109409_ #blockBreakingProgressions \ No newline at end of file diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 15f9272c1..803cf7b14 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -10,15 +10,15 @@ "FixFabulousDepthMixin", "RenderHooksMixin", "ShaderCloseMixin", - "TileRemoveMixin", - "TileWorldHookMixin", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin", - "FastChunkProviderMixin" + "LevelRendererAccessor", + "InstanceAddMixin", + "InstanceRemoveMixin" ], "injectors": { - "defaultRequire": 0 + "defaultRequire": 1 } }