improve LineLayer rendering

This commit is contained in:
Hannes Janetzek 2013-09-26 22:09:09 +02:00
parent c05710fcdb
commit ed7ff6e7a1

View File

@ -121,7 +121,7 @@ public final class LineLayer extends RenderElement {
// Note: just a hack to save some vertices, when there are more than 200 lines // Note: just a hack to save some vertices, when there are more than 200 lines
// per type // per type
if (rounded) { if (rounded && index != null) {
int cnt = 0; int cnt = 0;
for (int i = 0, n = index.length; i < n; i++, cnt++) { for (int i = 0, n = index.length; i < n; i++, cnt++) {
if (index[i] < 0) if (index[i] < 0)
@ -579,19 +579,25 @@ public final class LineLayer extends RenderElement {
} }
public static final class Renderer { public static final class Renderer {
// TODO: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter22.html
private static final int LINE_VERTICES_DATA_POS_OFFSET = 0;
// factor to normalize extrusion vector and scale to coord scale // factor to normalize extrusion vector and scale to coord scale
private final static float COORD_SCALE_BY_DIR_SCALE = private final static float COORD_SCALE_BY_DIR_SCALE =
MapRenderer.COORD_SCALE / LineLayer.DIR_SCALE; MapRenderer.COORD_SCALE / LineLayer.DIR_SCALE;
private final static int CAP_THIN = 0;
private final static int CAP_BUTT = 1;
private final static int CAP_ROUND = 2;
private final static int SHADER_FLAT = 1;
private final static int SHADER_PROJ = 0;
// shader handles // shader handles
private static int[] lineProgram = new int[2]; private static int[] lineProgram = new int[2];
private static int[] hLineVertexPosition = new int[2]; private static int[] hLineVertexPosition = new int[2];
private static int[] hLineColor = new int[2]; private static int[] hLineColor = new int[2];
private static int[] hLineMatrix = new int[2]; private static int[] hLineMatrix = new int[2];
private static int[] hLineScale = new int[2]; private static int[] hLineFade = new int[2];
private static int[] hLineWidth = new int[2]; private static int[] hLineWidth = new int[2];
private static int[] hLineMode = new int[2]; private static int[] hLineMode = new int[2];
public static int mTexID; public static int mTexID;
@ -617,7 +623,7 @@ public final class LineLayer extends RenderElement {
continue; continue;
hLineMatrix[i] = GL.glGetUniformLocation(lineProgram[i], "u_mvp"); hLineMatrix[i] = GL.glGetUniformLocation(lineProgram[i], "u_mvp");
hLineScale[i] = GL.glGetUniformLocation(lineProgram[i], "u_wscale"); hLineFade[i] = GL.glGetUniformLocation(lineProgram[i], "u_fade");
hLineWidth[i] = GL.glGetUniformLocation(lineProgram[i], "u_width"); hLineWidth[i] = GL.glGetUniformLocation(lineProgram[i], "u_width");
hLineColor[i] = GL.glGetUniformLocation(lineProgram[i], "u_color"); hLineColor[i] = GL.glGetUniformLocation(lineProgram[i], "u_color");
hLineMode[i] = GL.glGetUniformLocation(lineProgram[i], "u_mode"); hLineMode[i] = GL.glGetUniformLocation(lineProgram[i], "u_mode");
@ -642,22 +648,15 @@ 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);
Log.d(TAG, "TEX ID: " + mTexID);
return true; return true;
} }
public static RenderElement draw(ElementLayers layers, RenderElement curLayer, public static RenderElement draw(ElementLayers layers, RenderElement curLayer,
MapPosition pos, MapPosition pos, Matrices m, float div, int mode) {
Matrices m, float div, int mode) {
if (curLayer == null) if (curLayer == null)
return null; return null;
// FIXME HACK: fallback to simple shader
if (lineProgram[mode] == 0)
mode = 1;
GLState.useProgram(lineProgram[mode]); GLState.useProgram(lineProgram[mode]);
GLState.blend(true); GLState.blend(true);
@ -669,22 +668,18 @@ public final class LineLayer extends RenderElement {
if (!GLAdapter.GDX_DESKTOP_QUIRKS) if (!GLAdapter.GDX_DESKTOP_QUIRKS)
GLState.bindTex2D(mTexID); GLState.bindTex2D(mTexID);
int uLineScale = hLineScale[mode]; int uLineFade = hLineFade[mode];
int uLineMode = hLineMode[mode]; int uLineMode = hLineMode[mode];
int uLineColor = hLineColor[mode]; int uLineColor = hLineColor[mode];
int uLineWidth = hLineWidth[mode]; int uLineWidth = hLineWidth[mode];
GLState.enableVertexArrays(hLineVertexPosition[mode], -1); GLState.enableVertexArrays(hLineVertexPosition[mode], -1);
GL.glVertexAttribPointer(hLineVertexPosition[mode], 4, GL20.GL_SHORT, GL.glVertexAttribPointer(hLineVertexPosition[mode], 4, GL20.GL_SHORT, false, 0,
false, 0, layers.lineOffset + LINE_VERTICES_DATA_POS_OFFSET); layers.lineOffset);
//glUniformMatrix4fv(hLineMatrix[mode], 1, false, matrix, 0);
m.mvp.setAsUniform(hLineMatrix[mode]); m.mvp.setAsUniform(hLineMatrix[mode]);
//int zoom = FastMath.log2((int) pos.absScale);
int zoom = pos.zoomLevel;
double scale = pos.getZoomScale(); double scale = pos.getZoomScale();
// Line scale factor for non fixed lines: Within a zoom- // Line scale factor for non fixed lines: Within a zoom-
@ -692,113 +687,115 @@ public final class LineLayer extends RenderElement {
// Though lines should only scale by sqrt(2). This is achieved // Though lines should only scale by sqrt(2). This is achieved
// by inverting scaling of extrusion vector with: width/sqrt(s). // by inverting scaling of extrusion vector with: width/sqrt(s).
// within one zoom-level: 1 <= s <= 2 // within one zoom-level: 1 <= s <= 2
double s = scale / div; double relativeScale = scale / div;
float lineScale = (float) Math.sqrt(s * 2 / 2.2); double variableScale = (float) Math.sqrt(relativeScale * 2 / 2.2);
// scale factor to map one pixel on tile to one pixel on screen: // scale factor to map one pixel on tile to one pixel on screen:
// only works with orthographic projection // used with orthographic projection, (shader mode == 1)
float pixel = 0; double pixel = (mode == SHADER_PROJ) ? 0 : (1.5 / relativeScale);
if (mode == 1) GL.glUniform1f(uLineFade, (float) pixel);
pixel = (float) (1.5 / s);
GL.glUniform1f(uLineScale, pixel); int capMode = 0;
int lineMode = 0; GL.glUniform1f(uLineMode, capMode);
GL.glUniform1f(uLineMode, lineMode);
boolean blur = false; boolean blur = false;
double width;
RenderElement l = curLayer; RenderElement l = curLayer;
for (; l != null && l.type == RenderElement.LINE; l = l.next) { for (; l != null && l.type == RenderElement.LINE; l = l.next) {
LineLayer ll = (LineLayer) l; LineLayer ll = (LineLayer) l;
Line line = ll.line; Line line = ll.line;
float width;
if (line.fade < zoom) { if (line.fade < pos.zoomLevel) {
GLUtils.setColor(uLineColor, line.color, 1); GLUtils.setColor(uLineColor, line.color, 1);
} else if (line.fade > zoom) { } else if (line.fade > pos.zoomLevel) {
continue; continue;
} else { } else {
float alpha = (float) (scale > 1.2 ? scale : 1.2) - 1; float alpha = (float) (scale > 1.2 ? scale : 1.2) - 1;
GLUtils.setColor(uLineColor, line.color, alpha); GLUtils.setColor(uLineColor, line.color, alpha);
} }
if (mode == 0 && blur && line.blur == 0) { if (mode == SHADER_PROJ && blur && line.blur == 0) {
GL.glUniform1f(uLineScale, 0); GL.glUniform1f(uLineFade, 0);
blur = false; blur = false;
} }
if (line.outline) { // draw LineLayer
// draw linelayers references by this outline if (!line.outline) {
for (LineLayer o = ll.outlines; o != null; o = o.outlines) {
if (o.line.fixed /* || strokeMaxZoom */) { if (line.fixed) {
width = (float) ((ll.width + o.width) / s);
} else {
width = (float) (ll.width / s + o.width / lineScale);
// check min-size for outline
if (o.line.min > 0 && o.width * lineScale < o.line.min * 2)
continue;
}
GL.glUniform1f(uLineWidth, width * COORD_SCALE_BY_DIR_SCALE);
if (line.blur != 0) {
GL.glUniform1f(uLineScale, (float) (1 - (line.blur / s)));
blur = true;
} else if (mode == 1) {
GL.glUniform1f(uLineScale, pixel / width);
}
if (o.roundCap) {
if (lineMode != 1) {
lineMode = 1;
GL.glUniform1f(uLineMode, lineMode);
}
} else if (lineMode != 0) {
lineMode = 0;
GL.glUniform1f(uLineMode, lineMode);
}
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, o.offset, o.verticesCnt);
}
} else {
if (line.fixed /* || strokeMaxZoom */) {
// invert scaling of extrusion vectors so that line // invert scaling of extrusion vectors so that line
// width stays the same. // width stays the same. 'max'?
width = (float) (ll.width / s); width = Math.max(ll.width, 1) / relativeScale;
} else { } else {
// reduce linear scaling of extrusion vectors so that width = ll.width / variableScale;
// line width increases by sqrt(2.2).
width = ll.width / lineScale;
// min-size hack to omit outline when line becomes
// very thin
if ((ll.line.min > 0) && (ll.width * lineScale < ll.line.min * 2))
width = (ll.width - 0.2f) / lineScale;
} }
GL.glUniform1f(uLineWidth, width * COORD_SCALE_BY_DIR_SCALE); GL.glUniform1f(uLineWidth, (float) (width * COORD_SCALE_BY_DIR_SCALE));
if (line.blur != 0) { // Line-edge fade
GL.glUniform1f(uLineScale, line.blur); if (line.blur > 0) {
GL.glUniform1f(uLineFade, line.blur);
blur = true; blur = true;
} else if (mode == 1) { } else if (mode == SHADER_FLAT) {
GL.glUniform1f(uLineScale, pixel / width); GL.glUniform1f(uLineFade, (float) (pixel / width));
//GL.glUniform1f(uLineScale, (float)(pixel / (ll.width / s)));
} }
if (ll.roundCap) { // Cap mode
if (lineMode != 1) { if (ll.width < 1.5 /*|| ll.line.fixed*/) {
lineMode = 1; if (capMode != CAP_THIN) {
GL.glUniform1f(uLineMode, lineMode); capMode = CAP_THIN;
GL.glUniform1f(uLineMode, capMode);
} }
} else if (lineMode != 0) { } else if (ll.roundCap) {
lineMode = 0; if (capMode != CAP_ROUND) {
GL.glUniform1f(uLineMode, lineMode); capMode = CAP_ROUND;
GL.glUniform1f(uLineMode, capMode);
}
} else if (capMode != CAP_BUTT) {
capMode = CAP_BUTT;
GL.glUniform1f(uLineMode, capMode);
} }
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, l.offset, l.verticesCnt); GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, l.offset, l.verticesCnt);
continue;
}
// draw LineLayers references by this outline
for (LineLayer o = ll.outlines; o != null; o = o.outlines) {
if (o.line.fixed)
width = (ll.width + o.width) / relativeScale;
else
width = ll.width / relativeScale + o.width / variableScale;
GL.glUniform1f(uLineWidth, (float) (width * COORD_SCALE_BY_DIR_SCALE));
// Line-edge fade
if (line.blur > 0) {
//GL.glUniform1f(uLineFade, (float) FastMath.clamp((1 - (line.blur / relativeScale)), 0, 1));
//GL.glUniform1f(uLineFade, (float) (1 - (line.blur / relativeScale)));
GL.glUniform1f(uLineFade, line.blur);
blur = true;
} else if (mode == SHADER_FLAT) {
GL.glUniform1f(uLineFade, (float) (pixel / width));
}
// Cap mode
if (o.roundCap) {
if (capMode != CAP_ROUND) {
capMode = CAP_ROUND;
GL.glUniform1f(uLineMode, capMode);
}
} else if (capMode != CAP_BUTT) {
capMode = CAP_BUTT;
GL.glUniform1f(uLineMode, capMode);
}
GL.glDrawArrays(GL20.GL_TRIANGLE_STRIP, o.offset, o.verticesCnt);
} }
} }
@ -815,44 +812,53 @@ public final class LineLayer extends RenderElement {
+ "uniform float u_mode;" + "uniform float u_mode;"
+ "varying vec2 v_st;" + "varying vec2 v_st;"
+ "void main() {" + "void main() {"
// scale extrusion to u_width pixel // scale extrusion to u_width pixel
// just ignore the two most insignificant bits of a_st :) // just ignore the two most insignificant bits.
+ " vec2 dir = a_pos.zw;" + " vec2 dir = a_pos.zw;"
+ " gl_Position = u_mvp * vec4(a_pos.xy + (u_width * dir), 0.0, 1.0);" + " gl_Position = u_mvp * vec4(a_pos.xy + (u_width * dir), 0.0, 1.0);"
// last two bits of a_st hold the texture coordinates
// ..maybe one could wrap texture so that `abs` is not required // last two bits hold the texture coordinates.
+ " v_st = abs(mod(dir, 4.0)) - 1.0;" + " v_st = abs(mod(dir, 4.0)) - 1.0;"
+ "}"; + "}";
/** Antialising for orthonogonal projection */
private final static String lineSimpleFragmentShader = "" private final static String lineSimpleFragmentShader = ""
+ "precision mediump float;" + "precision mediump float;"
+ "uniform sampler2D tex;" + "uniform sampler2D tex;"
+ "uniform float u_wscale;" + "uniform float u_fade;"
+ "uniform float u_mode;" + "uniform float u_mode;"
+ "uniform vec4 u_color;" + "uniform vec4 u_color;"
+ "varying vec2 v_st;" + "varying vec2 v_st;"
+ "void main() {" + "void main() {"
//+ " float len;" + "float len;"
// (currently required as overlay line renderers dont load the texture) + " if (u_mode == 2.0){"
//+ " if (u_mode == 0)" // round cap line
//+ " len = abs(v_st.s);"
//+ " else"
//+ " len = texture2D(tex, v_st).a;"
//+ " len = u_mode * length(v_st);"
// this avoids branching, need to check performance
+ (GLAdapter.GDX_DESKTOP_QUIRKS + (GLAdapter.GDX_DESKTOP_QUIRKS
? " float len = max((1.0 - u_mode) * abs(v_st.s), u_mode * length(v_st));" ? " len = length(v_st);"
: " float len = max((1.0 - u_mode) * abs(v_st.s), u_mode * texture2D(tex, v_st).a);") : " len = texture2D(tex, v_st).a;")
// interpolate alpha between: 0.0 < 1.0 - len < u_wscale + " } else {"
// where wscale is 'filter width' / 'line width' and 0 <= len <= sqrt(2) // flat cap line
//+ " gl_FragColor = u_color * smoothstep(0.0, u_wscale, 1.0 - len);" + " len = abs(v_st.s);"
//+ " gl_FragColor = mix(vec4(1.0,0.0,0.0,1.0), u_color, smoothstep(0.0, u_wscale, 1.0 - len));" + " }"
+ " float alpha = min(1.0, (1.0 - len) / u_wscale);" // u_mode == 0.0 -> thin line
+ " if (alpha > 0.1)" + " len = len * clamp(u_mode, len, 1.0);"
+ " gl_FragColor = u_color * alpha;"
+ " else" // use 'max' to avoid branching, need to check performance
+ " discard;" //+ (GLAdapter.GDX_DESKTOP_QUIRKS
// + "gl_FragColor = vec4(texture2D(tex, v_st).a);" // ? " float len = max((1.0 - u_mode) * abs(v_st.s), u_mode * length(v_st));"
// : " float len = max((1.0 - u_mode) * abs(v_st.s), u_mode * texture2D(tex, v_st).a);")
// Antialias line-edges:
// - 'len' is 0 at center of line. -> (1.0 - len) is 0 at the edges
// - 'u_fade' is 'pixel' / 'width', i.e. the inverse width of the
// line in pixel on screen.
// - 'pixel' is 1.5 / relativeScale
// - '(1.0 - len) / u_fade' interpolates the 'pixel' on line-edge
// between 0 and 1 (it is greater 1 for all inner pixel).
+ " gl_FragColor = u_color * clamp((1.0 - len) / u_fade, 0.0, 1.0);"
// -> nicer for thin lines
//+ " gl_FragColor = u_color * clamp((1.0 - (len * len)) / u_fade, 0.0, 1.0);"
+ "}"; + "}";
private final static String lineFragmentShader = "" private final static String lineFragmentShader = ""
@ -861,92 +867,32 @@ public final class LineLayer extends RenderElement {
+ "uniform sampler2D tex;" + "uniform sampler2D tex;"
+ "uniform float u_mode;" + "uniform float u_mode;"
+ "uniform vec4 u_color;" + "uniform vec4 u_color;"
+ "uniform float u_wscale;" + "uniform float u_fade;"
+ "varying vec2 v_st;" + "varying vec2 v_st;"
+ "void main() {" + "void main() {"
+ " float len;" + " float len;"
+ " float fuzz;" + " float fuzz;"
+ " if (u_mode == 0.0){" + " if (u_mode == 2.0){"
+ " len = abs(v_st.s);" // round cap line
+ " fuzz = fwidth(v_st.s);"
+ " } else {"
+ (GLAdapter.GDX_DESKTOP_QUIRKS + (GLAdapter.GDX_DESKTOP_QUIRKS
? " len = length(v_st);" ? " len = length(v_st);"
: " len = texture2D(tex, v_st).a;") : " len = texture2D(tex, v_st).a;")
+ " vec2 st_width = fwidth(v_st);" + " vec2 st_width = fwidth(v_st);"
+ " fuzz = max(st_width.s, st_width.t);" + " fuzz = max(st_width.s, st_width.t);"
+ " } else {"
// flat cap line
+ " len = abs(v_st.s);"
+ " fuzz = fwidth(v_st.s);"
+ " }" + " }"
//+ " gl_FragColor = u_color * smoothstep(0.0, fuzz + u_wscale, 1.0 - len);" // u_mode == 0.0 -> thin line
// smoothstep is too sharp, guess one could increase extrusion with z.. + " len = len * clamp(u_mode, len, 1.0);"
// this looks ok: + " if (fuzz > 2.0)"
//+ " gl_FragColor = u_color * min(1.0, (1.0 - len) / (u_wscale + fuzz));" + " gl_FragColor = u_color * 0.5;" //vec4(1.0, 1.0, 1.0, 1.0);"
// can be faster according to nvidia docs 'Optimize OpenGL ES 2.0 Performace' + " else"
+ " gl_FragColor = u_color * clamp((1.0 - len) / (u_wscale + fuzz), 0.0, 1.0);" + " gl_FragColor = u_color * clamp((1.0 - len) / max(u_fade, fuzz), 0.0, 1.0);"
//+ " gl_FragColor = mix(vec4(0.0,1.0,0.0,1.0), u_color, clamp((1.0 - len) / (u_wscale + fuzz), 0.0, 1.0));" //+ " gl_FragColor = u_color * clamp((1.0 - len), 0.0, 1.0);"
+ "}"; + "}";
// private final static String lineVertexShader = ""
// + "precision mediump float;"
// + "uniform mat4 u_mvp;"
// + "uniform float u_width;"
// + "attribute vec4 a_pos;"
// + "uniform int u_mode;"
// //+ "attribute vec2 a_st;"
// + "varying vec2 v_st;"
// + "const float dscale = 8.0/2048.0;"
// + "void main() {"
// // scale extrusion to u_width pixel
// // just ignore the two most insignificant bits of a_st :)
// + " vec2 dir = a_pos.zw;"
// + " gl_Position = u_mvp * vec4(a_pos.xy + (dscale * u_width * dir), 0.0, 1.0);"
// // last two bits of a_st hold the texture coordinates
// + " v_st = u_width * (abs(mod(dir, 4.0)) - 1.0);"
// // use bit operations when available (gles 1.3)
// // + " v_st = u_width * vec2(a_st.x & 3 - 1, a_st.y & 3 - 1);"
// + "}";
//
// private final static String lineSimpleFragmentShader = ""
// + "precision mediump float;"
// + "uniform float u_wscale;"
// + "uniform float u_width;"
// + "uniform int u_mode;"
// + "uniform vec4 u_color;"
// + "varying vec2 v_st;"
// + "void main() {"
// + " float len;"
// + " if (u_mode == 0)"
// + " len = abs(v_st.s);"
// + " else "
// + " len = length(v_st);"
// // fade to alpha. u_wscale is the width in pixel which should be
// // faded, u_width - len the position of this fragment on the
// // perpendicular to this line segment. this only works with no
// // perspective
// //+ " gl_FragColor = min(1.0, (u_width - len) / u_wscale) * u_color;"
// + " gl_FragColor = u_color * smoothstep(0.0, u_wscale, (u_width - len));"
// + "}";
//
// private final static String lineFragmentShader = ""
// + "#extension GL_OES_standard_derivatives : enable\n"
// + "precision mediump float;"
// + "uniform float u_wscale;"
// + "uniform float u_width;"
// + "uniform int u_mode;"
// + "uniform vec4 u_color;"
// + "varying vec2 v_st;"
// + "void main() {"
// + " float len;"
// + " float fuzz;"
// + " if (u_mode == 0){"
// + " len = abs(v_st.s);"
// + " fuzz = u_wscale + fwidth(v_st.s);"
// + " } else {"
// + " len = length(v_st);"
// + " vec2 st_width = fwidth(v_st);"
// + " fuzz = u_wscale + max(st_width.s, st_width.t);"
// + " }"
// + " gl_FragColor = u_color * min(1.0, (u_width - len) / fuzz);"
// + "}";
} }
} }