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

View File

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

View File

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

View File

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

View File

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