| Flutter Impeller |

Classes | Public Member Functions | Static Public Attributes | List of all members
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 | GetMaxColorAttacmentBindIndex () 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 233 of file render_target.cc.

233 {
234 if (index == 0) {
235 if (color0_.has_value()) {
236 return color0_.value();
237 }
238 return ColorAttachment{};
239 }
240 std::map<size_t, ColorAttachment>::const_iterator it = colors_.find(index);
241 if (it != colors_.end()) {
242 return it->second;
243 }
244 return ColorAttachment{};
245 }

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 155 of file render_target.cc.

155 {
156 if (index == 0u) {
157 if (color0_.has_value()) {
158 return color0_.value().texture->GetSize();
159 }
160 return std::nullopt;
161 }
162 auto found = colors_.find(index);
163
164 if (found == colors_.end()) {
165 return std::nullopt;
166 }
167
168 return found->second.texture->GetSize();
169 }

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

## ◆ GetDepthAttachment()

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

Definition at line 247 of file render_target.cc.

247 {
248 return depth_;
249 }

Referenced by impeller::InlinePassContext::GetRenderPass(), impeller::GetVKClearValues(), impeller::ContextVK::InitializeCommonlyUsedShadersIfNeeded(), impeller::testing::TEST_P(), and impeller::ToMTLRenderPassDescriptor().

## ◆ GetMaxColorAttacmentBindIndex()

size_t impeller::RenderTarget::GetMaxColorAttacmentBindIndex | ( |  | ) | const

Definition at line 191 of file render_target.cc.

191 {
192 size_t max = 0;
193 for (const auto& color : colors_) {
194 max = std::max(color.first, max);
195 }
196 return max;
197 }

## ◆ GetRenderTargetPixelFormat()

PixelFormat impeller::RenderTarget::GetRenderTargetPixelFormat | ( |  | ) | const

Definition at line 183 of file render_target.cc.

183 {
184 if (auto texture = GetRenderTargetTexture(); texture != nullptr) {
185 return texture->GetTextureDescriptor().format;
186 }
187
188 return PixelFormat::kUnknown;
189 }
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:176
impeller::PixelFormat::kUnknown
@ kUnknown

References GetRenderTargetTexture(), and impeller::kUnknown.

## ◆ GetRenderTargetSize()

ISize impeller::RenderTarget::GetRenderTargetSize | ( |  | ) | const

Definition at line 171 of file render_target.cc.

171 {
172 auto size = GetColorAttachmentSize(0u);
173 return size.has_value() ? size.value() : ISize{};
174 }
impeller::RenderTarget::GetColorAttachmentSize
std::optional< ISize > GetColorAttachmentSize(size_t index) const
Definition: render_target.cc:155
impeller::ISize
ISize64 ISize
Definition: size.h:162

References GetColorAttachmentSize().

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

## ◆ GetRenderTargetTexture()

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

Definition at line 176 of file render_target.cc.

176 {
177 if (!color0_.has_value()) {
178 return nullptr;
179 }
180 return color0_->resolve_texture ? color0_->resolve_texture : color0_->texture;
181 }

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

## ◆ GetSampleCount()

SampleCount impeller::RenderTarget::GetSampleCount | ( |  | ) | const

Definition at line 138 of file render_target.cc.

138 {
139 if (color0_.has_value()) {
140 return color0_.value().texture->GetTextureDescriptor().sample_count;
141 }
142 return SampleCount::kCount1;
143 }
impeller::SampleCount::kCount1
@ kCount1

References impeller::kCount1.

## ◆ GetStencilAttachment()

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

Definition at line 251 of file render_target.cc.

252 {
253 return stencil_;
254 }

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

## ◆ GetTotalAttachmentCount()

size_t impeller::RenderTarget::GetTotalAttachmentCount | ( |  | ) | const

Definition at line 256 of file render_target.cc.

