Skip to content

Commit

Permalink
Drawable performance fixes (maplibre#1592)
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>
Co-authored-by: Tim Sylvester <[email protected]>
  • Loading branch information
3 people authored Sep 2, 2023
1 parent d0778d3 commit 11d7206
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 135 deletions.
9 changes: 9 additions & 0 deletions include/mbgl/gfx/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <mbgl/gfx/drawable_data.hpp>

#include <cstdint>
#include <cstddef>
#include <memory>
#include <optional>
#include <vector>
Expand Down Expand Up @@ -227,6 +228,12 @@ class Drawable {
/// Set drawable data
void setData(UniqueDrawableData&& value) { drawableData = std::move(value); }

/// Set drawable user-defined type
void setType(std::size_t type_) { type = type_; }

/// Get drawable user-defined type
size_t getType() const { return type; }

protected:
bool enabled = true;
bool enableColor = true;
Expand All @@ -250,6 +257,8 @@ class Drawable {

Textures textures;
std::vector<DrawableTweakerPtr> tweakers;

std::size_t type = 0;
};

using DrawablePtr = std::shared_ptr<Drawable>;
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/gl/uniform_buffer_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class UniformBufferGL final : public gfx::UniformBuffer {

protected:
BufferID id = 0;
uint32_t hash;
};

/// Stores a collection of uniform buffers by name
Expand All @@ -42,8 +43,7 @@ class UniformBufferArrayGL final : public gfx::UniformBufferArray {

private:
std::unique_ptr<gfx::UniformBuffer> copy(const gfx::UniformBuffer& uniformBuffers) override {
return std::unique_ptr<gfx::UniformBuffer>(
new UniformBufferGL(static_cast<const UniformBufferGL&>(uniformBuffers)));
return std::make_unique<UniformBufferGL>(static_cast<const UniformBufferGL&>(uniformBuffers));
}
};

Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/util/compression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ enum CompressionFormat {
std::string compress(const std::string& raw, int windowBits = CompressionFormat::ZLIB);
std::string decompress(const std::string& raw, int windowBits = CompressionFormat::DETECT);

std::uint32_t crc32(const void* raw, size_t size);

} // namespace util
} // namespace mbgl
15 changes: 15 additions & 0 deletions platform/default/src/mbgl/util/compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,20 @@ std::string decompress(const std::string &raw, int windowBits) {

return result;
}

std::uint32_t crc32(const void *raw, size_t size) {
auto hash = ::crc32(0L, Z_NULL, 0);
if (raw) {
const auto *p = static_cast<const Bytef *>(raw);
while (size > 0) {
const auto blockSize = static_cast<uInt>(size);
hash = ::crc32(hash, p, blockSize);
p += blockSize;
size -= blockSize;
}
}
return static_cast<std::uint32_t>(hash);
}

} // namespace util
} // namespace mbgl
7 changes: 6 additions & 1 deletion shaders/drawable.symbol_icon.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ layout (std140) uniform SymbolDrawableUBO {
highp float u_pitch;
bool u_rotate_symbol;
highp float u_aspect_ratio;
highp vec2 u_pad1;
};

layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_pad2;
highp float u_pad7;
highp vec2 u_pad8;
};

layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down
7 changes: 6 additions & 1 deletion shaders/drawable.symbol_sdf.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ layout (std140) uniform SymbolDrawableUBO {
highp float u_pitch;
bool u_rotate_symbol;
highp float u_aspect_ratio;
highp vec2 u_pad1;
};

layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_pad2;
highp float u_pad7;
highp vec2 u_pad8;
};

layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down
7 changes: 6 additions & 1 deletion shaders/drawable.symbol_sdf.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ layout (std140) uniform SymbolDrawableUBO {
highp float u_pitch;
bool u_rotate_symbol;
highp float u_aspect_ratio;
highp vec2 u_pad1;
};

layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_pad2;
highp float u_pad7;
highp vec2 u_pad8;
};

layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down
7 changes: 6 additions & 1 deletion shaders/drawable.symbol_text_and_icon.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ layout (std140) uniform SymbolDrawableUBO {
highp float u_pitch;
bool u_rotate_symbol;
highp float u_aspect_ratio;
highp vec2 u_pad1;
};

layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_pad2;
highp float u_pad7;
highp vec2 u_pad8;
};

layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down
7 changes: 6 additions & 1 deletion shaders/drawable.symbol_text_and_icon.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ layout (std140) uniform SymbolDrawableUBO {
highp float u_pitch;
bool u_rotate_symbol;
highp float u_aspect_ratio;
highp vec2 u_pad1;
};

layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_pad2;
highp float u_pad7;
highp vec2 u_pad8;
};

layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/gl/drawable_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ void DrawableGL::setIndexData(gfx::IndexVectorBasePtr indexes, std::vector<Uniqu
impl->segments = std::move(segments);
}

void DrawableGL::setVertices(std::vector<uint8_t>&& data, std::size_t count, gfx::AttributeDataType type) {
void DrawableGL::setVertices(std::vector<uint8_t>&& data, std::size_t count, gfx::AttributeDataType type_) {
impl->vertexData = std::move(data);
impl->vertexCount = count;
impl->vertexType = type;
impl->vertexType = type_;
}

const gfx::VertexAttributeArray& DrawableGL::getVertexAttributes() const {
Expand Down
22 changes: 14 additions & 8 deletions src/mbgl/gl/uniform_buffer_gl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <mbgl/gl/uniform_buffer_gl.hpp>
#include <mbgl/gl/defines.hpp>
#include <mbgl/platform/gl_functions.hpp>
#include <mbgl/util/compression.hpp>
#include <mbgl/util/logging.hpp>

#include <cassert>
Expand All @@ -10,11 +11,12 @@ namespace gl {

using namespace platform;

UniformBufferGL::UniformBufferGL(const void* data, std::size_t size_)
: UniformBuffer(size_) {
UniformBufferGL::UniformBufferGL(const void* data_, std::size_t size_)
: UniformBuffer(size_),
hash(util::crc32(data_, size_)) {
MBGL_CHECK_ERROR(glGenBuffers(1, &id));
MBGL_CHECK_ERROR(glBindBuffer(GL_UNIFORM_BUFFER, id));
MBGL_CHECK_ERROR(glBufferData(GL_UNIFORM_BUFFER, size, data, GL_DYNAMIC_DRAW));
MBGL_CHECK_ERROR(glBufferData(GL_UNIFORM_BUFFER, size, data_, GL_DYNAMIC_DRAW));
MBGL_CHECK_ERROR(glBindBuffer(GL_UNIFORM_BUFFER, 0));
}

Expand All @@ -25,18 +27,22 @@ UniformBufferGL::~UniformBufferGL() {
}
}

void UniformBufferGL::update(const void* data, std::size_t size_) {
assert(size == size_);
void UniformBufferGL::update(const void* data_, std::size_t size_) {
assert(size == size_); // && size == data.size());
if (size != size_) {
Log::Error(
Event::General,
"Mismatched size given to UBO update, expected " + std::to_string(size) + ", got " + std::to_string(size_));
return;
}

MBGL_CHECK_ERROR(glBindBuffer(GL_UNIFORM_BUFFER, id));
MBGL_CHECK_ERROR(glBufferSubData(GL_UNIFORM_BUFFER, 0, size_, data));
MBGL_CHECK_ERROR(glBindBuffer(GL_UNIFORM_BUFFER, 0));
const uint32_t newHash = util::crc32(data_, size_);
if (newHash != hash) {
hash = newHash;
MBGL_CHECK_ERROR(glBindBuffer(GL_UNIFORM_BUFFER, id));
MBGL_CHECK_ERROR(glBufferSubData(GL_UNIFORM_BUFFER, 0, size_, data_));
MBGL_CHECK_ERROR(glBindBuffer(GL_UNIFORM_BUFFER, 0));
}
}

} // namespace gl
Expand Down
Loading

0 comments on commit 11d7206

Please sign in to comment.