Flutter Impeller
impeller::RenderTarget Class Referencefinal

#include <render_target.h>

Classes

struct  AttachmentConfig
 
struct  AttachmentConfigMSAA
 

Public Member Functions

 RenderTarget ()
 
 ~RenderTarget ()
 
bool IsValid () const
 
void SetupDepthStencilAttachments (const Context &context, Allocator &allocator, ISize size, bool msaa, std::string_view label="Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &depth_stencil_texture=nullptr)
 
SampleCount GetSampleCount () const
 
bool HasColorAttachment (size_t index) const
 
ISize GetRenderTargetSize () const
 
std::shared_ptr< Texture > GetRenderTargetTexture () const
 
PixelFormat GetRenderTargetPixelFormat () const
 
std::optional< ISize > GetColorAttachmentSize (size_t index) const
 
RenderTarget & SetColorAttachment (const ColorAttachment &attachment, size_t index)
 
ColorAttachment GetColorAttachment (size_t index) const
 Get the color attachment at [index]. More...
 
RenderTarget & SetDepthAttachment (std::optional< DepthAttachment > attachment)
 
RenderTarget & SetStencilAttachment (std::optional< StencilAttachment > attachment)
 
size_t GetMaxColorAttachmentBindIndex () const
 
const std::optional< DepthAttachment > & GetDepthAttachment () const
 
const std::optional< StencilAttachment > & GetStencilAttachment () const
 
size_t GetTotalAttachmentCount () const
 
bool IterateAllColorAttachments (const std::function< bool(size_t index, const ColorAttachment &attachment)> &iterator) const
 
void IterateAllAttachments (const std::function< bool(const Attachment &attachment)> &iterator) const
 
std::string ToString () const
 
RenderTargetConfig ToConfig () const
 

Static Public Attributes

static constexpr AttachmentConfig kDefaultColorAttachmentConfig
 
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
 
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
 

Detailed Description

Definition at line 38 of file render_target.h.

Constructor & Destructor Documentation

◆ RenderTarget()

impeller::RenderTarget::RenderTarget ( )
default

◆ ~RenderTarget()

impeller::RenderTarget::~RenderTarget ( )
default

Member Function Documentation

◆ GetColorAttachment()

ColorAttachment impeller::RenderTarget::GetColorAttachment ( size_t  index) const

Get the color attachment at [index].

This function does not validate whether or not the attachment was previously defined and will return a default constructed attachment if none is set.

Definition at line 248 of file render_target.cc.

248  {
249  if (index == 0) {
250  if (color0_.has_value()) {
251  return color0_.value();
252  }
253  return ColorAttachment{};
254  }
255  std::map<size_t, ColorAttachment>::const_iterator it = colors_.find(index);
256  if (it != colors_.end()) {
257  return it->second;
258  }
259  return ColorAttachment{};
260 }

Referenced by impeller::EntityPassTarget::Flip(), impeller::InlinePassContext::GetRenderPass(), impeller::Playground::OpenPlaygroundHere(), and impeller::testing::TEST_P().

◆ GetColorAttachmentSize()

std::optional< ISize > impeller::RenderTarget::GetColorAttachmentSize ( size_t  index) const

Definition at line 169 of file render_target.cc.

169  {
170  if (index == 0u) {
171  if (color0_.has_value()) {
172  return SizeForMipLevel(color0_.value().texture,
173  color0_.value().mip_level);
174  }
175  return std::nullopt;
176  }
177  auto found = colors_.find(index);
178 
179  if (found == colors_.end()) {
180  return std::nullopt;
181  }
182 
183  return SizeForMipLevel(found->second.texture, found->second.mip_level);
184 }
static ISize SizeForMipLevel(const std::shared_ptr< Texture > &texture, uint32_t mip_level)

References impeller::SizeForMipLevel().

Referenced by GetRenderTargetSize(), and impeller::Surface::Surface().

◆ GetDepthAttachment()

const std::optional< DepthAttachment > & impeller::RenderTarget::GetDepthAttachment ( ) const

◆ GetMaxColorAttachmentBindIndex()

size_t impeller::RenderTarget::GetMaxColorAttachmentBindIndex ( ) const

Definition at line 206 of file render_target.cc.

206  {
207  size_t max = 0;
208  for (const auto& color : colors_) {
209  max = std::max(color.first, max);
210  }
211  return max;
212 }

◆ GetRenderTargetPixelFormat()

PixelFormat impeller::RenderTarget::GetRenderTargetPixelFormat ( ) const

Definition at line 198 of file render_target.cc.

198  {
199  if (auto texture = GetRenderTargetTexture(); texture != nullptr) {
200  return texture->GetTextureDescriptor().format;
201  }
202 
203  return PixelFormat::kUnknown;
204 }
std::shared_ptr< Texture > GetRenderTargetTexture() const

References GetRenderTargetTexture(), and impeller::kUnknown.

◆ GetRenderTargetSize()

ISize impeller::RenderTarget::GetRenderTargetSize ( ) const

Definition at line 186 of file render_target.cc.

186  {
187  auto size = GetColorAttachmentSize(0u);
188  return size.has_value() ? size.value() : ISize{};
189 }
std::optional< ISize > GetColorAttachmentSize(size_t index) const
ISize64 ISize
Definition: size.h:162

References GetColorAttachmentSize().

Referenced by impeller::Canvas::GetLocalCoverageLimit(), impeller::AiksPlayground::OpenPlaygroundHere(), impeller::DlPlayground::OpenPlaygroundHere(), and impeller::testing::TEST_P().

◆ GetRenderTargetTexture()

std::shared_ptr< Texture > impeller::RenderTarget::GetRenderTargetTexture ( ) const

Definition at line 191 of file render_target.cc.

191  {
192  if (!color0_.has_value()) {
193  return nullptr;
194  }
195  return color0_->resolve_texture ? color0_->resolve_texture : color0_->texture;
196 }

Referenced by impeller::DisplayListToTexture(), impeller::InlinePassContext::EndPass(), impeller::EntityPassTarget::Flip(), GetRenderTargetPixelFormat(), impeller::InlinePassContext::GetTexture(), impeller::ContentContext::MakeSubpass(), and impeller::GoldenPlaygroundTest::OpenPlaygroundHere().

◆ GetSampleCount()

SampleCount impeller::RenderTarget::GetSampleCount ( ) const

Definition at line 152 of file render_target.cc.

152  {
153  if (color0_.has_value()) {
154  return color0_.value().texture->GetTextureDescriptor().sample_count;
155  }
156  return SampleCount::kCount1;
157 }

References impeller::kCount1.

◆ GetStencilAttachment()

const std::optional< StencilAttachment > & impeller::RenderTarget::GetStencilAttachment ( ) const

◆ GetTotalAttachmentCount()

size_t impeller::RenderTarget::GetTotalAttachmentCount ( ) const

Definition at line 271 of file render_target.cc.

271  {
272  size_t count = 0u;
273  for (const auto& [_, color] : colors_) {
274  if (color.texture) {
275  count++;
276  }
277  if (color.resolve_texture) {
278  count++;
279  }
280  }
281  if (color0_.has_value()) {
282  count++;
283  }
284  if (depth_.has_value()) {
285  count++;
286  }
287  if (stencil_.has_value()) {
288  count++;
289  }
290  return count;
291 }

◆ HasColorAttachment()

bool impeller::RenderTarget::HasColorAttachment ( size_t  index) const

Definition at line 159 of file render_target.cc.

159  {
160  if (index == 0u) {
161  return color0_.has_value();
162  }
163  if (auto found = colors_.find(index); found != colors_.end()) {
164  return true;
165  }
166  return false;
167 }

Referenced by IsValid().

◆ IsValid()

bool impeller::RenderTarget::IsValid ( ) const

Definition at line 35 of file render_target.cc.

35  {
36  // Validate that there is a color attachment at zero index.
37  if (!HasColorAttachment(0u)) {
39  << "Render target does not have color attachment at index 0.";
40  return false;
41  }
42 
43 #ifndef NDEBUG
44  // Validate that all attachments are of the same size.
45  {
46  std::optional<ISize> size;
47  bool sizes_are_same = true;
48  auto iterator = [&](const Attachment& attachment) -> bool {
49  ISize attachment_size =
50  SizeForMipLevel(attachment.texture, attachment.mip_level);
51  if (!size.has_value()) {
52  size = attachment_size;
53  }
54  if (size != attachment_size) {
55  sizes_are_same = false;
56  return false;
57  }
58  return true;
59  };
60  IterateAllAttachments(iterator);
61  if (!sizes_are_same) {
63  << "Sizes of all render target attachments are not the same.";
64  return false;
65  }
66  }
67 
68  // Validate that all attachments are of the same type and sample counts.
69  {
70  std::optional<TextureType> texture_type;
71  std::optional<SampleCount> sample_count;
72  bool passes_type_validation = true;
73  auto iterator = [&](const Attachment& attachment) -> bool {
74  if (!texture_type.has_value() || !sample_count.has_value()) {
75  texture_type = attachment.texture->GetTextureDescriptor().type;
76  sample_count = attachment.texture->GetTextureDescriptor().sample_count;
77  }
78 
79  if (texture_type != attachment.texture->GetTextureDescriptor().type) {
80  passes_type_validation = false;
81  VALIDATION_LOG << "Render target has incompatible texture types: "
82  << TextureTypeToString(texture_type.value()) << " != "
84  attachment.texture->GetTextureDescriptor().type)
85  << " on target " << ToString();
86  return false;
87  }
88 
89  if (sample_count !=
90  attachment.texture->GetTextureDescriptor().sample_count) {
91  passes_type_validation = false;
92  VALIDATION_LOG << "Render target (" << ToString()
93  << ") has incompatible sample counts.";
94 
95  return false;
96  }
97 
98  return true;
99  };
100  IterateAllAttachments(iterator);
101  if (!passes_type_validation) {
102  return false;
103  }
104  }
105 #endif // NDEBUG
106 
107  return true;
108 }
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
bool HasColorAttachment(size_t index) const
std::string ToString() const
constexpr const char * TextureTypeToString(TextureType type)
Definition: formats.h:457
#define VALIDATION_LOG
Definition: validation.h:91

