Skip to content

Commit

Permalink
Custom drawable layer v3 (#3210)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-cojocaru authored Feb 28, 2025
1 parent b09235b commit 7339fb9
Show file tree
Hide file tree
Showing 90 changed files with 3,932 additions and 464 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_circle.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_collision_box.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_collision_circle.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_custom_geometry.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_custom_symbol_icon.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_debug.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill.hpp
Expand Down Expand Up @@ -1217,6 +1218,7 @@ if(MLN_WITH_METAL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/clipping_mask.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/common.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/collision.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/custom_geometry.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/custom_symbol_icon.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/debug.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/fill.hpp
Expand All @@ -1226,6 +1228,7 @@ if(MLN_WITH_METAL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/hillshade.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/hillshade_prepare.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/line.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/location_indicator.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/raster.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/shader_group.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/shader_program.hpp
Expand Down Expand Up @@ -1256,6 +1259,7 @@ if(MLN_WITH_METAL)
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/circle.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/collision.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/clipping_mask.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/custom_geometry.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/custom_symbol_icon.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/debug.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/fill.cpp
Expand All @@ -1265,6 +1269,7 @@ if(MLN_WITH_METAL)
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/hillshade.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/hillshade_prepare.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/line.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/location_indicator.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/raster.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/symbol.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/widevector.cpp
Expand Down Expand Up @@ -1314,6 +1319,7 @@ if(MLN_WITH_VULKAN)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/clipping_mask.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/collision.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/common.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/custom_geometry.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/custom_symbol_icon.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/fill.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/vulkan/fill_extrusion.hpp
Expand Down Expand Up @@ -1354,6 +1360,7 @@ if(MLN_WITH_VULKAN)
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/circle.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/clipping_mask.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/collision.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/custom_geometry.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/custom_symbol_icon.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/debug.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/fill.cpp
Expand Down
8 changes: 8 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ http_archive(
urls = ["https://github.com/glfw/glfw/releases/download/3.4/glfw-3.4.zip"],
)

new_git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")

new_git_repository(
name = "tinyobjloader",
remote = "https://github.com/tinyobjloader/tinyobjloader.git",
branch = "release",
)

darwin_config = use_repo_rule("//platform/darwin:bazel/darwin_config_repository_rule.bzl", "darwin_config")

darwin_config(
Expand Down
6 changes: 6 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ MLN_GENERATED_OPENGL_SHADER_HEADERS = [
"include/mbgl/shaders/gl/drawable_circle.hpp",
"include/mbgl/shaders/gl/drawable_collision_box.hpp",
"include/mbgl/shaders/gl/drawable_collision_circle.hpp",
"include/mbgl/shaders/gl/drawable_custom_geometry.hpp",
"include/mbgl/shaders/gl/drawable_custom_symbol_icon.hpp",
"include/mbgl/shaders/gl/drawable_debug.hpp",
"include/mbgl/shaders/gl/drawable_fill.hpp",
Expand Down Expand Up @@ -1010,6 +1011,7 @@ MLN_DRAWABLES_HEADERS = [
"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_geometry_ubo.hpp",
"include/mbgl/shaders/custom_drawable_layer_ubo.hpp",
"include/mbgl/shaders/debug_layer_ubo.hpp",
"include/mbgl/shaders/fill_layer_ubo.hpp",
Expand Down Expand Up @@ -1081,6 +1083,7 @@ MLN_DRAWABLES_MTL_SOURCE = [
"src/mbgl/shaders/mtl/circle.cpp",
"src/mbgl/shaders/mtl/collision.cpp",
"src/mbgl/shaders/mtl/clipping_mask.cpp",
"src/mbgl/shaders/mtl/custom_geometry.cpp",
"src/mbgl/shaders/mtl/custom_symbol_icon.cpp",
"src/mbgl/shaders/mtl/debug.cpp",
"src/mbgl/shaders/mtl/fill.cpp",
Expand All @@ -1090,6 +1093,7 @@ MLN_DRAWABLES_MTL_SOURCE = [
"src/mbgl/shaders/mtl/hillshade.cpp",
"src/mbgl/shaders/mtl/hillshade_prepare.cpp",
"src/mbgl/shaders/mtl/line.cpp",
"src/mbgl/shaders/mtl/location_indicator.cpp",
"src/mbgl/shaders/mtl/raster.cpp",
"src/mbgl/shaders/mtl/symbol.cpp",
"src/mbgl/shaders/mtl/widevector.cpp",
Expand Down Expand Up @@ -1120,6 +1124,7 @@ MLN_DRAWABLES_MTL_HEADERS = [
"include/mbgl/shaders/mtl/clipping_mask.hpp",
"include/mbgl/shaders/mtl/collision.hpp",
"include/mbgl/shaders/mtl/common.hpp",
"include/mbgl/shaders/mtl/custom_geometry.hpp",
"include/mbgl/shaders/mtl/custom_symbol_icon.hpp",
"include/mbgl/shaders/mtl/debug.hpp",
"include/mbgl/shaders/mtl/fill.hpp",
Expand All @@ -1129,6 +1134,7 @@ MLN_DRAWABLES_MTL_HEADERS = [
"include/mbgl/shaders/mtl/hillshade.hpp",
"include/mbgl/shaders/mtl/hillshade_prepare.hpp",
"include/mbgl/shaders/mtl/line.hpp",
"include/mbgl/shaders/mtl/location_indicator.hpp",
"include/mbgl/shaders/mtl/raster.hpp",
"include/mbgl/shaders/mtl/shader_group.hpp",
"include/mbgl/shaders/mtl/shader_program.hpp",
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class Context {
bool persistent = false,
bool ssbo = false) = 0;

virtual UniqueUniformBufferArray createLayerUniformBufferArray() = 0;

/// Get the generic shader with the specified name
virtual gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) = 0;

Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/gfx/uniform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace gfx {
class Context;
class UniformBuffer;
class UniformBufferArray;
class RenderPass;

using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
using UniqueUniformBuffer = std::unique_ptr<UniformBuffer>;
Expand Down Expand Up @@ -75,6 +76,8 @@ class UniformBufferArray {
createOrUpdate(id, data, sizeof(T), context, persistent);
}

virtual void bind(gfx::RenderPass& renderPass) = 0;

UniformBufferArray& operator=(UniformBufferArray&&);
UniformBufferArray& operator=(const UniformBufferArray&);

Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/gl/uniform_buffer_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class UniformBufferArrayGL final : public gfx::UniformBufferArray {
void bind() const;
void unbind() const;

void bind(gfx::RenderPass&) override { bind(); }

private:
std::unique_ptr<gfx::UniformBuffer> copy(const gfx::UniformBuffer& uniformBuffers) override {
return std::make_unique<UniformBufferGL>(static_cast<const UniformBufferGL&>(uniformBuffers).clone());
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/mtl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class VertexBufferResource;

using UniqueShaderProgram = std::unique_ptr<ShaderProgram>;
using UniqueVertexBufferResource = std::unique_ptr<VertexBufferResource>;
using UniqueUniformBufferArray = std::unique_ptr<gfx::UniformBufferArray>;

class Context final : public gfx::Context {
public:
Expand Down Expand Up @@ -88,6 +89,8 @@ class Context final : public gfx::Context {
bool persistent = false,
bool ssbo = false) override;

UniqueUniformBufferArray createLayerUniformBufferArray() override;

gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) override;

TileLayerGroupPtr createTileLayerGroup(int32_t layerIndex, std::size_t initialCapacity, std::string name) override;
Expand Down
1 change: 0 additions & 1 deletion include/mbgl/mtl/index_buffer_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace mtl {

class IndexBufferResource : public gfx::IndexBufferResource {
public:
IndexBufferResource() noexcept = delete;
IndexBufferResource(BufferResource&&) noexcept;
IndexBufferResource(IndexBufferResource&& other) noexcept
: buffer(std::move(other.buffer)) {}
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/mtl/uniform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class UniformBufferArray final : public gfx::UniformBufferArray {
return *this;
}

void bind(RenderPass& renderPass) const noexcept;
void unbind(RenderPass&) const noexcept {}
void bindMtl(RenderPass&) const noexcept;
void bind(gfx::RenderPass& renderPass) override;

private:
gfx::UniqueUniformBuffer copy(const gfx::UniformBuffer& buffer) override {
Expand Down
1 change: 0 additions & 1 deletion include/mbgl/mtl/vertex_buffer_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace mtl {

class VertexBufferResource : public gfx::VertexBufferResource {
public:
VertexBufferResource() noexcept = delete;
VertexBufferResource(BufferResource&&) noexcept;
VertexBufferResource(VertexBufferResource&& other) noexcept
: buffer(std::move(other.buffer)) {}
Expand Down
16 changes: 16 additions & 0 deletions include/mbgl/shaders/custom_geometry_ubo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <mbgl/shaders/layer_ubo.hpp>

namespace mbgl {
namespace shaders {

struct alignas(16) CustomGeometryDrawableUBO {
/* 0 */ std::array<float, 4 * 4> matrix;
/* 64 */ Color color;
/* 80 */
};
static_assert(sizeof(CustomGeometryDrawableUBO) == 5 * 16);

} // namespace shaders
} // namespace mbgl
39 changes: 39 additions & 0 deletions include/mbgl/shaders/gl/drawable_custom_geometry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Generated code, do not modify this file!
#pragma once
#include <mbgl/shaders/shader_source.hpp>

namespace mbgl {
namespace shaders {

template <>
struct ShaderSource<BuiltIn::CustomGeometryShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "CustomGeometryShader";
static constexpr const char* vertex = R"(layout (std140) uniform CustomGeometryDrawableUBO {
mat4 u_matrix;
vec4 u_color;
};
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec2 a_uv;
out vec2 frag_uv;
void main() {
frag_uv = a_uv;
gl_Position = u_matrix * vec4(a_pos, 1.0);
})";
static constexpr const char* fragment = R"(layout (std140) uniform CustomGeometryDrawableUBO {
mat4 u_matrix;
vec4 u_color;
};
in vec2 frag_uv;
uniform sampler2D u_image;
void main() {
fragColor = texture(u_image, frag_uv) * u_color;
})";
};

} // namespace shaders
} // namespace mbgl
20 changes: 18 additions & 2 deletions include/mbgl/shaders/gl/drawable_location_indicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::LocationIndicatorShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LocationIndicatorShader";
static constexpr const char* vertex = R"()";
static constexpr const char* fragment = R"()";
static constexpr const char* vertex = R"(layout (std140) uniform LocationIndicatorDrawableUBO {
mat4 u_matrix;
vec4 u_color;
};
layout(location = 0) in vec2 a_pos;
void main() {
gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);
})";
static constexpr const char* fragment = R"(layout (std140) uniform LocationIndicatorDrawableUBO {
mat4 u_matrix;
vec4 u_color;
};
void main() {
fragColor = u_color;
})";
};

} // namespace shaders
Expand Down
27 changes: 25 additions & 2 deletions include/mbgl/shaders/gl/drawable_location_indicator_textured.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,31 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::LocationIndicatorTexturedShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LocationIndicatorTexturedShader";
static constexpr const char* vertex = R"()";
static constexpr const char* fragment = R"()";
static constexpr const char* vertex = R"(layout (std140) uniform LocationIndicatorDrawableUBO {
mat4 u_matrix;
vec4 u_color;
};
layout(location = 0) in vec2 a_pos;
layout(location = 1) in vec2 a_uv;
out vec2 frag_uv;
void main() {
frag_uv = a_uv;
gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);
})";
static constexpr const char* fragment = R"(layout (std140) uniform LocationIndicatorDrawableUBO {
mat4 u_matrix;
vec4 u_color;
};
in vec2 frag_uv;
uniform sampler2D u_image;
void main() {
fragColor = texture(u_image, frag_uv);
})";
};

} // namespace shaders
Expand Down
21 changes: 21 additions & 0 deletions include/mbgl/shaders/gl/shader_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ struct ShaderInfo<BuiltIn::CollisionCircleShader, gfx::Backend::Type::OpenGL> {
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::CustomGeometryShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
static const std::vector<UniformBlockInfo> uniformBlocks;
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
Expand Down Expand Up @@ -184,6 +191,20 @@ struct ShaderInfo<BuiltIn::LineShader, gfx::Backend::Type::OpenGL> {
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::LocationIndicatorShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
static const std::vector<UniformBlockInfo> uniformBlocks;
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::LocationIndicatorTexturedShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
static const std::vector<UniformBlockInfo> uniformBlocks;
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::RasterShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
Expand Down
Loading

0 comments on commit 7339fb9

Please sign in to comment.