Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metal stats #1781

Merged
merged 12 commits into from
Oct 20, 2023
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ if(MBGL_WITH_METAL)
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/drawable.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/drawable_impl.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/drawable_builder.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/index_buffer_resource.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/layer_group.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/mtl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/offscreen_texture.cpp
Expand All @@ -1227,6 +1228,7 @@ if(MBGL_WITH_METAL)
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/uniform_buffer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/upload_pass.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/vertex_attribute.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/mtl/vertex_buffer_resource.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/mtl/shader_program.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/background.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/background_pattern.cpp
Expand Down
6 changes: 4 additions & 2 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ MLN_OPENGL_HEADERS = [
"include/mbgl/gl/renderable_resource.hpp",
"include/mbgl/gl/renderer_backend.hpp",
"include/mbgl/layermanager/location_indicator_layer_factory.hpp",
"include/mbgl/platform/gl_functions.hpp"
"include/mbgl/platform/gl_functions.hpp",
]

MLN_DRAWABLES_SOURCE = [
Expand Down Expand Up @@ -1009,7 +1009,7 @@ MLN_DRAWABLES_GL_HEADERS = [
"include/mbgl/gl/vertex_attribute_gl.hpp",
"include/mbgl/gl/texture2d.hpp",
"include/mbgl/shaders/gl/shader_program_gl.hpp",
"include/mbgl/shaders/gl/shader_group_gl.hpp"
"include/mbgl/shaders/gl/shader_group_gl.hpp",
]

MLN_DRAWABLES_MTL_SOURCE = [
Expand All @@ -1019,6 +1019,7 @@ MLN_DRAWABLES_MTL_SOURCE = [
"src/mbgl/mtl/drawable.cpp",
"src/mbgl/mtl/drawable_impl.hpp",
"src/mbgl/mtl/drawable_builder.cpp",
"src/mbgl/mtl/index_buffer_resource.cpp",
"src/mbgl/mtl/layer_group.cpp",
"src/mbgl/mtl/mtl.cpp",
"src/mbgl/mtl/offscreen_texture.cpp",
Expand All @@ -1030,6 +1031,7 @@ MLN_DRAWABLES_MTL_SOURCE = [
"src/mbgl/mtl/uniform_buffer.cpp",
"src/mbgl/mtl/upload_pass.cpp",
"src/mbgl/mtl/vertex_attribute.cpp",
"src/mbgl/mtl/vertex_buffer_resource.cpp",
"src/mbgl/shaders/mtl/shader_program.cpp",
"src/mbgl/shaders/mtl/background.cpp",
"src/mbgl/shaders/mtl/background_pattern.cpp",
Expand Down
59 changes: 34 additions & 25 deletions include/mbgl/gfx/rendering_stats.hpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
#pragma once

#include <cstddef>
#include <string>

namespace mbgl {
namespace gfx {

struct RenderingStats {
RenderingStats() = default;
bool isZero() const;

int numDrawCalls;
int numActiveTextures;
int numCreatedTextures;
int numBuffers;
int numFrameBuffers;
int numFrames = 0;
int numDrawCalls = 0;
int totalDrawCalls = 0;

int memTextures;
int memIndexBuffers;
int memVertexBuffers;
int numCreatedTextures = 0;
int numActiveTextures = 0;
int numTextureBindings = 0;
int numTextureUpdates = 0;
std::size_t textureUpdateBytes = 0;

int stencilClears = 0;
int stencilUpdates = 0;
int numBuffers = 0;
int numFrameBuffers = 0;

RenderingStats& operator+=(const RenderingStats& right);
};
int numIndexBuffers = 0;
std::size_t indexUpdateBytes = 0;

inline RenderingStats& RenderingStats::operator+=(const RenderingStats& r) {
numDrawCalls += r.numDrawCalls;
numActiveTextures += r.numActiveTextures;
numCreatedTextures += r.numCreatedTextures;
numBuffers += r.numBuffers;
numFrameBuffers += r.numFrameBuffers;
int numVertexBuffers = 0;
std::size_t vertexUpdateBytes = 0;

memTextures += r.memTextures;
memIndexBuffers += r.memIndexBuffers;
memVertexBuffers += r.memVertexBuffers;
int numUniformBuffers = 0;
int numUniformUpdates = 0;
std::size_t uniformUpdateBytes = 0;

stencilClears += r.stencilClears;
stencilUpdates += r.stencilUpdates;
int memTextures = 0;
int memBuffers = 0;
int memIndexBuffers = 0;
int memVertexBuffers = 0;
int memUniformBuffers = 0;

return *this;
}
int stencilClears = 0;
int stencilUpdates = 0;

RenderingStats& operator+=(const RenderingStats&);

#if !defined(NDEBUG)
std::string toString(std::string_view separator) const;
#endif
};

} // namespace gfx
} // namespace mbgl
6 changes: 4 additions & 2 deletions include/mbgl/gfx/uniform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class UniformBuffer {
protected:
UniformBuffer(std::size_t size_)
: size(size_) {}
UniformBuffer(const UniformBuffer&) = default;
UniformBuffer(const UniformBuffer& other)
: size(other.size) {}
UniformBuffer(UniformBuffer&& other)
: size(other.size) {}

Expand All @@ -32,8 +33,9 @@ class UniformBuffer {

std::size_t getSize() const { return size; }

UniformBuffer& operator=(const UniformBuffer&) = delete;

protected:
UniformBuffer& operator=(const UniformBuffer&) = default;
UniformBuffer& operator=(UniformBuffer&& other) {
size = other.size;
return *this;
Expand Down
8 changes: 5 additions & 3 deletions include/mbgl/gl/uniform_buffer_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ namespace mbgl {
namespace gl {

class UniformBufferGL final : public gfx::UniformBuffer {
UniformBufferGL(const UniformBufferGL&);

public:
UniformBufferGL(const void* data, std::size_t size_);
UniformBufferGL(const UniformBufferGL& other)
: UniformBuffer(other) {}
UniformBufferGL(UniformBufferGL&& other)
: UniformBuffer(std::move(other)) {}
~UniformBufferGL() override;

BufferID getID() const { return id; }
void update(const void* data, std::size_t size_) override;

UniformBufferGL clone() const { return {*this}; }

protected:
BufferID id = 0;
uint32_t hash;
Expand All @@ -42,7 +44,7 @@ class UniformBufferArrayGL final : public gfx::UniformBufferArray {

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

Expand Down
16 changes: 12 additions & 4 deletions include/mbgl/mtl/buffer_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,40 @@
namespace mbgl {
namespace mtl {

class Context;

class BufferResource {
public:
BufferResource() = default;
BufferResource() = delete;
/** @brief Create a new Metal buffer
@param device The Metal device on which to create the buffer.
@param raw Data to use for the contents of the new buffer. May be null.
@param size The minimum size of the new buffer. Must be non-zero.
@param usage A `MTL::ResourceOptions` value. Currently, only `ResourceStorageModeShared` is supported.
*/
BufferResource(MTLDevicePtr device, const void* raw, std::size_t size, MTL::ResourceOptions usage);
BufferResource(const BufferResource&);
BufferResource(Context& context_, const void* raw, std::size_t size, MTL::ResourceOptions usage);
BufferResource(BufferResource&&);
virtual ~BufferResource();

BufferResource& operator=(BufferResource&&);

BufferResource clone() const;

void update(const void* data, std::size_t size, std::size_t offset);

std::size_t getSizeInBytes() const { return buffer ? buffer->length() : 0; }
void* contents() const { return buffer ? buffer->contents() : nullptr; }

Context& getContext() const { return context; }
const MTLBufferPtr& getMetalBuffer() const { return buffer; }

operator bool() const { return buffer.operator bool(); }
bool operator!() const { return !buffer.operator bool(); }

protected:
MTLDevicePtr device;
Context& context;
MTLBufferPtr buffer;
NS::UInteger size;
NS::UInteger usage;
};

Expand Down
14 changes: 8 additions & 6 deletions include/mbgl/mtl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class Context final : public gfx::Context {

std::unique_ptr<gfx::CommandEncoder> createCommandEncoder() override;

gfx::RenderingStats& renderingStats() { return stats; }
const gfx::RenderingStats& renderingStats() const override { return stats; }

BufferResource createBuffer(const void* data, std::size_t size, gfx::BufferUsageType) const;

UniqueShaderProgram createProgram(std::string name,
Expand All @@ -59,6 +56,8 @@ class Context final : public gfx::Context {
MTLTexturePtr createMetalTexture(MTLTextureDescriptorPtr textureDescriptor) const;
MTLSamplerStatePtr createMetalSamplerState(MTLSamplerDescriptorPtr samplerDescriptor) const;

void clear();

// Actually remove the objects we marked as abandoned with the above methods.
void performCleanup() override;

Expand Down Expand Up @@ -119,6 +118,9 @@ class Context final : public gfx::Context {
/// Get a reusable buffer containing the standard fixed tile indexes
const BufferResource& getTileIndexBuffer();

/// Get a buffer to be bound to unused vertex buffers
const gfx::UniqueVertexBufferResource& getEmptyVertexBuffer();

bool renderTileClippingMasks(gfx::RenderPass& renderPass,
RenderStaticData& staticData,
const std::vector<shaders::ClipUBO>& tileUBOs);
Expand All @@ -130,14 +132,14 @@ class Context final : public gfx::Context {
std::optional<BufferResource> tileVertexBuffer;
std::optional<BufferResource> tileIndexBuffer;

gfx::UniqueVertexBufferResource emptyVertexBuffer;

gfx::ShaderProgramBasePtr clipMaskShader;
MTLDepthStencilStatePtr clipMaskDepthStencilState;
MTLRenderPipelineStatePtr clipMaskPipelineState;
BufferResource clipMaskUniformsBuffer;
std::optional<BufferResource> clipMaskUniformsBuffer;
bool clipMaskUniformsBufferUsed = false;
const gfx::Renderable* stencilStateRenderable = nullptr;

gfx::RenderingStats stats;
};

} // namespace mtl
Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/mtl/index_buffer_resource.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <mbgl/gfx/index_buffer.hpp>
#include <mbgl/mtl/buffer_resource.hpp>

namespace mbgl {
Expand All @@ -8,10 +9,10 @@ namespace mtl {
class IndexBufferResource : public gfx::IndexBufferResource {
public:
IndexBufferResource() = default;
IndexBufferResource(BufferResource&& ptr)
: buffer(std::move(ptr)) {}
IndexBufferResource(BufferResource&&);
IndexBufferResource(IndexBufferResource&& other)
: buffer(std::move(other.buffer)) {}
~IndexBufferResource() override;

std::size_t getSizeInBytes() const { return buffer.getSizeInBytes(); }
void* contents() const { return buffer.contents(); }
Expand Down
8 changes: 5 additions & 3 deletions include/mbgl/mtl/uniform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ namespace mtl {
class UniformBuffer final : public gfx::UniformBuffer {
public:
UniformBuffer(BufferResource&&);
UniformBuffer(const UniformBuffer&) = default;
UniformBuffer(const UniformBuffer&) = delete;
UniformBuffer(UniformBuffer&&);
~UniformBuffer() override = default;
~UniformBuffer() override;

const BufferResource& getBufferResource() const { return buffer; }

UniformBuffer clone() const { return {buffer.clone()}; }

void update(const void* data, std::size_t size_) override;

protected:
Expand All @@ -40,7 +42,7 @@ class UniformBufferArray final : public gfx::UniformBufferArray {

private:
gfx::UniqueUniformBuffer copy(const gfx::UniformBuffer& buffer) override {
return std::make_unique<UniformBuffer>(static_cast<const UniformBuffer&>(buffer));
return std::make_unique<UniformBuffer>(static_cast<const UniformBuffer&>(buffer).clone());
}
};

Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/mtl/vertex_buffer_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace mtl {
class VertexBufferResource : public gfx::VertexBufferResource {
public:
VertexBufferResource() = default;
VertexBufferResource(BufferResource&& ptr)
: buffer(std::move(ptr)) {}
VertexBufferResource(BufferResource&&);
VertexBufferResource(VertexBufferResource&& other)
: buffer(std::move(other.buffer)) {}
~VertexBufferResource() override;

std::size_t getSizeInBytes() const { return buffer.getSizeInBytes(); }
void* contents() const { return buffer.contents(); }
Expand Down
6 changes: 5 additions & 1 deletion src/mbgl/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class Context {
public:
virtual std::unique_ptr<CommandEncoder> createCommandEncoder() = 0;

virtual const RenderingStats& renderingStats() const = 0;
gfx::RenderingStats& renderingStats() { return stats; }
const gfx::RenderingStats& renderingStats() const { return stats; }

#if !defined(NDEBUG)
public:
Expand Down Expand Up @@ -156,6 +157,9 @@ class Context {
}

#endif

protected:
gfx::RenderingStats stats;
};

} // namespace gfx
Expand Down
Loading
Loading