From 8d3c4a76636c4d5203315312bb35dd7254e6e5a0 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Tue, 23 Aug 2022 21:29:19 -0700 Subject: [PATCH] [Impeller] Use MSAA attachments for offscreen passes (flutter/engine#35648) --- .../entity/contents/content_context.cc | 7 +- .../flutter/impeller/entity/entity_pass.cc | 51 +++++++--- .../impeller/entity/inline_pass_context.cc | 3 +- .../renderer/backend/gles/context_gles.cc | 5 + .../renderer/backend/gles/context_gles.h | 3 + .../renderer/backend/metal/context_mtl.h | 3 + .../renderer/backend/metal/context_mtl.mm | 5 + .../renderer/backend/metal/formats_mtl.h | 4 + .../renderer/backend/metal/render_pass_mtl.mm | 14 +-- .../renderer/backend/vulkan/context_vk.cc | 4 + .../renderer/backend/vulkan/context_vk.h | 3 + .../renderer/backend/vulkan/formats_vk.h | 1 + .../src/flutter/impeller/renderer/context.h | 2 + .../src/flutter/impeller/renderer/formats.h | 2 + .../impeller/renderer/render_target.cc | 97 ++++++++++++++++++- .../flutter/impeller/renderer/render_target.h | 12 +++ 16 files changed, 189 insertions(+), 27 deletions(-) diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 7dd30168df..707a3bf5a7 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -245,7 +245,12 @@ std::shared_ptr ContentContext::MakeSubpass( SubpassCallback subpass_callback) const { auto context = GetContext(); - auto subpass_target = RenderTarget::CreateOffscreen(*context, texture_size); + RenderTarget subpass_target; + if (context->SupportsOffscreenMSAA()) { + subpass_target = RenderTarget::CreateOffscreenMSAA(*context, texture_size); + } else { + subpass_target = RenderTarget::CreateOffscreen(*context, texture_size); + } auto subpass_texture = subpass_target.GetRenderTargetTexture(); if (!subpass_texture) { return nullptr; diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index be4dc73118..953cb4052e 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -144,11 +144,18 @@ EntityPass* EntityPass::AddSubpass(std::unique_ptr pass) { bool EntityPass::Render(ContentContext& renderer, RenderTarget render_target) const { if (reads_from_pass_texture_) { - auto offscreen_target = RenderTarget::CreateOffscreen( - *renderer.GetContext(), render_target.GetRenderTargetSize(), - "EntityPass", // - StorageMode::kDevicePrivate, LoadAction::kClear, StoreAction::kStore, - StorageMode::kDevicePrivate, LoadAction::kClear, StoreAction::kStore); + auto offscreen_target = RenderTarget::CreateOffscreenMSAA( + *renderer.GetContext(), // context + render_target.GetRenderTargetSize(), // size + "EntityPass", // label + StorageMode::kDevicePrivate, // color_storage_mode + StorageMode::kDevicePrivate, // color_resolve_storage_mode + LoadAction::kClear, // color_load_action + StoreAction::kStoreAndMultisampleResolve, // color_store_action + StorageMode::kDevicePrivate, // stencil_storage_mode + LoadAction::kClear, // stencil_load_action + StoreAction::kStore // stencil_store_action + ); if (!OnRender(renderer, offscreen_target.GetRenderTargetSize(), offscreen_target, Point(), Point(), 0)) { return false; @@ -285,17 +292,31 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( RenderTarget subpass_target; if (subpass->reads_from_pass_texture_) { - subpass_target = RenderTarget::CreateOffscreen( - *renderer.GetContext(), ISize::Ceil(subpass_coverage->size), - "EntityPass", StorageMode::kDevicePrivate, LoadAction::kClear, - StoreAction::kStore, StorageMode::kDevicePrivate, LoadAction::kClear, - StoreAction::kStore); + subpass_target = RenderTarget::CreateOffscreenMSAA( + *renderer.GetContext(), // context + ISize::Ceil(subpass_coverage->size), // size + "EntityPass", // label + StorageMode::kDevicePrivate, // color_storage_mode + StorageMode::kDevicePrivate, // color_resolve_storage_mode + LoadAction::kClear, // color_load_action + StoreAction::kStoreAndMultisampleResolve, // color_store_action + StorageMode::kDevicePrivate, // stencil_storage_mode + LoadAction::kClear, // stencil_load_action + StoreAction::kStore // stencil_store_action + ); } else { - subpass_target = RenderTarget::CreateOffscreen( - *renderer.GetContext(), ISize::Ceil(subpass_coverage->size), - "EntityPass", StorageMode::kDevicePrivate, LoadAction::kClear, - StoreAction::kStore, StorageMode::kDeviceTransient, - LoadAction::kClear, StoreAction::kDontCare); + subpass_target = RenderTarget::CreateOffscreenMSAA( + *renderer.GetContext(), // context + ISize::Ceil(subpass_coverage->size), // size + "EntityPass", // label + StorageMode::kDevicePrivate, // color_storage_mode + StorageMode::kDevicePrivate, // color_resolve_storage_mode + LoadAction::kClear, // color_load_action + StoreAction::kStoreAndMultisampleResolve, // color_store_action + StorageMode::kDeviceTransient, // stencil_storage_mode + LoadAction::kClear, // stencil_load_action + StoreAction::kDontCare // stencil_store_action + ); } auto subpass_texture = subpass_target.GetRenderTargetTexture(); diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.cc b/engine/src/flutter/impeller/entity/inline_pass_context.cc index 26661e8d81..485d91d99d 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.cc +++ b/engine/src/flutter/impeller/entity/inline_pass_context.cc @@ -29,8 +29,7 @@ std::shared_ptr InlinePassContext::GetTexture() { if (!IsValid()) { return nullptr; } - auto color0 = render_target_.GetColorAttachments().find(0)->second; - return color0.resolve_texture ? color0.resolve_texture : color0.texture; + return render_target_.GetRenderTargetTexture(); } bool InlinePassContext::EndPass() { diff --git a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc index 7ff2339498..49f454f542 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.cc @@ -132,4 +132,9 @@ bool ContextGLES::HasThreadingRestrictions() const { return true; } +// |Context| +bool ContextGLES::SupportsOffscreenMSAA() const { + return false; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h index 7c7246dc79..405f31d1b1 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/context_gles.h @@ -69,6 +69,9 @@ class ContextGLES final : public Context, // |Context| bool HasThreadingRestrictions() const override; + // |Context| + bool SupportsOffscreenMSAA() const override; + FML_DISALLOW_COPY_AND_ASSIGN(ContextGLES); }; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h index 45368a26ac..98f6c9139a 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.h @@ -68,6 +68,9 @@ class ContextMTL final : public Context, // |Context| std::shared_ptr GetWorkQueue() const override; + // |Context| + bool SupportsOffscreenMSAA() const override; + std::shared_ptr CreateCommandBufferInQueue( id queue) const; diff --git a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm index b3a8d7ea5c..8cc908a8d0 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/context_mtl.mm @@ -232,4 +232,9 @@ id ContextMTL::GetMTLDevice() const { return device_; } +// |Context| +bool ContextMTL::SupportsOffscreenMSAA() const { + return true; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h index ef9bdc31fe..c9857f9a3b 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h @@ -247,6 +247,8 @@ constexpr MTLStoreAction ToMTLStoreAction(StoreAction action) { return MTLStoreActionStore; case StoreAction::kMultisampleResolve: return MTLStoreActionMultisampleResolve; + case StoreAction::kStoreAndMultisampleResolve: + return MTLStoreActionStoreAndMultisampleResolve; } return MTLStoreActionDontCare; } @@ -259,6 +261,8 @@ constexpr StoreAction FromMTLStoreAction(MTLStoreAction action) { return StoreAction::kStore; case MTLStoreActionMultisampleResolve: return StoreAction::kMultisampleResolve; + case MTLStoreActionStoreAndMultisampleResolve: + return StoreAction::kStoreAndMultisampleResolve; default: break; } diff --git a/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm index 8827498924..c277bbb505 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm @@ -22,17 +22,19 @@ namespace impeller { static bool ConfigureResolveTextureAttachment( const Attachment& desc, MTLRenderPassAttachmentDescriptor* attachment) { - if (desc.store_action == StoreAction::kMultisampleResolve && - !desc.resolve_texture) { + bool needs_resolve = + desc.store_action == StoreAction::kMultisampleResolve || + desc.store_action == StoreAction::kStoreAndMultisampleResolve; + + if (needs_resolve && !desc.resolve_texture) { VALIDATION_LOG << "Resolve store action specified on attachment but no " "resolve texture was specified."; return false; } - if (desc.resolve_texture && - desc.store_action != StoreAction::kMultisampleResolve) { - VALIDATION_LOG << "Resolve store action specified but there was no " - "resolve attachment."; + if (desc.resolve_texture && !needs_resolve) { + VALIDATION_LOG << "A resolve texture was specified even though the store " + "action doesn't require it."; return false; } diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc index e5ff525737..77bf5533af 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -501,4 +501,8 @@ void ContextVK::SetupSwapchain(vk::UniqueSurfaceKHR surface) { }); } +bool ContextVK::SupportsOffscreenMSAA() const { + return true; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h index f8a4ce6211..ab63afd061 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h @@ -109,6 +109,9 @@ class ContextVK final : public Context, public BackendCast { // |Context| std::shared_ptr GetWorkQueue() const override; + // |Context| + bool SupportsOffscreenMSAA() const override; + FML_DISALLOW_COPY_AND_ASSIGN(ContextVK); }; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h index c603dfd222..00cd5efa3c 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h @@ -283,6 +283,7 @@ constexpr vk::AttachmentStoreOp ToVKAttachmentStoreOp( case StoreAction::kDontCare: return vk::AttachmentStoreOp::eDontCare; case StoreAction::kMultisampleResolve: + case StoreAction::kStoreAndMultisampleResolve: // TODO (kaushikiska): vulkan doesn't support multisample resolve. return vk::AttachmentStoreOp::eDontCare; } diff --git a/engine/src/flutter/impeller/renderer/context.h b/engine/src/flutter/impeller/renderer/context.h index 7f13fce9ac..6db6d7bc99 100644 --- a/engine/src/flutter/impeller/renderer/context.h +++ b/engine/src/flutter/impeller/renderer/context.h @@ -41,6 +41,8 @@ class Context : public std::enable_shared_from_this { virtual bool HasThreadingRestrictions() const; + virtual bool SupportsOffscreenMSAA() const = 0; + protected: Context(); diff --git a/engine/src/flutter/impeller/renderer/formats.h b/engine/src/flutter/impeller/renderer/formats.h index 65509c6a3a..0e8ce849a2 100644 --- a/engine/src/flutter/impeller/renderer/formats.h +++ b/engine/src/flutter/impeller/renderer/formats.h @@ -99,6 +99,7 @@ enum class StoreAction { kDontCare, kStore, kMultisampleResolve, + kStoreAndMultisampleResolve, }; constexpr bool CanClearAttachment(LoadAction action) { @@ -115,6 +116,7 @@ constexpr bool CanClearAttachment(LoadAction action) { constexpr bool CanDiscardAttachmentWhenDone(StoreAction action) { switch (action) { case StoreAction::kStore: + case StoreAction::kStoreAndMultisampleResolve: return false; case StoreAction::kDontCare: case StoreAction::kMultisampleResolve: diff --git a/engine/src/flutter/impeller/renderer/render_target.cc b/engine/src/flutter/impeller/renderer/render_target.cc index 1ec8c9d914..da74b6a3c2 100644 --- a/engine/src/flutter/impeller/renderer/render_target.cc +++ b/engine/src/flutter/impeller/renderer/render_target.cc @@ -136,7 +136,8 @@ std::shared_ptr RenderTarget::GetRenderTargetTexture() const { if (found == colors_.end()) { return nullptr; } - return found->second.texture; + return found->second.resolve_texture ? found->second.resolve_texture + : found->second.texture; } RenderTarget& RenderTarget::SetColorAttachment(ColorAttachment attachment, @@ -211,7 +212,7 @@ RenderTarget RenderTarget::CreateOffscreen(const Context& context, return {}; } - color0.texture->SetLabel(SPrintF("%sColorTexture", label.c_str())); + color0.texture->SetLabel(SPrintF("%s Color Texture", label.c_str())); StencilAttachment stencil0; stencil0.load_action = stencil_load_action; @@ -224,7 +225,97 @@ RenderTarget RenderTarget::CreateOffscreen(const Context& context, return {}; } - stencil0.texture->SetLabel(SPrintF("%sStencilTexture", label.c_str())); + stencil0.texture->SetLabel(SPrintF("%s Stencil Texture", label.c_str())); + + RenderTarget target; + target.SetColorAttachment(std::move(color0), 0u); + target.SetStencilAttachment(std::move(stencil0)); + + return target; +} + +RenderTarget RenderTarget::CreateOffscreenMSAA( + const Context& context, + ISize size, + std::string label, + StorageMode color_storage_mode, + StorageMode color_resolve_storage_mode, + LoadAction color_load_action, + StoreAction color_store_action, + StorageMode stencil_storage_mode, + LoadAction stencil_load_action, + StoreAction stencil_store_action) { + if (size.IsEmpty()) { + return {}; + } + + // Create MSAA color texture. + + TextureDescriptor color0_tex_desc; + color0_tex_desc.type = TextureType::kTexture2DMultisample; + color0_tex_desc.sample_count = SampleCount::kCount4; + color0_tex_desc.format = PixelFormat::kDefaultColor; + color0_tex_desc.size = size; + color0_tex_desc.usage = static_cast(TextureUsage::kRenderTarget) | + static_cast(TextureUsage::kShaderRead); + + auto color0_msaa_tex = context.GetResourceAllocator()->CreateTexture( + color_storage_mode, color0_tex_desc); + if (!color0_msaa_tex) { + VALIDATION_LOG << "Could not create multisample color texture."; + return {}; + } + color0_msaa_tex->SetLabel( + SPrintF("%s Color Texture (Multisample)", label.c_str())); + + // Create color resolve texture. + + TextureDescriptor color0_resolve_tex_desc; + color0_resolve_tex_desc.format = PixelFormat::kDefaultColor; + color0_resolve_tex_desc.size = size; + color0_resolve_tex_desc.usage = + static_cast(TextureUsage::kRenderTarget) | + static_cast(TextureUsage::kShaderRead); + + auto color0_resolve_tex = context.GetResourceAllocator()->CreateTexture( + color_resolve_storage_mode, color0_resolve_tex_desc); + if (!color0_resolve_tex) { + VALIDATION_LOG << "Could not create color texture."; + return {}; + } + color0_resolve_tex->SetLabel(SPrintF("%s Color Texture", label.c_str())); + + // Color attachment. + + ColorAttachment color0; + color0.clear_color = Color::BlackTransparent(); + color0.load_action = color_load_action; + color0.store_action = color_store_action; + color0.texture = color0_msaa_tex; + color0.resolve_texture = color0_resolve_tex; + + // Create MSAA stencil texture. + + TextureDescriptor stencil_tex0; + stencil_tex0.type = TextureType::kTexture2DMultisample; + stencil_tex0.sample_count = SampleCount::kCount4; + stencil_tex0.format = PixelFormat::kDefaultStencil; + stencil_tex0.size = size; + stencil_tex0.usage = + static_cast(TextureUsage::kRenderTarget); + + StencilAttachment stencil0; + stencil0.load_action = stencil_load_action; + stencil0.store_action = stencil_store_action; + stencil0.clear_stencil = 0u; + stencil0.texture = context.GetResourceAllocator()->CreateTexture( + stencil_storage_mode, stencil_tex0); + + if (!stencil0.texture) { + return {}; + } + + stencil0.texture->SetLabel(SPrintF("%s Stencil Texture", label.c_str())); RenderTarget target; target.SetColorAttachment(std::move(color0), 0u); diff --git a/engine/src/flutter/impeller/renderer/render_target.h b/engine/src/flutter/impeller/renderer/render_target.h index 786e56ee7e..b81f32e0ad 100644 --- a/engine/src/flutter/impeller/renderer/render_target.h +++ b/engine/src/flutter/impeller/renderer/render_target.h @@ -30,6 +30,18 @@ class RenderTarget { LoadAction stencil_load_action = LoadAction::kClear, StoreAction stencil_store_action = StoreAction::kDontCare); + static RenderTarget CreateOffscreenMSAA( + const Context& context, + ISize size, + std::string label = "Offscreen MSAA", + StorageMode color_storage_mode = StorageMode::kDeviceTransient, + StorageMode color_resolve_storage_mode = StorageMode::kDevicePrivate, + LoadAction color_load_action = LoadAction::kClear, + StoreAction color_store_action = StoreAction::kMultisampleResolve, + StorageMode stencil_storage_mode = StorageMode::kDeviceTransient, + LoadAction stencil_load_action = LoadAction::kClear, + StoreAction stencil_store_action = StoreAction::kDontCare); + RenderTarget(); ~RenderTarget();