Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Simplify and fix sprite atlas coordinate calculations - #9038

Merged
jfirebaugh merged 4 commits into
masterfrom
sprite-atlas-coordinates
May 23, 2017
Merged

jfirebaugh merged 4 commits into
masterfrom
sprite-atlas-coordinates

Conversation

@jfirebaugh

@jfirebaugh jfirebaugh commented May 18, 2017 •

Copy link
Copy Markdown
Contributor
  • Always return image metrics exclusive of padding
  • Work with integer coordinates whenever possible
  • Eliminate redundant SpriteAtlasElement members
  • Fix asymmetric re-padding in getIconQuad when pixelRatio != 1 (fixes Investigate pixelated symbols in labels #6863)
  • Add explanatory comments

@jfirebaugh
jfirebaugh requested a review from ansis May 18, 2017 20:50
@jfirebaugh
jfirebaugh force-pushed the sprite-atlas-coordinates branch from 0cb8483 to 008f292 Compare May 18, 2017 22:11
@kkaefer kkaefer added the Core The cross-platform C++ core, aka mbgl label May 19, 2017
@ansis

ansis commented May 19, 2017

Copy link
Copy Markdown
Contributor

Does this have any affect on icons with a non-integer size (#3561)? I'm not seeing anything that would, but I thought I'd ask since I've missed things like this before.

This looks good, but I'm a bit rusty on all the SpriteAtlas edge cases. How deep of a review do you want on this?

@jfirebaugh

Copy link
Copy Markdown
Contributor Author

I'll add an integration test for non-integer pixel ratio and see.

The most helpful thing would be if you could look at the quads.cpp changes and confirm the comment there. The metrics that the SpriteAtlas provides are now exclusive of padding, and it seems like the quad calculations should also be exclusive of padding, creating a quad with the same intrinsic size as the original image, whose vertex attributes contain the texture coordinates of the image exclusive of padding. That would be much simpler, but cause changes to rendering results. Is there a subtlety about the the current behavior I'm missing?

@ansis

ansis commented May 19, 2017

Copy link
Copy Markdown
Contributor

The metrics that the SpriteAtlas provides are now exclusive of padding, and it seems like the quad calculations should also be exclusive of padding, creating a quad with the same intrinsic size as the original image, whose vertex attributes contain the texture coordinates of the image exclusive of padding. That would be much simpler, but cause changes to rendering results. Is there a subtlety about the the current behavior I'm missing?

If you have a 10px icon that isn't perfectly aligned to the pixel grid it will cover 11 actual pixels. The quad needs to be padded to account for this, otherwise they'll look slightly clipped on one edge in some cases.

@ansis

ansis commented May 19, 2017

Copy link
Copy Markdown
Contributor

So, the quad needs to be padded and the texture coordinates added to the buffers need to be padded. It's fine if SpriteAtlas returns exact coordinates as long as the padding happens elsewhere.

Since this has been very unclear and the padding is split across a bunch of different places I wouldn't be surprised if there was an off by one in terms of centering somewhere.

Comment thread src/mbgl/text/quads.cpp Outdated
float left = shapedIcon.left() - border;
float right = left + image.size[0] + 2 * border * pixelRatio / image.pixelRatio;
float top = shapedIcon.top() - border;
float bottom = top + image.size[1] + 2 * border * pixelRatio / image.pixelRatio;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some border-based adjustments are needed (#9038 (comment)) but multiplying border by the pixel ratio on one side and not on the other looks suspicious.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this behavior exists in -native and -js before this pull request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is something wrong here, it could be the cause of #6863

@jfirebaugh jfirebaugh May 19, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's how I would simplify this, based on the above:

diff --git a/src/mbgl/sprite/sprite_atlas.cpp b/src/mbgl/sprite/sprite_atlas.cpp
index c29b91797..e3df6864c 100644
--- a/src/mbgl/sprite/sprite_atlas.cpp
+++ b/src/mbgl/sprite/sprite_atlas.cpp
@@ -36,7 +36,7 @@ SpriteAtlasElement::SpriteAtlasElement(Rect<uint16_t> rect,
 SpriteAtlas::SpriteAtlas(Size size, float pixelRatio)
     : pixelSize(std::ceil(size.width * pixelRatio),
                 std::ceil(size.height * pixelRatio)),
-      padding(pixelRatio),
+      padding(1),
       bin(pixelSize.width, pixelSize.height),
       dirty(true) {
 }
diff --git a/src/mbgl/text/quads.cpp b/src/mbgl/text/quads.cpp
index 26ee237d6..2a6e2d772 100644
--- a/src/mbgl/text/quads.cpp
+++ b/src/mbgl/text/quads.cpp
@@ -22,21 +22,18 @@ SymbolQuad getIconQuad(const Anchor& anchor,
                        const float layoutTextSize,
                        const style::SymbolPlacementType placement, 
                        const Shaping& shapedText,
-                       const float pixelRatio) {
+                       const float) {
     const SpriteAtlasElement& image = shapedIcon.image();
 
     // If you have a 10px icon that isn't perfectly aligned to the pixel grid it will cover 11 actual
     // pixels. The quad needs to be padded to account for this, otherwise they'll look slightly clipped
     // on one edge in some cases.
-    //
-    // TODO: These calculations are more complex than they probably need to be, and may conceal off-by-one
-    // errors.
     const float border = 1.0;
 
-    float left = shapedIcon.left() - border;
-    float right = left + image.size[0] + 2 * border * pixelRatio / image.pixelRatio;
+    float left = shapedIcon.left() - border / image.pixelRatio;
+    float right = left + image.size[0] + 2 * border / image.pixelRatio;
     float top = shapedIcon.top() - border;
-    float bottom = top + image.size[1] + 2 * border * pixelRatio / image.pixelRatio;
+    float bottom = top + image.size[1] + 2 * border / image.pixelRatio;
     Point<float> tl;
     Point<float> tr;
     Point<float> br;
@@ -101,10 +98,10 @@ SymbolQuad getIconQuad(const Anchor& anchor,
     }
 
     Rect<uint16_t> textureRect {
-        static_cast<uint16_t>(image.tl[0] - border * pixelRatio),
-        static_cast<uint16_t>(image.tl[1] - border * pixelRatio),
-        static_cast<uint16_t>(image.br[0] - image.tl[0] + 2 * border * pixelRatio),
-        static_cast<uint16_t>(image.br[1] - image.tl[1] + 2 * border * pixelRatio)
+        static_cast<uint16_t>(image.tl[0] - border),
+        static_cast<uint16_t>(image.tl[1] - border),
+        static_cast<uint16_t>(image.br[0] - image.tl[0] + 2 * border),
+        static_cast<uint16_t>(image.br[1] - image.tl[1] + 2 * border)
     };
 
     return SymbolQuad { tl, tr, bl, br, textureRect, 0, 0, anchor.point, globalMinScale, std::numeric_limits<float>::infinity(), shapedText.writingMode };

Here are the rendering results affected by that change: https://fiddle.jshell.net/0n15e6nu/show/light/.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure now that this is indeed the cause of #6863, because the change only affects non-1x renders, and #6863 has been reported only on 2x renders.

@jfirebaugh
jfirebaugh force-pushed the sprite-atlas-coordinates branch from 008f292 to 20052fc Compare May 19, 2017 19:25
jfirebaugh added a commit to mapbox/mapbox-gl-js that referenced this pull request May 20, 2017
jfirebaugh added a commit to mapbox/mapbox-gl-js that referenced this pull request May 22, 2017
Lack of icon is already checked at the call site of shapeIcon; no need to check hasArea() here.
This makes pattern usage more like icons, and will be necessary for data-driven *-pattern properties.
@jfirebaugh
jfirebaugh force-pushed the sprite-atlas-coordinates branch from 0ee2a18 to 85108e4 Compare May 22, 2017 18:54
@jfirebaugh jfirebaugh changed the title Always use integer pixel coordinates when working with SpriteAtlas Simplify and fix sprite atlas coordinate calculations May 22, 2017
@jfirebaugh
jfirebaugh force-pushed the sprite-atlas-coordinates branch from 85108e4 to 67d5254 Compare May 22, 2017 20:12
@jfirebaugh
jfirebaugh requested a review from anandthakker May 23, 2017 16:32

SpriteAtlas& atlas = *bucket.spriteAtlas;
const bool iconScaled = layout.get<IconSize>().constantOr(1.0) != 1.0 ||
frame.pixelRatio != atlas.getPixelRatio() ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition now being covered here?

@jfirebaugh jfirebaugh May 23, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this condition was redundant and misleading. SpriteAtlas itself does not have any intrinsic pixel ratio; it has a collection of images of possibly heterogeneous pixel ratios.

* Always return image metrics exclusive of padding
* Work with integer coordinates whenever possible
* Eliminate redundant SpriteAtlasElement members
* Fix asymmetric re-padding in getIconQuad when pixelRatio != 1
* Add explanatory comments
@jfirebaugh
jfirebaugh force-pushed the sprite-atlas-coordinates branch from 67d5254 to f7cd879 Compare May 23, 2017 19:29
@jfirebaugh
jfirebaugh merged commit 1759603 into master May 23, 2017
@jfirebaugh
jfirebaugh deleted the sprite-atlas-coordinates branch May 23, 2017 19:59
@jfirebaugh jfirebaugh mentioned this pull request May 25, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Core The cross-platform C++ core, aka mbgl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate pixelated symbols in labels

4 participants