diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/VertexAttribute.java b/src/main/java/com/jozufozu/flywheel/backend/gl/VertexAttribute.java deleted file mode 100644 index c425183c4..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/VertexAttribute.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jozufozu.flywheel.backend.gl; - -// TODO: support glVertexAttribIPointer -/** - * A bindable attribute in a vertex array. - * - * @param size The number of components in the attribute, e.g. 3 for a vec3. - * @param type The type of the attribute, e.g. GL_FLOAT. - * @param normalized Whether the data is normalized. - */ -public record VertexAttribute(int size, GlNumericType type, boolean normalized) { - - public int getByteWidth() { - return size * type.getByteWidth(); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java b/src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java similarity index 92% rename from src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java rename to src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java index dabf09c7f..3abf7a7d0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/array/GlVertexArray.java @@ -1,8 +1,10 @@ -package com.jozufozu.flywheel.backend.gl; +package com.jozufozu.flywheel.backend.gl.array; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL32; +import com.jozufozu.flywheel.backend.gl.GlObject; +import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; @@ -80,14 +82,15 @@ public class GlVertexArray extends GlObject { int i = startAttrib; final int stride = type.getStride(); - for (VertexAttribute attribute : type.getAttributes()) { + for (var attribute : type.getAttributes()) { targets[i] = targetBuffer; attributes[i] = attribute; offsets[i] = offset; strides[i] = stride; - GL32.glVertexAttribPointer(i++, attribute.size(), attribute.type().getGlEnum(), attribute.normalized(), stride, offset); + attribute.pointer(offset, i, stride); + i++; offset += attribute.getByteWidth(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttribute.java b/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttribute.java new file mode 100644 index 000000000..18c458643 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttribute.java @@ -0,0 +1,7 @@ +package com.jozufozu.flywheel.backend.gl.array; + +public interface VertexAttribute { + int getByteWidth(); + + void pointer(long offset, int i, int stride); +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttributeF.java b/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttributeF.java new file mode 100644 index 000000000..001e73d06 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttributeF.java @@ -0,0 +1,25 @@ +package com.jozufozu.flywheel.backend.gl.array; + +import org.lwjgl.opengl.GL32; + +import com.jozufozu.flywheel.backend.gl.GlNumericType; + +/** + * A bindable attribute in a vertex array. + * + * @param type The type of the attribute, e.g. GL_FLOAT. + * @param size The number of components in the attribute, e.g. 3 for a vec3. + * @param normalized Whether the data is normalized. + */ +public record VertexAttributeF(GlNumericType type, int size, boolean normalized) implements VertexAttribute { + + @Override + public int getByteWidth() { + return size * type.getByteWidth(); + } + + @Override + public void pointer(long offset, int i, int stride) { + GL32.glVertexAttribPointer(i, size(), type().getGlEnum(), normalized(), stride, offset); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttributeI.java b/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttributeI.java new file mode 100644 index 000000000..d0f0bc903 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/array/VertexAttributeI.java @@ -0,0 +1,24 @@ +package com.jozufozu.flywheel.backend.gl.array; + +import org.lwjgl.opengl.GL32; + +import com.jozufozu.flywheel.backend.gl.GlNumericType; + +/** + * A bindable attribute in a vertex array. + * + * @param type The type of the attribute, e.g. GL_INT. + * @param size The number of components in the attribute, e.g. 3 for a vec3. + */ +public record VertexAttributeI(GlNumericType type, int size) implements VertexAttribute { + + @Override + public int getByteWidth() { + return size * type.getByteWidth(); + } + + @Override + public void pointer(long offset, int i, int stride) { + GL32.glVertexAttribIPointer(i, size(), type().getGlEnum(), stride, offset); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/DrawCall.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/DrawCall.java index 5ec95fb2c..a8f09ff28 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/DrawCall.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/DrawCall.java @@ -3,7 +3,7 @@ package com.jozufozu.flywheel.backend.instancing.instancing; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.gl.GlStateTracker; -import com.jozufozu.flywheel.backend.gl.GlVertexArray; +import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.backend.model.MeshPool; import com.jozufozu.flywheel.core.model.Mesh; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java index 1394cd948..759afd2c0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java @@ -7,7 +7,7 @@ import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructWriter; -import com.jozufozu.flywheel.backend.gl.GlVertexArray; +import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage; diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java index 36890cb5c..f8e41586f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.backend.model; -import com.jozufozu.flywheel.backend.gl.GlVertexArray; +import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.core.model.BlockMesh; public class ArrayModelRenderer { diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/MeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/model/MeshPool.java index 2bf506606..171ea5abf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/MeshPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/MeshPool.java @@ -14,7 +14,7 @@ import org.lwjgl.opengl.GL32; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.gl.GlPrimitive; -import com.jozufozu.flywheel.backend.gl.GlVertexArray; +import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; diff --git a/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java b/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java index 61f83a635..c3282c2ae 100644 --- a/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java +++ b/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java @@ -5,7 +5,7 @@ import static org.lwjgl.opengl.GL11.glDrawArrays; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.gl.GlStateTracker; -import com.jozufozu.flywheel.backend.gl.GlVertexArray; +import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; diff --git a/src/main/java/com/jozufozu/flywheel/core/layout/BufferLayout.java b/src/main/java/com/jozufozu/flywheel/core/layout/BufferLayout.java index 5673f0126..6d7ef05f9 100644 --- a/src/main/java/com/jozufozu/flywheel/core/layout/BufferLayout.java +++ b/src/main/java/com/jozufozu/flywheel/core/layout/BufferLayout.java @@ -5,7 +5,8 @@ import java.util.List; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.api.vertex.VertexType; -import com.jozufozu.flywheel.backend.gl.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttributeF; /** * Classic Vertex Format struct with a clever name. @@ -53,7 +54,7 @@ public class BufferLayout { private static int calculateStride(List layoutItems) { int stride = 0; - for (VertexAttribute spec : layoutItems) { + for (var spec : layoutItems) { stride += spec.getByteWidth(); } return stride; diff --git a/src/main/java/com/jozufozu/flywheel/core/layout/CommonItems.java b/src/main/java/com/jozufozu/flywheel/core/layout/CommonItems.java index 9c3fa247b..e8d51f557 100644 --- a/src/main/java/com/jozufozu/flywheel/core/layout/CommonItems.java +++ b/src/main/java/com/jozufozu/flywheel/core/layout/CommonItems.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.core.layout; import com.jozufozu.flywheel.backend.gl.GlNumericType; +import com.jozufozu.flywheel.backend.gl.array.VertexAttributeI; public class CommonItems { @@ -15,8 +16,8 @@ public class CommonItems { public static final PrimitiveItem RGBA = new PrimitiveItem(GlNumericType.UBYTE, 4, true); public static final PrimitiveItem RGB = new PrimitiveItem(GlNumericType.UBYTE, 3, true); - public static final PrimitiveItem LIGHT = new PrimitiveItem(GlNumericType.UBYTE, 2, false); - public static final PrimitiveItem LIGHT_SHORT = new PrimitiveItem(GlNumericType.USHORT, 2, false); + public static final PrimitiveItem LIGHT = new PrimitiveItem(new VertexAttributeI(GlNumericType.UBYTE, 2)); + public static final PrimitiveItem LIGHT_SHORT = new PrimitiveItem(new VertexAttributeI(GlNumericType.USHORT, 2)); public static final PrimitiveItem NORMALIZED_BYTE = new PrimitiveItem(GlNumericType.BYTE, 1, true); diff --git a/src/main/java/com/jozufozu/flywheel/core/layout/LayoutItem.java b/src/main/java/com/jozufozu/flywheel/core/layout/LayoutItem.java index 6e9217425..6b9b22d3d 100644 --- a/src/main/java/com/jozufozu/flywheel/core/layout/LayoutItem.java +++ b/src/main/java/com/jozufozu/flywheel/core/layout/LayoutItem.java @@ -2,7 +2,7 @@ package com.jozufozu.flywheel.core.layout; import java.util.function.Consumer; -import com.jozufozu.flywheel.backend.gl.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttribute; public interface LayoutItem { diff --git a/src/main/java/com/jozufozu/flywheel/core/layout/MatrixItem.java b/src/main/java/com/jozufozu/flywheel/core/layout/MatrixItem.java index 62682d43b..e715ff369 100644 --- a/src/main/java/com/jozufozu/flywheel/core/layout/MatrixItem.java +++ b/src/main/java/com/jozufozu/flywheel/core/layout/MatrixItem.java @@ -3,14 +3,15 @@ package com.jozufozu.flywheel.core.layout; import java.util.function.Consumer; import com.jozufozu.flywheel.backend.gl.GlNumericType; -import com.jozufozu.flywheel.backend.gl.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttributeF; public record MatrixItem(int rows, int cols) implements LayoutItem { @Override public void provideAttributes(Consumer consumer) { for (int i = 0; i < rows; i++) { - consumer.accept(new VertexAttribute(cols, GlNumericType.FLOAT, false)); + consumer.accept(new VertexAttributeF(GlNumericType.FLOAT, cols, false)); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/layout/PrimitiveItem.java b/src/main/java/com/jozufozu/flywheel/core/layout/PrimitiveItem.java index f6218238c..bc6423e17 100644 --- a/src/main/java/com/jozufozu/flywheel/core/layout/PrimitiveItem.java +++ b/src/main/java/com/jozufozu/flywheel/core/layout/PrimitiveItem.java @@ -3,7 +3,8 @@ package com.jozufozu.flywheel.core.layout; import java.util.function.Consumer; import com.jozufozu.flywheel.backend.gl.GlNumericType; -import com.jozufozu.flywheel.backend.gl.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttribute; +import com.jozufozu.flywheel.backend.gl.array.VertexAttributeF; public class PrimitiveItem implements LayoutItem { @@ -14,7 +15,11 @@ public class PrimitiveItem implements LayoutItem { } public PrimitiveItem(GlNumericType type, int count, boolean normalized) { - attribute = new VertexAttribute(count, type, normalized); + this(new VertexAttributeF(type, count, normalized)); + } + + public PrimitiveItem(VertexAttribute attribute) { + this.attribute = attribute; } @Override diff --git a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert index 46594fb28..93a50ddee 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/oriented.vert @@ -1,7 +1,7 @@ #use "flywheel:api/vertex.glsl" #use "flywheel:util/quaternion.glsl" -layout(location = 0) in vec2 oriented_light; // TODO: switch to ivec2 +layout(location = 0) in ivec2 oriented_light; layout(location = 1) in vec4 oriented_color; layout(location = 2) in vec3 oriented_pos; layout(location = 3) in vec3 oriented_pivot; diff --git a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert index 72931d407..e2ea23ef4 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert +++ b/src/main/resources/assets/flywheel/flywheel/instance/transformed.vert @@ -1,6 +1,6 @@ #use "flywheel:api/vertex.glsl" -layout(location = 0) in vec2 transformed_light; // TODO: switch to ivec2 +layout(location = 0) in ivec2 transformed_light; layout(location = 1) in vec4 transformed_color; layout(location = 2) in mat4 transformed_pose; layout(location = 6) in mat3 transformed_normal; diff --git a/src/main/resources/assets/flywheel/flywheel/layout/block.vert b/src/main/resources/assets/flywheel/flywheel/layout/block.vert index 865578d63..a3af3b16d 100644 --- a/src/main/resources/assets/flywheel/flywheel/layout/block.vert +++ b/src/main/resources/assets/flywheel/flywheel/layout/block.vert @@ -3,7 +3,7 @@ layout(location = 0) in vec3 _flw_v_pos; layout(location = 1) in vec4 _flw_v_color; layout(location = 2) in vec2 _flw_v_texCoord; -layout(location = 3) in vec2 _flw_v_light; // TODO: switch to ivec2 +layout(location = 3) in ivec2 _flw_v_light; layout(location = 4) in vec3 _flw_v_normal; void flw_layoutVertex() {