Introducing the Apple Pointer

- Support vertexAttribIPointer
This commit is contained in:
Jozufozu 2022-07-23 18:39:09 -07:00
parent 00af8bda2e
commit 69de15d4de
18 changed files with 87 additions and 36 deletions

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -0,0 +1,7 @@
package com.jozufozu.flywheel.backend.gl.array;
public interface VertexAttribute {
int getByteWidth();
void pointer(long offset, int i, int stride);
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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 {

View file

@ -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;

View file

@ -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;

View file

@ -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<VertexAttribute> layoutItems) {
int stride = 0;
for (VertexAttribute spec : layoutItems) {
for (var spec : layoutItems) {
stride += spec.getByteWidth();
}
return stride;

View file

@ -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);

View file

@ -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 {

View file

@ -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<VertexAttribute> consumer) {
for (int i = 0; i < rows; i++) {
consumer.accept(new VertexAttribute(cols, GlNumericType.FLOAT, false));
consumer.accept(new VertexAttributeF(GlNumericType.FLOAT, cols, false));
}
}

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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() {