From 2643a0efc74ac8b623744f399245c8b66c61e04f Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 20:56:18 -0500 Subject: [PATCH 01/16] start porting dash lines from js - switches to dash units that are scaled by line width - adds a lineatlas to store patterns line dashed lines - adds linesdf shader which renders line fill from the lineatlas js: 8e83d76af3bd877eb9639c02e296363e2c0186f8 --- include/mbgl/map/map.hpp | 2 + src/mbgl/geometry/line_atlas.cpp | 126 ++++++++++++++++++++++++++ src/mbgl/geometry/line_atlas.hpp | 40 ++++++++ src/mbgl/map/map.cpp | 4 +- src/mbgl/renderer/line_bucket.cpp | 14 +++ src/mbgl/renderer/line_bucket.hpp | 4 +- src/mbgl/renderer/painter.cpp | 4 +- src/mbgl/renderer/painter.hpp | 6 +- src/mbgl/renderer/painter_line.cpp | 34 ++++++- src/mbgl/shader/linesdf.fragment.glsl | 23 +++++ src/mbgl/shader/linesdf.vertex.glsl | 49 ++++++++++ src/mbgl/shader/linesdf_shader.cpp | 30 ++++++ src/mbgl/shader/linesdf_shader.hpp | 34 +++++++ src/mbgl/style/style_layer.cpp | 3 + src/mbgl/style/style_properties.hpp | 1 + 15 files changed, 365 insertions(+), 9 deletions(-) create mode 100644 src/mbgl/geometry/line_atlas.cpp create mode 100644 src/mbgl/geometry/line_atlas.hpp create mode 100644 src/mbgl/shader/linesdf.fragment.glsl create mode 100644 src/mbgl/shader/linesdf.vertex.glsl create mode 100644 src/mbgl/shader/linesdf_shader.cpp create mode 100644 src/mbgl/shader/linesdf_shader.hpp diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 45846170c..5d1f57658 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: @@ -182,6 +183,7 @@ private: util::ptr glyphStore; const std::unique_ptr spriteAtlas; util::ptr sprite; + const std::unique_ptr lineAtlas; util::ptr texturePool; const std::unique_ptr painter; diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp new file mode 100644 index 000000000..28ab0ba45 --- /dev/null +++ b/src/mbgl/geometry/line_atlas.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + +#include +#include + +using namespace mbgl; + +LineAtlas::LineAtlas(uint16_t w, uint16_t h) + : width(w), + height(h), + data(new char[w * h]), + dirty(true) { +} + +LineAtlas::~LineAtlas() { +} + +LinePatternPos LineAtlas::getDashPosition(std::vector &dasharray, bool round) { + std::ostringstream sskey; + + for (const float &part : dasharray) { + sskey << part << "-"; + } + sskey << round; + std::string key = sskey.str(); + + if (positions.find(key) == positions.end()) { + fprintf(stderr, "add %s\n", key.c_str()); + positions[key] = addDash(dasharray, round); + } + + return positions[key]; +} + +LinePatternPos LineAtlas::addDash(std::vector &dasharray, bool round) { + + int n = round ? 7 : 0; + int dashheight = 2 * n + 1; + const uint8_t offset = 128; + + // TODO check if enough space + + float length = 0; + for (const float &part : dasharray) { + length += part; + } + + float stretch = width / length; + float halfWidth = stretch * 0.5; + + for (int y = -n; y <= n; y++) { + int row = nextRow + n + y; + int index = width * row; + + float left = 0; + float right = dasharray[0]; + int partIndex = 1; + + for (int x = 0; x < width; x++) { + + while (right < x / stretch) { + left = right; + right = right + dasharray[partIndex]; + 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 ? y / n * halfWidth : 0; + float distEdge = halfWidth - fabs(distMiddle); + if (inside) { + 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 * n / height; + position.width = length; + + nextRow += dashheight; + + dirty = true; + bind(); + + return position; +}; + +void LineAtlas::bind() { + if (!texture) { + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + dirty = true; + } else { + glBindTexture(GL_TEXTURE_2D, texture); + } + + dirty = true; + + if (dirty) { + // TODO use texsubimage for updates + // TODO lock? + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data); + dirty = false; + } +}; diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp new file mode 100644 index 000000000..6e4d8ab5b --- /dev/null +++ b/src/mbgl/geometry/line_atlas.hpp @@ -0,0 +1,40 @@ +#ifndef MBGL_GEOMETRY_LINE_ATLAS +#define MBGL_GEOMETRY_LINE_ATLAS + +#include +#include + +#include + +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(std::vector&, bool); + LinePatternPos addDash(std::vector &dasharray, bool round); + + const int width; + const int height; + +private: + char *const data = nullptr; + std::atomic dirty; + uint32_t texture = 0; + uint32_t nextRow = 0; + std::map positions; +}; + +}; + +#endif diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index f78b8385c..2ad418916 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -98,8 +99,9 @@ Map::Map(View& view_, FileSource& fileSource_) glyphAtlas(util::make_unique(1024, 1024)), glyphStore(std::make_shared(fileSource)), spriteAtlas(util::make_unique(512, 512)), + lineAtlas(util::make_unique(512, 512)), texturePool(std::make_shared()), - painter(util::make_unique(*spriteAtlas, *glyphAtlas)) + painter(util::make_unique(*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 e88b771ee..4937c8ac6 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 7337ca80a..c4d0d4a05 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 1af33c413..0c17d52b5 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(); if (!lineShader) lineShader = util::make_unique(); if (!linejoinShader) linejoinShader = util::make_unique(); + if (!linesdfShader) linesdfShader = util::make_unique(); if (!linepatternShader) linepatternShader = util::make_unique(); if (!patternShader) patternShader = util::make_unique(); if (!iconShader) iconShader = util::make_unique(); diff --git a/src/mbgl/renderer/painter.hpp b/src/mbgl/renderer/painter.hpp index a39a368ac..4d4876a6f 100644 --- a/src/mbgl/renderer/painter.hpp +++ b/src/mbgl/renderer/painter.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -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; std::unique_ptr outlineShader; std::unique_ptr lineShader; std::unique_ptr linejoinShader; + std::unique_ptr linesdfShader; std::unique_ptr linepatternShader; std::unique_ptr patternShader; std::unique_ptr iconShader; diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index 71364f40c..eec583b5c 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include using namespace mbgl; @@ -41,9 +42,6 @@ void Painter::renderLine(LineBucket& bucket, util::ptr 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,33 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c bucket.drawPoints(*linejoinShader); } - if (properties.image.size()) { + if (properties.dash_array.size() && properties.dash_array[1] > 0) { + + 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; + + std::vector dash = { properties.dash_array[0], properties.dash_array[1] }; + LinePatternPos pos = lineAtlas.getDashPosition(dash, false); + 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 +131,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c lineShader->u_blur = blur; lineShader->u_color = color; - lineShader->u_dasharray = {{ dash_length, dash_gap }}; + lineShader->u_dasharray = {{ 1, -1 }}; bucket.drawLines(*lineShader); } diff --git a/src/mbgl/shader/linesdf.fragment.glsl b/src/mbgl/shader/linesdf.fragment.glsl new file mode 100644 index 000000000..90a5eac3e --- /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 000000000..0ab6804ab --- /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 000000000..9802afb53 --- /dev/null +++ b/src/mbgl/shader/linesdf_shader.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +#include + +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 000000000..918ed5cba --- /dev/null +++ b/src/mbgl/shader/linesdf_shader.hpp @@ -0,0 +1,34 @@ +#ifndef MBGL_SHADER_SHADER_LINESDF +#define MBGL_SHADER_SHADER_LINESDF + +#include +#include + +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> u_color = {"u_color", *this}; + Uniform> u_linewidth = {"u_linewidth", *this}; + Uniform u_ratio = {"u_ratio", *this}; + Uniform u_blur = {"u_blur", *this}; + Uniform> u_patternscale = { "u_patternscale", *this }; + Uniform u_tex_y = {"u_tex_y", *this}; + Uniform u_image = {"u_image", *this}; + Uniform u_sdfgamma = {"u_sdfgamma", *this}; + +private: + int32_t a_pos = -1; + int32_t a_data = -1; +}; + + +} + +#endif diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index e58756afa..024ac2f2c 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -188,6 +188,9 @@ void StyleLayer::applyStyleProperties(const float z, const times applyTransitionedStyleProperty(PropertyKey::LineDashLand, line.dash_array[0], z, now); applyTransitionedStyleProperty(PropertyKey::LineDashGap, line.dash_array[1], z, now); applyStyleProperty(PropertyKey::LineImage, line.image, z, now); + + // for scaling dasharrays + applyStyleProperty(PropertyKey::LineWidth, line.dash_line_width, std::floor(z), now + 10000); } template <> diff --git a/src/mbgl/style/style_properties.hpp b/src/mbgl/style/style_properties.hpp index c44b7c34c..e50cd87d3 100644 --- a/src/mbgl/style/style_properties.hpp +++ b/src/mbgl/style/style_properties.hpp @@ -37,6 +37,7 @@ struct LineProperties { float gap_width = 0; float blur = 0; std::array dash_array = {{ 1, -1 }}; + float dash_line_width = 1; std::string image; inline bool isVisible() const { From 8bfaf12793d68d6b6018fee50998079d4e5ade89 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 21:11:19 -0500 Subject: [PATCH 02/16] remove old dash support from line shader --- src/mbgl/renderer/painter_line.cpp | 1 - src/mbgl/shader/line.fragment.glsl | 8 -------- src/mbgl/shader/line.vertex.glsl | 3 --- src/mbgl/shader/line_shader.hpp | 1 - 4 files changed, 13 deletions(-) diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index eec583b5c..654d891d3 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -131,7 +131,6 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c lineShader->u_blur = blur; lineShader->u_color = color; - lineShader->u_dasharray = {{ 1, -1 }}; bucket.drawLines(*lineShader); } diff --git a/src/mbgl/shader/line.fragment.glsl b/src/mbgl/shader/line.fragment.glsl index f4ac1458b..717c46e10 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 bf0b537e8..1f5432991 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 c6adc0ff8..85152d6e0 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> u_color = {"u_color", *this}; Uniform> u_linewidth = {"u_linewidth", *this}; - Uniform> u_dasharray = {"u_dasharray", *this}; Uniform u_ratio = {"u_ratio", *this}; Uniform u_blur = {"u_blur", *this}; From 84e9dbdc09043a22d35a3d903dc9b024f1bf23a2 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 22:02:22 -0500 Subject: [PATCH 03/16] remove unused code --- src/mbgl/style/style_parser.cpp | 21 --------------------- src/mbgl/style/style_parser.hpp | 4 ---- 2 files changed, 25 deletions(-) diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index 2dec648af..928392a8d 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -314,18 +314,6 @@ bool StyleParser::setProperty(JSVal value, const char *property_name, PropertyKe return parsed; } -template -bool StyleParser::setProperty(JSVal value, const char *property_name, T &target) { - bool parsed; - T result; - std::tie(parsed, result) = parseProperty(value, property_name); - if (parsed) { - target = std::move(result); - } - return parsed; -} - - template bool StyleParser::parseOptionalProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { @@ -335,15 +323,6 @@ bool StyleParser::parseOptionalProperty(const char *property_name, PropertyKey k } } -template -bool StyleParser::parseOptionalProperty(const char *property_name, T &target, JSVal value) { - if (!value.HasMember(property_name)) { - return false; - } else { - return setProperty(replaceConstant(value[property_name]), property_name, target); - } -} - template<> std::tuple StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); diff --git a/src/mbgl/style/style_parser.hpp b/src/mbgl/style/style_parser.hpp index c37e85603..951f55478 100644 --- a/src/mbgl/style/style_parser.hpp +++ b/src/mbgl/style/style_parser.hpp @@ -69,11 +69,7 @@ private: template bool parseOptionalProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value); template - bool parseOptionalProperty(const char *property_name, T &target, JSVal value); - template bool setProperty(JSVal value, const char *property_name, PropertyKey key, ClassProperties &klass); - template - bool setProperty(JSVal value, const char *property_name, T &target); template std::tuple parseProperty(JSVal value, const char *property_name); From 2c615b613c3f4031cf7d40d37cee7ca5c6c96265 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 22:24:44 -0500 Subject: [PATCH 04/16] support dash arrays that are any length --- src/mbgl/geometry/line_atlas.cpp | 4 ++-- src/mbgl/geometry/line_atlas.hpp | 4 ++-- src/mbgl/renderer/painter_line.cpp | 5 ++--- src/mbgl/style/property_fallback.cpp | 2 -- src/mbgl/style/property_key.hpp | 2 -- src/mbgl/style/property_value.hpp | 5 ++++- src/mbgl/style/style_layer.cpp | 3 +-- src/mbgl/style/style_parser.cpp | 21 +++++++++++++++++++-- src/mbgl/style/style_properties.hpp | 3 ++- 9 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 28ab0ba45..ae5617bf3 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -17,7 +17,7 @@ LineAtlas::LineAtlas(uint16_t w, uint16_t h) LineAtlas::~LineAtlas() { } -LinePatternPos LineAtlas::getDashPosition(std::vector &dasharray, bool round) { +LinePatternPos LineAtlas::getDashPosition(const std::vector &dasharray, bool round) { std::ostringstream sskey; for (const float &part : dasharray) { @@ -34,7 +34,7 @@ LinePatternPos LineAtlas::getDashPosition(std::vector &dasharray, bool ro return positions[key]; } -LinePatternPos LineAtlas::addDash(std::vector &dasharray, bool round) { +LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool round) { int n = round ? 7 : 0; int dashheight = 2 * n + 1; diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index 6e4d8ab5b..800c047dc 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -21,8 +21,8 @@ public: void bind(); - LinePatternPos getDashPosition(std::vector&, bool); - LinePatternPos addDash(std::vector &dasharray, bool round); + LinePatternPos getDashPosition(const std::vector&, bool); + LinePatternPos addDash(const std::vector &dasharray, bool round); const int width; const int height; diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index 654d891d3..0aadd9219 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -70,7 +70,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c bucket.drawPoints(*linejoinShader); } - if (properties.dash_array.size() && properties.dash_array[1] > 0) { + if (properties.dash_array.size()) { useProgram(linesdfShader->program); @@ -81,8 +81,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c linesdfShader->u_blur = blur; linesdfShader->u_color = color; - std::vector dash = { properties.dash_array[0], properties.dash_array[1] }; - LinePatternPos pos = lineAtlas.getDashPosition(dash, false); + LinePatternPos pos = lineAtlas.getDashPosition(properties.dash_array, false); lineAtlas.bind(); float patternratio = std::pow(2.0, std::floor(std::log2(state.getScale())) - id.z) / 8.0; diff --git a/src/mbgl/style/property_fallback.cpp b/src/mbgl/style/property_fallback.cpp index 965baf6c4..dc747b37e 100644 --- a/src/mbgl/style/property_fallback.cpp +++ b/src/mbgl/style/property_fallback.cpp @@ -20,8 +20,6 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::LineWidth, defaultStyleProperties().width }, { PropertyKey::LineGapWidth, defaultStyleProperties().gap_width }, { PropertyKey::LineBlur, defaultStyleProperties().blur }, - { PropertyKey::LineDashLand, defaultStyleProperties().dash_array[0] }, - { PropertyKey::LineDashGap, defaultStyleProperties().dash_array[1] }, { PropertyKey::IconOpacity, defaultStyleProperties().icon.opacity }, { PropertyKey::IconRotate, defaultStyleProperties().icon.rotate }, diff --git a/src/mbgl/style/property_key.hpp b/src/mbgl/style/property_key.hpp index efeebf024..e283f1157 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 1b22b3117..b6ebcf3f6 100644 --- a/src/mbgl/style/property_value.hpp +++ b/src/mbgl/style/property_value.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace mbgl { typedef mapbox::util::variant< @@ -13,7 +15,8 @@ typedef mapbox::util::variant< RotateAnchorType, Function, Function, - Function + Function, + std::vector > PropertyValue; } diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index 024ac2f2c..54dfff300 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -185,8 +185,7 @@ void StyleLayer::applyStyleProperties(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); // for scaling dasharrays diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index 928392a8d..b000fd5b5 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -332,6 +332,24 @@ template<> std::tuple StyleParser::parseProperty(JSVal value, return std::tuple { true, { value.GetString(), value.GetStringLength() } }; } +template<> std::tuple> StyleParser::parseProperty(JSVal value, const char *property_name) { + if (!value.IsArray()) { + Log::Warning(Event::ParseStyle, "value of '%s' must be an array", property_name); + return std::tuple> { false, std::vector() }; + } + + std::vector vec; + for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { + JSVal part = value[i]; + if (!part.IsNumber()) { + Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", property_name); + return std::tuple> { false, std::vector() }; + } + vec.push_back(part.GetDouble()); + } + return std::tuple> { true, vec }; +} + template<> std::tuple StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); @@ -567,8 +585,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("line-gap-width-transition", Key::LineGapWidth, klass, value); parseOptionalProperty>("line-blur", Key::LineBlur, klass, value); parseOptionalProperty("line-blur-transition", Key::LineBlur, klass, value); - parseOptionalProperty>("line-dasharray", { Key::LineDashLand, Key::LineDashGap }, klass, value); - parseOptionalProperty("line-dasharray-transition", Key::LineDashArray, klass, value); + parseOptionalProperty>("line-dasharray", Key::LineDashArray, klass, value); parseOptionalProperty("line-image", Key::LineImage, klass, value); parseOptionalProperty>("icon-opacity", Key::IconOpacity, klass, value); diff --git a/src/mbgl/style/style_properties.hpp b/src/mbgl/style/style_properties.hpp index e50cd87d3..c5149b2d1 100644 --- a/src/mbgl/style/style_properties.hpp +++ b/src/mbgl/style/style_properties.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace mbgl { @@ -36,7 +37,7 @@ struct LineProperties { float width = 1; float gap_width = 0; float blur = 0; - std::array dash_array = {{ 1, -1 }}; + std::vector dash_array; float dash_line_width = 1; std::string image; From 4f827c21c62e9059cdfc108d2275ec99f639566c Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 22:56:44 -0500 Subject: [PATCH 05/16] implement round dash caps --- src/mbgl/geometry/line_atlas.cpp | 10 +++++----- src/mbgl/renderer/painter_line.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index ae5617bf3..45fcde99d 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -27,7 +27,7 @@ LinePatternPos LineAtlas::getDashPosition(const std::vector &dasharray, b std::string key = sskey.str(); if (positions.find(key) == positions.end()) { - fprintf(stderr, "add %s\n", key.c_str()); + //fprintf(stderr, "add %s\n", key.c_str()); positions[key] = addDash(dasharray, round); } @@ -73,12 +73,12 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun int signedDistance; if (round) { - float distMiddle = n ? y / n * halfWidth : 0; - float distEdge = halfWidth - fabs(distMiddle); + 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); + signedDistance = halfWidth - sqrt(dist * dist + distMiddle * distMiddle); } } else { @@ -91,7 +91,7 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun LinePatternPos position; position.y = (0.5 + nextRow + n) / height; - position.height = 2 * n / height; + position.height = (2.0 * n) / height; position.width = length; nextRow += dashheight; diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index 0aadd9219..2d57bb26e 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -81,7 +81,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c linesdfShader->u_blur = blur; linesdfShader->u_color = color; - LinePatternPos pos = lineAtlas.getDashPosition(properties.dash_array, false); + 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; From b66f4e2c4b21ee6e028610f4fddf8f22bb2ba5e2 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 23:09:14 -0500 Subject: [PATCH 06/16] use TexSubImage2D for line atlas updates --- src/mbgl/geometry/line_atlas.cpp | 51 ++++++++++++++++++++++++++------ src/mbgl/geometry/line_atlas.hpp | 3 +- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 45fcde99d..7485ef31a 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -15,9 +15,15 @@ LineAtlas::LineAtlas(uint16_t w, uint16_t h) } LineAtlas::~LineAtlas() { + std::lock_guard lock(mtx); + + MBGL_CHECK_ERROR(glDeleteTextures(1, &texture)); + texture = 0; } LinePatternPos LineAtlas::getDashPosition(const std::vector &dasharray, bool round) { + std::lock_guard lock(mtx); + std::ostringstream sskey; for (const float &part : dasharray) { @@ -103,21 +109,48 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun }; void LineAtlas::bind() { + bool first = false; if (!texture) { - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + 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)); dirty = true; } else { - glBindTexture(GL_TEXTURE_2D, texture); + MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } - dirty = true; - if (dirty) { + std::lock_guard lock(mtx); + 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 + ); + } + + // TODO use texsubimage for updates // TODO lock? glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data); diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index 800c047dc..329c907b1 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -3,7 +3,7 @@ #include #include - +#include #include namespace mbgl { @@ -28,6 +28,7 @@ public: const int height; private: + std::recursive_mutex mtx; char *const data = nullptr; std::atomic dirty; uint32_t texture = 0; From c472528f9bee16ccc81d805fe694e37392e7f421 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 15 Jan 2015 17:41:55 -0500 Subject: [PATCH 07/16] support functions of dash arrays --- src/mbgl/style/function_properties.cpp | 2 + src/mbgl/style/property_value.hpp | 2 +- src/mbgl/style/style_parser.cpp | 56 +++++++++++++++++--------- src/mbgl/util/interpolate.hpp | 6 +++ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/mbgl/style/function_properties.cpp b/src/mbgl/style/function_properties.cpp index 69466c1f6..81b1c85c7 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 defaultStopsValue() { return {{ 1, 0 }}; } template @@ -64,5 +65,6 @@ T StopsFunction::evaluate(float z) const { template bool StopsFunction::evaluate(float z) const; template float StopsFunction::evaluate(float z) const; template Color StopsFunction::evaluate(float z) const; +template std::vector StopsFunction>::evaluate(float z) const; } diff --git a/src/mbgl/style/property_value.hpp b/src/mbgl/style/property_value.hpp index b6ebcf3f6..e017981de 100644 --- a/src/mbgl/style/property_value.hpp +++ b/src/mbgl/style/property_value.hpp @@ -16,7 +16,7 @@ typedef mapbox::util::variant< Function, Function, Function, - std::vector + Function> > PropertyValue; } diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index b000fd5b5..795bcfa2d 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> parseFloatArray(JSVal value) { + if (!value.IsArray()) { + Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); + return std::tuple> { false, std::vector() }; + } + + std::vector 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> { false, std::vector() }; + } + vec.push_back(part.GetDouble()); + } + return std::tuple> { 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 StyleParser::parseFunctionArgument(JSVal value) { + JSVal rvalue = replaceConstant(value); + return std::get<1>(parseFloatArray(rvalue)); +} + template inline float defaultBaseValue() { return 1.75; } template <> inline float defaultBaseValue() { return 1.0; } @@ -332,24 +356,6 @@ template<> std::tuple StyleParser::parseProperty(JSVal value, return std::tuple { true, { value.GetString(), value.GetStringLength() } }; } -template<> std::tuple> StyleParser::parseProperty(JSVal value, const char *property_name) { - if (!value.IsArray()) { - Log::Warning(Event::ParseStyle, "value of '%s' must be an array", property_name); - return std::tuple> { false, std::vector() }; - } - - std::vector vec; - for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { - JSVal part = value[i]; - if (!part.IsNumber()) { - Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", property_name); - return std::tuple> { false, std::vector() }; - } - vec.push_back(part.GetDouble()); - } - return std::tuple> { true, vec }; -} - template<> std::tuple StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); @@ -423,6 +429,18 @@ template<> std::tuple> StyleParser::parseProperty(JSVal va } } +template<> std::tuple>> StyleParser::parseProperty(JSVal value, const char *property_name) { + if (value.IsObject()) { + return parseFunction>(value); + } else if (value.IsArray()) { + std::tuple> parsed = parseFloatArray(value); + return std::tuple>> { std::get<0>(parsed), ConstantFunction>(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>> { false, ConstantFunction>(std::vector()) }; + } +} + template bool StyleParser::parseOptionalProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value) { if (value.HasMember(property_name)) { @@ -585,7 +603,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("line-gap-width-transition", Key::LineGapWidth, klass, value); parseOptionalProperty>("line-blur", Key::LineBlur, klass, value); parseOptionalProperty("line-blur-transition", Key::LineBlur, klass, value); - parseOptionalProperty>("line-dasharray", Key::LineDashArray, klass, value); + parseOptionalProperty>>("line-dasharray", Key::LineDashArray, klass, value); parseOptionalProperty("line-image", Key::LineImage, klass, value); parseOptionalProperty>("icon-opacity", Key::IconOpacity, klass, value); diff --git a/src/mbgl/util/interpolate.hpp b/src/mbgl/util/interpolate.hpp index c9232db4e..5bc48d61a 100644 --- a/src/mbgl/util/interpolate.hpp +++ b/src/mbgl/util/interpolate.hpp @@ -2,6 +2,7 @@ #define MBGL_UTIL_INTERPOLATE #include +#include namespace mbgl { namespace util { @@ -21,6 +22,11 @@ inline std::array interpolate(const std::array& a, const std::array< }}; } +inline std::vector interpolate(const std::vector &a, const std::vector &b, const double t) { + fprintf(stderr, "interpolate this\n"); + return a; +} + } } From 221d256beae6ff06754ee52feffbfc2953df699b Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 15 Jan 2015 20:44:10 -0500 Subject: [PATCH 08/16] remove unused --- src/mbgl/style/style_parser.cpp | 11 ----------- src/mbgl/style/style_parser.hpp | 2 -- 2 files changed, 13 deletions(-) diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index 795bcfa2d..f09709dff 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -316,17 +316,6 @@ std::tuple> StyleParser::parseFunction(JSVal value) { } -template -bool StyleParser::parseFunction(PropertyKey key, ClassProperties &klass, JSVal value) { - bool parsed; - Function function; - std::tie(parsed, function) = parseFunction(value); - if (parsed) { - klass.set(key, function); - } - return parsed; -} - template bool StyleParser::setProperty(JSVal value, const char *property_name, PropertyKey key, ClassProperties &klass) { bool parsed; diff --git a/src/mbgl/style/style_parser.hpp b/src/mbgl/style/style_parser.hpp index 951f55478..42e22f945 100644 --- a/src/mbgl/style/style_parser.hpp +++ b/src/mbgl/style/style_parser.hpp @@ -74,8 +74,6 @@ private: template std::tuple parseProperty(JSVal value, const char *property_name); - template - bool parseFunction(PropertyKey key, ClassProperties &klass, JSVal value); template std::tuple> parseFunction(JSVal value); template From 1ce0ca0b7a38218e53601b0b1e9d72a3ff55e52e Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 16 Jan 2015 13:37:13 -0500 Subject: [PATCH 09/16] add warning when line atlas is overflowed --- src/mbgl/geometry/line_atlas.cpp | 6 ++++-- src/mbgl/geometry/line_atlas.hpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 7485ef31a..5cfb67021 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -33,7 +33,6 @@ LinePatternPos LineAtlas::getDashPosition(const std::vector &dasharray, b std::string key = sskey.str(); if (positions.find(key) == positions.end()) { - //fprintf(stderr, "add %s\n", key.c_str()); positions[key] = addDash(dasharray, round); } @@ -46,7 +45,10 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun int dashheight = 2 * n + 1; const uint8_t offset = 128; - // TODO check if enough space + if (nextRow + dashheight > height) { + fprintf(stderr, "[WARNING] line atlas bitmap overflow\n"); + return LinePatternPos(); + } float length = 0; for (const float &part : dasharray) { diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index 329c907b1..191dc0c4d 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -32,7 +32,7 @@ private: char *const data = nullptr; std::atomic dirty; uint32_t texture = 0; - uint32_t nextRow = 0; + int nextRow = 0; std::map positions; }; From bf4e9d086293d6a398955af7453101a0abdf8ba4 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 16 Jan 2015 14:39:47 -0500 Subject: [PATCH 10/16] remove duplicate lineatlas texture update --- src/mbgl/geometry/line_atlas.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 5cfb67021..fa5535a8c 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -111,6 +111,8 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun }; void LineAtlas::bind() { + std::lock_guard lock(mtx); + bool first = false; if (!texture) { MBGL_CHECK_ERROR(glGenTextures(1, &texture)); @@ -119,13 +121,12 @@ void LineAtlas::bind() { 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)); - dirty = true; + first = true; } else { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } if (dirty) { - std::lock_guard lock(mtx); if (first) { glTexImage2D( GL_TEXTURE_2D, // GLenum target @@ -153,9 +154,6 @@ void LineAtlas::bind() { } - // TODO use texsubimage for updates - // TODO lock? - glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data); dirty = false; } }; From 200b1ce449eb49e8c789aef7b23c59f00e7c681c Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 16 Jan 2015 14:40:30 -0500 Subject: [PATCH 11/16] remove names to suppress unused warnings --- src/mbgl/util/interpolate.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mbgl/util/interpolate.hpp b/src/mbgl/util/interpolate.hpp index 5bc48d61a..952d7b9c1 100644 --- a/src/mbgl/util/interpolate.hpp +++ b/src/mbgl/util/interpolate.hpp @@ -22,8 +22,7 @@ inline std::array interpolate(const std::array& a, const std::array< }}; } -inline std::vector interpolate(const std::vector &a, const std::vector &b, const double t) { - fprintf(stderr, "interpolate this\n"); +inline std::vector interpolate(const std::vector &a, const std::vector, const double) { return a; } From 07e23967d80d889a1ee6819a2a8b2fc9c2168616 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 16 Jan 2015 16:21:14 -0500 Subject: [PATCH 12/16] eamlessly join odd-length dasharrays js: 8314099fa4165714c743ebe891fcff0e618ebcde --- src/mbgl/geometry/line_atlas.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index fa5535a8c..b396d9325 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -57,6 +57,9 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun 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; @@ -64,13 +67,22 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun float left = 0; float right = dasharray[0]; - int partIndex = 1; + 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++; } From 4791bac6753e16ef910a47c514e48b355e20295f Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 16 Jan 2015 16:29:58 -0500 Subject: [PATCH 13/16] scale dashes by line-gap-width if present https://github.com/mapbox/mapbox-gl-js/pull/860#issuecomment-68788411 js: a5fb9f9fb5c7ea056b6ec228e0b398815cc0d356 --- src/mbgl/style/style_layer.cpp | 12 ++++++++++-- src/mbgl/style/style_properties.hpp | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index 54dfff300..8f580715d 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -188,8 +188,16 @@ void StyleLayer::applyStyleProperties(const float z, const times applyStyleProperty(PropertyKey::LineDashArray, line.dash_array, z, now); applyStyleProperty(PropertyKey::LineImage, line.image, z, now); - // for scaling dasharrays - applyStyleProperty(PropertyKey::LineWidth, line.dash_line_width, std::floor(z), now + 10000); + // 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_properties.hpp b/src/mbgl/style/style_properties.hpp index c5149b2d1..fe6a2c250 100644 --- a/src/mbgl/style/style_properties.hpp +++ b/src/mbgl/style/style_properties.hpp @@ -38,7 +38,7 @@ struct LineProperties { float gap_width = 0; float blur = 0; std::vector dash_array; - float dash_line_width = 1; + float dash_line_width = 0; std::string image; inline bool isVisible() const { From 8e23e3a3e844867bdcd8d485cf121a93905db256 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 21 Jan 2015 18:41:17 -0800 Subject: [PATCH 14/16] Remove support for prerendered layers https://github.com/mapbox/mapbox-gl-style-spec/issues/232 --- src/mbgl/map/map.cpp | 8 +- src/mbgl/map/tile_parser.cpp | 8 +- src/mbgl/renderer/painter_prerender.cpp | 45 ------- src/mbgl/renderer/painter_raster.cpp | 45 +------ src/mbgl/renderer/prerendered_texture.cpp | 154 ---------------------- src/mbgl/renderer/prerendered_texture.hpp | 37 ------ src/mbgl/renderer/raster_bucket.cpp | 1 - src/mbgl/renderer/raster_bucket.hpp | 2 - src/mbgl/style/style_bucket.hpp | 4 - src/mbgl/style/style_layer.cpp | 9 -- src/mbgl/style/style_layer.hpp | 3 - src/mbgl/style/style_parser.cpp | 28 +--- src/mbgl/util/raster.hpp | 1 - 13 files changed, 7 insertions(+), 338 deletions(-) delete mode 100644 src/mbgl/renderer/painter_prerender.cpp delete mode 100644 src/mbgl/renderer/prerendered_texture.cpp delete mode 100644 src/mbgl/renderer/prerendered_texture.hpp diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index a7c1636f0..2ec9018a8 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -575,12 +575,8 @@ void Map::updateSources(const util::ptr &group) { } for (const util::ptr &layer : group->layers) { if (!layer) continue; - if (layer->bucket) { - if (layer->bucket->style_source) { - (*activeSources.emplace(layer->bucket->style_source).first)->enabled = true; - } - } else if (layer->layers) { - updateSources(layer->layers); + if (layer->bucket && layer->bucket->style_source) { + (*activeSources.emplace(layer->bucket->style_source).first)->enabled = true; } } } diff --git a/src/mbgl/map/tile_parser.cpp b/src/mbgl/map/tile_parser.cpp index 1e12e5fc1..e72bb79dc 100644 --- a/src/mbgl/map/tile_parser.cpp +++ b/src/mbgl/map/tile_parser.cpp @@ -70,10 +70,8 @@ void TileParser::parseStyleLayers(util::ptr group) { if (layer_desc->isBackground()) { // background is a special, fake bucket continue; - } else if (layer_desc->layers) { - // This is a layer group. - parseStyleLayers(layer_desc->layers); } + if (layer_desc->bucket) { // This is a singular layer. Check if this bucket already exists. If not, // parse this bucket. @@ -88,7 +86,7 @@ void TileParser::parseStyleLayers(util::ptr group) { } } } else { - fprintf(stderr, "[WARNING] layer '%s' does not have child layers or buckets\n", layer_desc->id.c_str()); + fprintf(stderr, "[WARNING] layer '%s' does not have buckets\n", layer_desc->id.c_str()); } } } @@ -117,8 +115,6 @@ std::unique_ptr TileParser::createBucket(util::ptr bucket_d } else { fprintf(stderr, "[WARNING] unknown bucket render type for layer '%s' (source layer '%s')\n", bucket_desc->name.c_str(), bucket_desc->source_layer.c_str()); } - } else if (bucket_desc->render.is() && bucket_desc->render.get().prerendered == true) { - return createRasterBucket(bucket_desc->render.get()); } else { // The layer specified in the bucket does not exist. Do nothing. if (debug::tileParseWarnings) { diff --git a/src/mbgl/renderer/painter_prerender.cpp b/src/mbgl/renderer/painter_prerender.cpp deleted file mode 100644 index 3ea948535..000000000 --- a/src/mbgl/renderer/painter_prerender.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include -#include - -using namespace mbgl; - -void Painter::preparePrerender(RasterBucket &bucket) { - MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST)); - MBGL_CHECK_ERROR(glDisable(GL_STENCIL_TEST)); - -// Render the actual tile. -#if GL_EXT_discard_framebuffer - const GLenum discards[] = {GL_COLOR_ATTACHMENT0}; - MBGL_CHECK_ERROR(glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, discards)); -#endif - MBGL_CHECK_ERROR(glClearColor(0.0, 0.0, 0.0, 0.0)); - MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); - - MBGL_CHECK_ERROR(glViewport(0, 0, bucket.properties.size, bucket.properties.size)); -} - -void Painter::renderPrerenderedTexture(RasterBucket &bucket, const mat4 &matrix, const RasterProperties& properties) { - const int buffer = bucket.properties.buffer * 4096.0f; - - // draw the texture on a quad - useProgram(rasterShader->program); - rasterShader->u_matrix = matrix; - rasterShader->u_opacity = 1; - - depthRange(strata, 1.0f); - - MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0)); - rasterShader->u_image = 0; - rasterShader->u_buffer = buffer; - rasterShader->u_opacity = properties.opacity; - rasterShader->u_brightness_low = properties.brightness[0]; - rasterShader->u_brightness_high = properties.brightness[1]; - rasterShader->u_saturation_factor = saturationFactor(properties.saturation); - rasterShader->u_contrast_factor = contrastFactor(properties.contrast); - rasterShader->u_spin_weights = spinWeights(properties.hue_rotate); - bucket.texture.bindTexture(); - coveringRasterArray.bind(*rasterShader, tileStencilBuffer, BUFFER_OFFSET(0)); - MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)tileStencilBuffer.index())); -} diff --git a/src/mbgl/renderer/painter_raster.cpp b/src/mbgl/renderer/painter_raster.cpp index 4c98a5e65..a85b77378 100644 --- a/src/mbgl/renderer/painter_raster.cpp +++ b/src/mbgl/renderer/painter_raster.cpp @@ -9,53 +9,11 @@ using namespace mbgl; -void Painter::renderRaster(RasterBucket& bucket, util::ptr layer_desc, const Tile::ID& id, const mat4 &matrix) { +void Painter::renderRaster(RasterBucket& bucket, util::ptr layer_desc, const Tile::ID&, const mat4 &matrix) { if (pass != RenderPass::Translucent) return; const RasterProperties &properties = layer_desc->getProperties(); - if (layer_desc->layers) { - - if (!bucket.texture.getTexture()) { - - bucket.texture.bindFramebuffer(); - - preparePrerender(bucket); - - const int buffer = bucket.properties.buffer * 4096.0f; - - const mat4 preMatrix = [&]{ - mat4 vtxMatrix; - matrix::ortho(vtxMatrix, -buffer, 4096 + buffer, -4096 - buffer, buffer, 0, 1); - matrix::translate(vtxMatrix, vtxMatrix, 0, -4096, 0); - return vtxMatrix; - }(); - - for (const util::ptr &layer : layer_desc->layers->layers) { - setOpaque(); - renderLayer(layer, &id, &preMatrix); - setTranslucent(); - renderLayer(layer, &id, &preMatrix); - } - - if (bucket.properties.blur > 0) { - bucket.texture.blur(*this, bucket.properties.blur); - } - - bucket.texture.unbindFramebuffer(); - - MBGL_CHECK_ERROR(glEnable(GL_DEPTH_TEST)); - MBGL_CHECK_ERROR(glEnable(GL_STENCIL_TEST)); - - MBGL_CHECK_ERROR(glViewport(0, 0, gl_viewport[0], gl_viewport[1])); - - } - - renderPrerenderedTexture(bucket, matrix, properties); - - } - - // Only draw non-prerendered raster here if (bucket.hasData()) { depthMask(false); @@ -75,7 +33,6 @@ void Painter::renderRaster(RasterBucket& bucket, util::ptr layer_des depthMask(true); } - } float Painter::saturationFactor(float saturation) { diff --git a/src/mbgl/renderer/prerendered_texture.cpp b/src/mbgl/renderer/prerendered_texture.cpp deleted file mode 100644 index 260f1fcc3..000000000 --- a/src/mbgl/renderer/prerendered_texture.cpp +++ /dev/null @@ -1,154 +0,0 @@ -#include - -#include -#include - -using namespace mbgl; - -PrerenderedTexture::PrerenderedTexture(const StyleBucketRaster &properties_) - : properties(properties_) { -} - -PrerenderedTexture::~PrerenderedTexture() { - if (texture != 0) { - MBGL_CHECK_ERROR(glDeleteTextures(1, &texture)); - texture = 0; - } - - if (fbo != 0) { - MBGL_CHECK_ERROR(glDeleteFramebuffers(1, &fbo)); - fbo = 0; - } -} - - -void PrerenderedTexture::bindTexture() { - if (texture == 0) { - bindFramebuffer(); - unbindFramebuffer(); - } - - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); -} - -void PrerenderedTexture::bindFramebuffer() { - MBGL_CHECK_ERROR(glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previous_fbo)); - - if (texture == 0) { - MBGL_CHECK_ERROR(glGenTextures(1, &texture)); - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); -#ifndef GL_ES_VERSION_2_0 - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); -#endif - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, properties.size, properties.size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)); - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, 0)); - } - - if (fbo_depth_stencil == 0) { - // Create depth/stencil buffer - MBGL_CHECK_ERROR(glGenRenderbuffers(1, &fbo_depth_stencil)); - MBGL_CHECK_ERROR(glBindRenderbuffer(GL_RENDERBUFFER, fbo_depth_stencil)); -#ifdef GL_ES_VERSION_2_0 - MBGL_CHECK_ERROR(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, properties.size, properties.size)); -#else - MBGL_CHECK_ERROR(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, properties.size, properties.size)); -#endif - MBGL_CHECK_ERROR(glBindRenderbuffer(GL_RENDERBUFFER, 0)); - } - - if (fbo == 0) { - MBGL_CHECK_ERROR(glGenFramebuffers(1, &fbo)); - MBGL_CHECK_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, fbo)); - MBGL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0)); -#ifdef GL_ES_VERSION_2_0 - MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo_depth_stencil)); - MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fbo_depth_stencil)); -#else - MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fbo_depth_stencil)); -#endif - - GLenum status = MBGL_CHECK_ERROR(glCheckFramebufferStatus(GL_FRAMEBUFFER)); - if (status != GL_FRAMEBUFFER_COMPLETE) { - fprintf(stderr, "Couldn't create framebuffer: "); - switch (status) { - case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: fprintf(stderr, "incomplete attachment\n"); break; - case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: fprintf(stderr, "incomplete missing attachment\n"); break; -#ifdef GL_ES_VERSION_2_0 - case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: fprintf(stderr, "incomplete dimensions\n"); break; -#else - case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: fprintf(stderr, "incomplete draw buffer\n"); break; -#endif - case GL_FRAMEBUFFER_UNSUPPORTED: fprintf(stderr, "unsupported\n"); break; - default: fprintf(stderr, "other\n"); break; - } - return; - } - } else { - MBGL_CHECK_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, fbo)); - } -} - -void PrerenderedTexture::unbindFramebuffer() { - MBGL_CHECK_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, previous_fbo)); - - if (fbo != 0) { - MBGL_CHECK_ERROR(glDeleteFramebuffers(1, &fbo)); - fbo = 0; - } -} - -void PrerenderedTexture::blur(Painter& painter, uint16_t passes) { - const GLuint original_texture = texture; - - // Create a secondary texture - GLuint secondary_texture; - MBGL_CHECK_ERROR(glGenTextures(1, &secondary_texture)); - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, secondary_texture)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, properties.size, properties.size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)); - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, 0)); - - - painter.useProgram(painter.gaussianShader->program); - painter.gaussianShader->u_matrix = painter.flipMatrix; - painter.gaussianShader->u_image = 0; - MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0)); - - for (int i = 0; i < passes; i++) { - // Render horizontal - MBGL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, secondary_texture, 0)); -#if GL_EXT_discard_framebuffer - const GLenum discards[] = { GL_COLOR_ATTACHMENT0 }; - MBGL_CHECK_ERROR(glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, discards)); -#endif - MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); - - painter.gaussianShader->u_offset = {{ 1.0f / float(properties.size), 0 }}; - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, original_texture)); - painter.coveringGaussianArray.bind(*painter.gaussianShader, painter.tileStencilBuffer, BUFFER_OFFSET(0)); - MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)painter.tileStencilBuffer.index())); - - - - // Render vertical - MBGL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, original_texture, 0)); -#if GL_EXT_discard_framebuffer - MBGL_CHECK_ERROR(glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, discards)); -#endif - MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); - - painter.gaussianShader->u_offset = {{ 0, 1.0f / float(properties.size) }}; - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, secondary_texture)); - painter.coveringGaussianArray.bind(*painter.gaussianShader, painter.tileStencilBuffer, BUFFER_OFFSET(0)); - MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)painter.tileStencilBuffer.index())); - } - - MBGL_CHECK_ERROR(glDeleteTextures(1, &secondary_texture)); -} diff --git a/src/mbgl/renderer/prerendered_texture.hpp b/src/mbgl/renderer/prerendered_texture.hpp deleted file mode 100644 index e4dc61041..000000000 --- a/src/mbgl/renderer/prerendered_texture.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef MBGL_RENDERER_PRERENDERED_TEXTURE -#define MBGL_RENDERER_PRERENDERED_TEXTURE - -#include -#include - -namespace mbgl { - -class StyleBucketRaster; -class Painter; - -class PrerenderedTexture : private util::noncopyable { -public: - PrerenderedTexture(const StyleBucketRaster &properties); - ~PrerenderedTexture(); - - void bindTexture(); - void bindFramebuffer(); - void unbindFramebuffer(); - - inline GLuint getTexture() const { return texture; } - - void blur(Painter& painter, uint16_t passes); - -public: - const StyleBucketRaster &properties; - -private: - GLint previous_fbo = 0; - GLuint fbo = 0; - GLuint texture = 0; - GLuint fbo_depth_stencil = 0; -}; - -} - -#endif diff --git a/src/mbgl/renderer/raster_bucket.cpp b/src/mbgl/renderer/raster_bucket.cpp index dfba8cee2..6d09e41fd 100644 --- a/src/mbgl/renderer/raster_bucket.cpp +++ b/src/mbgl/renderer/raster_bucket.cpp @@ -5,7 +5,6 @@ using namespace mbgl; RasterBucket::RasterBucket(TexturePool& texturePool, const StyleBucketRaster& properties_) : properties(properties_), - texture(properties_), raster(texturePool) { } diff --git a/src/mbgl/renderer/raster_bucket.hpp b/src/mbgl/renderer/raster_bucket.hpp index 0a7651d7c..e03b08aa5 100644 --- a/src/mbgl/renderer/raster_bucket.hpp +++ b/src/mbgl/renderer/raster_bucket.hpp @@ -3,7 +3,6 @@ #include #include -#include #include @@ -24,7 +23,6 @@ public: bool setImage(const std::string &data); const StyleBucketRaster &properties; - PrerenderedTexture texture; void drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array); diff --git a/src/mbgl/style/style_bucket.hpp b/src/mbgl/style/style_bucket.hpp index d84d35d5b..cc71ea99f 100644 --- a/src/mbgl/style/style_bucket.hpp +++ b/src/mbgl/style/style_bucket.hpp @@ -80,10 +80,6 @@ public: class StyleBucketRaster { public: - bool prerendered = false; - uint16_t size = 256; - float blur = 0.0f; - float buffer = 0.03125f; }; typedef mapbox::util::variant &class_names, const t appliedProperties.add(ClassID::Fallback, begin, end, value); } } - - // Update all child layers as well. - if (layers) { - layers->setClasses(class_names, now, defaultTransition); - } } // Helper function for applying all properties of a a single class that haven't been applied yet. @@ -239,10 +234,6 @@ void StyleLayer::applyStyleProperties(const float z, const } void StyleLayer::updateProperties(float z, const timestamp now) { - if (layers) { - layers->updateProperties(z, now); - } - cleanupAppliedStyleProperties(now); switch (type) { diff --git a/src/mbgl/style/style_layer.hpp b/src/mbgl/style/style_layer.hpp index 641dc1e71..7cf661ca3 100644 --- a/src/mbgl/style/style_layer.hpp +++ b/src/mbgl/style/style_layer.hpp @@ -79,9 +79,6 @@ public: // Stores the evaluated, and cascaded styling information, specific to this // layer's type. StyleProperties properties; - - // Child layer array (if this layer has child layers). - util::ptr layers; }; } diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index 2dec648af..f472ae9e9 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -490,10 +490,6 @@ util::ptr StyleParser::createLayer(JSVal value) { util::ptr layer = std::make_shared( layer_id, std::move(paints)); - if (value.HasMember("layers")) { - layer->layers = createLayers(value["layers"]); - } - // Store the layer ID so we can reference it later. layers.emplace(layer_id, std::pair> { value, layer }); @@ -523,7 +519,7 @@ void StyleParser::parseLayer(std::pair> &pair) { } } - if (layer->bucket || (layer->layers && layer->type != StyleLayerType::Raster)) { + if (layer->bucket) { // Skip parsing this again. We already have a valid layer definition. return; } @@ -661,18 +657,9 @@ void StyleParser::parseReference(JSVal value, util::ptr &layer) { parseLayer(it->second); stack.pop_front(); - util::ptr reference = it->second.second; - layer->type = reference->type; - - if (reference->layers) { - Log::Warning(Event::ParseStyle, "layer '%s' references composite layer", layer->id.c_str()); - // We cannot parse this layer further. - return; - } else { - layer->bucket = reference->bucket; - } + layer->bucket = reference->bucket; } #pragma mark - Parse Bucket @@ -812,17 +799,6 @@ void StyleParser::parseLayout(JSVal value, util::ptr &layer) { parseRenderProperty(value, render.text.optional, "text-optional"); } break; - case StyleLayerType::Raster: { - StyleBucketRaster &render = bucket.render.get(); - - parseRenderProperty(value, render.size, "raster-size"); - parseRenderProperty(value, render.blur, "raster-blur"); - parseRenderProperty(value, render.buffer, "raster-buffer"); - if (layer->layers) { - render.prerendered = true; - } - } break; - default: // There are no render properties for these layer types. break; diff --git a/src/mbgl/util/raster.hpp b/src/mbgl/util/raster.hpp index ff27f509f..bbdfc5321 100644 --- a/src/mbgl/util/raster.hpp +++ b/src/mbgl/util/raster.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include From 9495b686e7e7e8cdfc429168d5b28758ed620a7d Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 22 Jan 2015 11:07:08 -0800 Subject: [PATCH 15/16] Use v7 styles / tests --- linux/main.cpp | 2 +- macosx/main.mm | 2 +- styles | 2 +- test/headless.cpp | 4 ++++ test/suite | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/linux/main.cpp b/linux/main.cpp index e2965a8ea..245cb527f 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -83,7 +83,7 @@ int main(int argc, char *argv[]) { // Load style if (style.empty()) - style = std::string("asset://") + std::string("styles/bright-v6.json"); + style = std::string("asset://") + std::string("styles/bright-v7.json"); map.setStyleURL(style); diff --git a/macosx/main.mm b/macosx/main.mm index 39ddde10d..277f8c44d 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -102,7 +102,7 @@ int main() { if (accessToken) fileSource.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]); // Load style - map.setStyleURL("asset://styles/bright-v6.json"); + map.setStyleURL("asset://styles/bright-v7.json"); int ret = view.run(); diff --git a/styles b/styles index 225f78f42..e8f361870 160000 --- a/styles +++ b/styles @@ -1 +1 @@ -Subproject commit 225f78f4281dbb315c030b1ba288e51193282cf7 +Subproject commit e8f361870c51aacbabb88aa0523703aaaa41ec4e diff --git a/test/headless.cpp b/test/headless.cpp index 3ec66dbd6..e9fd2c61b 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -161,6 +161,10 @@ TEST_P(HeadlessTest, render) { }; const rapidjson::Value& value = it->value; ASSERT_TRUE(value.IsObject()); + + if (value.HasMember("native") && !value["native"].GetBool()) + continue; + if (value.HasMember("center")) ASSERT_TRUE(value["center"].IsArray()); const std::string actual_image = base_directory + "tests/" + base + "/" + name + "/actual.png"; diff --git a/test/suite b/test/suite index 2892e261e..dba0f2b1f 160000 --- a/test/suite +++ b/test/suite @@ -1 +1 @@ -Subproject commit 2892e261eb6dd8ce42f304e009ef7b2ff5e1a9c8 +Subproject commit dba0f2b1fa749691435d06a251d733173d41e3f5 From 0c2ffc251c519af76a0ed6dd1925621ebe393e7b Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 22 Jan 2015 14:05:49 -0800 Subject: [PATCH 16/16] Revert "scale dashes by line-gap-width if present" This reverts commit 4791bac6753e16ef910a47c514e48b355e20295f. --- src/mbgl/style/style_layer.cpp | 12 ++---------- src/mbgl/style/style_properties.hpp | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index e3bff6ede..262ca23af 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -183,16 +183,8 @@ void StyleLayer::applyStyleProperties(const float z, const times 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; - } - } + // for scaling dasharrays + applyStyleProperty(PropertyKey::LineWidth, line.dash_line_width, std::floor(z), now + 10000); } template <> diff --git a/src/mbgl/style/style_properties.hpp b/src/mbgl/style/style_properties.hpp index fe6a2c250..c5149b2d1 100644 --- a/src/mbgl/style/style_properties.hpp +++ b/src/mbgl/style/style_properties.hpp @@ -38,7 +38,7 @@ struct LineProperties { float gap_width = 0; float blur = 0; std::vector dash_array; - float dash_line_width = 0; + float dash_line_width = 1; std::string image; inline bool isVisible() const {