diff --git a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java index 1c0aa8c0a..09a259bcb 100644 --- a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java +++ b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java @@ -73,4 +73,9 @@ public class AngelicaConfig { @Config.DefaultBoolean(true) @Config.RequiresMcRestart public static boolean optimizeTextureLoading; + + @Config.Comment("Optimize slow glGetInteger in Jabba w/ Nvidia Drivers") + @Config.DefaultBoolean(true) + @Config.RequiresMcRestart + public static boolean optimizeJabbaGetInteger; } diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java index 908a97406..237239e68 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java @@ -8,7 +8,9 @@ import com.gtnewhorizons.angelica.glsm.states.Color4; import com.gtnewhorizons.angelica.glsm.states.DepthState; import com.gtnewhorizons.angelica.glsm.states.FogState; import com.gtnewhorizons.angelica.glsm.states.GLColorMask; +import com.gtnewhorizons.angelica.glsm.states.MatrixState; import com.gtnewhorizons.angelica.glsm.states.TextureState; +import com.gtnewhorizons.angelica.glsm.states.ViewportState; import com.gtnewhorizons.angelica.hudcaching.HUDCaching; import lombok.Getter; import net.coderbot.iris.Iris; @@ -24,7 +26,9 @@ import net.coderbot.iris.texture.TextureInfoCache; import net.coderbot.iris.texture.TextureTracker; import net.coderbot.iris.texture.pbr.PBRTextureManager; import net.minecraft.client.renderer.OpenGlHelper; +import org.joml.Matrix4f; import org.joml.Vector3d; +import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.ARBMultitexture; import org.lwjgl.opengl.Drawable; @@ -55,6 +59,8 @@ public class GLStateManager { @Getter private static final AlphaState alphaState = new AlphaState(); @Getter private static final BooleanState lightingState = new BooleanState(GL11.GL_LIGHTING); @Getter private static final BooleanState rescaleNormalState = new BooleanState(GL12.GL_RESCALE_NORMAL); + @Getter private static final MatrixState matrixState = new MatrixState(); + @Getter private static final ViewportState viewportState = new ViewportState(); private static long dirty = 0; @@ -386,6 +392,10 @@ public class GLStateManager { } } + public static int getBoundTexture() { + return Textures[activeTexture].binding; + } + public static void glBindTexture(int target, int texture) { if(inGLNewList) { // Binding a texture, while building a list, is not allowed and is a silent noop @@ -694,4 +704,85 @@ public class GLStateManager { public static void dirty(long dirtyFlag) { dirty |= dirtyFlag; } + + // Matrix Operations + public static void glMatrixMode(int mode) { + matrixState.setMode(mode); + GL11.glMatrixMode(mode); + } + public static void glLoadIdentity() { + GL11.glLoadIdentity(); + matrixState.identity(); + } + + public static void glTranslatef(float x, float y, float z) { + GL11.glTranslatef(x, y, z); + matrixState.translate(x, y, z); + } + public static void glTranslated(double x, double y, double z) { + GL11.glTranslated(x, y, z); + matrixState.translate((float) x, (float) y, (float) z); + } + + public static void glScalef(float x, float y, float z) { + GL11.glScalef(x, y, z); + matrixState.scale(x, y, z); + } + + public static void glScaled(double x, double y, double z) { + GL11.glScaled(x, y, z); + matrixState.scale((float) x, (float) y, (float) z); + } + + public static void glMultMatrix(FloatBuffer matrix) { + GL11.glMultMatrix(matrix); + matrixState.multiply(matrix); + } + + public static void glRotatef(float angle, float x, float y, float z) { + GL11.glRotatef(angle, x, y, z); + matrixState.rotate((float)Math.toRadians(angle), x, y, z); + } + + public static void glRotated(double angle, double x, double y, double z) { + GL11.glRotated(angle, x, y, z); + matrixState.rotate((float)Math.toRadians(angle), (float) x, (float) y, (float) z); + } + + public static void glOrtho(double left, double right, double bottom, double top, double zNear, double zFar) { + GL11.glOrtho(left, right, bottom, top, zNear, zFar); + matrixState.ortho(left, right, bottom, top, zNear, zFar); + } + + public static void glFrustum(double left, double right, double bottom, double top, double zNear, double zFar) { + GL11.glFrustum(left, right, bottom, top, zNear, zFar); + matrixState.frustum(left, right, bottom, top, zNear, zFar); + } + public static void glPushMatrix() { + GL11.glPushMatrix(); + matrixState.push(); + } + + public static void glPopMatrix() { + GL11.glPopMatrix(); + matrixState.pop(); + } + + private static final Matrix4f perspectiveMatrix = new Matrix4f(); + private static final FloatBuffer perspectiveBuffer = BufferUtils.createFloatBuffer(16); + public static void gluPerspective(float fovy, float aspect, float zNear, float zFar) { + perspectiveMatrix.identity().perspective((float)Math.toRadians(fovy), aspect, zNear, zFar); + + perspectiveMatrix.get(0, perspectiveBuffer); + GL11.glMultMatrix(perspectiveBuffer); + + matrixState.multiply(perspectiveMatrix); + + } + + public static void glViewport(int x, int y, int width, int height) { + GL11.glViewport(x, y, width, height); + viewportState.set(x, y, width, height); + } + } diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/states/MatrixState.java b/src/main/java/com/gtnewhorizons/angelica/glsm/states/MatrixState.java new file mode 100644 index 000000000..e07454536 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/states/MatrixState.java @@ -0,0 +1,134 @@ +package com.gtnewhorizons.angelica.glsm.states; + +import org.joml.Matrix4f; +import org.joml.Matrix4fStack; +import org.lwjgl.opengl.GL11; + +import java.nio.FloatBuffer; + +public class MatrixState { + + public MatrixMode mode = MatrixMode.NONE; + public final Matrix4fStack projectionMatrix; + public final Matrix4fStack modelViewMatrix; + + public MatrixState() { + projectionMatrix = new Matrix4fStack(GL11.glGetInteger(GL11.GL_MAX_PROJECTION_STACK_DEPTH)); + modelViewMatrix = new Matrix4fStack(GL11.glGetInteger(GL11.GL_MAX_MODELVIEW_STACK_DEPTH)); + } + + public void identity() { + switch (mode) { + case MODELVIEW -> modelViewMatrix.identity(); + case PROJECTION -> projectionMatrix.identity(); + default -> { + } + } + } + + public void setMode(int mode) { + switch (mode) { + case GL11.GL_MODELVIEW -> this.mode = MatrixMode.MODELVIEW; + case GL11.GL_PROJECTION -> this.mode = MatrixMode.PROJECTION; + default -> this.mode = MatrixMode.NONE; + } + } + + public void translate(float x, float y, float z) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.translate(x, y, z); + case PROJECTION -> projectionMatrix.translate(x, y, z); + default -> { + } + } + } + + public void scale(float x, float y, float z) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.scale(x, y, z); + case PROJECTION -> projectionMatrix.scale(x, y, z); + default -> { + } + } + } + + private final Matrix4f tempMatrix = new Matrix4f(); + + public void multiply(FloatBuffer matrix) { + tempMatrix.set(matrix); + switch (mode) { + case MODELVIEW -> modelViewMatrix.mul(tempMatrix); + case PROJECTION -> projectionMatrix.mul(tempMatrix); + default -> { + } + } + } + + public void multiply(Matrix4f matrix) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.mul(matrix); + case PROJECTION -> projectionMatrix.mul(matrix); + default -> { + } + } + } + + public void rotate(float angle, float x, float y, float z) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.rotate(angle, x, y, z); + case PROJECTION -> projectionMatrix.rotate(angle, x, y, z); + default -> { + } + } + } + + public void ortho(double left, double right, double bottom, double top, double zNear, double zFar) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.ortho((float) left, (float) right, (float) bottom, (float) top, (float) zNear, (float) zFar); + case PROJECTION -> projectionMatrix.ortho((float) left, (float) right, (float) bottom, (float) top, (float) zNear, (float) zFar); + default -> { + } + } + } + + public void frustum(double left, double right, double bottom, double top, double zNear, double zFar) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.frustum((float) left, (float) right, (float) bottom, (float) top, (float) zNear, (float) zFar); + case PROJECTION -> projectionMatrix.frustum((float) left, (float) right, (float) bottom, (float) top, (float) zNear, (float) zFar); + default -> { + } + } + } + public void push() { + switch (mode) { + case MODELVIEW -> modelViewMatrix.pushMatrix(); + case PROJECTION -> projectionMatrix.pushMatrix(); + default -> { + } + } + } + + public void pop() { + switch (mode) { + case MODELVIEW -> modelViewMatrix.popMatrix(); + case PROJECTION -> projectionMatrix.popMatrix(); + default -> { + } + } + } + + public void perspective(float fovy, float aspect, float zNear, float zFar) { + switch (mode) { + case MODELVIEW -> modelViewMatrix.perspective(fovy, aspect, zNear, zFar); + case PROJECTION -> projectionMatrix.perspective(fovy, aspect, zNear, zFar); + default -> { + } + } + } + + enum MatrixMode { + NONE, + MODELVIEW, + PROJECTION + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/states/ViewportState.java b/src/main/java/com/gtnewhorizons/angelica/glsm/states/ViewportState.java new file mode 100644 index 000000000..51cbf8d2d --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/states/ViewportState.java @@ -0,0 +1,24 @@ +package com.gtnewhorizons.angelica.glsm.states; + +import java.nio.IntBuffer; + +public class ViewportState { + public int x; + public int y; + public int width; + public int height; + + public void set(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + public void get(IntBuffer params) { + params.put(0, x); + params.put(1, y); + params.put(2, width); + params.put(3, height); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java index a24617b9d..660761b05 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java @@ -25,8 +25,9 @@ public enum Mixins { ANGELICA(new Builder("Angelica").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT) .setPhase(Phase.EARLY).addMixinClasses( - "angelica.MixinEntityRenderer" - ,"angelica.MixinActiveRenderInfo" + "angelica.MixinActiveRenderInfo" + ,"angelica.MixinClippingHelperImpl" + ,"angelica.MixinEntityRenderer" ) ), @@ -135,6 +136,11 @@ public enum Mixins { ) ), + OPTIMIZE_JABBA_GET_INTEGER(new Builder("Optimize Jabba getInteger").setPhase(Phase.LATE) + .addTargetedMod(TargetedMod.JABBA).setSide(Side.CLIENT) + .setApplyIf(() -> AngelicaConfig.optimizeJabbaGetInteger) + .addMixinClasses("client.jabba.MixinTileEntityBaseRenderer")), + ANGELICA_TEXTURE(new Builder("Textures").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT) .setPhase(Phase.EARLY).setApplyIf(() -> AngelicaConfig.enableIris || AngelicaConfig.enableSodium).addMixinClasses( diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinActiveRenderInfo.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinActiveRenderInfo.java index 6969bd6a3..f07a9e4f7 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinActiveRenderInfo.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinActiveRenderInfo.java @@ -1,11 +1,15 @@ package com.gtnewhorizons.angelica.mixins.early.angelica; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.ViewportState; import com.gtnewhorizons.angelica.rendering.RenderingState; import net.minecraft.client.renderer.ActiveRenderInfo; +import org.lwjgl.opengl.GL11; 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.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.nio.FloatBuffer; @@ -19,4 +23,23 @@ public class MixinActiveRenderInfo { RenderingState.INSTANCE.setProjectionMatrix(projection); RenderingState.INSTANCE.setModelViewMatrix(modelview); } + + @Redirect(method = "updateRenderInfo", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glGetFloat(ILjava/nio/FloatBuffer;)V"), remap = false) + private static void angelica$glGetFloat(int pname, FloatBuffer params) { + if(pname == GL11.GL_MODELVIEW_MATRIX) { + GLStateManager.getMatrixState().modelViewMatrix.get(0, params); + } else if(pname == GL11.GL_PROJECTION_MATRIX) { + GLStateManager.getMatrixState().projectionMatrix.get(0, params); + } else { + GL11.glGetFloat(pname, params); + } + } + @Redirect(method = "updateRenderInfo", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glGetInteger(ILjava/nio/IntBuffer;)V"), remap = false) + private static void angelica$glGetInteger(int pname, java.nio.IntBuffer params) { + if(pname == GL11.GL_VIEWPORT) { + GLStateManager.getViewportState().get(params); + } else { + GL11.glGetInteger(pname, params); + } + } } diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinClippingHelperImpl.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinClippingHelperImpl.java new file mode 100644 index 000000000..d1b2a0b67 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinClippingHelperImpl.java @@ -0,0 +1,24 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import net.minecraft.client.renderer.culling.ClippingHelperImpl; +import org.lwjgl.opengl.GL11; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import java.nio.FloatBuffer; + +@Mixin(ClippingHelperImpl.class) +public class MixinClippingHelperImpl { + @Redirect(method="init", at=@At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glGetFloat(ILjava/nio/FloatBuffer;)V"), remap = false) + private void angelica$glGetFloat(int pname, FloatBuffer params) { + if(pname == GL11.GL_MODELVIEW_MATRIX) { + GLStateManager.getMatrixState().modelViewMatrix.get(0, params); + } else if(pname == GL11.GL_PROJECTION_MATRIX) { + GLStateManager.getMatrixState().projectionMatrix.get(0, params); + } else { + GL11.glGetFloat(pname, params); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinEntityRenderer.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinEntityRenderer.java index 654c91b1c..d5f9e8452 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinEntityRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinEntityRenderer.java @@ -21,8 +21,5 @@ public abstract class MixinEntityRenderer { viewEntity.lastTickPosY + (viewEntity.posY - viewEntity.lastTickPosY) * partialTicks, viewEntity.lastTickPosZ + (viewEntity.posZ - viewEntity.lastTickPosZ) * partialTicks ); -// RenderingState.INSTANCE.captureProjectionMatrix(); -// RenderingState.INSTANCE.captureModelViewMatrix(); - } } diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/late/client/jabba/MixinTileEntityBaseRenderer.java b/src/main/java/com/gtnewhorizons/angelica/mixins/late/client/jabba/MixinTileEntityBaseRenderer.java new file mode 100644 index 000000000..a8d5e6e10 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/late/client/jabba/MixinTileEntityBaseRenderer.java @@ -0,0 +1,19 @@ +package com.gtnewhorizons.angelica.mixins.late.client.jabba; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.lwjgl.opengl.GL11; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(targets = "mcp.mobius.betterbarrels.client.render.TileEntityBaseRenderer") +public class MixinTileEntityBaseRenderer { + @Redirect(method="saveBoundTexture", at=@At(value="INVOKE", target="Lorg/lwjgl/opengl/GL11;glGetInteger(I)I"), remap=false) + private int glGetInteger(int pname) { + if(pname == GL11.GL_TEXTURE_BINDING_2D) { + return GLStateManager.getBoundTexture(); + } else { + return GL11.glGetInteger(pname); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/RenderingState.java b/src/main/java/com/gtnewhorizons/angelica/rendering/RenderingState.java index 53c3419be..a59cdc0cd 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/RenderingState.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/RenderingState.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.rendering; +import com.gtnewhorizons.angelica.glsm.GLStateManager; import lombok.Getter; import org.joml.Matrix4f; import org.joml.Vector3d; @@ -26,22 +27,10 @@ public class RenderingState { cameraPosition.set(x, y, z); } - public void captureProjectionMatrix() { - // Not very fast, but given we're not on 3.2+ core and are still using the opengl matrix stack, we don't have much alternative - projectionBuffer.position(0); - GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionBuffer); - projectionMatrix.set(projectionBuffer); - } - - public void captureModelViewMatrix() { - modelViewBuffer.position(0); - GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelViewBuffer); - modelViewMatrix.set(modelViewBuffer); - } - public void setProjectionMatrix(FloatBuffer projection) { projectionMatrix.set(projection); projectionMatrix.get(0, projectionBuffer); + } public void setModelViewMatrix(FloatBuffer modelview) { diff --git a/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java b/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java index 124e84239..ab4d33a16 100644 --- a/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java +++ b/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java @@ -44,6 +44,8 @@ public class RedirectorTransformer implements IClassTransformer { private static final String GL11 = "org/lwjgl/opengl/GL11"; private static final String GL13 = "org/lwjgl/opengl/GL13"; private static final String GL14 = "org/lwjgl/opengl/GL14"; + private static final String Project = "org/lwjgl/util/glu/Project"; + private static final String OpenGlHelper = "net/minecraft/client/renderer/OpenGlHelper"; private static final String EXTBlendFunc = "org/lwjgl/opengl/EXTBlendFuncSeparate"; private static final String ARBMultiTexture = "org/lwjgl/opengl/ARBMultitexture"; @@ -66,7 +68,8 @@ public class RedirectorTransformer implements IClassTransformer { Pair.of("maxZ", "field_149757_G") ); - private static final ClassConstantPoolParser cstPoolParser = new ClassConstantPoolParser(GL11, GL13, GL14, OpenGlHelper, EXTBlendFunc, ARBMultiTexture, TessellatorClass, BlockClass); + private static final ClassConstantPoolParser cstPoolParser = new ClassConstantPoolParser(GL11, GL13, GL14, OpenGlHelper, EXTBlendFunc, ARBMultiTexture, TessellatorClass, BlockClass, + Project); private static final Map> methodRedirects = new HashMap<>(); private static final Map glCapRedirects = new HashMap<>(); private static final List TransformerExclusions = Arrays.asList( @@ -113,6 +116,20 @@ public class RedirectorTransformer implements IClassTransformer { .add("glPopAttrib") .add("glShadeModel") .add("glTexImage2D") + .add("glMatrixMode") + .add("glLoadIdentity") + .add("glTranslatef") + .add("glTranslated") + .add("glScalef") + .add("glScaled") + .add("glMultMatrix") + .add("glRotatef") + .add("glRotated") + .add("glOrtho") + .add("glFrustum") + .add("glPushMatrix") + .add("glPopMatrix") + .add("glViewport") ); methodRedirects.put(GL13, RedirectMap.newMap().add("glActiveTexture")); methodRedirects.put(GL14, RedirectMap.newMap().add("glBlendFuncSeparate", "tryBlendFuncSeparate")); @@ -121,6 +138,7 @@ public class RedirectorTransformer implements IClassTransformer { .add("func_148821_a", "tryBlendFuncSeparate")); methodRedirects.put(EXTBlendFunc, RedirectMap.newMap().add("glBlendFuncSeparateEXT", "tryBlendFuncSeparate")); methodRedirects.put(ARBMultiTexture, RedirectMap.newMap().add("glActiveTextureARB")); + methodRedirects.put(Project, RedirectMap.newMap().add("gluPerspective")); } @Override diff --git a/src/main/java/net/coderbot/iris/gl/IrisRenderSystem.java b/src/main/java/net/coderbot/iris/gl/IrisRenderSystem.java index cb6ba185b..b00f3ad0d 100644 --- a/src/main/java/net/coderbot/iris/gl/IrisRenderSystem.java +++ b/src/main/java/net/coderbot/iris/gl/IrisRenderSystem.java @@ -243,8 +243,7 @@ public class IrisRenderSystem { public static void setupProjectionMatrix(Matrix4f matrix) { GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glPushMatrix(); - PROJECTION_MATRIX_BUFFER.clear().rewind(); - matrix.get(PROJECTION_MATRIX_BUFFER); + matrix.get(0, PROJECTION_MATRIX_BUFFER); GL11.glLoadMatrix(PROJECTION_MATRIX_BUFFER); GL11.glMatrixMode(GL11.GL_MODELVIEW); }