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

[core] Transform{,State} cleanups #6790

Merged
merged 5 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ set(MBGL_CORE_FILES

# math
include/mbgl/math/clamp.hpp
include/mbgl/math/log2.hpp
include/mbgl/math/minmax.hpp
include/mbgl/math/wrap.hpp
src/mbgl/math/log2.cpp

# mbgl
include/mbgl/mbgl.hpp
Expand Down Expand Up @@ -481,7 +483,6 @@ set(MBGL_CORE_FILES
src/mbgl/util/mat3.hpp
src/mbgl/util/mat4.cpp
src/mbgl/util/mat4.hpp
src/mbgl/util/math.cpp
src/mbgl/util/math.hpp
src/mbgl/util/offscreen_texture.cpp
src/mbgl/util/offscreen_texture.hpp
Expand Down
27 changes: 27 additions & 0 deletions include/mbgl/math/log2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <cmath>
#include <cstdint>
#include <type_traits>

namespace mbgl {
namespace util {

// Computes the log2(x) rounded up to the next integer.
// (== number of bits required to store x)
uint32_t ceil_log2(uint64_t x);

template <typename T>
typename std::enable_if_t<std::is_floating_point<T>::value, T> log2(T x)
{
// log2() is producing wrong results on ARMv5 binaries
// running on ARMv7+ CPUs.
#if defined(__ANDROID__)
return std::log(x) / M_LN2;
#else
return std::log2(x);
#endif
}

} // namespace util
} // namespace mbgl
2 changes: 2 additions & 0 deletions include/mbgl/util/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ constexpr double DEGREES_MAX = 360;
constexpr double PITCH_MAX = M_PI / 3;
constexpr double MIN_ZOOM = 0.0;
constexpr double MAX_ZOOM = 25.5;
constexpr float MIN_ZOOM_F = MIN_ZOOM;
constexpr float MAX_ZOOM_F = MAX_ZOOM;

constexpr uint64_t DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024;

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/algorithm/generate_clip_ids_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <mbgl/algorithm/generate_clip_ids.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/platform/log.hpp>

namespace mbgl {
Expand Down
34 changes: 14 additions & 20 deletions src/mbgl/layout/symbol_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#include <mbgl/util/constants.hpp>
#include <mbgl/util/utf.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/math/clamp.hpp>
#include <mbgl/math/minmax.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/log.hpp>

Expand Down Expand Up @@ -418,8 +418,8 @@ 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) {

const float placementZoom = ::fmax(std::log(scale) / std::log(2) + zoom, 0);
constexpr const uint16_t vertexLength = 4;
const float placementZoom = util::max(util::log2(scale) + zoom, 0.0f);

for (const auto& symbol : symbols) {
const auto &tl = symbol.tl;
Expand All @@ -428,9 +428,8 @@ void SymbolLayout::addSymbols(Buffer &buffer, const SymbolQuads &symbols, float
const auto &br = symbol.br;
const auto &tex = symbol.tex;

float minZoom =
util::max(static_cast<float>(zoom + log(symbol.minScale) / log(2)), placementZoom);
float maxZoom = util::min(static_cast<float>(zoom + log(symbol.maxScale) / log(2)), 25.0f);
float minZoom = util::max(zoom + util::log2(symbol.minScale), placementZoom);
float maxZoom = util::min(zoom + util::log2(symbol.maxScale), util::MAX_ZOOM_F);
const auto &anchorPoint = symbol.anchorPoint;

// drop upside down versions of glyphs
Expand All @@ -449,16 +448,15 @@ 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;
assert(segment.vertexLength <= std::numeric_limits<uint16_t>::max());
uint16_t index = segment.vertexLength;

// Encode angle of glyph
uint8_t glyphAngle = std::round((symbol.glyphAngle / (M_PI * 2)) * 256);
Expand All @@ -474,14 +472,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 Expand Up @@ -510,8 +504,8 @@ void SymbolLayout::addToDebugBuffers(CollisionTile& collisionTile, SymbolBucket&
bl = util::matrixMultiply(collisionTile.reverseRotationMatrix, bl);
br = util::matrixMultiply(collisionTile.reverseRotationMatrix, br);

const float maxZoom = util::clamp(zoom + log(box.maxScale) / log(2), util::MIN_ZOOM, util::MAX_ZOOM);
const float placementZoom = util::clamp(zoom + log(box.placementScale) / log(2), util::MIN_ZOOM, util::MAX_ZOOM);
const float maxZoom = util::clamp(zoom + util::log2(box.maxScale), util::MIN_ZOOM_F, util::MAX_ZOOM_F);
const float placementZoom = util::clamp(zoom + util::log2(box.placementScale), util::MIN_ZOOM_F, util::MAX_ZOOM_F);

collisionBox.vertices.emplace_back(anchor.x, anchor.y, tl.x, tl.y, maxZoom, placementZoom);
collisionBox.vertices.emplace_back(anchor.x, anchor.y, tr.x, tr.y, maxZoom, placementZoom);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <mbgl/util/tile_coordinate.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/math/log2.hpp>

namespace mbgl {

Expand Down Expand Up @@ -408,7 +409,6 @@ void Map::cancelTransitions() {

void Map::setGestureInProgress(bool inProgress) {
impl->transform.setGestureInProgress(inProgress);
impl->onUpdate(Update::Repaint);
}

bool Map::isGestureInProgress() const {
Expand Down
10 changes: 3 additions & 7 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/math/clamp.hpp>

namespace mbgl {
Expand Down Expand Up @@ -117,11 +117,11 @@ double TransformState::pixel_y() const {
#pragma mark - Zoom

double TransformState::getZoom() const {
return std::log(scale) / M_LN2;
return scaleZoom(scale);
}

int32_t TransformState::getIntegerZoom() const {
return std::floor(getZoom());
return getZoom();
}

double TransformState::getZoomFraction() const {
Expand Down Expand Up @@ -175,10 +175,6 @@ float TransformState::getPitch() const {

#pragma mark - State

bool TransformState::isChanging() const {
return rotating || scaling || panning || gestureInProgress;
}

bool TransformState::isRotating() const {
return rotating;
}
Expand Down
1 change: 0 additions & 1 deletion src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class TransformState {
float getPitch() const;

// State
bool isChanging() const;
bool isRotating() const;
bool isScaling() const;
bool isPanning() const;
Expand Down
12 changes: 1 addition & 11 deletions src/mbgl/util/math.cpp → src/mbgl/math/log2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <mbgl/util/math.hpp>
#include <mbgl/math/log2.hpp>

namespace mbgl {
namespace util {
Expand All @@ -21,15 +21,5 @@ uint32_t ceil_log2(uint64_t x) {
return y;
}

double log2(double x) {
// log2() is producing wrong results on ARMv5 binaries
// running on ARMv7+ CPUs.
#if defined(__ANDROID__)
return std::log(x) / 0.6931471805599453; // log(x) / log(2)
#else
return ::log2(x);
#endif
}

} // namespace util
} // namespace mbgl
17 changes: 8 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) {
constexpr 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 @@ -62,18 +64,15 @@ void CircleBucket::addGeometry(const GeometryCollection& geometryCollection) {
vertices.emplace_back(x, y, -1, 1); // 4

auto& segment = segments.back();
assert(segment.vertexLength <= std::numeric_limits<uint16_t>::max());
uint16_t index = segment.vertexLength;

// 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
20 changes: 10 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,20 @@ 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();
assert(lineSegment.vertexLength <= std::numeric_limits<uint16_t>::max());
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 +76,18 @@ 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();
assert(triangleSegment.vertexLength <= std::numeric_limits<uint16_t>::max());
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
8 changes: 4 additions & 4 deletions src/mbgl/renderer/line_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,17 @@ 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();
assert(segment.vertexLength <= std::numeric_limits<uint16_t>::max());
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
Loading