summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mbgl/map/map.hpp2
-rw-r--r--src/mbgl/geometry/line_atlas.cpp171
-rw-r--r--src/mbgl/geometry/line_atlas.hpp41
-rw-r--r--src/mbgl/map/map.cpp4
-rw-r--r--src/mbgl/renderer/line_bucket.cpp14
-rw-r--r--src/mbgl/renderer/line_bucket.hpp4
-rw-r--r--src/mbgl/renderer/painter.cpp4
-rw-r--r--src/mbgl/renderer/painter.hpp6
-rw-r--r--src/mbgl/renderer/painter_line.cpp32
-rw-r--r--src/mbgl/shader/line.fragment.glsl8
-rw-r--r--src/mbgl/shader/line.vertex.glsl3
-rw-r--r--src/mbgl/shader/line_shader.hpp1
-rw-r--r--src/mbgl/shader/linesdf.fragment.glsl23
-rw-r--r--src/mbgl/shader/linesdf.vertex.glsl49
-rw-r--r--src/mbgl/shader/linesdf_shader.cpp30
-rw-r--r--src/mbgl/shader/linesdf_shader.hpp34
-rw-r--r--src/mbgl/style/function_properties.cpp2
-rw-r--r--src/mbgl/style/property_fallback.cpp2
-rw-r--r--src/mbgl/style/property_key.hpp2
-rw-r--r--src/mbgl/style/property_value.hpp5
-rw-r--r--src/mbgl/style/style_layer.cpp14
-rw-r--r--src/mbgl/style/style_parser.cpp71
-rw-r--r--src/mbgl/style/style_parser.hpp6
-rw-r--r--src/mbgl/style/style_properties.hpp4
-rw-r--r--src/mbgl/util/interpolate.hpp5
25 files changed, 468 insertions, 69 deletions
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp
index 62c4abc232..a07941b2f2 100644
--- a/include/mbgl/map/map.hpp
+++ b/include/mbgl/map/map.hpp
@@ -30,6 +30,7 @@ class FileSource;
class View;
class GlyphAtlas;
class SpriteAtlas;
+class LineAtlas;
class Map : private util::noncopyable {
public:
@@ -185,6 +186,7 @@ private:
util::ptr<GlyphStore> glyphStore;
const std::unique_ptr<SpriteAtlas> spriteAtlas;
util::ptr<Sprite> sprite;
+ const std::unique_ptr<LineAtlas> lineAtlas;
util::ptr<TexturePool> texturePool;
const std::unique_ptr<Painter> painter;
diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp
new file mode 100644
index 0000000000..b396d93259
--- /dev/null
+++ b/src/mbgl/geometry/line_atlas.cpp
@@ -0,0 +1,171 @@
+#include <mbgl/geometry/line_atlas.hpp>
+#include <mbgl/platform/gl.hpp>
+#include <mbgl/platform/platform.hpp>
+
+#include <sstream>
+#include <cmath>
+
+using namespace mbgl;
+
+LineAtlas::LineAtlas(uint16_t w, uint16_t h)
+ : width(w),
+ height(h),
+ data(new char[w * h]),
+ dirty(true) {
+}
+
+LineAtlas::~LineAtlas() {
+ std::lock_guard<std::recursive_mutex> lock(mtx);
+
+ MBGL_CHECK_ERROR(glDeleteTextures(1, &texture));
+ texture = 0;
+}
+
+LinePatternPos LineAtlas::getDashPosition(const std::vector<float> &dasharray, bool round) {
+ std::lock_guard<std::recursive_mutex> lock(mtx);
+
+ std::ostringstream sskey;
+
+ for (const float &part : dasharray) {
+ sskey << part << "-";
+ }
+ sskey << round;
+ std::string key = sskey.str();
+
+ if (positions.find(key) == positions.end()) {
+ positions[key] = addDash(dasharray, round);
+ }
+
+ return positions[key];
+}
+
+LinePatternPos LineAtlas::addDash(const std::vector<float> &dasharray, bool round) {
+
+ int n = round ? 7 : 0;
+ int dashheight = 2 * n + 1;
+ const uint8_t offset = 128;
+
+ if (nextRow + dashheight > height) {
+ fprintf(stderr, "[WARNING] line atlas bitmap overflow\n");
+ return LinePatternPos();
+ }
+
+ float length = 0;
+ for (const float &part : dasharray) {
+ length += part;
+ }
+
+ float stretch = width / length;
+ float halfWidth = stretch * 0.5;
+ // If dasharray has an odd length, both the first and last parts
+ // are dashes and should be joined seamlessly.
+ bool oddLength = dasharray.size() % 2 == 1;
+
+ for (int y = -n; y <= n; y++) {
+ int row = nextRow + n + y;
+ int index = width * row;
+
+ float left = 0;
+ float right = dasharray[0];
+ unsigned int partIndex = 1;
+
+ if (oddLength) {
+ left -= dasharray.back();
+ }
+
+ for (int x = 0; x < width; x++) {
+
+ while (right < x / stretch) {
+ left = right;
+ right = right + dasharray[partIndex];
+
+ if (oddLength && partIndex == dasharray.size() - 1) {
+ right += dasharray.front();
+ }
+
+ partIndex++;
+ }
+
+ float distLeft = fabs(x - left * stretch);
+ float distRight = fabs(x - right * stretch);
+ float dist = fmin(distLeft, distRight);
+ bool inside = (partIndex % 2) == 1;
+ int signedDistance;
+
+ if (round) {
+ float distMiddle = n ? (float)y / n * (halfWidth + 1) : 0;
+ if (inside) {
+ float distEdge = halfWidth - fabs(distMiddle);
+ signedDistance = sqrt(dist * dist + distEdge * distEdge);
+ } else {
+ signedDistance = halfWidth - sqrt(dist * dist + distMiddle * distMiddle);
+ }
+
+ } else {
+ signedDistance = int((inside ? 1 : -1) * dist);
+ }
+
+ data[index + x] = fmax(0, fmin(255, signedDistance + offset));
+ }
+ }
+
+ LinePatternPos position;
+ position.y = (0.5 + nextRow + n) / height;
+ position.height = (2.0 * n) / height;
+ position.width = length;
+
+ nextRow += dashheight;
+
+ dirty = true;
+ bind();
+
+ return position;
+};
+
+void LineAtlas::bind() {
+ std::lock_guard<std::recursive_mutex> lock(mtx);
+
+ bool first = false;
+ if (!texture) {
+ MBGL_CHECK_ERROR(glGenTextures(1, &texture));
+ MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
+ MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
+ MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
+ MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
+ MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
+ first = true;
+ } else {
+ MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
+ }
+
+ if (dirty) {
+ if (first) {
+ glTexImage2D(
+ GL_TEXTURE_2D, // GLenum target
+ 0, // GLint level
+ GL_ALPHA, // GLint internalformat
+ width, // GLsizei width
+ height, // GLsizei height
+ 0, // GLint border
+ GL_ALPHA, // GLenum format
+ GL_UNSIGNED_BYTE, // GLenum type
+ data // const GLvoid * data
+ );
+ } else {
+ glTexSubImage2D(
+ GL_TEXTURE_2D, // GLenum target
+ 0, // GLint level
+ 0, // GLint xoffset
+ 0, // GLint yoffset
+ width, // GLsizei width
+ height, // GLsizei height
+ GL_ALPHA, // GLenum format
+ GL_UNSIGNED_BYTE, // GLenum type
+ data // const GLvoid *pixels
+ );
+ }
+
+
+ dirty = false;
+ }
+};
diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp
new file mode 100644
index 0000000000..191dc0c4d4
--- /dev/null
+++ b/src/mbgl/geometry/line_atlas.hpp
@@ -0,0 +1,41 @@
+#ifndef MBGL_GEOMETRY_LINE_ATLAS
+#define MBGL_GEOMETRY_LINE_ATLAS
+
+#include <vector>
+#include <map>
+#include <mutex>
+#include <atomic>
+
+namespace mbgl {
+
+typedef struct {
+ float width;
+ float height;
+ float y;
+} LinePatternPos;
+
+class LineAtlas {
+public:
+ LineAtlas(uint16_t width, uint16_t height);
+ ~LineAtlas();
+
+ void bind();
+
+ LinePatternPos getDashPosition(const std::vector<float>&, bool);
+ LinePatternPos addDash(const std::vector<float> &dasharray, bool round);
+
+ const int width;
+ const int height;
+
+private:
+ std::recursive_mutex mtx;
+ char *const data = nullptr;
+ std::atomic<bool> dirty;
+ uint32_t texture = 0;
+ int nextRow = 0;
+ std::map<std::string, LinePatternPos> positions;
+};
+
+};
+
+#endif
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 2ec9018a8e..b6da256657 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -20,6 +20,7 @@
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/texture_pool.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
+#include <mbgl/geometry/line_atlas.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/string.hpp>
@@ -98,8 +99,9 @@ Map::Map(View& view_, FileSource& fileSource_)
glyphAtlas(util::make_unique<GlyphAtlas>(1024, 1024)),
glyphStore(std::make_shared<GlyphStore>(fileSource)),
spriteAtlas(util::make_unique<SpriteAtlas>(512, 512)),
+ lineAtlas(util::make_unique<LineAtlas>(512, 512)),
texturePool(std::make_shared<TexturePool>()),
- painter(util::make_unique<Painter>(*spriteAtlas, *glyphAtlas))
+ painter(util::make_unique<Painter>(*spriteAtlas, *glyphAtlas, *lineAtlas))
{
view.initialize(this);
// Make sure that we're doing an initial drawing in all cases.
diff --git a/src/mbgl/renderer/line_bucket.cpp b/src/mbgl/renderer/line_bucket.cpp
index e88b771eef..4937c8ac63 100644
--- a/src/mbgl/renderer/line_bucket.cpp
+++ b/src/mbgl/renderer/line_bucket.cpp
@@ -374,6 +374,20 @@ void LineBucket::drawLines(LineShader& shader) {
}
}
+void LineBucket::drawLineSDF(LineSDFShader& shader) {
+ char *vertex_index = BUFFER_OFFSET(vertex_start * vertexBuffer.itemSize);
+ char *elements_index = BUFFER_OFFSET(triangle_elements_start * triangleElementsBuffer.itemSize);
+ for (triangle_group_type& group : triangleGroups) {
+ if (!group.elements_length) {
+ continue;
+ }
+ group.array[2].bind(shader, vertexBuffer, triangleElementsBuffer, vertex_index);
+ MBGL_CHECK_ERROR(glDrawElements(GL_TRIANGLES, group.elements_length * 3, GL_UNSIGNED_SHORT, elements_index));
+ vertex_index += group.vertex_length * vertexBuffer.itemSize;
+ elements_index += group.elements_length * triangleElementsBuffer.itemSize;
+ }
+}
+
void LineBucket::drawLinePatterns(LinepatternShader& shader) {
char *vertex_index = BUFFER_OFFSET(vertex_start * vertexBuffer.itemSize);
char *elements_index = BUFFER_OFFSET(triangle_elements_start * triangleElementsBuffer.itemSize);
diff --git a/src/mbgl/renderer/line_bucket.hpp b/src/mbgl/renderer/line_bucket.hpp
index 7337ca80ad..c4d0d4a050 100644
--- a/src/mbgl/renderer/line_bucket.hpp
+++ b/src/mbgl/renderer/line_bucket.hpp
@@ -16,11 +16,12 @@ class LineVertexBuffer;
class TriangleElementsBuffer;
class LineShader;
class LinejoinShader;
+class LineSDFShader;
class LinepatternShader;
struct pbf;
class LineBucket : public Bucket {
- typedef ElementGroup<2> triangle_group_type;
+ typedef ElementGroup<3> triangle_group_type;
typedef ElementGroup<1> point_group_type;
public:
@@ -38,6 +39,7 @@ public:
bool hasPoints() const;
void drawLines(LineShader& shader);
+ void drawLineSDF(LineSDFShader& shader);
void drawLinePatterns(LinepatternShader& shader);
void drawPoints(LinejoinShader& shader);
diff --git a/src/mbgl/renderer/painter.cpp b/src/mbgl/renderer/painter.cpp
index 1af33c4130..0c17d52b5d 100644
--- a/src/mbgl/renderer/painter.cpp
+++ b/src/mbgl/renderer/painter.cpp
@@ -24,9 +24,10 @@ using namespace mbgl;
#define BUFFER_OFFSET(i) ((char *)nullptr + (i))
-Painter::Painter(SpriteAtlas& spriteAtlas_, GlyphAtlas& glyphAtlas_)
+Painter::Painter(SpriteAtlas& spriteAtlas_, GlyphAtlas& glyphAtlas_, LineAtlas& lineAtlas_)
: spriteAtlas(spriteAtlas_)
, glyphAtlas(glyphAtlas_)
+ , lineAtlas(lineAtlas_)
{
}
@@ -93,6 +94,7 @@ void Painter::setupShaders() {
if (!outlineShader) outlineShader = util::make_unique<OutlineShader>();
if (!lineShader) lineShader = util::make_unique<LineShader>();
if (!linejoinShader) linejoinShader = util::make_unique<LinejoinShader>();
+ if (!linesdfShader) linesdfShader = util::make_unique<LineSDFShader>();
if (!linepatternShader) linepatternShader = util::make_unique<LinepatternShader>();
if (!patternShader) patternShader = util::make_unique<PatternShader>();
if (!iconShader) iconShader = util::make_unique<IconShader>();
diff --git a/src/mbgl/renderer/painter.hpp b/src/mbgl/renderer/painter.hpp
index a39a368acb..4d4876a6f1 100644
--- a/src/mbgl/renderer/painter.hpp
+++ b/src/mbgl/renderer/painter.hpp
@@ -14,6 +14,7 @@
#include <mbgl/shader/pattern_shader.hpp>
#include <mbgl/shader/line_shader.hpp>
#include <mbgl/shader/linejoin_shader.hpp>
+#include <mbgl/shader/linesdf_shader.hpp>
#include <mbgl/shader/linepattern_shader.hpp>
#include <mbgl/shader/icon_shader.hpp>
#include <mbgl/shader/raster_shader.hpp>
@@ -38,6 +39,7 @@ class Tile;
class Sprite;
class SpriteAtlas;
class GlyphAtlas;
+class LineAtlas;
class Source;
class StyleSource;
class StyleLayerGroup;
@@ -56,7 +58,7 @@ class RasterTileData;
class Painter : private util::noncopyable {
public:
- Painter(SpriteAtlas&, GlyphAtlas&);
+ Painter(SpriteAtlas&, GlyphAtlas&, LineAtlas&);
~Painter();
void setup();
@@ -194,11 +196,13 @@ public:
SpriteAtlas& spriteAtlas;
GlyphAtlas& glyphAtlas;
+ LineAtlas& lineAtlas;
std::unique_ptr<PlainShader> plainShader;
std::unique_ptr<OutlineShader> outlineShader;
std::unique_ptr<LineShader> lineShader;
std::unique_ptr<LinejoinShader> linejoinShader;
+ std::unique_ptr<LineSDFShader> linesdfShader;
std::unique_ptr<LinepatternShader> linepatternShader;
std::unique_ptr<PatternShader> patternShader;
std::unique_ptr<IconShader> iconShader;
diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp
index 71364f40c4..2d57bb26ea 100644
--- a/src/mbgl/renderer/painter_line.cpp
+++ b/src/mbgl/renderer/painter_line.cpp
@@ -4,6 +4,7 @@
#include <mbgl/style/style_layer.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
+#include <mbgl/geometry/line_atlas.hpp>
#include <mbgl/map/map.hpp>
using namespace mbgl;
@@ -41,9 +42,6 @@ void Painter::renderLine(LineBucket& bucket, util::ptr<StyleLayer> layer_desc, c
color[2] *= properties.opacity;
color[3] *= properties.opacity;
- float dash_length = properties.dash_array[0];
- float dash_gap = properties.dash_array[1];
-
float ratio = state.getPixelRatio();
mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor);
@@ -72,7 +70,32 @@ void Painter::renderLine(LineBucket& bucket, util::ptr<StyleLayer> layer_desc, c
bucket.drawPoints(*linejoinShader);
}
- if (properties.image.size()) {
+ if (properties.dash_array.size()) {
+
+ useProgram(linesdfShader->program);
+
+ linesdfShader->u_matrix = vtxMatrix;
+ linesdfShader->u_exmatrix = extrudeMatrix;
+ linesdfShader->u_linewidth = {{ outset, inset }};
+ linesdfShader->u_ratio = ratio;
+ linesdfShader->u_blur = blur;
+ linesdfShader->u_color = color;
+
+ LinePatternPos pos = lineAtlas.getDashPosition(properties.dash_array, bucket.properties.cap == CapType::Round);
+ lineAtlas.bind();
+
+ float patternratio = std::pow(2.0, std::floor(std::log2(state.getScale())) - id.z) / 8.0;
+ float scaleX = patternratio / pos.width / properties.dash_line_width;
+ float scaleY = -pos.height / 2.0;
+
+ linesdfShader->u_patternscale = {{ scaleX, scaleY }};
+ linesdfShader->u_tex_y = pos.y;
+ linesdfShader->u_image = 0;
+ linesdfShader->u_sdfgamma = lineAtlas.width / (properties.dash_line_width * pos.width * 256.0 * state.getPixelRatio());
+
+ bucket.drawLineSDF(*linesdfShader);
+
+ } else if (properties.image.size()) {
SpriteAtlasPosition imagePos = spriteAtlas.getPosition(properties.image, true);
float factor = 8.0 / std::pow(2, state.getIntegerZoom() - id.z);
@@ -107,7 +130,6 @@ void Painter::renderLine(LineBucket& bucket, util::ptr<StyleLayer> layer_desc, c
lineShader->u_blur = blur;
lineShader->u_color = color;
- lineShader->u_dasharray = {{ dash_length, dash_gap }};
bucket.drawLines(*lineShader);
}
diff --git a/src/mbgl/shader/line.fragment.glsl b/src/mbgl/shader/line.fragment.glsl
index f4ac1458b3..717c46e10d 100644
--- a/src/mbgl/shader/line.fragment.glsl
+++ b/src/mbgl/shader/line.fragment.glsl
@@ -2,10 +2,7 @@ uniform vec2 u_linewidth;
uniform vec4 u_color;
uniform float u_blur;
-uniform vec2 u_dasharray;
-
varying vec2 v_normal;
-varying float v_linesofar;
void main() {
// Calculate the distance of the pixel from the line in pixels.
@@ -16,10 +13,5 @@ void main() {
// (v_linewidth.s)
float alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 0.0, 1.0);
- // Calculate the antialiasing fade factor based on distance to the dash.
- // Only affects alpha when line is dashed
- float pos = mod(v_linesofar, u_dasharray.x + u_dasharray.y);
- alpha *= max(step(0.0, -u_dasharray.y), clamp(min(pos, u_dasharray.x - pos), 0.0, 1.0));
-
gl_FragColor = u_color * alpha;
}
diff --git a/src/mbgl/shader/line.vertex.glsl b/src/mbgl/shader/line.vertex.glsl
index bf0b537e83..1f5432991c 100644
--- a/src/mbgl/shader/line.vertex.glsl
+++ b/src/mbgl/shader/line.vertex.glsl
@@ -20,11 +20,9 @@ uniform vec2 u_linewidth;
uniform vec4 u_color;
varying vec2 v_normal;
-varying float v_linesofar;
void main() {
vec2 a_extrude = a_data.xy;
- float a_linesofar = a_data.z * 128.0 + a_data.w;
// We store the texture normals in the most insignificant bit
// transform y so that 0 => -1 and 1 => 1
@@ -43,5 +41,4 @@ void main() {
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0.0, 1.0) + u_exmatrix * dist;
- v_linesofar = a_linesofar * u_ratio;
}
diff --git a/src/mbgl/shader/line_shader.hpp b/src/mbgl/shader/line_shader.hpp
index c6adc0ff8d..85152d6e0c 100644
--- a/src/mbgl/shader/line_shader.hpp
+++ b/src/mbgl/shader/line_shader.hpp
@@ -16,7 +16,6 @@ public:
UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this};
Uniform<std::array<float, 4>> u_color = {"u_color", *this};
Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this};
- Uniform<std::array<float, 2>> u_dasharray = {"u_dasharray", *this};
Uniform<float> u_ratio = {"u_ratio", *this};
Uniform<float> u_blur = {"u_blur", *this};
diff --git a/src/mbgl/shader/linesdf.fragment.glsl b/src/mbgl/shader/linesdf.fragment.glsl
new file mode 100644
index 0000000000..90a5eac3e2
--- /dev/null
+++ b/src/mbgl/shader/linesdf.fragment.glsl
@@ -0,0 +1,23 @@
+uniform vec2 u_linewidth;
+uniform vec4 u_color;
+uniform float u_blur;
+uniform sampler2D u_image;
+uniform float u_sdfgamma;
+
+varying vec2 v_normal;
+varying vec2 v_tex;
+
+void main() {
+ // Calculate the distance of the pixel from the line in pixels.
+ float dist = length(v_normal) * u_linewidth.s;
+
+ // Calculate the antialiasing fade factor. This is either when fading in
+ // the line in case of an offset line (v_linewidth.t) or when fading out
+ // (v_linewidth.s)
+ float alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 0.0, 1.0);
+
+ float sdfdist = texture2D(u_image, v_tex).a;
+ alpha *= smoothstep(0.5 - u_sdfgamma, 0.5 + u_sdfgamma, sdfdist);
+
+ gl_FragColor = u_color * alpha;
+}
diff --git a/src/mbgl/shader/linesdf.vertex.glsl b/src/mbgl/shader/linesdf.vertex.glsl
new file mode 100644
index 0000000000..0ab6804ab5
--- /dev/null
+++ b/src/mbgl/shader/linesdf.vertex.glsl
@@ -0,0 +1,49 @@
+// floor(127 / 2) == 63.0
+// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
+// stored in a byte (-128..127). we scale regular normals up to length 63, but
+// there are also "special" normals that have a bigger length (of up to 126 in
+// this case).
+// #define scale 63.0
+#define scale 0.015873016
+
+attribute vec2 a_pos;
+attribute vec4 a_data;
+
+// matrix is for the vertex position, exmatrix is for rotating and projecting
+// the extrusion vector.
+uniform mat4 u_matrix;
+uniform mat4 u_exmatrix;
+
+// shared
+uniform float u_ratio;
+uniform vec2 u_linewidth;
+uniform vec2 u_patternscale;
+uniform float u_tex_y;
+
+varying vec2 v_normal;
+varying vec2 v_tex;
+
+void main() {
+ vec2 a_extrude = a_data.xy;
+ float a_linesofar = a_data.z * 128.0 + a_data.w;
+
+ // We store the texture normals in the most insignificant bit
+ // transform y so that 0 => -1 and 1 => 1
+ // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap
+ // y is 1 if the normal points up, and -1 if it points down
+ vec2 normal = mod(a_pos, 2.0);
+ normal.y = sign(normal.y - 0.5);
+ v_normal = normal;
+
+ // Scale the extrusion vector down to a normal and then up by the line width
+ // of this vertex.
+ vec4 dist = vec4(u_linewidth.s * a_extrude * scale, 0.0, 0.0);
+
+ // Remove the texture normal bit of the position before scaling it with the
+ // model/view matrix. Add the extrusion vector *after* the model/view matrix
+ // because we're extruding the line in pixel space, regardless of the current
+ // tile's zoom level.
+ gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0.0, 1.0) + u_exmatrix * dist;
+
+ v_tex = vec2(a_linesofar * u_patternscale.x, normal.y * u_patternscale.y + u_tex_y);
+}
diff --git a/src/mbgl/shader/linesdf_shader.cpp b/src/mbgl/shader/linesdf_shader.cpp
new file mode 100644
index 0000000000..9802afb532
--- /dev/null
+++ b/src/mbgl/shader/linesdf_shader.cpp
@@ -0,0 +1,30 @@
+#include <mbgl/shader/linesdf_shader.hpp>
+#include <mbgl/shader/shaders.hpp>
+#include <mbgl/platform/gl.hpp>
+
+#include <cstdio>
+
+using namespace mbgl;
+
+LineSDFShader::LineSDFShader()
+ : Shader(
+ "line",
+ shaders[LINESDF_SHADER].vertex,
+ shaders[LINESDF_SHADER].fragment
+ ) {
+ if (!valid) {
+ fprintf(stderr, "invalid line shader\n");
+ return;
+ }
+
+ a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos"));
+ a_data = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data"));
+}
+
+void LineSDFShader::bind(char *offset) {
+ MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos));
+ MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0));
+
+ MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data));
+ MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_BYTE, false, 8, offset + 4));
+}
diff --git a/src/mbgl/shader/linesdf_shader.hpp b/src/mbgl/shader/linesdf_shader.hpp
new file mode 100644
index 0000000000..918ed5cbab
--- /dev/null
+++ b/src/mbgl/shader/linesdf_shader.hpp
@@ -0,0 +1,34 @@
+#ifndef MBGL_SHADER_SHADER_LINESDF
+#define MBGL_SHADER_SHADER_LINESDF
+
+#include <mbgl/shader/shader.hpp>
+#include <mbgl/shader/uniform.hpp>
+
+namespace mbgl {
+
+class LineSDFShader : public Shader {
+public:
+ LineSDFShader();
+
+ void bind(char *offset);
+
+ UniformMatrix<4> u_matrix = {"u_matrix", *this};
+ UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this};
+ Uniform<std::array<float, 4>> u_color = {"u_color", *this};
+ Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this};
+ Uniform<float> u_ratio = {"u_ratio", *this};
+ Uniform<float> u_blur = {"u_blur", *this};
+ Uniform<std::array<float, 2>> u_patternscale = { "u_patternscale", *this };
+ Uniform<float> u_tex_y = {"u_tex_y", *this};
+ Uniform<int32_t> u_image = {"u_image", *this};
+ Uniform<float> u_sdfgamma = {"u_sdfgamma", *this};
+
+private:
+ int32_t a_pos = -1;
+ int32_t a_data = -1;
+};
+
+
+}
+
+#endif
diff --git a/src/mbgl/style/function_properties.cpp b/src/mbgl/style/function_properties.cpp
index 69466c1f64..81b1c85c72 100644
--- a/src/mbgl/style/function_properties.cpp
+++ b/src/mbgl/style/function_properties.cpp
@@ -12,6 +12,7 @@ inline T defaultStopsValue();
template <> inline bool defaultStopsValue() { return true; }
template <> inline float defaultStopsValue() { return 1.0f; }
template <> inline Color defaultStopsValue() { return {{ 0, 0, 0, 1 }}; }
+template <> inline std::vector<float> defaultStopsValue() { return {{ 1, 0 }}; }
template <typename T>
@@ -64,5 +65,6 @@ T StopsFunction<T>::evaluate(float z) const {
template bool StopsFunction<bool>::evaluate(float z) const;
template float StopsFunction<float>::evaluate(float z) const;
template Color StopsFunction<Color>::evaluate(float z) const;
+template std::vector<float> StopsFunction<std::vector<float>>::evaluate(float z) const;
}
diff --git a/src/mbgl/style/property_fallback.cpp b/src/mbgl/style/property_fallback.cpp
index 965baf6c4b..dc747b37e6 100644
--- a/src/mbgl/style/property_fallback.cpp
+++ b/src/mbgl/style/property_fallback.cpp
@@ -20,8 +20,6 @@ const std::map<PropertyKey, PropertyValue> PropertyFallbackValue::properties = {
{ PropertyKey::LineWidth, defaultStyleProperties<LineProperties>().width },
{ PropertyKey::LineGapWidth, defaultStyleProperties<LineProperties>().gap_width },
{ PropertyKey::LineBlur, defaultStyleProperties<LineProperties>().blur },
- { PropertyKey::LineDashLand, defaultStyleProperties<LineProperties>().dash_array[0] },
- { PropertyKey::LineDashGap, defaultStyleProperties<LineProperties>().dash_array[1] },
{ PropertyKey::IconOpacity, defaultStyleProperties<SymbolProperties>().icon.opacity },
{ PropertyKey::IconRotate, defaultStyleProperties<SymbolProperties>().icon.rotate },
diff --git a/src/mbgl/style/property_key.hpp b/src/mbgl/style/property_key.hpp
index efeebf0242..e283f11579 100644
--- a/src/mbgl/style/property_key.hpp
+++ b/src/mbgl/style/property_key.hpp
@@ -24,8 +24,6 @@ enum class PropertyKey {
LineGapWidth,
LineBlur,
LineDashArray, // for transitions only
- LineDashLand,
- LineDashGap,
LineImage,
IconOpacity,
diff --git a/src/mbgl/style/property_value.hpp b/src/mbgl/style/property_value.hpp
index 1b22b31177..e017981dee 100644
--- a/src/mbgl/style/property_value.hpp
+++ b/src/mbgl/style/property_value.hpp
@@ -5,6 +5,8 @@
#include <mbgl/style/function_properties.hpp>
#include <mbgl/style/types.hpp>
+#include <vector>
+
namespace mbgl {
typedef mapbox::util::variant<
@@ -13,7 +15,8 @@ typedef mapbox::util::variant<
RotateAnchorType,
Function<bool>,
Function<float>,
- Function<Color>
+ Function<Color>,
+ Function<std::vector<float>>
> PropertyValue;
}
diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp
index 76e0a514a9..e3bff6edef 100644
--- a/src/mbgl/style/style_layer.cpp
+++ b/src/mbgl/style/style_layer.cpp
@@ -180,9 +180,19 @@ void StyleLayer::applyStyleProperties<LineProperties>(const float z, const times
applyTransitionedStyleProperty(PropertyKey::LineWidth, line.width, z, now);
applyTransitionedStyleProperty(PropertyKey::LineGapWidth, line.gap_width, z, now);
applyTransitionedStyleProperty(PropertyKey::LineBlur, line.blur, z, now);
- applyTransitionedStyleProperty(PropertyKey::LineDashLand, line.dash_array[0], z, now);
- applyTransitionedStyleProperty(PropertyKey::LineDashGap, line.dash_array[1], z, now);
+ applyStyleProperty(PropertyKey::LineDashArray, line.dash_array, z, now);
applyStyleProperty(PropertyKey::LineImage, line.image, z, now);
+
+ // scale dash width by line-gap-width if present
+ applyStyleProperty(PropertyKey::LineGapWidth, line.dash_line_width, std::floor(z), now + 10000);
+ if (line.dash_line_width <= 0) {
+ // otherwise scale by line-width
+ applyStyleProperty(PropertyKey::LineWidth, line.dash_line_width, std::floor(z), now + 10000);
+ if (line.dash_line_width <= 0) {
+ // otherwise scale by default line-width value
+ line.dash_line_width = line.width;
+ }
+ }
}
template <>
diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp
index f472ae9e96..cdec991a16 100644
--- a/src/mbgl/style/style_parser.cpp
+++ b/src/mbgl/style/style_parser.cpp
@@ -209,6 +209,24 @@ Color parseColor(JSVal value) {
css_color.a}};
}
+std::tuple<bool,std::vector<float>> parseFloatArray(JSVal value) {
+ if (!value.IsArray()) {
+ Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers");
+ return std::tuple<bool, std::vector<float>> { false, std::vector<float>() };
+ }
+
+ std::vector<float> vec;
+ for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
+ JSVal part = value[i];
+ if (!part.IsNumber()) {
+ Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers");
+ return std::tuple<bool, std::vector<float>> { false, std::vector<float>() };
+ }
+ vec.push_back(part.GetDouble());
+ }
+ return std::tuple<bool, std::vector<float>> { true, vec };
+}
+
template <>
bool StyleParser::parseFunctionArgument(JSVal value) {
JSVal rvalue = replaceConstant(value);
@@ -239,6 +257,12 @@ Color StyleParser::parseFunctionArgument(JSVal value) {
return parseColor(rvalue);
}
+template <>
+std::vector<float> StyleParser::parseFunctionArgument(JSVal value) {
+ JSVal rvalue = replaceConstant(value);
+ return std::get<1>(parseFloatArray(rvalue));
+}
+
template <typename T> inline float defaultBaseValue() { return 1.75; }
template <> inline float defaultBaseValue<Color>() { return 1.0; }
@@ -293,17 +317,6 @@ std::tuple<bool, Function<T>> StyleParser::parseFunction(JSVal value) {
template <typename T>
-bool StyleParser::parseFunction(PropertyKey key, ClassProperties &klass, JSVal value) {
- bool parsed;
- Function<T> function;
- std::tie(parsed, function) = parseFunction<T>(value);
- if (parsed) {
- klass.set(key, function);
- }
- return parsed;
-}
-
-template <typename T>
bool StyleParser::setProperty(JSVal value, const char *property_name, PropertyKey key, ClassProperties &klass) {
bool parsed;
T result;
@@ -314,18 +327,6 @@ bool StyleParser::setProperty(JSVal value, const char *property_name, PropertyKe
return parsed;
}
-template <typename T>
-bool StyleParser::setProperty(JSVal value, const char *property_name, T &target) {
- bool parsed;
- T result;
- std::tie(parsed, result) = parseProperty<T>(value, property_name);
- if (parsed) {
- target = std::move(result);
- }
- return parsed;
-}
-
-
template<typename T>
bool StyleParser::parseOptionalProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
@@ -335,15 +336,6 @@ bool StyleParser::parseOptionalProperty(const char *property_name, PropertyKey k
}
}
-template <typename T>
-bool StyleParser::parseOptionalProperty(const char *property_name, T &target, JSVal value) {
- if (!value.HasMember(property_name)) {
- return false;
- } else {
- return setProperty<T>(replaceConstant(value[property_name]), property_name, target);
- }
-}
-
template<> std::tuple<bool, std::string> StyleParser::parseProperty(JSVal value, const char *property_name) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name);
@@ -426,6 +418,18 @@ template<> std::tuple<bool, Function<Color>> StyleParser::parseProperty(JSVal va
}
}
+template<> std::tuple<bool, Function<std::vector<float>>> StyleParser::parseProperty(JSVal value, const char *property_name) {
+ if (value.IsObject()) {
+ return parseFunction<std::vector<float>>(value);
+ } else if (value.IsArray()) {
+ std::tuple<bool, std::vector<float>> parsed = parseFloatArray(value);
+ return std::tuple<bool, Function<std::vector<float>>> { std::get<0>(parsed), ConstantFunction<std::vector<float>>(std::get<1>(parsed)) };
+ } else {
+ Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers, or a number array function", property_name);
+ return std::tuple<bool, Function<std::vector<float>>> { false, ConstantFunction<std::vector<float>>(std::vector<float>()) };
+ }
+}
+
template <typename T>
bool StyleParser::parseOptionalProperty(const char *property_name, const std::vector<PropertyKey> &keys, ClassProperties &klass, JSVal value) {
if (value.HasMember(property_name)) {
@@ -584,8 +588,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) {
parseOptionalProperty<PropertyTransition>("line-gap-width-transition", Key::LineGapWidth, klass, value);
parseOptionalProperty<Function<float>>("line-blur", Key::LineBlur, klass, value);
parseOptionalProperty<PropertyTransition>("line-blur-transition", Key::LineBlur, klass, value);
- parseOptionalProperty<Function<float>>("line-dasharray", { Key::LineDashLand, Key::LineDashGap }, klass, value);
- parseOptionalProperty<PropertyTransition>("line-dasharray-transition", Key::LineDashArray, klass, value);
+ parseOptionalProperty<Function<std::vector<float>>>("line-dasharray", Key::LineDashArray, klass, value);
parseOptionalProperty<std::string>("line-image", Key::LineImage, klass, value);
parseOptionalProperty<Function<float>>("icon-opacity", Key::IconOpacity, klass, value);
diff --git a/src/mbgl/style/style_parser.hpp b/src/mbgl/style/style_parser.hpp
index c37e856034..42e22f9458 100644
--- a/src/mbgl/style/style_parser.hpp
+++ b/src/mbgl/style/style_parser.hpp
@@ -69,18 +69,12 @@ private:
template <typename T>
bool parseOptionalProperty(const char *property_name, const std::vector<PropertyKey> &keys, ClassProperties &klass, JSVal value);
template <typename T>
- bool parseOptionalProperty(const char *property_name, T &target, JSVal value);
- template <typename T>
bool setProperty(JSVal value, const char *property_name, PropertyKey key, ClassProperties &klass);
- template <typename T>
- bool setProperty(JSVal value, const char *property_name, T &target);
template <typename T>
std::tuple<bool, T> parseProperty(JSVal value, const char *property_name);
template <typename T>
- bool parseFunction(PropertyKey key, ClassProperties &klass, JSVal value);
- template <typename T>
std::tuple<bool, Function<T>> parseFunction(JSVal value);
template <typename T>
T parseFunctionArgument(JSVal value);
diff --git a/src/mbgl/style/style_properties.hpp b/src/mbgl/style/style_properties.hpp
index c44b7c34c8..fe6a2c2503 100644
--- a/src/mbgl/style/style_properties.hpp
+++ b/src/mbgl/style/style_properties.hpp
@@ -9,6 +9,7 @@
#include <string>
#include <type_traits>
#include <memory>
+#include <vector>
namespace mbgl {
@@ -36,7 +37,8 @@ struct LineProperties {
float width = 1;
float gap_width = 0;
float blur = 0;
- std::array<float, 2> dash_array = {{ 1, -1 }};
+ std::vector<float> dash_array;
+ float dash_line_width = 0;
std::string image;
inline bool isVisible() const {
diff --git a/src/mbgl/util/interpolate.hpp b/src/mbgl/util/interpolate.hpp
index c9232db4eb..952d7b9c10 100644
--- a/src/mbgl/util/interpolate.hpp
+++ b/src/mbgl/util/interpolate.hpp
@@ -2,6 +2,7 @@
#define MBGL_UTIL_INTERPOLATE
#include <array>
+#include <vector>
namespace mbgl {
namespace util {
@@ -21,6 +22,10 @@ inline std::array<T, 4> interpolate(const std::array<T, 4>& a, const std::array<
}};
}
+inline std::vector<float> interpolate(const std::vector<float> &a, const std::vector<float>, const double) {
+ return a;
+}
+
}
}