diff options
author | Molly Lloyd <mollymerp@users.noreply.github.com> | 2018-01-23 10:49:23 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-23 10:49:23 -0800 |
commit | f3294200c6c866e5ab031ad8346c59a76aa37249 (patch) | |
tree | 6d9aedb552552607641a15c415a60be99a0877d5 /src | |
parent | 2d15aed43c9faa875a8f625c3afc286f1175e0ce (diff) | |
download | qtlocation-mapboxgl-f3294200c6c866e5ab031ad8346c59a76aa37249.tar.gz |
[core] add raster-dem source type and hillshade layer type (#10642)
Diffstat (limited to 'src')
50 files changed, 1797 insertions, 21 deletions
diff --git a/src/mbgl/geometry/dem_data.cpp b/src/mbgl/geometry/dem_data.cpp new file mode 100644 index 0000000000..78134dadc1 --- /dev/null +++ b/src/mbgl/geometry/dem_data.cpp @@ -0,0 +1,94 @@ +#include <mbgl/geometry/dem_data.hpp> +#include <mbgl/math/clamp.hpp> + +namespace mbgl { + +DEMData::DEMData(const PremultipliedImage& _image): + dim(_image.size.height), + border(std::max<int32_t>(std::ceil(_image.size.height / 2), 1)), + stride(dim + 2 * border), + image({ static_cast<uint32_t>(stride), static_cast<uint32_t>(stride) }) { + + if (_image.size.height != _image.size.width){ + throw std::runtime_error("raster-dem tiles must be square."); + } + + std::memset(image.data.get(), 0, image.bytes()); + + for (int32_t y = 0; y < dim; y++) { + for (int32_t x = 0; x < dim; x++) { + const int32_t i = y * dim + x; + const int32_t j = i * 4; + set(x, y, (_image.data[j] * 256 * 256 + _image.data[j+1] * 256 + _image.data[j+2])/10 - 10000); + } + } + + // in order to avoid flashing seams between tiles, here we are initially populating a 1px border of + // pixels around the image with the data of the nearest pixel from the image. this data is eventually + // replaced when the tile's neighboring tiles are loaded and the accurate data can be backfilled using + // DEMData#backfillBorder + + for (int32_t x = 0; x < dim; x++) { + // left vertical border + set(-1, x, get(0, x)); + + // right vertical border + set(dim, x, get(dim - 1, x)); + + //left horizontal border + set(x, -1, get(x, 0)); + + // right horizontal border + set(x, dim, get(x, dim - 1)); + } + + // corners + set(-1, -1, get(0, 0)); + set(dim, -1, get(dim - 1, 0)); + set( -1, dim, get(0, dim - 1)); + set(dim, dim, get(dim - 1, dim - 1)); +} + +// This function takes the DEMData from a neighboring tile and backfills the edge/corner +// data in order to create a one pixel "buffer" of image data around the tile. This is +// necessary because the hillshade formula calculates the dx/dz, dy/dz derivatives at each +// pixel of the tile by querying the 8 surrounding pixels, and if we don't have the pixel +// buffer we get seams at tile boundaries. +void DEMData::backfillBorder(const DEMData& borderTileData, int8_t dx, int8_t dy) { + auto& o = borderTileData; + + // Tiles from the same source should always be of the same dimensions. + assert(dim == o.dim); + + // We determine the pixel range to backfill based which corner/edge `borderTileData` + // represents. For example, dx = -1, dy = -1 represents the upper left corner of the + // base tile, so we only need to backfill one pixel at coordinates (-1, -1) of the tile + // image. + int32_t _xMin = dx * dim; + int32_t _xMax = dx * dim + dim; + int32_t _yMin = dy * dim; + int32_t _yMax = dy * dim + dim; + + if (dx == -1) _xMin = _xMax - 1; + else if (dx == 1) _xMax = _xMin + 1; + + if (dy == -1) _yMin = _yMax - 1; + else if (dy == 1) _yMax = _yMin + 1; + + int32_t xMin = util::clamp(_xMin, -border, dim + border); + int32_t xMax = util::clamp(_xMax, -border, dim + border); + + int32_t yMin = util::clamp(_yMin, -border, dim + border); + int32_t yMax = util::clamp(_yMax, -border, dim + border); + + int32_t ox = -dx * dim; + int32_t oy = -dy * dim; + + for (int32_t y = yMin; y < yMax; y++) { + for (int32_t x = xMin; x < xMax; x++) { + set(x, y, o.get(x + ox, y + oy)); + } + } +} + +} // namespace mbgl diff --git a/src/mbgl/geometry/dem_data.hpp b/src/mbgl/geometry/dem_data.hpp new file mode 100644 index 0000000000..507a51661d --- /dev/null +++ b/src/mbgl/geometry/dem_data.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include <mbgl/math/clamp.hpp> +#include <mbgl/util/image.hpp> + +#include <memory> +#include <array> +#include <cassert> +#include <vector> + +namespace mbgl { + +class DEMData { +public: + DEMData(const PremultipliedImage& image); + void backfillBorder(const DEMData& borderTileData, int8_t dx, int8_t dy); + + void set(const int32_t x, const int32_t y, const int32_t value) { + reinterpret_cast<int32_t*>(image.data.get())[idx(x, y)] = value + 65536; + } + + int32_t get(const int32_t x, const int32_t y) const { + return reinterpret_cast<const int32_t*>(image.data.get())[idx(x, y)] - 65536; + } + + const PremultipliedImage* getImage() const { + return ℑ + } + + const int32_t dim; + const int32_t border; + const int32_t stride; + + + private: + PremultipliedImage image; + + size_t idx(const int32_t x, const int32_t y) const { + assert(x >= -border); + assert(x < dim + border); + assert(y >= -border); + assert(y < dim + border); + return (y + border) * stride + (x + border); + } + +}; + +} // namespace mbgl diff --git a/src/mbgl/programs/hillshade_prepare_program.cpp b/src/mbgl/programs/hillshade_prepare_program.cpp new file mode 100644 index 0000000000..0c0446d3f5 --- /dev/null +++ b/src/mbgl/programs/hillshade_prepare_program.cpp @@ -0,0 +1,7 @@ +#include <mbgl/programs/hillshade_prepare_program.hpp> + +namespace mbgl { + +static_assert(sizeof(HillshadePrepareLayoutVertex) == 8, "expected HillshadeLayoutVertex size"); + +} // namespace mbgl diff --git a/src/mbgl/programs/hillshade_prepare_program.hpp b/src/mbgl/programs/hillshade_prepare_program.hpp new file mode 100644 index 0000000000..0f31a22df5 --- /dev/null +++ b/src/mbgl/programs/hillshade_prepare_program.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include <mbgl/programs/program.hpp> +#include <mbgl/programs/attributes.hpp> +#include <mbgl/programs/uniforms.hpp> +#include <mbgl/shaders/hillshade_prepare.hpp> +#include <mbgl/util/geometry.hpp> + +namespace mbgl { + +namespace uniforms { +MBGL_DEFINE_UNIFORM_VECTOR(uint16_t, 2, u_dimension); +} // namespace uniforms + +class HillshadePrepareProgram : public Program< + shaders::hillshade_prepare, + gl::Triangle, + gl::Attributes< + attributes::a_pos, + attributes::a_texture_pos>, + gl::Uniforms< + uniforms::u_matrix, + uniforms::u_dimension, + uniforms::u_zoom, + uniforms::u_image>, + style::Properties<>> { +public: + using Program::Program; + + static LayoutVertex layoutVertex(Point<int16_t> p, Point<uint16_t> t) { + return LayoutVertex { + {{ + p.x, + p.y + }}, + {{ + t.x, + t.y + }} + }; + } +}; + +using HillshadePrepareLayoutVertex = HillshadePrepareProgram::LayoutVertex; +using HillshadePrepareAttributes = HillshadePrepareProgram::Attributes; + +} // namespace mbgl diff --git a/src/mbgl/programs/hillshade_program.cpp b/src/mbgl/programs/hillshade_program.cpp new file mode 100644 index 0000000000..f054ad4b74 --- /dev/null +++ b/src/mbgl/programs/hillshade_program.cpp @@ -0,0 +1,7 @@ +#include <mbgl/programs/hillshade_program.hpp> + +namespace mbgl { + +static_assert(sizeof(HillshadeLayoutVertex) == 8, "expected HillshadeLayoutVertex size"); + +} // namespace mbgl diff --git a/src/mbgl/programs/hillshade_program.hpp b/src/mbgl/programs/hillshade_program.hpp new file mode 100644 index 0000000000..5f9b4d1c2f --- /dev/null +++ b/src/mbgl/programs/hillshade_program.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include <mbgl/programs/program.hpp> +#include <mbgl/programs/attributes.hpp> +#include <mbgl/programs/uniforms.hpp> +#include <mbgl/shaders/hillshade.hpp> +#include <mbgl/util/geometry.hpp> +#include <mbgl/style/layers/hillshade_layer_properties.hpp> + +namespace mbgl { + +namespace uniforms { +MBGL_DEFINE_UNIFORM_SCALAR(Color, u_shadow); +MBGL_DEFINE_UNIFORM_SCALAR(Color, u_highlight); +MBGL_DEFINE_UNIFORM_SCALAR(Color, u_accent); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_light); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_latrange); +} // namespace uniforms + +class HillshadeProgram : public Program< + shaders::hillshade, + gl::Triangle, + gl::Attributes< + attributes::a_pos, + attributes::a_texture_pos>, + gl::Uniforms< + uniforms::u_matrix, + uniforms::u_image, + uniforms::u_highlight, + uniforms::u_shadow, + uniforms::u_accent, + uniforms::u_light, + uniforms::u_latrange>, + style::HillshadePaintProperties>{ +public: + using Program::Program; + + static LayoutVertex layoutVertex(Point<int16_t> p, Point<uint16_t> t) { + return LayoutVertex { + {{ + p.x, + p.y + }}, + {{ + t.x, + t.y + }} + }; + } +}; + +using HillshadeLayoutVertex = HillshadeProgram::LayoutVertex; +using HillshadeAttributes = HillshadeProgram::Attributes; + +} // namespace mbgl diff --git a/src/mbgl/programs/programs.hpp b/src/mbgl/programs/programs.hpp index a3891fafac..f533a6f633 100644 --- a/src/mbgl/programs/programs.hpp +++ b/src/mbgl/programs/programs.hpp @@ -6,6 +6,8 @@ #include <mbgl/programs/extrusion_texture_program.hpp> #include <mbgl/programs/fill_program.hpp> #include <mbgl/programs/fill_extrusion_program.hpp> +#include <mbgl/programs/hillshade_program.hpp> +#include <mbgl/programs/hillshade_prepare_program.hpp> #include <mbgl/programs/line_program.hpp> #include <mbgl/programs/raster_program.hpp> #include <mbgl/programs/symbol_program.hpp> @@ -28,6 +30,8 @@ public: fillPattern(context, programParameters), fillOutline(context, programParameters), fillOutlinePattern(context, programParameters), + hillshade(context, programParameters), + hillshadePrepare(context, programParameters), line(context, programParameters), lineSDF(context, programParameters), linePattern(context, programParameters), @@ -51,6 +55,8 @@ public: ProgramMap<FillPatternProgram> fillPattern; ProgramMap<FillOutlineProgram> fillOutline; ProgramMap<FillOutlinePatternProgram> fillOutlinePattern; + HillshadeProgram hillshade; + HillshadePrepareProgram hillshadePrepare; ProgramMap<LineProgram> line; ProgramMap<LineSDFProgram> lineSDF; ProgramMap<LinePatternProgram> linePattern; diff --git a/src/mbgl/renderer/buckets/hillshade_bucket.cpp b/src/mbgl/renderer/buckets/hillshade_bucket.cpp new file mode 100644 index 0000000000..8011681ee0 --- /dev/null +++ b/src/mbgl/renderer/buckets/hillshade_bucket.cpp @@ -0,0 +1,113 @@ +#include <mbgl/renderer/buckets/hillshade_bucket.hpp> +#include <mbgl/renderer/layers/render_hillshade_layer.hpp> +#include <mbgl/programs/hillshade_program.hpp> +#include <mbgl/programs/hillshade_prepare_program.hpp> +#include <mbgl/gl/context.hpp> + +namespace mbgl { + +using namespace style; + +HillshadeBucket::HillshadeBucket(PremultipliedImage&& image_): demdata(image_) { +} + +HillshadeBucket::HillshadeBucket(DEMData&& demdata_) : demdata(std::move(demdata_)) { +} + +const DEMData& HillshadeBucket::getDEMData() const { + return demdata; +} + +DEMData& HillshadeBucket::getDEMData() { + return demdata; +} + +void HillshadeBucket::upload(gl::Context& context) { + if (!hasData()) { + return; + } + + + const PremultipliedImage* image = demdata.getImage(); + dem = context.createTexture(*image); + + if (!segments.empty()) { + vertexBuffer = context.createVertexBuffer(std::move(vertices)); + indexBuffer = context.createIndexBuffer(std::move(indices)); + } + uploaded = true; +} + +void HillshadeBucket::clear() { + vertexBuffer = {}; + indexBuffer = {}; + segments.clear(); + vertices.clear(); + indices.clear(); + + uploaded = false; +} + +void HillshadeBucket::setMask(TileMask&& mask_) { + if (mask == mask_) { + return; + } + + mask = std::move(mask_); + clear(); + + if (mask == TileMask{ { 0, 0, 0 } }) { + // We want to render the full tile, and keeping the segments/vertices/indices empty means + // using the global shared buffers for covering the entire tile. + return; + } + + // Create a new segment so that we will upload (empty) buffers even when there is nothing to + // draw for this tile. + segments.emplace_back(0, 0); + + constexpr const uint16_t vertexLength = 4; + + // Create the vertex buffer for the specified tile mask. + for (const auto& id : mask) { + // Create a quad for every masked tile. + const int32_t vertexExtent = util::EXTENT >> id.z; + + const Point<int16_t> tlVertex = { static_cast<int16_t>(id.x * vertexExtent), + static_cast<int16_t>(id.y * vertexExtent) }; + const Point<int16_t> brVertex = { static_cast<int16_t>(tlVertex.x + vertexExtent), + static_cast<int16_t>(tlVertex.y + vertexExtent) }; + + 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.vertexSize(), indices.indexSize()); + } + + vertices.emplace_back( + HillshadeProgram::layoutVertex({ tlVertex.x, tlVertex.y }, { static_cast<uint16_t>(tlVertex.x), static_cast<uint16_t>(tlVertex.y) })); + vertices.emplace_back( + HillshadeProgram::layoutVertex({ brVertex.x, tlVertex.y }, { static_cast<uint16_t>(brVertex.x), static_cast<uint16_t>(tlVertex.y) })); + vertices.emplace_back( + HillshadeProgram::layoutVertex({ tlVertex.x, brVertex.y }, { static_cast<uint16_t>(tlVertex.x), static_cast<uint16_t>(brVertex.y) })); + vertices.emplace_back( + HillshadeProgram::layoutVertex({ brVertex.x, brVertex.y }, { static_cast<uint16_t>(brVertex.x), static_cast<uint16_t>(brVertex.y) })); + + auto& segment = segments.back(); + assert(segment.vertexLength <= std::numeric_limits<uint16_t>::max()); + const uint16_t offset = segment.vertexLength; + + // 0, 1, 2 + // 1, 2, 3 + indices.emplace_back(offset, offset + 1, offset + 2); + indices.emplace_back(offset + 1, offset + 2, offset + 3); + + segment.vertexLength += vertexLength; + segment.indexLength += 6; + } +} + +bool HillshadeBucket::hasData() const { + return demdata.getImage()->valid(); +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/buckets/hillshade_bucket.hpp b/src/mbgl/renderer/buckets/hillshade_bucket.hpp new file mode 100644 index 0000000000..3d9f6c61af --- /dev/null +++ b/src/mbgl/renderer/buckets/hillshade_bucket.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include <mbgl/gl/index_buffer.hpp> +#include <mbgl/gl/texture.hpp> +#include <mbgl/gl/vertex_buffer.hpp> +#include <mbgl/programs/hillshade_program.hpp> +#include <mbgl/programs/hillshade_prepare_program.hpp> +#include <mbgl/renderer/bucket.hpp> +#include <mbgl/renderer/tile_mask.hpp> +#include <mbgl/geometry/dem_data.hpp> +#include <mbgl/util/image.hpp> +#include <mbgl/util/mat4.hpp> +#include <mbgl/util/optional.hpp> + +namespace mbgl { + +class HillshadeBucket : public Bucket { +public: + HillshadeBucket(PremultipliedImage&&); + HillshadeBucket(std::shared_ptr<PremultipliedImage>); + HillshadeBucket(DEMData&&); + + + void upload(gl::Context&) override; + bool hasData() const override; + + void clear(); + void setMask(TileMask&&); + + optional<gl::Texture> dem; + optional<gl::Texture> texture; + + TileMask mask{ { 0, 0, 0 } }; + + const DEMData& getDEMData() const; + DEMData& getDEMData(); + + bool isPrepared() const { + return prepared; + } + + void setPrepared (bool preparedState) { + prepared = preparedState; + } + + // Raster-DEM Tile Sources use the default buffers from Painter + gl::VertexVector<HillshadeLayoutVertex> vertices; + gl::IndexVector<gl::Triangles> indices; + SegmentVector<HillshadeAttributes> segments; + + optional<gl::VertexBuffer<HillshadeLayoutVertex>> vertexBuffer; + optional<gl::IndexBuffer<gl::Triangles>> indexBuffer; +private: + DEMData demdata; + bool prepared = false; +}; + +} // namespace mbgl diff --git a/src/mbgl/renderer/layers/render_hillshade_layer.cpp b/src/mbgl/renderer/layers/render_hillshade_layer.cpp new file mode 100644 index 0000000000..7a767522c0 --- /dev/null +++ b/src/mbgl/renderer/layers/render_hillshade_layer.cpp @@ -0,0 +1,158 @@ +#include <mbgl/renderer/layers/render_hillshade_layer.hpp> +#include <mbgl/renderer/buckets/hillshade_bucket.hpp> +#include <mbgl/renderer/render_tile.hpp> +#include <mbgl/renderer/paint_parameters.hpp> +#include <mbgl/renderer/render_static_data.hpp> +#include <mbgl/programs/programs.hpp> +#include <mbgl/programs/hillshade_program.hpp> +#include <mbgl/programs/hillshade_prepare_program.hpp> +#include <mbgl/tile/tile.hpp> +#include <mbgl/style/layers/hillshade_layer_impl.hpp> +#include <mbgl/util/geo.hpp> +#include <mbgl/util/offscreen_texture.hpp> + +namespace mbgl { + +using namespace style; +RenderHillshadeLayer::RenderHillshadeLayer(Immutable<style::HillshadeLayer::Impl> _impl) + : RenderLayer(style::LayerType::Hillshade, _impl), + unevaluated(impl().paint.untransitioned()) { +} + +const style::HillshadeLayer::Impl& RenderHillshadeLayer::impl() const { + return static_cast<const style::HillshadeLayer::Impl&>(*baseImpl); +} + +std::unique_ptr<Bucket> RenderHillshadeLayer::createBucket(const BucketParameters&, const std::vector<const RenderLayer*>&) const { + assert(false); + return nullptr; +} + +const std::array<float, 2> RenderHillshadeLayer::getLatRange(const UnwrappedTileID& id) { + const LatLng latlng0 = LatLng(id); + const LatLng latlng1 = LatLng(UnwrappedTileID(id.canonical.z, id.canonical.x, id.canonical.y + 1)); + return {{ (float)latlng0.latitude(), (float)latlng1.latitude() }}; +} + +const std::array<float, 2> RenderHillshadeLayer::getLight(const PaintParameters& parameters){ + float azimuthal = evaluated.get<HillshadeIlluminationDirection>() * util::DEG2RAD; + if (evaluated.get<HillshadeIlluminationAnchor>() == HillshadeIlluminationAnchorType::Viewport) azimuthal = azimuthal - parameters.state.getAngle(); + return {{evaluated.get<HillshadeExaggeration>(), azimuthal}}; +} + +void RenderHillshadeLayer::transition(const TransitionParameters& parameters) { + unevaluated = impl().paint.transitioned(parameters, std::move(unevaluated)); +} + +void RenderHillshadeLayer::evaluate(const PropertyEvaluationParameters& parameters) { + evaluated = unevaluated.evaluate(parameters); + passes = (evaluated.get<style::HillshadeExaggeration >() > 0) + ? (RenderPass::Translucent | RenderPass::Pass3D) + : RenderPass::None; +} + +bool RenderHillshadeLayer::hasTransition() const { + return unevaluated.hasTransition(); +} + +void RenderHillshadeLayer::render(PaintParameters& parameters, RenderSource*) { + if (parameters.pass != RenderPass::Translucent && parameters.pass != RenderPass::Pass3D) + return; + + auto draw = [&] (const mat4& matrix, + const auto& vertexBuffer, + const auto& indexBuffer, + const auto& segments, + const UnwrappedTileID& id) { + parameters.programs.hillshade.draw( + parameters.context, + gl::Triangles(), + parameters.depthModeForSublayer(0, gl::DepthMode::ReadOnly), + gl::StencilMode::disabled(), + parameters.colorModeForRenderPass(), + HillshadeProgram::UniformValues { + uniforms::u_matrix::Value{ matrix }, + uniforms::u_image::Value{ 0 }, + uniforms::u_highlight::Value{ evaluated.get<HillshadeHighlightColor>() }, + uniforms::u_shadow::Value{ evaluated.get<HillshadeShadowColor>() }, + uniforms::u_accent::Value{ evaluated.get<HillshadeAccentColor>() }, + uniforms::u_light::Value{ getLight(parameters) }, + uniforms::u_latrange::Value{ getLatRange(id) }, + }, + vertexBuffer, + indexBuffer, + segments, + HillshadeProgram::PaintPropertyBinders { evaluated, 0 }, + evaluated, + parameters.state.getZoom(), + getID() + ); + }; + + mat4 mat; + matrix::ortho(mat, 0, util::EXTENT, -util::EXTENT, 0, 0, 1); + matrix::translate(mat, mat, 0, -util::EXTENT, 0); + + for (const RenderTile& tile : renderTiles) { + assert(dynamic_cast<HillshadeBucket*>(tile.tile.getBucket(*baseImpl))); + HillshadeBucket& bucket = *reinterpret_cast<HillshadeBucket*>(tile.tile.getBucket(*baseImpl)); + if (!bucket.hasData()){ + continue; + } + + if (!bucket.isPrepared() && parameters.pass == RenderPass::Pass3D) { + const uint16_t tilesize = bucket.getDEMData().dim; + OffscreenTexture view(parameters.context, { tilesize, tilesize }); + view.bind(); + + parameters.context.bindTexture(*bucket.dem, 0, gl::TextureFilter::Nearest, gl::TextureMipMap::No, gl::TextureWrap::Clamp, gl::TextureWrap::Clamp); + const Properties<>::PossiblyEvaluated properties; + + parameters.programs.hillshadePrepare.draw( + parameters.context, + gl::Triangles(), + parameters.depthModeForSublayer(0, gl::DepthMode::ReadOnly), + gl::StencilMode::disabled(), + parameters.colorModeForRenderPass(), + HillshadePrepareProgram::UniformValues { + uniforms::u_matrix::Value { mat }, + uniforms::u_dimension::Value { {{uint16_t(tilesize * 2), uint16_t(tilesize * 2) }} }, + uniforms::u_zoom::Value{ float(tile.id.canonical.z) }, + uniforms::u_image::Value{ 0 } + }, + parameters.staticData.rasterVertexBuffer, + parameters.staticData.quadTriangleIndexBuffer, + parameters.staticData.rasterSegments, + HillshadePrepareProgram::PaintPropertyBinders { properties, 0 }, + properties, + parameters.state.getZoom(), + getID() + ); + bucket.texture = std::move(view.getTexture()); + bucket.setPrepared(true); + } else if (parameters.pass == RenderPass::Translucent) { + assert(bucket.texture); + parameters.context.bindTexture(*bucket.texture, 0, gl::TextureFilter::Linear, gl::TextureMipMap::No, gl::TextureWrap::Clamp, gl::TextureWrap::Clamp); + + if (bucket.vertexBuffer && bucket.indexBuffer && !bucket.segments.empty()) { + // Draw only the parts of the tile that aren't drawn by another tile in the layer. + draw(tile.matrix, + *bucket.vertexBuffer, + *bucket.indexBuffer, + bucket.segments, + tile.id); + } else { + // Draw the full tile. + draw(tile.matrix, + parameters.staticData.rasterVertexBuffer, + parameters.staticData.quadTriangleIndexBuffer, + parameters.staticData.rasterSegments, + tile.id); + } + } + + + } +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/layers/render_hillshade_layer.hpp b/src/mbgl/renderer/layers/render_hillshade_layer.hpp new file mode 100644 index 0000000000..e9b9db1ec3 --- /dev/null +++ b/src/mbgl/renderer/layers/render_hillshade_layer.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include <mbgl/renderer/render_layer.hpp> +#include <mbgl/style/layers/hillshade_layer_impl.hpp> +#include <mbgl/style/layers/hillshade_layer_properties.hpp> +#include <mbgl/tile/tile_id.hpp> + +namespace mbgl { + +class RenderHillshadeLayer: public RenderLayer { +public: + RenderHillshadeLayer(Immutable<style::HillshadeLayer::Impl>); + ~RenderHillshadeLayer() final = default; + + void transition(const TransitionParameters&) override; + void evaluate(const PropertyEvaluationParameters&) override; + bool hasTransition() const override; + + void render(PaintParameters&, RenderSource*) override; + + std::unique_ptr<Bucket> createBucket(const BucketParameters&, const std::vector<const RenderLayer*>&) const override; + + // Paint properties + style::HillshadePaintProperties::Unevaluated unevaluated; + style::HillshadePaintProperties::PossiblyEvaluated evaluated; + + const style::HillshadeLayer::Impl& impl() const; +private: + const std::array<float, 2> getLatRange(const UnwrappedTileID& id); + const std::array<float, 2> getLight(const PaintParameters& parameters); +}; + +template <> +inline bool RenderLayer::is<RenderHillshadeLayer>() const { + return type == style::LayerType::Hillshade; +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/render_layer.cpp b/src/mbgl/renderer/render_layer.cpp index eb2b74ffe0..248905f834 100644 --- a/src/mbgl/renderer/render_layer.cpp +++ b/src/mbgl/renderer/render_layer.cpp @@ -4,6 +4,7 @@ #include <mbgl/renderer/layers/render_custom_layer.hpp> #include <mbgl/renderer/layers/render_fill_extrusion_layer.hpp> #include <mbgl/renderer/layers/render_fill_layer.hpp> +#include <mbgl/renderer/layers/render_hillshade_layer.hpp> #include <mbgl/renderer/layers/render_line_layer.hpp> #include <mbgl/renderer/layers/render_raster_layer.hpp> #include <mbgl/renderer/layers/render_symbol_layer.hpp> @@ -26,6 +27,8 @@ std::unique_ptr<RenderLayer> RenderLayer::create(Immutable<Layer::Impl> impl) { return std::make_unique<RenderSymbolLayer>(staticImmutableCast<SymbolLayer::Impl>(impl)); case LayerType::Raster: return std::make_unique<RenderRasterLayer>(staticImmutableCast<RasterLayer::Impl>(impl)); + case LayerType::Hillshade: + return std::make_unique<RenderHillshadeLayer>(staticImmutableCast<HillshadeLayer::Impl>(impl)); case LayerType::Background: return std::make_unique<RenderBackgroundLayer>(staticImmutableCast<BackgroundLayer::Impl>(impl)); case LayerType::Custom: diff --git a/src/mbgl/renderer/render_source.cpp b/src/mbgl/renderer/render_source.cpp index 6624bb7d96..d160eb16e3 100644 --- a/src/mbgl/renderer/render_source.cpp +++ b/src/mbgl/renderer/render_source.cpp @@ -2,6 +2,7 @@ #include <mbgl/renderer/render_source_observer.hpp> #include <mbgl/renderer/sources/render_geojson_source.hpp> #include <mbgl/renderer/sources/render_raster_source.hpp> +#include <mbgl/renderer/sources/render_raster_dem_source.hpp> #include <mbgl/renderer/sources/render_vector_source.hpp> #include <mbgl/renderer/tile_parameters.hpp> #include <mbgl/annotation/render_annotation_source.hpp> @@ -19,6 +20,8 @@ std::unique_ptr<RenderSource> RenderSource::create(Immutable<Source::Impl> impl) return std::make_unique<RenderVectorSource>(staticImmutableCast<VectorSource::Impl>(impl)); case SourceType::Raster: return std::make_unique<RenderRasterSource>(staticImmutableCast<RasterSource::Impl>(impl)); + case SourceType::RasterDEM: + return std::make_unique<RenderRasterDEMSource>(staticImmutableCast<RasterSource::Impl>(impl)); case SourceType::GeoJSON: return std::make_unique<RenderGeoJSONSource>(staticImmutableCast<GeoJSONSource::Impl>(impl)); case SourceType::Video: diff --git a/src/mbgl/renderer/render_source.hpp b/src/mbgl/renderer/render_source.hpp index 8c84af4f1e..db88230e53 100644 --- a/src/mbgl/renderer/render_source.hpp +++ b/src/mbgl/renderer/render_source.hpp @@ -84,7 +84,7 @@ protected: bool enabled = false; - void onTileChanged(Tile&) final; + void onTileChanged(Tile&) override; void onTileError(Tile&, std::exception_ptr) final; }; diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index c38f1d56cb..61e7d17242 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -14,6 +14,7 @@ #include <mbgl/renderer/layers/render_background_layer.hpp> #include <mbgl/renderer/layers/render_custom_layer.hpp> #include <mbgl/renderer/layers/render_fill_extrusion_layer.hpp> +#include <mbgl/renderer/layers/render_hillshade_layer.hpp> #include <mbgl/renderer/style_diff.hpp> #include <mbgl/renderer/query.hpp> #include <mbgl/renderer/backend_scope.hpp> @@ -289,7 +290,7 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { RenderLayer* layer = getRenderLayer(layerImpl->id); assert(layer); - if (!parameters.staticData.has3D && layer->is<RenderFillExtrusionLayer>()) { + if (!parameters.staticData.has3D && (layer->is<RenderFillExtrusionLayer>() || layer->is<RenderHillshadeLayer>())) { parameters.staticData.has3D = true; } diff --git a/src/mbgl/renderer/sources/render_raster_dem_source.cpp b/src/mbgl/renderer/sources/render_raster_dem_source.cpp new file mode 100644 index 0000000000..76716518d7 --- /dev/null +++ b/src/mbgl/renderer/sources/render_raster_dem_source.cpp @@ -0,0 +1,165 @@ +#include <mbgl/renderer/sources/render_raster_dem_source.hpp> +#include <mbgl/renderer/render_tile.hpp> +#include <mbgl/tile/raster_dem_tile.hpp> +#include <mbgl/algorithm/update_tile_masks.hpp> +#include <mbgl/geometry/dem_data.hpp> +#include <mbgl/renderer/buckets/hillshade_bucket.hpp> +#include <iostream> + +namespace mbgl { + +using namespace style; + +RenderRasterDEMSource::RenderRasterDEMSource(Immutable<style::RasterSource::Impl> impl_) + : RenderSource(impl_) { + tilePyramid.setObserver(this); +} + +const style::RasterSource::Impl& RenderRasterDEMSource::impl() const { + return static_cast<const style::RasterSource::Impl&>(*baseImpl); +} + +bool RenderRasterDEMSource::isLoaded() const { + return tilePyramid.isLoaded(); +} + +void RenderRasterDEMSource::update(Immutable<style::Source::Impl> baseImpl_, + const std::vector<Immutable<Layer::Impl>>& layers, + const bool needsRendering, + const bool needsRelayout, + const TileParameters& parameters) { + std::swap(baseImpl, baseImpl_); + + enabled = needsRendering; + + optional<Tileset> tileset = impl().getTileset(); + + if (!tileset) { + return; + } + + if (tileURLTemplates != tileset->tiles) { + tileURLTemplates = tileset->tiles; + + // TODO: this removes existing buckets, and will cause flickering. + // Should instead refresh tile data in place. + tilePyramid.tiles.clear(); + tilePyramid.renderTiles.clear(); + tilePyramid.cache.clear(); + } + + tilePyramid.update(layers, + needsRendering, + needsRelayout, + parameters, + SourceType::RasterDEM, + impl().getTileSize(), + tileset->zoomRange, + tileset->bounds, + [&] (const OverscaledTileID& tileID) { + return std::make_unique<RasterDEMTile>(tileID, parameters, *tileset); + }); +} + +void RenderRasterDEMSource::onTileChanged(Tile& tile){ + RasterDEMTile& demtile = static_cast<RasterDEMTile&>(tile); + + std::map<DEMTileNeighbors, DEMTileNeighbors> opposites = { + { DEMTileNeighbors::Left, DEMTileNeighbors::Right }, + { DEMTileNeighbors::Right, DEMTileNeighbors::Left }, + { DEMTileNeighbors::TopLeft, DEMTileNeighbors::BottomRight }, + { DEMTileNeighbors::TopCenter, DEMTileNeighbors::BottomCenter }, + { DEMTileNeighbors::TopRight, DEMTileNeighbors::BottomLeft }, + { DEMTileNeighbors::BottomRight, DEMTileNeighbors::TopLeft }, + { DEMTileNeighbors::BottomCenter, DEMTileNeighbors:: TopCenter }, + { DEMTileNeighbors::BottomLeft, DEMTileNeighbors::TopRight } + }; + + if (tile.isRenderable() && demtile.neighboringTiles != DEMTileNeighbors::Complete) { + const CanonicalTileID canonical = tile.id.canonical; + const uint32_t dim = std::pow(2, canonical.z); + const uint32_t px = (canonical.x - 1 + dim) % dim; + const int pxw = canonical.x == 0 ? tile.id.wrap - 1 : tile.id.wrap; + const uint32_t nx = (canonical.x + 1 + dim) % dim; + const int nxw = (canonical.x + 1 == dim) ? tile.id.wrap + 1 : tile.id.wrap; + + auto getNeighbor = [&] (DEMTileNeighbors mask){ + if (mask == DEMTileNeighbors::Left){ + return OverscaledTileID(tile.id.overscaledZ, pxw, canonical.z, px, canonical.y); + } else if (mask == DEMTileNeighbors::Right){ + return OverscaledTileID(tile.id.overscaledZ, nxw, canonical.z, nx, canonical.y); + } else if (mask == DEMTileNeighbors::TopLeft){ + return OverscaledTileID(tile.id.overscaledZ, pxw, canonical.z, px, canonical.y - 1); + } else if (mask == DEMTileNeighbors::TopCenter){ + return OverscaledTileID(tile.id.overscaledZ, tile.id.wrap, canonical.z, canonical.x, canonical.y - 1); + } else if (mask == DEMTileNeighbors::TopRight){ + return OverscaledTileID(tile.id.overscaledZ, nxw, canonical.z, nx, canonical.y - 1); + } else if (mask == DEMTileNeighbors::BottomLeft){ + return OverscaledTileID(tile.id.overscaledZ, pxw, canonical.z, px, canonical.y + 1); + } else if (mask == DEMTileNeighbors::BottomCenter){ + return OverscaledTileID(tile.id.overscaledZ, tile.id.wrap, canonical.z, canonical.x, canonical.y + 1); + } else if (mask == DEMTileNeighbors::BottomRight){ + return OverscaledTileID(tile.id.overscaledZ, nxw, canonical.z, nx, canonical.y + 1); + } else{ + throw std::runtime_error("mask is not a valid tile neighbor"); + } + }; + + for (uint8_t i = 0; i < 8; i++) { + DEMTileNeighbors mask = DEMTileNeighbors(std::pow(2,i)); + // only backfill if this neighbor has not been previously backfilled + if ((demtile.neighboringTiles & mask) != mask) { + OverscaledTileID neighborid = getNeighbor(mask); + Tile* renderableNeighbor = tilePyramid.getTile(neighborid); + if (renderableNeighbor != nullptr && renderableNeighbor->isRenderable()) { + RasterDEMTile& borderTile = static_cast<RasterDEMTile&>(*renderableNeighbor); + demtile.backfillBorder(borderTile, mask); + + // if the border tile has not been backfilled by a previous instance of the main + // tile, backfill its corresponding neighbor as well. + const DEMTileNeighbors& borderMask = opposites[mask]; + if ((borderTile.neighboringTiles & borderMask) != borderMask){ + borderTile.backfillBorder(demtile, borderMask); + } + } + } + } + } + RenderSource::onTileChanged(tile); +} + +void RenderRasterDEMSource::startRender(PaintParameters& parameters) { + algorithm::updateTileMasks(tilePyramid.getRenderTiles()); + tilePyramid.startRender(parameters); +} + +void RenderRasterDEMSource::finishRender(PaintParameters& parameters) { + tilePyramid.finishRender(parameters); +} + +std::vector<std::reference_wrapper<RenderTile>> RenderRasterDEMSource::getRenderTiles() { + return tilePyramid.getRenderTiles(); +} + +std::unordered_map<std::string, std::vector<Feature>> +RenderRasterDEMSource::queryRenderedFeatures(const ScreenLineString&, + const TransformState&, + const std::vector<const RenderLayer*>&, + const RenderedQueryOptions&, + const CollisionIndex& ) const { + return std::unordered_map<std::string, std::vector<Feature>> {}; +} + +std::vector<Feature> RenderRasterDEMSource::querySourceFeatures(const SourceQueryOptions&) const { + return {}; +} + +void RenderRasterDEMSource::onLowMemory() { + tilePyramid.onLowMemory(); +} + +void RenderRasterDEMSource::dumpDebugLogs() const { + tilePyramid.dumpDebugLogs(); +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/sources/render_raster_dem_source.hpp b/src/mbgl/renderer/sources/render_raster_dem_source.hpp new file mode 100644 index 0000000000..b6b8bf977c --- /dev/null +++ b/src/mbgl/renderer/sources/render_raster_dem_source.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include <mbgl/renderer/render_source.hpp> +#include <mbgl/renderer/tile_pyramid.hpp> +#include <mbgl/style/sources/raster_source_impl.hpp> + +namespace mbgl { + +class RenderRasterDEMSource : public RenderSource { +public: + RenderRasterDEMSource(Immutable<style::RasterSource::Impl>); + + bool isLoaded() const final; + + void update(Immutable<style::Source::Impl>, + const std::vector<Immutable<style::Layer::Impl>>&, + bool needsRendering, + bool needsRelayout, + const TileParameters&) final; + + void startRender(PaintParameters&) final; + void finishRender(PaintParameters&) final; + + std::vector<std::reference_wrapper<RenderTile>> getRenderTiles() final; + + std::unordered_map<std::string, std::vector<Feature>> + queryRenderedFeatures(const ScreenLineString& geometry, + const TransformState& transformState, + const std::vector<const RenderLayer*>& layers, + const RenderedQueryOptions& options, + const CollisionIndex& collisionIndex) const final; + + std::vector<Feature> + querySourceFeatures(const SourceQueryOptions&) const final; + + void onLowMemory() final; + void dumpDebugLogs() const final; + +private: + const style::RasterSource::Impl& impl() const; + + TilePyramid tilePyramid; + optional<std::vector<std::string>> tileURLTemplates; + +protected: + void onTileChanged(Tile&) final; +}; + +template <> +inline bool RenderSource::is<RenderRasterDEMSource>() const { + return baseImpl->type == style::SourceType::RasterDEM; +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/tile_pyramid.cpp b/src/mbgl/renderer/tile_pyramid.cpp index f6eb62f655..07239b7a1c 100644 --- a/src/mbgl/renderer/tile_pyramid.cpp +++ b/src/mbgl/renderer/tile_pyramid.cpp @@ -56,6 +56,11 @@ std::vector<std::reference_wrapper<RenderTile>> TilePyramid::getRenderTiles() { return { renderTiles.begin(), renderTiles.end() }; } +Tile* TilePyramid::getTile(const OverscaledTileID& tileID){ + auto it = tiles.find(tileID); + return it == tiles.end() ? cache.get(tileID) : it->second.get(); +} + void TilePyramid::update(const std::vector<Immutable<style::Layer::Impl>>& layers, const bool needsRendering, const bool needsRelayout, @@ -147,7 +152,7 @@ void TilePyramid::update(const std::vector<Immutable<style::Layer::Impl>>& layer if (tileRange && !tileRange->contains(tileID.canonical)) { return nullptr; } - std::unique_ptr<Tile> tile = cache.get(tileID); + std::unique_ptr<Tile> tile = cache.pop(tileID); if (!tile) { tile = createTile(tileID); if (tile) { diff --git a/src/mbgl/renderer/tile_pyramid.hpp b/src/mbgl/renderer/tile_pyramid.hpp index 3755cee06d..ad3f91bf88 100644 --- a/src/mbgl/renderer/tile_pyramid.hpp +++ b/src/mbgl/renderer/tile_pyramid.hpp @@ -47,6 +47,7 @@ public: void finishRender(PaintParameters&); std::vector<std::reference_wrapper<RenderTile>> getRenderTiles(); + Tile* getTile(const OverscaledTileID&); std::unordered_map<std::string, std::vector<Feature>> queryRenderedFeatures(const ScreenLineString& geometry, diff --git a/src/mbgl/shaders/hillshade.cpp b/src/mbgl/shaders/hillshade.cpp new file mode 100644 index 0000000000..4083faa4b4 --- /dev/null +++ b/src/mbgl/shaders/hillshade.cpp @@ -0,0 +1,80 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/hillshade.hpp> + +namespace mbgl { +namespace shaders { + +const char* hillshade::name = "hillshade"; +const char* hillshade::vertexSource = R"MBGL_SHADER( +uniform mat4 u_matrix; + +attribute vec2 a_pos; +attribute vec2 a_texture_pos; + +varying vec2 v_pos; + +void main() { + gl_Position = u_matrix * vec4(a_pos, 0, 1); + v_pos = a_texture_pos / 8192.0; +} + +)MBGL_SHADER"; +const char* hillshade::fragmentSource = R"MBGL_SHADER( +uniform sampler2D u_image; +varying vec2 v_pos; + +uniform vec2 u_latrange; +uniform vec2 u_light; +uniform vec4 u_shadow; +uniform vec4 u_highlight; +uniform vec4 u_accent; + +#define PI 3.141592653589793 + +void main() { + vec4 pixel = texture2D(u_image, v_pos); + + vec2 deriv = ((pixel.rg * 2.0) - 1.0); + + // We divide the slope by a scale factor based on the cosin of the pixel's approximate latitude + // to account for mercator projection distortion. see #4807 for details + float scaleFactor = cos(radians((u_latrange[0] - u_latrange[1]) * (1.0 - v_pos.y) + u_latrange[1])); + // We also multiply the slope by an arbitrary z-factor of 1.25 + float slope = atan(1.25 * length(deriv) / scaleFactor); + float aspect = deriv.x != 0.0 ? atan(deriv.y, -deriv.x) : PI / 2.0 * (deriv.y > 0.0 ? 1.0 : -1.0); + + float intensity = u_light.x; + // We add PI to make this property match the global light object, which adds PI/2 to the light's azimuthal + // position property to account for 0deg corresponding to north/the top of the viewport in the style spec + // and the original shader was written to accept (-illuminationDirection - 90) as the azimuthal. + float azimuth = u_light.y + PI; + + // We scale the slope exponentially based on intensity, using a calculation similar to + // the exponential interpolation function in the style spec: + // https://github.com/mapbox/mapbox-gl-js/blob/master/src/style-spec/expression/definitions/interpolate.js#L217-L228 + // so that higher intensity values create more opaque hillshading. + float base = 1.875 - intensity * 1.75; + float maxValue = 0.5 * PI; + float scaledSlope = intensity != 0.5 ? ((pow(base, slope) - 1.0) / (pow(base, maxValue) - 1.0)) * maxValue : slope; + + // The accent color is calculated with the cosine of the slope while the shade color is calculated with the sine + // so that the accent color's rate of change eases in while the shade color's eases out. + float accent = cos(scaledSlope); + // We multiply both the accent and shade color by a clamped intensity value + // so that intensities >= 0.5 do not additionally affect the color values + // while intensity values < 0.5 make the overall color more transparent. + vec4 accent_color = (1.0 - accent) * u_accent * clamp(intensity * 2.0, 0.0, 1.0); + float shade = abs(mod((aspect + azimuth) / PI + 0.5, 2.0) - 1.0); + vec4 shade_color = mix(u_shadow, u_highlight, shade) * sin(scaledSlope) * clamp(intensity * 2.0, 0.0, 1.0); + gl_FragColor = accent_color * (1.0 - shade_color.a) + shade_color; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/hillshade.hpp b/src/mbgl/shaders/hillshade.hpp new file mode 100644 index 0000000000..a4a27cb595 --- /dev/null +++ b/src/mbgl/shaders/hillshade.hpp @@ -0,0 +1,16 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#pragma once + +namespace mbgl { +namespace shaders { + +class hillshade { +public: + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/hillshade_prepare.cpp b/src/mbgl/shaders/hillshade_prepare.cpp new file mode 100644 index 0000000000..733658435e --- /dev/null +++ b/src/mbgl/shaders/hillshade_prepare.cpp @@ -0,0 +1,99 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/hillshade_prepare.hpp> + +namespace mbgl { +namespace shaders { + +const char* hillshade_prepare::name = "hillshade_prepare"; +const char* hillshade_prepare::vertexSource = R"MBGL_SHADER( +uniform mat4 u_matrix; + +attribute vec2 a_pos; +attribute vec2 a_texture_pos; + +varying vec2 v_pos; + +void main() { + gl_Position = u_matrix * vec4(a_pos, 0, 1); + v_pos = (a_texture_pos / 8192.0) / 2.0 + 0.25; +} + +)MBGL_SHADER"; +const char* hillshade_prepare::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#endif + +uniform sampler2D u_image; +varying vec2 v_pos; +uniform vec2 u_dimension; +uniform float u_zoom; + +float getElevation(vec2 coord, float bias) { + // Convert encoded elevation value to meters + vec4 data = texture2D(u_image, coord) * 255.0; + return (data.r + data.g * 256.0 + data.b * 256.0 * 256.0) / 4.0; +} + +void main() { + vec2 epsilon = 1.0 / u_dimension; + + // queried pixels: + // +-----------+ + // | | | | + // | a | b | c | + // | | | | + // +-----------+ + // | | | | + // | d | e | f | + // | | | | + // +-----------+ + // | | | | + // | g | h | i | + // | | | | + // +-----------+ + + float a = getElevation(v_pos + vec2(-epsilon.x, -epsilon.y), 0.0); + float b = getElevation(v_pos + vec2(0, -epsilon.y), 0.0); + float c = getElevation(v_pos + vec2(epsilon.x, -epsilon.y), 0.0); + float d = getElevation(v_pos + vec2(-epsilon.x, 0), 0.0); + float e = getElevation(v_pos, 0.0); + float f = getElevation(v_pos + vec2(epsilon.x, 0), 0.0); + float g = getElevation(v_pos + vec2(-epsilon.x, epsilon.y), 0.0); + float h = getElevation(v_pos + vec2(0, epsilon.y), 0.0); + float i = getElevation(v_pos + vec2(epsilon.x, epsilon.y), 0.0); + + // here we divide the x and y slopes by 8 * pixel size + // where pixel size (aka meters/pixel) is: + // circumference of the world / (pixels per tile * number of tiles) + // which is equivalent to: 8 * 40075016.6855785 / (512 * pow(2, u_zoom)) + // which can be reduced to: pow(2, 19.25619978527 - u_zoom) + // we want to vertically exaggerate the hillshading though, because otherwise + // it is barely noticeable at low zooms. to do this, we multiply this by some + // scale factor pow(2, (u_zoom - 14) * a) where a is an arbitrary value and 14 is the + // maxzoom of the tile source. here we use a=0.3 which works out to the + // expression below. see nickidlugash's awesome breakdown for more info + // https://github.com/mapbox/mapbox-gl-js/pull/5286#discussion_r148419556 + float exaggeration = u_zoom < 2.0 ? 0.4 : u_zoom < 4.5 ? 0.35 : 0.3; + + vec2 deriv = vec2( + (c + f + f + i) - (a + d + d + g), + (g + h + h + i) - (a + b + b + c) + ) / pow(2.0, (u_zoom - 14.0) * exaggeration + 19.2562 - u_zoom); + + gl_FragColor = clamp(vec4( + deriv.x / 2.0 + 0.5, + deriv.y / 2.0 + 0.5, + 1.0, + 1.0), 0.0, 1.0); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/hillshade_prepare.hpp b/src/mbgl/shaders/hillshade_prepare.hpp new file mode 100644 index 0000000000..c38b4a0d19 --- /dev/null +++ b/src/mbgl/shaders/hillshade_prepare.hpp @@ -0,0 +1,16 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#pragma once + +namespace mbgl { +namespace shaders { + +class hillshade_prepare { +public: + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/style/conversion/layer.cpp b/src/mbgl/style/conversion/layer.cpp index 0ca582f8dc..8f624e3324 100644 --- a/src/mbgl/style/conversion/layer.cpp +++ b/src/mbgl/style/conversion/layer.cpp @@ -6,6 +6,7 @@ #include <mbgl/style/layers/circle_layer.hpp> #include <mbgl/style/layers/fill_layer.hpp> #include <mbgl/style/layers/fill_extrusion_layer.hpp> +#include <mbgl/style/layers/hillshade_layer.hpp> #include <mbgl/style/layers/line_layer.hpp> #include <mbgl/style/layers/raster_layer.hpp> #include <mbgl/style/layers/symbol_layer.hpp> @@ -96,6 +97,23 @@ static optional<std::unique_ptr<Layer>> convertRasterLayer(const std::string& id return { std::make_unique<RasterLayer>(id, *source) }; } +static optional<std::unique_ptr<Layer>> convertHillshadeLayer(const std::string& id, const Convertible& value, Error& error) { + auto sourceValue = objectMember(value, "source"); + if (!sourceValue) { + error = { "layer must have a source" }; + return {}; + } + + optional<std::string> source = toString(*sourceValue); + if (!source) { + error = { "layer source must be a string" }; + return {}; + } + + return { std::make_unique<HillshadeLayer>(id, *source) }; +} + + static optional<std::unique_ptr<Layer>> convertBackgroundLayer(const std::string& id, const Convertible&, Error&) { return { std::make_unique<BackgroundLayer>(id) }; } @@ -144,6 +162,8 @@ optional<std::unique_ptr<Layer>> Converter<std::unique_ptr<Layer>>::operator()(c converted = convertVectorLayer<SymbolLayer>(*id, value, error); } else if (*type == "raster") { converted = convertRasterLayer(*id, value, error); + } else if (*type == "hillshade") { + converted = convertHillshadeLayer(*id, value, error); } else if (*type == "background") { converted = convertBackgroundLayer(*id, value, error); } else { diff --git a/src/mbgl/style/conversion/make_property_setters.hpp b/src/mbgl/style/conversion/make_property_setters.hpp index 18370df636..adfcc4dd61 100644 --- a/src/mbgl/style/conversion/make_property_setters.hpp +++ b/src/mbgl/style/conversion/make_property_setters.hpp @@ -10,6 +10,7 @@ #include <mbgl/style/layers/circle_layer.hpp> #include <mbgl/style/layers/fill_extrusion_layer.hpp> #include <mbgl/style/layers/raster_layer.hpp> +#include <mbgl/style/layers/hillshade_layer.hpp> #include <mbgl/style/layers/background_layer.hpp> #include <unordered_map> @@ -70,6 +71,7 @@ inline auto makeLayoutPropertySetters() { + return result; } @@ -194,6 +196,19 @@ inline auto makePaintPropertySetters() { result["raster-fade-duration"] = &setProperty<RasterLayer, PropertyValue<float>, &RasterLayer::setRasterFadeDuration>; result["raster-fade-duration-transition"] = &setTransition<RasterLayer, &RasterLayer::setRasterFadeDurationTransition>; + result["hillshade-illumination-direction"] = &setProperty<HillshadeLayer, PropertyValue<float>, &HillshadeLayer::setHillshadeIlluminationDirection>; + result["hillshade-illumination-direction-transition"] = &setTransition<HillshadeLayer, &HillshadeLayer::setHillshadeIlluminationDirectionTransition>; + result["hillshade-illumination-anchor"] = &setProperty<HillshadeLayer, PropertyValue<HillshadeIlluminationAnchorType>, &HillshadeLayer::setHillshadeIlluminationAnchor>; + result["hillshade-illumination-anchor-transition"] = &setTransition<HillshadeLayer, &HillshadeLayer::setHillshadeIlluminationAnchorTransition>; + result["hillshade-exaggeration"] = &setProperty<HillshadeLayer, PropertyValue<float>, &HillshadeLayer::setHillshadeExaggeration>; + result["hillshade-exaggeration-transition"] = &setTransition<HillshadeLayer, &HillshadeLayer::setHillshadeExaggerationTransition>; + result["hillshade-shadow-color"] = &setProperty<HillshadeLayer, PropertyValue<Color>, &HillshadeLayer::setHillshadeShadowColor>; + result["hillshade-shadow-color-transition"] = &setTransition<HillshadeLayer, &HillshadeLayer::setHillshadeShadowColorTransition>; + result["hillshade-highlight-color"] = &setProperty<HillshadeLayer, PropertyValue<Color>, &HillshadeLayer::setHillshadeHighlightColor>; + result["hillshade-highlight-color-transition"] = &setTransition<HillshadeLayer, &HillshadeLayer::setHillshadeHighlightColorTransition>; + result["hillshade-accent-color"] = &setProperty<HillshadeLayer, PropertyValue<Color>, &HillshadeLayer::setHillshadeAccentColor>; + result["hillshade-accent-color-transition"] = &setTransition<HillshadeLayer, &HillshadeLayer::setHillshadeAccentColorTransition>; + result["background-color"] = &setProperty<BackgroundLayer, PropertyValue<Color>, &BackgroundLayer::setBackgroundColor>; result["background-color-transition"] = &setTransition<BackgroundLayer, &BackgroundLayer::setBackgroundColorTransition>; result["background-pattern"] = &setProperty<BackgroundLayer, PropertyValue<std::string>, &BackgroundLayer::setBackgroundPattern>; diff --git a/src/mbgl/style/conversion/source.cpp b/src/mbgl/style/conversion/source.cpp index c10d0babcf..670f50c041 100644 --- a/src/mbgl/style/conversion/source.cpp +++ b/src/mbgl/style/conversion/source.cpp @@ -5,6 +5,7 @@ #include <mbgl/style/conversion/tileset.hpp> #include <mbgl/style/sources/geojson_source.hpp> #include <mbgl/style/sources/raster_source.hpp> +#include <mbgl/style/sources/raster_dem_source.hpp> #include <mbgl/style/sources/vector_source.hpp> #include <mbgl/style/sources/image_source.hpp> #include <mbgl/util/geo.hpp> @@ -55,6 +56,28 @@ static optional<std::unique_ptr<Source>> convertRasterSource(const std::string& return { std::make_unique<RasterSource>(id, std::move(*urlOrTileset), tileSize) }; } +static optional<std::unique_ptr<Source>> convertRasterDEMSource(const std::string& id, + const Convertible& value, + Error& error) { + optional<variant<std::string, Tileset>> urlOrTileset = convertURLOrTileset(value, error); + if (!urlOrTileset) { + return {}; + } + + uint16_t tileSize = util::tileSize; + auto tileSizeValue = objectMember(value, "tileSize"); + if (tileSizeValue) { + optional<float> size = toNumber(*tileSizeValue); + if (!size || *size < 0 || *size > std::numeric_limits<uint16_t>::max()) { + error = { "invalid tileSize" }; + return {}; + } + tileSize = *size; + } + + return { std::make_unique<RasterDEMSource>(id, std::move(*urlOrTileset), tileSize) }; +} + static optional<std::unique_ptr<Source>> convertVectorSource(const std::string& id, const Convertible& value, Error& error) { @@ -155,9 +178,11 @@ optional<std::unique_ptr<Source>> Converter<std::unique_ptr<Source>>::operator() error = { "source type must be a string" }; return {}; } - + const std::string tname = *type; if (*type == "raster") { return convertRasterSource(id, value, error); + } else if (*type == "raster-dem") { + return convertRasterDEMSource(id, value, error); } else if (*type == "vector") { return convertVectorSource(id, value, error); } else if (*type == "geojson") { diff --git a/src/mbgl/style/expression/value.cpp b/src/mbgl/style/expression/value.cpp index ef4769df02..faa44e78aa 100644 --- a/src/mbgl/style/expression/value.cpp +++ b/src/mbgl/style/expression/value.cpp @@ -305,6 +305,10 @@ template type::Type valueTypeToExpressionType<TranslateAnchorType>(); template optional<TranslateAnchorType> fromExpressionValue<TranslateAnchorType>(const Value&); template Value toExpressionValue(const TranslateAnchorType&); +template type::Type valueTypeToExpressionType<HillshadeIlluminationAnchorType>(); +template optional<HillshadeIlluminationAnchorType> fromExpressionValue<HillshadeIlluminationAnchorType>(const Value&); +template Value toExpressionValue(const HillshadeIlluminationAnchorType&); + template type::Type valueTypeToExpressionType<LightAnchorType>(); template optional<LightAnchorType> fromExpressionValue<LightAnchorType>(const Value&); template Value toExpressionValue(const LightAnchorType&); diff --git a/src/mbgl/style/layers/hillshade_layer.cpp b/src/mbgl/style/layers/hillshade_layer.cpp new file mode 100644 index 0000000000..ea736af1ad --- /dev/null +++ b/src/mbgl/style/layers/hillshade_layer.cpp @@ -0,0 +1,238 @@ +// This file is generated. Edit scripts/generate-style-code.js, then run `make style-code`. + +#include <mbgl/style/layers/hillshade_layer.hpp> +#include <mbgl/style/layers/hillshade_layer_impl.hpp> +#include <mbgl/style/layer_observer.hpp> + +namespace mbgl { +namespace style { + +HillshadeLayer::HillshadeLayer(const std::string& layerID, const std::string& sourceID) + : Layer(makeMutable<Impl>(LayerType::Hillshade, layerID, sourceID)) { +} + +HillshadeLayer::HillshadeLayer(Immutable<Impl> impl_) + : Layer(std::move(impl_)) { +} + +HillshadeLayer::~HillshadeLayer() = default; + +const HillshadeLayer::Impl& HillshadeLayer::impl() const { + return static_cast<const Impl&>(*baseImpl); +} + +Mutable<HillshadeLayer::Impl> HillshadeLayer::mutableImpl() const { + return makeMutable<Impl>(impl()); +} + +std::unique_ptr<Layer> HillshadeLayer::cloneRef(const std::string& id_) const { + auto impl_ = mutableImpl(); + impl_->id = id_; + impl_->paint = HillshadePaintProperties::Transitionable(); + return std::make_unique<HillshadeLayer>(std::move(impl_)); +} + +void HillshadeLayer::Impl::stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>&) const { +} + +// Source + +const std::string& HillshadeLayer::getSourceID() const { + return impl().source; +} + + +// Visibility + +void HillshadeLayer::setVisibility(VisibilityType value) { + if (value == getVisibility()) + return; + auto impl_ = mutableImpl(); + impl_->visibility = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +// Zoom range + +void HillshadeLayer::setMinZoom(float minZoom) { + auto impl_ = mutableImpl(); + impl_->minZoom = minZoom; + baseImpl = std::move(impl_); +} + +void HillshadeLayer::setMaxZoom(float maxZoom) { + auto impl_ = mutableImpl(); + impl_->maxZoom = maxZoom; + baseImpl = std::move(impl_); +} + +// Layout properties + + +// Paint properties + +PropertyValue<float> HillshadeLayer::getDefaultHillshadeIlluminationDirection() { + return { 335 }; +} + +PropertyValue<float> HillshadeLayer::getHillshadeIlluminationDirection() const { + return impl().paint.template get<HillshadeIlluminationDirection>().value; +} + +void HillshadeLayer::setHillshadeIlluminationDirection(PropertyValue<float> value) { + if (value == getHillshadeIlluminationDirection()) + return; + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeIlluminationDirection>().value = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void HillshadeLayer::setHillshadeIlluminationDirectionTransition(const TransitionOptions& options) { + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeIlluminationDirection>().options = options; + baseImpl = std::move(impl_); +} + +TransitionOptions HillshadeLayer::getHillshadeIlluminationDirectionTransition() const { + return impl().paint.template get<HillshadeIlluminationDirection>().options; +} + +PropertyValue<HillshadeIlluminationAnchorType> HillshadeLayer::getDefaultHillshadeIlluminationAnchor() { + return { HillshadeIlluminationAnchorType::Viewport }; +} + +PropertyValue<HillshadeIlluminationAnchorType> HillshadeLayer::getHillshadeIlluminationAnchor() const { + return impl().paint.template get<HillshadeIlluminationAnchor>().value; +} + +void HillshadeLayer::setHillshadeIlluminationAnchor(PropertyValue<HillshadeIlluminationAnchorType> value) { + if (value == getHillshadeIlluminationAnchor()) + return; + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeIlluminationAnchor>().value = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void HillshadeLayer::setHillshadeIlluminationAnchorTransition(const TransitionOptions& options) { + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeIlluminationAnchor>().options = options; + baseImpl = std::move(impl_); +} + +TransitionOptions HillshadeLayer::getHillshadeIlluminationAnchorTransition() const { + return impl().paint.template get<HillshadeIlluminationAnchor>().options; +} + +PropertyValue<float> HillshadeLayer::getDefaultHillshadeExaggeration() { + return { 0.5 }; +} + +PropertyValue<float> HillshadeLayer::getHillshadeExaggeration() const { + return impl().paint.template get<HillshadeExaggeration>().value; +} + +void HillshadeLayer::setHillshadeExaggeration(PropertyValue<float> value) { + if (value == getHillshadeExaggeration()) + return; + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeExaggeration>().value = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void HillshadeLayer::setHillshadeExaggerationTransition(const TransitionOptions& options) { + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeExaggeration>().options = options; + baseImpl = std::move(impl_); +} + +TransitionOptions HillshadeLayer::getHillshadeExaggerationTransition() const { + return impl().paint.template get<HillshadeExaggeration>().options; +} + +PropertyValue<Color> HillshadeLayer::getDefaultHillshadeShadowColor() { + return { Color::black() }; +} + +PropertyValue<Color> HillshadeLayer::getHillshadeShadowColor() const { + return impl().paint.template get<HillshadeShadowColor>().value; +} + +void HillshadeLayer::setHillshadeShadowColor(PropertyValue<Color> value) { + if (value == getHillshadeShadowColor()) + return; + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeShadowColor>().value = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void HillshadeLayer::setHillshadeShadowColorTransition(const TransitionOptions& options) { + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeShadowColor>().options = options; + baseImpl = std::move(impl_); +} + +TransitionOptions HillshadeLayer::getHillshadeShadowColorTransition() const { + return impl().paint.template get<HillshadeShadowColor>().options; +} + +PropertyValue<Color> HillshadeLayer::getDefaultHillshadeHighlightColor() { + return { Color::white() }; +} + +PropertyValue<Color> HillshadeLayer::getHillshadeHighlightColor() const { + return impl().paint.template get<HillshadeHighlightColor>().value; +} + +void HillshadeLayer::setHillshadeHighlightColor(PropertyValue<Color> value) { + if (value == getHillshadeHighlightColor()) + return; + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeHighlightColor>().value = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void HillshadeLayer::setHillshadeHighlightColorTransition(const TransitionOptions& options) { + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeHighlightColor>().options = options; + baseImpl = std::move(impl_); +} + +TransitionOptions HillshadeLayer::getHillshadeHighlightColorTransition() const { + return impl().paint.template get<HillshadeHighlightColor>().options; +} + +PropertyValue<Color> HillshadeLayer::getDefaultHillshadeAccentColor() { + return { Color::black() }; +} + +PropertyValue<Color> HillshadeLayer::getHillshadeAccentColor() const { + return impl().paint.template get<HillshadeAccentColor>().value; +} + +void HillshadeLayer::setHillshadeAccentColor(PropertyValue<Color> value) { + if (value == getHillshadeAccentColor()) + return; + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeAccentColor>().value = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void HillshadeLayer::setHillshadeAccentColorTransition(const TransitionOptions& options) { + auto impl_ = mutableImpl(); + impl_->paint.template get<HillshadeAccentColor>().options = options; + baseImpl = std::move(impl_); +} + +TransitionOptions HillshadeLayer::getHillshadeAccentColorTransition() const { + return impl().paint.template get<HillshadeAccentColor>().options; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/layers/hillshade_layer_impl.cpp b/src/mbgl/style/layers/hillshade_layer_impl.cpp new file mode 100644 index 0000000000..ed5aa922bf --- /dev/null +++ b/src/mbgl/style/layers/hillshade_layer_impl.cpp @@ -0,0 +1,11 @@ +#include <mbgl/style/layers/hillshade_layer_impl.hpp> + +namespace mbgl { +namespace style { + +bool HillshadeLayer::Impl::hasLayoutDifference(const Layer::Impl&) const { + return false; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/layers/hillshade_layer_impl.hpp b/src/mbgl/style/layers/hillshade_layer_impl.hpp new file mode 100644 index 0000000000..5618b7dfe2 --- /dev/null +++ b/src/mbgl/style/layers/hillshade_layer_impl.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include <mbgl/style/layer_impl.hpp> +#include <mbgl/style/layers/hillshade_layer.hpp> +#include <mbgl/style/layers/hillshade_layer_properties.hpp> + +namespace mbgl { +namespace style { + +class HillshadeLayer::Impl : public Layer::Impl { +public: + using Layer::Impl::Impl; + + bool hasLayoutDifference(const Layer::Impl&) const override; + void stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>&) const override; + + HillshadePaintProperties::Transitionable paint; +}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/layers/hillshade_layer_properties.cpp b/src/mbgl/style/layers/hillshade_layer_properties.cpp new file mode 100644 index 0000000000..f296ab4520 --- /dev/null +++ b/src/mbgl/style/layers/hillshade_layer_properties.cpp @@ -0,0 +1,9 @@ +// This file is generated. Edit scripts/generate-style-code.js, then run `make style-code`. + +#include <mbgl/style/layers/hillshade_layer_properties.hpp> + +namespace mbgl { +namespace style { + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/layers/hillshade_layer_properties.hpp b/src/mbgl/style/layers/hillshade_layer_properties.hpp new file mode 100644 index 0000000000..260d7ea808 --- /dev/null +++ b/src/mbgl/style/layers/hillshade_layer_properties.hpp @@ -0,0 +1,49 @@ +// This file is generated. Edit scripts/generate-style-code.js, then run `make style-code`. + +#pragma once + +#include <mbgl/style/types.hpp> +#include <mbgl/style/layout_property.hpp> +#include <mbgl/style/paint_property.hpp> +#include <mbgl/style/properties.hpp> +#include <mbgl/programs/attributes.hpp> +#include <mbgl/programs/uniforms.hpp> + +namespace mbgl { +namespace style { + +struct HillshadeIlluminationDirection : PaintProperty<float> { + static float defaultValue() { return 335; } +}; + +struct HillshadeIlluminationAnchor : PaintProperty<HillshadeIlluminationAnchorType> { + static HillshadeIlluminationAnchorType defaultValue() { return HillshadeIlluminationAnchorType::Viewport; } +}; + +struct HillshadeExaggeration : PaintProperty<float> { + static float defaultValue() { return 0.5; } +}; + +struct HillshadeShadowColor : PaintProperty<Color> { + static Color defaultValue() { return Color::black(); } +}; + +struct HillshadeHighlightColor : PaintProperty<Color> { + static Color defaultValue() { return Color::white(); } +}; + +struct HillshadeAccentColor : PaintProperty<Color> { + static Color defaultValue() { return Color::black(); } +}; + +class HillshadePaintProperties : public Properties< + HillshadeIlluminationDirection, + HillshadeIlluminationAnchor, + HillshadeExaggeration, + HillshadeShadowColor, + HillshadeHighlightColor, + HillshadeAccentColor +> {}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/layers/layer.cpp.ejs b/src/mbgl/style/layers/layer.cpp.ejs index 573aabda8b..be44bb353d 100644 --- a/src/mbgl/style/layers/layer.cpp.ejs +++ b/src/mbgl/style/layers/layer.cpp.ejs @@ -59,7 +59,7 @@ const std::string& <%- camelize(type) %>Layer::getSourceID() const { return impl().source; } -<% if (type !== 'raster') { -%> +<% if (type !== 'raster' && type !== 'hillshade') { -%> void <%- camelize(type) %>Layer::setSourceLayer(const std::string& sourceLayer) { auto impl_ = mutableImpl(); impl_->sourceLayer = sourceLayer; diff --git a/src/mbgl/style/sources/raster_dem_source.cpp b/src/mbgl/style/sources/raster_dem_source.cpp new file mode 100644 index 0000000000..dc9feb8eeb --- /dev/null +++ b/src/mbgl/style/sources/raster_dem_source.cpp @@ -0,0 +1,19 @@ +#include <mbgl/style/sources/raster_dem_source.hpp> +#include <mbgl/style/sources/raster_source_impl.hpp> +#include <mbgl/style/source_observer.hpp> +#include <mbgl/style/conversion/json.hpp> +#include <mbgl/style/conversion/tileset.hpp> +#include <mbgl/storage/file_source.hpp> +#include <mbgl/util/mapbox.hpp> + +namespace mbgl { +namespace style { + +RasterDEMSource::RasterDEMSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize) + : RasterSource(std::move(id), urlOrTileset_, tileSize, SourceType::RasterDEM){ +} + + + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source.cpp b/src/mbgl/style/sources/raster_source.cpp index 0a0412a4ed..53f29d660b 100644 --- a/src/mbgl/style/sources/raster_source.cpp +++ b/src/mbgl/style/sources/raster_source.cpp @@ -9,8 +9,8 @@ namespace mbgl { namespace style { -RasterSource::RasterSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize) - : Source(makeMutable<Impl>(std::move(id), tileSize)), +RasterSource::RasterSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize, SourceType sourceType) + : Source(makeMutable<Impl>(sourceType, std::move(id), tileSize)), urlOrTileset(std::move(urlOrTileset_)) { } diff --git a/src/mbgl/style/sources/raster_source_impl.cpp b/src/mbgl/style/sources/raster_source_impl.cpp index 50dae1f07e..4db25aafd1 100644 --- a/src/mbgl/style/sources/raster_source_impl.cpp +++ b/src/mbgl/style/sources/raster_source_impl.cpp @@ -3,8 +3,8 @@ namespace mbgl { namespace style { -RasterSource::Impl::Impl(std::string id_, uint16_t tileSize_) - : Source::Impl(SourceType::Raster, std::move(id_)), +RasterSource::Impl::Impl(SourceType sourceType, std::string id_, uint16_t tileSize_) + : Source::Impl(sourceType, std::move(id_)), tileSize(tileSize_) { } diff --git a/src/mbgl/style/sources/raster_source_impl.hpp b/src/mbgl/style/sources/raster_source_impl.hpp index c41d5485b2..96f59a2159 100644 --- a/src/mbgl/style/sources/raster_source_impl.hpp +++ b/src/mbgl/style/sources/raster_source_impl.hpp @@ -8,7 +8,7 @@ namespace style { class RasterSource::Impl : public Source::Impl { public: - Impl(std::string id, uint16_t tileSize); + Impl(SourceType sourceType, std::string id, uint16_t tileSize); Impl(const Impl&, Tileset); optional<Tileset> getTileset() const; diff --git a/src/mbgl/style/style_impl.cpp b/src/mbgl/style/style_impl.cpp index 3214c6316e..f2a9c0f222 100644 --- a/src/mbgl/style/style_impl.cpp +++ b/src/mbgl/style/style_impl.cpp @@ -9,6 +9,7 @@ #include <mbgl/style/layers/line_layer.hpp> #include <mbgl/style/layers/circle_layer.hpp> #include <mbgl/style/layers/raster_layer.hpp> +#include <mbgl/style/layers/hillshade_layer.hpp> #include <mbgl/style/layer_impl.hpp> #include <mbgl/style/parser.hpp> #include <mbgl/style/transition_options.hpp> diff --git a/src/mbgl/style/types.cpp b/src/mbgl/style/types.cpp index cd5e597fc0..bdfa20a047 100644 --- a/src/mbgl/style/types.cpp +++ b/src/mbgl/style/types.cpp @@ -25,6 +25,11 @@ MBGL_DEFINE_ENUM(TranslateAnchorType, { { TranslateAnchorType::Viewport, "viewport" }, }); +MBGL_DEFINE_ENUM(HillshadeIlluminationAnchorType, { + { HillshadeIlluminationAnchorType::Map, "map" }, + { HillshadeIlluminationAnchorType::Viewport, "viewport" }, +}); + MBGL_DEFINE_ENUM(RotateAnchorType, { { RotateAnchorType::Map, "map" }, { RotateAnchorType::Viewport, "viewport" }, diff --git a/src/mbgl/tile/raster_dem_tile.cpp b/src/mbgl/tile/raster_dem_tile.cpp new file mode 100644 index 0000000000..b270378ece --- /dev/null +++ b/src/mbgl/tile/raster_dem_tile.cpp @@ -0,0 +1,124 @@ +#include <mbgl/tile/raster_dem_tile.hpp> +#include <mbgl/tile/raster_dem_tile_worker.hpp> +#include <mbgl/tile/tile_observer.hpp> +#include <mbgl/tile/tile_loader_impl.hpp> +#include <mbgl/style/source.hpp> +#include <mbgl/storage/resource.hpp> +#include <mbgl/storage/response.hpp> +#include <mbgl/storage/file_source.hpp> +#include <mbgl/renderer/tile_parameters.hpp> +#include <mbgl/renderer/buckets/hillshade_bucket.hpp> +#include <mbgl/actor/scheduler.hpp> + +namespace mbgl { + +RasterDEMTile::RasterDEMTile(const OverscaledTileID& id_, + const TileParameters& parameters, + const Tileset& tileset) + : Tile(id_), + loader(*this, id_, parameters, tileset), + mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())), + worker(parameters.workerScheduler, + ActorRef<RasterDEMTile>(*this, mailbox)) { + + if ( id.canonical.y == 0 ){ + // this tile doesn't have upper neighboring tiles so marked those as backfilled + neighboringTiles = neighboringTiles | DEMTileNeighbors::NoUpper; + } + + if (id.canonical.y + 1 == std::pow(2, id.canonical.z)){ + // this tile doesn't have lower neighboring tiles so marked those as backfilled + neighboringTiles = neighboringTiles | DEMTileNeighbors::NoLower; + } +} + +RasterDEMTile::~RasterDEMTile() = default; + +void RasterDEMTile::setError(std::exception_ptr err) { + loaded = true; + observer->onTileError(*this, err); +} + +void RasterDEMTile::setMetadata(optional<Timestamp> modified_, optional<Timestamp> expires_) { + modified = modified_; + expires = expires_; +} + +void RasterDEMTile::setData(std::shared_ptr<const std::string> data) { + pending = true; + ++correlationID; + worker.invoke(&RasterDEMTileWorker::parse, data, correlationID); +} + +void RasterDEMTile::onParsed(std::unique_ptr<HillshadeBucket> result, const uint64_t resultCorrelationID) { + bucket = std::move(result); + loaded = true; + if (resultCorrelationID == correlationID) { + pending = false; + } + renderable = bucket ? true : false; + observer->onTileChanged(*this); +} + +void RasterDEMTile::onError(std::exception_ptr err, const uint64_t resultCorrelationID) { + loaded = true; + if (resultCorrelationID == correlationID) { + pending = false; + } + observer->onTileError(*this, err); +} + +void RasterDEMTile::upload(gl::Context& context) { + if (bucket) { + bucket->upload(context); + } +} + + +Bucket* RasterDEMTile::getBucket(const style::Layer::Impl&) const { + return bucket.get(); +} + +HillshadeBucket* RasterDEMTile::getBucket() const { + return bucket.get(); +} + +void RasterDEMTile::backfillBorder(const RasterDEMTile& borderTile, const DEMTileNeighbors mask) { + int32_t dx = borderTile.id.canonical.x - id.canonical.x; + const int8_t dy = borderTile.id.canonical.y - id.canonical.y; + const uint32_t dim = pow(2, id.canonical.z); + if (dx == 0 && dy == 0) return; + if (std::abs(dy) > 1) return; + // neighbor is in another world wrap + if (std::abs(dx) > 1) { + if (std::abs(int(dx + dim)) == 1) { + dx += dim; + } else if (std::abs(int(dx - dim)) == 1) { + dx -= dim; + } + } + const HillshadeBucket* borderBucket = borderTile.getBucket(); + if (borderBucket) { + const DEMData& borderDEM = borderBucket->getDEMData(); + DEMData& tileDEM = bucket->getDEMData(); + + tileDEM.backfillBorder(borderDEM, dx, dy); + // update the bitmask to indicate that this tiles have been backfilled by flipping the relevant bit + this->neighboringTiles = this->neighboringTiles | mask; + // mark HillshadeBucket.prepared as false so it runs through the prepare render pass + // with the new texture data we just backfilled + bucket->setPrepared(false); + } +} + +void RasterDEMTile::setMask(TileMask&& mask) { + if (bucket) { + bucket->setMask(std::move(mask)); + } +} + +void RasterDEMTile::setNecessity(TileNecessity necessity) { + loader.setNecessity(necessity); +} + +} // namespace mbgl diff --git a/src/mbgl/tile/raster_dem_tile.hpp b/src/mbgl/tile/raster_dem_tile.hpp new file mode 100644 index 0000000000..68f8a91e00 --- /dev/null +++ b/src/mbgl/tile/raster_dem_tile.hpp @@ -0,0 +1,104 @@ +#pragma once + +#include <mbgl/tile/tile.hpp> +#include <mbgl/tile/tile_loader.hpp> +#include <mbgl/tile/raster_dem_tile_worker.hpp> +#include <mbgl/actor/actor.hpp> + +namespace mbgl { + +class Tileset; +class TileParameters; +class HillshadeBucket; + +enum class DEMTileNeighbors : uint8_t { + // 0b00000000 + Empty = 0 << 1, + + // 0b00000001 + Left = 1 << 0, + // 0b00000010 + Right = 1 << 1, + // 0b00000100 + TopLeft = 1 << 2, + // 0b00001000 + TopCenter = 1 << 3, + // 0b00010000 + TopRight = 1 << 4, + // 0b00100000 + BottomLeft = 1 << 5, + // 0b01000000 + BottomCenter = 1 << 6, + // 0b10000000 + BottomRight = 1 << 7, + + // helper enums for tiles with no upper/lower neighbors + // and completely backfilled tiles + + // 0b00011100 + NoUpper = 0b00011100, + // 0b11100000 + NoLower = 0b11100000, + // 0b11111111 + Complete = 0b11111111 +}; + +inline DEMTileNeighbors operator|(DEMTileNeighbors a, DEMTileNeighbors b) { + return static_cast<DEMTileNeighbors>(int(a) | int(b)); +}; + +inline DEMTileNeighbors operator&(DEMTileNeighbors a, DEMTileNeighbors b) { + return static_cast<DEMTileNeighbors>(int(a) & int(b)); +} + +inline bool operator!=(DEMTileNeighbors a, DEMTileNeighbors b) { + return static_cast<unsigned char>(a) != static_cast<unsigned char>(b); +} + +namespace style { +class Layer; +} // namespace style + +class RasterDEMTile : public Tile { +public: + RasterDEMTile(const OverscaledTileID&, + const TileParameters&, + const Tileset&); + ~RasterDEMTile() override; + + void setNecessity(TileNecessity) final; + + void setError(std::exception_ptr); + void setMetadata(optional<Timestamp> modified, optional<Timestamp> expires); + void setData(std::shared_ptr<const std::string> data); + + void upload(gl::Context&) override; + Bucket* getBucket(const style::Layer::Impl&) const override; + + HillshadeBucket* getBucket() const; + void backfillBorder(const RasterDEMTile& borderTile, const DEMTileNeighbors mask); + + // neighboringTiles is a bitmask for which neighboring tiles have been backfilled + // there are max 8 possible neighboring tiles, so each bit represents one neighbor + DEMTileNeighbors neighboringTiles = DEMTileNeighbors::Empty; + + void setMask(TileMask&&) override; + + void onParsed(std::unique_ptr<HillshadeBucket> result, uint64_t correlationID); + void onError(std::exception_ptr, uint64_t correlationID); + +private: + TileLoader<RasterDEMTile> loader; + + std::shared_ptr<Mailbox> mailbox; + Actor<RasterDEMTileWorker> worker; + + uint64_t correlationID = 0; + + // Contains the Bucket object for the tile. Buckets are render + // objects and they get added by tile parsing operations. + std::unique_ptr<HillshadeBucket> bucket; + +}; + +} // namespace mbgl diff --git a/src/mbgl/tile/raster_dem_tile_worker.cpp b/src/mbgl/tile/raster_dem_tile_worker.cpp new file mode 100644 index 0000000000..ed8573788f --- /dev/null +++ b/src/mbgl/tile/raster_dem_tile_worker.cpp @@ -0,0 +1,27 @@ +#include <mbgl/tile/raster_dem_tile_worker.hpp> +#include <mbgl/tile/raster_dem_tile.hpp> +#include <mbgl/renderer/buckets/hillshade_bucket.hpp> +#include <mbgl/actor/actor.hpp> +#include <mbgl/util/premultiply.hpp> + +namespace mbgl { + +RasterDEMTileWorker::RasterDEMTileWorker(ActorRef<RasterDEMTileWorker>, ActorRef<RasterDEMTile> parent_) + : parent(std::move(parent_)) { +} + +void RasterDEMTileWorker::parse(std::shared_ptr<const std::string> data, uint64_t correlationID) { + if (!data) { + parent.invoke(&RasterDEMTile::onParsed, nullptr, correlationID); // No data; empty tile. + return; + } + + try { + auto bucket = std::make_unique<HillshadeBucket>(decodeImage(*data)); + parent.invoke(&RasterDEMTile::onParsed, std::move(bucket), correlationID); + } catch (...) { + parent.invoke(&RasterDEMTile::onError, std::current_exception(), correlationID); + } +} + +} // namespace mbgl diff --git a/src/mbgl/tile/raster_dem_tile_worker.hpp b/src/mbgl/tile/raster_dem_tile_worker.hpp new file mode 100644 index 0000000000..14fd1f43b5 --- /dev/null +++ b/src/mbgl/tile/raster_dem_tile_worker.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include <mbgl/actor/actor_ref.hpp> + +#include <memory> +#include <string> + +namespace mbgl { + +class RasterDEMTile; + +class RasterDEMTileWorker { +public: + RasterDEMTileWorker(ActorRef<RasterDEMTileWorker>, ActorRef<RasterDEMTile>); + + void parse(std::shared_ptr<const std::string> data, uint64_t correlationID); + +private: + ActorRef<RasterDEMTile> parent; +}; + +} // namespace mbgl diff --git a/src/mbgl/tile/raster_tile.cpp b/src/mbgl/tile/raster_tile.cpp index 85fcea77b7..ff23d4493e 100644 --- a/src/mbgl/tile/raster_tile.cpp +++ b/src/mbgl/tile/raster_tile.cpp @@ -24,9 +24,6 @@ RasterTile::RasterTile(const OverscaledTileID& id_, RasterTile::~RasterTile() = default; -void RasterTile::cancel() { -} - void RasterTile::setError(std::exception_ptr err) { loaded = true; observer->onTileError(*this, err); diff --git a/src/mbgl/tile/raster_tile.hpp b/src/mbgl/tile/raster_tile.hpp index 192769ed8f..e25329119a 100644 --- a/src/mbgl/tile/raster_tile.hpp +++ b/src/mbgl/tile/raster_tile.hpp @@ -20,7 +20,7 @@ public: RasterTile(const OverscaledTileID&, const TileParameters&, const Tileset&); - ~RasterTile() final; + ~RasterTile() override; void setNecessity(TileNecessity) final; @@ -28,8 +28,6 @@ public: void setMetadata(optional<Timestamp> modified, optional<Timestamp> expires); void setData(std::shared_ptr<const std::string> data); - void cancel() override; - void upload(gl::Context&) override; Bucket* getBucket(const style::Layer::Impl&) const override; diff --git a/src/mbgl/tile/tile.cpp b/src/mbgl/tile/tile.cpp index 85899a98cb..88db2ba07c 100644 --- a/src/mbgl/tile/tile.cpp +++ b/src/mbgl/tile/tile.cpp @@ -18,6 +18,9 @@ void Tile::setObserver(TileObserver* observer_) { observer = observer_; } +void Tile::cancel() { +} + void Tile::setTriedCache() { triedOptional = true; observer->onTileChanged(*this); diff --git a/src/mbgl/tile/tile.hpp b/src/mbgl/tile/tile.hpp index 1bed180f3d..23365c6ae3 100644 --- a/src/mbgl/tile/tile.hpp +++ b/src/mbgl/tile/tile.hpp @@ -43,7 +43,7 @@ public: virtual void setNecessity(TileNecessity) {} // Mark this tile as no longer needed and cancel any pending work. - virtual void cancel() = 0; + virtual void cancel(); virtual void upload(gl::Context&) = 0; virtual Bucket* getBucket(const style::Layer::Impl&) const = 0; diff --git a/src/mbgl/tile/tile_cache.cpp b/src/mbgl/tile/tile_cache.cpp index 3fafb1259c..463d397608 100644 --- a/src/mbgl/tile/tile_cache.cpp +++ b/src/mbgl/tile/tile_cache.cpp @@ -33,13 +33,22 @@ void TileCache::add(const OverscaledTileID& key, std::unique_ptr<Tile> tile) { // purge oldest key/tile if necessary if (orderedKeys.size() > size) { - get(orderedKeys.front()); + pop(orderedKeys.front()); } assert(orderedKeys.size() <= size); } -std::unique_ptr<Tile> TileCache::get(const OverscaledTileID& key) { +Tile* TileCache::get(const OverscaledTileID& key) { + auto it = tiles.find(key); + if (it != tiles.end()) { + return it->second.get(); + } else { + return nullptr; + } +} + +std::unique_ptr<Tile> TileCache::pop(const OverscaledTileID& key) { std::unique_ptr<Tile> tile; diff --git a/src/mbgl/tile/tile_cache.hpp b/src/mbgl/tile/tile_cache.hpp index 80fe98a20c..88358b8cdc 100644 --- a/src/mbgl/tile/tile_cache.hpp +++ b/src/mbgl/tile/tile_cache.hpp @@ -17,7 +17,8 @@ public: void setSize(size_t); size_t getSize() const { return size; }; void add(const OverscaledTileID& key, std::unique_ptr<Tile> data); - std::unique_ptr<Tile> get(const OverscaledTileID& key); + std::unique_ptr<Tile> pop(const OverscaledTileID& key); + Tile* get(const OverscaledTileID& key); bool has(const OverscaledTileID& key); void clear(); diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 802b527a26..cdd51a293d 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -133,7 +133,7 @@ canonicalizeTileURL(const std::string& str, const style::SourceType type, const std::string result = "mapbox://tiles/"; result.append(str, path.directory.first + versionLen, path.directory.second - versionLen); result.append(str, path.filename.first, path.filename.second); - if (type == style::SourceType::Raster) { + if (type == style::SourceType::Raster || type == style::SourceType::RasterDEM) { result += tileSize == util::tileSize ? "@2x" : "{ratio}"; } |