diff options
author | Mikko Pulkki <mikko.pulkki@mapbox.com> | 2019-10-15 12:09:21 +0300 |
---|---|---|
committer | Mikko Pulkki <55925868+mpulkki-mapbox@users.noreply.github.com> | 2019-10-30 14:32:27 +0200 |
commit | fb062bb23e66af3d5a889b767aae380884b259e5 (patch) | |
tree | 4cb52d0a85e5eebbef4a340dce767c938e6052fa /src/mbgl/gl | |
parent | c8576e089f12438a2384a4fe822af650e94ace74 (diff) | |
download | qtlocation-mapboxgl-fb062bb23e66af3d5a889b767aae380884b259e5.tar.gz |
[render-test] Add resource usage tracking to gfx context
Diffstat (limited to 'src/mbgl/gl')
-rw-r--r-- | src/mbgl/gl/context.cpp | 30 | ||||
-rw-r--r-- | src/mbgl/gl/context.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/gl/index_buffer_resource.cpp | 14 | ||||
-rw-r--r-- | src/mbgl/gl/index_buffer_resource.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/gl/object.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/gl/texture_resource.cpp | 45 | ||||
-rw-r--r-- | src/mbgl/gl/texture_resource.hpp | 7 | ||||
-rw-r--r-- | src/mbgl/gl/upload_pass.cpp | 12 | ||||
-rw-r--r-- | src/mbgl/gl/vertex_buffer_resource.cpp | 14 | ||||
-rw-r--r-- | src/mbgl/gl/vertex_buffer_resource.hpp | 5 |
10 files changed, 126 insertions, 12 deletions
diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index f81ac48ee5..e2bcc7fadc 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -54,13 +54,15 @@ Context::Context(RendererBackend& backend_) GLint value; MBGL_CHECK_ERROR(glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &value)); return value; - }()), backend(backend_) { -} + }()), + backend(backend_), + stats() {} Context::~Context() { if (cleanupOnDestruction) { reset(); } + assert(stats.isZero()); } void Context::initializeExtensions(const std::function<gl::ProcAddress(const char*)>& getProcAddress) { @@ -206,10 +208,12 @@ UniqueTexture Context::createUniqueTexture() { if (pooledTextures.empty()) { pooledTextures.resize(TextureMax); MBGL_CHECK_ERROR(glGenTextures(TextureMax, pooledTextures.data())); + stats.numCreatedTextures += TextureMax; } TextureID id = pooledTextures.back(); pooledTextures.pop_back(); + stats.numActiveTextures++; // NOLINTNEXTLINE(performance-move-const-arg) return UniqueTexture{std::move(id), {this}}; } @@ -238,6 +242,7 @@ VertexArray Context::createVertexArray() { UniqueFramebuffer Context::createFramebuffer() { FramebufferID id = 0; MBGL_CHECK_ERROR(glGenFramebuffers(1, &id)); + stats.numFrameBuffers++; // NOLINTNEXTLINE(performance-move-const-arg) return UniqueFramebuffer{ std::move(id), { this } }; } @@ -245,8 +250,10 @@ UniqueFramebuffer Context::createFramebuffer() { std::unique_ptr<gfx::TextureResource> Context::createTextureResource( const Size size, const gfx::TexturePixelType format, const gfx::TextureChannelDataType type) { auto obj = createUniqueTexture(); + int textureByteSize = gl::TextureResource::getStorageSize(size, format, type); + stats.memTextures += textureByteSize; std::unique_ptr<gfx::TextureResource> resource = - std::make_unique<gl::TextureResource>(std::move(obj)); + std::make_unique<gl::TextureResource>(std::move(obj), textureByteSize); // Always use texture unit 0 for manipulating it. activeTextureUnit = 0; @@ -517,6 +524,8 @@ void Context::clear(optional<mbgl::Color> color, } MBGL_CHECK_ERROR(glClear(mask)); + + stats.numDrawCalls = 0; } void Context::setCullFaceMode(const gfx::CullFaceMode& mode) { @@ -583,6 +592,14 @@ std::unique_ptr<gfx::CommandEncoder> Context::createCommandEncoder() { return std::make_unique<gl::CommandEncoder>(*this); } +gfx::RenderingStats& Context::renderingStats() { + return stats; +} + +const gfx::RenderingStats& Context::renderingStats() const { + return stats; +} + void Context::finish() { MBGL_CHECK_ERROR(glFinish()); } @@ -611,6 +628,8 @@ void Context::draw(const gfx::DrawMode& drawMode, static_cast<GLsizei>(indexLength), GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(sizeof(uint16_t) * indexOffset))); + + stats.numDrawCalls++; } void Context::performCleanup() { @@ -647,6 +666,7 @@ void Context::performCleanup() { } } MBGL_CHECK_ERROR(glDeleteBuffers(int(abandonedBuffers.size()), abandonedBuffers.data())); + stats.numBuffers -= int(abandonedBuffers.size()); abandonedBuffers.clear(); } @@ -659,6 +679,8 @@ void Context::performCleanup() { } } MBGL_CHECK_ERROR(glDeleteTextures(int(abandonedTextures.size()), abandonedTextures.data())); + stats.numCreatedTextures -= int(abandonedTextures.size()); + assert(stats.numCreatedTextures >= 0); abandonedTextures.clear(); } @@ -682,6 +704,8 @@ void Context::performCleanup() { } MBGL_CHECK_ERROR( glDeleteFramebuffers(int(abandonedFramebuffers.size()), abandonedFramebuffers.data())); + stats.numFrameBuffers -= int(abandonedFramebuffers.size()); + assert(stats.numFrameBuffers >= 0); abandonedFramebuffers.clear(); } diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index edcdde1ec6..b0d043ff9f 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -43,6 +43,9 @@ public: std::unique_ptr<gfx::CommandEncoder> createCommandEncoder() override; + gfx::RenderingStats& renderingStats(); + const gfx::RenderingStats& renderingStats() const override; + void initializeExtensions(const std::function<gl::ProcAddress(const char*)>&); void enableDebugging(); @@ -132,6 +135,7 @@ private: RendererBackend& backend; bool cleanupOnDestruction = true; + gfx::RenderingStats stats; std::unique_ptr<extension::Debugging> debugging; std::unique_ptr<extension::VertexArray> vertexArray; diff --git a/src/mbgl/gl/index_buffer_resource.cpp b/src/mbgl/gl/index_buffer_resource.cpp new file mode 100644 index 0000000000..2ef178846b --- /dev/null +++ b/src/mbgl/gl/index_buffer_resource.cpp @@ -0,0 +1,14 @@ +#include <mbgl/gl/context.hpp> +#include <mbgl/gl/index_buffer_resource.hpp> + +namespace mbgl { +namespace gl { + +IndexBufferResource::~IndexBufferResource() { + auto& stats = buffer.get_deleter().context.renderingStats(); + stats.memIndexBuffers -= byteSize; + assert(stats.memIndexBuffers >= 0); +} + +} // namespace gl +} // namespace mbgl
\ No newline at end of file diff --git a/src/mbgl/gl/index_buffer_resource.hpp b/src/mbgl/gl/index_buffer_resource.hpp index 2da25fdb96..00c66be5b5 100644 --- a/src/mbgl/gl/index_buffer_resource.hpp +++ b/src/mbgl/gl/index_buffer_resource.hpp @@ -8,10 +8,11 @@ namespace gl { class IndexBufferResource : public gfx::IndexBufferResource { public: - IndexBufferResource(UniqueBuffer&& buffer_) : buffer(std::move(buffer_)) { - } + IndexBufferResource(UniqueBuffer&& buffer_, int byteSize_) : buffer(std::move(buffer_)), byteSize(byteSize_) {} + ~IndexBufferResource(); UniqueBuffer buffer; + int byteSize; }; } // namespace gl diff --git a/src/mbgl/gl/object.cpp b/src/mbgl/gl/object.cpp index ec2998a27d..c075aa8f13 100644 --- a/src/mbgl/gl/object.cpp +++ b/src/mbgl/gl/object.cpp @@ -28,6 +28,8 @@ void TextureDeleter::operator()(TextureID id) const { } else { context->pooledTextures.push_back(id); } + context->renderingStats().numActiveTextures--; + assert(context->renderingStats().numActiveTextures >= 0); } void VertexArrayDeleter::operator()(VertexArrayID id) const { diff --git a/src/mbgl/gl/texture_resource.cpp b/src/mbgl/gl/texture_resource.cpp new file mode 100644 index 0000000000..b9bf620eea --- /dev/null +++ b/src/mbgl/gl/texture_resource.cpp @@ -0,0 +1,45 @@ +#include <mbgl/gl/context.hpp> +#include <mbgl/gl/texture_resource.hpp> + +namespace mbgl { +namespace gl { + +static int channelCount(gfx::TexturePixelType format) { + switch (format) { + case gfx::TexturePixelType::Alpha: + case gfx::TexturePixelType::Depth: + case gfx::TexturePixelType::Luminance: + case gfx::TexturePixelType::Stencil: + return 1; + case gfx::TexturePixelType::RGBA: + return 4; + default: + assert(!"Unknown texture pixel type"); + return 0; + } +} + +static int channelStorageSize(gfx::TextureChannelDataType type) { + switch (type) { + case gfx::TextureChannelDataType::HalfFloat: + return 2; + case gfx::TextureChannelDataType::UnsignedByte: + return 1; + default: + assert(!"Unknown texture channel data type"); + return 0; + } +} + +TextureResource::~TextureResource() { + auto& stats = texture.get_deleter().context->renderingStats(); + stats.memTextures -= byteSize; + assert(stats.memTextures >= 0); +} + +int TextureResource::getStorageSize(const Size& size, gfx::TexturePixelType format, gfx::TextureChannelDataType type) { + return size.width * size.height * channelCount(format) * channelStorageSize(type); +} + +} // namespace gl +} // namespace mbgl
\ No newline at end of file diff --git a/src/mbgl/gl/texture_resource.hpp b/src/mbgl/gl/texture_resource.hpp index ed742e75b7..494e5ae8a3 100644 --- a/src/mbgl/gl/texture_resource.hpp +++ b/src/mbgl/gl/texture_resource.hpp @@ -8,14 +8,17 @@ namespace gl { class TextureResource : public gfx::TextureResource { public: - TextureResource(UniqueTexture&& texture_) : texture(std::move(texture_)) { - } + TextureResource(UniqueTexture&& texture_, int byteSize_) : texture(std::move(texture_)), byteSize(byteSize_) {} + ~TextureResource(); + + static int getStorageSize(const Size& size, gfx::TexturePixelType format, gfx::TextureChannelDataType type); UniqueTexture texture; gfx::TextureFilterType filter = gfx::TextureFilterType::Nearest; gfx::TextureMipMapType mipmap = gfx::TextureMipMapType::No; gfx::TextureWrapType wrapX = gfx::TextureWrapType::Clamp; gfx::TextureWrapType wrapY = gfx::TextureWrapType::Clamp; + int byteSize; }; } // namespace gl diff --git a/src/mbgl/gl/upload_pass.cpp b/src/mbgl/gl/upload_pass.cpp index 4312488fb4..962bc72239 100644 --- a/src/mbgl/gl/upload_pass.cpp +++ b/src/mbgl/gl/upload_pass.cpp @@ -20,12 +20,14 @@ std::unique_ptr<gfx::VertexBufferResource> UploadPass::createVertexBufferResourc const void* data, std::size_t size, const gfx::BufferUsageType usage) { BufferID id = 0; MBGL_CHECK_ERROR(glGenBuffers(1, &id)); + commandEncoder.context.renderingStats().numBuffers++; + commandEncoder.context.renderingStats().memVertexBuffers += size; // NOLINTNEXTLINE(performance-move-const-arg) UniqueBuffer result{ std::move(id), { commandEncoder.context } }; commandEncoder.context.vertexBuffer = result; MBGL_CHECK_ERROR( glBufferData(GL_ARRAY_BUFFER, size, data, Enum<gfx::BufferUsageType>::to(usage))); - return std::make_unique<gl::VertexBufferResource>(std::move(result)); + return std::make_unique<gl::VertexBufferResource>(std::move(result), size); } void UploadPass::updateVertexBufferResource(gfx::VertexBufferResource& resource, @@ -39,13 +41,15 @@ std::unique_ptr<gfx::IndexBufferResource> UploadPass::createIndexBufferResource( const void* data, std::size_t size, const gfx::BufferUsageType usage) { BufferID id = 0; MBGL_CHECK_ERROR(glGenBuffers(1, &id)); + commandEncoder.context.renderingStats().numBuffers++; + commandEncoder.context.renderingStats().memIndexBuffers += size; // NOLINTNEXTLINE(performance-move-const-arg) UniqueBuffer result{ std::move(id), { commandEncoder.context } }; commandEncoder.context.bindVertexArray = 0; commandEncoder.context.globalVertexArrayState.indexBuffer = result; MBGL_CHECK_ERROR( glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, Enum<gfx::BufferUsageType>::to(usage))); - return std::make_unique<gl::IndexBufferResource>(std::move(result)); + return std::make_unique<gl::IndexBufferResource>(std::move(result), size); } void UploadPass::updateIndexBufferResource(gfx::IndexBufferResource& resource, @@ -65,8 +69,10 @@ UploadPass::createTextureResource(const Size size, gfx::TexturePixelType format, gfx::TextureChannelDataType type) { auto obj = commandEncoder.context.createUniqueTexture(); + int textureByteSize = gl::TextureResource::getStorageSize(size, format, type); + commandEncoder.context.renderingStats().memTextures += textureByteSize; std::unique_ptr<gfx::TextureResource> resource = - std::make_unique<gl::TextureResource>(std::move(obj)); + std::make_unique<gl::TextureResource>(std::move(obj), textureByteSize); commandEncoder.context.pixelStoreUnpack = { 1 }; updateTextureResource(*resource, size, data, format, type); // We are using clamp to edge here since OpenGL ES doesn't allow GL_REPEAT on NPOT textures. diff --git a/src/mbgl/gl/vertex_buffer_resource.cpp b/src/mbgl/gl/vertex_buffer_resource.cpp new file mode 100644 index 0000000000..cddbdd43d0 --- /dev/null +++ b/src/mbgl/gl/vertex_buffer_resource.cpp @@ -0,0 +1,14 @@ +#include <mbgl/gl/context.hpp> +#include <mbgl/gl/vertex_buffer_resource.hpp> + +namespace mbgl { +namespace gl { + +VertexBufferResource::~VertexBufferResource() { + auto& stats = buffer.get_deleter().context.renderingStats(); + stats.memVertexBuffers -= byteSize; + assert(stats.memVertexBuffers >= 0); +} + +} // namespace gl +} // namespace mbgl
\ No newline at end of file diff --git a/src/mbgl/gl/vertex_buffer_resource.hpp b/src/mbgl/gl/vertex_buffer_resource.hpp index 95e5e75d45..f9c599c757 100644 --- a/src/mbgl/gl/vertex_buffer_resource.hpp +++ b/src/mbgl/gl/vertex_buffer_resource.hpp @@ -8,10 +8,11 @@ namespace gl { class VertexBufferResource : public gfx::VertexBufferResource { public: - VertexBufferResource(UniqueBuffer&& buffer_) : buffer(std::move(buffer_)) { - } + VertexBufferResource(UniqueBuffer&& buffer_, int byteSize_) : buffer(std::move(buffer_)), byteSize(byteSize_) {} + ~VertexBufferResource(); UniqueBuffer buffer; + int byteSize; }; } // namespace gl |