Skip to content

Commit

Permalink
Use drawables for location indicator layer (#2836)
Browse files Browse the repository at this point in the history
Co-authored-by: Bart Louwers <bart.louwers@gmail.com>
  • Loading branch information
adrian-cojocaru and louwers authored Oct 8, 2024
1 parent 37bc023 commit 156872f
Show file tree
Hide file tree
Showing 22 changed files with 847 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/hillshade_prepare_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/line_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/line_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/location_indicator_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/location_indicator_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/raster_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/raster_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp
Expand Down Expand Up @@ -1135,6 +1137,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/background_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/circle_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/collision_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/common_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/layers/custom_drawable_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/layermanager/custom_drawable_layer_factory.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/debug_layer_ubo.hpp
Expand Down Expand Up @@ -1351,6 +1354,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/common.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/debug.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/fill.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/vulkan/heatmap.cpp
Expand Down
1 change: 1 addition & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,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/common_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
3 changes: 3 additions & 0 deletions include/mbgl/gfx/texture2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Texture2D {
TextureWrapType wrapU{TextureWrapType::Clamp};
/// Wrapping behavior along V coordinate
TextureWrapType wrapV{TextureWrapType::Clamp};

uint8_t maxAnisotropy{1};
bool mipmapped{false};
};

public:
Expand Down
20 changes: 20 additions & 0 deletions include/mbgl/shaders/common_ubo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <mbgl/shaders/layer_ubo.hpp>

namespace mbgl {
namespace shaders {

struct alignas(16) CommonUBO {
std::array<float, 4 * 4> matrix;
Color color;
};
static_assert(sizeof(CommonUBO) % 16 == 0);

enum {
idCommonUBO = globalUBOCount,
commonUBOCount
};

} // namespace shaders
} // namespace mbgl
15 changes: 15 additions & 0 deletions include/mbgl/shaders/shader_defines.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <mbgl/shaders/common_ubo.hpp>
#include <mbgl/shaders/background_layer_ubo.hpp>
#include <mbgl/shaders/circle_layer_ubo.hpp>
#include <mbgl/shaders/collision_layer_ubo.hpp>
Expand Down Expand Up @@ -31,6 +32,7 @@ static constexpr auto maxUBOCountPerShader = std::max({static_cast<size_t>(backg
static_cast<size_t>(circleUBOCount),
static_cast<size_t>(clippingMaskUBOCount),
static_cast<size_t>(collisionUBOCount),
static_cast<size_t>(commonUBOCount),
static_cast<size_t>(customSymbolUBOCount),
static_cast<size_t>(debugUBOCount),
static_cast<size_t>(fillUBOCount),
Expand Down Expand Up @@ -62,6 +64,11 @@ enum {
collisionTextureCount
};

enum {
idCommonTexture,
commonTextureCount
};

enum {
idCustomSymbolImageTexture,
customSymbolTextureCount
Expand Down Expand Up @@ -114,6 +121,7 @@ static constexpr auto maxTextureCountPerShader = std::max({static_cast<size_t>(b
static_cast<size_t>(circleTextureCount),
static_cast<size_t>(clippingMaskTextureCount),
static_cast<size_t>(collisionTextureCount),
static_cast<size_t>(commonTextureCount),
static_cast<size_t>(customSymbolTextureCount),
static_cast<size_t>(debugTextureCount),
static_cast<size_t>(fillTextureCount),
Expand Down Expand Up @@ -159,6 +167,12 @@ enum {
collisionVertexAttributeCount
};

enum {
idCommonPosVertexAttribute,
idCommonTexVertexAttribute,
commonVertexAttributeCount
};

enum {
idCustomSymbolPosVertexAttribute,
idCustomSymbolTexVertexAttribute,
Expand Down Expand Up @@ -276,6 +290,7 @@ static constexpr auto maxVertexAttributeCountPerShader = std::max({
static_cast<size_t>(circleVertexAttributeCount),
static_cast<size_t>(clippingMaskVertexAttributeCount),
static_cast<size_t>(collisionVertexAttributeCount),
static_cast<size_t>(commonVertexAttributeCount),
static_cast<size_t>(customSymbolVertexAttributeCount),
static_cast<size_t>(debugVertexAttributeCount),
static_cast<size_t>(fillVertexAttributeCount),
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/shaders/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ enum class BuiltIn {
CircleShader,
CollisionBoxShader,
CollisionCircleShader,
CommonShader,
CommonTexturedShader,
DebugShader,
FillShader,
FillOutlineShader,
Expand Down
78 changes: 78 additions & 0 deletions include/mbgl/shaders/vulkan/common.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/vulkan/shader_program.hpp>

namespace mbgl {
namespace shaders {
Expand Down Expand Up @@ -100,5 +101,82 @@ layout(set = 0, binding = 0) uniform GlobalPaintParamsUBO {
)";
};

template <>
struct ShaderSource<BuiltIn::CommonShader, gfx::Backend::Type::Vulkan> {
static constexpr const char* name = "CommonShader";

static const std::array<UniformBlockInfo, 1> uniforms;
static const std::array<AttributeInfo, 1> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static constexpr std::array<TextureInfo, 0> textures{};

static constexpr auto vertex = R"(
layout(location = 0) in vec2 in_position;
layout(set = 0, binding = 1) uniform CommonUBO {
mat4 matrix;
vec4 color;
} ubo;
void main() {
gl_Position = ubo.matrix * vec4(in_position, 0, 1);
gl_Position.y *= -1.0;
}
)";

static constexpr auto fragment = R"(
layout(location = 0) out vec4 out_color;
layout(set = 0, binding = 1) uniform CommonUBO {
mat4 matrix;
vec4 color;
} ubo;
void main() {
out_color = ubo.color;
}
)";
};

template <>
struct ShaderSource<BuiltIn::CommonTexturedShader, gfx::Backend::Type::Vulkan> {
static constexpr const char* name = "CommonTexturedShader";

static const std::array<UniformBlockInfo, 1> uniforms;
static const std::array<AttributeInfo, 2> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;

static constexpr auto vertex = R"(
layout(location = 0) in vec2 in_position;
layout(location = 1) in vec2 in_texcoord;
layout(set = 0, binding = 1) uniform CommonUBO {
mat4 matrix;
vec4 color;
} ubo;
layout(location = 0) out vec2 frag_uv;
void main() {
gl_Position = ubo.matrix * vec4(in_position, 0, 1);
gl_Position.y *= -1.0;
frag_uv = in_texcoord;
}
)";

static constexpr auto fragment = R"(
layout(location = 0) in vec2 frag_uv;
layout(location = 0) out vec4 out_color;
layout(set = 1, binding = 0) uniform sampler2D image_sampler;
void main() {
out_color = texture(image_sampler, frag_uv);
}
)";
};

} // namespace shaders
} // namespace mbgl
3 changes: 3 additions & 0 deletions include/mbgl/vulkan/texture2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class Texture2D : public gfx::Texture2D {
void transitionToShaderReadLayout(const vk::UniqueCommandBuffer&);
void transitionToGeneralLayout(const vk::UniqueCommandBuffer&);

uint32_t getMipLevels() const;
void generateMips(const vk::UniqueCommandBuffer& buffer);

private:
Context& context;

Expand Down
2 changes: 1 addition & 1 deletion platform/android/gradle/download-vulkan-validation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ task unzip(dependsOn: download, type: Copy) {

android.sourceSets.vulkan.jniLibs {
srcDir(tasks.unzip)
}
}
2 changes: 2 additions & 0 deletions platform/default/src/mbgl/layermanager/layer_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ LayerManagerDefault::LayerManagerDefault() {
#if !defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
addLayerType(std::make_unique<CustomLayerFactory>());
#endif
#endif
#if defined(MLN_RENDER_BACKEND_OPENGL) || MLN_RENDER_BACKEND_VULKAN
#if !defined(MBGL_LAYER_LOCATION_INDICATOR_DISABLE_ALL)
addLayerType(std::make_unique<LocationIndicatorLayerFactory>());
#endif
Expand Down
7 changes: 3 additions & 4 deletions platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@

using namespace std::numbers;

#if defined(MLN_RENDER_BACKEND_OPENGL) && !defined(MBGL_LAYER_LOCATION_INDICATOR_DISABLE_ALL)
#include <mbgl/style/layers/location_indicator_layer.hpp>
#ifdef ENABLE_LOCATION_INDICATOR

namespace {
const std::string mbglPuckAssetsPath{MAPBOX_PUCK_ASSETS_PATH};
Expand Down Expand Up @@ -1244,7 +1243,7 @@ void GLFWView::toggleCustomSource() {
void GLFWView::toggleLocationIndicatorLayer() {
MLN_TRACE_FUNC();

#if defined(MLN_RENDER_BACKEND_OPENGL) && !defined(MBGL_LAYER_LOCATION_INDICATOR_DISABLE_ALL)
#ifdef ENABLE_LOCATION_INDICATOR
puck = static_cast<mbgl::style::LocationIndicatorLayer *>(map->getStyle().getLayer("puck"));
static const mbgl::LatLng puckLocation{35.683389, 139.76525}; // A location on the crossing of 4 tiles
if (puck == nullptr) {
Expand Down Expand Up @@ -1309,7 +1308,7 @@ using Nanoseconds = std::chrono::nanoseconds;
void GLFWView::onWillStartRenderingFrame() {
MLN_TRACE_FUNC();

#if defined(MLN_RENDER_BACKEND_OPENGL) && !defined(MBGL_LAYER_LOCATION_INDICATOR_DISABLE_ALL)
#ifdef ENABLE_LOCATION_INDICATOR
puck = static_cast<mbgl::style::LocationIndicatorLayer *>(map->getStyle().getLayer("puck"));
if (puck) {
uint64_t ns = mbgl::Clock::now().time_since_epoch().count();
Expand Down
14 changes: 10 additions & 4 deletions platform/glfw/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
#include <mbgl/util/geometry.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
#if defined(MLN_RENDER_BACKEND_OPENGL) && !defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
#include <mbgl/style/layers/location_indicator_layer.hpp>
#endif

#include <utility>
#include <optional>

#if (defined(MLN_RENDER_BACKEND_OPENGL) || defined(MLN_RENDER_BACKEND_VULKAN)) && \
!defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
#define ENABLE_LOCATION_INDICATOR
#endif

#ifdef ENABLE_LOCATION_INDICATOR
#include <mbgl/style/layers/location_indicator_layer.hpp>
#endif

struct GLFWwindow;
class GLFWBackend;
class GLFWRendererFrontend;
Expand Down Expand Up @@ -163,7 +169,7 @@ class GLFWView : public mbgl::MapObserver {
mbgl::ResourceOptions mapResourceOptions;
mbgl::ClientOptions mapClientOptions;

#if defined(MLN_RENDER_BACKEND_OPENGL) && !defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
#ifdef ENABLE_LOCATION_INDICATOR
bool puckFollowsCameraCenter = false;
mbgl::style::LocationIndicatorLayer *puck = nullptr;
#endif
Expand Down
59 changes: 59 additions & 0 deletions src/mbgl/renderer/layers/location_indicator_layer_tweaker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <mbgl/renderer/layers/location_indicator_layer_tweaker.hpp>

#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/layers/render_location_indicator_layer.hpp>
#include <mbgl/style/layers/location_indicator_layer_properties.hpp>

namespace mbgl {

void LocationIndicatorLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& params) {
if (layerGroup.empty()) {
return;
}

const auto& props = static_cast<const style::LocationIndicatorLayerProperties&>(*evaluatedProperties);

const shaders::CommonUBO quadUBO = {/* .matrix */ util::cast<float>(projectionPuck),
/* .color */ Color::black()};

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
auto& drawableUniforms = drawable.mutableUniformBuffers();

if (!drawable.getEnabled()) {
return;
}

switch (static_cast<RenderLocationIndicatorLayer::LocationIndicatorComponentType>(drawable.getType())) {
case RenderLocationIndicatorLayer::LocationIndicatorComponentType::Circle: {
shaders::CommonUBO circleUBO = {/* .matrix */ util::cast<float>(projectionCircle),
/* .color */ props.evaluated.get<style::AccuracyRadiusColor>()};

drawableUniforms.createOrUpdate(shaders::idCommonUBO, &circleUBO, params.context);
break;
}

case RenderLocationIndicatorLayer::LocationIndicatorComponentType::CircleOutline: {
shaders::CommonUBO circleUBO = {/* .matrix */ util::cast<float>(projectionCircle),
/* .color */ props.evaluated.get<style::AccuracyRadiusBorderColor>()};

drawableUniforms.createOrUpdate(shaders::idCommonUBO, &circleUBO, params.context);
break;
}

case RenderLocationIndicatorLayer::LocationIndicatorComponentType::PuckShadow:
[[fallthrough]];
case RenderLocationIndicatorLayer::LocationIndicatorComponentType::Puck:
[[fallthrough]];
case RenderLocationIndicatorLayer::LocationIndicatorComponentType::PuckHat:
drawableUniforms.createOrUpdate(shaders::idCommonUBO, &quadUBO, params.context);
break;

default:
assert(false);
break;
}
});
}

} // namespace mbgl
33 changes: 33 additions & 0 deletions src/mbgl/renderer/layers/location_indicator_layer_tweaker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <mbgl/renderer/layer_tweaker.hpp>
#include <mbgl/gfx/uniform_buffer.hpp>

#include <string>

namespace mbgl {

/**
Location indicator layer specific tweaker
*/
class LocationIndicatorLayerTweaker : public LayerTweaker {
public:
LocationIndicatorLayerTweaker(std::string id_,
Immutable<style::LayerProperties> properties,
const mbgl::mat4& projectionCircle_,
const mbgl::mat4& projectionPuck_)
: LayerTweaker(std::move(id_), properties),
projectionCircle(projectionCircle_),
projectionPuck(projectionPuck_) {}

public:
~LocationIndicatorLayerTweaker() override = default;

void execute(LayerGroupBase&, const PaintParameters& params) override;

private:
const mbgl::mat4& projectionCircle;
const mbgl::mat4& projectionPuck;
};

} // namespace mbgl
Loading

0 comments on commit 156872f

Please sign in to comment.