256 {
257 size_t count = 0u;
258 for (const auto& [_, color] : colors_) {
259 if (color.texture) {
260 count++;
261 }
262 if (color.resolve_texture) {
263 count++;
264 }
265 }
266 if (color0_.has_value()) {
267 count++;
268 }
269 if (depth_.has_value()) {
270 count++;
271 }
272 if (stencil_.has_value()) {
273 count++;
274 }
275 return count;
276 }

## ◆ HasColorAttachment()

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

Definition at line 145 of file render_target.cc.

145 {
146 if (index == 0u) {
147 return color0_.has_value();
148 }
149 if (auto found = colors_.find(index); found != colors_.end()) {
150 return true;
151 }
152 return false;
153 }

Referenced by IsValid().

## ◆ IsValid()

bool impeller::RenderTarget::IsValid | ( |  | ) | const

Definition at line 23 of file render_target.cc.

23 {
24 // Validate that there is a color attachment at zero index.
25 if (!HasColorAttachment(0u)) {
26 VALIDATION_LOG
27 << "Render target does not have color attachment at index 0.";
28 return false;
29 }
30
31 #ifndef NDEBUG
32 // Validate that all attachments are of the same size.
33 {
34 std::optional<ISize> size;
35 bool sizes_are_same = true;
36 auto iterator = [&](const Attachment& attachment) -> bool {
37 if (!size.has_value()) {
38 size = attachment.texture->GetSize();
39 }
40 if (size != attachment.texture->GetSize()) {
41 sizes_are_same = false;
42 return false;
43 }
44 return true;
45 };
46 IterateAllAttachments(iterator);
47 if (!sizes_are_same) {
48 VALIDATION_LOG
49 << "Sizes of all render target attachments are not the same.";
50 return false;
51 }
52 }
53
54 // Validate that all attachments are of the same type and sample counts.
55 {
56 std::optional<TextureType> texture_type;
57 std::optional<SampleCount> sample_count;
58 bool passes_type_validation = true;
59 auto iterator = [&](const Attachment& attachment) -> bool {
60 if (!texture_type.has_value() || !sample_count.has_value()) {
61 texture_type = attachment.texture->GetTextureDescriptor().type;
62 sample_count = attachment.texture->GetTextureDescriptor().sample_count;
63 }
64
65 if (texture_type != attachment.texture->GetTextureDescriptor().type) {
66 passes_type_validation = false;
67 VALIDATION_LOG << "Render target has incompatible texture types: "
68 << TextureTypeToString(texture_type.value()) << " != "
69 << TextureTypeToString(
70 attachment.texture->GetTextureDescriptor().type)
71 << " on target " << ToString();
72 return false;
73 }
74
75 if (sample_count !=
76 attachment.texture->GetTextureDescriptor().sample_count) {
77 passes_type_validation = false;
78 VALIDATION_LOG << "Render target (" << ToString()
79 << ") has incompatible sample counts.";
80
81 return false;
82 }
83
84 return true;
85 };
86 IterateAllAttachments(iterator);
87 if (!passes_type_validation) {
88 return false;
89 }
90 }
91 #endif // NDEBUG
92
93 return true;
94 }
impeller::RenderTarget::IterateAllAttachments
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
Definition: render_target.cc:112
impeller::RenderTarget::HasColorAttachment
bool HasColorAttachment(size_t index) const
Definition: render_target.cc:145
impeller::RenderTarget::ToString
std::string ToString() const
Definition: render_target.cc:278
impeller::TextureTypeToString
constexpr const char * TextureTypeToString(TextureType type)
Definition: formats.h:269
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91

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

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

## ◆ IterateAllAttachments()

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

Definition at line 112 of file render_target.cc.

113 {
114 if (color0_.has_value()) {
115 if (!iterator(color0_.value())) {
116 return;
117 }
118 }
119 for (const auto& color : colors_) {
120 if (!iterator(color.second)) {
121 return;
122 }
123 }
124
125 if (depth_.has_value()) {
126 if (!iterator(depth_.value())) {
127 return;
128 }
129 }
130
131 if (stencil_.has_value()) {
132 if (!iterator(stencil_.value())) {
133 return;
134 }
135 }
136 }

