From ff9f2fc513f323fa0cc9d1aa7bc2eb49724b9454 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Sat, 29 Oct 2016 18:59:07 +0300 Subject: [PATCH] [core] Use numeric_limits<>::max() for checking element groups --- src/mbgl/layout/symbol_layout.cpp | 19 +++++++------------ src/mbgl/renderer/circle_bucket.cpp | 16 +++++++--------- src/mbgl/renderer/circle_bucket.hpp | 2 +- src/mbgl/renderer/fill_bucket.cpp | 18 ++++++++---------- src/mbgl/renderer/fill_bucket.hpp | 4 ++-- src/mbgl/renderer/line_bucket.cpp | 7 +++---- src/mbgl/renderer/line_bucket.hpp | 2 +- src/mbgl/renderer/symbol_bucket.hpp | 4 ++-- src/mbgl/sprite/sprite_atlas.hpp | 2 +- 9 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/mbgl/layout/symbol_layout.cpp b/src/mbgl/layout/symbol_layout.cpp index 9af6f7ed0b4..e02661047e9 100644 --- a/src/mbgl/layout/symbol_layout.cpp +++ b/src/mbgl/layout/symbol_layout.cpp @@ -418,6 +418,7 @@ std::unique_ptr SymbolLayout::place(CollisionTile& collisionTile) template void SymbolLayout::addSymbols(Buffer &buffer, const SymbolQuads &symbols, float scale, const bool keepUpright, const style::SymbolPlacementType placement, const float placementAngle) { + static const uint16_t vertexLength = 4; const float placementZoom = util::max(std::log2(scale) + zoom, 0.0f); for (const auto& symbol : symbols) { @@ -447,16 +448,14 @@ void SymbolLayout::addSymbols(Buffer &buffer, const SymbolQuads &symbols, float minZoom = 0; } - const int glyph_vertex_length = 4; - - if (buffer.segments.empty() || buffer.segments.back().vertexLength + glyph_vertex_length > 65535) { + if (buffer.segments.back().vertexLength + vertexLength > std::numeric_limits::max()) { buffer.segments.emplace_back(buffer.vertices.size(), buffer.triangles.size()); } // We're generating triangle fans, so we always start with the first // coordinate in this polygon. auto& segment = buffer.segments.back(); - size_t index = segment.vertexLength; + uint16_t index = uint16_t(segment.vertexLength); // Encode angle of glyph uint8_t glyphAngle = std::round((symbol.glyphAngle / (M_PI * 2)) * 256); @@ -472,14 +471,10 @@ void SymbolLayout::addSymbols(Buffer &buffer, const SymbolQuads &symbols, float minZoom, maxZoom, placementZoom, glyphAngle); // add the two triangles, referencing the four coordinates we just inserted. - buffer.triangles.emplace_back(static_cast(index + 0), - static_cast(index + 1), - static_cast(index + 2)); - buffer.triangles.emplace_back(static_cast(index + 1), - static_cast(index + 2), - static_cast(index + 3)); - - segment.vertexLength += glyph_vertex_length; + buffer.triangles.emplace_back(index + 0, index + 1, index + 2); + buffer.triangles.emplace_back(index + 1, index + 2, index + 3); + + segment.vertexLength += vertexLength; segment.primitiveLength += 2; } } diff --git a/src/mbgl/renderer/circle_bucket.cpp b/src/mbgl/renderer/circle_bucket.cpp index cb6ba7d458f..c2ecb07cfbd 100644 --- a/src/mbgl/renderer/circle_bucket.cpp +++ b/src/mbgl/renderer/circle_bucket.cpp @@ -31,6 +31,8 @@ bool CircleBucket::hasData() const { } void CircleBucket::addGeometry(const GeometryCollection& geometryCollection) { + static const uint16_t vertexLength = 4; + for (auto& circle : geometryCollection) { for(auto & geometry : circle) { auto x = geometry.x; @@ -42,7 +44,7 @@ void CircleBucket::addGeometry(const GeometryCollection& geometryCollection) { if ((mode != MapMode::Still) && (x < 0 || x >= util::EXTENT || y < 0 || y >= util::EXTENT)) continue; - if (!segments.size() || segments.back().vertexLength + 4 > 65535) { + if (segments.back().vertexLength + vertexLength > std::numeric_limits::max()) { // Move to a new segments because the old one can't hold the geometry. segments.emplace_back(vertices.size(), triangles.size()); } @@ -66,14 +68,10 @@ void CircleBucket::addGeometry(const GeometryCollection& geometryCollection) { // 1, 2, 3 // 1, 4, 3 - triangles.emplace_back(index, - static_cast(index + 1), - static_cast(index + 2)); - triangles.emplace_back(index, - static_cast(index + 3), - static_cast(index + 2)); - - segment.vertexLength += 4; + triangles.emplace_back(index, index + 1, index + 2); + triangles.emplace_back(index, index + 3, index + 2); + + segment.vertexLength += vertexLength; segment.primitiveLength += 2; } } diff --git a/src/mbgl/renderer/circle_bucket.hpp b/src/mbgl/renderer/circle_bucket.hpp index a682d423d6a..494e64cc0f2 100644 --- a/src/mbgl/renderer/circle_bucket.hpp +++ b/src/mbgl/renderer/circle_bucket.hpp @@ -22,7 +22,7 @@ class CircleBucket : public Bucket { std::vector vertices; std::vector triangles; - std::vector segments; + std::vector segments { { 0, 0 } }; optional> vertexBuffer; optional> indexBuffer; diff --git a/src/mbgl/renderer/fill_bucket.cpp b/src/mbgl/renderer/fill_bucket.cpp index 2c69524c0de..e1012f335e6 100644 --- a/src/mbgl/renderer/fill_bucket.cpp +++ b/src/mbgl/renderer/fill_bucket.cpp @@ -39,7 +39,7 @@ void FillBucket::addGeometry(const GeometryCollection& geometry) { for (const auto& ring : polygon) { totalVertices += ring.size(); - if (totalVertices > 65535) + if (totalVertices > std::numeric_limits::max()) throw GeometryTooLongException(); } @@ -51,7 +51,7 @@ void FillBucket::addGeometry(const GeometryCollection& geometry) { if (nVertices == 0) continue; - if (lineSegments.empty() || lineSegments.back().vertexLength + nVertices > 65535) { + if (lineSegments.back().vertexLength + nVertices > std::numeric_limits::max()) { lineSegments.emplace_back(vertices.size(), lines.size()); } @@ -59,13 +59,11 @@ void FillBucket::addGeometry(const GeometryCollection& geometry) { uint16_t lineIndex = lineSegment.vertexLength; vertices.emplace_back(ring[0].x, ring[0].y); - lines.emplace_back(static_cast(lineIndex + nVertices - 1), - static_cast(lineIndex)); + lines.emplace_back(lineIndex + nVertices - 1, lineIndex); for (uint32_t i = 1; i < nVertices; i++) { vertices.emplace_back(ring[i].x, ring[i].y); - lines.emplace_back(static_cast(lineIndex + i - 1), - static_cast(lineIndex + i)); + lines.emplace_back(lineIndex + i - 1, lineIndex + i); } lineSegment.vertexLength += nVertices; @@ -77,7 +75,7 @@ void FillBucket::addGeometry(const GeometryCollection& geometry) { std::size_t nIndicies = indices.size(); assert(nIndicies % 3 == 0); - if (triangleSegments.empty() || triangleSegments.back().vertexLength + totalVertices > 65535) { + if (triangleSegments.back().vertexLength + totalVertices > std::numeric_limits::max()) { triangleSegments.emplace_back(startVertices, triangles.size()); } @@ -85,9 +83,9 @@ void FillBucket::addGeometry(const GeometryCollection& geometry) { uint16_t triangleIndex = triangleSegment.vertexLength; for (uint32_t i = 0; i < nIndicies; i += 3) { - triangles.emplace_back(static_cast(triangleIndex + indices[i]), - static_cast(triangleIndex + indices[i + 1]), - static_cast(triangleIndex + indices[i + 2])); + triangles.emplace_back(triangleIndex + indices[i], + triangleIndex + indices[i + 1], + triangleIndex + indices[i + 2]); } triangleSegment.vertexLength += totalVertices; diff --git a/src/mbgl/renderer/fill_bucket.hpp b/src/mbgl/renderer/fill_bucket.hpp index 715b8e27452..58fa916f82d 100644 --- a/src/mbgl/renderer/fill_bucket.hpp +++ b/src/mbgl/renderer/fill_bucket.hpp @@ -22,8 +22,8 @@ class FillBucket : public Bucket { std::vector vertices; std::vector lines; std::vector triangles; - std::vector lineSegments; - std::vector triangleSegments; + std::vector lineSegments { { 0, 0 } }; + std::vector triangleSegments { { 0, 0 } }; optional> vertexBuffer; optional> lineIndexBuffer; diff --git a/src/mbgl/renderer/line_bucket.cpp b/src/mbgl/renderer/line_bucket.cpp index 229b14d4893..6ebddb85c57 100644 --- a/src/mbgl/renderer/line_bucket.cpp +++ b/src/mbgl/renderer/line_bucket.cpp @@ -348,7 +348,8 @@ void LineBucket::addGeometry(const GeometryCoordinates& coordinates) { const std::size_t endVertex = vertices.size(); const std::size_t vertexCount = endVertex - startVertex; - if (segments.empty() || segments.back().vertexLength + vertexCount > 65535) { + if (segments.back().vertexLength + vertexCount > std::numeric_limits::max()) { + // Move to a new group because the old one can't hold the geometry. segments.emplace_back(startVertex, triangles.size()); } @@ -356,9 +357,7 @@ void LineBucket::addGeometry(const GeometryCoordinates& coordinates) { uint16_t index = segment.vertexLength; for (const auto& triangle : triangleStore) { - triangles.emplace_back(static_cast(index + triangle.a), - static_cast(index + triangle.b), - static_cast(index + triangle.c)); + triangles.emplace_back(index + triangle.a, index + triangle.b, index + triangle.c); } segment.vertexLength += vertexCount; diff --git a/src/mbgl/renderer/line_bucket.hpp b/src/mbgl/renderer/line_bucket.hpp index 7b4c5f1b571..3ee142a70bf 100644 --- a/src/mbgl/renderer/line_bucket.hpp +++ b/src/mbgl/renderer/line_bucket.hpp @@ -28,7 +28,7 @@ class LineBucket : public Bucket { std::vector vertices; std::vector triangles; - std::vector segments; + std::vector segments { { 0, 0 } }; optional> vertexBuffer; optional> indexBuffer; diff --git a/src/mbgl/renderer/symbol_bucket.hpp b/src/mbgl/renderer/symbol_bucket.hpp index a905e253633..881475e4a9b 100644 --- a/src/mbgl/renderer/symbol_bucket.hpp +++ b/src/mbgl/renderer/symbol_bucket.hpp @@ -36,7 +36,7 @@ class SymbolBucket : public Bucket { struct TextBuffer { std::vector vertices; std::vector triangles; - std::vector segments; + std::vector segments { { 0, 0 } }; optional> vertexBuffer; optional> indexBuffer; @@ -45,7 +45,7 @@ class SymbolBucket : public Bucket { struct IconBuffer { std::vector vertices; std::vector triangles; - std::vector segments; + std::vector segments { { 0, 0 } }; optional> vertexBuffer; optional> indexBuffer; diff --git a/src/mbgl/sprite/sprite_atlas.hpp b/src/mbgl/sprite/sprite_atlas.hpp index f4ed1b306ea..3e0ca515084 100644 --- a/src/mbgl/sprite/sprite_atlas.hpp +++ b/src/mbgl/sprite/sprite_atlas.hpp @@ -106,7 +106,7 @@ class SpriteAtlas : public util::noncopyable { void _setSprite(const std::string&, const std::shared_ptr& = nullptr); void emitSpriteLoadedIfComplete(); - const uint16_t width, height; + const dimension width, height; const dimension pixelWidth, pixelHeight; const float pixelRatio;