Repository navigation
Simplify and fix sprite atlas coordinate calculations - #9038
Conversation
0cb8483 to
008f292
Compare
|
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? |
|
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? |
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. |
|
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. |
| 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It looks like this behavior exists in -native and -js before this pull request.
There was a problem hiding this comment.
if there is something wrong here, it could be the cause of #6863
There was a problem hiding this comment.
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/.
008f292 to
20052fc
Compare
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.
0ee2a18 to
85108e4
Compare
85108e4 to
67d5254
Compare
|
|
||
| SpriteAtlas& atlas = *bucket.spriteAtlas; | ||
| const bool iconScaled = layout.get<IconSize>().constantOr(1.0) != 1.0 || | ||
| frame.pixelRatio != atlas.getPixelRatio() || |
There was a problem hiding this comment.
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
67d5254 to
f7cd879
Compare
Uh oh!
There was an error while loading. Please reload this page.