References HasColorAttachment(), IterateAllAttachments(), impeller::SizeForMipLevel(), impeller::TextureTypeToString(), ToString(), and VALIDATION_LOG.

Referenced by impeller::RenderTargetCache::CreateOffscreen(), impeller::RenderTargetCache::CreateOffscreenMSAA(), impeller::DisplayListToTexture(), impeller::EntityPassTarget::IsValid(), impeller::GoldenPlaygroundTest::OpenPlaygroundHere(), and impeller::testing::TEST_P().

◆ IterateAllAttachments()

void impeller::RenderTarget::IterateAllAttachments ( const std::function< bool(const Attachment &attachment)> &  iterator) const

Definition at line 126 of file render_target.cc.

127  {
128  if (color0_.has_value()) {
129  if (!iterator(color0_.value())) {
130  return;
131  }
132  }
133  for (const auto& color : colors_) {
134  if (!iterator(color.second)) {
135  return;
136  }
137  }
138 
139  if (depth_.has_value()) {
140  if (!iterator(depth_.value())) {
141  return;
142  }
143  }
144 
145  if (stencil_.has_value()) {
146  if (!iterator(stencil_.value())) {
147  return;
148  }
149  }
150 }

Referenced by IsValid().

◆ IterateAllColorAttachments()

bool impeller::RenderTarget::IterateAllColorAttachments ( const std::function< bool(size_t index, const ColorAttachment &attachment)> &  iterator) const

