BufferObject: 'flip' buffer automatically when position != 0

This commit is contained in:
Hannes Janetzek 2013-10-06 20:10:04 +02:00
parent c07709803c
commit af37be4502
5 changed files with 29 additions and 34 deletions

View File

@ -24,22 +24,23 @@ import org.oscim.backend.Log;
public final class BufferObject {
private final static String TAG = BufferObject.class.getName();
private static GL20 GL;
private static final int MB = 1024 * 1024;
private static final int LIMIT_BUFFERS = 16 * MB;
// GL identifier
public int id;
// allocated bytes
public int size;
private static GL20 GL;
BufferObject next;
/** GL identifier */
private int id;
int target;
/** allocated bytes */
private int size;
BufferObject(int target, int id) {
/** GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER */
private int target;
private BufferObject next;
private BufferObject(int target, int id) {
this.id = id;
this.target = target;
}
@ -47,9 +48,10 @@ public final class BufferObject {
public void loadBufferData(Buffer buf, int newSize) {
boolean clear = false;
if (buf.position() != 0) {
Log.d(TAG, "rewind your buffer: " + buf.position());
}
if (buf.position() != 0)
buf.flip();
//throw new IllegalArgumentException("rewind buffer! " + buf.position());
GL.glBindBuffer(target, id);
@ -68,6 +70,10 @@ public final class BufferObject {
GL.glBindBuffer(target, id);
}
public void unbind() {
GL.glBindBuffer(target, 0);
}
// ---------------------------- pool ----------------------------
// bytes currently loaded in VBOs
private static int mBufferMemoryUsage;
@ -77,9 +83,7 @@ public final class BufferObject {
if (mBufferMemoryUsage < LIMIT_BUFFERS)
return;
Log.d(TAG, "buffer object usage: "
+ mBufferMemoryUsage / MB
+ "MB, force: " + force);
Log.d(TAG, "use: " + mBufferMemoryUsage / MB + "MB");
mBufferMemoryUsage -= BufferObject.limitUsage(1024 * 1024);

View File

@ -153,9 +153,8 @@ public abstract class ElementRenderer extends LayerRenderer {
sbuf.put(fillCoords, 0, 8);
layers.compile(sbuf, addFill);
sbuf.flip();
if (newSize != sbuf.remaining()) {
if (newSize != sbuf.position()) {
Log.d(TAG, "wrong size: "
+ " new size: " + newSize
+ " buffer pos: " + sbuf.position()
@ -164,10 +163,7 @@ public abstract class ElementRenderer extends LayerRenderer {
return false;
}
// * SHORT_BYTES
newSize *= 2;
layers.vbo.loadBufferData(sbuf, newSize);
layers.vbo.loadBufferData(sbuf.flip(), newSize * 2);
return true;
}

View File

@ -378,10 +378,9 @@ public class ExtrusionLayer extends RenderElement {
mNumIndices += mIndiceCnt[i];
}
sbuf.flip();
int size = mNumIndices * 2;
vboIndices = BufferObject.get(GL20.GL_ELEMENT_ARRAY_BUFFER, size);
vboIndices.loadBufferData(sbuf, size);
vboIndices.loadBufferData(sbuf.flip(), size);
GL20 GL = GLAdapter.get();
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
@ -391,10 +390,9 @@ public class ExtrusionLayer extends RenderElement {
for (VertexItem vi = mVertices; vi != null; vi = vi.next)
sbuf.put(vi.vertices, 0, vi.used);
sbuf.flip();
size = mNumVertices * 4 * 2;
vboVertices = BufferObject.get(GL20.GL_ARRAY_BUFFER, size);
vboVertices.loadBufferData(sbuf, size);
vboVertices.loadBufferData(sbuf.flip(), size);
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);

View File

@ -59,7 +59,7 @@ public class MeshLayer extends RenderElement {
verticesCnt = vertexItems.getSize() / 2;
Log.d(TAG, "-> " + verticesCnt + " " + numIndices);
//Log.d(TAG, "-> " + verticesCnt + " " + numIndices);
if (numIndices <= 0) {
vertexItems = VertexItem.pool.releaseAll(vertexItems);
@ -74,13 +74,13 @@ public class MeshLayer extends RenderElement {
return;
}
Log.d(TAG, "compile");
//Log.d(TAG, "compile");
// add vertices to shared VBO
ElementLayers.addPoolItems(this, sbuf);
int cnt = indiceItems.getSize();
Log.d(TAG, "check " + cnt + ":" + numIndices);
//Log.d(TAG, "check " + cnt + ":" + numIndices);
if (cnt != numIndices) {
numIndices = cnt;
@ -94,12 +94,10 @@ public class MeshLayer extends RenderElement {
indiceItems = VertexItem.pool.releaseAll(indiceItems);
sbuf.flip();
if (indicesVbo == null)
indicesVbo = BufferObject.get(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
indicesVbo.loadBufferData(sbuf, sbuf.limit() * 2);
indicesVbo.loadBufferData(sbuf.flip(), sbuf.limit() * 2);
}
@Override

View File

@ -82,10 +82,9 @@ public class CustomRenderer2 extends ElementRenderer {
}
FloatBuffer buf = MapRenderer.getFloatBuffer(12);
buf.put(vertices);
buf.flip();
mVBO = BufferObject.get(GL20.GL_ARRAY_BUFFER, 0);
mVBO.loadBufferData(buf, 12 * 4);
mVBO.loadBufferData(buf.flip(), 12 * 4);
setReady(true);
}