From 11616a0b1619a358729f9630f7dd55fe00466748 Mon Sep 17 00:00:00 2001 From: JozsefA Date: Sat, 13 Feb 2021 16:26:34 -0800 Subject: [PATCH] avoid 'glGet's at all costs --- .../mixin/FogColorTrackerMixin.java | 20 +++++++++++++++++++ .../render/backend/gl/BasicProgram.java | 3 +-- .../foundation/render/backend/gl/GlFog.java | 20 +++++++++++++++++-- .../resources/META-INF/accesstransformer.cfg | 8 +++++++- src/main/resources/create.mixins.json | 9 ++++++++- 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/mixin/FogColorTrackerMixin.java diff --git a/src/main/java/com/simibubi/create/foundation/mixin/FogColorTrackerMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/FogColorTrackerMixin.java new file mode 100644 index 000000000..599cd50c7 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/mixin/FogColorTrackerMixin.java @@ -0,0 +1,20 @@ +package com.simibubi.create.foundation.mixin; + +import com.mojang.blaze3d.platform.GlStateManager; +import com.simibubi.create.foundation.render.backend.gl.GlFog; +import org.lwjgl.opengl.GL11; +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.callback.CallbackInfo; + +@Mixin(GlStateManager.class) +public class FogColorTrackerMixin { + + @Inject(at = @At("TAIL"), method = "fog") + private static void copyFogColor(int pname, float[] params, CallbackInfo ci) { + if (pname == GL11.GL_FOG_COLOR) { + GlFog.FOG_COLOR = params; + } + } +} diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/gl/BasicProgram.java b/src/main/java/com/simibubi/create/foundation/render/backend/gl/BasicProgram.java index c471945c2..c75fa89eb 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/gl/BasicProgram.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/gl/BasicProgram.java @@ -47,8 +47,7 @@ public class BasicProgram extends GlProgram { GL20.glUniform3f(uCameraPos, camX, camY, camZ); GL20.glUniform2f(uFogRange, GlFog.getFogStart(), GlFog.getFogEnd()); - GL20.glGetFloatv(GL20.GL_FOG_COLOR, Backend.VEC4_BUFFER); - GL20.glUniform4fv(uFogColor, Backend.VEC4_BUFFER); + GL20.glUniform4fv(uFogColor, GlFog.FOG_COLOR); } protected static void uploadMatrixUniform(int uniform, Matrix4f mat) { diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlFog.java b/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlFog.java index 29ed4432f..220abfd1b 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlFog.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlFog.java @@ -1,13 +1,29 @@ package com.simibubi.create.foundation.render.backend.gl; +import com.mojang.blaze3d.platform.GlStateManager; +import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL20; public class GlFog { + public static float[] FOG_COLOR = new float[] {0, 0, 0, 0}; + + public static boolean fogEnabled() { + return GlStateManager.FOG.field_179049_a.field_179201_b; + } + + public static int getFogMode() { + return GlStateManager.FOG.field_179047_b; + } + + public static float getFogDensity() { + return GlStateManager.FOG.field_179048_c; + } + public static float getFogEnd() { - return GL20.glGetFloat(GL20.GL_FOG_END); + return GlStateManager.FOG.field_179046_e; } public static float getFogStart() { - return GL20.glGetFloat(GL20.GL_FOG_START); + return GlStateManager.FOG.field_179045_d; } } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 440395e69..c03cd0bd8 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -32,4 +32,10 @@ public net.minecraft.client.Minecraft field_193996_ah #renderPartialTicksPaused # Functions needed to setup a projection matrix public net.minecraft.client.renderer.GameRenderer field_78529_t #rendererUpdateCount public net.minecraft.client.renderer.GameRenderer func_228380_a_(Lcom/mojang/blaze3d/matrix/MatrixStack;F)V #bobViewWhenHurt -public net.minecraft.client.renderer.GameRenderer func_228383_b_(Lcom/mojang/blaze3d/matrix/MatrixStack;F)V #bobView \ No newline at end of file +public net.minecraft.client.renderer.GameRenderer func_228383_b_(Lcom/mojang/blaze3d/matrix/MatrixStack;F)V #bobView + +# Expose fog state to the public +public com.mojang.blaze3d.platform.GlStateManager$FogState +public com.mojang.blaze3d.platform.GlStateManager field_225663_h_ #FOG +public com.mojang.blaze3d.platform.GlStateManager$BooleanState +public com.mojang.blaze3d.platform.GlStateManager$BooleanState field_179201_b #field_179201_b \ No newline at end of file diff --git a/src/main/resources/create.mixins.json b/src/main/resources/create.mixins.json index ad05fd38b..e9d682678 100644 --- a/src/main/resources/create.mixins.json +++ b/src/main/resources/create.mixins.json @@ -3,7 +3,14 @@ "package": "com.simibubi.create.foundation.mixin", "compatibilityLevel": "JAVA_8", "refmap": "create.refmap.json", - "client": ["OnRemoveTileMixin", "ShaderCloseMixin", "CancelTileEntityRenderMixin", "LightUpdateMixin", "RenderHooksMixin"], + "client": [ + "OnRemoveTileMixin", + "ShaderCloseMixin", + "CancelTileEntityRenderMixin", + "LightUpdateMixin", + "RenderHooksMixin", + "FogColorTrackerMixin" + ], "injectors": { "defaultRequire": 1 },