Definition at line 110 of file render_target.cc.

112  {
113  if (color0_.has_value()) {
114  if (!iterator(0, color0_.value())) {
115  return false;
116  }
117  }
118  for (const auto& [index, attachment] : colors_) {
119  if (!iterator(index, attachment)) {
120  return false;
121  }
122  }
123  return true;
124 }

Referenced by impeller::GetVKClearValues(), impeller::ContextVK::InitializeCommonlyUsedShadersIfNeeded(), and impeller::ToMTLRenderPassDescriptor().

◆ SetColorAttachment()

RenderTarget & impeller::RenderTarget::SetColorAttachment ( const ColorAttachment &  attachment,
size_t  index 
)

◆ SetDepthAttachment()

RenderTarget & impeller::RenderTarget::SetDepthAttachment ( std::optional< DepthAttachment >  attachment)

Definition at line 228 of file render_target.cc.

229  {
230  if (!attachment.has_value()) {
231  depth_ = std::nullopt;
232  } else if (attachment->IsValid()) {
233  depth_ = std::move(attachment);
234  }
235  return *this;
236 }

Referenced by impeller::RenderTargetAllocator::CreateOffscreen(), impeller::RenderTargetAllocator::CreateOffscreenMSAA(), impeller::InlinePassContext::GetRenderPass(), impeller::Playground::OpenPlaygroundHere(), SetupDepthStencilAttachments(), impeller::testing::TEST_P(), impeller::SurfaceGLES::WrapFBO(), and impeller::WrapTextureWithRenderTarget().

