From a51c3719fcfec10ce8f7f23f44f3168c7dbae3aa Mon Sep 17 00:00:00 2001 From: sisyphussy <221229741+sisyphussy@users.noreply.github.com> Date: Tue, 28 Oct 2025 16:41:29 +0100 Subject: [PATCH] Better VertexBuffer API (#170) Co-authored-by: RecursivePineapple --- .../client/renderer/vbo/VertexBuffer.java | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vbo/VertexBuffer.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vbo/VertexBuffer.java index da5dcfe7..8645fc5d 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vbo/VertexBuffer.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vbo/VertexBuffer.java @@ -10,17 +10,14 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; public class VertexBuffer implements AutoCloseable { - private int id; - private int vertexCount; - private VertexFormat format; - private int drawMode; - - public VertexBuffer() { - this.id = GL15.glGenBuffers(); - } + protected int id; + protected int vertexCount; + protected final VertexFormat format; + protected final int drawMode; public VertexBuffer(VertexFormat format, int drawMode) { - this(); + if (format == null) throw new IllegalStateException("No format specified for VBO"); + this.id = GL15.glGenBuffers(); this.format = format; this.drawMode = drawMode; } @@ -34,19 +31,45 @@ public class VertexBuffer implements AutoCloseable { } public void upload(ByteBuffer buffer, int vertexCount) { + upload(buffer, vertexCount, GL15.GL_STATIC_DRAW); + } + + public void upload(ByteBuffer buffer, int vertexCount, int type) { if (this.id == -1) return; this.vertexCount = vertexCount; this.bind(); - GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); + GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, type); this.unbind(); } public VertexBuffer upload(ByteBuffer buffer) { - if (format == null) throw new IllegalStateException("No format specified for VBO upload"); - upload(buffer, buffer.remaining() / format.getVertexSize()); + upload(buffer, buffer.remaining() / format.getVertexSize(), GL15.GL_STATIC_DRAW); return this; } + /** + * GL_DYNAMIC_DRAW is more efficient for buffers that have their values constantly updated and read multiple times. + */ + public void uploadDynamic(ByteBuffer buffer, int vertexCount) { + upload(buffer, vertexCount, GL15.GL_DYNAMIC_DRAW); + } + + public void uploadDynamic(ByteBuffer buffer) { + upload(buffer, buffer.remaining() / format.getVertexSize(), GL15.GL_DYNAMIC_DRAW); + } + + /** + * GL_STREAM_DRAW is more efficient for buffers that have their values constantly updated and read at most a few + * times (example: uploading a set of vertices that only get used a few times at most) + */ + public void uploadStream(ByteBuffer buffer, int vertexCount) { + upload(buffer, vertexCount, GL15.GL_STREAM_DRAW); + } + + public void uploadStream(ByteBuffer buffer) { + upload(buffer, buffer.remaining() / format.getVertexSize(), GL15.GL_STREAM_DRAW); + } + public void close() { if (this.id >= 0) { GL15.glDeleteBuffers(this.id); @@ -67,7 +90,6 @@ public class VertexBuffer implements AutoCloseable { } public void setupState() { - if (format == null) throw new IllegalStateException("No format specified for VBO setup"); bind(); format.setupBufferState(0L); } @@ -86,4 +108,12 @@ public class VertexBuffer implements AutoCloseable { public VertexFormat getVertexFormat() { return format; } + + public int getDrawMode() { + return drawMode; + } + + public int getVertexCount() { + return vertexCount; + } }