use MAX_INDICES with bindQuadIndices()

This commit is contained in:
Hannes Janetzek 2014-09-30 18:00:46 +02:00
parent b0ee833301
commit b2008aa086
4 changed files with 38 additions and 41 deletions

View File

@ -25,7 +25,6 @@ import org.oscim.backend.GLAdapter;
import org.oscim.backend.canvas.Color; import org.oscim.backend.canvas.Color;
import org.oscim.map.Map; import org.oscim.map.Map;
import org.oscim.renderer.bucket.RenderBuckets; import org.oscim.renderer.bucket.RenderBuckets;
import org.oscim.renderer.bucket.TextureBucket;
import org.oscim.renderer.bucket.TextureItem; import org.oscim.renderer.bucket.TextureItem;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,7 +44,11 @@ public class MapRenderer {
private static int mQuadIndicesID; private static int mQuadIndicesID;
private static int mQuadVerticesID; private static int mQuadVerticesID;
public final static int maxQuads = 512;
/** Number of Quads that can be rendered with bindQuadIndicesVBO() */
public final static int MAX_QUADS = 512;
/** Number of Indices that can be rendered with bindQuadIndicesVBO() */
public final static int MAX_INDICES = MAX_QUADS * 6;
public static long frametime; public static long frametime;
private static boolean rerender; private static boolean rerender;
@ -168,9 +171,9 @@ public class MapRenderer {
int[] vboIds = GLUtils.glGenBuffers(2); int[] vboIds = GLUtils.glGenBuffers(2);
mQuadIndicesID = vboIds[0]; mQuadIndicesID = vboIds[0];
int maxIndices = maxQuads * TextureBucket.INDICES_PER_SPRITE;
short[] indices = new short[maxIndices]; short[] indices = new short[MAX_INDICES];
for (int i = 0, j = 0; i < maxIndices; i += 6, j += 4) { for (int i = 0, j = 0; i < MAX_INDICES; i += 6, j += 4) {
indices[i + 0] = (short) (j + 0); indices[i + 0] = (short) (j + 0);
indices[i + 1] = (short) (j + 1); indices[i + 1] = (short) (j + 1);
indices[i + 2] = (short) (j + 2); indices[i + 2] = (short) (j + 2);
@ -253,14 +256,12 @@ public class MapRenderer {
} }
/** /**
* Bind indices for rendering up to MapRenderer.maxQuads (512) in * Bind indices for rendering up to MAX_QUADS (512),
* one draw call. Vertex order is 0-1-2 2-1-3 * ie. MAX_INDICES (512*6) in one draw call.
* * Vertex order is 0-1-2 2-1-3
* @param bind - true to activate, false to unbind (dont forget!) */
* */ public static void bindQuadIndicesVBO() {
public static void bindQuadIndicesVBO(boolean bind) { GLState.bindElementBuffer(mQuadIndicesID);
GLState.bindElementBuffer(bind ? mQuadIndicesID : 0);
} }
/** /**

View File

@ -19,6 +19,8 @@ package org.oscim.renderer.bucket;
import static org.oscim.backend.GL20.GL_SHORT; import static org.oscim.backend.GL20.GL_SHORT;
import static org.oscim.backend.GL20.GL_TRIANGLES; import static org.oscim.backend.GL20.GL_TRIANGLES;
import static org.oscim.backend.GL20.GL_UNSIGNED_SHORT; import static org.oscim.backend.GL20.GL_UNSIGNED_SHORT;
import static org.oscim.renderer.MapRenderer.MAX_INDICES;
import static org.oscim.renderer.MapRenderer.bindQuadIndicesVBO;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
@ -207,17 +209,14 @@ public class BitmapBucket extends TextureBucket {
GL.glUniform1f(s.uAlpha, alpha); GL.glUniform1f(s.uAlpha, alpha);
v.mvp.setAsUniform(s.uMVP); v.mvp.setAsUniform(s.uMVP);
MapRenderer.bindQuadIndicesVBO(true); bindQuadIndicesVBO();
for (TextureItem t = tb.textures; t != null; t = t.next) { for (TextureItem t = tb.textures; t != null; t = t.next) {
t.bind(); t.bind();
int maxIndices = MapRenderer.maxQuads * INDICES_PER_SPRITE; for (int i = 0; i < t.indices; i += MAX_INDICES) {
/* to.offset * (24(shorts) *
// draw up to maxVertices in each iteration */ * 2(short-bytes) / 6(indices) == 8) */
for (int i = 0; i < t.indices; i += maxIndices) {
// to.offset * (24(shorts) * 2(short-bytes) / 6(indices) == 8)
int off = (t.offset + i) * 8 + tb.vertexOffset; int off = (t.offset + i) * 8 + tb.vertexOffset;
GL.glVertexAttribPointer(s.aPos, 2, GL.glVertexAttribPointer(s.aPos, 2,
@ -227,16 +226,14 @@ public class BitmapBucket extends TextureBucket {
GL_SHORT, false, 12, off + 8); GL_SHORT, false, 12, off + 8);
int numIndices = t.indices - i; int numIndices = t.indices - i;
if (numIndices > maxIndices) if (numIndices > MAX_INDICES)
numIndices = maxIndices; numIndices = MAX_INDICES;
GL.glDrawElements(GL_TRIANGLES, numIndices, GL.glDrawElements(GL_TRIANGLES, numIndices,
GL_UNSIGNED_SHORT, 0); GL_UNSIGNED_SHORT, 0);
} }
} }
MapRenderer.bindQuadIndicesVBO(false);
return b.next; return b.next;
} }
} }

View File

