MLNCustomStyleLayer + Metal example (#2041)

This commit is contained in:
Stefan Karschti
2024-02-15 18:43:40 +02:00
committed by GitHub
parent 17e6e7cdcb
commit 1d00e5b9ea
56 changed files with 513 additions and 246 deletions

View File

@@ -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
@@ -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

View File

@@ -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",
@@ -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",
@@ -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 = [
@@ -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",
]

View File

@@ -13,8 +13,9 @@ namespace mbgl {
class TileAtlasTextures;
using TileAtlasTexturesPtr = std::shared_ptr<TileAtlasTextures>;
namespace gfx {
class PaintParameters;
namespace gfx {
class Drawable;
/**

View File

@@ -8,6 +8,8 @@
namespace mbgl {
class PaintParameters;
namespace gfx {
class Drawable;

View File

@@ -13,6 +13,7 @@ namespace mbgl {
template <class AttributeList>
class Segment;
class PaintParameters;
namespace gfx {

View File

@@ -3,6 +3,9 @@
#include <mbgl/renderer/layer_group.hpp>
namespace mbgl {
class PaintParameters;
namespace gl {
/**

View File

@@ -11,6 +11,7 @@ namespace mbgl {
template <class AttributeList>
class Segment;
class PaintParameters;
namespace gfx {

View File

@@ -3,6 +3,9 @@
#include <mbgl/renderer/layer_group.hpp>
namespace mbgl {
class PaintParameters;
namespace mtl {
/**

View File

@@ -8,6 +8,9 @@
#include <optional>
namespace mbgl {
class PaintParameters;
namespace mtl {
/**

View File

@@ -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.
*
@@ -38,7 +25,7 @@ public:
/**
* 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
@@ -46,10 +33,10 @@ public:
* 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.
*
@@ -57,9 +44,9 @@ public:
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.

View File

@@ -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

View File

@@ -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

View File

@@ -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",
@@ -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",
@@ -211,7 +202,7 @@ objc_library(
"MetalKit",
],
"//conditions:default": [
"GLKit", # needed for LimeGreenStyleLayer
"GLKit", # needed for CustomStyleLayerExample
],
}),
visibility = ["//visibility:public"],

View File

@@ -3,6 +3,7 @@
#include <jni.h>
#include <mbgl/style/layers/custom_layer.hpp>
#include <sstream>
#include <memory>
// DEBUGGING

View File

@@ -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",
@@ -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"],
)
@@ -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,
@@ -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 +
@@ -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",

View File

@@ -0,0 +1,5 @@
#import "Mapbox.h"
@interface CustomStyleLayerExample : MLNCustomStyleLayer
@end

View File

@@ -0,0 +1,188 @@
#if !MLN_RENDER_BACKEND_METAL
/* OPENGL Custom Layer example implementation */
#import "CustomStyleLayerExample.h"
#import <GLKit/GLKit.h>
@implementation CustomStyleLayerExample {
GLuint _program;
GLuint _vertexShader;
GLuint _fragmentShader;
GLuint _buffer;
GLuint _aPos;
}
- (void)didMoveToMapView:(MLNMapView *)mapView {
static const GLchar *vertexShaderSource = "#version 300 es\nlayout (location = 0) in vec2 a_pos; void main() { gl_Position = vec4(a_pos, 1, 1); }";
static const GLchar *fragmentShaderSource = "#version 300 es\nout highp vec4 fragColor; void main() { fragColor = vec4(0, 0.5, 0, 0.5); }";
_program = glCreateProgram();
_vertexShader = glCreateShader(GL_VERTEX_SHADER);
_fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(_vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(_vertexShader);
glAttachShader(_program, _vertexShader);
glShaderSource(_fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(_fragmentShader);
glAttachShader(_program, _fragmentShader);
glLinkProgram(_program);
_aPos = glGetAttribLocation(_program, "a_pos");
GLfloat triangle[] = { 0, 0.5, 0.5, -0.5, -0.5, -0.5 };
glGenBuffers(1, &_buffer);
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat), triangle, GL_STATIC_DRAW);
}
- (void)drawInMapView:(MLNMapView *)mapView withContext:(MLNStyleLayerDrawingContext)context {
glUseProgram(_program);
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glEnableVertexAttribArray(_aPos);
glVertexAttribPointer(_aPos, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
}
- (void)willMoveFromMapView:(MLNMapView *)mapView {
if (!_program) {
return;
}
glDeleteBuffers(1, &_buffer);
glDetachShader(_program, _vertexShader);
glDetachShader(_program, _fragmentShader);
glDeleteShader(_vertexShader);
glDeleteShader(_fragmentShader);
glDeleteProgram(_program);
}
@end
#else // MLN_RENDER_BACKEND_METAL:
/* Metal Custom Layer example implementation */
#import "CustomStyleLayerExample.h"
@implementation CustomStyleLayerExample {
// The render pipeline state
id<MTLRenderPipelineState> _pipelineState;
id<MTLDepthStencilState> _depthStencilStateWithoutStencil;
}
- (void)didMoveToMapView:(MLNMapView *)mapView {
MLNBackendResource resource = [mapView backendResource];
NSString *shaderSource = @
" #include <metal_stdlib>\n"
" using namespace metal;\n"
" typedef struct\n"
" {\n"
" vector_float2 position;\n"
" vector_float4 color;\n"
" } Vertex;\n"
" struct RasterizerData\n"
" {\n"
" float4 position [[position]];\n"
" float4 color;\n"
" };\n"
" vertex RasterizerData\n"
" vertexShader(uint vertexID [[vertex_id]],\n"
" constant Vertex *vertices [[buffer(0)]],\n"
" constant vector_uint2 *viewportSizePointer [[buffer(1)]])\n"
" {\n"
" RasterizerData out;\n"
" float2 pixelSpacePosition = vertices[vertexID].position.xy;\n"
" vector_float2 viewportSize = vector_float2(*viewportSizePointer);\n"
" out.position = vector_float4(0.0, 0.0, 0.0, 1.0);\n"
" out.position.xy = pixelSpacePosition / (viewportSize / 2.0);\n"
" out.color = vertices[vertexID].color;\n"
" return out;\n"
" }\n"
" fragment float4 fragmentShader(RasterizerData in [[stage_in]])\n"
" {\n"
" return in.color;\n"
" }\n";
NSError *error = nil;
id<MTLDevice> _device = resource.device;
id<MTLLibrary> library = [_device newLibraryWithSource:shaderSource options:nil error:&error];
NSAssert(library, @"Error compiling shaders: %@", error);
id<MTLFunction> vertexFunction = [library newFunctionWithName:@"vertexShader"];
id<MTLFunction> fragmentFunction = [library newFunctionWithName:@"fragmentShader"];
// Configure a pipeline descriptor that is used to create a pipeline state.
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
pipelineStateDescriptor.label = @"Simple Pipeline";
pipelineStateDescriptor.vertexFunction = vertexFunction;
pipelineStateDescriptor.fragmentFunction = fragmentFunction;
pipelineStateDescriptor.colorAttachments[0].pixelFormat = resource.mtkView.colorPixelFormat;
pipelineStateDescriptor.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
pipelineStateDescriptor.stencilAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
_pipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor
error:&error];
NSAssert(_pipelineState, @"Failed to create pipeline state: %@", error);
// Notice that we don't configure the stencilTest property, leaving stencil testing disabled
MTLDepthStencilDescriptor *depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init];
depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; // Or another value as needed
depthStencilDescriptor.depthWriteEnabled = NO;
_depthStencilStateWithoutStencil = [_device newDepthStencilStateWithDescriptor:depthStencilDescriptor];
}
- (void)drawInMapView:(MLNMapView *)mapView withContext:(MLNStyleLayerDrawingContext)context {
// Use the supplied render command encoder to encode commands
id<MTLRenderCommandEncoder> renderEncoder = self.renderEncoder;
if(renderEncoder != nil)
{
MLNBackendResource resource = [mapView backendResource];
vector_uint2 _viewportSize;
_viewportSize.x = resource.mtkView.drawableSize.width;
_viewportSize.y = resource.mtkView.drawableSize.height;
typedef struct
{
vector_float2 position;
vector_float4 color;
} Vertex;
static const Vertex triangleVertices[] =
{
// 2D positions, RGBA colors
{ { 250, -250 }, { 1, 0, 0, 1 } },
{ { -250, -250 }, { 0, 1, 0, 1 } },
{ { 0, 250 }, { 0, 0, 1, 1 } },
};
[renderEncoder setRenderPipelineState:_pipelineState];
[renderEncoder setDepthStencilState:_depthStencilStateWithoutStencil];
// Pass in the parameter data.
[renderEncoder setVertexBytes:triangleVertices
length:sizeof(triangleVertices)
atIndex:0];
[renderEncoder setVertexBytes:&_viewportSize
length:sizeof(_viewportSize)
atIndex:1];
// Draw the triangle.
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
vertexCount:3];
}
}
- (void)willMoveFromMapView:(MLNMapView *)mapView {
// Clean up
}
@end
#endif

View File

@@ -1,5 +0,0 @@
#import "Mapbox.h"
@interface LimeGreenStyleLayer : MLNOpenGLStyleLayer
@end

View File

@@ -1,58 +0,0 @@
#import "LimeGreenStyleLayer.h"
#import <GLKit/GLKit.h>
@implementation LimeGreenStyleLayer {
GLuint _program;
GLuint _vertexShader;
GLuint _fragmentShader;
GLuint _buffer;
GLuint _aPos;
}
- (void)didMoveToMapView:(MLNMapView *)mapView {
static const GLchar *vertexShaderSource = "#version 300 es\nlayout (location = 0) in vec2 a_pos; void main() { gl_Position = vec4(a_pos, 1, 1); }";
static const GLchar *fragmentShaderSource = "#version 300 es\nout highp vec4 fragColor; void main() { fragColor = vec4(0, 0.5, 0, 0.5); }";
_program = glCreateProgram();
_vertexShader = glCreateShader(GL_VERTEX_SHADER);
_fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(_vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(_vertexShader);
glAttachShader(_program, _vertexShader);
glShaderSource(_fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(_fragmentShader);
glAttachShader(_program, _fragmentShader);
glLinkProgram(_program);
_aPos = glGetAttribLocation(_program, "a_pos");
GLfloat triangle[] = { 0, 0.5, 0.5, -0.5, -0.5, -0.5 };
glGenBuffers(1, &_buffer);
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat), triangle, GL_STATIC_DRAW);
}
- (void)drawInMapView:(MLNMapView *)mapView withContext:(MLNStyleLayerDrawingContext)context {
glUseProgram(_program);
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glEnableVertexAttribArray(_aPos);
glVertexAttribPointer(_aPos, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
}
- (void)willMoveFromMapView:(MLNMapView *)mapView {
if (!_program) {
return;
}
glDeleteBuffers(1, &_buffer);
glDetachShader(_program, _vertexShader);
glDetachShader(_program, _fragmentShader);
glDeleteShader(_vertexShader);
glDeleteShader(_fragmentShader);
glDeleteProgram(_program);
}
@end

View File

@@ -53,11 +53,13 @@ MLN_DARWIN_OBJC_HEADERS = [
"src/MLNAnnotation.h",
"src/MLNAttributedExpression.h",
"src/MLNAttributionInfo.h",
"src/MLNBackendResource.h",
"src/MLNClockDirectionFormatter.h",
"src/MLNCluster.h",
"src/MLNCompassDirectionFormatter.h",
"src/MLNComputedShapeSource.h",
"src/MLNCoordinateFormatter.h",
"src/MLNCustomStyleLayer.h",
"src/MLNCustomDrawableStyleLayer.h",
"src/MLNDefaultStyle.h",
"src/MLNDistanceFormatter.h",
@@ -75,7 +77,6 @@ MLN_DARWIN_OBJC_HEADERS = [
"src/MLNOfflinePack.h",
"src/MLNOfflineRegion.h",
"src/MLNOfflineStorage.h",
"src/MLNOpenGLStyleLayer.h",
"src/MLNOverlay.h",
"src/MLNPointAnnotation.h",
"src/MLNPointCollection.h",
@@ -126,6 +127,7 @@ MLN_DARWIN_OBJCPP_HEADERS = [
MLN_DARWIN_PRIVATE_HEADERS = [
"src/MLNAttributionInfo_Private.h",
"src/MLNComputedShapeSource_Private.h",
"src/MLNCustomStyleLayer_Private.h",
"src/MLNFeature_Private.h",
"src/MLNFoundation_Private.h",
"src/MLNGeometry_Private.h",
@@ -160,6 +162,7 @@ MLN_DARWIN_PRIVATE_HEADERS = [
MLN_DARWIN_PUBLIC_OBJCPP_SOURCE = [
"src/MLNAttributionInfo.mm",
"src/MLNComputedShapeSource.mm",
"src/MLNCustomStyleLayer.mm",
"src/MLNDefaultStyle.mm",
"src/MLNFeature.mm",
"src/MLNForegroundStyleLayer.mm",
@@ -205,11 +208,6 @@ MLN_DARWIN_PUBLIC_OBJCPP_SOURCE = [
"src/NSPredicate+MLNAdditions.mm",
"src/NSValue+MLNStyleAttributeAdditions.mm",
]
MLN_DARWIN_PUBLIC_OBJCPP_OPENGL_SOURCE = [
"src/MLNOpenGLStyleLayer_Private.h",
"src/MLNOpenGLStyleLayer.h",
"src/MLNOpenGLStyleLayer.mm",
]
MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE = [
"src/MLNCustomDrawableStyleLayer_Private.h",
"src/MLNCustomDrawableStyleLayer.mm",

View File

@@ -0,0 +1,19 @@
#if MLN_RENDER_BACKEND_METAL
#import <MetalKit/MetalKit.h>
typedef struct
{
MTKView *mtkView;
id <MTLDevice> device;
MTLRenderPassDescriptor *renderPassDescriptor;
id <MTLCommandBuffer> commandBuffer;
} MLNBackendResource;
#else
typedef struct
{
} MLNBackendResource;
#endif

View File

@@ -7,6 +7,10 @@
#import "MLNStyleLayer.h"
#import "MLNGeometry.h"
#if MLN_RENDER_BACKEND_METAL
#import <MetalKit/MetalKit.h>
#endif
NS_ASSUME_NONNULL_BEGIN
@class MLNMapView;
@@ -23,7 +27,7 @@ typedef struct MLNStyleLayerDrawingContext {
} MLNStyleLayerDrawingContext;
MLN_EXPORT
@interface MLNOpenGLStyleLayer : MLNStyleLayer
@interface MLNCustomStyleLayer : MLNStyleLayer
@property (nonatomic, weak, readonly) MLNStyle *style;
@@ -36,6 +40,10 @@ MLN_EXPORT
#endif
#pragma clang diagnostic pop
#if MLN_RENDER_BACKEND_METAL
@property (nonatomic, weak) id<MTLRenderCommandEncoder> renderEncoder;
#endif
- (instancetype)initWithIdentifier:(NSString *)identifier;
- (void)didMoveToMapView:(MLNMapView *)mapView;

View File

@@ -1,35 +1,43 @@
#import "MLNOpenGLStyleLayer.h"
#import "MLNOpenGLStyleLayer_Private.h"
#import "MLNCustomStyleLayer.h"
#import "MLNCustomStyleLayer_Private.h"
#import "MLNMapView_Private.h"
#import "MLNStyle_Private.h"
#import "MLNStyleLayer_Private.h"
#import "MLNGeometry_Private.h"
#if MLN_RENDER_BACKEND_METAL
#import <MetalKit/MetalKit.h>
#endif
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/math/wrap.hpp>
class MLNOpenGLLayerHost;
#if MLN_RENDER_BACKEND_METAL
#include <mbgl/style/layers/mtl/custom_layer_render_parameters.hpp>
#endif
class MLNCustomLayerHost;
/**
An `MLNOpenGLStyleLayer` is a style layer that is rendered by OpenGL code that
An `MLNCustomStyleLayer` is a style layer that is rendered by OpenGL / Metal code that
you provide.
By default, this class does nothing. You can subclass this class to provide
custom OpenGL drawing code that is run on each frame of the map. Your subclass
custom OpenGL or Metal drawing code that is run on each frame of the map. Your subclass
should override the `-didMoveToMapView:`, `-willMoveFromMapView:`, and
`-drawInMapView:withContext:` methods.
You can access an existing OpenGL style layer using the
You can access an existing MLNCustomStyleLayer using the
`-[MLNStyle layerWithIdentifier:]` method if you know its identifier;
otherwise, find it using the `MLNStyle.layers` property. You can also create a
new OpenGL style layer and add it to the style using a method such as
new MLNCustomStyleLayer and add it to the style using a method such as
`-[MLNStyle addLayer:]`.
@warning This API is undocumented and therefore unsupported. It may change at
any time without notice.
*/
@interface MLNOpenGLStyleLayer ()
@interface MLNCustomStyleLayer ()
@property (nonatomic, readonly) mbgl::style::CustomLayer *rawLayer;
@@ -45,10 +53,10 @@ class MLNOpenGLLayerHost;
@end
@implementation MLNOpenGLStyleLayer
@implementation MLNCustomStyleLayer
/**
Returns an OpenGL style layer object initialized with the given identifier.
Returns an MLNCustomStyleLayer style layer object initialized with the given identifier.
After initializing and configuring the style layer, add it to a map view’s
style using the `-[MLNStyle addLayer:]` or
@@ -60,7 +68,7 @@ class MLNOpenGLLayerHost;
*/
- (instancetype)initWithIdentifier:(NSString *)identifier {
auto layer = std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String,
std::make_unique<MLNOpenGLLayerHost>(self));
std::make_unique<MLNCustomLayerHost>(self));
return self = [super initWithPendingLayer:std::move(layer)];
}
@@ -88,13 +96,13 @@ class MLNOpenGLLayerHost;
// MARK: - Adding to and removing from a map view
- (void)addToStyle:(MLNStyle *)style belowLayer:(MLNStyleLayer *)otherLayer {
self.style = style;
self.style.openGLLayers[self.identifier] = self;
self.style.customLayers[self.identifier] = self;
[super addToStyle:style belowLayer:otherLayer];
}
- (void)removeFromStyle:(MLNStyle *)style {
[super removeFromStyle:style];
self.style.openGLLayers[self.identifier] = nil;
self.style.customLayers[self.identifier] = nil;
self.style = nil;
}
@@ -164,9 +172,9 @@ class MLNOpenGLLayerHost;
@end
class MLNOpenGLLayerHost : public mbgl::style::CustomLayerHost {
class MLNCustomLayerHost : public mbgl::style::CustomLayerHost {
public:
MLNOpenGLLayerHost(MLNOpenGLStyleLayer *styleLayer) {
MLNCustomLayerHost(MLNCustomStyleLayer *styleLayer) {
layerRef = styleLayer;
layer = nil;
}
@@ -180,17 +188,23 @@ public:
}
}
void render(const mbgl::style::CustomLayerRenderParameters &params) {
void render(const mbgl::style::CustomLayerRenderParameters& parameters) {
if(!layer) return;
#if MLN_RENDER_BACKEND_METAL
MTL::RenderCommandEncoder* ptr = static_cast<const mbgl::style::mtl::CustomLayerRenderParameters&>(parameters).encoder.get();
id<MTLRenderCommandEncoder> encoder = (__bridge id<MTLRenderCommandEncoder>)ptr;
layer.renderEncoder = encoder;
#endif
MLNStyleLayerDrawingContext drawingContext = {
.size = CGSizeMake(params.width, params.height),
.centerCoordinate = CLLocationCoordinate2DMake(params.latitude, params.longitude),
.zoomLevel = params.zoom,
.direction = mbgl::util::wrap(params.bearing, 0., 360.),
.pitch = static_cast<CGFloat>(params.pitch),
.fieldOfView = static_cast<CGFloat>(params.fieldOfView),
.projectionMatrix = MLNMatrix4Make(params.projectionMatrix)
.size = CGSizeMake(parameters.width, parameters.height),
.centerCoordinate = CLLocationCoordinate2DMake(parameters.latitude, parameters.longitude),
.zoomLevel = parameters.zoom,
.direction = mbgl::util::wrap(parameters.bearing, 0., 360.),
.pitch = static_cast<CGFloat>(parameters.pitch),
.fieldOfView = static_cast<CGFloat>(parameters.fieldOfView),
.projectionMatrix = MLNMatrix4Make(parameters.projectionMatrix)
};
if (layer.mapView) {
[layer drawInMapView:layer.mapView withContext:drawingContext];
@@ -209,14 +223,14 @@ public:
layer = nil;
}
private:
__weak MLNOpenGLStyleLayer * layerRef;
MLNOpenGLStyleLayer * layer = nil;
__weak MLNCustomStyleLayer * layerRef;
MLNCustomStyleLayer * layer = nil;
};
namespace mbgl {
MLNStyleLayer* OpenGLStyleLayerPeerFactory::createPeer(style::Layer* rawLayer) {
return [[MLNOpenGLStyleLayer alloc] initWithRawLayer:rawLayer];
MLNStyleLayer* CustomStyleLayerPeerFactory::createPeer(style::Layer* rawLayer) {
return [[MLNCustomStyleLayer alloc] initWithRawLayer:rawLayer];
}
} // namespace mbgl

View File

@@ -6,7 +6,7 @@
namespace mbgl {
class OpenGLStyleLayerPeerFactory : public LayerPeerFactory, public mbgl::CustomLayerFactory {
class CustomStyleLayerPeerFactory : public LayerPeerFactory, public mbgl::CustomLayerFactory {
// LayerPeerFactory overrides.
LayerFactory* getCoreLayerFactory() final { return this; }
virtual MLNStyleLayer* createPeer(style::Layer*) final;

View File

@@ -39,9 +39,7 @@
#import "NSDate+MLNAdditions.h"
#if !MLN_RENDER_BACKEND_METAL
#import "MLNOpenGLStyleLayer.h"
#endif
#import "MLNCustomStyleLayer.h"
#if TARGET_OS_IPHONE
#import "UIImage+MLNAdditions.h"
@@ -82,7 +80,7 @@ const MLNExceptionName MLNRedundantSourceIdentifierException = @"MLNRedundantSou
@property (nonatomic, readonly, weak) id <MLNStylable> stylable;
@property (nonatomic, readonly) mbgl::style::Style *rawStyle;
@property (readonly, copy, nullable) NSURL *URL;
@property (nonatomic, readwrite, strong) NSMutableDictionary<NSString *, MLNOpenGLStyleLayer *> *openGLLayers;
@property (nonatomic, readwrite, strong) NSMutableDictionary<NSString *, MLNCustomStyleLayer *> *customLayers;
@property (nonatomic) NSMutableDictionary<NSString *, NSDictionary<NSObject *, MLNTextLanguage *> *> *localizedLayersByIdentifier;
@end
@@ -125,7 +123,7 @@ const MLNExceptionName MLNRedundantSourceIdentifierException = @"MLNRedundantSou
if (self = [super init]) {
_stylable = stylable;
_rawStyle = rawStyle;
_openGLLayers = [NSMutableDictionary dictionary];
_customLayers = [NSMutableDictionary dictionary];
_localizedLayersByIdentifier = [NSMutableDictionary dictionary];
MLNLogDebug(@"Initializing with style name: %@ stylable: %@", self.name, stylable);
}

View File

@@ -9,10 +9,7 @@
#import "MLNLineStyleLayer_Private.h"
#import "MLNRasterStyleLayer_Private.h"
#import "MLNSymbolStyleLayer_Private.h"
#if !MLN_RENDER_BACKEND_METAL
#import "MLNOpenGLStyleLayer_Private.h"
#endif
#import "MLNCustomStyleLayer_Private.h"
#if MLN_DRAWABLE_RENDERER
#import "MLNCustomDrawableStyleLayer_Private.h"
@@ -70,8 +67,8 @@ LayerManagerDarwin::LayerManagerDarwin() {
#endif
#if defined(MBGL_LAYER_CUSTOM_DISABLE_RUNTIME)
addLayerTypeCoreOnly(std::make_unique<CustomLayerFactory>());
#elif !defined(MBGL_LAYER_CUSTOM_DISABLE_ALL) && !MLN_RENDER_BACKEND_METAL
addLayerType(std::make_unique<OpenGLStyleLayerPeerFactory>());
#elif !defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
addLayerType(std::make_unique<CustomStyleLayerPeerFactory>());
#endif
#if MLN_DRAWABLE_RENDERER

View File

@@ -13,7 +13,7 @@ namespace mbgl {
@class MLNAttributionInfo;
@class MLNMapView;
@class MLNOpenGLStyleLayer;
@class MLNCustomStyleLayer;
@class MLNVectorTileSource;
@class MLNVectorStyleLayer;
@@ -25,7 +25,7 @@ namespace mbgl {
@property (nonatomic, readonly) mbgl::style::Style *rawStyle;
- (nullable NSArray<MLNAttributionInfo *> *)attributionInfosWithFontSize:(CGFloat)fontSize linkColor:(nullable MLNColor *)linkColor;
@property (nonatomic, readonly, strong) NSMutableDictionary<NSString *, MLNOpenGLStyleLayer *> *openGLLayers;
@property (nonatomic, readonly, strong) NSMutableDictionary<NSString *, MLNCustomStyleLayer *> *customLayers;
- (void)setStyleClasses:(NSArray<NSString *> *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration;
@end

View File

@@ -232,9 +232,7 @@
XCTAssertThrowsSpecificNamed([self.style insertLayer:[[MLNFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] belowLayer:initial],NSException, @"MLNRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.style insertLayer:[[MLNFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] aboveLayer:initial], NSException, @"MLNRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.style insertLayer:[[MLNFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] atIndex:0], NSException, @"MLNRedundantLayerIdentifierException");
#if !MLN_RENDER_BACKEND_METAL
XCTAssertThrowsSpecificNamed([self.style insertLayer:[[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"my-layer"] atIndex:0], NSException, @"MLNRedundantLayerIdentifierException");
#endif
XCTAssertThrowsSpecificNamed([self.style insertLayer:[[MLNCustomStyleLayer alloc] initWithIdentifier:@"my-layer"] atIndex:0], NSException, @"MLNRedundantLayerIdentifierException");
}
- (void)testRemovingLayerBeforeAddingSameLayer {

View File

@@ -17,7 +17,7 @@
// This test does not strictly need to be in this test file/target. Including here for convenience.
- (void)testOpenGLLayerDoesNotLeakWhenCreatedAndDestroyedWithoutAddingToStyle {
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
__weak id weakLayer = layer;
layer = nil;
@@ -31,7 +31,7 @@
__weak id weakLayer = nil;
@autoreleasepool {
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
[self.style insertLayer:layer atIndex:0];
weakLayer = layer;
@@ -49,15 +49,15 @@
}
- (void)testReusingOpenGLLayerIdentifier {
__weak MLNOpenGLStyleLayer *weakLayer2;
__weak MLNCustomStyleLayer *weakLayer2;
@autoreleasepool {
MLNOpenGLStyleLayer *layer1 = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer1 = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
[self.style insertLayer:layer1 atIndex:0];
[self waitForMapViewToBeRendered];
[self.style removeLayer:layer1];
MLNOpenGLStyleLayer *layer2 = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer2 = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
weakLayer2 = layer2;
XCTAssertNotNil(layer2);
@@ -88,7 +88,7 @@
__weak id retrievedLayer = nil;
@autoreleasepool {
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
[self.style insertLayer:layer atIndex:0];
layer = nil;
@@ -110,7 +110,7 @@
}
- (void)testReusingOpenGLLayer {
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
[self.style insertLayer:layer atIndex:0];
[self waitForMapViewToBeRendered];
@@ -127,7 +127,7 @@
- (void)testOpenGLLayerDoesNotLeakWhenRemovedFromStyle {
__weak id weakLayer;
@autoreleasepool {
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
weakLayer = layer;
[self.style insertLayer:layer atIndex:0];
layer = nil;
@@ -146,11 +146,11 @@
}
- (void)testOpenGLLayerDoesNotLeakWhenStyleChanged {
__weak MLNOpenGLStyleLayer *weakLayer;
__weak MLNCustomStyleLayer *weakLayer;
@autoreleasepool {
{
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
weakLayer = layer;
[self.style insertLayer:layer atIndex:0];
layer = nil;
@@ -192,7 +192,7 @@
self.styleLoadingExpectation = [self expectationWithDescription:@"Map view should finish loading style."];
[self waitForExpectationsWithTimeout:10 handler:nil];
MLNOpenGLStyleLayer *layer = [[MLNOpenGLStyleLayer alloc] initWithIdentifier:@"gl-layer"];
MLNCustomStyleLayer *layer = [[MLNCustomStyleLayer alloc] initWithIdentifier:@"gl-layer"];
weakLayer = layer;
[mapView2.style insertLayer:layer atIndex:0];
layer = nil;

View File

@@ -13,9 +13,7 @@
#import "MBXState.h"
#import "MLNSettings.h"
#if !MLN_RENDER_BACKEND_METAL
#import "LimeGreenStyleLayer.h"
#endif
#import "CustomStyleLayerExample.h"
#if MLN_DRAWABLE_RENDERER
#import "ExampleCustomDrawableStyleLayer.h"
@@ -101,9 +99,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
MBXSettingsRuntimeStylingRasterTileSource,
MBXSettingsRuntimeStylingImageSource,
MBXSettingsRuntimeStylingRouteLine,
#if !MLN_RENDER_BACKEND_METAL
MBXSettingsRuntimeStylingAddLimeGreenTriangleLayer,
#endif
MBXSettingsRuntimeStylingAddCustomTriangleLayer,
MBXSettingsRuntimeStylingDDSPolygon,
MBXSettingsRuntimeStylingCustomLatLonGrid,
MBXSettingsRuntimeStylingLineGradient,
@@ -435,8 +431,10 @@ CLLocationCoordinate2D randomWorldCoordinate(void) {
@"Style Raster Tile Source",
@"Style Image Source",
@"Add Route Line",
#if !MLN_RENDER_BACKEND_METAL
@"Add Lime Green Triangle Layer",
#if MLN_RENDER_BACKEND_METAL
@"Add Custom Triangle Layer (Metal)",
#else
@"Add Custom Triangle Layer (OpenGL)",
#endif
@"Dynamically Style Polygon",
@"Add Custom Lat/Lon Grid",
@@ -654,11 +652,9 @@ CLLocationCoordinate2D randomWorldCoordinate(void) {
case MBXSettingsRuntimeStylingRouteLine:
[self styleRouteLine];
break;
#if !MLN_RENDER_BACKEND_METAL
case MBXSettingsRuntimeStylingAddLimeGreenTriangleLayer:
[self styleAddLimeGreenTriangleLayer];
case MBXSettingsRuntimeStylingAddCustomTriangleLayer:
[self styleAddCustomTriangleLayer];
break;
#endif
case MBXSettingsRuntimeStylingDDSPolygon:
[self stylePolygonWithDDS];
break;
@@ -1604,13 +1600,11 @@ CLLocationCoordinate2D randomWorldCoordinate(void) {
[self.mapView.style addLayer:routeLayer];
}
#if !MLN_RENDER_BACKEND_METAL
- (void)styleAddLimeGreenTriangleLayer
- (void)styleAddCustomTriangleLayer
{
LimeGreenStyleLayer *layer = [[LimeGreenStyleLayer alloc] initWithIdentifier:@"mbx-custom"];
CustomStyleLayerExample *layer = [[CustomStyleLayerExample alloc] initWithIdentifier:@"mbx-custom"];
[self.mapView.style addLayer:layer];
}
#endif
- (void)stylePolygonWithDDS {
CLLocationCoordinate2D leftCoords[] = {

0
platform/ios/scripts/bazel-package.sh Normal file → Executable file
View File

View File

@@ -113,7 +113,7 @@
"platform/darwin/src/MLNPointCollection.mm",
"platform/darwin/src/MLNLineStyleLayer.mm",
"platform/ios/src/MLNMapAccessibilityElement.mm",
"platform/darwin/src/MLNOpenGLStyleLayer.mm",
"platform/darwin/src/MLNCustomStyleLayer.mm",
"platform/darwin/src/MLNSettings.mm",
"platform/darwin/src/NSCompoundPredicate+MLNAdditions.mm",
"platform/ios/src/MLNTelemetryConfig.m",
@@ -185,7 +185,7 @@
"MLNOfflineRegion.h": "platform/darwin/src/MLNOfflineRegion.h",
"MLNMapViewDelegate.h": "platform/ios/src/MLNMapViewDelegate.h",
"MLNDistanceFormatter.h": "platform/darwin/src/MLNDistanceFormatter.h",
"MLNOpenGLStyleLayer.h": "platform/darwin/src/MLNOpenGLStyleLayer.h",
"MLNCustomStyleLayer.h": "platform/darwin/src/MLNCustomStyleLayer.h",
"MLNTileSource.h": "platform/darwin/src/MLNTileSource.h",
"MLNTilePyramidOfflineRegion.h": "platform/darwin/src/MLNTilePyramidOfflineRegion.h",
"MLNVectorTileSource.h": "platform/darwin/src/MLNVectorTileSource.h",
@@ -277,7 +277,7 @@
"MLNGeometry_Private.h": "platform/darwin/src/MLNGeometry_Private.h",
"NSCompoundPredicate+MLNAdditions.h": "platform/darwin/src/NSCompoundPredicate+MLNAdditions.h",
"NSExpression+MLNPrivateAdditions.h": "platform/darwin/src/NSExpression+MLNPrivateAdditions.h",
"MLNOpenGLStyleLayer_Private.h": "platform/darwin/src/MLNOpenGLStyleLayer_Private.h",
"MLNCustomStyleLayer_Private.h": "platform/darwin/src/MLNCustomStyleLayer_Private.h",
"MLNTileSource_Private.h": "platform/darwin/src/MLNTileSource_Private.h",
"MLNFaux3DUserLocationAnnotationView.h": "platform/ios/src/MLNFaux3DUserLocationAnnotationView.h",
"MLNUserLocationAnnotationView_Private.h": "platform/ios/src/MLNUserLocationAnnotationView_Private.h",

View File

@@ -2,6 +2,8 @@
#import <mbgl/map/map_observer.hpp>
#import <mbgl/util/image.hpp>
#import "MLNBackendResource.h"
#import <UIKit/UIView.h>
#import <UIKit/UIImage.h>
#import <QuartzCore/CALayer.h>
@@ -54,6 +56,8 @@ public:
// Called by the view delegate when it's time to render.
void render();
virtual MLNBackendResource getObject() = 0;
// mbgl::MapObserver implementation
void onCameraWillChange(mbgl::MapObserver::CameraChangeMode) override;
void onCameraIsChanging() override;

View File

@@ -47,6 +47,7 @@ public:
void deleteView() override;
UIImage* snapshot() override;
void layoutChanged() override;
MLNBackendResource getObject() override;
// End implementation of MLNMapViewImpl
private:

View File

@@ -52,8 +52,10 @@ public:
commandQueue = [mtlView.device newCommandQueue];
}
commandBuffer = [commandQueue commandBuffer];
commandBufferPtr = NS::RetainPtr((__bridge MTL::CommandBuffer*)commandBuffer);
if (!commandBuffer) {
commandBuffer = [commandQueue commandBuffer];
commandBufferPtr = NS::RetainPtr((__bridge MTL::CommandBuffer*)commandBuffer);
}
}
const mbgl::mtl::RendererBackend& getBackend() const override { return backend; }
@@ -219,3 +221,14 @@ void MLNMapViewMetalImpl::layoutChanged() {
size = { static_cast<uint32_t>(mapView.bounds.size.width * scaleFactor),
static_cast<uint32_t>(mapView.bounds.size.height * scaleFactor) };
}
MLNBackendResource MLNMapViewMetalImpl::getObject() {
auto& resource = getResource<MLNMapViewMetalRenderableResource>();
auto renderPassDescriptor = resource.getRenderPassDescriptor().get();
return {
resource.mtlView,
resource.mtlView.device,
[MTLRenderPassDescriptor renderPassDescriptor],
resource.commandBuffer
};
}

View File

@@ -56,5 +56,6 @@ public:
void deleteView() override;
UIImage* snapshot() override;
void layoutChanged() override;
MLNBackendResource getObject() override;
// End implementation of MLNMapViewImpl
};

View File

@@ -252,3 +252,7 @@ EAGLContext* MLNMapViewOpenGLImpl::getEAGLContext() {
auto& resource = getResource<MLNMapViewOpenGLRenderableResource>();
return resource.context;
}
MLNBackendResource MLNMapViewOpenGLImpl::getObject() {
return MLNBackendResource();
}

View File

@@ -6,6 +6,7 @@
#import "MLNMapCamera.h"
#import "MLNTypes.h"
#import "MLNStyle.h"
#import "MLNBackendResource.h"
NS_ASSUME_NONNULL_BEGIN
@@ -2000,6 +2001,8 @@ MLN_EXPORT
*/
@property (nonatomic) MLNMapDebugMaskOptions debugMask;
- (MLNBackendResource)backendResource;
@end
NS_ASSUME_NONNULL_END

View File

@@ -7327,6 +7327,10 @@ static void *windowScreenContext = &windowScreenContext;
return _annotationViewReuseQueueByIdentifier[identifier];
}
- (MLNBackendResource)backendResource {
return _mbglView->getObject();
}
@end
// MARK: - IBAdditions methods

View File

@@ -46,6 +46,7 @@ FOUNDATION_EXPORT MLN_EXPORT const unsigned char MapboxVersionString[];
#import "MLNOfflinePack.h"
#import "MLNOfflineRegion.h"
#import "MLNOfflineStorage.h"
#import "MLNCustomStyleLayer.h"
#import "MLNOverlay.h"
#import "MLNPointAnnotation.h"
#import "MLNPointCollection.h"

View File

@@ -1,7 +1,7 @@
#import "MapDocument.h"
#import "AppDelegate.h"
#import "LimeGreenStyleLayer.h"
#import "CustomStyleLayerExample.h"
#import "DroppedPinAnnotation.h"
#import "MLNMapsnapshotter.h"
@@ -787,7 +787,7 @@ NSArray<id <MLNAnnotation>> *MBXFlattenedShapes(NSArray<id <MLNAnnotation>> *sha
[self.undoManager setActionName:@"Add Lime Green Layer"];
}
LimeGreenStyleLayer *layer = [[LimeGreenStyleLayer alloc] initWithIdentifier:@"mbx-custom"];
CustomStyleLayerExample *layer = [[CustomStyleLayerExample alloc] initWithIdentifier:@"mbx-custom"];
MLNStyleLayer *houseNumberLayer = [self.mapView.style layerWithIdentifier:@"housenum-label"];
if (houseNumberLayer) {
[self.mapView.style insertLayer:layer belowLayer:houseNumberLayer];

View File

@@ -19,7 +19,7 @@
"platform/macos/src/MLNAnnotationImage.m",
"platform/darwin/src/NSExpression+MLNAdditions.mm",
"platform/darwin/src/MLNFeature.mm",
"platform/darwin/src/MLNOpenGLStyleLayer.mm",
"platform/darwin/src/MLNCustomStyleLayer.mm",
"platform/macos/src/NSColor+MLNAdditions.mm",
"platform/macos/src/MLNAttributionButton.mm",
"platform/darwin/src/MLNFillStyleLayer.mm",
@@ -108,7 +108,7 @@
"MLNBackgroundStyleLayer.h": "platform/darwin/src/MLNBackgroundStyleLayer.h",
"MLNPointCollection.h": "platform/darwin/src/MLNPointCollection.h",
"MLNShape.h": "platform/darwin/src/MLNShape.h",
"MLNOpenGLStyleLayer.h": "platform/darwin/src/MLNOpenGLStyleLayer.h",
"MLNCustomStyleLayer.h": "platform/darwin/src/MLNCustomStyleLayer.h",
"MLNSource.h": "platform/darwin/src/MLNSource.h",
"MLNPolygon.h": "platform/darwin/src/MLNPolygon.h",
"MLNClockDirectionFormatter.h": "platform/darwin/src/MLNClockDirectionFormatter.h",
@@ -202,7 +202,7 @@
"NSDate+MLNAdditions.h": "platform/darwin/src/NSDate+MLNAdditions.h",
"NSColor+MLNAdditions.h": "platform/macos/src/NSColor+MLNAdditions.h",
"MLNMultiPoint_Private.h": "platform/darwin/src/MLNMultiPoint_Private.h",
"MLNOpenGLStyleLayer_Private.h": "platform/darwin/src/MLNOpenGLStyleLayer_Private.h",
"MLNCustomStyleLayer_Private.h": "platform/darwin/src/MLNCustomStyleLayer_Private.h",
"MLNPolygon_Private.h": "platform/darwin/src/MLNPolygon_Private.h",
"MLNShape_Private.h": "platform/darwin/src/MLNShape_Private.h",
"MLNTilePyramidOfflineRegion_Private.h": "platform/darwin/src/MLNTilePyramidOfflineRegion_Private.h",

View File

@@ -47,7 +47,7 @@ FOUNDATION_EXPORT MLN_EXPORT const unsigned char MapboxVersionString[];
#import "MLNBackgroundStyleLayer.h"
#import "MLNHeatmapStyleLayer.h"
#import "MLNHillshadeStyleLayer.h"
#import "MLNOpenGLStyleLayer.h"
#import "MLNCustomStyleLayer.h"
#import "MLNSource.h"
#import "MLNTileSource.h"
#import "MLNVectorTileSource.h"

View File

@@ -6,30 +6,27 @@
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/gfx/context.hpp>
#if MLN_RENDER_BACKEND_METAL
#include <mbgl/style/layers/mtl/custom_layer_render_parameters.hpp>
#endif
#include <memory>
namespace mbgl {
namespace gfx {
void DrawableCustomLayerHostTweaker::execute([[maybe_unused]] gfx::Drawable& drawable,
const PaintParameters& paintParameters) {
const mbgl::PaintParameters& paintParameters) {
// custom drawing
auto& context = paintParameters.context;
const TransformState& state = paintParameters.state;
context.resetState(paintParameters.depthModeForSublayer(0, gfx::DepthMaskType::ReadOnly),
paintParameters.colorModeForRenderPass());
style::CustomLayerRenderParameters parameters;
parameters.width = state.getSize().width;
parameters.height = state.getSize().height;
parameters.latitude = state.getLatLng().latitude();
parameters.longitude = state.getLatLng().longitude();
parameters.zoom = state.getZoom();
parameters.bearing = util::rad2deg(-state.getBearing());
parameters.pitch = state.getPitch();
parameters.fieldOfView = state.getFieldOfView();
mat4 projMatrix;
state.getProjMatrix(projMatrix);
parameters.projectionMatrix = projMatrix;
#if MLN_RENDER_BACKEND_METAL
style::mtl::CustomLayerRenderParameters parameters(paintParameters);
#else
style::CustomLayerRenderParameters parameters(paintParameters);
#endif
host->render(parameters);

View File

@@ -145,7 +145,12 @@ struct IndexBufferGL : public gfx::IndexBufferBase {
};
void DrawableGL::upload(gfx::UploadPass& uploadPass) {
if (isCustom) {
return;
}
if (!shader) {
Log::Warning(Event::General, "Missing shader for drawable " + util::toString(getID()) + "/" + getName());
assert(false);
return;
}

View File

@@ -479,6 +479,9 @@ MTL::VertexFormat mtlVertexTypeOf(gfx::AttributeDataType type) noexcept {
} // namespace
void Drawable::upload(gfx::UploadPass& uploadPass_) {
if (isCustom) {
return;
}
if (!shader) {
Log::Warning(Event::General, "Missing shader for drawable " + util::toString(getID()) + "/" + getName());
assert(false);

View File

@@ -15,9 +15,7 @@ RenderPass::RenderPass(CommandEncoder& commandEncoder_, const char* name, const
commandEncoder(commandEncoder_) {
auto& resource = descriptor.renderable.getResource<RenderableResource>();
if (!resource.getCommandBuffer()) {
resource.bind();
}
resource.bind();
if (const auto& buffer = resource.getCommandBuffer()) {
if (auto rpd = resource.getRenderPassDescriptor()) {

View File

@@ -19,9 +19,7 @@ UploadPass::UploadPass(gfx::Renderable& renderable, CommandEncoder& commandEncod
: commandEncoder(commandEncoder_) {
auto& resource = renderable.getResource<RenderableResource>();
if (!resource.getCommandBuffer()) {
resource.bind();
}
resource.bind();
if (const auto& buffer_ = resource.getCommandBuffer()) {
buffer = buffer_;

View File

@@ -12,6 +12,7 @@
#include <mbgl/platform/gl_functions.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/renderable_resource.hpp>
#include <mbgl/style/layers/custom_layer_render_parameters.hpp>
#endif
#if MLN_DRAWABLE_RENDERER
@@ -86,7 +87,6 @@ void RenderCustomLayer::render(PaintParameters& paintParameters) {
// TODO: remove cast
auto& glContext = static_cast<gl::Context&>(paintParameters.context);
const TransformState& state = paintParameters.state;
// Reset GL state to a known state so the CustomLayer always has a clean slate.
glContext.bindVertexArray = 0;
@@ -95,21 +95,7 @@ void RenderCustomLayer::render(PaintParameters& paintParameters) {
glContext.setColorMode(paintParameters.colorModeForRenderPass());
glContext.setCullFaceMode(gfx::CullFaceMode::disabled());
CustomLayerRenderParameters parameters;
parameters.width = state.getSize().width;
parameters.height = state.getSize().height;
parameters.latitude = state.getLatLng().latitude();
parameters.longitude = state.getLatLng().longitude();
parameters.zoom = state.getZoom();
parameters.bearing = util::rad2deg(-state.getBearing());
parameters.pitch = state.getPitch();
parameters.fieldOfView = state.getFieldOfView();
mat4 projMatrix;
state.getProjMatrix(projMatrix);
parameters.projectionMatrix = projMatrix;
MBGL_CHECK_ERROR(host->render(parameters));
MBGL_CHECK_ERROR(host->render(CustomLayerRenderParameters(paintParameters)));
// Reset the view back to our original one, just in case the CustomLayer
// changed the viewport or Framebuffer.

View File

@@ -350,7 +350,7 @@ public:
void init(gfx::Drawable&) override{};
void execute(gfx::Drawable& drawable, const PaintParameters& parameters) override {
virtual void execute(gfx::Drawable& drawable, const PaintParameters& parameters) override {
if (!drawable.getTileID().has_value()) {
return;
}

View File

@@ -13,8 +13,8 @@
namespace mbgl {
class PaintParameters;
class PatternAtlas;
class PaintParameters;
namespace gfx {
class UploadPass;

View File

@@ -9,6 +9,7 @@
#include <mbgl/gfx/renderable.hpp>
#include <mbgl/gfx/upload_pass.hpp>
#include <mbgl/programs/programs.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/pattern_atlas.hpp>
#include <mbgl/renderer/renderer_observer.hpp>
#include <mbgl/renderer/render_static_data.hpp>

View File

@@ -4,6 +4,7 @@
#include <mbgl/renderer/layers/render_custom_drawable_layer.hpp>
#include <mbgl/style/layer_observer.hpp>
#include <mbgl/gfx/context.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/change_request.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/gfx/cull_face_mode.hpp>

View File

@@ -0,0 +1,24 @@
#include <mbgl/style/layers/custom_layer_render_parameters.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/util/mat4.hpp>
namespace mbgl {
namespace style {
CustomLayerRenderParameters::CustomLayerRenderParameters(const mbgl::PaintParameters& paintParameters) {
const TransformState& state = paintParameters.state;
width = state.getSize().width;
height = state.getSize().height;
latitude = state.getLatLng().latitude();
longitude = state.getLatLng().longitude();
zoom = state.getZoom();
bearing = util::rad2deg(-state.getBearing());
pitch = state.getPitch();
fieldOfView = state.getFieldOfView();
mat4 projMatrix;
state.getProjMatrix(projMatrix);
projectionMatrix = projMatrix;
}
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,20 @@
#include <mbgl/style/layers/mtl/custom_layer_render_parameters.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/mtl/render_pass.hpp>
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>
namespace mbgl {
namespace style {
namespace mtl {
CustomLayerRenderParameters::CustomLayerRenderParameters(const mbgl::PaintParameters& paintParameters)
: mbgl::style::CustomLayerRenderParameters(paintParameters) {
const mbgl::mtl::RenderPass& renderPass = static_cast<mbgl::mtl::RenderPass&>(*paintParameters.renderPass);
encoder = renderPass.getMetalEncoder();
}
} // namespace mtl
} // namespace style
} // namespace mbgl

View File

@@ -15,6 +15,8 @@
#include <mbgl/util/mat4.hpp>
#include <mbgl/util/run_loop.hpp>
#include <memory>
using namespace mbgl;
using namespace mbgl::style;
using namespace mbgl::platform;