improve MapRenderer

- add functions to bind shared VBOs
- make MapView field non-static
This commit is contained in:
Hannes Janetzek 2014-01-17 19:19:01 +01:00
parent 5e27740761
commit 8ea6fef1ca
9 changed files with 65 additions and 64 deletions

@ -1 +1 @@
Subproject commit b983af2578d3b72e5cb8839d43c4c366d07d2d46 Subproject commit 6093f28947a9188f58c6a362d196920dfd5d02c8

View File

@ -18,6 +18,9 @@ package org.oscim.backend;
public class GLAdapter { public class GLAdapter {
public final static boolean debug = false;
public final static boolean debugView = false;
/** The instance provided by backend */ /** The instance provided by backend */
public static GL20 g; public static GL20 g;
@ -26,8 +29,6 @@ public class GLAdapter {
public static boolean NON_PREMUL_CANVAS; public static boolean NON_PREMUL_CANVAS;
public final static boolean debug = true;
public static GL20 get() { public static GL20 get() {
if (g == null) if (g == null)
throw new IllegalStateException(); throw new IllegalStateException();

View File

@ -35,6 +35,7 @@ public class GLState {
static void init(GL20 gl) { static void init(GL20 gl) {
GL = gl; GL = gl;
vertexArray[0] = false; vertexArray[0] = false;
vertexArray[1] = false; vertexArray[1] = false;
blend = false; blend = false;
@ -44,6 +45,7 @@ public class GLState {
GL.glDisable(GL20.GL_STENCIL_TEST); GL.glDisable(GL20.GL_STENCIL_TEST);
GL.glDisable(GL20.GL_DEPTH_TEST); GL.glDisable(GL20.GL_DEPTH_TEST);
GL.glDisable(GL20.GL_BLEND);
} }
public static boolean useProgram(int shaderProgram) { public static boolean useProgram(int shaderProgram) {

View File

@ -24,6 +24,7 @@ import java.nio.ShortBuffer;
import org.oscim.backend.GL20; import org.oscim.backend.GL20;
import org.oscim.backend.GLAdapter; import org.oscim.backend.GLAdapter;
import org.oscim.backend.canvas.Color;
import org.oscim.core.MapPosition; import org.oscim.core.MapPosition;
import org.oscim.map.Map; import org.oscim.map.Map;
import org.oscim.map.Viewport; import org.oscim.map.Viewport;
@ -41,12 +42,10 @@ public class MapRenderer {
/** scale factor used for short vertices */ /** scale factor used for short vertices */
public static final float COORD_SCALE = 8.0f; public static final float COORD_SCALE = 8.0f;
private static Map mMap;
public static int screenWidth, screenHeight; public static int screenWidth, screenHeight;
private static Viewport mViewport; private final Map mMap;
private static MapPosition mMapPosition; private final MapPosition mMapPosition;
public class Matrices { public class Matrices {
@ -81,16 +80,15 @@ public class MapRenderer {
} }
} }
private static Matrices mMatrices; private final Matrices mMatrices;
// private private static float[] mClearColor = null;
static float[] mClearColor = null;
public static int mQuadIndicesID; private static int mQuadIndicesID;
private static int mQuadVerticesID; private static int mQuadVerticesID;
public final static int maxQuads = 64; public final static int maxQuads = 64;
private static boolean mUpdateColor = false; private static volatile boolean mUpdateColor = false;
// drawlock to synchronize Main- and GL-Thread // drawlock to synchronize Main- and GL-Thread
// static ReentrantLock tilelock = new ReentrantLock(); // static ReentrantLock tilelock = new ReentrantLock();
@ -144,7 +142,6 @@ public class MapRenderer {
public MapRenderer(Map map) { public MapRenderer(Map map) {
mMap = map; mMap = map;
mViewport = map.getViewport();
mMapPosition = new MapPosition(); mMapPosition = new MapPosition();
mMatrices = new Matrices(); mMatrices = new Matrices();
@ -153,6 +150,7 @@ public class MapRenderer {
// FIXME should be done in 'destroy' method // FIXME should be done in 'destroy' method
// clear all previous vbo refs // clear all previous vbo refs
BufferObject.clear(); BufferObject.clear();
setBackgroundColor(Color.DKGRAY);
} }
public static void setBackgroundColor(int color) { public static void setBackgroundColor(int color) {
@ -223,7 +221,7 @@ public class MapRenderer {
mBufferPool.releaseBuffers(); mBufferPool.releaseBuffers();
} }
private static void draw() { private void draw() {
if (mUpdateColor) { if (mUpdateColor) {
float cc[] = mClearColor; float cc[] = mClearColor;
@ -238,34 +236,35 @@ public class MapRenderer {
| GL20.GL_STENCIL_BUFFER_BIT); | GL20.GL_STENCIL_BUFFER_BIT);
GLState.blend(false); GLState.blend(false);
GL.glDisable(GL20.GL_BLEND); GLState.bindTex2D(-1);
GLState.useProgram(-1);
boolean changed = false; boolean changed = false;
MapPosition pos = mMapPosition; MapPosition pos = mMapPosition;
synchronized (mViewport) { Viewport viewport = mMap.getViewport();
synchronized (viewport) {
mMap.getAnimator().updateAnimation(); mMap.getAnimator().updateAnimation();
// get current MapPosition // get current MapPosition
changed = mViewport.getMapPosition(pos); changed = viewport.getMapPosition(pos);
if (changed) if (changed)
mViewport.getMapViewProjection(mMatrices.mapPlane); viewport.getMapViewProjection(mMatrices.mapPlane);
mViewport.getMatrix(mMatrices.view, mMatrices.proj, mMatrices.viewproj); viewport.getMatrix(mMatrices.view, mMatrices.proj, mMatrices.viewproj);
if (debugView) { if (GLAdapter.debugView) {
// modify this to scale only the view, to see
// which tiles are rendered
mMatrices.mvp.setScale(0.5f, 0.5f, 1); mMatrices.mvp.setScale(0.5f, 0.5f, 1);
mMatrices.viewproj.multiplyLhs(mMatrices.mvp); mMatrices.viewproj.multiplyLhs(mMatrices.mvp);
mMatrices.proj.multiplyLhs(mMatrices.mvp);
} }
} }
//log.debug("begin frame");
GLState.bindTex2D(-1);
GLState.useProgram(-1);
//GL.glBindTexture(GL20.GL_TEXTURE_2D, 0);
/* update layers */ /* update layers */
LayerRenderer[] layers = mMap.getLayers().getLayerRenderer(); LayerRenderer[] layers = mMap.getLayers().getLayerRenderer();
@ -279,11 +278,11 @@ public class MapRenderer {
renderer.update(pos, changed, mMatrices); renderer.update(pos, changed, mMatrices);
if (renderer.isReady)
renderer.render(pos, mMatrices);
if (GLAdapter.debug) if (GLAdapter.debug)
GLUtils.checkGlError(renderer.getClass().getName()); GLUtils.checkGlError(renderer.getClass().getName());
if (renderer.isReady)
renderer.render(mMapPosition, mMatrices);
} }
if (GLUtils.checkGlOutOfMemory("finish")) { if (GLUtils.checkGlOutOfMemory("finish")) {
@ -305,14 +304,7 @@ public class MapRenderer {
screenWidth = width; screenWidth = width;
screenHeight = height; screenHeight = height;
mViewport.getMatrix(null, mMatrices.proj, null); mMap.getViewport().getMatrix(null, mMatrices.proj, null);
if (debugView) {
// modify this to scale only the view, to see better which tiles
// are rendered
mMatrices.mvp.setScale(0.5f, 0.5f, 1);
mMatrices.proj.multiplyLhs(mMatrices.mvp);
}
GL.glViewport(0, 0, width, height); GL.glViewport(0, 0, width, height);
GL.glScissor(0, 0, width, height); GL.glScissor(0, 0, width, height);
@ -381,8 +373,6 @@ public class MapRenderer {
public void onSurfaceCreated() { public void onSurfaceCreated() {
GL = GLAdapter.get(); GL = GLAdapter.get();
// log.debug("surface created");
// log.debug(GL.glGetString(GL20.GL_EXTENSIONS)); // log.debug(GL.glGetString(GL20.GL_EXTENSIONS));
GLState.init(GL); GLState.init(GL);
@ -393,7 +383,6 @@ public class MapRenderer {
// classes that require GL context for initialization // classes that require GL context for initialization
ElementLayers.initRenderer(GL); ElementLayers.initRenderer(GL);
LayerRenderer.init(GL); LayerRenderer.init(GL);
mNewSurface = true; mNewSurface = true;
@ -401,18 +390,26 @@ public class MapRenderer {
private boolean mNewSurface; private boolean mNewSurface;
public static final boolean debugView = false; /**
* Bind VBO for a simple quad. Handy for simple custom RenderLayers
public static int getQuadIndicesVBO() { * Vertices: float[]{ -1, -1, -1, 1, 1, -1, 1, 1 }
return mQuadIndicesID; *
* @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)
GL.glVertexAttribPointer(location, 2, GL20.GL_FLOAT, false, 0, 0);
} }
/** /**
* Get VBO ID for a simple quad. Handy for simple custom RenderLayers * Bind indices for rendering up to 64 (MapRenderer.maxQuads) in
* Vertices: { -1, -1, -1, 1, 1, -1, 1, 1 } * one draw call. Vertex order is 0-1-2 2-1-3
*/ *
public static int getQuadVertexVBO() { * @param bind - true to activate, false to unbind (dont forget!)
return mQuadVerticesID; * */
public static void bindQuadIndicesVBO(boolean bind) {
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, bind ? mQuadIndicesID : 0);
} }
} }

View File

@ -207,7 +207,7 @@ public class BitmapLayer extends TextureLayer {
m.mvp.setAsUniform(hTextureMVMatrix); m.mvp.setAsUniform(hTextureMVMatrix);
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, MapRenderer.mQuadIndicesID); MapRenderer.bindQuadIndicesVBO(true);
for (TextureItem ti = tl.textures; ti != null; ti = ti.next) { for (TextureItem ti = tl.textures; ti != null; ti = ti.next) {
@ -235,7 +235,7 @@ public class BitmapLayer extends TextureLayer {
} }
} }
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); MapRenderer.bindQuadIndicesVBO(false);
return renderElement.next; return renderElement.next;
} }

View File

@ -348,8 +348,8 @@ public final class LineTexLayer extends RenderElement {
m.mvp.setAsUniform(hMatrix); m.mvp.setAsUniform(hMatrix);
int maxIndices = MapRenderer.maxQuads * 6; int maxIndices = MapRenderer.maxQuads * 6;
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER,
MapRenderer.mQuadIndicesID); MapRenderer.bindQuadIndicesVBO(true);
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID); GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID);
GL.glVertexAttribPointer(hVertexFlip, 1, GL.glVertexAttribPointer(hVertexFlip, 1,
@ -446,7 +446,7 @@ public final class LineTexLayer extends RenderElement {
//GlUtils.checkGlError(TAG); //GlUtils.checkGlError(TAG);
} }
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); MapRenderer.bindQuadIndicesVBO(false);
GL.glDisableVertexAttribArray(hVertexPosition0); GL.glDisableVertexAttribArray(hVertexPosition0);
GL.glDisableVertexAttribArray(hVertexPosition1); GL.glDisableVertexAttribArray(hVertexPosition1);

View File

@ -22,6 +22,7 @@ import java.nio.FloatBuffer;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
import org.oscim.backend.GL20; import org.oscim.backend.GL20;
import org.oscim.backend.GLAdapter;
import org.oscim.core.GeometryBuffer; import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapPosition; import org.oscim.core.MapPosition;
import org.oscim.core.Tile; import org.oscim.core.Tile;
@ -154,7 +155,7 @@ public final class PolygonLayer extends RenderElement {
// Set up the program for rendering polygons // Set up the program for rendering polygons
if (i == 0) { if (i == 0) {
if (MapRenderer.debugView) if (GLAdapter.debugView)
polygonProgram[i] = GLUtils.createProgram(polygonVertexShaderZ, polygonProgram[i] = GLUtils.createProgram(polygonVertexShaderZ,
polygonFragmentShaderZ); polygonFragmentShaderZ);
else else

View File

@ -133,14 +133,14 @@ public class TextureItem extends Inlist<TextureItem> {
pool = null; pool = null;
} }
if (num > 0) {
int[] textureIds = GLUtils.glGenTextures(num); int[] textureIds = GLUtils.glGenTextures(num);
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
TextureItem to = new TextureItem(textureIds[i]); TextureItem to = new TextureItem(textureIds[i]);
initTexture(to); initTexture(to);
pool = Inlist.push(pool, to); pool = Inlist.push(pool, to);
} }
}
fill = num; fill = num;
} }

View File

@ -144,7 +144,7 @@ public abstract class TextureLayer extends RenderElement {
m.proj.setAsUniform(hTextureProjMatrix); m.proj.setAsUniform(hTextureProjMatrix);
m.mvp.setAsUniform(hTextureMVMatrix); m.mvp.setAsUniform(hTextureMVMatrix);
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, MapRenderer.mQuadIndicesID); MapRenderer.bindQuadIndicesVBO(true);
for (TextureItem ti = tl.textures; ti != null; ti = ti.next) { for (TextureItem ti = tl.textures; ti != null; ti = ti.next) {
@ -176,7 +176,7 @@ public abstract class TextureLayer extends RenderElement {
} }
} }
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0); MapRenderer.bindQuadIndicesVBO(false);
return renderElement.next; return renderElement.next;
} }