From 2c49c4374cb9e3654c1d347b1071542918ce5fd2 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Thu, 14 Nov 2019 15:23:15 +0200 Subject: [core] Store 'sdf' flag in vertex attribute --- src/mbgl/layout/symbol_instance.cpp | 6 ++++-- src/mbgl/layout/symbol_instance.hpp | 10 +++------- src/mbgl/layout/symbol_layout.cpp | 13 +++++++++---- src/mbgl/programs/symbol_program.hpp | 36 +++++++++++++++++------------------- src/mbgl/text/quads.cpp | 17 ++++++++++------- src/mbgl/text/quads.hpp | 7 +++++-- 6 files changed, 48 insertions(+), 41 deletions(-) (limited to 'src') 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& 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& textOffset, const ImageMap& imageMap, + SymbolContent iconType, bool allowVerticalPlacement); bool empty() const; GeometryCoordinates line; @@ -42,13 +45,6 @@ struct SymbolInstanceSharedData { optional 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 #include - #include #include 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 layoutVertex(Point labelAnchor, - Point o, - float glyphOffsetY, - uint16_t tx, - uint16_t ty, - const Range& sizeData) { + Point o, + float glyphOffsetY, + uint16_t tx, + uint16_t ty, + const Range& sizeData, + bool isSDF) { + const uint16_t aSizeMin = (std::min(MAX_PACKED_SIZE, static_cast(sizeData.min * SIZE_PACK_FACTOR)) << 1) + uint16_t(isSDF); + const uint16_t aSizeMax = std::min(MAX_PACKED_SIZE, static_cast(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(labelAnchor.x), - static_cast(labelAnchor.y), - static_cast(::round(o.x * 32)), // use 1/32 pixels for placement - static_cast(::round((o.y + glyphOffsetY) * 32)) - }}, - {{ - tx, - ty, - static_cast(sizeData.min * 256), - static_cast(sizeData.max * 256) - }} - }; + {{static_cast(labelAnchor.x), + static_cast(labelAnchor.y), + static_cast(::round(o.x * 32)), // use 1/32 pixels for placement + static_cast(::round((o.y + glyphOffsetY) * 32))}}, + {{tx, ty, aSizeMin, aSizeMax}}}; } static gfx::Vertex dynamicLayoutVertex(Point 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 +#include +#include #include #include #include -#include -#include -#include #include +#include #include #include @@ -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(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& 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 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 tex_, WritingModeType writingMode_, Point 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 tl; @@ -39,13 +42,13 @@ public: Rect tex; WritingModeType writingMode; Point glyphOffset; + bool isSDF; size_t sectionIndex; }; using SymbolQuads = std::vector; -SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, - WritingModeType writingMode); +SymbolQuad getIconQuad(const PositionedIcon& shapedIcon, WritingModeType writingMode, SymbolContent iconType); SymbolQuads getGlyphQuads(const Shaping& shapedText, const std::array textOffset, -- cgit v1.2.1