Skip to content

Commit

Permalink
Add ankerl::unordered_dense containers (maplibre#1881)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mwilsnd and pre-commit-ci[bot] authored Nov 27, 2023
1 parent 71d4cea commit 04d5695
Show file tree
Hide file tree
Showing 34 changed files with 119 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@
[submodule "vendor/boost"]
path = vendor/boost
url = https://github.com/maplibre/maplibre-gl-native-boost.git
[submodule "vendor/unordered_dense"]
path = vendor/unordered_dense
url = https://github.com/martinus/unordered_dense.git
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ cc_library(
"//vendor:polylabel",
"//vendor:protozero",
"//vendor:unique_resource",
"//vendor:unordered_dense",
"//vendor:vector-tile",
"//vendor:wagyu",
] + select({
Expand Down
29 changes: 19 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ option(MLN_WITH_OSMESA "Build with OSMesa (Software) renderer" OFF)
option(MLN_WITH_WERROR "Make all compilation warnings errors" ON)
option(MLN_LEGACY_RENDERER "Include the legacy rendering pathway" ON)
option(MLN_DRAWABLE_RENDERER "Include the drawable rendering pathway" OFF)
option(MLN_USE_UNORDERED_DENSE "Use ankerl dense containers for performance" ON)

if (MLN_WITH_CLANG_TIDY)
find_program(CLANG_TIDY_COMMAND NAMES clang-tidy)
Expand Down Expand Up @@ -387,6 +388,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/color.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/compression.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/constants.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/containers.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/convert.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/default_style.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/enum.hpp
Expand Down Expand Up @@ -990,6 +992,7 @@ if(MLN_WITH_OPENGL)
PUBLIC
"MLN_LEGACY_RENDERER=$<BOOL:${MLN_LEGACY_RENDERER}>"
"MLN_DRAWABLE_RENDERER=$<BOOL:${MLN_DRAWABLE_RENDERER}>"
"MLN_USE_UNORDERED_DENSE=$<BOOL:${MLN_USE_UNORDERED_DENSE}>"
)
list(APPEND
INCLUDE_FILES
Expand Down Expand Up @@ -1166,7 +1169,9 @@ if(MBGL_WITH_METAL)
message(STATUS "Configuring Metal renderer backend")
target_compile_definitions(
mbgl-core
PRIVATE MBGL_RENDER_BACKEND_METAL=1
PRIVATE
MBGL_RENDER_BACKEND_METAL=1
"MLN_USE_UNORDERED_DENSE=$<BOOL:${MLN_USE_UNORDERED_DENSE}>"
)
list(APPEND
INCLUDE_FILES
Expand Down Expand Up @@ -1259,14 +1264,6 @@ if(MBGL_WITH_METAL)
)
endif()

if(MBGL_WITH_METAL)
message(STATUS "Configuring Metal renderer backend")
target_compile_definitions(
mbgl-core
PRIVATE MBGL_RENDER_BACKEND_METAL=1
)
endif()

target_sources(
mbgl-core PRIVATE
${INCLUDE_FILES}
Expand Down Expand Up @@ -1324,6 +1321,16 @@ include(${PROJECT_SOURCE_DIR}/vendor/unique_resource.cmake)
include(${PROJECT_SOURCE_DIR}/vendor/vector-tile.cmake)
include(${PROJECT_SOURCE_DIR}/vendor/wagyu.cmake)

add_subdirectory(${PROJECT_SOURCE_DIR}/vendor/unordered_dense)
set_target_properties(
unordered_dense
PROPERTIES
INTERFACE_MAPBOX_NAME "unordered_dense"
INTERFACE_MAPBOX_URL "https://github.com/martinus/unordered_dense"
INTERFACE_MAPBOX_AUTHOR "Martin Leitner-Ankerl"
INTERFACE_MAPBOX_LICENSE ${PROJECT_SOURCE_DIR}/vendor/unordered_dense/LICENSE
)

target_link_libraries(
mbgl-core
PRIVATE
Expand All @@ -1350,6 +1357,7 @@ target_link_libraries(
Mapbox::Base::geojson.hpp
Mapbox::Base::geometry.hpp
Mapbox::Base::variant
unordered_dense
)

export(TARGETS
Expand Down Expand Up @@ -1380,6 +1388,7 @@ export(TARGETS
mbgl-vendor-unique_resource
mbgl-vendor-vector-tile
mbgl-vendor-wagyu
unordered_dense

FILE MapboxCoreTargets.cmake
)
Expand Down Expand Up @@ -1419,4 +1428,4 @@ endif()

add_subdirectory(${PROJECT_SOURCE_DIR}/test)
add_subdirectory(${PROJECT_SOURCE_DIR}/benchmark)
add_subdirectory(${PROJECT_SOURCE_DIR}/render-test)
add_subdirectory(${PROJECT_SOURCE_DIR}/render-test)
1 change: 1 addition & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/color.hpp",
"include/mbgl/util/compression.hpp",
"include/mbgl/util/constants.hpp",
"include/mbgl/util/containers.hpp",
"include/mbgl/util/convert.hpp",
"include/mbgl/util/default_style.hpp",
"include/mbgl/util/enum.hpp",
Expand Down
2 changes: 2 additions & 0 deletions bazel/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ MAPLIBRE_FLAGS = select({
"/DMBGL_USE_GLES2=1",
"/DMBGL_RENDER_BACKEND_OPENGL=1",
"/D_USE_MATH_DEFINES",
"/DMLN_USE_UNORDERED_DENSE",
],
"//conditions:default": [
"-DMBGL_USE_GLES2=1",
"-DMBGL_RENDER_BACKEND_OPENGL=1",
"-DGLES_SILENCE_DEPRECATION",
"-DMLN_USE_UNORDERED_DENSE",
]
})
3 changes: 2 additions & 1 deletion include/mbgl/gfx/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mbgl/util/color.hpp>
#include <mbgl/util/identity.hpp>
#include <mbgl/util/traits.hpp>
#include <mbgl/util/containers.hpp>

#include <cstdint>
#include <cstddef>
Expand Down Expand Up @@ -43,7 +44,7 @@ using Texture2DPtr = std::shared_ptr<Texture2D>;
class Drawable {
public:
/// @brief Map from sampler location to texture info
using Textures = std::unordered_map<int32_t, gfx::Texture2DPtr>;
using Textures = mbgl::unordered_map<int32_t, gfx::Texture2DPtr>;

protected:
Drawable(std::string name);
Expand Down
7 changes: 3 additions & 4 deletions include/mbgl/gfx/shader_group.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#pragma once

#include <mbgl/gfx/shader.hpp>
#include <mbgl/util/containers.hpp>

#include <memory>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>

namespace mbgl {

Expand Down Expand Up @@ -147,13 +146,13 @@ class ShaderGroup {
/// @return A `gfx::ShaderPtr`
virtual gfx::ShaderPtr getOrCreateShader(
gfx::Context&,
[[maybe_unused]] const std::unordered_set<StringIdentity>& propertiesAsUniforms,
[[maybe_unused]] const mbgl::unordered_set<StringIdentity>& propertiesAsUniforms,
[[maybe_unused]] std::string_view firstAttribName = "a_pos") {
return {};
}

private:
std::unordered_map<std::string, std::shared_ptr<gfx::Shader>> programs;
mbgl::unordered_map<std::string, std::shared_ptr<gfx::Shader>> programs;
mutable std::shared_mutex programLock;
};

Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/gfx/uniform_block.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <mbgl/util/string_indexer.hpp>
#include <mbgl/util/containers.hpp>

#include <algorithm>
#include <functional>
Expand Down Expand Up @@ -62,7 +63,7 @@ class UniformBlock {
/// Stores a collection of uniform blocks by name
class UniformBlockArray {
public:
using UniformBlockMap = std::unordered_map<StringIdentity, std::unique_ptr<UniformBlock>>;
using UniformBlockMap = mbgl::unordered_map<StringIdentity, std::unique_ptr<UniformBlock>>;

/// @brief Constructor
UniformBlockArray() = default;
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/gfx/uniform_buffer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <mbgl/util/string_indexer.hpp>
#include <mbgl/util/containers.hpp>

#include <memory>
#include <string>
Expand Down Expand Up @@ -48,7 +49,7 @@ class UniformBuffer {
/// Stores a collection of uniform buffers by name
class UniformBufferArray {
public:
using UniformBufferMap = std::unordered_map<StringIdentity, std::shared_ptr<UniformBuffer>>;
using UniformBufferMap = mbgl::unordered_map<StringIdentity, std::shared_ptr<UniformBuffer>>;

UniformBufferArray() = default;
UniformBufferArray(UniformBufferArray&&);
Expand Down
9 changes: 4 additions & 5 deletions include/mbgl/gfx/vertex_attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#include <mbgl/gfx/gfx_types.hpp>
#include <mbgl/renderer/paint_property_binder.hpp>
#include <mbgl/util/string_indexer.hpp>
#include <mbgl/util/containers.hpp>

#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -281,7 +280,7 @@ class VertexAttribute {
/// Stores a collection of vertex attributes by name
class VertexAttributeArray {
public:
using AttributeMap = std::unordered_map<StringIdentity, std::unique_ptr<VertexAttribute>>;
using AttributeMap = mbgl::unordered_map<StringIdentity, std::unique_ptr<VertexAttribute>>;

VertexAttributeArray() = default;
VertexAttributeArray(VertexAttributeArray&&);
Expand Down Expand Up @@ -394,7 +393,7 @@ class VertexAttributeArray {
template <typename... DataDrivenPaintProperty, typename Binders, typename Evaluated>
void readDataDrivenPaintProperties(const Binders& binders,
const Evaluated& evaluated,
std::unordered_set<StringIdentity>& propertiesAsUniforms) {
mbgl::unordered_set<StringIdentity>& propertiesAsUniforms) {
// Read each property in the type pack
propertiesAsUniforms.reserve(sizeof...(DataDrivenPaintProperty));
(readDataDrivenPaintProperty<DataDrivenPaintProperty>(binders.template get<DataDrivenPaintProperty>(),
Expand All @@ -413,7 +412,7 @@ class VertexAttributeArray {
template <typename DataDrivenPaintProperty, typename Binder>
void readDataDrivenPaintProperty(const Binder& binder,
const bool isConstant,
std::unordered_set<StringIdentity>& propertiesAsUniforms) {
mbgl::unordered_set<StringIdentity>& propertiesAsUniforms) {
if (!binder) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/mtl/command_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/gfx/command_encoder.hpp>
#include <mbgl/mtl/mtl_fwd.hpp>
#include <mbgl/util/containers.hpp>

#include <functional>
#include <memory>
Expand Down Expand Up @@ -65,8 +66,8 @@ class CommandEncoder final : public gfx::CommandEncoder {
std::vector<GroupEntry> debugGroupNames;
std::vector<gfx::DebugGroup<gfx::RenderPass>> renderDebugGroups;
std::vector<gfx::DebugGroup<gfx::UploadPass>> uploadDebugGroups;
std::unordered_set<RenderPass*> renderPasses;
std::unordered_set<UploadPass*> uploadPasses;
mbgl::unordered_set<RenderPass*> renderPasses;
mbgl::unordered_set<UploadPass*> uploadPasses;
};

} // namespace mtl
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/mtl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mbgl/mtl/buffer_resource.hpp>
#include <mbgl/mtl/mtl_fwd.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/containers.hpp>

#include <memory>
#include <optional>
Expand Down Expand Up @@ -61,7 +62,7 @@ class Context final : public gfx::Context {
std::string_view vertexName,
std::string_view fragmentName,
const ProgramParameters& programParameters,
const std::unordered_map<std::string, std::string>& additionalDefines);
const mbgl::unordered_map<std::string, std::string>& additionalDefines);

MTLTexturePtr createMetalTexture(MTLTextureDescriptorPtr textureDescriptor) const;
MTLSamplerStatePtr createMetalSamplerState(MTLSamplerDescriptorPtr samplerDescriptor) const;
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/renderer/layer_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <mbgl/shaders/layer_ubo.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/util/immutable.hpp>
#include <mbgl/util/containers.hpp>

#include <array>
#include <memory>
#include <string>
#include <unordered_set>

namespace mbgl {

Expand Down Expand Up @@ -69,8 +69,8 @@ class LayerTweaker {
/// @brief Set the collection of attribute names which will be provided at uniform values rather than per-vertex
/// attributes.
/// @details These values should not have "a_" prefixes, as produced by `readDataDrivenPaintProperties`.
void setPropertiesAsUniforms(const std::unordered_set<StringIdentity>&);
const std::unordered_set<StringIdentity>& getPropertiesAsUniforms() const;
void setPropertiesAsUniforms(const mbgl::unordered_set<StringIdentity>&);
const mbgl::unordered_set<StringIdentity>& getPropertiesAsUniforms() const;

void enableOverdrawInspector(bool);

Expand Down Expand Up @@ -99,7 +99,7 @@ class LayerTweaker {
#if MLN_RENDER_BACKEND_METAL
// For Metal, whether a property is provided through attribtues or uniforms is specified in
// a uniform buffer rather than by a shader compiled with different preprocessor definitions.
std::unordered_set<StringIdentity> propertiesAsUniforms;
mbgl::unordered_set<StringIdentity> propertiesAsUniforms;
#endif // MLN_RENDER_BACKEND_METAL

// Indicates that the evaluated properties have changed
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/shaders/gl/shader_group_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/programs/program_parameters.hpp>
#include <mbgl/util/hash.hpp>
#include <mbgl/util/containers.hpp>

#include <sstream>
#include <unordered_set>

namespace mbgl {
namespace gl {
Expand All @@ -21,7 +21,7 @@ class ShaderGroupGL final : public gfx::ShaderGroup {
~ShaderGroupGL() noexcept override = default;

gfx::ShaderPtr getOrCreateShader(gfx::Context& context,
const std::unordered_set<StringIdentity>& propertiesAsUniforms,
const mbgl::unordered_set<StringIdentity>& propertiesAsUniforms,
std::string_view firstAttribName) override {
constexpr auto& vert = shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::vertex;
constexpr auto& frag = shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::fragment;
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/shaders/mtl/shader_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mbgl/shaders/mtl/background.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/util/containers.hpp>

#include <numeric>
#include <string>
Expand All @@ -22,7 +23,7 @@ class ShaderGroup final : public gfx::ShaderGroup {
~ShaderGroup() noexcept override = default;

gfx::ShaderPtr getOrCreateShader(gfx::Context& gfxContext,
const std::unordered_set<StringIdentity>& propertiesAsUniforms,
const mbgl::unordered_set<StringIdentity>& propertiesAsUniforms,
std::string_view /*firstAttribName*/) override {
constexpr auto& name = shaders::ShaderSource<ShaderID, gfx::Backend::Type::Metal>::name;
constexpr auto& source = shaders::ShaderSource<ShaderID, gfx::Backend::Type::Metal>::source;
Expand Down
23 changes: 23 additions & 0 deletions include/mbgl/util/containers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#if MLN_USE_UNORDERED_DENSE
#include <ankerl/unordered_dense.h>
#else
#include <unordered_map>
#include <unordered_set>
#endif

namespace mbgl {
#if MLN_USE_UNORDERED_DENSE
template <typename Key, typename Value>
using unordered_map = ankerl::unordered_dense::map<Key, Value>;

template <typename Value>
using unordered_set = ankerl::unordered_dense::set<Value>;
#else
template <typename Key, typename Value>
using unordered_map = std::unordered_map<Key, Value>;

template <typename Value>
using unordered_set = std::unordered_set<Value>;
#endif
} // namespace mbgl
3 changes: 2 additions & 1 deletion src/mbgl/layout/circle_layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mbgl/renderer/buckets/circle_bucket.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/style/layers/circle_layer_impl.hpp>
#include <mbgl/util/containers.hpp>

namespace mbgl {

Expand Down Expand Up @@ -54,7 +55,7 @@ class CircleLayout final : public Layout {

void createBucket(const ImagePositions&,
std::unique_ptr<FeatureIndex>& featureIndex,
std::unordered_map<std::string, LayerRenderData>& renderData,
mbgl::unordered_map<std::string, LayerRenderData>& renderData,
const bool,
const bool,
const CanonicalTileID& canonical) override {
Expand Down
Loading

0 comments on commit 04d5695

Please sign in to comment.