Compare commits

..
Author SHA1 Message Date
Jason Mitchell a3d689e806 Because of course someone uses DoubleBuffer 2023-12-26 08:45:26 -08:00
embeddedt d4e22b6c0c Avoid restoring GL buffers we didn't modify 2023-12-26 07:35:29 -08:00
Jason Mitchell 7e24f6a395 Reduce stuttering on Nvidia - Track the model view and projection matrix (#56)
* Track the model view and projection matrix
* Cache ViewportState as well
* Add mixin to speedup Jabba's saveBoundTexture
2023-12-25 20:16:43 -08:00
embeddedt c8e6a6f34f Make glPushAttrib match native behavior if bypassing state manager cache 2023-12-25 17:19:59 -08:00
Jason Mitchell f7a79cf02f Switch up how we're grabbing the projection & model view matrix to avoid needing to call glGetFloat() again 2023-12-25 16:14:35 -08:00
13 changed files with 398 additions and 22 deletions

No files matched your search

@@ -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;
}
@@ -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,10 @@ 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.Matrix4d;
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;
@@ -33,6 +38,7 @@ import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.stream.IntStream;
@@ -55,6 +61,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 +394,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
@@ -672,7 +684,7 @@ public class GLStateManager {
public static void glPushAttrib(int mask) {
// TODO: Proper state tracking; but at least for now the current cases of this didn't do anything related to textures and
// just overly broadly set ALL_BITS :facepalm:
GL11.glPushAttrib(mask & ~(GL11.GL_TEXTURE_BIT));
GL11.glPushAttrib(GLStateManager.BYPASS_CACHE ? mask : (mask & ~(GL11.GL_TEXTURE_BIT)));
}
public static void glPopAttrib() {
@@ -694,4 +706,94 @@ 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 final Matrix4d conersionMatrix4d = new Matrix4d();
public static final Matrix4f conersionMatrix4f = new Matrix4f();
public static void glMultMatrix(DoubleBuffer matrix) {
GL11.glMultMatrix(matrix);
conersionMatrix4d.set(matrix);
conersionMatrix4f.set(conersionMatrix4d);
matrixState.multiply(conersionMatrix4f);
}
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);
}
}
@@ -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
}
}
@@ -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);
}
}
@@ -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.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(
@@ -0,0 +1,45 @@
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;
@Mixin(ActiveRenderInfo.class)
public class MixinActiveRenderInfo {
@Shadow private static FloatBuffer modelview;
@Shadow private static FloatBuffer projection;
@Inject(method = "updateRenderInfo", at = @At(value = "TAIL"))
private static void angelica$onUpdateRenderInfo(CallbackInfo ci) {
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);
}
}
}
@@ -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);
}
}
}
@@ -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();
}
}
@@ -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);
}
}
}
@@ -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,17 +27,14 @@ 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 setProjectionMatrix(FloatBuffer projection) {
projectionMatrix.set(projection);
projectionMatrix.get(0, projectionBuffer);
}
public void captureModelViewMatrix() {
modelViewBuffer.position(0);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelViewBuffer);
modelViewMatrix.set(modelViewBuffer);
public void setModelViewMatrix(FloatBuffer modelview) {
modelViewMatrix.set(modelview);
modelViewMatrix.get(0, modelViewBuffer);
}
}
@@ -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<String, Map<String, String>> methodRedirects = new HashMap<>();
private static final Map<Integer, String> glCapRedirects = new HashMap<>();
private static final List<String> 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
@@ -8,12 +8,14 @@ import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;
import java.util.Arrays;
import java.util.EnumSet;
public class GlStateTracker {
private static final int UNASSIGNED_HANDLE = -1;
private final int[] bufferState = new int[GlBufferTarget.COUNT];
//private final int[] bufferRestoreState = new int[GlBufferTarget.COUNT];
private final EnumSet<GlBufferTarget> mutatedBuffers = EnumSet.noneOf(GlBufferTarget.class);
private int vertexArrayState;
//private int vertexArrayRestoreState;
@@ -31,6 +33,8 @@ public class GlStateTracker {
this.bufferState[target.ordinal()] = buffer;
mutatedBuffers.add(target);
return prevBuffer != buffer;
}
@@ -47,9 +51,10 @@ public class GlStateTracker {
}
public void applyRestoreState() {
for (int i = 0; i < GlBufferTarget.COUNT; i++) {
GL15.glBindBuffer(GlBufferTarget.VALUES[i].getTargetParameter(), 0);
}
mutatedBuffers.forEach(target -> {
GL15.glBindBuffer(target.getTargetParameter(), 0);
});
mutatedBuffers.clear();
if(GlFunctions.isVertexArraySupported()) {
GL30.glBindVertexArray(0);
}
@@ -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);
}