From 7afd82172000f96859adf31c6500c748f1e5594e Mon Sep 17 00:00:00 2001 From: mwilsnd <53413200+mwilsnd@users.noreply.github.com> Date: Thu, 17 Aug 2023 13:23:50 -0400 Subject: [PATCH] Drawable PR 3 - Layers (#1501) Co-authored-by: Tim Sylvester Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- BUILD.bazel | 26 ++++++++++++------- include/mbgl/gfx/drawable_builder.hpp | 1 - .../shaders/gl/drawable_hillshade_prepare.hpp | 2 +- include/mbgl/shaders/gl/drawable_line_sdf.hpp | 4 +-- .../drawable.hillshade_prepare.fragment.glsl | 2 +- shaders/drawable.line_sdf.fragment.glsl | 2 +- shaders/drawable.line_sdf.vertex.glsl | 2 +- .../renderer/layers/render_heatmap_layer.cpp | 24 ++++++----------- .../renderer/layers/render_line_layer.cpp | 18 +++---------- src/mbgl/renderer/render_layer.cpp | 22 ++++++++++++++-- src/mbgl/renderer/render_layer.hpp | 13 +++++++++- 11 files changed, 66 insertions(+), 50 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 7dd819a9b..5362a9514 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,6 +1,16 @@ load("//bazel:flags.bzl", "CPP_FLAGS", "MAPLIBRE_FLAGS") load("@bazel_skylib//rules:common_settings.bzl", "string_flag") -load("//bazel:core.bzl", +load( + "//bazel:core.bzl", + "MLN_CORE_HEADERS", + "MLN_CORE_SOURCE", + "MLN_DRAWABLES_SOURCE", + "MLN_DRAWABLES_HEADERS", + "MLN_GENERATED_SHADER_HEADERS", + "MLN_GENERATED_STYLE_SOURCE", + "MLN_OPENGL_HEADERS", + "MLN_OPENGL_SOURCE", + "MLN_PRIVATE_GENERATED_STYLE_HEADERS", "MLN_PUBLIC_GENERATED_STYLE_HEADERS", "MLN_PRIVATE_GENERATED_STYLE_HEADERS", "MLN_GENERATED_STYLE_SOURCE", @@ -83,17 +93,15 @@ cc_library( # the prefix from the path. cc_library( name = "mbgl-core", - srcs = MLN_CORE_SOURCE - + PREFIXED["MLN_GENERATED_STYLE_SOURCE"] - + MLN_OPENGL_SOURCE - + select({ + srcs = MLN_CORE_SOURCE + + MLN_GENERATED_STYLE_SOURCE + + MLN_OPENGL_SOURCE + select({ ":drawable_renderer": MLN_DRAWABLES_SOURCE, ":split_renderer": MLN_DRAWABLES_SOURCE, - "//conditions:default": [] + "//conditions:default": [], }), - hdrs = MLN_CORE_HEADERS - + MLN_OPENGL_HEADERS - + select({ + hdrs = MLN_CORE_HEADERS + + MLN_OPENGL_HEADERS + select({ ":drawable_renderer": MLN_DRAWABLES_HEADERS, ":split_renderer": MLN_DRAWABLES_HEADERS, "//conditions:default": [] diff --git a/include/mbgl/gfx/drawable_builder.hpp b/include/mbgl/gfx/drawable_builder.hpp index 80614c576..7803c6596 100644 --- a/include/mbgl/gfx/drawable_builder.hpp +++ b/include/mbgl/gfx/drawable_builder.hpp @@ -148,7 +148,6 @@ public: /// Add a triangle void addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); - /// Add another triangle based on the previous two points void appendTriangle(int16_t x0, int16_t y0); diff --git a/include/mbgl/shaders/gl/drawable_hillshade_prepare.hpp b/include/mbgl/shaders/gl/drawable_hillshade_prepare.hpp index 8cfbb8677..f08cc8b92 100644 --- a/include/mbgl/shaders/gl/drawable_hillshade_prepare.hpp +++ b/include/mbgl/shaders/gl/drawable_hillshade_prepare.hpp @@ -87,7 +87,7 @@ void main() { // 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 - u_maxzoom) * a) where a is an arbitrary value - // Here we use a=0.3 which works out to the expression below. see + // 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; diff --git a/include/mbgl/shaders/gl/drawable_line_sdf.hpp b/include/mbgl/shaders/gl/drawable_line_sdf.hpp index 43c2fb016..37c641081 100644 --- a/include/mbgl/shaders/gl/drawable_line_sdf.hpp +++ b/include/mbgl/shaders/gl/drawable_line_sdf.hpp @@ -56,7 +56,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; lowp float u_floorwidth_t; - + highp float pad2; }; @@ -216,7 +216,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; lowp float u_floorwidth_t; - + highp float pad2; }; diff --git a/shaders/drawable.hillshade_prepare.fragment.glsl b/shaders/drawable.hillshade_prepare.fragment.glsl index 49502d917..ea9be647d 100644 --- a/shaders/drawable.hillshade_prepare.fragment.glsl +++ b/shaders/drawable.hillshade_prepare.fragment.glsl @@ -56,7 +56,7 @@ void main() { // 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 - u_maxzoom) * a) where a is an arbitrary value - // Here we use a=0.3 which works out to the expression below. see + // 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; diff --git a/shaders/drawable.line_sdf.fragment.glsl b/shaders/drawable.line_sdf.fragment.glsl index 027ce2728..c15478885 100644 --- a/shaders/drawable.line_sdf.fragment.glsl +++ b/shaders/drawable.line_sdf.fragment.glsl @@ -32,7 +32,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; lowp float u_floorwidth_t; - + highp float pad2; }; diff --git a/shaders/drawable.line_sdf.vertex.glsl b/shaders/drawable.line_sdf.vertex.glsl index 6629e8b0f..879857830 100644 --- a/shaders/drawable.line_sdf.vertex.glsl +++ b/shaders/drawable.line_sdf.vertex.glsl @@ -46,7 +46,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; lowp float u_floorwidth_t; - + highp float pad2; }; diff --git a/src/mbgl/renderer/layers/render_heatmap_layer.cpp b/src/mbgl/renderer/layers/render_heatmap_layer.cpp index 92207547e..ec8facf72 100644 --- a/src/mbgl/renderer/layers/render_heatmap_layer.cpp +++ b/src/mbgl/renderer/layers/render_heatmap_layer.cpp @@ -202,23 +202,15 @@ void RenderHeatmapLayer::render(PaintParameters& parameters) { #endif // MLN_LEGACY_RENDERER void RenderHeatmapLayer::updateColorRamp() { - auto colorValue = unevaluated.get().getValue(); - if (colorValue.isUndefined()) { - colorValue = HeatmapLayer::getDefaultHeatmapColor(); - } + if (colorRamp) { + auto colorValue = unevaluated.get().getValue(); + if (colorValue.isUndefined()) { + colorValue = HeatmapLayer::getDefaultHeatmapColor(); + } - const auto length = (*colorRamp).bytes(); - - for (uint32_t i = 0; i < length; i += 4) { - const auto color = colorValue.evaluate(static_cast(i) / length); - (*colorRamp).data[i + 0] = static_cast(std::floor(color.r * 255.f)); - (*colorRamp).data[i + 1] = static_cast(std::floor(color.g * 255.f)); - (*colorRamp).data[i + 2] = static_cast(std::floor(color.b * 255.f)); - (*colorRamp).data[i + 3] = static_cast(std::floor(color.a * 255.f)); - } - - if (colorRampTexture) { - colorRampTexture = std::nullopt; + if (applyColorRamp(colorValue, *colorRamp)) { + colorRampTexture = std::nullopt; + } } } diff --git a/src/mbgl/renderer/layers/render_line_layer.cpp b/src/mbgl/renderer/layers/render_line_layer.cpp index c9cfb04ba..7c4757deb 100644 --- a/src/mbgl/renderer/layers/render_line_layer.cpp +++ b/src/mbgl/renderer/layers/render_line_layer.cpp @@ -306,24 +306,12 @@ bool RenderLineLayer::queryIntersectsFeature(const GeometryCoordinates& queryGeo } void RenderLineLayer::updateColorRamp() { - auto colorValue = unevaluated.get().getValue(); - if (colorValue.isUndefined()) { + const style::ColorRampPropertyValue colorValue = unevaluated.get().getValue(); + if (!colorRamp || !applyColorRamp(colorValue, *colorRamp)) { return; } - const auto length = colorRamp->bytes(); - - for (uint32_t i = 0; i < length; i += 4) { - const auto color = colorValue.evaluate(static_cast(i) / length); - colorRamp->data[i] = static_cast(std::floor(color.r * 255.f)); - colorRamp->data[i + 1] = static_cast(std::floor(color.g * 255.f)); - colorRamp->data[i + 2] = static_cast(std::floor(color.b * 255.f)); - colorRamp->data[i + 3] = static_cast(std::floor(color.a * 255.f)); - } - - if (colorRampTexture) { - colorRampTexture = std::nullopt; - } + colorRampTexture = std::nullopt; #if MLN_DRAWABLE_RENDERER if (colorRampTexture2D) { diff --git a/src/mbgl/renderer/render_layer.cpp b/src/mbgl/renderer/render_layer.cpp index 8b061ef8f..63841c282 100644 --- a/src/mbgl/renderer/render_layer.cpp +++ b/src/mbgl/renderer/render_layer.cpp @@ -1,13 +1,14 @@ #include +#include #include #include #include -#include +#include #include #include +#include #include -#include #include #if MLN_DRAWABLE_RENDERER @@ -223,4 +224,21 @@ void RenderLayer::activateLayerGroup(const LayerGroupBasePtr& layerGroup_, } #endif +bool RenderLayer::applyColorRamp(const style::ColorRampPropertyValue& colorValue, PremultipliedImage& image) { + if (colorValue.isUndefined()) { + return false; + } + + const auto length = image.bytes(); + + for (uint32_t i = 0; i < length; i += 4) { + const auto color = colorValue.evaluate(static_cast(i) / length); + image.data[i + 0] = static_cast(std::floor(color.r * 255.f)); + image.data[i + 1] = static_cast(std::floor(color.g * 255.f)); + image.data[i + 2] = static_cast(std::floor(color.b * 255.f)); + image.data[i + 3] = static_cast(std::floor(color.a * 255.f)); + } + return true; +} + } // namespace mbgl diff --git a/src/mbgl/renderer/render_layer.hpp b/src/mbgl/renderer/render_layer.hpp index 0cb6bda0c..ce23d04a9 100644 --- a/src/mbgl/renderer/render_layer.hpp +++ b/src/mbgl/renderer/render_layer.hpp @@ -51,6 +51,10 @@ using UniformBufferPtr = std::shared_ptr; #endif } // namespace gfx +namespace style { +class ColorRampPropertyValue; +} // namespace style + class LayerRenderData { public: std::shared_ptr bucket; @@ -212,13 +216,20 @@ protected: bool hasRenderTile(const OverscaledTileID&) const; /// Get the bucket ID from which a given tile was built + /// @details When a new style is loaded and contains a layer with the same ID, `layerChanged` will be called during + /// style + /// parsing, but the `Bucket` in a tile's `RenderData` will only be replaced when the asynchronous load for + /// the tile is complete, at which point the drawable for the tile may need to be updated or replaced. util::SimpleIdentity getRenderTileBucketID(const OverscaledTileID&) const; /// Set the bucket ID from which a given tile was built - /// @return true if updated, false if missing or unchanged + /// @return true if updated, false if the tile ID is not present in the set of tiles to be rendered or the ID is + /// unchanged bool setRenderTileBucketID(const OverscaledTileID&, util::SimpleIdentity bucketID); #endif // MLN_DRAWABLE_RENDERER + static bool applyColorRamp(const style::ColorRampPropertyValue&, PremultipliedImage&); + protected: // Stores current set of tiles to be rendered for this layer. RenderTiles renderTiles;