From 79ac9e96e4d6f4239a581709a4ab60c69554c23e Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sun, 26 Sep 2021 21:15:02 -0700 Subject: [PATCH] Testing + fixing --- .../backend/gl/attrib/IAttribSpec.java | 2 +- .../backend/gl/attrib/MatrixAttributes.java | 4 +- .../backend/gl/attrib/VertexAttribSpec.java | 4 +- .../flywheel/backend/gl/shader/GlProgram.java | 4 +- .../core/materials/model/ModelData.java | 16 +++--- .../flywheel/mixin/matrix/Matrix3fMixin.java | 40 ++++++++++++++ .../flywheel/mixin/matrix/Matrix4fMixin.java | 54 +++++++++++++++++++ .../com/jozufozu/flywheel/util/Attribute.java | 11 ++++ src/main/resources/flywheel.mixins.json | 4 ++ 9 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/Attribute.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/IAttribSpec.java b/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/IAttribSpec.java index 779d5c502..04a8daf00 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/IAttribSpec.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/IAttribSpec.java @@ -2,7 +2,7 @@ package com.jozufozu.flywheel.backend.gl.attrib; public interface IAttribSpec { - void vertexAttribPointer(int stride, int index, int pointer); + void vertexAttribPointer(int stride, int index, int offset); int getSize(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/MatrixAttributes.java b/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/MatrixAttributes.java index 0a16dd11f..aef66036c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/MatrixAttributes.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/MatrixAttributes.java @@ -18,9 +18,9 @@ public enum MatrixAttributes implements IAttribSpec { } @Override - public void vertexAttribPointer(int stride, int index, int pointer) { + public void vertexAttribPointer(int stride, int index, int offset) { for (int i = 0; i < rows; i++) { - long attribPointer = pointer + (long) i * cols * GlNumericType.FLOAT.getByteWidth(); + long attribPointer = offset + (long) i * cols * GlNumericType.FLOAT.getByteWidth(); GL20.glVertexAttribPointer(index + i, cols, GlNumericType.FLOAT.getGlEnum(), false, stride, attribPointer); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/VertexAttribSpec.java b/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/VertexAttribSpec.java index 373f4cfb6..7219ddb20 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/VertexAttribSpec.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/attrib/VertexAttribSpec.java @@ -25,8 +25,8 @@ public class VertexAttribSpec implements IAttribSpec { } @Override - public void vertexAttribPointer(int stride, int index, int pointer) { - GL20.glVertexAttribPointer(index, count, type.getGlEnum(), normalized, stride, pointer); + public void vertexAttribPointer(int stride, int index, int offset) { + GL20.glVertexAttribPointer(index, count, type.getGlEnum(), normalized, stride, offset); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java index eade29843..e4356c2bf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java @@ -19,6 +19,8 @@ import net.minecraft.resources.ResourceLocation; import com.mojang.math.Matrix4f; public abstract class GlProgram extends GlObject { + private static final FloatBuffer floatBuffer = MemoryStack.stackGet() + .mallocFloat(16); public final ResourceLocation name; @@ -70,8 +72,6 @@ public abstract class GlProgram extends GlObject { } protected static void uploadMatrixUniform(int uniform, Matrix4f mat) { - FloatBuffer floatBuffer = MemoryStack.stackGet() - .mallocFloat(16); mat.store(floatBuffer); glUniformMatrix4fv(uniform, false, floatBuffer); } diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java index 8b36c186e..2d437a1d2 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java @@ -4,20 +4,20 @@ import java.nio.FloatBuffer; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.materials.BasicData; -import com.jozufozu.flywheel.util.RenderUtil; +import com.jozufozu.flywheel.util.Attribute; import com.mojang.blaze3d.vertex.PoseStack; public class ModelData extends BasicData { private static final float[] empty = new float[25]; public final float[] matrices = empty.clone(); - private final FloatBuffer buf = FloatBuffer.wrap(matrices); + private final FloatBuffer wrap = FloatBuffer.wrap(matrices); public ModelData setTransform(PoseStack stack) { - this.buf.reset(); + this.wrap.position(0); - stack.last().pose().store(this.buf); - stack.last().normal().store(this.buf); + ((Attribute)(Object) stack.last().pose()).append(this.wrap); + ((Attribute)(Object) stack.last().normal()).append(this.wrap); markDirty(); return this; } @@ -30,8 +30,8 @@ public class ModelData extends BasicData { *

*/ public ModelData setEmptyTransform() { - this.buf.reset(); - this.buf.put(empty); + this.wrap.position(0) + .put(empty); markDirty(); return this; } @@ -39,6 +39,6 @@ public class ModelData extends BasicData { @Override public void write(VecBuffer buf) { super.write(buf); - buf.put(this.buf); + buf.putFloatArray(matrices); } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java new file mode 100644 index 000000000..b40c487f5 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix3fMixin.java @@ -0,0 +1,40 @@ +package com.jozufozu.flywheel.mixin.matrix; + +import java.nio.FloatBuffer; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import com.jozufozu.flywheel.util.Attribute; +import com.mojang.math.Matrix3f; + +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; + +@OnlyIn(Dist.CLIENT) +@Mixin(Matrix3f.class) +public abstract class Matrix3fMixin implements Attribute { + + @Shadow protected float m00; + @Shadow protected float m01; + @Shadow protected float m02; + @Shadow protected float m10; + @Shadow protected float m11; + @Shadow protected float m12; + @Shadow protected float m20; + @Shadow protected float m21; + @Shadow protected float m22; + + @Override + public void append(FloatBuffer buffer) { + buffer.put(m00); + buffer.put(m10); + buffer.put(m20); + buffer.put(m01); + buffer.put(m11); + buffer.put(m21); + buffer.put(m02); + buffer.put(m12); + buffer.put(m22); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java new file mode 100644 index 000000000..378252cbc --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/matrix/Matrix4fMixin.java @@ -0,0 +1,54 @@ +package com.jozufozu.flywheel.mixin.matrix; + +import java.nio.FloatBuffer; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import com.jozufozu.flywheel.util.Attribute; +import com.mojang.math.Matrix4f; + +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; + +@OnlyIn(Dist.CLIENT) +@Mixin(Matrix4f.class) +public abstract class Matrix4fMixin implements Attribute { + + @Shadow protected float m00; + @Shadow protected float m01; + @Shadow protected float m02; + @Shadow protected float m03; + @Shadow protected float m10; + @Shadow protected float m11; + @Shadow protected float m12; + @Shadow protected float m13; + @Shadow protected float m20; + @Shadow protected float m21; + @Shadow protected float m22; + @Shadow protected float m23; + @Shadow protected float m30; + @Shadow protected float m31; + @Shadow protected float m32; + @Shadow protected float m33; + + @Override + public void append(FloatBuffer buffer) { + buffer.put(m00); + buffer.put(m10); + buffer.put(m20); + buffer.put(m30); + buffer.put(m01); + buffer.put(m11); + buffer.put(m21); + buffer.put(m31); + buffer.put(m02); + buffer.put(m12); + buffer.put(m22); + buffer.put(m32); + buffer.put(m03); + buffer.put(m13); + buffer.put(m23); + buffer.put(m33); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/Attribute.java b/src/main/java/com/jozufozu/flywheel/util/Attribute.java new file mode 100644 index 000000000..75739b500 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/Attribute.java @@ -0,0 +1,11 @@ +package com.jozufozu.flywheel.util; + +import java.nio.FloatBuffer; + +public interface Attribute { + + /** + * Append the contents of this object to the given FloatBuffer. + */ + void append(FloatBuffer buffer); +} diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index b9cf08632..d08e53e85 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -20,6 +20,10 @@ "LeakChunkStorageArrayMixin", "PausedPartialTickAccessor" ], + "mixins": [ + "matrix.Matrix3fMixin", + "matrix.Matrix4fMixin" + ], "injectors": { "defaultRequire": 1 }