[Impeller] Don't store textures that aren't read (most of the time) (flutter/engine#35690)

This commit is contained in:
Brandon DeRosier
2022-08-25 12:42:13 -07:00
committed by GitHub
parent 23e1f583d6
commit 6633f66050
6 changed files with 93 additions and 57 deletions

View File

@@ -44,13 +44,17 @@ void Canvas::Save() {
Save(false);
}
void Canvas::Save(bool create_subpass, Entity::BlendMode blend_mode) {
void Canvas::Save(
bool create_subpass,
Entity::BlendMode blend_mode,
std::optional<EntityPass::BackdropFilterProc> backdrop_filter) {
auto entry = CanvasStackEntry{};
entry.xformation = xformation_stack_.back().xformation;
entry.stencil_depth = xformation_stack_.back().stencil_depth;
if (create_subpass) {
entry.is_subpass = true;
auto subpass = std::make_unique<EntityPass>();
subpass->SetBackdropFilter(backdrop_filter);
subpass->SetBlendMode(blend_mode);
current_pass_ = GetCurrentPass().AddSubpass(std::move(subpass));
current_pass_->SetTransformation(xformation_stack_.back().xformation);
@@ -313,12 +317,11 @@ size_t Canvas::GetStencilDepth() const {
void Canvas::SaveLayer(Paint paint,
std::optional<Rect> bounds,
std::optional<Paint::ImageFilterProc> backdrop_filter) {
Save(true, paint.blend_mode);
Save(true, paint.blend_mode, backdrop_filter);
auto& new_layer_pass = GetCurrentPass();
new_layer_pass.SetDelegate(
std::make_unique<PaintPassDelegate>(paint, bounds));
new_layer_pass.SetBackdropFilter(backdrop_filter);
if (bounds.has_value() && !backdrop_filter.has_value()) {
// Render target switches due to a save layer can be elided. In such cases

View File

@@ -124,7 +124,9 @@ class Canvas {
size_t GetStencilDepth() const;
void Save(bool create_subpass,
Entity::BlendMode = Entity::BlendMode::kSourceOver);
Entity::BlendMode = Entity::BlendMode::kSourceOver,
std::optional<EntityPass::BackdropFilterProc> backdrop_filter =
std::nullopt);
void RestoreClip();

View File

@@ -39,7 +39,7 @@ void EntityPass::SetDelegate(std::unique_ptr<EntityPassDelegate> delegate) {
void EntityPass::AddEntity(Entity entity) {
if (entity.GetBlendMode() > Entity::BlendMode::kLastPipelineBlendMode) {
reads_from_pass_texture_ = true;
reads_from_pass_texture_ += 1;
}
elements_.emplace_back(std::move(entity));
@@ -133,7 +133,7 @@ EntityPass* EntityPass::AddSubpass(std::unique_ptr<EntityPass> pass) {
if (pass->blend_mode_ > Entity::BlendMode::kLastPipelineBlendMode ||
pass->backdrop_filter_proc_.has_value()) {
reads_from_pass_texture_ = true;
reads_from_pass_texture_ += 1;
}
auto subpass_pointer = pass.get();
@@ -143,7 +143,7 @@ EntityPass* EntityPass::AddSubpass(std::unique_ptr<EntityPass> pass) {
bool EntityPass::Render(ContentContext& renderer,
RenderTarget render_target) const {
if (reads_from_pass_texture_) {
if (reads_from_pass_texture_ > 0) {
auto offscreen_target = RenderTarget::CreateOffscreenMSAA(
*renderer.GetContext(), // context
render_target.GetRenderTargetSize(), // size
@@ -291,7 +291,7 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
}
RenderTarget subpass_target;
if (subpass->reads_from_pass_texture_) {
if (subpass->reads_from_pass_texture_ > 0) {
subpass_target = RenderTarget::CreateOffscreenMSAA(
*renderer.GetContext(), // context
ISize::Ceil(subpass_coverage->size), // size
@@ -372,7 +372,8 @@ bool EntityPass::OnRender(ContentContext& renderer,
TRACE_EVENT0("impeller", "EntityPass::OnRender");
auto context = renderer.GetContext();
InlinePassContext pass_context(context, render_target);
InlinePassContext pass_context(context, render_target,
reads_from_pass_texture_);
if (!pass_context.IsValid()) {
return false;
}
@@ -530,10 +531,12 @@ void EntityPass::SetBlendMode(Entity::BlendMode blend_mode) {
}
void EntityPass::SetBackdropFilter(std::optional<BackdropFilterProc> proc) {
backdrop_filter_proc_ = proc;
if (proc.has_value() && superpass_) {
superpass_->reads_from_pass_texture_ = true;
if (superpass_) {
VALIDATION_LOG << "Backdrop filters cannot be set on EntityPasses that "
"have already been appended to another pass.";
}
backdrop_filter_proc_ = proc;
}
} // namespace impeller

View File

@@ -116,12 +116,13 @@ class EntityPass {
size_t stencil_depth_ = 0u;
Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver;
bool cover_whole_screen = false;
/// This flag is set to `true` whenever an entity is added to the pass that
/// requires reading the pass texture during rendering. This can happen in the
/// following scenarios:
/// This value is incremented whenever something is added to the pass that
/// requires reading from the backdrop texture. Currently, this can happen in
/// the following scenarios:
/// 1. An entity with an "advanced blend" is added to the pass.
/// 2. A subpass with a backdrop filter is added to the pass.
bool reads_from_pass_texture_ = false;
uint32_t reads_from_pass_texture_ = 0;
std::optional<BackdropFilterProc> backdrop_filter_proc_ = std::nullopt;

View File

@@ -6,12 +6,16 @@
#include "impeller/base/validation.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/formats.h"
namespace impeller {
InlinePassContext::InlinePassContext(std::shared_ptr<Context> context,
RenderTarget render_target)
: context_(context), render_target_(render_target) {}
RenderTarget render_target,
uint32_t pass_texture_reads)
: context_(context),
render_target_(render_target),
total_pass_reads_(pass_texture_reads) {}
InlinePassContext::~InlinePassContext() {
EndPass();
@@ -57,46 +61,67 @@ const RenderTarget& InlinePassContext::GetRenderTarget() const {
std::shared_ptr<RenderPass> InlinePassContext::GetRenderPass(
uint32_t pass_depth) {
// Create a new render pass if one isn't active.
if (!IsActive()) {
command_buffer_ = context_->CreateCommandBuffer();
if (!command_buffer_) {
VALIDATION_LOG << "Could not create command buffer.";
return nullptr;
}
command_buffer_->SetLabel(
"EntityPass Command Buffer: Depth=" + std::to_string(pass_depth) +
" Count=" + std::to_string(pass_count_));
// Never clear the texture for subsequent passes.
if (pass_count_ > 0) {
if (!render_target_.GetColorAttachments().empty()) {
auto color0 = render_target_.GetColorAttachments().find(0)->second;
color0.load_action = LoadAction::kLoad;
render_target_.SetColorAttachment(color0, 0);
}
if (auto stencil = render_target_.GetStencilAttachment();
stencil.has_value()) {
stencil->load_action = LoadAction::kLoad;
render_target_.SetStencilAttachment(stencil.value());
}
}
pass_ = command_buffer_->CreateRenderPass(render_target_);
if (!pass_) {
VALIDATION_LOG << "Could not create render pass.";
return nullptr;
}
pass_->SetLabel(
"EntityPass Render Pass: Depth=" + std::to_string(pass_depth) +
" Count=" + std::to_string(pass_count_));
++pass_count_;
if (IsActive()) {
return pass_;
}
/// Create a new render pass if one isn't active. This path will run the first
/// time this method is called, but it'll also run if the pass has been
/// previously ended via `EndPass`.
command_buffer_ = context_->CreateCommandBuffer();
if (!command_buffer_) {
VALIDATION_LOG << "Could not create command buffer.";
return nullptr;
}
if (render_target_.GetColorAttachments().empty()) {
VALIDATION_LOG << "Color attachment unexpectedly missing from the "
"EntityPass render target.";
return nullptr;
}
auto color0 = render_target_.GetColorAttachments().find(0)->second;
auto stencil = render_target_.GetStencilAttachment();
if (!stencil.has_value()) {
VALIDATION_LOG << "Stencil attachment unexpectedly missing from the "
"EntityPass render target.";
return nullptr;
}
command_buffer_->SetLabel(
"EntityPass Command Buffer: Depth=" + std::to_string(pass_depth) +
" Count=" + std::to_string(pass_count_));
// Only clear the color and stencil if this is the very first pass of the
// layer.
color0.load_action = pass_count_ > 0 ? LoadAction::kLoad : LoadAction::kClear;
stencil->load_action = color0.load_action;
// If we're on the last pass of the layer, there's no need to store the
// multisample texture or stencil because nothing needs to read it.
color0.store_action = pass_count_ == total_pass_reads_
? StoreAction::kMultisampleResolve
: StoreAction::kStoreAndMultisampleResolve;
stencil->store_action = pass_count_ == total_pass_reads_
? StoreAction::kDontCare
: StoreAction::kStore;
render_target_.SetColorAttachment(color0, 0);
render_target_.SetStencilAttachment(stencil.value());
pass_ = command_buffer_->CreateRenderPass(render_target_);
if (!pass_) {
VALIDATION_LOG << "Could not create render pass.";
return nullptr;
}
pass_->SetLabel(
"EntityPass Render Pass: Depth=" + std::to_string(pass_depth) +
" Count=" + std::to_string(pass_count_));
++pass_count_;
return pass_;
}

View File

@@ -13,7 +13,8 @@ namespace impeller {
class InlinePassContext {
public:
InlinePassContext(std::shared_ptr<Context> context,
RenderTarget render_target);
RenderTarget render_target,
uint32_t pass_texture_reads);
~InlinePassContext();
bool IsValid() const;
@@ -30,6 +31,7 @@ class InlinePassContext {
std::shared_ptr<CommandBuffer> command_buffer_;
std::shared_ptr<RenderPass> pass_;
uint32_t pass_count_ = 0;
uint32_t total_pass_reads_ = 0;
FML_DISALLOW_COPY_AND_ASSIGN(InlinePassContext);
};