refactor: RenderElement:

- remove curItem
- rename verticesCnt -> numVertices
- add getter/setters
This commit is contained in:
Hannes Janetzek 2014-01-23 23:05:20 +01:00
parent eff5935068
commit 0f02215e63
10 changed files with 82 additions and 74 deletions

View File

@ -48,7 +48,7 @@ public class BitmapLayer extends TextureLayer {
mVertices = new short[24]; mVertices = new short[24];
// used for size calculation of Layers buffer. // used for size calculation of Layers buffer.
verticesCnt = 4; numVertices = 4;
} }
/** /**
@ -106,7 +106,7 @@ public class BitmapLayer extends TextureLayer {
buf[pos++] = texMax; buf[pos++] = texMax;
buf[pos++] = texMax; buf[pos++] = texMax;
this.offset = sbuf.position() * 2; // bytes setOffset(sbuf.position() * 2);
sbuf.put(buf); sbuf.put(buf);
} }
@ -215,7 +215,7 @@ public class BitmapLayer extends TextureLayer {
// draw up to maxVertices in each iteration // draw up to maxVertices in each iteration
for (int i = 0; i < t.vertices; i += maxVertices) { for (int i = 0; i < t.vertices; i += maxVertices) {
// to.offset * (24(shorts) * 2(short-bytes) / 6(indices) == 8) // to.offset * (24(shorts) * 2(short-bytes) / 6(indices) == 8)
int off = (t.offset + i) * 8 + tl.offset; int off = (t.offset + i) * 8 + tl.getOffset();
GL.glVertexAttribPointer(hTextureVertex, 4, GL.glVertexAttribPointer(hTextureVertex, 4,
GL20.GL_SHORT, false, 12, off); GL20.GL_SHORT, false, 12, off);

View File

@ -208,10 +208,10 @@ public class ElementLayers {
int size = 0; int size = 0;
for (RenderElement l = baseLayers; l != null; l = l.next) for (RenderElement l = baseLayers; l != null; l = l.next)
size += l.verticesCnt * VERTEX_SHORT_CNT[l.type]; size += l.numVertices * VERTEX_SHORT_CNT[l.type];
for (RenderElement l = textureLayers; l != null; l = l.next) for (RenderElement l = textureLayers; l != null; l = l.next)
size += l.verticesCnt * TEXTURE_VERTEX_SHORTS; size += l.numVertices * TEXTURE_VERTEX_SHORTS;
return size; return size;
} }
@ -283,16 +283,14 @@ public class ElementLayers {
if (last == null) if (last == null)
continue; continue;
l.offset = pos; l.setOffset(pos);
pos += l.numVertices;
pos += l.verticesCnt;
last.next = items; last.next = items;
items = l.vertexItems; items = l.vertexItems;
last = null; last = null;
l.vertexItems = null; l.vertexItems = null;
l.curItem = null;
} }
items = VertexItem.pool.releaseAll(items); items = VertexItem.pool.releaseAll(items);
@ -301,7 +299,7 @@ public class ElementLayers {
static void addPoolItems(RenderElement l, ShortBuffer sbuf) { static void addPoolItems(RenderElement l, ShortBuffer sbuf) {
// offset of layer data in vbo // offset of layer data in vbo
l.offset = sbuf.position() * SHORT_BYTES; l.setOffset(sbuf.position() * SHORT_BYTES);
for (VertexItem it = l.vertexItems; it != null; it = it.next) { for (VertexItem it = l.vertexItems; it != null; it = it.next) {
if (it.next == null) if (it.next == null)
@ -318,11 +316,9 @@ public class ElementLayers {
// clear line and polygon layers directly // clear line and polygon layers directly
for (RenderElement l = baseLayers; l != null; l = l.next) { for (RenderElement l = baseLayers; l != null; l = l.next) {
if (l.vertexItems != null) { if (l.vertexItems != null)
l.vertexItems = VertexItem.pool.releaseAll(l.vertexItems); l.vertexItems = VertexItem.pool.releaseAll(l.vertexItems);
l.curItem = null; l.numVertices = 0;
}
l.verticesCnt = 0;
} }
for (RenderElement l = textureLayers; l != null; l = l.next) for (RenderElement l = textureLayers; l != null; l = l.next)

View File

@ -30,6 +30,7 @@ import org.oscim.renderer.MapRenderer;
import org.oscim.renderer.MapRenderer.Matrices; import org.oscim.renderer.MapRenderer.Matrices;
import org.oscim.theme.styles.Line; import org.oscim.theme.styles.Line;
import org.oscim.utils.FastMath; import org.oscim.utils.FastMath;
import org.oscim.utils.pool.Inlist;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -95,12 +96,10 @@ public final class LineLayer extends RenderElement {
else if (line.cap == Cap.SQUARE) else if (line.cap == Cap.SQUARE)
squared = true; squared = true;
if (vertexItems == null) { if (vertexItems == null)
vertexItems = VertexItem.pool.get(); vertexItems = VertexItem.pool.get();
curItem = vertexItems;
}
VertexItem si = curItem; VertexItem si = Inlist.last(vertexItems);
short v[] = si.vertices; short v[] = si.vertices;
int opos = si.used; int opos = si.used;
@ -155,7 +154,7 @@ public final class LineLayer extends RenderElement {
// + 2 for drawing triangle-strip // + 2 for drawing triangle-strip
// + 4 for round caps // + 4 for round caps
// + 2 for closing polygons // + 2 for closing polygons
verticesCnt += length + (rounded ? 6 : 2) + (closed ? 2 : 0); numVertices += length + (rounded ? 6 : 2) + (closed ? 2 : 0);
int ipos = pos; int ipos = pos;
@ -276,7 +275,7 @@ public final class LineLayer extends RenderElement {
} }
if (rounded) if (rounded)
verticesCnt -= 2; numVertices -= 2;
// add first vertex twice // add first vertex twice
ddx = (int) ((ux - tx) * DIR_SCALE); ddx = (int) ((ux - tx) * DIR_SCALE);
@ -507,7 +506,7 @@ public final class LineLayer extends RenderElement {
} }
if (rounded) if (rounded)
verticesCnt -= 2; numVertices -= 2;
ddx = (int) ((ux - vx) * DIR_SCALE); ddx = (int) ((ux - vx) * DIR_SCALE);
ddy = (int) ((uy - vy) * DIR_SCALE); ddy = (int) ((uy - vy) * DIR_SCALE);
@ -549,16 +548,13 @@ public final class LineLayer extends RenderElement {
} }
si.used = opos; si.used = opos;
curItem = si; //curItem = si;
} }
@Override @Override
public void clear() { public void clear() {
if (vertexItems != null) { vertexItems = VertexItem.pool.releaseAll(vertexItems);
vertexItems = VertexItem.pool.releaseAll(vertexItems); numVertices = 0;
curItem = null;
}
verticesCnt = 0;
} }
@Override @Override
@ -634,7 +630,8 @@ public final class LineLayer extends RenderElement {
mTexID = GLUtils.loadTexture(pixel, 128, 128, GL20.GL_ALPHA, mTexID = GLUtils.loadTexture(pixel, 128, 128, GL20.GL_ALPHA,
GL20.GL_NEAREST, GL20.GL_NEAREST, GL20.GL_NEAREST, GL20.GL_NEAREST,
GL20.GL_MIRRORED_REPEAT, GL20.GL_MIRRORED_REPEAT); GL20.GL_MIRRORED_REPEAT,
GL20.GL_MIRRORED_REPEAT);
return true; return true;
} }
@ -714,7 +711,8 @@ public final class LineLayer extends RenderElement {
// width stays the same. // width stays the same.
width = ll.width / (line.fixed ? scale : variableScale); width = ll.width / (line.fixed ? scale : variableScale);
GL.glUniform1f(uLineWidth, (float) (width * COORD_SCALE_BY_DIR_SCALE)); GL.glUniform1f(uLineWidth,
(float) (width * COORD_SCALE_BY_DIR_SCALE));
// Line-edge fade // Line-edge fade
if (line.blur > 0) { if (line.blur > 0) {
@ -741,7 +739,8 @@ public final class LineLayer extends RenderElement {
GL.glUniform1f(uLineMode, capMode); GL.glUniform1f(uLineMode, capMode);
} }
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, l.offset, l.verticesCnt); GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP,
l.getOffset(), l.numVertices);
continue; continue;
} }
@ -754,7 +753,8 @@ public final class LineLayer extends RenderElement {
else else
width = ll.width / scale + o.width / variableScale; width = ll.width / scale + o.width / variableScale;
GL.glUniform1f(uLineWidth, (float) (width * COORD_SCALE_BY_DIR_SCALE)); GL.glUniform1f(uLineWidth,
(float) (width * COORD_SCALE_BY_DIR_SCALE));
// Line-edge fade // Line-edge fade
if (line.blur > 0) { if (line.blur > 0) {
@ -775,7 +775,8 @@ public final class LineLayer extends RenderElement {
GL.glUniform1f(uLineMode, capMode); GL.glUniform1f(uLineMode, capMode);
} }
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, o.offset, o.verticesCnt); GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP,
o.getOffset(), o.numVertices);
} }
} }

View File

@ -28,6 +28,7 @@ import org.oscim.renderer.GLUtils;
import org.oscim.renderer.MapRenderer; import org.oscim.renderer.MapRenderer;
import org.oscim.renderer.MapRenderer.Matrices; import org.oscim.renderer.MapRenderer.Matrices;
import org.oscim.theme.styles.Line; import org.oscim.theme.styles.Line;
import org.oscim.utils.pool.Inlist;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -108,22 +109,24 @@ public final class LineTexLayer extends RenderElement {
public void addLine(float[] points, short[] index) { public void addLine(float[] points, short[] index) {
if (vertexItems == null) { VertexItem si = Inlist.last(vertexItems);
vertexItems = VertexItem.pool.get();
curItem = vertexItems; if (si == null) {
si = VertexItem.pool.get();
vertexItems = si;
//curItem = vertexItems;
// HACK add one vertex offset when compiling // HACK add one vertex offset when compiling
// buffer otherwise one cant use the full // buffer otherwise one cant use the full
// VertexItem (see Layers.compile) // VertexItem (see Layers.compile)
// add the two 'x' at front and end // add the two 'x' at front and end
//verticesCnt = 2; //numVertices = 2;
// the additional end vertex to make sure // the additional end vertex to make sure
// not to read outside allocated memory // not to read outside allocated memory
verticesCnt = 1; numVertices = 1;
} }
VertexItem si = curItem;
short v[] = si.vertices; short v[] = si.vertices;
int opos = si.used; int opos = si.used;
@ -218,7 +221,7 @@ public final class LineTexLayer extends RenderElement {
even = false; even = false;
// vertex 0 and 2 were added // vertex 0 and 2 were added
verticesCnt += 3; numVertices += 3;
evenQuads++; evenQuads++;
} else { } else {
// go to next block // go to next block
@ -226,7 +229,7 @@ public final class LineTexLayer extends RenderElement {
opos += 18; opos += 18;
// vertex 1 and 3 were added // vertex 1 and 3 were added
verticesCnt += 1; numVertices += 1;
oddQuads++; oddQuads++;
} }
} }
@ -241,7 +244,6 @@ public final class LineTexLayer extends RenderElement {
opos += 12; opos += 12;
si.used = opos; si.used = opos;
curItem = si;
} }
@Override @Override
@ -367,6 +369,7 @@ public final class LineTexLayer extends RenderElement {
for (; l != null && l.type == RenderElement.TEXLINE; l = l.next) { for (; l != null && l.type == RenderElement.TEXLINE; l = l.next) {
LineTexLayer ll = (LineTexLayer) l; LineTexLayer ll = (LineTexLayer) l;
Line line = ll.line; Line line = ll.line;
int lOffset = l.getOffset();
GLUtils.setColor(hTexColor, line.stippleColor, 1); GLUtils.setColor(hTexColor, line.stippleColor, 1);
GLUtils.setColor(hBgColor, line.color, 1); GLUtils.setColor(hBgColor, line.color, 1);
@ -393,7 +396,7 @@ public final class LineTexLayer extends RenderElement {
numIndices = maxIndices; numIndices = maxIndices;
// i / 6 * (24 shorts per block * 2 short bytes) // i / 6 * (24 shorts per block * 2 short bytes)
int add = (l.offset + i * 8) + vOffset; int add = (lOffset + i * 8) + vOffset;
GL.glVertexAttribPointer(hVertexPosition0, GL.glVertexAttribPointer(hVertexPosition0,
4, GL20.GL_SHORT, false, STRIDE, 4, GL20.GL_SHORT, false, STRIDE,
@ -422,7 +425,7 @@ public final class LineTexLayer extends RenderElement {
if (numIndices > maxIndices) if (numIndices > maxIndices)
numIndices = maxIndices; numIndices = maxIndices;
// i / 6 * (24 shorts per block * 2 short bytes) // i / 6 * (24 shorts per block * 2 short bytes)
int add = (l.offset + i * 8) + vOffset; int add = (lOffset + i * 8) + vOffset;
GL.glVertexAttribPointer(hVertexPosition0, GL.glVertexAttribPointer(hVertexPosition0,
4, GL20.GL_SHORT, false, STRIDE, 4, GL20.GL_SHORT, false, STRIDE,

View File

@ -60,9 +60,9 @@ public class MeshLayer extends RenderElement {
numIndices += Tessellator.tessellate(geom, MapRenderer.COORD_SCALE, numIndices += Tessellator.tessellate(geom, MapRenderer.COORD_SCALE,
Inlist.last(vertexItems), Inlist.last(vertexItems),
Inlist.last(indiceItems), Inlist.last(indiceItems),
verticesCnt); numVertices);
verticesCnt = vertexItems.getSize() / 2; numVertices = vertexItems.getSize() / 2;
if (numIndices <= 0) { if (numIndices <= 0) {
log.debug("empty " + geom.index); log.debug("empty " + geom.index);
@ -144,7 +144,7 @@ public class MeshLayer extends RenderElement {
GLUtils.setColor(hColor, ml.area.color, 1); GLUtils.setColor(hColor, ml.area.color, 1);
GL.glVertexAttribPointer(hVertexPosition, 2, GL20.GL_SHORT, GL.glVertexAttribPointer(hVertexPosition, 2, GL20.GL_SHORT,
false, 0, ml.offset); false, 0, ml.getOffset());
GL.glDrawElements(GL20.GL_TRIANGLES, ml.numIndices, GL.glDrawElements(GL20.GL_TRIANGLES, ml.numIndices,
GL20.GL_UNSIGNED_SHORT, 0); GL20.GL_UNSIGNED_SHORT, 0);

View File

@ -34,6 +34,7 @@ import org.oscim.renderer.MapRenderer.Matrices;
import org.oscim.theme.styles.Area; import org.oscim.theme.styles.Area;
import org.oscim.utils.FastMath; import org.oscim.utils.FastMath;
import org.oscim.utils.Interpolation; import org.oscim.utils.Interpolation;
import org.oscim.utils.pool.Inlist;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -53,8 +54,7 @@ public final class PolygonLayer extends RenderElement {
super(RenderElement.POLYGON); super(RenderElement.POLYGON);
level = layer; level = layer;
curItem = VertexItem.pool.get(); vertexItems = VertexItem.pool.get();
vertexItems = curItem;
} }
public void addPolygon(GeometryBuffer geom) { public void addPolygon(GeometryBuffer geom) {
@ -64,7 +64,7 @@ public final class PolygonLayer extends RenderElement {
public void addPolygon(float[] points, short[] index) { public void addPolygon(float[] points, short[] index) {
short center = (short) ((Tile.SIZE >> 1) * S); short center = (short) ((Tile.SIZE >> 1) * S);
VertexItem si = curItem; VertexItem si = Inlist.last(vertexItems);
short[] v = si.vertices; short[] v = si.vertices;
int outPos = si.used; int outPos = si.used;
@ -79,7 +79,7 @@ public final class PolygonLayer extends RenderElement {
continue; continue;
} }
verticesCnt += length / 2 + 2; numVertices += length / 2 + 2;
int inPos = pos; int inPos = pos;
@ -115,7 +115,6 @@ public final class PolygonLayer extends RenderElement {
} }
si.used = outPos; si.used = outPos;
curItem = si;
} }
@Override @Override
@ -326,7 +325,7 @@ public final class PolygonLayer extends RenderElement {
// set stencil mask to draw to // set stencil mask to draw to
GL.glStencilMask(1 << cur++); GL.glStencilMask(1 << cur++);
GL.glDrawArrays(GL20.GL_TRIANGLE_FAN, l.offset, l.verticesCnt); GL.glDrawArrays(GL20.GL_TRIANGLE_FAN, l.getOffset(), l.numVertices);
// draw up to 7 layers into stencil buffer // draw up to 7 layers into stencil buffer
if (cur == STENCIL_BITS - 1) { if (cur == STENCIL_BITS - 1) {

View File

@ -26,7 +26,7 @@ public abstract class RenderElement extends Inlist<RenderElement> {
public final static byte LINE = 0; public final static byte LINE = 0;
public final static byte POLYGON = 1; public final static byte POLYGON = 1;
public static final byte MESH = 2; public final static byte MESH = 2;
public final static byte TEXLINE = 3; public final static byte TEXLINE = 3;
public final static byte SYMBOL = 4; public final static byte SYMBOL = 4;
public final static byte BITMAP = 5; public final static byte BITMAP = 5;
@ -38,22 +38,31 @@ public abstract class RenderElement extends Inlist<RenderElement> {
public final byte type; public final byte type;
// drawing order from bottom to top /** drawing order from bottom to top */
int level; int level;
// number of vertices for this layer /** number of vertices for this layer */
public int verticesCnt; protected int numVertices;
// in case of line and polygon layer: protected VertexItem vertexItems;
// - number of VERTICES offset for this layertype in VBO
// otherwise:
// - offset in byte in VBO
public int offset;
VertexItem vertexItems;
protected VertexItem curItem;
abstract protected void compile(ShortBuffer sbuf); abstract protected void compile(ShortBuffer sbuf);
abstract protected void clear(); abstract protected void clear();
/**
* for line and polygon layers:
* - number of VERTICES mOffset for this layertype in VBO
* otherwise:
* - offset in byte in VBO
*/
private int mOffset;
public int getOffset() {
return mOffset;
}
public void setOffset(int offset) {
mOffset = offset;
}
} }