Referenced by IsValid().

## ◆ IterateAllColorAttachments()

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

Definition at line 96 of file render_target.cc.

98 {
99 if (color0_.has_value()) {
100 if (!iterator(0, color0_.value())) {
101 return false;
102 }
103 }
104 for (const auto& [index, attachment] : colors_) {
105 if (!iterator(index, attachment)) {
106 return false;
107 }
108 }
109 return true;
110 }

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

## ◆ SetColorAttachment()

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

Definition at line 199 of file render_target.cc.

201 {
202 if (!attachment.IsValid()) {
203 return *this;
204 }
205 if (index == 0u) {
206 color0_ = attachment;
207 } else {
208 colors_[index] = attachment;
209 }
210 return *this;
211 }

References impeller::Attachment::IsValid().

Referenced by impeller::RenderTargetAllocator::CreateOffscreen(), impeller::RenderTargetAllocator::CreateOffscreenMSAA(), impeller::testing::CreateTestCanvas(), impeller::EntityPassTarget::Flip(), impeller::InlinePassContext::GetRenderPass(), impeller::Playground::OpenPlaygroundHere(), impeller::testing::TEST_P(), impeller::SurfaceGLES::WrapFBO(), impeller::SurfaceVK::WrapSwapchainImage(), and impeller::WrapTextureWithRenderTarget().

## ◆ SetDepthAttachment()

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

Definition at line 213 of file render_target.cc.

214 {
215 if (!attachment.has_value()) {
216 depth_ = std::nullopt;
217 } else if (attachment->IsValid()) {
218 depth_ = std::move(attachment);
219 }
220 return *this;
221 }

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 223 of file render_target.cc.

224 {
225 if (!attachment.has_value()) {
226 stencil_ = std::nullopt;
227 } else if (attachment->IsValid()) {
228 stencil_ = std::move(attachment);
229 }
230 return *this;
231 }

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 475 of file render_target.cc.

482 {
483 std::shared_ptr<Texture> depth_stencil_texture;
484 if (existing_depth_stencil_texture) {
485 depth_stencil_texture = existing_depth_stencil_texture;
486 } else {
487 TextureDescriptor depth_stencil_texture_desc;
488 depth_stencil_texture_desc.storage_mode =
489 stencil_attachment_config.storage_mode;
490 if (msaa) {
491 depth_stencil_texture_desc.type = TextureType::kTexture2DMultisample;
492 depth_stencil_texture_desc.sample_count = SampleCount::kCount4;
493 }
494 depth_stencil_texture_desc.format =
495 context.GetCapabilities()->GetDefaultDepthStencilFormat();
496 depth_stencil_texture_desc.size = size;
497 depth_stencil_texture_desc.usage = TextureUsage::kRenderTarget;
498 depth_stencil_texture = allocator.CreateTexture(depth_stencil_texture_desc);
499 if (!depth_stencil_texture) {
500 return; // Error messages are handled by `Allocator::CreateTexture`.
501 }
502 }
503
504 DepthAttachment depth0;
505 depth0.load_action = stencil_attachment_config.load_action;
506 depth0.store_action = stencil_attachment_config.store_action;
507 depth0.clear_depth = 0u;
508 depth0.texture = depth_stencil_texture;
509
510 StencilAttachment stencil0;
511 stencil0.load_action = stencil_attachment_config.load_action;
512 stencil0.store_action = stencil_attachment_config.store_action;
513 stencil0.clear_stencil = 0u;
514 stencil0.texture = std::move(depth_stencil_texture);
515 stencil0.texture->SetLabel(label, "Depth+Stencil Texture");
516
517 SetDepthAttachment(std::move(depth0));
518 SetStencilAttachment(std::move(stencil0));
519 }
impeller::RenderTarget::SetDepthAttachment
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
Definition: render_target.cc:213
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:223
impeller::TextureType::kTexture2DMultisample
@ kTexture2DMultisample
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::SampleCount::kCount4
@ kCount4

