Controllable icon indicating copy to clipboard operation
Controllable copied to clipboard

🐛 Fixed crash with OptiFine

Open KaiAF opened this issue 1 year ago • 7 comments

The cause of the crash was when trying to inject the "controllableLastRender" method, it was looking for a specific set of arguments while without OptiFine it worked, but with it, it would crash. This is because in OptiFine it adds two new fields inside the render method. Seen below:

@@ -899,24 +1041,33 @@
                RenderSystem.disableBlend();
                RenderSystem.disableDepthTest();
                RenderSystem.resetTextureMatrix();
                this.postEffect.process(p_109094_);
+               RenderSystem.enableTexture();
             }
 
             this.minecraft.getMainRenderTarget().bindWrite(true);
+         } else {
+            RenderSystem.viewport(0, 0, this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
          }
 
          Window window = this.minecraft.getWindow();
          RenderSystem.clear(256, Minecraft.ON_OSX);
-         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, 21000.0F);
+         float guiFarPlane = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? Reflector.ForgeHooksClient_getGuiFarPlane.callFloat() : 21000.0F; // optifine
+         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, guiFarPlane);
          RenderSystem.setProjectionMatrix(matrix4f, VertexSorting.ORTHOGRAPHIC_Z);
          PoseStack posestack = RenderSystem.getModelViewStack();
          posestack.pushPose();
          posestack.setIdentity();
-         posestack.translate(0.0F, 0.0F, -11000.0F);
+         float guiOffsetZ = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? 1000.0F - guiFarPlane : -11000.0F;
+         posestack.translate(0.0, 0.0, guiOffsetZ); // optifine
          RenderSystem.applyModelViewMatrix();
          Lighting.setupFor3DItems();
          GuiGraphics guigraphics = new GuiGraphics(this.minecraft, this.renderBuffers.bufferSource());
+         if (this.lightTexture.isCustom()) {
+            this.lightTexture.setAllowed(false);
+         }
+
          if (p_109096_ && this.minecraft.level != null) {
             this.minecraft.getProfiler().popPush("gui");
             if (this.minecraft.player != null) {
                float f = Mth.lerp(p_109094_, this.minecraft.player.oSpinningEffectIntensity, this.minecraft.player.spinningEffectIntensity);

image

Fixes #433, fixes #418, fixes #472, fixes #477

KaiAF avatar Oct 07 '23 19:10 KaiAF

@MrCrayfish can you approve this request?

1nd1r4 avatar Dec 18 '23 14:12 1nd1r4

The cause of the crash was when trying to inject the "controllableLastRender" method, it was looking for a specific set of arguments while without OptiFine it worked, but with it, it would crash. This is because in OptiFine it adds two new fields inside the render method. Seen below:

@@ -899,24 +1041,33 @@
                RenderSystem.disableBlend();
                RenderSystem.disableDepthTest();
                RenderSystem.resetTextureMatrix();
                this.postEffect.process(p_109094_);
+               RenderSystem.enableTexture();
             }
 
             this.minecraft.getMainRenderTarget().bindWrite(true);
+         } else {
+            RenderSystem.viewport(0, 0, this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
          }
 
          Window window = this.minecraft.getWindow();
          RenderSystem.clear(256, Minecraft.ON_OSX);
-         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, 21000.0F);
+         float guiFarPlane = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? Reflector.ForgeHooksClient_getGuiFarPlane.callFloat() : 21000.0F; // optifine
+         Matrix4f matrix4f = (new Matrix4f()).setOrtho(0.0F, (float)((double)window.getWidth() / window.getGuiScale()), (float)((double)window.getHeight() / window.getGuiScale()), 0.0F, 1000.0F, guiFarPlane);
          RenderSystem.setProjectionMatrix(matrix4f, VertexSorting.ORTHOGRAPHIC_Z);
          PoseStack posestack = RenderSystem.getModelViewStack();
          posestack.pushPose();
          posestack.setIdentity();
-         posestack.translate(0.0F, 0.0F, -11000.0F);
+         float guiOffsetZ = Reflector.ForgeHooksClient_getGuiFarPlane.exists() ? 1000.0F - guiFarPlane : -11000.0F;
+         posestack.translate(0.0, 0.0, guiOffsetZ); // optifine
          RenderSystem.applyModelViewMatrix();
          Lighting.setupFor3DItems();
          GuiGraphics guigraphics = new GuiGraphics(this.minecraft, this.renderBuffers.bufferSource());
+         if (this.lightTexture.isCustom()) {
+            this.lightTexture.setAllowed(false);
+         }
+
          if (p_109096_ && this.minecraft.level != null) {
             this.minecraft.getProfiler().popPush("gui");
             if (this.minecraft.player != null) {
                float f = Mth.lerp(p_109094_, this.minecraft.player.oSpinningEffectIntensity, this.minecraft.player.spinningEffectIntensity);

image

Fixes #433, fixes #418, fixes #472

Can you just upload your version somewhere?

nightinnn1 avatar Jan 21 '24 12:01 nightinnn1

@nightinnn1, sure. I could not figure out to publish it on GitHub since the build script is using a lot of secret variables. So, I just uploaded it to my file server.

Forge download, 1.20.1 -> https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3.jar

KaiAF avatar Jan 21 '24 13:01 KaiAF

@nightinnn1, sure. I could not figure out to publish it on GitHub since the build script is using a lot of secret variables. So, I just uploaded it to my file server.

Forge download, 1.20.1 -> https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3.jar

Your mode version crashing even without optifine :(

nightinnn1 avatar Jan 21 '24 14:01 nightinnn1

sorry, provided wrong link try https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3-release.jar

KaiAF avatar Jan 21 '24 14:01 KaiAF

sorry, provided wrong link try https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3-release.jar

It works. kudos.

While trying other setting mods. I found out embeddium+oculus works better and faster than optifine (for me at least). I tried running your hotfix but unfortunately it crashes with embeddium+oculus.

Do you think you could look into the argument for me? It only crashes when the setting mod has a shader loader. I tried with other setting mod (without shader loader) and it didn't crash.

Musky420 avatar Jan 24 '24 11:01 Musky420

sorry, provided wrong link try https://textures.livzmc.net/controllable/controllable-forge-1.20.1-0.20.3-release.jar

It works. kudos.

While trying other setting mods. I found out embeddium+oculus works better and faster than optifine (for me at least). I tried running your hotfix but unfortunately it crashes with embeddium+oculus.

Do you think you could look into the argument for me? It only crashes when the setting mod has a shader loader. I tried with other setting mod (without shader loader) and it didn't crash.

Hi, is there any way to use Optifine (with all my shaders, texture packs etc) with controllable?

PrometherioNPL avatar Apr 05 '24 15:04 PrometherioNPL