Skip to content

Commit

Permalink
Custom Drawable Layer v1 + DrawableBuilder thick lines (#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankarschti authored Oct 13, 2023
1 parent 6a8be9a commit 89b9c39
Show file tree
Hide file tree
Showing 56 changed files with 1,638 additions and 618 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_builder.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_builder_impl.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_builder_impl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_atlases_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_custom_layer_host_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/hillshade_prepare_drawable_data.hpp
Expand Down Expand Up @@ -236,6 +237,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/annotation/annotation.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/backend_scope.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/gfx_types.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/polyline_generator.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/renderable.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/renderer_backend.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/rendering_stats.hpp
Expand Down Expand Up @@ -466,6 +468,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/index_buffer.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/index_vector.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/offscreen_texture.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/polyline_generator.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/program.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/render_pass.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/renderbuffer.hpp
Expand Down Expand Up @@ -1115,6 +1118,8 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/background_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/circle_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/collision_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/layers/custom_drawable_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/layermanager/custom_drawable_layer_factory.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/fill_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/heatmap_layer_ubo.hpp
Expand Down Expand Up @@ -1143,6 +1148,12 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/src/mbgl/gl/uniform_block_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/uniform_buffer_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/vertex_attribute_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_drawable_layer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/layermanager/custom_drawable_layer_factory.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_drawable_layer_impl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_drawable_layer_impl.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/render_custom_drawable_layer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/render_custom_drawable_layer.hpp
)
endif()
endif()
Expand Down
13 changes: 12 additions & 1 deletion bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ MLN_CORE_SOURCE = [
"src/mbgl/util/premultiply.cpp",
"src/mbgl/util/quaternion.cpp",
"src/mbgl/util/quaternion.hpp",
"src/mbgl/gfx/polyline_generator.cpp",
"src/mbgl/util/rapidjson.cpp",
"src/mbgl/util/rapidjson.hpp",
"src/mbgl/util/rect.hpp",
Expand Down Expand Up @@ -818,6 +819,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/monotonic_timer.hpp",
"include/mbgl/util/noncopyable.hpp",
"include/mbgl/util/platform.hpp",
"include/mbgl/gfx/polyline_generator.hpp",
"include/mbgl/util/premultiply.hpp",
"include/mbgl/util/projection.hpp",
"include/mbgl/util/range.hpp",
Expand Down Expand Up @@ -900,6 +902,7 @@ MLN_DRAWABLES_SOURCE = [
"src/mbgl/gfx/drawable.cpp",
"src/mbgl/gfx/drawable_builder.cpp",
"src/mbgl/gfx/drawable_builder_impl.hpp",
"src/mbgl/gfx/drawable_builder_impl.cpp",
"src/mbgl/gfx/drawable_atlases_tweaker.cpp",
"src/mbgl/gfx/drawable_custom_layer_host_tweaker.cpp",
"src/mbgl/gfx/hillshade_prepare_drawable_data.hpp",
Expand Down Expand Up @@ -941,6 +944,12 @@ MLN_DRAWABLES_SOURCE = [
"src/mbgl/renderer/layers/collision_layer_tweaker.hpp",
"src/mbgl/shaders/shader_program_base.cpp",
"src/mbgl/util/identity.cpp",
"src/mbgl/style/layers/custom_drawable_layer.cpp",
"src/mbgl/layermanager/custom_drawable_layer_factory.cpp",
"src/mbgl/style/layers/custom_drawable_layer_impl.cpp",
"src/mbgl/style/layers/custom_drawable_layer_impl.hpp",
"src/mbgl/renderer/layers/render_custom_drawable_layer.cpp",
"src/mbgl/renderer/layers/render_custom_drawable_layer.hpp",
]

MLN_DRAWABLES_HEADERS = [
Expand Down Expand Up @@ -974,7 +983,9 @@ MLN_DRAWABLES_HEADERS = [
"include/mbgl/shaders/shader_program_base.hpp",
"include/mbgl/shaders/symbol_layer_ubo.hpp",
"include/mbgl/util/identity.hpp",
"include/mbgl/util/suppress_copies.hpp"
"include/mbgl/util/suppress_copies.hpp",
"include/mbgl/style/layers/custom_drawable_layer.hpp",
"include/mbgl/layermanager/custom_drawable_layer_factory.hpp",
]

MLN_DRAWABLES_GL_SOURCE = [
Expand Down
28 changes: 17 additions & 11 deletions include/mbgl/gfx/drawable_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/gfx/types.hpp>
#include <mbgl/util/string_indexer.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/gfx/polyline_generator.hpp>

#include <array>
#include <memory>
Expand Down Expand Up @@ -154,14 +157,6 @@ class DrawableBuilder {
/// Clear the tweaker collection
void clearTweakers() { tweakers.clear(); }

/// 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);

/// Add a rectangle consisting of two triangles
void addQuad(int16_t x0, int16_t y0, int16_t x1, int16_t y1);

/// A set of attribute values to be added for each vertex
void setVertexAttributes(const VertexAttributeArray& value);
void setVertexAttributes(VertexAttributeArray&&);
Expand All @@ -180,6 +175,17 @@ class DrawableBuilder {
/// Set shared indices and segments
void setSegments(gfx::DrawMode, gfx::IndexVectorBasePtr, const SegmentBase*, std::size_t segmentCount);

/// Create a segment wrapper
virtual std::unique_ptr<Drawable::DrawSegment> createSegment(gfx::DrawMode, SegmentBase&&) = 0;

/// 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);

/// Add a rectangle consisting of two triangles
void addQuad(int16_t x0, int16_t y0, int16_t x1, int16_t y1);

/// Add lines based on existing vertices
void addLines(const std::vector<uint16_t>& indexes,
std::size_t indexOffset,
Expand All @@ -192,8 +198,8 @@ class DrawableBuilder {
std::size_t indexLength,
std::size_t baseIndex = 0);

/// Create a segment wrapper
virtual std::unique_ptr<Drawable::DrawSegment> createSegment(gfx::DrawMode, SegmentBase&&) = 0;
/// Add a polyline. If the last point equals the first it will be closed, otherwise open
void addPolyline(const GeometryCoordinates& coordinates, const gfx::PolylineGeneratorOptions&);

protected:
std::size_t curVertexCount() const;
Expand Down Expand Up @@ -224,7 +230,7 @@ class DrawableBuilder {
gfx::Drawable::Textures textures;
std::vector<DrawableTweakerPtr> tweakers;

struct Impl;
class Impl;
std::unique_ptr<Impl> impl;
};

Expand Down
103 changes: 103 additions & 0 deletions include/mbgl/gfx/polyline_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/gfx/vertex_vector.hpp>
#include <mbgl/gfx/index_vector.hpp>
#include <mbgl/programs/segment.hpp>
#include <mbgl/util/math.hpp>

#include <cstdint>
#include <cstddef>
#include <memory>
#include <vector>
#include <functional>
#include <optional>

namespace mbgl {
namespace gfx {

class PolylineGeneratorDistances {
public:
PolylineGeneratorDistances(double clipStart_, double clipEnd_, double total_)
: clipStart(clipStart_),
clipEnd(clipEnd_),
total(total_) {}

// Scale line distance from tile units to [0, 2^15).
double scaleToMaxLineDistance(double tileDistance) const;

private:
double clipStart;
double clipEnd;
double total;
};

struct PolylineGeneratorOptions {
FeatureType type{FeatureType::LineString};
style::LineJoinType joinType{style::LineJoinType::Miter};
float miterLimit{2.f};
style::LineCapType beginCap{style::LineCapType::Butt};
style::LineCapType endCap{style::LineCapType::Butt};
float roundLimit{1.f};
uint32_t overscaling{1}; // TODO: what is this???
std::optional<PolylineGeneratorDistances> lineDistances;
};

template <class PolylineLayoutVertex, class PolylineSegment>
class PolylineGenerator {
public:
using Vertices = gfx::VertexVector<PolylineLayoutVertex>;
using Segments = std::vector<PolylineSegment>;
using LayoutVertexFunc = std::function<PolylineLayoutVertex(
Point<int16_t> p, Point<double> e, bool round, bool up, int8_t dir, int32_t linesofar /*= 0*/)>;
using CreateSegmentFunc = std::function<PolylineSegment(std::size_t vertexOffset, std::size_t indexOffset)>;
using GetSegmentFunc = std::function<mbgl::SegmentBase&(PolylineSegment& segment)>;
using Indexes = gfx::IndexVector<gfx::Triangles>;

public:
PolylineGenerator(Vertices& polylineVertices,
LayoutVertexFunc layoutVertexFunc,
Segments& polylineSegments,
CreateSegmentFunc createSegmentFunc,
GetSegmentFunc getSegmentFunc,
Indexes& polylineIndexes);
~PolylineGenerator() = default;

void generate(const GeometryCoordinates& coordinates, const PolylineGeneratorOptions& options);

private:
struct TriangleElement;

void addCurrentVertex(const GeometryCoordinate& currentCoordinate,
double& distance,
const Point<double>& normal,
double endLeft,
double endRight,
bool round,
std::size_t startVertex,
std::vector<TriangleElement>& triangleStore,
std::optional<PolylineGeneratorDistances> lineDistances);
void addPieSliceVertex(const GeometryCoordinate& currentVertex,
double distance,
const Point<double>& extrude,
bool lineTurnsLeft,
std::size_t startVertex,
std::vector<TriangleElement>& triangleStore,
std::optional<PolylineGeneratorDistances> lineDistances);

private:
Vertices& vertices;
LayoutVertexFunc layoutVertex;
Segments& segments;
CreateSegmentFunc createSegment;
GetSegmentFunc getSegment;
Indexes& indexes;

std::ptrdiff_t e1;
std::ptrdiff_t e2;
std::ptrdiff_t e3;
};

} // namespace gfx
} // namespace mbgl
18 changes: 18 additions & 0 deletions include/mbgl/layermanager/custom_drawable_layer_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>

#include <array>

namespace mbgl {

class CustomDrawableLayerFactory : public LayerFactory {
protected:
const style::LayerTypeInfo* getTypeInfo() const noexcept final;
std::unique_ptr<style::Layer> createLayer(const std::string& id,
const style::conversion::Convertible& value) noexcept final;
std::unique_ptr<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) noexcept final;
};

} // namespace mbgl
12 changes: 5 additions & 7 deletions include/mbgl/renderer/layer_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,25 @@ class LayerTweaker {

void enableOverdrawInspector(bool);

virtual void execute(LayerGroupBase&, const RenderTree&, const PaintParameters&) = 0;
virtual void execute(LayerGroupBase&, const PaintParameters&) = 0;

void updateProperties(Immutable<style::LayerProperties>);

protected:
/// Determine whether this tweaker should apply to the given drawable
bool checkTweakDrawable(const gfx::Drawable&) const;

/// Calculate matrices for this tile.
/// @param nearClipped If true, the near plane is moved further to enhance depth buffer precision.
/// @param inViewportPixelUnits If false, the translation is scaled based on the current zoom.
static mat4 getTileMatrix(const UnwrappedTileID&,
const RenderTree&,
const TransformState&,
const PaintParameters&,
const std::array<float, 2>& translation,
style::TranslateAnchorType,
bool nearClipped,
bool inViewportPixelUnits,
bool aligned = false);

protected:
/// Determine whether this tweaker should apply to the given drawable
bool checkTweakDrawable(const gfx::Drawable&) const;

std::string id;
Immutable<style::LayerProperties> evaluatedProperties;

Expand Down
52 changes: 52 additions & 0 deletions include/mbgl/style/layers/custom_drawable_layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <mbgl/style/layer.hpp>
#include <mbgl/gfx/shader_registry.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/renderer/render_tree.hpp>
#include <mbgl/renderer/change_request.hpp>

#include <array>
#include <memory>

namespace mbgl {
namespace style {

class CustomDrawableLayerHost {
public:
virtual ~CustomDrawableLayerHost() = default;

virtual void initialize() = 0;

virtual void update(RenderLayer& proxyLayer,
gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState& state,
const std::shared_ptr<UpdateParameters>&,
const RenderTree& renderTree,
UniqueChangeRequestVec& changes) = 0;

virtual void deinitialize() = 0;
};

class CustomDrawableLayer final : public Layer {
public:
CustomDrawableLayer(const std::string& id, std::unique_ptr<CustomDrawableLayerHost> host);

CustomDrawableLayer(const CustomDrawableLayer&) = delete;
~CustomDrawableLayer() final;
class Impl;
const Impl& impl() const;
Mutable<Impl> mutableImpl() const;

private:
std::optional<conversion::Error> setPropertyInternal(const std::string& name,
const conversion::Convertible& value) final;
StyleProperty getProperty(const std::string&) const final;
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
Mutable<Layer::Impl> mutableBaseImpl() const final;
};

} // namespace style
} // namespace mbgl
2 changes: 1 addition & 1 deletion platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ sh_binary(
"//platform/macos:macos_objc_srcs",
"//platform/macos:macos_objcpp_srcs",
"//platform/macos:macos_private_hdrs",
"//platform/macos:macos_public_hdrs"
"//platform/macos:macos_public_hdrs",
] + select({
"//:metal_renderer": [],
"//conditions:default": [
Expand Down
1 change: 1 addition & 0 deletions platform/darwin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ filegroup(
srcs = MLN_DARWIN_PUBLIC_OBJCPP_SOURCE,
visibility = ["//visibility:public"],
)

filegroup(
name = "darwin_objcpp_opengl_srcs",
srcs = MLN_DARWIN_PUBLIC_OBJCPP_OPENGL_SOURCE,
Expand Down
1 change: 0 additions & 1 deletion platform/darwin/bazel/files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ MLN_DARWIN_PUBLIC_OBJCPP_OPENGL_SOURCE = [
"src/MLNOpenGLStyleLayer.h",
"src/MLNOpenGLStyleLayer.mm",
]

MLN_DARWIN_PUBLIC_OBJC_SOURCE = [
"src/MLNAttributedExpression.m",
"src/MLNClockDirectionFormatter.m",
Expand Down
Loading

0 comments on commit 89b9c39

Please sign in to comment.