References impeller::DepthAttachment::clear_depth, impeller::StencilAttachment::clear_stencil, impeller::Allocator::CreateTexture(), impeller::TextureDescriptor::format, impeller::Context::GetCapabilities(), 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 302 of file render_target.cc.

302 {
303 if (!color0_.has_value()) {
304 return RenderTargetConfig{};
305 }
306 const auto& color_attachment = color0_.value();
307 return RenderTargetConfig{
308 .size = color_attachment.texture->GetSize(),
309 .mip_count = color_attachment.texture->GetMipCount(),
310 .has_msaa = color_attachment.resolve_texture != nullptr,
311 .has_depth_stencil = depth_.has_value() && stencil_.has_value()};
312 }

References impeller::RenderTargetConfig::size.

## ◆ ToString()

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

Definition at line 278 of file render_target.cc.

278 {
279 std::stringstream stream;
280
281 if (color0_.has_value()) {
282 stream << SPrintF("Color[%d]=(%s)", 0,
283 ColorAttachmentToString(color0_.value()).c_str());
284 }
285 for (const auto& [index, color] : colors_) {
286 stream << SPrintF("Color[%zu]=(%s)", index,
287 ColorAttachmentToString(color).c_str());
288 }
289 if (depth_) {
290 stream << ",";
291 stream << SPrintF("Depth=(%s)",
292 DepthAttachmentToString(depth_.value()).c_str());
293 }
294 if (stencil_) {
295 stream << ",";
296 stream << SPrintF("Stencil=(%s)",
297 StencilAttachmentToString(stencil_.value()).c_str());
298 }
299 return stream.str();
300 }
impeller::DepthAttachmentToString
std::string DepthAttachmentToString(const DepthAttachment &depth)
Definition: formats.cc:130
impeller::ColorAttachmentToString
std::string ColorAttachmentToString(const ColorAttachment &color)
Definition: formats.cc:123
impeller::StencilAttachmentToString
std::string StencilAttachmentToString(const StencilAttachment &stencil)
Definition: formats.cc:137
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12

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

Referenced by IsValid().

## Member Data Documentation

## ◆ kDefaultColorAttachmentConfig

constexpr AttachmentConfig impeller::RenderTarget::kDefaultColorAttachmentConfig
| staticconstexpr

Initial value:
= {
.storage_mode = StorageMode::kDevicePrivate,
.load_action = LoadAction::kClear,
.store_action = StoreAction::kStore,
.clear_color = Color::BlackTransparent()}
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::LoadAction::kClear
@ kClear
impeller::StoreAction::kStore
@ kStore
impeller::Color::BlackTransparent
static constexpr Color BlackTransparent()
Definition: color.h:270

Definition at line 55 of file render_target.h.

Referenced by impeller::ContentContext::MakeSubpass(), and impeller::testing::TEST_P().

## ◆ kDefaultColorAttachmentConfigMSAA

constexpr AttachmentConfigMSAA impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
| staticconstexpr

Initial value:
= {
.storage_mode = StorageMode::kDeviceTransient,
.resolve_storage_mode = StorageMode::kDevicePrivate,
.load_action = LoadAction::kClear,
.store_action = StoreAction::kMultisampleResolve,
.clear_color = Color::BlackTransparent()}
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve

Definition at line 61 of file render_target.h.

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

## ◆ kDefaultStencilAttachmentConfig

constexpr AttachmentConfig impeller::RenderTarget::kDefaultStencilAttachmentConfig
| staticconstexpr

Initial value:
= {
.storage_mode = StorageMode::kDeviceTransient,
.load_action = LoadAction::kClear,
.store_action = StoreAction::kDontCare,
.clear_color = Color::BlackTransparent()}
impeller::StoreAction::kDontCare
@ kDontCare

Definition at line 68 of file render_target.h.

Referenced by impeller::ContentContext::MakeSubpass(), impeller::SurfaceVK::WrapSwapchainImage(), and impeller::WrapTextureWithRenderTarget().

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

- impeller/renderer/render_target.h
- impeller/renderer/render_target.cc