Flutter Impeller
inline_pass_context.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <utility>
8 
10 #include "impeller/core/formats.h"
14 
15 namespace impeller {
16 
18  std::shared_ptr<Context> context,
19  EntityPassTarget& pass_target,
20  uint32_t pass_texture_reads,
21  std::optional<RenderPassResult> collapsed_parent_pass)
22  : context_(std::move(context)),
23  pass_target_(pass_target),
24  total_pass_reads_(pass_texture_reads),
25  is_collapsed_(collapsed_parent_pass.has_value()) {
26  if (collapsed_parent_pass.has_value()) {
27  pass_ = collapsed_parent_pass.value().pass;
28  }
29 }
30 
32  if (!is_collapsed_) {
33  EndPass();
34  }
35 }
36 
38  return pass_target_.IsValid();
39 }
40 
42  return pass_ != nullptr;
43 }
44 
45 std::shared_ptr<Texture> InlinePassContext::GetTexture() {
46  if (!IsValid()) {
47  return nullptr;
48  }
49  return pass_target_.GetRenderTarget().GetRenderTargetTexture();
50 }
51 
53  if (!IsActive()) {
54  return true;
55  }
56 
57  if (command_buffer_) {
58  if (!command_buffer_->SubmitCommandsAsync(std::move(pass_))) {
60  << "Failed to encode and submit command buffer while ending "
61  "render pass.";
62  return false;
63  }
64  }
65 
66  pass_ = nullptr;
67  command_buffer_ = nullptr;
68 
69  return true;
70 }
71 
73  return pass_target_;
74 }
75 
77  uint32_t pass_depth) {
78  if (IsActive()) {
79  return {.pass = pass_};
80  }
81 
82  /// Create a new render pass if one isn't active. This path will run the first
83  /// time this method is called, but it'll also run if the pass has been
84  /// previously ended via `EndPass`.
85 
86  command_buffer_ = context_->CreateCommandBuffer();
87  if (!command_buffer_) {
88  VALIDATION_LOG << "Could not create command buffer.";
89  return {};
90  }
91 
92  if (pass_target_.GetRenderTarget().GetColorAttachments().empty()) {
93  VALIDATION_LOG << "Color attachment unexpectedly missing from the "
94  "EntityPass render target.";
95  return {};
96  }
97 
98  command_buffer_->SetLabel(
99  "EntityPass Command Buffer: Depth=" + std::to_string(pass_depth) +
100  " Count=" + std::to_string(pass_count_));
101 
102  RenderPassResult result;
103  {
104  // If the pass target has a resolve texture, then we're using MSAA.
105  bool is_msaa = pass_target_.GetRenderTarget()
107  .find(0)
108  ->second.resolve_texture != nullptr;
109  if (pass_count_ > 0 && is_msaa) {
110  result.backdrop_texture =
111  pass_target_.Flip(*context_->GetResourceAllocator());
112  if (!result.backdrop_texture) {
113  VALIDATION_LOG << "Could not flip the EntityPass render target.";
114  }
115  }
116  }
117 
118  // Find the color attachment a second time, since the target may have just
119  // flipped.
120  auto color0 =
121  pass_target_.GetRenderTarget().GetColorAttachments().find(0)->second;
122  bool is_msaa = color0.resolve_texture != nullptr;
123 
124  if (pass_count_ > 0) {
125  // When MSAA is being used, we end up overriding the entire backdrop by
126  // drawing the previous pass texture, and so we don't have to clear it and
127  // can use kDontCare.
128  color0.load_action = is_msaa ? LoadAction::kDontCare : LoadAction::kLoad;
129  } else {
130  color0.load_action = LoadAction::kClear;
131  }
132 
133  color0.store_action =
135 
136  auto stencil = pass_target_.GetRenderTarget().GetStencilAttachment();
137  if (!stencil.has_value()) {
138  VALIDATION_LOG << "Stencil attachment unexpectedly missing from the "
139  "EntityPass render target.";
140  return {};
141  }
142 
143  // Only clear the stencil if this is the very first pass of the
144  // layer.
145  stencil->load_action =
146  pass_count_ > 0 ? LoadAction::kLoad : LoadAction::kClear;
147  // If we're on the last pass of the layer, there's no need to store the
148  // stencil because nothing needs to read it.
149  stencil->store_action = pass_count_ == total_pass_reads_
152  pass_target_.target_.SetStencilAttachment(stencil.value());
153 
154  pass_target_.target_.SetColorAttachment(color0, 0);
155 
156  pass_ = command_buffer_->CreateRenderPass(pass_target_.GetRenderTarget());
157  if (!pass_) {
158  VALIDATION_LOG << "Could not create render pass.";
159  return {};
160  }
161 
162  pass_->SetLabel(
163  "EntityPass Render Pass: Depth=" + std::to_string(pass_depth) +
164  " Count=" + std::to_string(pass_count_));
165 
166  result.pass = pass_;
167 
168  if (!context_->GetCapabilities()->SupportsReadFromResolve() &&
169  result.backdrop_texture ==
170  result.pass->GetRenderTarget().GetRenderTargetTexture()) {
171  VALIDATION_LOG << "EntityPass backdrop restore configuration is not valid "
172  "for the current graphics backend.";
173  }
174 
175  ++pass_count_;
176  return result;
177 }
178 
180  return pass_count_;
181 }
182 
183 } // namespace impeller
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::LoadAction::kLoad
@ kLoad
impeller::EntityPassTarget::IsValid
bool IsValid() const
Definition: entity_pass_target.cc:61
impeller::RenderTarget::SetColorAttachment
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
Definition: render_target.cc:180
texture_descriptor.h
formats.h
impeller::StoreAction::kDontCare
@ kDontCare
impeller::EntityPassTarget
Definition: entity_pass_target.h:14
impeller::RenderTarget::GetColorAttachments
const std::map< size_t, ColorAttachment > & GetColorAttachments() const
Definition: render_target.cc:209
impeller::InlinePassContext::RenderPassResult
Definition: inline_pass_context.h:16
validation.h
impeller::InlinePassContext::InlinePassContext
InlinePassContext(std::shared_ptr< Context > context, EntityPassTarget &pass_target, uint32_t pass_texture_reads, std::optional< RenderPassResult > collapsed_parent_pass=std::nullopt)
Definition: inline_pass_context.cc:17
impeller::InlinePassContext::RenderPassResult::backdrop_texture
std::shared_ptr< Texture > backdrop_texture
Definition: inline_pass_context.h:18
impeller::InlinePassContext::GetPassCount
uint32_t GetPassCount() const
Definition: inline_pass_context.cc:179
impeller::LoadAction::kClear
@ kClear
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:155
impeller::InlinePassContext::IsActive
bool IsActive() const
Definition: inline_pass_context.cc:41
impeller::InlinePassContext::GetRenderPass
RenderPassResult GetRenderPass(uint32_t pass_depth)
Definition: inline_pass_context.cc:76
impeller::InlinePassContext::GetTexture
std::shared_ptr< Texture > GetTexture()
Definition: inline_pass_context.cc:45
impeller::StoreAction::kStore
@ kStore
impeller::InlinePassContext::EndPass
bool EndPass()
Definition: inline_pass_context.cc:52
entity_pass_target.h
impeller::InlinePassContext::IsValid
bool IsValid() const
Definition: inline_pass_context.cc:37
impeller::InlinePassContext::GetPassTarget
EntityPassTarget & GetPassTarget() const
Definition: inline_pass_context.cc:72
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
command_buffer.h
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:199
std
Definition: comparable.h:98
impeller::LoadAction::kDontCare
@ kDontCare
impeller::InlinePassContext::~InlinePassContext
~InlinePassContext()
Definition: inline_pass_context.cc:31
impeller::RenderTarget::GetStencilAttachment
const std::optional< StencilAttachment > & GetStencilAttachment() const
Definition: render_target.cc:218
impeller::InlinePassContext::RenderPassResult::pass
std::shared_ptr< RenderPass > pass
Definition: inline_pass_context.h:17
impeller
Definition: aiks_context.cc:10
impeller::EntityPassTarget::GetRenderTarget
const RenderTarget & GetRenderTarget() const
Definition: entity_pass_target.cc:57
inline_pass_context.h
impeller::EntityPassTarget::Flip
std::shared_ptr< Texture > Flip(Allocator &allocator)
Flips the backdrop and returns a readable texture that can be bound/sampled to restore the previous p...
Definition: entity_pass_target.cc:18