From 552c132512254047d41aeeb1e6c99ef71f5772d8 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 21 Sep 2022 15:34:58 -0700 Subject: [PATCH] Porting from all directions - Diagonal-port restore state changes from 1.18/next - Back-port buffer uploader changes and RenderLayerEvent dispatch point change from 1.19/dev - Make CrumblingRenderer return earlier if there is nothing to render - Bump version --- gradle.properties | 2 +- .../flywheel/backend/gl/GlStateTracker.java | 29 +++++++++---------- .../instancing/InstancingEngine.java | 5 ++++ .../core/crumbling/CrumblingRenderer.java | 18 +++++++++--- .../flywheel/mixin/BufferUploaderMixin.java | 29 ------------------- .../flywheel/mixin/LevelRendererMixin.java | 23 ++------------- src/main/resources/flywheel.mixins.json | 1 - 7 files changed, 37 insertions(+), 70 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java diff --git a/gradle.properties b/gradle.properties index e9ca1f061..d01adbfc2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs = -Xmx3G org.gradle.daemon = false # mod version info -mod_version = 0.6.5 +mod_version = 0.6.6 artifact_minecraft_version = 1.18.2 minecraft_version = 1.18.2 diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java index eec0a4cf8..e63897fc6 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java @@ -7,13 +7,12 @@ import com.mojang.blaze3d.platform.GlStateManager; * Tracks bound buffers/vbos because GlStateManager doesn't do that for us. */ public class GlStateTracker { - - private static final int[] buffers = new int[GlBufferType.values().length]; + private static final int[] BUFFERS = new int[GlBufferType.values().length]; private static int vao; private static int program; public static int getBuffer(GlBufferType type) { - return buffers[type.ordinal()]; + return BUFFERS[type.ordinal()]; } public static int getVertexArray() { @@ -24,36 +23,36 @@ public class GlStateTracker { return program; } - public static void _setBuffer(GlBufferType type, int buffer) { - buffers[type.ordinal()] = buffer; - } - - public static void _setProgram(int id) { - program = id; + public static void _setBuffer(GlBufferType type, int id) { + BUFFERS[type.ordinal()] = id; } public static void _setVertexArray(int id) { vao = id; } + public static void _setProgram(int id) { + program = id; + } + public static State getRestoreState() { - return new State(buffers.clone(), vao, program); + return new State(BUFFERS.clone(), vao, program); } public static record State(int[] buffers, int vao, int program) { public void restore() { + if (vao != GlStateTracker.vao) { + GlStateManager._glBindVertexArray(vao); + } + GlBufferType[] values = GlBufferType.values(); for (int i = 0; i < values.length; i++) { - if (buffers[i] != GlStateTracker.buffers[i]) { + if (buffers[i] != GlStateTracker.BUFFERS[i]) { GlStateManager._glBindBuffer(values[i].glEnum, buffers[i]); } } - if (vao != GlStateTracker.vao) { - GlStateManager._glBindVertexArray(vao); - } - if (program != GlStateTracker.program) { GlStateManager._glUseProgram(program); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index e3efc8a71..5e8c30a54 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -10,6 +10,7 @@ import javax.annotation.Nullable; import com.jozufozu.flywheel.api.MaterialGroup; import com.jozufozu.flywheel.backend.RenderLayer; +import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.instancing.Engine; import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.core.compile.ProgramCompiler; @@ -73,6 +74,8 @@ public class InstancingEngine

implements Engine { */ @Override public void render(TaskEngine taskEngine, RenderLayerEvent event) { + GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); + double camX; double camY; double camZ; @@ -92,6 +95,8 @@ public class InstancingEngine

implements Engine { } getGroupsToRender(event.getLayer()).forEach(group -> group.render(viewProjection, camX, camY, camZ, event.getLayer())); + + restoreState.restore(); } private Stream> getGroupsToRender(@Nullable RenderLayer layer) { 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 e9aca9ff3..e15e91a4e 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.SortedSet; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.GlTextureUnit; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.SerialTaskEngine; @@ -16,6 +17,7 @@ import com.jozufozu.flywheel.mixin.LevelRendererAccessor; import com.jozufozu.flywheel.util.Lazy; import com.jozufozu.flywheel.util.Pair; import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -30,6 +32,7 @@ import net.minecraft.client.resources.model.ModelBakery; import net.minecraft.core.BlockPos; import net.minecraft.server.level.BlockDestructionProgress; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -54,13 +57,20 @@ public class CrumblingRenderer { INVALIDATOR = state.second(); } - public static void renderBreaking(RenderLayerEvent event) { - if (!Backend.canUseInstancing(event.getWorld())) return; - - Int2ObjectMap> activeStages = getActiveStageBlockEntities(event.getWorld()); + public static void render(ClientLevel level, Camera camera, PoseStack stack) { + if (!Backend.canUseInstancing(level)) return; + Int2ObjectMap> activeStages = getActiveStageBlockEntities(level); if (activeStages.isEmpty()) return; + Vec3 cameraPos = camera.getPosition(); + + GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); + CrumblingRenderer.renderBreaking(activeStages, new RenderLayerEvent(level, null, stack, null, cameraPos.x, cameraPos.y, cameraPos.z)); + restoreState.restore(); + } + + private static void renderBreaking(Int2ObjectMap> activeStages, RenderLayerEvent event) { State state = STATE.get(); InstanceManager instanceManager = state.instanceManager; InstancingEngine materials = state.materialManager; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java deleted file mode 100644 index 7cd63581c..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jozufozu.flywheel.mixin; - -import javax.annotation.Nullable; - -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; - -import com.jozufozu.flywheel.backend.gl.GlStateTracker; -import com.mojang.blaze3d.vertex.BufferUploader; -import com.mojang.blaze3d.vertex.VertexFormat; - -@Mixin(BufferUploader.class) -public class BufferUploaderMixin { - - @Shadow - @Nullable - private static VertexFormat lastFormat; - - @Inject(method = "reset", at = @At("HEAD")) - private static void stopBufferUploaderFromClearingBufferStateIfNothingIsBound(CallbackInfo ci) { - // Trust our tracker over BufferUploader's. - if (GlStateTracker.getVertexArray() == 0) { - lastFormat = null; - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java index 497a81ee5..f3838d4bc 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java @@ -10,13 +10,11 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; -import com.mojang.blaze3d.vertex.BufferUploader; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; @@ -30,7 +28,6 @@ import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.culling.Frustum; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.Vec3; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.MinecraftForge; @@ -58,7 +55,7 @@ public class LevelRendererMixin { * 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/client/renderer/ShaderInstance;clear()V"), method = "renderChunkLayer") + @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; @@ -74,18 +71,11 @@ public class LevelRendererMixin { flywheel$renderLayer(type, stack, camX, camY, camZ); } flywheel$LayerRendered = false; - BufferUploader.reset(); } @Unique private void flywheel$renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ) { - RenderBuffers renderBuffers = this.renderBuffers; - - GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); - MinecraftForge.EVENT_BUS.post(new RenderLayerEvent(level, type, stack, renderBuffers, camX, camY, camZ)); - - restoreState.restore(); } @Inject(at = @At("TAIL"), method = "allChanged") @@ -95,17 +85,10 @@ public class LevelRendererMixin { MinecraftForge.EVENT_BUS.post(new ReloadRenderersEvent(level)); } - @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;checkPoseStack(Lcom/mojang/blaze3d/vertex/PoseStack;)V", ordinal = 2 // after the game renders the breaking overlay normally ), method = "renderLevel") - private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera info, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) { - if (!Backend.isOn()) return; - - Vec3 cameraPos = info.getPosition(); - - GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); - CrumblingRenderer.renderBreaking(new RenderLayerEvent(level, null, stack, null, cameraPos.x, cameraPos.y, cameraPos.z)); - restoreState.restore(); + private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) { + CrumblingRenderer.render(level, camera, stack); } // Instancing diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index cccc7ef58..7a674f5b3 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -8,7 +8,6 @@ "BlockEntityRenderDispatcherAccessor", "BlockEntityTypeMixin", "BufferBuilderMixin", - "BufferUploaderMixin", "CameraMixin", "ClientLevelMixin", "ChunkRebuildHooksMixin",