diff --git a/filament/backend/src/vulkan/VulkanHandles.cpp b/filament/backend/src/vulkan/VulkanHandles.cpp index 0ba05957ad..5f9bf0f427 100644 --- a/filament/backend/src/vulkan/VulkanHandles.cpp +++ b/filament/backend/src/vulkan/VulkanHandles.cpp @@ -163,8 +163,12 @@ VulkanRenderTarget::VulkanRenderTarget(VulkanContext& context, uint32_t width, u const VulkanAttachment& spec = color[index]; VulkanTexture* texture = spec.texture; if (texture && texture->samples == 1) { - VulkanTexture* msTexture = new VulkanTexture(context, texture->target, level, - texture->format, samples, width, height, depth, texture->usage, stagePool); + VulkanTexture* msTexture = texture->getSidecar(); + if (UTILS_UNLIKELY(msTexture == nullptr)) { + msTexture = new VulkanTexture(context, texture->target, level, + texture->format, samples, width, height, depth, texture->usage, stagePool); + texture->setSidecar(msTexture); + } mMsaaAttachments[index] = createAttachment(context, { .texture = msTexture }); mMsaaAttachments[index].view = msTexture->getAttachmentView(0, 0, VK_IMAGE_ASPECT_COLOR_BIT); @@ -185,8 +189,12 @@ VulkanRenderTarget::VulkanRenderTarget(VulkanContext& context, uint32_t width, u } // Create sidecar MSAA texture for the depth attachment. - VulkanTexture* msTexture = new VulkanTexture(context, depthTexture->target, level, - depthTexture->format, samples, width, height, depth, depthTexture->usage, stagePool); + VulkanTexture* msTexture = depthTexture->getSidecar(); + if (UTILS_UNLIKELY(msTexture == nullptr)) { + msTexture = new VulkanTexture(context, depthTexture->target, level, + depthTexture->format, samples, width, height, depth, depthTexture->usage, stagePool); + depthTexture->setSidecar(msTexture); + } mMsaaDepthAttachment = createAttachment(context, { .format = {}, .image = {}, @@ -201,17 +209,6 @@ VulkanRenderTarget::VulkanRenderTarget(VulkanContext& context, uint32_t width, u VK_IMAGE_ASPECT_DEPTH_BIT); } -VulkanRenderTarget::~VulkanRenderTarget() { - for (int index = 0; index < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; index++) { - if (mMsaaAttachments[index].texture != mColor[index].texture) { - delete mMsaaAttachments[index].texture; - } - } - if (mMsaaDepthAttachment.texture != mDepth.texture) { - delete mMsaaDepthAttachment.texture; - } -} - void VulkanRenderTarget::transformClientRectToPlatform(VulkanSwapChain* currentSurface, VkRect2D* bounds) const { const auto& extent = getExtent(currentSurface); flipVertically(bounds, extent.height); diff --git a/filament/backend/src/vulkan/VulkanHandles.h b/filament/backend/src/vulkan/VulkanHandles.h index ea312e7b06..13f168aa74 100644 --- a/filament/backend/src/vulkan/VulkanHandles.h +++ b/filament/backend/src/vulkan/VulkanHandles.h @@ -53,8 +53,6 @@ struct VulkanRenderTarget : private HwRenderTarget { // Creates a special "default" render target (i.e. associated with the swap chain) explicit VulkanRenderTarget(VulkanContext& context); - ~VulkanRenderTarget(); - void transformClientRectToPlatform(VulkanSwapChain* currentSurface, VkRect2D* bounds) const; void transformClientRectToPlatform(VulkanSwapChain* currentSurface, VkViewport* bounds) const; VkExtent2D getExtent(VulkanSwapChain* currentSurface) const; diff --git a/filament/backend/src/vulkan/VulkanTexture.cpp b/filament/backend/src/vulkan/VulkanTexture.cpp index eb985f0160..8bdfca9404 100644 --- a/filament/backend/src/vulkan/VulkanTexture.cpp +++ b/filament/backend/src/vulkan/VulkanTexture.cpp @@ -245,6 +245,7 @@ VulkanTexture::VulkanTexture(VulkanContext& context, SamplerType target, uint8_t } VulkanTexture::~VulkanTexture() { + delete mSidecarMSAA; vkDestroyImage(mContext.device, mTextureImage, VKALLOC); vkFreeMemory(mContext.device, mTextureImageMemory, VKALLOC); for (auto entry : mCachedImageViews) { diff --git a/filament/backend/src/vulkan/VulkanTexture.h b/filament/backend/src/vulkan/VulkanTexture.h index 0db30b03aa..c7c4c3e1a4 100644 --- a/filament/backend/src/vulkan/VulkanTexture.h +++ b/filament/backend/src/vulkan/VulkanTexture.h @@ -49,6 +49,8 @@ struct VulkanTexture : public HwTexture { VkFormat getVkFormat() const { return mVkFormat; } VkImage getVkImage() const { return mTextureImage; } + void setSidecar(VulkanTexture* sidecar) { mSidecarMSAA = sidecar; } + VulkanTexture* getSidecar() const { return mSidecarMSAA; } private: // Gets or creates a cached VkImageView for a range of miplevels and array layers. @@ -67,6 +69,7 @@ private: void updateWithBlitImage(const PixelBufferDescriptor& hostData, uint32_t width, uint32_t height, uint32_t depth, int miplevel); + VulkanTexture* mSidecarMSAA = nullptr; const VkFormat mVkFormat; const VkComponentMapping mSwizzle; VkImageViewType mViewType;