From 7a2e6c63c1187eaa2c472adea105f57dec676e86 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 26 Aug 2016 18:16:09 -0400 Subject: [PATCH] v0.4.2: vertex buffers now support selective updating --- .../java/com/watabou/glwrap/Vertexbuffer.java | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/glwrap/Vertexbuffer.java b/SPD-classes/src/main/java/com/watabou/glwrap/Vertexbuffer.java index de2de3815..c9204df51 100644 --- a/SPD-classes/src/main/java/com/watabou/glwrap/Vertexbuffer.java +++ b/SPD-classes/src/main/java/com/watabou/glwrap/Vertexbuffer.java @@ -30,7 +30,7 @@ public class Vertexbuffer { private int id; private FloatBuffer vertices; - private boolean needsUpdate; + private int updateStart, updateEnd; private static ArrayList buffers = new ArrayList<>(); @@ -42,24 +42,49 @@ public class Vertexbuffer { this.vertices = vertices; buffers.add(this); - //forces an update so the GL data isn't blank - needsUpdate = true; - updateGLData(); + updateStart = 0; + updateEnd = vertices.limit(); } + //For flagging the buffer for a full update without changing anything + public void updateVertices(){ + updateVertices(vertices); + } + + //For flagging an update with a full set of new data public void updateVertices( FloatBuffer vertices ){ + updateVertices(vertices, 0, vertices.limit()); + } + + //For flagging an update with a subset of data changed + public void updateVertices( FloatBuffer vertices, int start, int end){ this.vertices = vertices; - needsUpdate = true; + + if (updateStart == -1) + updateStart = start; + else + updateStart = Math.min(start, updateStart); + + if (updateEnd == -1) + updateEnd = end; + else + updateEnd = Math.max(end, updateEnd); } public void updateGLData(){ - if (!needsUpdate) return; + if (updateStart == -1) return; - vertices.position(0); + vertices.position(updateStart); bind(); - GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, (vertices.limit()*4), vertices, GLES20.GL_DYNAMIC_DRAW); + + if (updateStart == 0 && updateEnd == vertices.limit()){ + GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertices.limit()*4, vertices, GLES20.GL_DYNAMIC_DRAW); + } else { + GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, updateStart*4, (updateEnd - updateStart)*4, vertices); + } + release(); - needsUpdate = false; + updateStart = updateEnd = -1; } public void bind(){ @@ -77,7 +102,7 @@ public class Vertexbuffer { public static void refreshAllBuffers(){ for (Vertexbuffer buf : buffers) { - buf.needsUpdate = true; + buf.updateVertices(); buf.updateGLData(); } }