From f821ed15502564021044ccf8cdcf114ccd1616c7 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 6 Nov 2023 10:56:07 -0800 Subject: [PATCH] [Impeller] Fix EntityPassTarget::Flip with implict MSAA. (flutter/engine#47701) This allows backdrop filters to work with GLES and MSAA. The swap implementation was only swapping out the resolve texture. But with implicit msaa resolve, the resolve texture and msaa texture are the same - so both need to be swapped. Fixes https://github.com/flutter/flutter/issues/137301 --- .../flutter/ci/licenses_golden/excluded_files | 1 + engine/src/flutter/impeller/BUILD.gn | 1 - engine/src/flutter/impeller/core/texture.h | 1 - engine/src/flutter/impeller/entity/BUILD.gn | 13 +- .../flutter/impeller/entity/entity_pass.cc | 11 +- .../impeller/entity/entity_pass_target.cc | 17 ++- .../impeller/entity/entity_pass_target.h | 4 +- .../entity/entity_pass_target_unittests.cc | 114 ++++++++++++++++++ 8 files changed, 142 insertions(+), 20 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/entity_pass_target_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index 211c6df6be..272a0cf629 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -138,6 +138,7 @@ ../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc ../../../flutter/impeller/entity/contents/test ../../../flutter/impeller/entity/contents/vertices_contents_unittests.cc +../../../flutter/impeller/entity/entity_pass_target_unittests.cc ../../../flutter/impeller/entity/entity_unittests.cc ../../../flutter/impeller/entity/geometry/geometry_unittests.cc ../../../flutter/impeller/entity/render_target_cache_unittests.cc diff --git a/engine/src/flutter/impeller/BUILD.gn b/engine/src/flutter/impeller/BUILD.gn index 3df283d93d..2eaab0e2b7 100644 --- a/engine/src/flutter/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/BUILD.gn @@ -98,7 +98,6 @@ impeller_component("impeller_unittests") { "aiks:aiks_unittests", "display_list:display_list_unittests", "entity:entity_unittests", - "entity:render_target_cache_unittests", "fixtures", "geometry:geometry_unittests", "image:image_unittests", diff --git a/engine/src/flutter/impeller/core/texture.h b/engine/src/flutter/impeller/core/texture.h index 95b21876af..8c6e3238b6 100644 --- a/engine/src/flutter/impeller/core/texture.h +++ b/engine/src/flutter/impeller/core/texture.h @@ -6,7 +6,6 @@ #include -#include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "impeller/core/formats.h" #include "impeller/core/texture_descriptor.h" diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index bf4c88139c..51e2346030 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -282,10 +282,12 @@ impeller_component("entity_unittests") { "contents/checkerboard_contents_unittests.cc", "contents/filters/inputs/filter_input_unittests.cc", "contents/vertices_contents_unittests.cc", + "entity_pass_target_unittests.cc", "entity_playground.cc", "entity_playground.h", "entity_unittests.cc", "geometry/geometry_unittests.cc", + "render_target_cache_unittests.cc", ] deps = [ @@ -296,14 +298,3 @@ impeller_component("entity_unittests") { "//flutter/impeller/typographer/backends/skia:typographer_skia_backend", ] } - -impeller_component("render_target_cache_unittests") { - testonly = true - - sources = [ "render_target_cache_unittests.cc" ] - - deps = [ - ":entity", - "//flutter/testing:testing_lib", - ] -} diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index f37bb4d4c1..c6c39a6416 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -286,7 +286,8 @@ static EntityPassTarget CreateRenderTarget(ContentContext& renderer, } return EntityPassTarget( - target, renderer.GetDeviceCapabilities().SupportsReadFromResolve()); + target, renderer.GetDeviceCapabilities().SupportsReadFromResolve(), + renderer.GetDeviceCapabilities().SupportsImplicitResolvingMSAA()); } uint32_t EntityPass::GetTotalPassReads(ContentContext& renderer) const { @@ -397,7 +398,10 @@ bool EntityPass::Render(ContentContext& renderer, entity.SetContents(contents); entity.SetBlendMode(BlendMode::kSource); - entity.Render(renderer, *render_pass); + if (!entity.Render(renderer, *render_pass)) { + VALIDATION_LOG << "Failed to render EntityPass root blit."; + return false; + } } if (!render_pass->EncodeCommands()) { @@ -455,7 +459,8 @@ bool EntityPass::Render(ContentContext& renderer, EntityPassTarget pass_target( root_render_target, - renderer.GetDeviceCapabilities().SupportsReadFromResolve()); + renderer.GetDeviceCapabilities().SupportsReadFromResolve(), + renderer.GetDeviceCapabilities().SupportsImplicitResolvingMSAA()); return OnRender( // renderer, // renderer diff --git a/engine/src/flutter/impeller/entity/entity_pass_target.cc b/engine/src/flutter/impeller/entity/entity_pass_target.cc index 46e7e47b4f..d2a63bc435 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_target.cc +++ b/engine/src/flutter/impeller/entity/entity_pass_target.cc @@ -11,9 +11,11 @@ namespace impeller { EntityPassTarget::EntityPassTarget(const RenderTarget& render_target, - bool supports_read_from_resolve) + bool supports_read_from_resolve, + bool supports_implicit_msaa) : target_(render_target), - supports_read_from_resolve_(supports_read_from_resolve) {} + supports_read_from_resolve_(supports_read_from_resolve), + supports_implicit_msaa_(supports_implicit_msaa) {} std::shared_ptr EntityPassTarget::Flip(Allocator& allocator) { auto color0 = target_.GetColorAttachments().find(0)->second; @@ -45,7 +47,16 @@ std::shared_ptr EntityPassTarget::Flip(Allocator& allocator) { } } - std::swap(color0.resolve_texture, secondary_color_texture_); + // If the color0 resolve texture is the same as the texture, then we're + // running on the GLES backend with implicit resolve. + if (supports_implicit_msaa_) { + auto new_secondary = color0.resolve_texture; + color0.resolve_texture = secondary_color_texture_; + color0.texture = secondary_color_texture_; + secondary_color_texture_ = new_secondary; + } else { + std::swap(color0.resolve_texture, secondary_color_texture_); + } target_.SetColorAttachment(color0, 0); diff --git a/engine/src/flutter/impeller/entity/entity_pass_target.h b/engine/src/flutter/impeller/entity/entity_pass_target.h index edb40b25f1..76e90ece6f 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_target.h +++ b/engine/src/flutter/impeller/entity/entity_pass_target.h @@ -14,7 +14,8 @@ class InlinePassContext; class EntityPassTarget { public: explicit EntityPassTarget(const RenderTarget& render_target, - bool supports_read_from_resolve); + bool supports_read_from_resolve, + bool supports_implicit_msaa); /// @brief Flips the backdrop and returns a readable texture that can be /// bound/sampled to restore the previous pass. @@ -34,6 +35,7 @@ class EntityPassTarget { std::shared_ptr secondary_color_texture_; bool supports_read_from_resolve_; + bool supports_implicit_msaa_; friend InlinePassContext; diff --git a/engine/src/flutter/impeller/entity/entity_pass_target_unittests.cc b/engine/src/flutter/impeller/entity/entity_pass_target_unittests.cc new file mode 100644 index 0000000000..390f792991 --- /dev/null +++ b/engine/src/flutter/impeller/entity/entity_pass_target_unittests.cc @@ -0,0 +1,114 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +#include "flutter/testing/testing.h" +#include "gtest/gtest.h" +#include "impeller/core/formats.h" +#include "impeller/entity/entity_pass_target.h" +#include "impeller/entity/entity_playground.h" + +namespace impeller { +namespace testing { + +using EntityPassTargetTest = EntityPlayground; +INSTANTIATE_PLAYGROUND_SUITE(EntityPassTargetTest); + +TEST_P(EntityPassTargetTest, SwapWithMSAATexture) { + if (GetContentContext() + ->GetDeviceCapabilities() + .SupportsImplicitResolvingMSAA()) { + GTEST_SKIP() << "Implicit MSAA is used on this device."; + } + auto content_context = GetContentContext(); + auto buffer = content_context->GetContext()->CreateCommandBuffer(); + auto render_target = RenderTarget::CreateOffscreenMSAA( + *content_context->GetContext(), + *GetContentContext()->GetRenderTargetCache(), {100, 100}); + + auto entity_pass_target = EntityPassTarget(render_target, false, false); + + auto color0 = entity_pass_target.GetRenderTarget() + .GetColorAttachments() + .find(0u) + ->second; + auto msaa_tex = color0.texture; + auto resolve_tex = color0.resolve_texture; + + entity_pass_target.Flip( + *content_context->GetContext()->GetResourceAllocator()); + + color0 = entity_pass_target.GetRenderTarget() + .GetColorAttachments() + .find(0u) + ->second; + + ASSERT_EQ(msaa_tex, color0.texture); + ASSERT_NE(resolve_tex, color0.resolve_texture); +} + +TEST_P(EntityPassTargetTest, SwapWithMSAAImplicitResolve) { + auto content_context = GetContentContext(); + auto buffer = content_context->GetContext()->CreateCommandBuffer(); + auto context = content_context->GetContext(); + auto& allocator = *context->GetResourceAllocator(); + + // Emulate implicit MSAA resolve by making color resolve and msaa texture the + // same. + RenderTarget render_target; + { + PixelFormat pixel_format = + context->GetCapabilities()->GetDefaultColorFormat(); + + // Create MSAA color texture. + + TextureDescriptor color0_tex_desc; + color0_tex_desc.storage_mode = StorageMode::kDevicePrivate; + color0_tex_desc.type = TextureType::kTexture2DMultisample; + color0_tex_desc.sample_count = SampleCount::kCount4; + color0_tex_desc.format = pixel_format; + color0_tex_desc.size = ISize{100, 100}; + color0_tex_desc.usage = static_cast(TextureUsage::kRenderTarget); + + auto color0_msaa_tex = allocator.CreateTexture(color0_tex_desc); + + // Color attachment. + + ColorAttachment color0; + color0.load_action = LoadAction::kDontCare; + color0.store_action = StoreAction::kStoreAndMultisampleResolve; + color0.texture = color0_msaa_tex; + color0.resolve_texture = color0_msaa_tex; + + render_target.SetColorAttachment(color0, 0u); + render_target.SetStencilAttachment(std::nullopt); + } + + auto entity_pass_target = EntityPassTarget(render_target, false, true); + + auto color0 = entity_pass_target.GetRenderTarget() + .GetColorAttachments() + .find(0u) + ->second; + auto msaa_tex = color0.texture; + auto resolve_tex = color0.resolve_texture; + + ASSERT_EQ(msaa_tex, resolve_tex); + + entity_pass_target.Flip( + *content_context->GetContext()->GetResourceAllocator()); + + color0 = entity_pass_target.GetRenderTarget() + .GetColorAttachments() + .find(0u) + ->second; + + ASSERT_NE(msaa_tex, color0.texture); + ASSERT_NE(resolve_tex, color0.resolve_texture); + ASSERT_EQ(color0.texture, color0.resolve_texture); +} + +} // namespace testing +} // namespace impeller