@ -16,6 +16,9 @@
*/ */
package org.oscim.renderer.bucket; package org.oscim.renderer.bucket;
import static org.oscim.renderer.MapRenderer.MAX_INDICES;
import static org.oscim.renderer.MapRenderer.bindQuadIndicesVBO;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
@ -273,7 +276,7 @@ public final class LineTexBucket extends RenderBucket {
mVertexFlipID = vboIds[0]; mVertexFlipID = vboIds[0];
/* bytes: 0, 1, 0, 1, 0, ... */ /* bytes: 0, 1, 0, 1, 0, ... */
byte[] flip = new byte[MapRenderer.maxQuads * 4]; byte[] flip = new byte[MapRenderer.MAX_QUADS * 4];
for (int i = 0; i < flip.length; i++) for (int i = 0; i < flip.length; i++)
flip[i] = (byte) (i % 2); flip[i] = (byte) (i % 2);
@ -326,9 +329,7 @@ public final class LineTexBucket extends RenderBucket {
v.mvp.setAsUniform(shader.uMVP); v.mvp.setAsUniform(shader.uMVP);
int maxIndices = MapRenderer.maxQuads * 6; bindQuadIndicesVBO();
MapRenderer.bindQuadIndicesVBO(true);
GLState.bindVertexBuffer(mVertexFlipID); GLState.bindVertexBuffer(mVertexFlipID);
GL.glVertexAttribPointer(shader.aFlip, 1, GL.glVertexAttribPointer(shader.aFlip, 1,
@ -368,10 +369,10 @@ public final class LineTexBucket extends RenderBucket {
// TODO interleave 1. and 2. pass to improve vertex cache usage? // TODO interleave 1. and 2. pass to improve vertex cache usage?
/* first pass */ /* first pass */
int allIndices = (lb.evenQuads * 6); int allIndices = (lb.evenQuads * 6);
for (int i = 0; i < allIndices; i += maxIndices) { for (int i = 0; i < allIndices; i += MAX_INDICES) {
int numIndices = allIndices - i; int numIndices = allIndices - i;
if (numIndices > maxIndices) if (numIndices > MAX_INDICES)
numIndices = maxIndices; numIndices = MAX_INDICES;
/* i / 6 * (24 shorts per block * 2 short bytes) */ /* i / 6 * (24 shorts per block * 2 short bytes) */
int add = (b.vertexOffset + i * 8) + vOffset; int add = (b.vertexOffset + i * 8) + vOffset;
@ -394,10 +395,10 @@ public final class LineTexBucket extends RenderBucket {
/* second pass */ /* second pass */
allIndices = (lb.oddQuads * 6); allIndices = (lb.oddQuads * 6);
for (int i = 0; i < allIndices; i += maxIndices) { for (int i = 0; i < allIndices; i += MAX_INDICES) {
int numIndices = allIndices - i; int numIndices = allIndices - i;
if (numIndices > maxIndices) if (numIndices > MAX_INDICES)
numIndices = maxIndices; numIndices = MAX_INDICES;
/* i / 6 * (24 shorts per block * 2 short bytes) */ /* i / 6 * (24 shorts per block * 2 short bytes) */
int add = (b.vertexOffset + i * 8) + vOffset; int add = (b.vertexOffset + i * 8) + vOffset;

View File

@ -20,6 +20,7 @@ import static org.oscim.backend.GL20.GL_SHORT;
import static org.oscim.backend.GL20.GL_TRIANGLES; import static org.oscim.backend.GL20.GL_TRIANGLES;
import static org.oscim.backend.GL20.GL_UNSIGNED_SHORT; import static org.oscim.backend.GL20.GL_UNSIGNED_SHORT;
import static org.oscim.renderer.MapRenderer.COORD_SCALE; import static org.oscim.renderer.MapRenderer.COORD_SCALE;
import static org.oscim.renderer.MapRenderer.MAX_INDICES;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
@ -123,31 +124,28 @@ public class TextureBucket extends RenderBucket {
v.proj.setAsUniform(shader.uProj); v.proj.setAsUniform(shader.uProj);
v.mvp.setAsUniform(shader.uMV); v.mvp.setAsUniform(shader.uMV);
MapRenderer.bindQuadIndicesVBO(true); MapRenderer.bindQuadIndicesVBO();
for (TextureItem t = tb.textures; t != null; t = t.next) { for (TextureItem t = tb.textures; t != null; t = t.next) {
GL.glUniform2f(shader.uTexSize, GL.glUniform2f(shader.uTexSize,
1f / (t.width * COORD_SCALE), 1f / (t.width * COORD_SCALE),
1f / (t.height * COORD_SCALE)); 1f / (t.height * COORD_SCALE));
t.bind(); t.bind();
int maxIndices = MapRenderer.maxQuads * INDICES_PER_SPRITE;
/* draw up to maxVertices in each iteration */ /* draw up to maxVertices in each iteration */
for (int i = 0; i < t.indices; i += maxIndices) { for (int i = 0; i < t.indices; i += MAX_INDICES) {
/* to.offset * (24(shorts) * 2(short-bytes) /* to.offset * (24(shorts) * 2(short-bytes)
* / 6(indices) == 8) */ * / 6(indices) == 8) */
int off = (t.offset + i) * 8 + tb.vertexOffset; int off = (t.offset + i) * 8 + tb.vertexOffset;
int numIndices = t.indices - i; int numIndices = t.indices - i;
if (numIndices > maxIndices) if (numIndices > MAX_INDICES)
numIndices = maxIndices; numIndices = MAX_INDICES;
tb.render(off, numIndices); tb.render(off, numIndices);
} }
} }
MapRenderer.bindQuadIndicesVBO(false);
return b.next; return b.next;
} }
} }