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

View File

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

View File

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

View File

@ -240,7 +240,6 @@ public abstract class ExtrusionRenderer extends LayerRenderer {
if (v.pos.zoomLevel < 18) if (v.pos.zoomLevel < 18)
GL.glDisable(GL20.GL_CULL_FACE); GL.glDisable(GL20.GL_CULL_FACE);
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
} }
private static void setMatrix(GLViewport v, ExtrusionBuckets l, boolean offset) { 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 boolean stencil = false;
private static int shader; private static int shader;
private static float[] clearColor; private static float[] clearColor;
private static int glVertexBuffer;
private static int glIndexBuffer;
private static int currentTexId; private static int currentTexId;
@ -44,6 +46,8 @@ public class GLState {
stencil = false; stencil = false;
shader = -1; shader = -1;
currentTexId = -1; currentTexId = -1;
glVertexBuffer = -1;
glIndexBuffer = -1;
clearColor = null; clearColor = null;
GL.glDisable(GL20.GL_STENCIL_TEST); GL.glDisable(GL20.GL_STENCIL_TEST);
@ -158,4 +162,48 @@ public class GLState {
GL.glClearColor(color[0], color[1], color[2], color[3]); 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.glDepthMask(false);
GL.glStencilMask(0); GL.glStencilMask(0);
GLState.test(false, false);
GLState.blend(false); GLState.blend(false);
GLState.bindTex2D(-1); GLState.bindTex2D(-1);
GLState.useProgram(-1); GLState.useProgram(-1);
GLState.bindElementBuffer(-1);
GLState.bindVertexBuffer(-1);
mMap.animator().updateAnimation(); mMap.animator().updateAnimation();
mViewport.setFrom(mMap.viewport()); mViewport.setFrom(mMap.viewport());
@ -180,11 +183,11 @@ public class MapRenderer {
buf.put(indices); buf.put(indices);
buf.flip(); buf.flip();
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, GLState.bindElementBuffer(mQuadIndicesID);
mQuadIndicesID);
GL.glBufferData(GL20.GL_ELEMENT_ARRAY_BUFFER, GL.glBufferData(GL20.GL_ELEMENT_ARRAY_BUFFER,
indices.length * 2, buf, GL20.GL_STATIC_DRAW); indices.length * 2, buf,
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); GL20.GL_STATIC_DRAW);
GLState.bindElementBuffer(0);
/** initialize default quad */ /** initialize default quad */
FloatBuffer floatBuffer = MapRenderer.getFloatBuffer(8); FloatBuffer floatBuffer = MapRenderer.getFloatBuffer(8);
@ -193,10 +196,11 @@ public class MapRenderer {
floatBuffer.flip(); floatBuffer.flip();
mQuadVerticesID = vboIds[1]; mQuadVerticesID = vboIds[1];
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mQuadVerticesID); GLState.bindVertexBuffer(mQuadVerticesID);
GL.glBufferData(GL20.GL_ARRAY_BUFFER, GL.glBufferData(GL20.GL_ARRAY_BUFFER,
quad.length * 4, floatBuffer, GL20.GL_STATIC_DRAW); quad.length * 4, floatBuffer,
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0); GL20.GL_STATIC_DRAW);
GLState.bindVertexBuffer(0);
GLState.init(GL); GLState.init(GL);
@ -238,13 +242,14 @@ public class MapRenderer {
* Vertices: float[]{ -1, -1, -1, 1, 1, -1, 1, 1 } * Vertices: float[]{ -1, -1, -1, 1, 1, -1, 1, 1 }
* *
* GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4); * GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4);
*
* @param bind - true to activate, false to unbind
*/ */
public static void bindQuadVertexVBO(int location, boolean bind) { public static void bindQuadVertexVBO(int location) {
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mQuadVerticesID);
if (location >= 0) if (location >= 0) {
GLState.bindVertexBuffer(mQuadVerticesID);
GLState.enableVertexArrays(location, -1);
GL.glVertexAttribPointer(location, 2, GL20.GL_FLOAT, false, 0, 0); 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!) * @param bind - true to activate, false to unbind (dont forget!)
* */ * */
public static void bindQuadIndicesVBO(boolean bind) { 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); GLState.bindTex2D(renderTex);
GL.glUniform1i(mShader.uTexColor, 0); GL.glUniform1i(mShader.uTexColor, 0);
MapRenderer.bindQuadVertexVBO(mShader.aPos, true); MapRenderer.bindQuadVertexVBO(mShader.aPos);
GL.glUniform2f(mShader.uPixel, GL.glUniform2f(mShader.uPixel,
(float) (1.0 / texW * 0.5), (float) (1.0 / texW * 0.5),
(float) (1.0 / texH * 0.5)); (float) (1.0 / texH * 0.5));
GLState.enableVertexArrays(mShader.aPos, -1);
GLState.test(false, false); GLState.test(false, false);
GLState.blend(true); GLState.blend(true);
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4); GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4);

View File

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