| Flutter Impeller |

render_target.h
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
5 #ifndef FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
6 #define FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
7
8 #include <functional>
9 #include <map>
10 #include <optional>
11
12 #include "flutter/fml/hash_combine.h"
13 #include "impeller/core/allocator.h"
14 #include "impeller/core/formats.h"
15 #include "impeller/geometry/size.h"
16
17 namespace impeller {
18
19 class Context;
20
21 struct RenderTargetConfig {
22 ISize size = ISize{0, 0};
23 size_t mip_count = 0;
24 bool has_msaa = false;
25 bool has_depth_stencil = false;
26
27 constexpr bool operator==(const RenderTargetConfig& o) const {
28 return size == o.size && mip_count == o.mip_count &&
29 has_msaa == o.has_msaa && has_depth_stencil == o.has_depth_stencil;
30 }
31
32 constexpr size_t Hash() const {
33 return fml::HashCombine(size.width, size.height, mip_count, has_msaa,
34 has_depth_stencil);
35 }
36 };
37
38 class RenderTarget final {
39 public:
40 struct AttachmentConfig {
41 StorageMode storage_mode;
42 LoadAction load_action;
43 StoreAction store_action;
44 Color clear_color;
45 };
46
47 struct AttachmentConfigMSAA {
48 StorageMode storage_mode;
49 StorageMode resolve_storage_mode;
50 LoadAction load_action;
51 StoreAction store_action;
52 Color clear_color;
53 };
54
55 static constexpr AttachmentConfig kDefaultColorAttachmentConfig = {
56 .storage_mode = StorageMode::kDevicePrivate,
57 .load_action = LoadAction::kClear,
58 .store_action = StoreAction::kStore,
59 .clear_color = Color::BlackTransparent()};
60
61 static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA = {
62 .storage_mode = StorageMode::kDeviceTransient,
63 .resolve_storage_mode = StorageMode::kDevicePrivate,
64 .load_action = LoadAction::kClear,
65 .store_action = StoreAction::kMultisampleResolve,
66 .clear_color = Color::BlackTransparent()};
67
68 static constexpr AttachmentConfig kDefaultStencilAttachmentConfig = {
69 .storage_mode = StorageMode::kDeviceTransient,
70 .load_action = LoadAction::kClear,
71 .store_action = StoreAction::kDontCare,
72 .clear_color = Color::BlackTransparent()};
73
74 RenderTarget();
75
76 ~RenderTarget();
77
78 bool IsValid() const;
79
80 void SetupDepthStencilAttachments(
81 const Context& context,
82 Allocator& allocator,
83 ISize size,
84 bool msaa,
85 const std::string& label = "Offscreen",
86 RenderTarget::AttachmentConfig stencil_attachment_config =
87 RenderTarget::kDefaultStencilAttachmentConfig,
88 const std::shared_ptr<Texture>& depth_stencil_texture = nullptr);
89
90 SampleCount GetSampleCount() const;
91
92 bool HasColorAttachment(size_t index) const;
93
94 ISize GetRenderTargetSize() const;
95
96 std::shared_ptr<Texture> GetRenderTargetTexture() const;
97
98 PixelFormat GetRenderTargetPixelFormat() const;
99
100 std::optional<ISize> GetColorAttachmentSize(size_t index) const;
101
102 RenderTarget& SetColorAttachment(const ColorAttachment& attachment,
103 size_t index);
104
105 RenderTarget& SetDepthAttachment(std::optional<DepthAttachment> attachment);
106
107 RenderTarget& SetStencilAttachment(
108 std::optional<StencilAttachment> attachment);
109
110 size_t GetMaxColorAttacmentBindIndex() const;
111
112 const std::map<size_t, ColorAttachment>& GetColorAttachments() const;
113
114 const std::optional<DepthAttachment>& GetDepthAttachment() const;
115
116 const std::optional<StencilAttachment>& GetStencilAttachment() const;
117
118 size_t GetTotalAttachmentCount() const;
119
120 void IterateAllAttachments(
121 const std::function<bool(const Attachment& attachment)>& iterator) const;
122
123 std::string ToString() const;
124
125 RenderTargetConfig ToConfig() const {
126 auto& color_attachment = GetColorAttachments().find(0)->second;
127 return RenderTargetConfig{
128 .size = color_attachment.texture->GetSize(),
129 .mip_count = color_attachment.texture->GetMipCount(),
130 .has_msaa = color_attachment.resolve_texture != nullptr,
131 .has_depth_stencil = depth_.has_value() && stencil_.has_value()};
132 }
133
134 private:
135 std::map<size_t, ColorAttachment> colors_;
136 std::optional<DepthAttachment> depth_;
137 std::optional<StencilAttachment> stencil_;
138 };
139
140 /// @brief a wrapper around the impeller [Allocator] instance that can be used
141 /// to provide caching of allocated render target textures.
142 class RenderTargetAllocator {
143 public:
144 explicit RenderTargetAllocator(std::shared_ptr<Allocator> allocator);
145
146 virtual ~RenderTargetAllocator() = default;
147
148 virtual RenderTarget CreateOffscreen(
149 const Context& context,
150 ISize size,
151 int mip_count,
152 const std::string& label = "Offscreen",
153 RenderTarget::AttachmentConfig color_attachment_config =
154 RenderTarget::kDefaultColorAttachmentConfig,
155 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
156 RenderTarget::kDefaultStencilAttachmentConfig,
157 const std::shared_ptr<Texture>& existing_color_texture = nullptr,
158 const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr);
159
160 virtual RenderTarget CreateOffscreenMSAA(
161 const Context& context,
162 ISize size,
163 int mip_count,
164 const std::string& label = "Offscreen MSAA",
165 RenderTarget::AttachmentConfigMSAA color_attachment_config =
166 RenderTarget::kDefaultColorAttachmentConfigMSAA,
167 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
168 RenderTarget::kDefaultStencilAttachmentConfig,
169 const std::shared_ptr<Texture>& existing_color_msaa_texture = nullptr,
170 const std::shared_ptr<Texture>& existing_color_resolve_texture = nullptr,
171 const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr);
172
173 /// @brief Mark the beginning of a frame workload.
174 ///
175 /// This may be used to reset any tracking state on whether or not a
176 /// particular texture instance is still in use.
177 virtual void Start();
178
179 /// @brief Mark the end of a frame workload.
180 ///
181 /// This may be used to deallocate any unused textures.
182 virtual void End();
183
184 private:
185 std::shared_ptr<Allocator> allocator_;
186 };
187
188 } // namespace impeller
189
190 #endif // FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::RenderTarget::GetTotalAttachmentCount
size_t GetTotalAttachmentCount() const
Definition: render_target.cc:212
impeller::RenderTarget::AttachmentConfig::store_action
StoreAction store_action
Definition: render_target.h:43
impeller::RenderTargetAllocator::CreateOffscreen
virtual RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, const std::string &label="Offscreen", RenderTarget::AttachmentConfig color_attachment_config=RenderTarget::kDefaultColorAttachmentConfig, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr)
Definition: render_target.cc:259
impeller::StoreAction
StoreAction
Definition: formats.h:208
impeller::RenderTarget::AttachmentConfig::load_action
LoadAction load_action
Definition: render_target.h:42
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
impeller::RenderTargetAllocator::Start
virtual void Start()
Mark the beginning of a frame workload.
Definition: render_target.cc:255
impeller::Color
Definition: color.h:123
impeller::ColorAttachment
Definition: formats.h:665
impeller::RenderTarget::SetColorAttachment
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
Definition: render_target.cc:169
impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
Definition: render_target.h:61
impeller::RenderTarget::AttachmentConfigMSAA::store_action
StoreAction store_action
Definition: render_target.h:51
impeller::RenderTargetConfig::Hash
constexpr size_t Hash() const
Definition: render_target.h:32
impeller::RenderTarget::ToConfig
RenderTargetConfig ToConfig() const
Definition: render_target.h:125
formats.h
impeller::StoreAction::kDontCare
@ kDontCare
impeller::RenderTarget::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
Definition: render_target.cc:153
impeller::RenderTarget::GetColorAttachmentSize
std::optional< ISize > GetColorAttachmentSize(size_t index) const
Definition: render_target.cc:129
impeller::RenderTargetAllocator::RenderTargetAllocator
RenderTargetAllocator(std::shared_ptr< Allocator > allocator)
Definition: render_target.cc:251
impeller::RenderTarget::GetColorAttachments
const std::map< size_t, ColorAttachment > & GetColorAttachments() const
Definition: render_target.cc:198
impeller::RenderTarget::AttachmentConfigMSAA::load_action
LoadAction load_action
Definition: render_target.h:50
impeller::RenderTarget::AttachmentConfigMSAA::resolve_storage_mode
StorageMode resolve_storage_mode
Definition: render_target.h:49
impeller::RenderTarget::AttachmentConfigMSAA
Definition: render_target.h:47
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:40
impeller::RenderTargetConfig::has_depth_stencil
bool has_depth_stencil
Definition: render_target.h:25
impeller::TSize
Definition: size.h:19
impeller::LoadAction::kClear
@ kClear
impeller::RenderTarget::GetDepthAttachment
const std::optional< DepthAttachment > & GetDepthAttachment() const
Definition: render_target.cc:203
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::StorageMode
StorageMode
Specified where the allocation resides and how it is used.
Definition: formats.h:32
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:144
impeller::RenderTarget::IterateAllAttachments
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
Definition: render_target.cc:94
impeller::RenderTarget::~RenderTarget
~RenderTarget()
impeller::LoadAction
LoadAction
Definition: formats.h:202
impeller::RenderTargetConfig::has_msaa
bool has_msaa
Definition: render_target.h:24
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::RenderTargetConfig::size
ISize size
Definition: render_target.h:22
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:23
impeller::RenderTarget::AttachmentConfig::storage_mode
StorageMode storage_mode
Definition: render_target.h:41
impeller::Attachment
Definition: formats.h:656
impeller::RenderTarget
Definition: render_target.h:38
impeller::StoreAction::kStore
@ kStore
impeller::RenderTargetConfig::mip_count
size_t mip_count
Definition: render_target.h:23
impeller::RenderTarget::IsValid
bool IsValid() const
Definition: render_target.cc:23
impeller::RenderTargetAllocator::CreateOffscreenMSAA
virtual RenderTarget CreateOffscreenMSAA(const Context &context, ISize size, int mip_count, const std::string &label="Offscreen MSAA", RenderTarget::AttachmentConfigMSAA color_attachment_config=RenderTarget::kDefaultColorAttachmentConfigMSAA, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_msaa_texture=nullptr, const std::shared_ptr< Texture > &existing_color_resolve_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr)
Definition: render_target.cc:313
impeller::RenderTargetAllocator
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
Definition: render_target.h:142
impeller::RenderTargetConfig::operator==
constexpr bool operator==(const RenderTargetConfig &o) const
Definition: render_target.h:27
impeller::TSize::width
Type width
Definition: size.h:22
impeller::RenderTarget::AttachmentConfig::clear_color
Color clear_color
Definition: render_target.h:44
impeller::RenderTarget::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_target.cc:139
allocator.h
impeller::RenderTarget::AttachmentConfigMSAA::storage_mode
StorageMode storage_mode
Definition: render_target.h:48
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:188
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
impeller::RenderTargetAllocator::End
virtual void End()
Mark the end of a frame workload.
Definition: render_target.cc:257
impeller::Color::BlackTransparent
static constexpr Color BlackTransparent()
Definition: color.h:269
impeller::SampleCount
SampleCount
Definition: formats.h:295
impeller::RenderTarget::GetMaxColorAttacmentBindIndex
size_t GetMaxColorAttacmentBindIndex() const
Definition: render_target.cc:161
impeller::TSize::height
Type height
Definition: size.h:23
impeller::RenderTargetConfig
Definition: render_target.h:21
impeller::RenderTarget::GetStencilAttachment
const std::optional< StencilAttachment > & GetStencilAttachment() const
Definition: render_target.cc:207
impeller::RenderTarget::SetDepthAttachment
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
Definition: render_target.cc:178
impeller::RenderTarget::SetupDepthStencilAttachments
void SetupDepthStencilAttachments(const Context &context, Allocator &allocator, ISize size, bool msaa, const std::string &label="Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &depth_stencil_texture=nullptr)
Definition: render_target.cc:413
impeller::RenderTarget::GetSampleCount
SampleCount GetSampleCount() const
Definition: render_target.cc:115
impeller::RenderTarget::HasColorAttachment
bool HasColorAttachment(size_t index) const
Definition: render_target.cc:122
impeller
Definition: allocation.cc:12
impeller::RenderTarget::ToString
std::string ToString() const
Definition: render_target.cc:231
impeller::RenderTargetAllocator::~RenderTargetAllocator
virtual ~RenderTargetAllocator()=default
size.h
impeller::RenderTarget::AttachmentConfigMSAA::clear_color
Color clear_color
Definition: render_target.h:52
impeller::RenderTarget::RenderTarget
RenderTarget()