Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mbgl/mtl/texture2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Texture2D : public gfx::Texture2D {
private:
MTL::PixelFormat getMetalPixelFormat() const noexcept;
void createMetalTexture() noexcept;
void destroyMetalTexture() noexcept;

Context& context;
MTLTexturePtr metalTexture;
Expand Down
7 changes: 6 additions & 1 deletion platform/default/src/mbgl/vulkan/headless_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,21 @@ gfx::Renderable& HeadlessBackend::getDefaultRenderable() {
}

PremultipliedImage HeadlessBackend::readStillImage() {
if (!resource) {
resource = std::make_unique<HeadlessRenderableResource>(*this);
}

auto& contextImpl = static_cast<Context&>(*context);
auto& resourceImpl = static_cast<HeadlessRenderableResource&>(*resource);

if (!texture) {
texture = std::make_unique<Texture2D>(contextImpl);
texture->setFormat(gfx::TexturePixelType::RGBA, gfx::TextureChannelDataType::UnsignedByte);
texture->setSize(size);
texture->setUsage(Texture2DUsage::Read);
}

texture->setSize(size);

contextImpl.waitFrame();
texture->copyImage(resourceImpl.getAcquiredImage());

Expand Down
24 changes: 19 additions & 5 deletions src/mbgl/mtl/texture2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ Texture2D::Texture2D(Context& context_)
: context(context_) {}

Texture2D::~Texture2D() {
if (metalTexture) {
context.renderingStats().numActiveTextures--;
context.renderingStats().memTextures -= getDataSize(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
}
destroyMetalTexture();
}

gfx::Texture2D& Texture2D::setSamplerConfiguration(const SamplerState& samplerState_) noexcept {
Expand All @@ -36,6 +33,9 @@ gfx::Texture2D& Texture2D::setFormat(gfx::TexturePixelType pixelFormat_,
if (pixelFormat_ == pixelFormat && channelType_ == channelType) {
return *this;
}

destroyMetalTexture();

pixelFormat = pixelFormat_;
channelType = channelType_;
textureDirty = true;
Expand All @@ -46,6 +46,9 @@ gfx::Texture2D& Texture2D::setSize(mbgl::Size size_) noexcept {
if (size_ == size) {
return *this;
}

destroyMetalTexture();

size = size_;
textureDirty = true;
return *this;
Expand Down Expand Up @@ -146,7 +149,8 @@ void Texture2D::createMetalTexture() noexcept {
if (size == Size{0, 0}) {
return;
}
metalTexture.reset();

destroyMetalTexture();

const auto format = getMetalPixelFormat();
if (format == MTL::PixelFormat::PixelFormatInvalid) {
Expand Down Expand Up @@ -195,6 +199,16 @@ void Texture2D::create() noexcept {
}
}

void Texture2D::destroyMetalTexture() noexcept {
if (!metalTexture) {
return;
}

metalTexture.reset();
context.renderingStats().numActiveTextures--;
context.renderingStats().memTextures -= getDataSize();
}

gfx::Texture2D& Texture2D::setUsage(MTL::TextureUsage usage_) noexcept {
usage = usage_;
textureDirty = true;
Expand Down
8 changes: 8 additions & 0 deletions src/mbgl/vulkan/texture2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ gfx::Texture2D& Texture2D::setFormat(gfx::TexturePixelType pixelFormat_,
if (pixelFormat_ == pixelFormat && channelType_ == channelType) {
return *this;
}

destroyTexture();

pixelFormat = pixelFormat_;
channelType = channelType_;
textureDirty = true;
Expand All @@ -72,6 +75,9 @@ gfx::Texture2D& Texture2D::setSize(mbgl::Size size_) noexcept {
if (size_ == size) {
return *this;
}

destroyTexture();

size = size_;
textureDirty = true;
return *this;
Expand Down Expand Up @@ -267,6 +273,8 @@ vk::SamplerAddressMode Texture2D::vulkanAddressMode(const gfx::TextureWrapType t
void Texture2D::createTexture() {
if (size.width == 0 || size.height == 0) return;

destroyTexture();

const auto& backend = context.getBackend();

const auto format = vulkanFormat(pixelFormat, channelType);
Expand Down
Loading