◆ SetStencilAttachment()

RenderTarget & impeller::RenderTarget::SetStencilAttachment ( std::optional< StencilAttachment >  attachment)

Definition at line 238 of file render_target.cc.

239  {
240  if (!attachment.has_value()) {
241  stencil_ = std::nullopt;
242  } else if (attachment->IsValid()) {
243  stencil_ = std::move(attachment);
244  }
245  return *this;
246 }

Referenced by impeller::RenderTargetAllocator::CreateOffscreen(), impeller::RenderTargetAllocator::CreateOffscreenMSAA(), impeller::InlinePassContext::GetRenderPass(), impeller::Playground::OpenPlaygroundHere(), SetupDepthStencilAttachments(), impeller::testing::TEST_P(), impeller::SurfaceGLES::WrapFBO(), and impeller::WrapTextureWithRenderTarget().

◆ SetupDepthStencilAttachments()

void impeller::RenderTarget::SetupDepthStencilAttachments ( const Context &  context,
Allocator &  allocator,
ISize  size,
bool  msaa,
std::string_view  label = "Offscreen",
RenderTarget::AttachmentConfig  stencil_attachment_config = RenderTarget::kDefaultStencilAttachmentConfig,
const std::shared_ptr< Texture > &  depth_stencil_texture = nullptr 
)

Definition at line 496 of file render_target.cc.

503  {
504  std::shared_ptr<Texture> depth_stencil_texture;
505  if (existing_depth_stencil_texture) {
506  depth_stencil_texture = existing_depth_stencil_texture;
507  } else {
508  TextureDescriptor depth_stencil_texture_desc;
509  depth_stencil_texture_desc.storage_mode =
510  stencil_attachment_config.storage_mode;
511  if (msaa) {
512  depth_stencil_texture_desc.type = TextureType::kTexture2DMultisample;
513  depth_stencil_texture_desc.sample_count = SampleCount::kCount4;
514  }
515  depth_stencil_texture_desc.format =
516  context.GetCapabilities()->GetDefaultDepthStencilFormat();
517  depth_stencil_texture_desc.size = size;
518  depth_stencil_texture_desc.usage = TextureUsage::kRenderTarget;
519  depth_stencil_texture = allocator.CreateTexture(depth_stencil_texture_desc);
520  if (!depth_stencil_texture) {
521  return; // Error messages are handled by `Allocator::CreateTexture`.
522  }
523  }
524 
525  DepthAttachment depth0;
526  depth0.load_action = stencil_attachment_config.load_action;
527  depth0.store_action = stencil_attachment_config.store_action;
528  depth0.clear_depth = 0u;
529  depth0.texture = depth_stencil_texture;
530 
531  StencilAttachment stencil0;
532  stencil0.load_action = stencil_attachment_config.load_action;
533  stencil0.store_action = stencil_attachment_config.store_action;
534  stencil0.clear_stencil = 0u;
535  stencil0.texture = std::move(depth_stencil_texture);
536  stencil0.texture->SetLabel(label, "Depth+Stencil Texture");
537 
538  SetDepthAttachment(std::move(depth0));
539  SetStencilAttachment(std::move(stencil0));
540 }
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
std::shared_ptr< ContextGLES > context

