From 94e83a10d17d2c915340274e6c443b8ce134bd71 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Tue, 9 Nov 2021 16:48:54 -0800 Subject: [PATCH] TextureBinder for dealing with vanilla RenderTypes --- .../flywheel/backend/gl/error/GlError.java | 3 +- .../backend/instancing/InstanceWorld.java | 7 +-- .../flywheel/mixin/ShaderInstanceMixin.java | 59 +++++++++++++++++++ .../jozufozu/flywheel/util/TextureBinder.java | 30 ++++++++++ src/main/resources/flywheel.mixins.json | 3 +- 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/TextureBinder.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java index 2461fad51..3df53b71b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java @@ -39,9 +39,10 @@ public enum GlError { } public static void pollAndThrow(Supplier context) { + // TODO: build flag? to enable or disable this function GlError poll = GlError.poll(); if (poll != null) { - throw new GlException(poll, context.get()); + //throw new GlException(poll, context.get()); } } } 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 9c9d94e1e..0e454d2f5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -9,11 +9,10 @@ import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.util.ChunkIter; -import com.mojang.blaze3d.systems.RenderSystem; +import com.jozufozu.flywheel.util.TextureBinder; 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; @@ -103,9 +102,7 @@ public class InstanceWorld { public void renderLayer(RenderLayerEvent event) { event.type.setupRenderState(); - ShaderInstance shader = RenderSystem.getShader(); - if (shader != null) - shader.apply(); + TextureBinder.bindActiveTextures(); materialManager.render(event.layer, event.viewProjection, event.camX, event.camY, event.camZ); diff --git a/src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java new file mode 100644 index 000000000..9778874cd --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/ShaderInstanceMixin.java @@ -0,0 +1,59 @@ +package com.jozufozu.flywheel.mixin; + +import java.util.List; +import java.util.Map; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import com.jozufozu.flywheel.util.TextureBinder; +import com.mojang.blaze3d.pipeline.RenderTarget; +import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.systems.RenderSystem; + +import net.minecraft.client.renderer.ShaderInstance; +import net.minecraft.client.renderer.texture.AbstractTexture; + +@Mixin(ShaderInstance.class) +public class ShaderInstanceMixin implements TextureBinder { + @Shadow + @Final + private List samplerLocations; + + @Shadow + @Final + private List samplerNames; + + @Shadow + @Final + private Map samplerMap; + + @Override + public void bind() { + int i = GlStateManager._getActiveTexture(); + + for(int j = 0; j < samplerLocations.size(); ++j) { + String s = samplerNames.get(j); + if (samplerMap.get(s) != null) { + RenderSystem.activeTexture('\u84c0' + j); + RenderSystem.enableTexture(); + Object object = this.samplerMap.get(s); + int l = -1; + if (object instanceof RenderTarget) { + l = ((RenderTarget)object).getColorTextureId(); + } else if (object instanceof AbstractTexture) { + l = ((AbstractTexture)object).getId(); + } else if (object instanceof Integer) { + l = (Integer)object; + } + + if (l != -1) { + RenderSystem.bindTexture(l); + } + } + } + + GlStateManager._activeTexture(i); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java b/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java new file mode 100644 index 000000000..2c7e816de --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/TextureBinder.java @@ -0,0 +1,30 @@ +package com.jozufozu.flywheel.util; + +import com.mojang.blaze3d.systems.RenderSystem; + +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.ShaderInstance; + +/** + * This is a silly hack that's needed because flywheel does things too different from vanilla. + * + *

+ * When a {@link RenderType} is setup, the associated textures are passed to the active ShaderInstance, and properly + * bound later on when {@link ShaderInstance#apply()} is called. + * This interface (and {@link com.jozufozu.flywheel.mixin.ShaderInstanceMixin mixin} binds textures to opengl + * without binding the shader. + *

+ */ +public interface TextureBinder { + + /** + * Call this after calling {@link RenderType#setupRenderState()}. + */ + static void bindActiveTextures() { + TextureBinder shader = (TextureBinder) RenderSystem.getShader(); + if (shader != null) + shader.bind(); + } + + void bind(); +} diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 472139c7f..98248ac9c 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -18,7 +18,8 @@ "InstanceAddMixin", "InstanceRemoveMixin", "LeakChunkStorageArrayMixin", - "PausedPartialTickAccessor" + "PausedPartialTickAccessor", + "ShaderInstanceMixin" ], "mixins": [ "matrix.Matrix3fMixin",