Skip to content

Commit

Permalink
UBO by index instead of map (maplibre#1980)
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
alexcristici and pre-commit-ci[bot] authored Jan 31, 2024
1 parent ea15e7b commit 61a6b66
Show file tree
Hide file tree
Showing 74 changed files with 986 additions and 598 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/shader_program_base.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/identity.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/suppress_copies.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/shader_info.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/shader_program_gl.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/buffer_allocator.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl.hpp
Expand Down Expand Up @@ -1139,6 +1140,8 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/line_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/raster_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/symbol_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/ubo_max_count.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/shader_program_gl.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl_builder.hpp
Expand All @@ -1149,6 +1152,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/gl/texture2d.hpp
)
list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/gl/shader_info.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/gl/shader_program_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/drawable_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/drawable_gl_builder.cpp
Expand Down
3 changes: 3 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ MLN_DRAWABLES_HEADERS = [
"include/mbgl/shaders/raster_layer_ubo.hpp",
"include/mbgl/shaders/shader_program_base.hpp",
"include/mbgl/shaders/symbol_layer_ubo.hpp",
"include/mbgl/shaders/ubo_max_count.hpp",
"include/mbgl/util/identity.hpp",
"include/mbgl/util/suppress_copies.hpp",
"include/mbgl/style/layers/custom_drawable_layer.hpp",
Expand All @@ -1006,6 +1007,7 @@ MLN_DRAWABLES_GL_SOURCE = [
"src/mbgl/gl/uniform_block_gl.cpp",
"src/mbgl/gl/uniform_buffer_gl.cpp",
"src/mbgl/gl/vertex_attribute_gl.cpp",
"src/mbgl/shaders/gl/shader_info.cpp",
"src/mbgl/shaders/gl/shader_program_gl.cpp",
]

Expand All @@ -1018,6 +1020,7 @@ MLN_DRAWABLES_GL_HEADERS = [
"include/mbgl/gl/uniform_buffer_gl.hpp",
"include/mbgl/gl/vertex_attribute_gl.hpp",
"include/mbgl/gl/texture2d.hpp",
"include/mbgl/shaders/gl/shader_info.hpp",
"include/mbgl/shaders/gl/shader_program_gl.hpp",
"include/mbgl/shaders/gl/shader_group_gl.hpp",
]
Expand Down
51 changes: 16 additions & 35 deletions include/mbgl/gfx/uniform_block.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#pragma once

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

#include <algorithm>
#include <functional>
#include <string>
#include <memory>
#include <unordered_map>
#include <mbgl/shaders/ubo_max_count.hpp>

namespace mbgl {
namespace gfx {

class UniformBuffer;
class UniformBlock;

using UniqueUniformBlock = std::unique_ptr<UniformBlock>;

/// @brief This class represents an uniform block
class UniformBlock {
Expand Down Expand Up @@ -60,11 +56,9 @@ class UniformBlock {
std::size_t size;
};

/// Stores a collection of uniform blocks by name
/// Stores a collection of uniform blocks by id
class UniformBlockArray {
public:
using UniformBlockMap = mbgl::unordered_map<StringIdentity, std::unique_ptr<UniformBlock>>;

/// @brief Constructor
UniformBlockArray() = default;

Expand All @@ -77,22 +71,19 @@ class UniformBlockArray {
/// @brief Destructor
virtual ~UniformBlockArray() = default;

/// @brief Get map of elements.
const UniformBlockMap& getMap() const { return uniformBlockMap; }

/// @brief Number of elements
std::size_t size() const { return uniformBlockMap.size(); }
/// @brief Number of maximum allocated elements
std::size_t allocatedSize() const { return uniformBlockVector.size(); }

/// @brief Get an uniform block element.
/// @return Pointer to the element on success, or null if the uniform block doesn't exists.
const std::unique_ptr<UniformBlock>& get(const StringIdentity id) const;
const std::unique_ptr<UniformBlock>& get(const size_t id) const;

/// @brief Add a new uniform block element.
/// @param name
/// @brief Set a new uniform block element.
/// @param id
/// @param index
/// @param size
/// @return Pointer to the new element on success, or null if the uniform block already exists.
const std::unique_ptr<UniformBlock>& add(const StringIdentity id, int index, std::size_t size);
const std::unique_ptr<UniformBlock>& set(const size_t id, const size_t index, std::size_t size);

/// @brief Move assignment operator
UniformBlockArray& operator=(UniformBlockArray&&);
Expand All @@ -101,31 +92,21 @@ class UniformBlockArray {
UniformBlockArray& operator=(const UniformBlockArray&);

/// Do something with each block
template <typename Func /* void(const StringIdentity, const UniformBlock&) */>
template <typename Func /* void(const UniformBlock&) */>
void visit(Func f) {
std::for_each(uniformBlockMap.begin(), uniformBlockMap.end(), [&](const auto& kv) {
if (kv.second) {
f(kv.first, *kv.second);
std::for_each(uniformBlockVector.begin(), uniformBlockVector.end(), [&](const auto& block) {
if (block) {
f(*block);
}
});
}

protected:
const std::unique_ptr<UniformBlock>& add(const StringIdentity id, std::unique_ptr<UniformBlock>&& uniformBlock) {
const auto result = uniformBlockMap.insert(std::make_pair(id, std::unique_ptr<UniformBlock>()));
if (result.second) {
result.first->second = std::move(uniformBlock);
return result.first->second;
} else {
return nullref;
}
}

virtual std::unique_ptr<UniformBlock> create(int index, std::size_t size) = 0;
virtual std::unique_ptr<UniformBlock> copy(const UniformBlock& uniformBlock) = 0;

protected:
UniformBlockMap uniformBlockMap;
std::array<UniqueUniformBlock, shaders::maxUBOCountPerShader> uniformBlockVector;
static std::unique_ptr<UniformBlock> nullref;
};

Expand Down
37 changes: 12 additions & 25 deletions include/mbgl/gfx/uniform_buffer.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#pragma once

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

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

namespace mbgl {
namespace gfx {
Expand All @@ -15,6 +10,7 @@ class Context;
class UniformBuffer;
class UniformBufferArray;

using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
using UniqueUniformBuffer = std::unique_ptr<UniformBuffer>;
using UniqueUniformBufferArray = std::unique_ptr<UniformBufferArray>;

Expand Down Expand Up @@ -46,37 +42,30 @@ class UniformBuffer {
std::size_t size;
};

/// Stores a collection of uniform buffers by name
/// Stores a collection of uniform buffers by id
class UniformBufferArray {
public:
using UniformBufferMap = mbgl::unordered_map<StringIdentity, std::shared_ptr<UniformBuffer>>;

UniformBufferArray() = default;
UniformBufferArray(UniformBufferArray&&);
// Would need to use the virtual assignment operator
UniformBufferArray(const UniformBufferArray&) = delete;
virtual ~UniformBufferArray() = default;

/// Number of elements
std::size_t size() const { return uniformBufferMap.size(); }
/// Number of maximum allocated elements
std::size_t allocatedSize() const { return uniformBufferVector.size(); }

/// Get an uniform buffer element.
/// Returns a pointer to the element on success, or null if the uniform buffer doesn't exists.
const std::shared_ptr<UniformBuffer>& get(const StringIdentity id) const;
const std::shared_ptr<UniformBuffer>& get(const size_t id) const;

/// Add a new uniform buffer element or replace the existing one.
const std::shared_ptr<UniformBuffer>& addOrReplace(const StringIdentity id,
std::shared_ptr<UniformBuffer> uniformBuffer);
/// Set a new uniform buffer element or replace the existing one.
const std::shared_ptr<UniformBuffer>& set(const size_t id, std::shared_ptr<UniformBuffer> uniformBuffer);

/// Create and add a new buffer or update an existing one
void createOrUpdate(const StringIdentity id,
const std::vector<uint8_t>& data,
gfx::Context&,
bool persistent = false);
void createOrUpdate(
const StringIdentity id, const void* data, std::size_t size, gfx::Context&, bool persistent = false);
void createOrUpdate(const size_t id, const std::vector<uint8_t>& data, gfx::Context&, bool persistent = false);
void createOrUpdate(const size_t id, const void* data, std::size_t size, gfx::Context&, bool persistent = false);
template <typename T>
std::enable_if_t<!std::is_pointer_v<T>> createOrUpdate(const StringIdentity id,
std::enable_if_t<!std::is_pointer_v<T>> createOrUpdate(const size_t id,
const T* data,
gfx::Context& context,
bool persistent = false) {
Expand All @@ -87,12 +76,10 @@ class UniformBufferArray {
UniformBufferArray& operator=(const UniformBufferArray&);

protected:
const std::shared_ptr<UniformBuffer>& add(const StringIdentity id, std::shared_ptr<UniformBuffer>&&);

virtual std::unique_ptr<UniformBuffer> copy(const UniformBuffer&) = 0;

protected:
UniformBufferMap uniformBufferMap;
std::array<UniformBufferPtr, shaders::maxUBOCountPerShader> uniformBufferVector;
static std::shared_ptr<UniformBuffer> nullref;
};

Expand Down
6 changes: 6 additions & 0 deletions include/mbgl/shaders/background_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ struct alignas(16) BackgroundPatternLayerUBO {
};
static_assert(sizeof(BackgroundPatternLayerUBO) == 96);

enum {
idBackgroundDrawableUBO,
idBackgroundLayerUBO,
backgroundUBOCount
};

} // namespace shaders
} // namespace mbgl
8 changes: 8 additions & 0 deletions include/mbgl/shaders/circle_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ struct alignas(16) CircleInterpolateUBO {
};
static_assert(sizeof(CircleInterpolateUBO) % 16 == 0);

enum {
idCircleDrawableUBO,
idCirclePaintParamsUBO,
idCircleEvaluatedPropsUBO,
idCircleInterpolateUBO,
circleUBOCount
};

} // namespace shaders
} // namespace mbgl
5 changes: 5 additions & 0 deletions include/mbgl/shaders/collision_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ struct alignas(16) CollisionUBO {
static_assert(sizeof(CollisionUBO) % 16 == 0);
static_assert(sizeof(CollisionUBO) == 80);

enum {
idCollisionUBO,
collisionUBOCount
};

} // namespace shaders
} // namespace mbgl
5 changes: 5 additions & 0 deletions include/mbgl/shaders/debug_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ struct alignas(16) DebugUBO {
};
static_assert(sizeof(DebugUBO) % 16 == 0);

enum {
idDebugUBO,
debugUBOCount
};

} // namespace shaders
} // namespace mbgl
48 changes: 28 additions & 20 deletions include/mbgl/shaders/fill_extrusion_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@
namespace mbgl {
namespace shaders {

/// Evaluated properties that depend on the tile
struct alignas(16) FillExtrusionDrawableTilePropsUBO {
/* 0 */ std::array<float, 4> pattern_from;
/* 16 */ std::array<float, 4> pattern_to;
/* 32 */
};
static_assert(sizeof(FillExtrusionDrawableTilePropsUBO) == 2 * 16);

/// Attribute interpolations
struct alignas(16) FillExtrusionInterpolateUBO {
/* 0 */ float base_t;
/* 4 */ float height_t;
/* 8 */ float color_t;
/* 12 */ float pattern_from_t;
/* 16 */ float pattern_to_t;
/* 20 */ float pad1, pad2, pad3;
/* 32 */
};
static_assert(sizeof(FillExtrusionInterpolateUBO) == 2 * 16);

struct alignas(16) FillExtrusionDrawableUBO {
/* 0 */ std::array<float, 4 * 4> matrix;
/* 64 */ std::array<float, 4> scale;
Expand Down Expand Up @@ -56,5 +36,33 @@ struct alignas(16) FillExtrusionDrawablePropsUBO {
};
static_assert(sizeof(FillExtrusionDrawablePropsUBO) == 5 * 16);

/// Evaluated properties that depend on the tile
struct alignas(16) FillExtrusionDrawableTilePropsUBO {
/* 0 */ std::array<float, 4> pattern_from;
/* 16 */ std::array<float, 4> pattern_to;
/* 32 */
};
static_assert(sizeof(FillExtrusionDrawableTilePropsUBO) == 2 * 16);

/// Attribute interpolations
struct alignas(16) FillExtrusionInterpolateUBO {
/* 0 */ float base_t;
/* 4 */ float height_t;
/* 8 */ float color_t;
/* 12 */ float pattern_from_t;
/* 16 */ float pattern_to_t;
/* 20 */ float pad1, pad2, pad3;
/* 32 */
};
static_assert(sizeof(FillExtrusionInterpolateUBO) == 2 * 16);

enum {
idFillExtrusionDrawableUBO,
idFillExtrusionDrawablePropsUBO,
idFillExtrusionDrawableTilePropsUBO,
idFillExtrusionInterpolateUBO,
fillExtrusionUBOCount
};

} // namespace shaders
} // namespace mbgl
30 changes: 30 additions & 0 deletions include/mbgl/shaders/fill_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ struct alignas(16) FillInterpolateUBO {
};
static_assert(sizeof(FillInterpolateUBO) % 16 == 0);

enum {
idFillDrawableUBO,
idFillEvaluatedPropsUBO,
idFillInterpolateUBO,
fillUBOCount
};

//
// Fill outline

Expand All @@ -52,6 +59,13 @@ struct alignas(16) FillOutlineInterpolateUBO {
};
static_assert(sizeof(FillOutlineInterpolateUBO) == 1 * 16);

enum {
idFillOutlineDrawableUBO,
idFillOutlineEvaluatedPropsUBO,
idFillOutlineInterpolateUBO,
fillOutlineUBOCount
};

//
// Fill Pattern

Expand Down Expand Up @@ -87,6 +101,14 @@ struct alignas(16) FillPatternInterpolateUBO {
};
static_assert(sizeof(FillPatternInterpolateUBO) == 1 * 16);

enum {
idFillPatternDrawableUBO,
idFillPatternTilePropsUBO,
idFillPatternEvaluatedPropsUBO,
idFillPatternInterpolateUBO,
fillPatternUBOCount
};

//
// Fill pattern outline

Expand Down Expand Up @@ -121,5 +143,13 @@ struct alignas(16) FillOutlinePatternInterpolateUBO {
};
static_assert(sizeof(FillOutlinePatternInterpolateUBO) == 1 * 16);

enum {
idFillOutlinePatternDrawableUBO,
idFillOutlinePatternTilePropsUBO,
idFillOutlinePatternEvaluatedPropsUBO,
idFillOutlinePatternInterpolateUBO,
fillOutlinePatternUBOCount
};

} // namespace shaders
} // namespace mbgl
Loading

0 comments on commit 61a6b66

Please sign in to comment.