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.
This commit is contained in:
Jozufozu 2023-12-03 17:03:13 -08:00
parent 733d43e3b9
commit 4f8e6af3d0
9 changed files with 25 additions and 78 deletions

View file

@ -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;
* </p>
*
* @see com.jozufozu.flywheel.api.instance.InstanceType
* @see VertexType
*/
public class BufferLayout {

View file

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

View file

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

View file

@ -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<Mesh, BufferedMesh> meshes = new HashMap<>();
private final List<BufferedMesh> 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() {

View file

@ -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<Mesh, BufferedMesh> meshes = new HashMap<>();
private final List<BufferedMesh> 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);
}
}

View file

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

View file

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

View file

@ -15,8 +15,8 @@ public class BlockVertex implements VertexType {
.build();
@Override
public BufferLayout getLayout() {
return FORMAT;
public int getStride() {
return FORMAT.getStride();
}
@Override

View file

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