diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-11-14 15:23:15 +0200 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-12-02 17:11:49 +0200 |
commit | 2c49c4374cb9e3654c1d347b1071542918ce5fd2 (patch) | |
tree | 9791f6f960939e92a5c891160d86065cc0cf0184 | |
parent | 2bb2a40b6d13d4fe44e8879003a3e53416033a34 (diff) | |
download | qtlocation-mapboxgl-2c49c4374cb9e3654c1d347b1071542918ce5fd2.tar.gz |
[core] Store 'sdf' flag in vertex attribute
-rw-r--r-- | src/mbgl/layout/symbol_instance.cpp | 6 | ||||
-rw-r--r-- | src/mbgl/layout/symbol_instance.hpp | 10 | ||||
-rw-r--r-- | src/mbgl/layout/symbol_layout.cpp | 13 | ||||
-rw-r--r-- | src/mbgl/programs/symbol_program.hpp | 36 | ||||
-rw-r--r-- | src/mbgl/text/quads.cpp | 17 | ||||
-rw-r--r-- | src/mbgl/text/quads.hpp | 7 | ||||
-rw-r--r-- | test/text/cross_tile_symbol_index.test.cpp | 12 | ||||
-rw-r--r-- | test/text/quads.test.cpp | 30 |
8 files changed, 73 insertions, 58 deletions
diff --git a/src/mbgl/layout/symbol_instance.cpp b/src/mbgl/layout/symbol_instance.cpp index d48a3af388..094f859830 100644 --- a/src/mbgl/layout/symbol_instance.cpp +++ b/src/mbgl/layout/symbol_instance.cpp @@ -26,13 +26,15 @@ SymbolInstanceSharedData::SymbolInstanceSharedData(GeometryCoordinates line_, const style::SymbolPlacementType textPlacement, const std::array<float, 2>& textOffset, const ImageMap& imageMap, + SymbolContent iconType, bool allowVerticalPlacement) : line(std::move(line_)) { // Create the quads used for rendering the icon and glyphs. if (shapedIcon) { - iconQuad = getIconQuad(*shapedIcon, getAnyShaping(shapedTextOrientations).writingMode); + iconQuad = getIconQuad(*shapedIcon, getAnyShaping(shapedTextOrientations).writingMode, iconType); if (verticallyShapedIcon) { - verticalIconQuad = getIconQuad(*verticallyShapedIcon, shapedTextOrientations.vertical.writingMode); + verticalIconQuad = + getIconQuad(*verticallyShapedIcon, shapedTextOrientations.vertical.writingMode, iconType); } } diff --git a/src/mbgl/layout/symbol_instance.hpp b/src/mbgl/layout/symbol_instance.hpp index bc2ff48d33..e20c05a0ab 100644 --- a/src/mbgl/layout/symbol_instance.hpp +++ b/src/mbgl/layout/symbol_instance.hpp @@ -21,6 +21,8 @@ struct ShapedTextOrientations { bool singleLine = false; }; +enum class SymbolContent : uint8_t { None = 0, Text = 1 << 0, IconRGBA = 1 << 1, IconSDF = 1 << 2 }; + struct SymbolInstanceSharedData { SymbolInstanceSharedData(GeometryCoordinates line, const ShapedTextOrientations& shapedTextOrientations, @@ -30,6 +32,7 @@ struct SymbolInstanceSharedData { const style::SymbolPlacementType textPlacement, const std::array<float, 2>& textOffset, const ImageMap& imageMap, + SymbolContent iconType, bool allowVerticalPlacement); bool empty() const; GeometryCoordinates line; @@ -42,13 +45,6 @@ struct SymbolInstanceSharedData { optional<SymbolQuad> verticalIconQuad; }; -enum class SymbolContent : uint8_t { - None = 0, - Text = 1 << 0, - IconRGBA = 1 << 1, - IconSDF = 1 << 2 -}; - class SymbolInstance { public: SymbolInstance(Anchor& anchor_, diff --git a/src/mbgl/layout/symbol_layout.cpp b/src/mbgl/layout/symbol_layout.cpp index 1207c1c668..c03ad8835f 100644 --- a/src/mbgl/layout/symbol_layout.cpp +++ b/src/mbgl/layout/symbol_layout.cpp @@ -597,6 +597,7 @@ void SymbolLayout::addFeature(const std::size_t layoutFeatureIndex, textPlacement, textOffset, imageMap, + iconType, allowVerticalPlacement); }; @@ -864,10 +865,14 @@ size_t SymbolLayout::addSymbol(SymbolBucket::Buffer& buffer, uint16_t index = segment.vertexLength; // coordinates (2 triangles) - buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex(labelAnchor.point, tl, symbol.glyphOffset.y, tex.x, tex.y, sizeData)); - buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex(labelAnchor.point, tr, symbol.glyphOffset.y, tex.x + tex.w, tex.y, sizeData)); - buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex(labelAnchor.point, bl, symbol.glyphOffset.y, tex.x, tex.y + tex.h, sizeData)); - buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex(labelAnchor.point, br, symbol.glyphOffset.y, tex.x + tex.w, tex.y + tex.h, sizeData)); + buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex( + labelAnchor.point, tl, symbol.glyphOffset.y, tex.x, tex.y, sizeData, symbol.isSDF)); + buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex( + labelAnchor.point, tr, symbol.glyphOffset.y, tex.x + tex.w, tex.y, sizeData, symbol.isSDF)); + buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex( + labelAnchor.point, bl, symbol.glyphOffset.y, tex.x, tex.y + tex.h, sizeData, symbol.isSDF)); + buffer.vertices.emplace_back(SymbolSDFIconProgram::layoutVertex( + labelAnchor.point, br, symbol.glyphOffset.y, tex.x + tex.w, tex.y + tex.h, sizeData, symbol.isSDF)); // Dynamic/Opacity vertices are initialized so that the vertex count always agrees with // the layout vertex buffer, but they will always be updated before rendering happens diff --git a/src/mbgl/programs/symbol_program.hpp b/src/mbgl/programs/symbol_program.hpp index 80e6245a03..f614181a1e 100644 --- a/src/mbgl/programs/symbol_program.hpp +++ b/src/mbgl/programs/symbol_program.hpp @@ -14,12 +14,15 @@ #include <mbgl/style/layers/symbol_layer_impl.hpp> #include <mbgl/renderer/layers/render_symbol_layer.hpp> - #include <cmath> #include <array> namespace mbgl { +const uint16_t MAX_GLYPH_ICON_SIZE = 255; +const uint16_t SIZE_PACK_FACTOR = 128; +const uint16_t MAX_PACKED_SIZE = MAX_GLYPH_ICON_SIZE * SIZE_PACK_FACTOR; + namespace style { class SymbolPropertyValues; } // namespace style @@ -197,26 +200,21 @@ public: class SymbolProgramBase { public: static gfx::Vertex<SymbolLayoutAttributes> layoutVertex(Point<float> labelAnchor, - Point<float> o, - float glyphOffsetY, - uint16_t tx, - uint16_t ty, - const Range<float>& sizeData) { + Point<float> o, + float glyphOffsetY, + uint16_t tx, + uint16_t ty, + const Range<float>& sizeData, + bool isSDF) { + const uint16_t aSizeMin = (std::min(MAX_PACKED_SIZE, static_cast<uint16_t>(sizeData.min * SIZE_PACK_FACTOR)) << 1) + uint16_t(isSDF); + const uint16_t aSizeMax = std::min(MAX_PACKED_SIZE, static_cast<uint16_t>(sizeData.max * SIZE_PACK_FACTOR)); return { // combining pos and offset to reduce number of vertex attributes passed to shader (8 max for some devices) - {{ - static_cast<int16_t>(labelAnchor.x), - static_cast<int16_t>(labelAnchor.y), - static_cast<int16_t>(::round(o.x * 32)), // use 1/32 pixels for placement - static_cast<int16_t>(::round((o.y + glyphOffsetY) * 32)) - }}, - {{ - tx, - ty, - static_cast<uint16_t>(sizeData.min * 256), - static_cast<uint16_t>(sizeData.max * 256) - }} - }; + {{static_cast<int16_t>(labelAnchor.x), + static_cast<int16_t>(labelAnchor.y), + static_cast<int16_t>(::round(o.x * 32)), // use 1/32 pixels for placement + static_cast<int16_t>(::round((o.y + glyphOffsetY) * 32))}}, + {{tx, ty, aSizeMin, aSizeMax}}}; } static gfx::Vertex<SymbolDynamicLayoutAttributes> dynamicLayoutVertex(Point<float> anchorPoint, float labelAngle) { diff --git a/src/mbgl/text/quads.cpp b/src/mbgl/text/quads.cpp index ee17510c35..480bcd79a9 100644 --- a/src/mbgl/text/quads.cpp +++ b/src/mbgl/text/quads.cpp @@ -1,10 +1,11 @@ +#include <mbgl/geometry/anchor.hpp> +#include <mbgl/layout/symbol_instance.hpp> +#include <mbgl/style/layers/symbol_layer_properties.hpp> #include <mbgl/text/quads.hpp> #include <mbgl/text/shaping.hpp> #include <mbgl/tile/geometry_tile_data.hpp> -#include <mbgl/geometry/anchor.hpp> -#include <mbgl/style/layers/symbol_layer_properties.hpp> -#include <mbgl/util/math.hpp> #include <mbgl/util/constants.hpp> +#include <mbgl/util/math.hpp> #include <mbgl/util/optional.hpp> #include <cassert> @@ -13,8 +14,7 @@ namespace mbgl { using namespace style; -SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, - WritingModeType writingMode) { +SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, WritingModeType writingMode, SymbolContent iconType) { const ImagePosition& image = shapedIcon.image(); // If you have a 10px icon that isn't perfectly aligned to the pixel grid it will cover 11 actual @@ -70,7 +70,7 @@ SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, static_cast<uint16_t>(image.textureRect.h + border * 2) }; - return SymbolQuad { tl, tr, bl, br, textureRect, writingMode, { 0.0f, 0.0f } }; + return SymbolQuad{tl, tr, bl, br, textureRect, writingMode, {0.0f, 0.0f}, iconType == SymbolContent::IconSDF}; } SymbolQuads getGlyphQuads(const Shaping& shapedText, @@ -96,6 +96,7 @@ SymbolQuads getGlyphQuads(const Shaping& shapedText, const bool rotateVerticalGlyph = (alongLine || allowVerticalPlacement) && positionedGlyph.vertical; const float halfAdvance = positionedGlyph.metrics.advance * positionedGlyph.scale / 2.0; const Rect<uint16_t>& rect = positionedGlyph.rect; + bool isSDF = true; // Align images and scaled glyphs in the middle of a vertical line. if (allowVerticalPlacement && shapedText.verticalizable) { @@ -111,6 +112,7 @@ SymbolQuads getGlyphQuads(const Shaping& shapedText, } pixelRatio = image->second->pixelRatio; rectBuffer = ImagePosition::padding / pixelRatio; + isSDF = image->second->sdf; } const Point<float> glyphOffset = @@ -179,7 +181,8 @@ SymbolQuads getGlyphQuads(const Shaping& shapedText, br = util::matrixMultiply(matrix, br); } - quads.emplace_back(tl, tr, bl, br, rect, shapedText.writingMode, glyphOffset, positionedGlyph.sectionIndex); + quads.emplace_back( + tl, tr, bl, br, rect, shapedText.writingMode, glyphOffset, isSDF, positionedGlyph.sectionIndex); } } diff --git a/src/mbgl/text/quads.hpp b/src/mbgl/text/quads.hpp index f67266b1ec..29f798ba0d 100644 --- a/src/mbgl/text/quads.hpp +++ b/src/mbgl/text/quads.hpp @@ -12,6 +12,7 @@ namespace mbgl { class Anchor; class PositionedIcon; +enum class SymbolContent : uint8_t; class SymbolQuad { public: @@ -22,6 +23,7 @@ public: Rect<uint16_t> tex_, WritingModeType writingMode_, Point<float> glyphOffset_, + bool isSDF_, size_t sectionIndex_ = 0) : tl(tl_), tr(tr_), @@ -30,6 +32,7 @@ public: tex(tex_), writingMode(writingMode_), glyphOffset(glyphOffset_), + isSDF(isSDF_), sectionIndex(sectionIndex_) {} Point<float> tl; @@ -39,13 +42,13 @@ public: Rect<uint16_t> tex; WritingModeType writingMode; Point<float> glyphOffset; + bool isSDF; size_t sectionIndex; }; using SymbolQuads = std::vector<SymbolQuad>; -SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, - WritingModeType writingMode); +SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, WritingModeType writingMode, SymbolContent iconType); SymbolQuads getGlyphQuads(const Shaping& shapedText, const std::array<float, 2> textOffset, diff --git a/test/text/cross_tile_symbol_index.test.cpp b/test/text/cross_tile_symbol_index.test.cpp index a02055e70f..cb3806e656 100644 --- a/test/text/cross_tile_symbol_index.test.cpp +++ b/test/text/cross_tile_symbol_index.test.cpp @@ -16,8 +16,16 @@ SymbolInstance makeSymbolInstance(float x, float y, std::u16string key) { std::array<float, 2> variableTextOffset{{0.0f, 0.0f}}; style::SymbolPlacementType placementType = style::SymbolPlacementType::Point; - auto sharedData = std::make_shared<SymbolInstanceSharedData>( - std::move(line), shaping, nullopt, nullopt, layout_, placementType, textOffset, imageMap, false); + auto sharedData = std::make_shared<SymbolInstanceSharedData>(std::move(line), + shaping, + nullopt, + nullopt, + layout_, + placementType, + textOffset, + imageMap, + SymbolContent::IconSDF, + false); return SymbolInstance(anchor, std::move(sharedData), shaping, nullopt, nullopt, 0, 0, placementType, textOffset, 0, 0, iconOffset, subfeature, 0, 0, key, 0.0f, 0.0f, 0.0f, variableTextOffset, false); } diff --git a/test/text/quads.test.cpp b/test/text/quads.test.cpp index 4d7f254efa..64255c72db 100644 --- a/test/text/quads.test.cpp +++ b/test/text/quads.test.cpp @@ -1,10 +1,11 @@ #include <mbgl/geometry/anchor.hpp> +#include <mbgl/layout/symbol_instance.hpp> #include <mbgl/style/image_impl.hpp> +#include <mbgl/style/layers/symbol_layer_properties.hpp> #include <mbgl/test/util.hpp> +#include <mbgl/text/glyph.hpp> #include <mbgl/text/quads.hpp> #include <mbgl/text/shaping.hpp> -#include <mbgl/text/glyph.hpp> -#include <mbgl/style/layers/symbol_layer_properties.hpp> using namespace mbgl; using namespace mbgl::style; @@ -21,8 +22,7 @@ TEST(getIconQuads, normal) { GeometryCoordinates line; - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_EQ(quad.tl.x, -14); EXPECT_EQ(quad.tl.y, -10); @@ -60,7 +60,7 @@ TEST(getIconQuads, style) { EXPECT_FLOAT_EQ(-18.5f, shapedIcon.left()); SymbolLayoutProperties::Evaluated layout; - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -19.5); EXPECT_FLOAT_EQ(quad.tl.y, -19.5); @@ -76,7 +76,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Width, {{0, 0, 0, 0}}, {{0, 0}}, 24.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -64.4444427); EXPECT_FLOAT_EQ(quad.tl.y, 0); @@ -92,7 +92,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Width, {{0, 0, 0, 0}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -32.2222214); EXPECT_FLOAT_EQ(quad.tl.y, -5); @@ -108,7 +108,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Width, {{5, 10, 5, 10}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -43.3333321); EXPECT_FLOAT_EQ(quad.tl.y, -5); @@ -124,7 +124,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Height, {{0, 0, 0, 0}}, {{0, 0}}, 24.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -30); EXPECT_FLOAT_EQ(quad.tl.y, -12.2222214); @@ -141,7 +141,7 @@ TEST(getIconQuads, style) { SymbolLayoutProperties::Evaluated layout; auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Height, {{0, 0, 0, 0}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -20); EXPECT_FLOAT_EQ(quad.tl.y, -6.11111069); @@ -157,7 +157,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Height, {{5, 10, 5, 20}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -20); EXPECT_FLOAT_EQ(quad.tl.y, -11.666666); @@ -173,7 +173,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{0, 0, 0, 0}}, {{0, 0}}, 24.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -64.4444427); EXPECT_FLOAT_EQ(quad.tl.y, -12.2222214); @@ -189,7 +189,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{0, 0, 0, 0}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -32.2222214); EXPECT_FLOAT_EQ(quad.tl.y, -6.11111069); @@ -205,7 +205,7 @@ TEST(getIconQuads, style) { { auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{5, 10, 5, 10}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -43.3333321); EXPECT_FLOAT_EQ(quad.tl.y, -11.666666); @@ -223,7 +223,7 @@ TEST(getIconQuads, style) { layout.get<TextSize>() = 12.0f; auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{0, 5, 10, 15}}, {{0, 0}}, 12.0f / 24.0f); - SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal, SymbolContent::IconRGBA); EXPECT_FLOAT_EQ(quad.tl.x, -48.3333321); EXPECT_FLOAT_EQ(quad.tl.y, -6.66666603); |