From 4f8e6af3d0127b2481834d8aff9c6a244574af79 Mon Sep 17 00:00:00 2001
From: Jozufozu
Date: Sun, 3 Dec 2023 17:03:13 -0800
Subject: [PATCH] Laying out the API
- Remove BufferLayout from VertexType.
- Remove VertexType from mesh, and only allow writes to a VertexList.
- MeshPools directly refer to BufferLayout instead of going through
VertexType.
---
.../flywheel/api/layout/BufferLayout.java | 2 --
.../com/jozufozu/flywheel/api/model/Mesh.java | 18 -----------
.../flywheel/api/vertex/VertexType.java | 11 ++-----
.../engine/indirect/IndirectMeshPool.java | 16 +++-------
.../engine/instancing/InstancedMeshPool.java | 31 ++++++-------------
.../flywheel/lib/model/ModelUtil.java | 2 +-
.../flywheel/lib/model/SimpleMesh.java | 14 ++-------
.../flywheel/lib/vertex/BlockVertex.java | 4 +--
.../lib/vertex/PosTexNormalVertex.java | 5 ++-
9 files changed, 25 insertions(+), 78 deletions(-)
diff --git a/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java b/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java
index b289a436a..808c5031b 100644
--- a/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java
+++ b/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java
@@ -3,7 +3,6 @@ package com.jozufozu.flywheel.api.layout;
import java.util.List;
import com.google.common.collect.ImmutableList;
-import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.gl.array.VertexAttribute;
/**
@@ -14,7 +13,6 @@ import com.jozufozu.flywheel.gl.array.VertexAttribute;
*
*
* @see com.jozufozu.flywheel.api.instance.InstanceType
- * @see VertexType
*/
public class BufferLayout {
diff --git a/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java b/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java
index 4c7d3bd58..46f8ba3ef 100644
--- a/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java
+++ b/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java
@@ -3,14 +3,11 @@ package com.jozufozu.flywheel.api.model;
import org.joml.Vector4fc;
import com.jozufozu.flywheel.api.vertex.MutableVertexList;
-import com.jozufozu.flywheel.api.vertex.VertexType;
/**
* A holder for arbitrary vertex data that can be written to memory or a vertex list.
*/
public interface Mesh {
- VertexType vertexType();
-
/**
* @return The number of vertices this mesh has.
*/
@@ -24,21 +21,6 @@ public interface Mesh {
return vertexCount() == 0;
}
- /**
- * The size in bytes that this mesh's data takes up.
- */
- default int size() {
- return vertexType().getStride() * vertexCount();
- }
-
- /**
- * Write this mesh into memory. The written data will use the format defined by {@link #vertexType()} and the amount of
- * bytes written will be the same as the return value of {@link #size()}.
- *
- * @param ptr The address to which data is written to.
- */
- void write(long ptr);
-
/**
* Write this mesh into a vertex list. Vertices with index {@literal <}0 or {@literal >=}{@link #vertexCount()} will not be
* read or modified.
diff --git a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java
index 38371e769..2e67b232a 100644
--- a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java
+++ b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java
@@ -1,19 +1,14 @@
package com.jozufozu.flywheel.api.vertex;
-import com.jozufozu.flywheel.api.layout.BufferLayout;
-
/**
* A vertex type containing metadata about a specific vertex layout.
*/
+// TODO: query a bitset of vertex attributes that are used?
public interface VertexType extends VertexListProvider {
/**
- * The layout of this type of vertex when buffered.
+ * The byte size of a single vertex.
*/
- BufferLayout getLayout();
-
- default int getStride() {
- return getLayout().getStride();
- }
+ int getStride();
default int byteOffset(int vertexIndex) {
return getStride() * vertexIndex;
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java
index ef665e870..80e436581 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java
@@ -10,17 +10,16 @@ import org.joml.Vector4fc;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.api.model.Mesh;
-import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.gl.GlNumericType;
import com.jozufozu.flywheel.gl.array.GlVertexArray;
import com.jozufozu.flywheel.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.lib.memory.MemoryBlock;
import com.jozufozu.flywheel.lib.model.QuadIndexSequence;
+import com.jozufozu.flywheel.lib.vertex.BlockVertex;
import com.jozufozu.flywheel.lib.vertex.BlockVertexList;
-import com.jozufozu.flywheel.lib.vertex.VertexTypes;
public class IndirectMeshPool {
- private static final VertexType VERTEX_TYPE = VertexTypes.BLOCK;
+ private static final BufferLayout LAYOUT = BlockVertex.FORMAT;
private final Map meshes = new HashMap<>();
private final List meshList = new ArrayList<>();
@@ -40,13 +39,8 @@ public class IndirectMeshPool {
vertexArray = GlVertexArray.create();
vertexArray.setElementBuffer(ebo.handle());
- BufferLayout layout = VERTEX_TYPE.getLayout();
- vertexArray.bindVertexBuffer(0, vbo.handle(), 0, layout.getStride());
- vertexArray.bindAttributes(0, 0, layout.attributes());
- }
-
- public VertexType getVertexType() {
- return VERTEX_TYPE;
+ vertexArray.bindVertexBuffer(0, vbo.handle(), 0, LAYOUT.getStride());
+ vertexArray.bindAttributes(0, 0, LAYOUT.attributes());
}
/**
@@ -160,7 +154,7 @@ public class IndirectMeshPool {
}
public int size() {
- return mesh.vertexCount() * VERTEX_TYPE.getStride();
+ return mesh.vertexCount() * LAYOUT.getStride();
}
public int indexCount() {
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java
index 4ceb6655b..cb47de048 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java
@@ -13,15 +13,15 @@ import org.lwjgl.opengl.GL32;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.api.model.Mesh;
-import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.gl.GlPrimitive;
import com.jozufozu.flywheel.gl.array.GlVertexArray;
import com.jozufozu.flywheel.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.gl.buffer.MappedBuffer;
-import com.jozufozu.flywheel.lib.vertex.VertexTypes;
+import com.jozufozu.flywheel.lib.vertex.BlockVertex;
+import com.jozufozu.flywheel.lib.vertex.BlockVertexList;
public class InstancedMeshPool {
- private static final VertexType VERTEX_TYPE = VertexTypes.BLOCK;
+ private static final BufferLayout LAYOUT = BlockVertex.FORMAT;
private final Map meshes = new HashMap<>();
private final List allBuffered = new ArrayList<>();
@@ -37,16 +37,11 @@ public class InstancedMeshPool {
* Create a new mesh pool.
*/
public InstancedMeshPool() {
- int stride = VERTEX_TYPE.getLayout()
- .getStride();
+ int stride = LAYOUT.getStride();
vbo = new GlBuffer();
vbo.growthFunction(l -> Math.max(l + stride * 128L, (long) (l * 1.6)));
}
- public VertexType getVertexType() {
- return VERTEX_TYPE;
- }
-
/**
* Allocate a mesh in the arena.
*
@@ -118,7 +113,7 @@ public class InstancedMeshPool {
try (MappedBuffer mapped = vbo.map()) {
long ptr = mapped.ptr();
- var vertexList = VERTEX_TYPE.createVertexList();
+ var vertexList = new BlockVertexList();
for (BufferedMesh mesh : pendingUpload) {
vertexList.ptr(ptr + mesh.byteIndex);
@@ -141,7 +136,7 @@ public class InstancedMeshPool {
@Override
public String toString() {
- return "InstancedMeshPool{" + "vertexType=" + VERTEX_TYPE + ", byteSize=" + byteSize + ", meshCount=" + meshes.size() + '}';
+ return "InstancedMeshPool{" + "byteSize=" + byteSize + ", meshCount=" + meshes.size() + '}';
}
public class BufferedMesh {
@@ -159,16 +154,11 @@ public class InstancedMeshPool {
}
public int size() {
- return mesh.vertexCount() * VERTEX_TYPE.getStride();
- }
-
- public VertexType getVertexType() {
- return VERTEX_TYPE;
+ return mesh.vertexCount() * LAYOUT.getStride();
}
public int getAttributeCount() {
- return VERTEX_TYPE.getLayout()
- .getAttributeCount();
+ return LAYOUT.getAttributeCount();
}
public boolean isDeleted() {
@@ -181,9 +171,8 @@ public class InstancedMeshPool {
public void setup(GlVertexArray vao) {
if (boundTo.add(vao)) {
- BufferLayout type = VERTEX_TYPE.getLayout();
- vao.bindVertexBuffer(0, InstancedMeshPool.this.vbo.handle(), byteIndex, type.getStride());
- vao.bindAttributes(0, 0, type.attributes());
+ vao.bindVertexBuffer(0, InstancedMeshPool.this.vbo.handle(), byteIndex, LAYOUT.getStride());
+ vao.bindAttributes(0, 0, LAYOUT.attributes());
vao.setElementBuffer(ebo);
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java
index 2cf7df856..8349754f8 100644
--- a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java
@@ -62,7 +62,7 @@ public final class ModelUtil {
VertexFormat srcFormat = drawState.format();
ByteBuffer src = buffer.vertexBuffer();
- MemoryBlock dst = MemoryBlock.malloc((long) vertexCount * vertexType.getLayout().getStride());
+ MemoryBlock dst = MemoryBlock.malloc((long) vertexCount * vertexType.getStride());
long srcPtr = MemoryUtil.memAddress(src);
long dstPtr = dst.ptr();
diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/SimpleMesh.java b/src/main/java/com/jozufozu/flywheel/lib/model/SimpleMesh.java
index 742b7fe83..76220bafb 100644
--- a/src/main/java/com/jozufozu/flywheel/lib/model/SimpleMesh.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/model/SimpleMesh.java
@@ -24,13 +24,13 @@ public class SimpleMesh implements QuadMesh {
this.descriptor = descriptor;
int bytes = (int) data.size();
- int stride = vertexType.getLayout().getStride();
+ int stride = vertexType.getStride();
if (bytes % stride != 0) {
throw new IllegalArgumentException("MemoryBlock contains non-whole amount of vertices!");
}
vertexCount = bytes / stride;
- vertexList = vertexType().createVertexList();
+ vertexList = this.vertexType.createVertexList();
vertexList.ptr(data.ptr());
vertexList.vertexCount(vertexCount);
@@ -41,21 +41,11 @@ public class SimpleMesh implements QuadMesh {
this(vertexType, data, null);
}
- @Override
- public VertexType vertexType() {
- return vertexType;
- }
-
@Override
public int vertexCount() {
return vertexCount;
}
- @Override
- public void write(long ptr) {
- data.copyTo(ptr);
- }
-
@Override
public void write(MutableVertexList dst) {
vertexList.writeAll(dst);
diff --git a/src/main/java/com/jozufozu/flywheel/lib/vertex/BlockVertex.java b/src/main/java/com/jozufozu/flywheel/lib/vertex/BlockVertex.java
index 2c2aa7a6d..9f16e570f 100644
--- a/src/main/java/com/jozufozu/flywheel/lib/vertex/BlockVertex.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/vertex/BlockVertex.java
@@ -15,8 +15,8 @@ public class BlockVertex implements VertexType {
.build();
@Override
- public BufferLayout getLayout() {
- return FORMAT;
+ public int getStride() {
+ return FORMAT.getStride();
}
@Override
diff --git a/src/main/java/com/jozufozu/flywheel/lib/vertex/PosTexNormalVertex.java b/src/main/java/com/jozufozu/flywheel/lib/vertex/PosTexNormalVertex.java
index be92aca18..bf2343aad 100644
--- a/src/main/java/com/jozufozu/flywheel/lib/vertex/PosTexNormalVertex.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/vertex/PosTexNormalVertex.java
@@ -10,10 +10,9 @@ public class PosTexNormalVertex implements VertexType {
.addItem(CommonItems.VEC2, "tex")
.addItem(CommonItems.NORM_3x8, "normal")
.build();
-
@Override
- public BufferLayout getLayout() {
- return FORMAT;
+ public int getStride() {
+ return FORMAT.getStride();
}
@Override