diff --git a/impeller/renderer/backend/vulkan/surface_vk.cc b/impeller/renderer/backend/vulkan/surface_vk.cc index c5700a7783949..949da581be0f2 100644 --- a/impeller/renderer/backend/vulkan/surface_vk.cc +++ b/impeller/renderer/backend/vulkan/surface_vk.cc @@ -30,14 +30,13 @@ std::unique_ptr SurfaceVK::WrapSwapchainImage( msaa_tex_desc.size = swapchain_image->GetSize(); msaa_tex_desc.usage = static_cast(TextureUsage::kRenderTarget); - if (!swapchain_image->HasMSAATexture()) { + if (!swapchain_image->GetMSAATexture()) { msaa_tex = context->GetResourceAllocator()->CreateTexture(msaa_tex_desc); msaa_tex->SetLabel("ImpellerOnscreenColorMSAA"); if (!msaa_tex) { VALIDATION_LOG << "Could not allocate MSAA color texture."; return nullptr; } - swapchain_image->SetMSAATexture(msaa_tex); } else { msaa_tex = swapchain_image->GetMSAATexture(); } @@ -77,6 +76,10 @@ std::unique_ptr SurfaceVK::WrapSwapchainImage( RenderTarget render_target_desc; render_target_desc.SetColorAttachment(color0, 0u); + render_target_desc.SetupDepthStencilAttachments( + *context, *context->GetResourceAllocator(), swapchain_image->GetSize(), + enable_msaa, "Onscreen", RenderTarget::kDefaultStencilAttachmentConfig, + swapchain_image->GetDepthStencilTexture()); // The constructor is private. So make_unique may not be used. return std::unique_ptr( diff --git a/impeller/renderer/backend/vulkan/surface_vk.h b/impeller/renderer/backend/vulkan/surface_vk.h index ea4203135324c..73bbf2b7319f4 100644 --- a/impeller/renderer/backend/vulkan/surface_vk.h +++ b/impeller/renderer/backend/vulkan/surface_vk.h @@ -7,7 +7,6 @@ #include -#include "flutter/fml/macros.h" #include "impeller/renderer/backend/vulkan/context_vk.h" #include "impeller/renderer/backend/vulkan/swapchain_image_vk.h" #include "impeller/renderer/surface.h" diff --git a/impeller/renderer/backend/vulkan/swapchain_image_vk.cc b/impeller/renderer/backend/vulkan/swapchain_image_vk.cc index c6fc383a4c2e9..fc749b0ba72e3 100644 --- a/impeller/renderer/backend/vulkan/swapchain_image_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain_image_vk.cc @@ -36,15 +36,20 @@ bool SwapchainImageVK::IsValid() const { } std::shared_ptr SwapchainImageVK::GetMSAATexture() const { - return msaa_tex_; + return msaa_texture_; } -bool SwapchainImageVK::HasMSAATexture() const { - return msaa_tex_ != nullptr; +std::shared_ptr SwapchainImageVK::GetDepthStencilTexture() const { + return depth_stencil_texture_; } -void SwapchainImageVK::SetMSAATexture(std::shared_ptr msaa_tex) { - msaa_tex_ = std::move(msaa_tex); +void SwapchainImageVK::SetMSAATexture(std::shared_ptr texture) { + msaa_texture_ = std::move(texture); +} + +void SwapchainImageVK::SetDepthStencilTexture( + std::shared_ptr texture) { + depth_stencil_texture_ = std::move(texture); } PixelFormat SwapchainImageVK::GetPixelFormat() const { diff --git a/impeller/renderer/backend/vulkan/swapchain_image_vk.h b/impeller/renderer/backend/vulkan/swapchain_image_vk.h index 48673ebe4845d..026f1cda7b8c2 100644 --- a/impeller/renderer/backend/vulkan/swapchain_image_vk.h +++ b/impeller/renderer/backend/vulkan/swapchain_image_vk.h @@ -33,21 +33,24 @@ class SwapchainImageVK final : public TextureSourceVK { std::shared_ptr GetMSAATexture() const; - bool HasMSAATexture() const; + std::shared_ptr GetDepthStencilTexture() const; // |TextureSourceVK| vk::ImageView GetImageView() const override; vk::ImageView GetRenderTargetView() const override; - void SetMSAATexture(std::shared_ptr msaa_tex); + void SetMSAATexture(std::shared_ptr texture); + + void SetDepthStencilTexture(std::shared_ptr texture); bool IsSwapchainImage() const override { return true; } private: vk::Image image_ = VK_NULL_HANDLE; vk::UniqueImageView image_view_ = {}; - std::shared_ptr msaa_tex_; + std::shared_ptr msaa_texture_; + std::shared_ptr depth_stencil_texture_; bool is_valid_ = false; SwapchainImageVK(const SwapchainImageVK&) = delete; diff --git a/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc b/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc index 76411732c004a..6538e28ea3364 100644 --- a/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc +++ b/impeller/renderer/backend/vulkan/swapchain_impl_vk.cc @@ -228,6 +228,35 @@ SwapchainImplVK::SwapchainImplVK(const std::shared_ptr& context, texture_desc.size = ISize::MakeWH(swapchain_info.imageExtent.width, swapchain_info.imageExtent.height); + // Allocate a single onscreen MSAA texture and Depth+Stencil Texture to + // be shared by all swapchain images. + TextureDescriptor msaa_desc; + msaa_desc.storage_mode = StorageMode::kDeviceTransient; + msaa_desc.type = TextureType::kTexture2DMultisample; + msaa_desc.sample_count = SampleCount::kCount4; + msaa_desc.format = texture_desc.format; + msaa_desc.size = texture_desc.size; + msaa_desc.usage = static_cast(TextureUsage::kRenderTarget); + + TextureDescriptor depth_stencil_desc; + depth_stencil_desc.storage_mode = StorageMode::kDeviceTransient; + depth_stencil_desc.type = TextureType::kTexture2DMultisample; + depth_stencil_desc.sample_count = SampleCount::kCount4; + depth_stencil_desc.format = + context->GetCapabilities()->GetDefaultDepthStencilFormat(); + depth_stencil_desc.size = texture_desc.size; + depth_stencil_desc.usage = static_cast(TextureUsage::kRenderTarget); + + std::shared_ptr msaa_texture = + context->GetResourceAllocator()->CreateTexture(msaa_desc); + std::shared_ptr depth_stencil_texture = + context->GetResourceAllocator()->CreateTexture(depth_stencil_desc); + if (!msaa_texture || !depth_stencil_texture) { + VALIDATION_LOG + << "Failed to allocate onscreen MSAA or depth+stencil texture."; + return; + } + std::vector> swapchain_images; for (const auto& image : images) { auto swapchain_image = @@ -239,6 +268,8 @@ SwapchainImplVK::SwapchainImplVK(const std::shared_ptr& context, VALIDATION_LOG << "Could not create swapchain image."; return; } + swapchain_image->SetMSAATexture(msaa_texture); + swapchain_image->SetDepthStencilTexture(depth_stencil_texture); ContextVK::SetDebugName( vk_context.GetDevice(), swapchain_image->GetImage(), diff --git a/impeller/renderer/render_target.cc b/impeller/renderer/render_target.cc index 9cab7db94db69..f14609fa077b6 100644 --- a/impeller/renderer/render_target.cc +++ b/impeller/renderer/render_target.cc @@ -398,7 +398,8 @@ void RenderTarget::SetupDepthStencilAttachments( ISize size, bool msaa, const std::string& label, - RenderTarget::AttachmentConfig stencil_attachment_config) { + RenderTarget::AttachmentConfig stencil_attachment_config, + const std::shared_ptr& depth_stencil_texture) { TextureDescriptor depth_stencil_texture_desc; depth_stencil_texture_desc.storage_mode = stencil_attachment_config.storage_mode; @@ -412,9 +413,14 @@ void RenderTarget::SetupDepthStencilAttachments( depth_stencil_texture_desc.usage = static_cast(TextureUsage::kRenderTarget); - auto depth_stencil_texture = - allocator.CreateTexture(depth_stencil_texture_desc); - if (!depth_stencil_texture) { + std::shared_ptr new_depth_stencil_texture = + depth_stencil_texture == nullptr + ? allocator.CreateTexture(depth_stencil_texture_desc) + : depth_stencil_texture; + FML_DCHECK(depth_stencil_texture == nullptr || + depth_stencil_texture->GetTextureDescriptor() == + depth_stencil_texture_desc); + if (!new_depth_stencil_texture) { return; // Error messages are handled by `Allocator::CreateTexture`. } @@ -422,13 +428,13 @@ void RenderTarget::SetupDepthStencilAttachments( depth0.load_action = stencil_attachment_config.load_action; depth0.store_action = stencil_attachment_config.store_action; depth0.clear_depth = 0u; - depth0.texture = depth_stencil_texture; + depth0.texture = new_depth_stencil_texture; StencilAttachment stencil0; stencil0.load_action = stencil_attachment_config.load_action; stencil0.store_action = stencil_attachment_config.store_action; stencil0.clear_stencil = 0u; - stencil0.texture = std::move(depth_stencil_texture); + stencil0.texture = std::move(new_depth_stencil_texture); stencil0.texture->SetLabel( SPrintF("%s Depth+Stencil Texture", label.c_str())); diff --git a/impeller/renderer/render_target.h b/impeller/renderer/render_target.h index 0bfce5137c8fa..2a1accc6b653e 100644 --- a/impeller/renderer/render_target.h +++ b/impeller/renderer/render_target.h @@ -84,7 +84,8 @@ class RenderTarget final { bool msaa, const std::string& label = "Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config = - RenderTarget::kDefaultStencilAttachmentConfig); + RenderTarget::kDefaultStencilAttachmentConfig, + const std::shared_ptr& depth_stencil_texture = nullptr); SampleCount GetSampleCount() const; @@ -176,15 +177,6 @@ class RenderTargetAllocator { virtual void End(); private: - void SetupDepthStencilAttachments( - Allocator& allocator, - const Context& context, - ISize size, - bool msaa, - const std::string& label = "Offscreen", - RenderTarget::AttachmentConfig stencil_attachment_config = - RenderTarget::kDefaultStencilAttachmentConfig); - std::shared_ptr allocator_; };