References impeller::DepthAttachment::clear_depth, impeller::StencilAttachment::clear_stencil, context, impeller::Allocator::CreateTexture(), impeller::TextureDescriptor::format, impeller::kCount4, impeller::kRenderTarget, impeller::kTexture2DMultisample, impeller::Attachment::load_action, impeller::RenderTarget::AttachmentConfig::load_action, impeller::TextureDescriptor::sample_count, SetDepthAttachment(), SetStencilAttachment(), impeller::TextureDescriptor::size, impeller::TextureDescriptor::storage_mode, impeller::RenderTarget::AttachmentConfig::storage_mode, impeller::Attachment::store_action, impeller::RenderTarget::AttachmentConfig::store_action, impeller::Attachment::texture, impeller::TextureDescriptor::type, and impeller::TextureDescriptor::usage.

Referenced by impeller::RenderTargetAllocator::CreateOffscreen(), impeller::RenderTargetAllocator::CreateOffscreenMSAA(), and impeller::SurfaceVK::WrapSwapchainImage().

◆ ToConfig()

RenderTargetConfig impeller::RenderTarget::ToConfig ( ) const

Definition at line 317 of file render_target.cc.

317  {
318  if (!color0_.has_value()) {
319  return RenderTargetConfig{};
320  }
321  const auto& color_attachment = color0_.value();
322  return RenderTargetConfig{
323  .size = color_attachment.texture->GetSize(),
324  .mip_count = color_attachment.texture->GetMipCount(),
325  .has_msaa = color_attachment.resolve_texture != nullptr,
326  .has_depth_stencil = depth_.has_value() && stencil_.has_value()};
327 }

References impeller::RenderTargetConfig::size.

◆ ToString()

std::string impeller::RenderTarget::ToString ( ) const

Definition at line 293 of file render_target.cc.

293  {
294  std::stringstream stream;
295 
296  if (color0_.has_value()) {
297  stream << std::format("Color[{}]=({})", 0,
298  ColorAttachmentToString(color0_.value()));
299  }
300  for (const auto& [index, color] : colors_) {
301  stream << std::format("Color[{}]=({})", index,
302  ColorAttachmentToString(color));
303  }
304  if (depth_) {
305  stream << ",";
306  stream << std::format("Depth=({})",
307  DepthAttachmentToString(depth_.value()));
308  }
309  if (stencil_) {
310  stream << ",";
311  stream << std::format("Stencil=({})",
312  StencilAttachmentToString(stencil_.value()));
313  }
314  return stream.str();
315 }
std::string DepthAttachmentToString(const DepthAttachment &depth)
Definition: formats.cc:144
std::string ColorAttachmentToString(const ColorAttachment &color)
Definition: formats.cc:137
std::string StencilAttachmentToString(const StencilAttachment &stencil)
Definition: formats.cc:151

References impeller::ColorAttachmentToString(), impeller::DepthAttachmentToString(), and impeller::StencilAttachmentToString().

Referenced by IsValid().

Member Data Documentation

◆ kDefaultColorAttachmentConfig

constexpr AttachmentConfig impeller::RenderTarget::kDefaultColorAttachmentConfig
staticconstexpr

◆ kDefaultColorAttachmentConfigMSAA

constexpr AttachmentConfigMSAA impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
staticconstexpr
Initial value:
= {
.resolve_storage_mode = StorageMode::kDevicePrivate,
.load_action = LoadAction::kClear,
.clear_color = Color::BlackTransparent()}

Definition at line 61 of file render_target.h.

Referenced by impeller::ContentContext::MakeSubpass().

◆ kDefaultStencilAttachmentConfig

constexpr AttachmentConfig impeller::RenderTarget::kDefaultStencilAttachmentConfig
staticconstexpr

The documentation for this class was generated from the following files: