Skip to content

Commit

Permalink
MLNCustomStyleLayer + Metal example (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankarschti committed Feb 15, 2024
1 parent 17e6e7c commit 1d00e5b
Show file tree
Hide file tree
Showing 56 changed files with 513 additions and 246 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,8 @@ if(MLN_WITH_OPENGL)
INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/backend.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/layers/custom_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/layers/custom_layer_render_parameters.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/layers/mtl/custom_layer_render_parameters.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/layermanager/custom_layer_factory.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/renderable_resource.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/renderer_backend.hpp
Expand Down Expand Up @@ -1072,6 +1074,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/src/mbgl/gl/fence.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/fence.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_layer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_layer_render_parameters.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/layermanager/custom_layer_factory.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_layer_impl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_layer_impl.hpp
Expand Down
4 changes: 4 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ MLN_CORE_SOURCE = [
"src/mbgl/style/layers/circle_layer_impl.cpp",
"src/mbgl/style/layers/circle_layer_impl.hpp",
"src/mbgl/style/layers/custom_layer.cpp",
"src/mbgl/style/layers/custom_layer_render_parameters.cpp",
"src/mbgl/style/layers/custom_layer_impl.cpp",
"src/mbgl/style/layers/custom_layer_impl.hpp",
"src/mbgl/style/layers/fill_extrusion_layer_impl.cpp",
Expand Down Expand Up @@ -774,6 +775,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/style/layer.hpp",
"include/mbgl/style/layer_properties.hpp",
"include/mbgl/style/layers/custom_layer.hpp",
"include/mbgl/style/layers/custom_layer_render_parameters.hpp",
"include/mbgl/style/position.hpp",
"include/mbgl/style/property_expression.hpp",
"include/mbgl/style/property_value.hpp",
Expand Down Expand Up @@ -1066,6 +1068,7 @@ MLN_DRAWABLES_MTL_SOURCE = [
"src/mbgl/shaders/mtl/symbol_icon.cpp",
"src/mbgl/shaders/mtl/symbol_sdf.cpp",
"src/mbgl/shaders/mtl/symbol_text_and_icon.cpp",
"src/mbgl/style/layers/mtl/custom_layer_render_parameters.cpp",
]

MLN_DRAWABLES_MTL_HEADERS = [
Expand Down Expand Up @@ -1111,4 +1114,5 @@ MLN_DRAWABLES_MTL_HEADERS = [
"include/mbgl/shaders/mtl/symbol_icon.hpp",
"include/mbgl/shaders/mtl/symbol_sdf.hpp",
"include/mbgl/shaders/mtl/symbol_text_and_icon.hpp",
"include/mbgl/style/layers/mtl/custom_layer_render_parameters.hpp",
]
3 changes: 2 additions & 1 deletion include/mbgl/gfx/drawable_atlases_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ namespace mbgl {
class TileAtlasTextures;
using TileAtlasTexturesPtr = std::shared_ptr<TileAtlasTextures>;

namespace gfx {
class PaintParameters;

namespace gfx {
class Drawable;

/**
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/gfx/drawable_custom_layer_host_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace mbgl {

class PaintParameters;

namespace gfx {

class Drawable;
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/gl/drawable_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace mbgl {

template <class AttributeList>
class Segment;
class PaintParameters;

namespace gfx {

Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/gl/layer_group_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <mbgl/renderer/layer_group.hpp>

namespace mbgl {

class PaintParameters;

namespace gl {

/**
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/mtl/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace mbgl {

template <class AttributeList>
class Segment;
class PaintParameters;

namespace gfx {

Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/mtl/layer_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <mbgl/renderer/layer_group.hpp>

namespace mbgl {

class PaintParameters;

namespace mtl {

/**
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/mtl/tile_layer_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <optional>

namespace mbgl {

class PaintParameters;

namespace mtl {

/**
Expand Down
31 changes: 9 additions & 22 deletions include/mbgl/style/layers/custom_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
#pragma once

#include <mbgl/style/layer.hpp>
#include <mbgl/style/layers/custom_layer_render_parameters.hpp>

#include <array>
#include <memory>

namespace mbgl {
namespace style {

/**
* Parameters that define the current camera position for a
* `CustomLayerHost::render()` function.
*/
struct CustomLayerRenderParameters {
double width;
double height;
double latitude;
double longitude;
double zoom;
double bearing;
double pitch;
double fieldOfView;
std::array<double, 16> projectionMatrix;
};
namespace style {

class CustomLayerHost {
public:
virtual ~CustomLayerHost() = default;
/**
* Initialize any GL state needed by the custom layer. This method is called
* Initialize any GL/Metal state needed by the custom layer. This method is called
* once, from the main thread, at a point when the GL context is active but
* before rendering for the first time.
*
Expand All @@ -38,28 +25,28 @@ class CustomLayerHost {

/**
* Render the layer. This method is called once per frame. The
* implementation should not make any assumptions about the GL state (other
* implementation should not make any assumptions about the GL/Metal state (other
* than that the correct context is active). It may make changes to the
* state, and is not required to reset values such as the depth mask,
* stencil mask, and corresponding test flags to their original values. Make
* sure that you are drawing your fragments with a z value of 1 to take
* advantage of the opaque fragment culling in case there are opaque layers
* above your custom layer.
*/
virtual void render(const CustomLayerRenderParameters&) = 0;
virtual void render(const mbgl::style::CustomLayerRenderParameters&) = 0;

/**
* Called when the system has destroyed the underlying GL context. The
* Called when the system has destroyed the underlying GL/Metal context. The
* `deinitialize` function will not be called in this case, however
* `initialize` will be called instead to prepare for a new render.
*
*/
virtual void contextLost() = 0;

/**
* Destroy any GL state needed by the custom layer, and deallocate context,
* Destroy any GL/Metal state needed by the custom layer, and deallocate context,
* if necessary. This method is called once, from the main thread, at a
* point when the GL context is active.
* point when the GL/Metal context is active.
*
* Note that it may be called even when the `initialize` function has not
* been called.
Expand Down
31 changes: 31 additions & 0 deletions include/mbgl/style/layers/custom_layer_render_parameters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <memory>
#include <array>

namespace mbgl {

class PaintParameters;

namespace style {

/**
* Parameters that define the current camera position for a
* `CustomLayerHost::render()` function.
*/
struct CustomLayerRenderParameters {
double width;
double height;
double latitude;
double longitude;
double zoom;
double bearing;
double pitch;
double fieldOfView;
std::array<double, 16> projectionMatrix;

CustomLayerRenderParameters(const PaintParameters&);
};

} // namespace style
} // namespace mbgl
30 changes: 30 additions & 0 deletions include/mbgl/style/layers/mtl/custom_layer_render_parameters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <mbgl/style/layers/custom_layer_render_parameters.hpp>

#include <mbgl/mtl/mtl_fwd.hpp>
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>

#include <memory>

namespace mbgl {

class PaintParameters;

namespace style {

namespace mtl {

/**
* Metal subclass of CustomLayerRenderParameters
*/
struct CustomLayerRenderParameters : mbgl::style::CustomLayerRenderParameters {
mbgl::mtl::MTLRenderCommandEncoderPtr encoder;

CustomLayerRenderParameters(const PaintParameters&);
};

} // namespace mtl
} // namespace style
} // namespace mbgl
17 changes: 4 additions & 13 deletions platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ objc_library(
"//platform/ios:ios_public_hdrs",
"//platform/ios:ios_sdk_hdrs",
] + select({
"//:metal_renderer": [],
"//conditions:default": [
"//platform/darwin:darwin_objcpp_opengl_srcs",
],
}) + select({
"//:legacy_renderer": [],
"//conditions:default": [
"//platform/darwin:darwin_objcpp_custom_drawable_srcs",
Expand Down Expand Up @@ -193,13 +188,9 @@ objc_library(
name = "iosapp",
srcs = [
"//platform/ios:ios_app_srcs",
] + select({
"//:metal_renderer": [],
"//conditions:default": [
"//platform/darwin:app/LimeGreenStyleLayer.h",
"//platform/darwin:app/LimeGreenStyleLayer.m",
],
}),
"//platform/darwin:app/CustomStyleLayerExample.h",
"//platform/darwin:app/CustomStyleLayerExample.m",
],
defines = ["GLES_SILENCE_DEPRECATION"],
includes = [
"darwin/app",
Expand All @@ -211,7 +202,7 @@ objc_library(
"MetalKit",
],
"//conditions:default": [
"GLKit", # needed for LimeGreenStyleLayer
"GLKit", # needed for CustomStyleLayerExample
],
}),
visibility = ["//visibility:public"],
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/example_custom_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <jni.h>
#include <mbgl/style/layers/custom_layer.hpp>
#include <sstream>
#include <memory>

// DEBUGGING

Expand Down
22 changes: 3 additions & 19 deletions platform/darwin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ load(
"MLN_DARWIN_OBJC_HEADERS",
"MLN_DARWIN_PRIVATE_HEADERS",
"MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE",
"MLN_DARWIN_PUBLIC_OBJCPP_OPENGL_SOURCE",
"MLN_DARWIN_PUBLIC_OBJCPP_SOURCE",
"MLN_DARWIN_PUBLIC_OBJC_SOURCE",
"MLN_GENERATED_DARWIN_STYLE_HEADERS",
Expand All @@ -33,10 +32,7 @@ filegroup(

filegroup(
name = "darwin_objcpp_hdrs",
srcs = MLN_DARWIN_OBJCPP_HEADERS + select({
"//:metal_renderer": [],
"//conditions:default": ["src/MLNOpenGLStyleLayer.h"],
}),
srcs = MLN_DARWIN_OBJCPP_HEADERS,
visibility = ["//visibility:public"],
)

Expand All @@ -46,12 +42,6 @@ filegroup(
visibility = ["//visibility:public"],
)

filegroup(
name = "darwin_objcpp_opengl_srcs",
srcs = MLN_DARWIN_PUBLIC_OBJCPP_OPENGL_SOURCE,
visibility = ["//visibility:public"],
)

filegroup(
name = "darwin_objcpp_custom_drawable_srcs",
srcs = MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE,
Expand Down Expand Up @@ -319,14 +309,13 @@ exports_files(
[
"test/amsterdam.geojson",
"test/MLNSDKTestHelpers.swift",
"app/LimeGreenStyleLayer.h",
"app/LimeGreenStyleLayer.m",
"app/CustomStyleLayerExample.h",
"app/CustomStyleLayerExample.m",
"app/ExampleCustomDrawableStyleLayer.h",
"app/ExampleCustomDrawableStyleLayer.mm",
"include/mbgl/util/image+MLNAdditions.hpp",
] + MLN_DARWIN_PUBLIC_OBJC_SOURCE +
MLN_DARWIN_PUBLIC_OBJCPP_SOURCE +
MLN_DARWIN_PUBLIC_OBJCPP_OPENGL_SOURCE +
MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE +
MLN_DARWIN_PRIVATE_HEADERS +
MLN_DARWIN_OBJC_HEADERS +
Expand Down Expand Up @@ -359,11 +348,6 @@ js_library(
":generated_style_hdrs",
":generated_style_srcs",
] + select({
"//:metal_renderer": [],
"//conditions:default": [
":darwin_objcpp_opengl_srcs",
],
}) + select({
"//:legacy_renderer": [],
"//conditions:default": [
":darwin_objcpp_custom_drawable_srcs",
Expand Down
5 changes: 5 additions & 0 deletions platform/darwin/app/CustomStyleLayerExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import "Mapbox.h"

@interface CustomStyleLayerExample : MLNCustomStyleLayer

@end

0 comments on commit 1d00e5b

Please sign in to comment.