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

Commit

Permalink
[core] Use numeric_limits<>::max() for checking element groups
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Oct 30, 2016
1 parent 3e6194b commit ff9f2fc
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 42 deletions.
19 changes: 7 additions & 12 deletions src/mbgl/layout/symbol_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ std::unique_ptr<SymbolBucket> SymbolLayout::place(CollisionTile& collisionTile)

template <typename Buffer>
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) {
Expand Down Expand Up @@ -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<uint16_t>::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);
Expand All @@ -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<uint16_t>(index + 0),
static_cast<uint16_t>(index + 1),
static_cast<uint16_t>(index + 2));
buffer.triangles.emplace_back(static_cast<uint16_t>(index + 1),
static_cast<uint16_t>(index + 2),
static_cast<uint16_t>(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;
}
}
Expand Down
16 changes: 7 additions & 9 deletions src/mbgl/renderer/circle_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<uint16_t>::max()) {
// Move to a new segments because the old one can't hold the geometry.
segments.emplace_back(vertices.size(), triangles.size());
}
Expand All @@ -66,14 +68,10 @@ void CircleBucket::addGeometry(const GeometryCollection& geometryCollection) {

// 1, 2, 3
// 1, 4, 3
triangles.emplace_back(index,
static_cast<uint16_t>(index + 1),
static_cast<uint16_t>(index + 2));
triangles.emplace_back(index,
static_cast<uint16_t>(index + 3),
static_cast<uint16_t>(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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/circle_bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CircleBucket : public Bucket {

std::vector<CircleVertex> vertices;
std::vector<gl::Triangle> triangles;
std::vector<gl::Segment> segments;
std::vector<gl::Segment> segments { { 0, 0 } };

optional<gl::VertexBuffer<CircleVertex>> vertexBuffer;
optional<gl::IndexBuffer<gl::Triangle>> indexBuffer;
Expand Down
18 changes: 8 additions & 10 deletions src/mbgl/renderer/fill_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>::max())
throw GeometryTooLongException();
}

Expand All @@ -51,21 +51,19 @@ 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<uint16_t>::max()) {
lineSegments.emplace_back(vertices.size(), lines.size());
}

auto& lineSegment = lineSegments.back();
uint16_t lineIndex = lineSegment.vertexLength;

vertices.emplace_back(ring[0].x, ring[0].y);
lines.emplace_back(static_cast<uint16_t>(lineIndex + nVertices - 1),
static_cast<uint16_t>(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<uint16_t>(lineIndex + i - 1),
static_cast<uint16_t>(lineIndex + i));
lines.emplace_back(lineIndex + i - 1, lineIndex + i);
}

lineSegment.vertexLength += nVertices;
Expand All @@ -77,17 +75,17 @@ 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<uint16_t>::max()) {
triangleSegments.emplace_back(startVertices, triangles.size());
}

auto& triangleSegment = triangleSegments.back();
uint16_t triangleIndex = triangleSegment.vertexLength;

for (uint32_t i = 0; i < nIndicies; i += 3) {
triangles.emplace_back(static_cast<uint16_t>(triangleIndex + indices[i]),
static_cast<uint16_t>(triangleIndex + indices[i + 1]),
static_cast<uint16_t>(triangleIndex + indices[i + 2]));
triangles.emplace_back(triangleIndex + indices[i],
triangleIndex + indices[i + 1],
triangleIndex + indices[i + 2]);
}

triangleSegment.vertexLength += totalVertices;
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/renderer/fill_bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FillBucket : public Bucket {
std::vector<FillVertex> vertices;
std::vector<gl::Line> lines;
std::vector<gl::Triangle> triangles;
std::vector<gl::Segment> lineSegments;
std::vector<gl::Segment> triangleSegments;
std::vector<gl::Segment> lineSegments { { 0, 0 } };
std::vector<gl::Segment> triangleSegments { { 0, 0 } };

optional<gl::VertexBuffer<FillVertex>> vertexBuffer;
optional<gl::IndexBuffer<gl::Line>> lineIndexBuffer;
Expand Down
7 changes: 3 additions & 4 deletions src/mbgl/renderer/line_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,16 @@ 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<uint16_t>::max()) {
// Move to a new group because the old one can't hold the geometry.
segments.emplace_back(startVertex, triangles.size());
}

auto& segment = segments.back();
uint16_t index = segment.vertexLength;

for (const auto& triangle : triangleStore) {
triangles.emplace_back(static_cast<uint16_t>(index + triangle.a),
static_cast<uint16_t>(index + triangle.b),
static_cast<uint16_t>(index + triangle.c));
triangles.emplace_back(index + triangle.a, index + triangle.b, index + triangle.c);
}

segment.vertexLength += vertexCount;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/line_bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LineBucket : public Bucket {

std::vector<LineVertex> vertices;
std::vector<gl::Triangle> triangles;
std::vector<gl::Segment> segments;
std::vector<gl::Segment> segments { { 0, 0 } };

optional<gl::VertexBuffer<LineVertex>> vertexBuffer;
optional<gl::IndexBuffer<gl::Triangle>> indexBuffer;
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/renderer/symbol_bucket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SymbolBucket : public Bucket {
struct TextBuffer {
std::vector<SymbolVertex> vertices;
std::vector<gl::Triangle> triangles;
std::vector<gl::Segment> segments;
std::vector<gl::Segment> segments { { 0, 0 } };

optional<gl::VertexBuffer<SymbolVertex>> vertexBuffer;
optional<gl::IndexBuffer<gl::Triangle>> indexBuffer;
Expand All @@ -45,7 +45,7 @@ class SymbolBucket : public Bucket {
struct IconBuffer {
std::vector<SymbolVertex> vertices;
std::vector<gl::Triangle> triangles;
std::vector<gl::Segment> segments;
std::vector<gl::Segment> segments { { 0, 0 } };

optional<gl::VertexBuffer<SymbolVertex>> vertexBuffer;
optional<gl::IndexBuffer<gl::Triangle>> indexBuffer;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/sprite/sprite_atlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SpriteAtlas : public util::noncopyable {
void _setSprite(const std::string&, const std::shared_ptr<const SpriteImage>& = nullptr);
void emitSpriteLoadedIfComplete();

const uint16_t width, height;
const dimension width, height;
const dimension pixelWidth, pixelHeight;
const float pixelRatio;

Expand Down

0 comments on commit ff9f2fc

Please sign in to comment.