GLState: track buffer bindings

- reset buffer bindings at frame start
This commit is contained in:
Hannes Janetzek 2014-09-05 05:58:25 +02:00
parent 3f8b028d60
commit 3a30476f7e
8 changed files with 81 additions and 36 deletions

View File

@ -135,6 +135,7 @@ public class VectorTileRenderer extends TileRenderer {
continue;
drawGrandParent(t, v);
}
GL.glDepthMask(false);
/* make sure stencil buffer write is disabled */
@ -177,7 +178,6 @@ public class VectorTileRenderer extends TileRenderer {
buckets.bind();
PolygonBucket.Renderer.clip(mClipMVP, mClipMode);
boolean first = true;
for (RenderBucket b = buckets.get(); b != null;) {
@ -193,8 +193,6 @@ public class VectorTileRenderer extends TileRenderer {
break;
case TEXLINE:
b = LineTexBucket.Renderer.draw(b, v, div, buckets);
if (buckets.ibo != null)
buckets.ibo.bind();
break;
case MESH:
b = MeshBucket.Renderer.draw(b, v);
@ -211,6 +209,8 @@ public class VectorTileRenderer extends TileRenderer {
b = b.next;
break;
}
/* make sure buffers are bound again */
buckets.bind();
}

View File

@ -72,8 +72,6 @@ public abstract class BucketRenderer extends LayerRenderer {
protected synchronized void render(GLViewport v) {
MapPosition layerPos = mMapPosition;
buckets.bind();
GLState.test(false, false);
GLState.blend(true);
@ -82,6 +80,9 @@ public abstract class BucketRenderer extends LayerRenderer {
setMatrix(v, true);
for (RenderBucket b = buckets.get(); b != null;) {
buckets.bind();
switch (b.type) {
case POLYGON:
b = PolygonBucket.Renderer.draw(b, v, 1, true);
@ -91,8 +92,6 @@ public abstract class BucketRenderer extends LayerRenderer {
break;
case TEXLINE:
b = LineTexBucket.Renderer.draw(b, v, div, buckets);
// rebind
buckets.ibo.bind();
break;
case MESH:
b = MeshBucket.Renderer.draw(b, v);
@ -102,13 +101,9 @@ public abstract class BucketRenderer extends LayerRenderer {
break;
case BITMAP:
b = BitmapBucket.Renderer.draw(b, v, 1, 1);
// rebind
buckets.ibo.bind();
break;
case SYMBOL:
b = TextureBucket.Renderer.draw(buckets, b, v, div);
// rebind
buckets.ibo.bind();
break;
default:
log.error("invalid bucket {}", b.type);

View File

@ -59,7 +59,7 @@ public final class BufferObject extends Inlist<BufferObject> {
buf.flip();
}
GL.glBindBuffer(target, id);
GLState.bindBuffer(target, id);
/* reuse memory allocated for vbo when possible and allocated
* memory is less then four times the new data */
@ -75,11 +75,11 @@ public final class BufferObject extends Inlist<BufferObject> {
}
public void bind() {
GL.glBindBuffer(target, id);
GLState.bindBuffer(target, id);
}
public void unbind() {
GL.glBindBuffer(target, 0);
GLState.bindBuffer(target, 0);
}
// ---------------------------- pool ----------------------------

View File

@ -240,7 +240,6 @@ public abstract class ExtrusionRenderer extends LayerRenderer {
if (v.pos.zoomLevel < 18)
GL.glDisable(GL20.GL_CULL_FACE);
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
}
private static void setMatrix(GLViewport v, ExtrusionBuckets l, boolean offset) {

View File

@ -31,6 +31,8 @@ public class GLState {
private static boolean stencil = false;
private static int shader;
private static float[] clearColor;
private static int glVertexBuffer;
private static int glIndexBuffer;
private static int currentTexId;
@ -44,6 +46,8 @@ public class GLState {
stencil = false;
shader = -1;
currentTexId = -1;
glVertexBuffer = -1;
glIndexBuffer = -1;
clearColor = null;
GL.glDisable(GL20.GL_STENCIL_TEST);
@ -158,4 +162,48 @@ public class GLState {
GL.glClearColor(color[0], color[1], color[2], color[3]);
}
public static void bindBuffer(int target, int id) {
//log.debug(">> buffer {} {}", target == GL20.GL_ARRAY_BUFFER, id);
if (target == GL20.GL_ARRAY_BUFFER) {
if (glVertexBuffer == id)
return;
glVertexBuffer = id;
}
else if (target == GL20.GL_ELEMENT_ARRAY_BUFFER) {
if (glIndexBuffer == id)
return;
glIndexBuffer = id;
}
else {
log.debug("invalid target {}", target);
return;
}
//log.debug("bind buffer {} {}", target == GL20.GL_ARRAY_BUFFER, id);
if (id >= 0)
GL.glBindBuffer(target, id);
}
public static void bindElementBuffer(int id) {
if (glIndexBuffer == id)
return;
glIndexBuffer = id;
if (id >= 0)
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, id);
}
public static void bindVertexBuffer(int id) {
if (glVertexBuffer == id)
return;
glVertexBuffer = id;
if (id >= 0)
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, id);
}
}

View File

@ -88,9 +88,12 @@ public class MapRenderer {
GL.glDepthMask(false);
GL.glStencilMask(0);
GLState.test(false, false);
GLState.blend(false);
GLState.bindTex2D(-1);
GLState.useProgram(-1);
GLState.bindElementBuffer(-1);
GLState.bindVertexBuffer(-1);
mMap.animator().updateAnimation();
mViewport.setFrom(mMap.viewport());
@ -180,11 +183,11 @@ public class MapRenderer {
buf.put(indices);
buf.flip();
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER,
mQuadIndicesID);
GLState.bindElementBuffer(mQuadIndicesID);
GL.glBufferData(GL20.GL_ELEMENT_ARRAY_BUFFER,
indices.length * 2, buf, GL20.GL_STATIC_DRAW);
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
indices.length * 2, buf,
GL20.GL_STATIC_DRAW);
GLState.bindElementBuffer(0);
/** initialize default quad */
FloatBuffer floatBuffer = MapRenderer.getFloatBuffer(8);
@ -193,10 +196,11 @@ public class MapRenderer {
floatBuffer.flip();
mQuadVerticesID = vboIds[1];
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mQuadVerticesID);
GLState.bindVertexBuffer(mQuadVerticesID);
GL.glBufferData(GL20.GL_ARRAY_BUFFER,
quad.length * 4, floatBuffer, GL20.GL_STATIC_DRAW);
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);
quad.length * 4, floatBuffer,
GL20.GL_STATIC_DRAW);
GLState.bindVertexBuffer(0);
GLState.init(GL);
@ -238,13 +242,14 @@ public class MapRenderer {
* Vertices: float[]{ -1, -1, -1, 1, 1, -1, 1, 1 }
*
* GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4);
*
* @param bind - true to activate, false to unbind
*/
public static void bindQuadVertexVBO(int location, boolean bind) {
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mQuadVerticesID);
if (location >= 0)
public static void bindQuadVertexVBO(int location) {
if (location >= 0) {
GLState.bindVertexBuffer(mQuadVerticesID);
GLState.enableVertexArrays(location, -1);
GL.glVertexAttribPointer(location, 2, GL20.GL_FLOAT, false, 0, 0);
}
}
/**
@ -254,7 +259,8 @@ public class MapRenderer {
* @param bind - true to activate, false to unbind (dont forget!)
* */
public static void bindQuadIndicesVBO(boolean bind) {
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, bind ? mQuadIndicesID : 0);
GLState.bindElementBuffer(bind ? mQuadIndicesID : 0);
}
/**

View File

@ -213,14 +213,12 @@ public class OffscreenRenderer extends LayerRenderer {
GLState.bindTex2D(renderTex);
GL.glUniform1i(mShader.uTexColor, 0);
MapRenderer.bindQuadVertexVBO(mShader.aPos, true);
MapRenderer.bindQuadVertexVBO(mShader.aPos);
GL.glUniform2f(mShader.uPixel,
(float) (1.0 / texW * 0.5),
(float) (1.0 / texH * 0.5));
GLState.enableVertexArrays(mShader.aPos, -1);
GLState.test(false, false);
GLState.blend(true);
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4);

View File

@ -284,10 +284,11 @@ public final class LineTexBucket extends RenderBucket {
ShortBuffer sbuf = buf.asShortBuffer();
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID);
//GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID);
GLState.bindVertexBuffer(mVertexFlipID);
GL.glBufferData(GL20.GL_ARRAY_BUFFER, flip.length, sbuf,
GL20.GL_STATIC_DRAW);
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);
GLState.bindVertexBuffer(0);
// mTexID = new int[10];
// byte[] stipple = new byte[2];
@ -329,7 +330,7 @@ public final class LineTexBucket extends RenderBucket {
MapRenderer.bindQuadIndicesVBO(true);
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID);
GLState.bindVertexBuffer(mVertexFlipID);
GL.glVertexAttribPointer(shader.aFlip, 1,
GL20.GL_BYTE, false, 0, 0);
@ -419,8 +420,6 @@ public final class LineTexBucket extends RenderBucket {
//GlUtils.checkGlError(TAG);
}
MapRenderer.bindQuadIndicesVBO(false);
GL.glDisableVertexAttribArray(aPos0);
GL.glDisableVertexAttribArray(aPos1);
GL.glDisableVertexAttribArray(aLen0);