Custom Drawable Layer: Symbol Icon (#1908)

This commit is contained in:
Stefan Karschti
2024-02-16 17:39:47 +02:00
committed by GitHub
parent 612b256821
commit d6db72e8ba
32 changed files with 789 additions and 30 deletions

View File

@@ -0,0 +1,167 @@
#pragma once
#include <mbgl/gfx/backend.hpp>
#include <mbgl/gfx/command_encoder.hpp>
#include <mbgl/gfx/draw_scope.hpp>
#include <mbgl/gfx/program.hpp>
#include <mbgl/gfx/renderbuffer.hpp>
#include <mbgl/gfx/rendering_stats.hpp>
#include <mbgl/gfx/texture.hpp>
#include <mbgl/gfx/types.hpp>
#if MLN_DRAWABLE_RENDERER
#include <mbgl/gfx/uniform_buffer.hpp>
#endif
#include <memory>
#include <string>
namespace mbgl {
class PaintParameters;
class ProgramParameters;
#if MLN_DRAWABLE_RENDERER
class TileLayerGroup;
class LayerGroup;
class RenderTarget;
using TileLayerGroupPtr = std::shared_ptr<TileLayerGroup>;
using LayerGroupPtr = std::shared_ptr<LayerGroup>;
using RenderTargetPtr = std::shared_ptr<RenderTarget>;
#endif
namespace gfx {
class OffscreenTexture;
class ShaderRegistry;
#if MLN_DRAWABLE_RENDERER
class Drawable;
class DrawableBuilder;
class ShaderProgramBase;
class Texture2D;
class VertexAttributeArray;
using DrawablePtr = std::shared_ptr<Drawable>;
using ShaderProgramBasePtr = std::shared_ptr<ShaderProgramBase>;
using Texture2DPtr = std::shared_ptr<Texture2D>;
using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
using UniqueDrawableBuilder = std::unique_ptr<DrawableBuilder>;
using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;
#endif
class Context {
protected:
Context(uint32_t maximumVertexBindingCount_)
: maximumVertexBindingCount(maximumVertexBindingCount_) {}
public:
static constexpr const uint32_t minimumRequiredVertexBindingCount = 8;
const uint32_t maximumVertexBindingCount;
Context(Context&&) = delete;
Context(const Context&) = delete;
Context& operator=(Context&& other) = delete;
Context& operator=(const Context& other) = delete;
virtual ~Context() = default;
virtual void beginFrame() = 0;
virtual void endFrame() = 0;
/// Called at the end of a frame.
virtual void performCleanup() = 0;
/// Called when the app receives a memory warning and before it goes to the background.
virtual void reduceMemoryUsage() = 0;
virtual std::unique_ptr<OffscreenTexture> createOffscreenTexture(Size, TextureChannelDataType) = 0;
/// Creates an empty texture with the specified dimensions.
Texture createTexture(const Size size,
TexturePixelType format = TexturePixelType::RGBA,
TextureChannelDataType type = TextureChannelDataType::UnsignedByte) {
return {size, createTextureResource(size, format, type)};
}
template <RenderbufferPixelType pixelType>
Renderbuffer<pixelType> createRenderbuffer(const Size size) {
return {size, createRenderbufferResource(pixelType, size)};
}
DrawScope createDrawScope() { return DrawScope{createDrawScopeResource()}; }
virtual std::unique_ptr<CommandEncoder> createCommandEncoder() = 0;
gfx::RenderingStats& renderingStats() { return stats; }
const gfx::RenderingStats& renderingStats() const { return stats; }
#if !defined(NDEBUG)
virtual void visualizeStencilBuffer() = 0;
virtual void visualizeDepthBuffer(float depthRangeSize) = 0;
#endif
virtual void clearStencilBuffer(int32_t) = 0;
/// Sets dirty state
virtual void setDirtyState() = 0;
#if MLN_DRAWABLE_RENDERER
/// Create a new vertex attribute array
virtual gfx::VertexAttributeArrayPtr createVertexAttributeArray() const = 0;
/// Create a new drawable builder
virtual UniqueDrawableBuilder createDrawableBuilder(std::string name) = 0;
/// Create a new uniform buffer
/// @param data The data to copy, may be `nullptr`
/// @param size The size of the buffer
/// @param persistent Performance hint, optimize for few or many uses
virtual UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent = false) = 0;
/// Get the generic shader with the specified name
virtual gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) = 0;
/// Create a tile layer group implementation
virtual TileLayerGroupPtr createTileLayerGroup(int32_t layerIndex,
std::size_t initialCapacity,
std::string name) = 0;
/// Create a layer group implementation
virtual LayerGroupPtr createLayerGroup(int32_t layerIndex, std::size_t initialCapacity, std::string name) = 0;
/// Create a texture
virtual Texture2DPtr createTexture2D() = 0;
/// Create a render target
virtual RenderTargetPtr createRenderTarget(const Size size, const TextureChannelDataType type) = 0;
/// Resets the context state to defaults
virtual void resetState(gfx::DepthMode depthMode, gfx::ColorMode colorMode) = 0;
/// Update the uniform buffer with the provided data if it already exists, otherwise create it.
/// @return True if the buffer was created, false if it was updated
virtual bool emplaceOrUpdateUniformBuffer(gfx::UniformBufferPtr&,
const void* data,
std::size_t size,
bool persistent = false) = 0;
/// `emplaceOrUpdateUniformBuffer` with type inference
template <typename T>
std::enable_if_t<!std::is_pointer_v<T>, bool> emplaceOrUpdateUniformBuffer(gfx::UniformBufferPtr& ptr,
const T* data,
bool persistent = false) {
return emplaceOrUpdateUniformBuffer(ptr, data, sizeof(T), persistent);
}
#endif
protected:
virtual std::unique_ptr<TextureResource> createTextureResource(Size, TexturePixelType, TextureChannelDataType) = 0;
virtual std::unique_ptr<RenderbufferResource> createRenderbufferResource(RenderbufferPixelType, Size) = 0;
virtual std::unique_ptr<DrawScopeResource> createDrawScopeResource() = 0;
gfx::RenderingStats stats;
};
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,36 @@
#pragma once
#include <mbgl/shaders/layer_ubo.hpp>
namespace mbgl {
namespace shaders {
/// Custom Symbol Icon matrix
struct alignas(16) CustomSymbolIconDrawableUBO {
/* 0 */ std::array<float, 4 * 4> matrix;
/* 64 */
};
static_assert(sizeof(CustomSymbolIconDrawableUBO) == 4 * 16);
/// Custom Symbol Icon Parameters
struct alignas(16) CustomSymbolIconParametersUBO {
/* 0 */ std::array<float, 2> extrude_scale;
/* 8 */ std::array<float, 2> anchor;
/* 16 */ float angle_degrees;
/* 20 */ uint32_t scale_with_map;
/* 24 */ uint32_t pitch_with_map;
/* 28 */ float camera_to_center_distance;
/* 32 */ float aspect_ratio;
/* 36 */ float pad0, pad1, pad2;
/* 48 */
};
static_assert(sizeof(CustomSymbolIconParametersUBO) == 3 * 16);
enum {
idCustomSymbolIconDrawableUBO,
idCustomSymbolIconParametersUBO,
customDrawableUBOCount
};
} // namespace shaders
} // namespace mbgl

View File

@@ -0,0 +1,92 @@
// Generated code, do not modify this file!
#pragma once
#include <mbgl/shaders/shader_source.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "CustomSymbolIconShader";
static constexpr const char* vertex = R"(layout(location = 0) in vec2 a_pos;
layout(location = 1) in vec2 a_tex;
layout(std140) uniform CustomSymbolIconDrawableUBO {
highp mat4 u_matrix;
};
layout(std140) uniform CustomSymbolIconParametersUBO {
highp vec2 u_extrude_scale;
highp vec2 u_anchor;
highp float u_angle_degrees;
bool u_scale_with_map;
bool u_pitch_with_map;
highp float u_camera_to_center_distance;
highp float u_aspect_ratio;
highp float pad0, pad1, pad3;
};
out vec2 v_tex;
vec2 rotateVec2(vec2 v, float angle) {
float cosA = cos(angle);
float sinA = sin(angle);
return vec2(v.x * cosA - v.y * sinA, v.x * sinA + v.y * cosA);
}
vec2 ellipseRotateVec2(vec2 v, float angle, float radiusRatio /* A/B */) {
float cosA = cos(angle);
float sinA = sin(angle);
float invRatio = 1.0 / radiusRatio;
return vec2(v.x * cosA - radiusRatio * v.y * sinA, invRatio * v.x * sinA + v.y * cosA);
}
void main() {
// decode the extrusion vector (-1, -1) to (1, 1)
vec2 extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);
// make anchor relative to (0.5, 0.5) and corners in range (-1, -1) to (1, 1)
vec2 anchor = (u_anchor - vec2(0.5, 0.5)) * 2.0;
// decode center
vec2 center = floor(a_pos * 0.5);
// rotate extrusion around anchor
float angle = radians(-u_angle_degrees);
vec2 corner = extrude - anchor;
// compute
if (u_pitch_with_map) {
if (u_scale_with_map) {
corner *= u_extrude_scale;
} else {
vec4 projected_center = u_matrix * vec4(center, 0, 1);
corner *= u_extrude_scale * (projected_center.w / u_camera_to_center_distance);
}
corner = center + rotateVec2(corner, angle);
gl_Position = u_matrix * vec4(corner, 0, 1);
} else {
gl_Position = u_matrix * vec4(center, 0, 1);
if (u_scale_with_map) {
gl_Position.xy += ellipseRotateVec2(corner * u_extrude_scale * u_camera_to_center_distance, angle, u_aspect_ratio);
} else {
gl_Position.xy += ellipseRotateVec2(corner * u_extrude_scale * gl_Position.w, angle, u_aspect_ratio);
}
}
// texture coordinates
v_tex = a_tex;
}
)";
static constexpr const char* fragment = R"(uniform sampler2D u_texture;
in vec2 v_tex;
void main() {
fragColor = texture(u_texture, v_tex);
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -54,6 +54,12 @@ struct ShaderInfo<BuiltIn::CollisionCircleShader, gfx::Backend::Type::OpenGL> {
static const std::vector<TextureInfo> textures;
};
template <>
struct ShaderInfo<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::OpenGL> {
static const std::vector<UniformBlockInfo> uniformBlocks;
static const std::vector<TextureInfo> textures;
};
template <>
struct ShaderInfo<BuiltIn::DebugShader, gfx::Backend::Type::OpenGL> {
static const std::vector<UniformBlockInfo> uniformBlocks;

View File

@@ -0,0 +1,105 @@
#pragma once
#include <mbgl/shaders/mtl/common.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/custom_drawable_layer_ubo.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CustomSymbolIconShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
static const std::array<AttributeInfo, 2> attributes;
static const std::array<UniformBlockInfo, 2> uniforms;
static const std::array<TextureInfo, 1> textures;
static constexpr auto source = R"(
struct alignas(16) CustomSymbolIconDrawableUBO {
float4x4 matrix;
};
struct alignas(16) CustomSymbolIconParametersUBO {
float2 extrude_scale;
float2 anchor;
float angle_degrees;
int scale_with_map;
int pitch_with_map;
float camera_to_center_distance;
float aspect_ratio;
float pad0, pad1, pad3;
};
struct VertexStage {
float2 a_pos [[attribute(0)]];
float2 a_tex [[attribute(1)]];
};
struct FragmentStage {
float4 position [[position, invariant]];
half2 tex;
};
float2 rotateVec2(float2 v, float angle) {
float cosA = cos(angle);
float sinA = sin(angle);
return float2(v.x * cosA - v.y * sinA, v.x * sinA + v.y * cosA);
}
float2 ellipseRotateVec2(float2 v, float angle, float radiusRatio /* A/B */) {
float cosA = cos(angle);
float sinA = sin(angle);
float invRatio = 1.0 / radiusRatio;
return float2(v.x * cosA - radiusRatio * v.y * sinA, invRatio * v.x * sinA + v.y * cosA);
}
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const CustomSymbolIconDrawableUBO& drawable [[buffer(2)]],
device const CustomSymbolIconParametersUBO& parameters [[buffer(3)]]) {
const float2 extrude = glMod(float2(vertx.a_pos), 2.0) * 2.0 - 1.0;
const float2 anchor = (parameters.anchor - float2(0.5, 0.5)) * 2.0;
const float2 center = floor(float2(vertx.a_pos) * 0.5);
const float angle = radians(-parameters.angle_degrees);
float2 corner = extrude - anchor;
float4 position;
if (parameters.pitch_with_map) {
if (parameters.scale_with_map) {
corner *= parameters.extrude_scale;
} else {
float4 projected_center = drawable.matrix * float4(center, 0, 1);
corner *= parameters.extrude_scale * (projected_center.w / parameters.camera_to_center_distance);
}
corner = center + rotateVec2(corner, angle);
position = drawable.matrix * float4(corner, 0, 1);
} else {
position = drawable.matrix * float4(center, 0, 1);
const float factor = parameters.scale_with_map ? parameters.camera_to_center_distance : position.w;
position.xy += ellipseRotateVec2(corner * parameters.extrude_scale * factor, angle, parameters.aspect_ratio);
}
return {
.position = position,
.tex = half2(vertx.a_tex)
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
texture2d<float, access::sample> image [[texture(0)]],
sampler image_sampler [[sampler(0)]]) {
#if defined(OVERDRAW_INSPECTOR)
return half4(1.0);
#endif
return half4(image.sample(image_sampler, float2(in.tex)));
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -3,6 +3,7 @@
#include <mbgl/shaders/background_layer_ubo.hpp>
#include <mbgl/shaders/circle_layer_ubo.hpp>
#include <mbgl/shaders/collision_layer_ubo.hpp>
#include <mbgl/shaders/custom_drawable_layer_ubo.hpp>
#include <mbgl/shaders/debug_layer_ubo.hpp>
#include <mbgl/shaders/fill_layer_ubo.hpp>
#include <mbgl/shaders/fill_extrusion_layer_ubo.hpp>
@@ -22,6 +23,7 @@ namespace shaders {
static constexpr auto maxUBOCountPerShader = std::max({static_cast<size_t>(backgroundUBOCount),
static_cast<size_t>(circleUBOCount),
static_cast<size_t>(collisionUBOCount),
static_cast<size_t>(customDrawableUBOCount),
static_cast<size_t>(debugUBOCount),
static_cast<size_t>(fillUBOCount),
static_cast<size_t>(fillOutlineUBOCount),
@@ -53,6 +55,11 @@ enum {
collisionTextureCount
};
enum {
idCustomSymbolIconTexture,
customSymbolIconTextureCount
};
enum {
idDebugOverlayTexture,
debugTextureCount
@@ -99,6 +106,7 @@ enum {
static constexpr auto maxTextureCountPerShader = std::max({static_cast<size_t>(backgroundTextureCount),
static_cast<size_t>(circleTextureCount),
static_cast<size_t>(collisionTextureCount),
static_cast<size_t>(customSymbolIconTextureCount),
static_cast<size_t>(debugTextureCount),
static_cast<size_t>(fillTextureCount),
static_cast<size_t>(fillExtrusionTextureCount),

View File

@@ -28,6 +28,7 @@
#include <mbgl/shaders/gl/drawable_symbol_icon.hpp>
#include <mbgl/shaders/gl/drawable_symbol_sdf.hpp>
#include <mbgl/shaders/gl/drawable_symbol_text_and_icon.hpp>
#include <mbgl/shaders/gl/drawable_custom_symbol_icon.hpp>
#include <mbgl/shaders/gl/prelude.hpp>
#include <mbgl/shaders/gl/background.hpp>
#include <mbgl/shaders/gl/background_pattern.hpp>

View File

@@ -34,6 +34,7 @@ enum class BuiltIn {
SymbolIconShader,
SymbolSDFIconShader,
SymbolTextAndIconShader,
CustomSymbolIconShader,
Prelude,
BackgroundProgram,
BackgroundPatternProgram,

View File

@@ -7,6 +7,8 @@
#include <mbgl/renderer/render_tree.hpp>
#include <mbgl/renderer/change_request.hpp>
#include <mbgl/gfx/drawable_builder.hpp>
#include <mbgl/gfx/texture2d.hpp>
#include <mbgl/gfx/context.hpp>
#include <array>
#include <memory>
@@ -46,6 +48,16 @@ public:
float opacity = 1.f;
};
struct SymbolOptions {
Size size;
gfx::Texture2DPtr texture;
std::array<float, 2> anchor{0.5f, 0.5f};
std::array<std::array<float, 2>, 2> textureCoordinates{{{0, 0}, {1, 1}}};
float angleDegrees{.0f};
bool scaleWithMap{false};
bool pitchWithMap{false};
};
public:
/// @brief Construct a new Interface object (internal core use only)
Interface(RenderLayer& layer,
@@ -84,6 +96,13 @@ public:
*/
void setFillOptions(const FillOptions& options);
/**
* @brief Set the Symbol options
*
* @param options
*/
void setSymbolOptions(const SymbolOptions& options);
/**
* @brief Add a polyline
*
@@ -92,8 +111,20 @@ public:
*/
void addPolyline(const GeometryCoordinates& coordinates);
/**
* @brief Add a multipolygon area fill
*
* @param geometry a collection of rings with optional holes
*/
void addFill(const GeometryCollection& geometry);
/**
* @brief Add a symbol
*
* @param point
*/
void addSymbol(const GeometryCoordinate& point);
/**
* @brief Finish the current drawable building session
*
@@ -113,15 +144,20 @@ public:
private:
gfx::ShaderPtr lineShaderDefault() const;
gfx::ShaderPtr fillShaderDefault() const;
gfx::ShaderPtr symbolShaderDefault() const;
std::unique_ptr<gfx::DrawableBuilder> createBuilder(const std::string& name, gfx::ShaderPtr shader) const;
gfx::ShaderPtr lineShader;
gfx::ShaderPtr fillShader;
std::unique_ptr<gfx::DrawableBuilder> builder;
std::optional<OverscaledTileID> tileID;
gfx::ShaderPtr lineShader;
gfx::ShaderPtr fillShader;
gfx::ShaderPtr symbolShader;
LineOptions lineOptions;
FillOptions fillOptions;
SymbolOptions symbolOptions;
};
class CustomDrawableLayer final : public Layer {