View File

@ -44,7 +44,7 @@ public final class SymbolLayer extends TextureLayer {
public void addSymbol(SymbolItem item) { public void addSymbol(SymbolItem item) {
// needed to calculate 'sbuf' size for compile // needed to calculate 'sbuf' size for compile
verticesCnt += VERTICES_PER_SPRITE; numVertices += VERTICES_PER_SPRITE;
for (SymbolItem it = symbols; it != null; it = it.next) { for (SymbolItem it = symbols; it != null; it = it.next) {
if (it.bitmap == item.bitmap) { if (it.bitmap == item.bitmap) {
@ -62,7 +62,7 @@ public final class SymbolLayer extends TextureLayer {
@Override @Override
protected void compile(ShortBuffer sbuf) { protected void compile(ShortBuffer sbuf) {
// offset of layer data in vbo // offset of layer data in vbo
this.offset = sbuf.position() * 2; //SHORT_BYTES; setOffset(sbuf.position() * 2); //SHORT_BYTES;
short numIndices = 0; short numIndices = 0;
@ -202,7 +202,7 @@ public final class SymbolLayer extends TextureLayer {
public void clearItems() { public void clearItems() {
symbols = SymbolItem.pool.releaseAll(symbols); symbols = SymbolItem.pool.releaseAll(symbols);
//verticesCnt = 0; //numVertices = 0;
} }
@Override @Override
@ -213,7 +213,7 @@ public final class SymbolLayer extends TextureLayer {
//symbols = SymbolItem.pool.releaseAll(symbols); //symbols = SymbolItem.pool.releaseAll(symbols);
//vertexItems = VertexItem.pool.releaseAll(vertexItems); //vertexItems = VertexItem.pool.releaseAll(vertexItems);
//verticesCnt = 0; //numVertices = 0;
} }
@Override @Override

View File

@ -84,7 +84,7 @@ public final class TextLayer extends TextureLayer {
int pos = vi.used; // 0 int pos = vi.used; // 0
short buf[] = vi.vertices; short buf[] = vi.vertices;
verticesCnt = 0; numVertices = 0;
int advanceY = 0; int advanceY = 0;
float x = 0; float x = 0;
@ -246,8 +246,8 @@ public final class TextLayer extends TextureLayer {
buf[pos++] = v1; buf[pos++] = v1;
// six indices to draw the four vertices // six indices to draw the four vertices
verticesCnt += 4;
numIndices += TextureLayer.INDICES_PER_SPRITE; numIndices += TextureLayer.INDICES_PER_SPRITE;
numVertices += 4;
if (it.next == null || (it.next.text != it.text) if (it.next == null || (it.next.text != it.text)
|| (it.next.string != it.string)) { || (it.next.string != it.string)) {
@ -276,7 +276,7 @@ public final class TextLayer extends TextureLayer {
clearLabels(); clearLabels();
//labels = TextItem.pool.releaseAll(labels); //labels = TextItem.pool.releaseAll(labels);
//vertexItems = VertexItem.pool.releaseAll(vertexItems); //vertexItems = VertexItem.pool.releaseAll(vertexItems);
//verticesCnt = 0; //numVertices = 0;
} }
public void clearLabels() { public void clearLabels() {

View File

@ -69,7 +69,7 @@ public abstract class TextureLayer extends RenderElement {
textures = textures.dispose(); textures = textures.dispose();
vertexItems = VertexItem.pool.releaseAll(vertexItems); vertexItems = VertexItem.pool.releaseAll(vertexItems);
verticesCnt = 0; numVertices = 0;
} }
static void putSprite(short buf[], int pos, static void putSprite(short buf[], int pos,
@ -174,7 +174,7 @@ public abstract class TextureLayer extends RenderElement {
// draw up to maxVertices in each iteration // draw up to maxVertices in each iteration
for (int i = 0; i < t.vertices; i += maxVertices) { for (int i = 0; i < t.vertices; i += maxVertices) {
// to.offset * (24(shorts) * 2(short-bytes) / 6(indices) == 8) // to.offset * (24(shorts) * 2(short-bytes) / 6(indices) == 8)
int off = (t.offset + i) * 8 + tl.offset; int off = (t.offset + i) * 8 + tl.getOffset();
GL.glVertexAttribPointer(hTextureVertex, 4, GL.glVertexAttribPointer(hTextureVertex, 4,
GL20.GL_SHORT, false, 12, off); GL20.GL_SHORT, false, 12, off);