| 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 std::string_view 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 /// @brief Get the color attachment at [index].
106 ///
107 /// This function does not validate whether or not the attachment was
108 /// previously defined and will return a default constructed attachment
109 /// if none is set.
110 ColorAttachment GetColorAttachment(size_t index) const;
111
112 RenderTarget& SetDepthAttachment(std::optional<DepthAttachment> attachment);
113
114 RenderTarget& SetStencilAttachment(
115 std::optional<StencilAttachment> attachment);
116
117 size_t GetMaxColorAttacmentBindIndex() const;
118
119 const std::optional<DepthAttachment>& GetDepthAttachment() const;
120
121 const std::optional<StencilAttachment>& GetStencilAttachment() const;
122
123 size_t GetTotalAttachmentCount() const;
124
125 bool IterateAllColorAttachments(
126 const std::function<bool(size_t index,
127 const ColorAttachment& attachment)>& iterator)
128 const;
129
130 void IterateAllAttachments(
131 const std::function<bool(const Attachment& attachment)>& iterator) const;
132
133 std::string ToString() const;
134
135 RenderTargetConfig ToConfig() const;
136
137 private:
138 std::optional<ColorAttachment> color0_;
139 std::optional<DepthAttachment> depth_;
140 std::optional<StencilAttachment> stencil_;
141 // Note: color0 is stored in the color0_ field above and not in this map,
142 // to avoid heap allocations for the commonly created render target formats
143 // in Flutter.
144 std::map<size_t, ColorAttachment> colors_;
145 };
146
147 /// @brief a wrapper around the impeller [Allocator] instance that can be used
148 /// to provide caching of allocated render target textures.
149 class RenderTargetAllocator {
150 public:
151 explicit RenderTargetAllocator(std::shared_ptr<Allocator> allocator);
152
153 virtual ~RenderTargetAllocator() = default;
154
155 virtual RenderTarget CreateOffscreen(
156 const Context& context,
157 ISize size,
158 int mip_count,
159 std::string_view label = "Offscreen",
160 RenderTarget::AttachmentConfig color_attachment_config =
161 RenderTarget::kDefaultColorAttachmentConfig,
162 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
163 RenderTarget::kDefaultStencilAttachmentConfig,
164 const std::shared_ptr<Texture>& existing_color_texture = nullptr,
165 const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr,
166 std::optional<PixelFormat> target_pixel_format = std::nullopt);
167
168 virtual RenderTarget CreateOffscreenMSAA(
169 const Context& context,
170 ISize size,
171 int mip_count,
172 std::string_view label = "Offscreen MSAA",
173 RenderTarget::AttachmentConfigMSAA color_attachment_config =
174 RenderTarget::kDefaultColorAttachmentConfigMSAA,
175 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
176 RenderTarget::kDefaultStencilAttachmentConfig,
177 const std::shared_ptr<Texture>& existing_color_msaa_texture = nullptr,
178 const std::shared_ptr<Texture>& existing_color_resolve_texture = nullptr,
179 const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr,
180 std::optional<PixelFormat> target_pixel_format = std::nullopt);
181
182 /// @brief Disable any caching until the next call to `EnabledCache`.
183 virtual void DisableCache() {}
184
185 /// @brief Re-enable any caching if disabled.
186 virtual void EnableCache() {}
187
188 /// @brief Mark the beginning of a frame workload.
189 ///
190 /// This may be used to reset any tracking state on whether or not a
191 /// particular texture instance is still in use.
192 virtual void Start();
193
194 /// @brief Mark the end of a frame workload.
195 ///
196 /// This may be used to deallocate any unused textures.
197 virtual void End();
198
199 private:
200 std::shared_ptr<Allocator> allocator_;
201 };
202
203 } // namespace impeller
204
205 #endif // FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
allocator.h
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:24
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
impeller::RenderTargetAllocator
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
Definition: render_target.h:149
impeller::RenderTargetAllocator::RenderTargetAllocator
RenderTargetAllocator(std::shared_ptr< Allocator > allocator)
Definition: render_target.cc:314
impeller::RenderTargetAllocator::CreateOffscreenMSAA
virtual RenderTarget CreateOffscreenMSAA(const Context &context, ISize size, int mip_count, std::string_view 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, std::optional< PixelFormat > target_pixel_format=std::nullopt)
Definition: render_target.cc:378
impeller::RenderTargetAllocator::~RenderTargetAllocator
virtual ~RenderTargetAllocator()=default
impeller::RenderTargetAllocator::EnableCache
virtual void EnableCache()
Re-enable any caching if disabled.
Definition: render_target.h:186
impeller::RenderTargetAllocator::CreateOffscreen
virtual RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, std::string_view 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, std::optional< PixelFormat > target_pixel_format=std::nullopt)
Definition: render_target.cc:322
impeller::RenderTargetAllocator::Start
virtual void Start()
Mark the beginning of a frame workload.
Definition: render_target.cc:318
impeller::RenderTargetAllocator::DisableCache
virtual void DisableCache()
Disable any caching until the next call to EnabledCache.
Definition: render_target.h:183
impeller::RenderTargetAllocator::End
virtual void End()
Mark the end of a frame workload.
Definition: render_target.cc:320
impeller::RenderTarget
Definition: render_target.h:38
impeller::RenderTarget::~RenderTarget
~RenderTarget()
impeller::RenderTarget::RenderTarget
RenderTarget()
impeller::RenderTarget::GetColorAttachment
ColorAttachment GetColorAttachment(size_t index) const
Get the color attachment at [index].
Definition: render_target.cc:233
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:176
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
impeller::RenderTarget::GetSampleCount
SampleCount GetSampleCount() const
Definition: render_target.cc:138
impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
Definition: render_target.h:61
impeller::RenderTarget::IterateAllAttachments
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
Definition: render_target.cc:112
impeller::RenderTarget::SetColorAttachment
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
Definition: render_target.cc:199
impeller::RenderTarget::HasColorAttachment
bool HasColorAttachment(size_t index) const
Definition: render_target.cc:145
impeller::RenderTarget::GetMaxColorAttacmentBindIndex
size_t GetMaxColorAttacmentBindIndex() const
Definition: render_target.cc:191
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
impeller::RenderTarget::ToString
std::string ToString() const
Definition: render_target.cc:278
impeller::RenderTarget::SetDepthAttachment
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
Definition: render_target.cc:213
impeller::RenderTarget::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
Definition: render_target.cc:183
impeller::RenderTarget::GetTotalAttachmentCount
size_t GetTotalAttachmentCount() const
Definition: render_target.cc:256
impeller::RenderTarget::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_target.cc:171
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:223
impeller::RenderTarget::IterateAllColorAttachments
bool IterateAllColorAttachments(const std::function< bool(size_t index, const ColorAttachment &attachment)> &iterator) const
Definition: render_target.cc:96
impeller::RenderTarget::GetColorAttachmentSize
std::optional< ISize > GetColorAttachmentSize(size_t index) const
Definition: render_target.cc:155
impeller::RenderTarget::GetDepthAttachment
const std::optional< DepthAttachment > & GetDepthAttachment() const
Definition: render_target.cc:247
impeller::RenderTarget::IsValid
bool IsValid() const
Definition: render_target.cc:23
impeller::RenderTarget::GetStencilAttachment
const std::optional< StencilAttachment > & GetStencilAttachment() const
Definition: render_target.cc:251
impeller::RenderTarget::SetupDepthStencilAttachments
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)
Definition: render_target.cc:481
impeller::RenderTarget::ToConfig
RenderTargetConfig ToConfig() const
Definition: render_target.cc:302
formats.h
impeller
Definition: allocation.cc:12
impeller::StorageMode
StorageMode
Specified where the allocation resides and how it is used.
Definition: formats.h:32
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
impeller::LoadAction
LoadAction
Definition: formats.h:205
impeller::LoadAction::kClear
@ kClear
impeller::StoreAction
StoreAction
Definition: formats.h:211
impeller::StoreAction::kStore
@ kStore
impeller::StoreAction::kDontCare
@ kDontCare
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::SampleCount
SampleCount
Definition: formats.h:298
size.h
impeller::Attachment
Definition: formats.h:660
impeller::ColorAttachment
Definition: formats.h:669
impeller::Color
Definition: color.h:124
impeller::Color::BlackTransparent
static constexpr Color BlackTransparent()
Definition: color.h:270
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:40
impeller::RenderTarget::AttachmentConfig::clear_color
Color clear_color
Definition: render_target.h:44
impeller::RenderTarget::AttachmentConfig::load_action
LoadAction load_action
Definition: render_target.h:42
impeller::RenderTarget::AttachmentConfig::store_action
StoreAction store_action
Definition: render_target.h:43
impeller::RenderTarget::AttachmentConfig::storage_mode
StorageMode storage_mode
Definition: render_target.h:41
impeller::RenderTarget::AttachmentConfigMSAA
Definition: render_target.h:47
impeller::RenderTarget::AttachmentConfigMSAA::clear_color
Color clear_color
Definition: render_target.h:52
impeller::RenderTarget::AttachmentConfigMSAA::storage_mode
StorageMode storage_mode
Definition: render_target.h:48
impeller::RenderTarget::AttachmentConfigMSAA::load_action
LoadAction load_action
Definition: render_target.h:50
impeller::RenderTarget::AttachmentConfigMSAA::store_action
StoreAction store_action
Definition: render_target.h:51
impeller::RenderTarget::AttachmentConfigMSAA::resolve_storage_mode
StorageMode resolve_storage_mode
Definition: render_target.h:49
impeller::RenderTargetConfig
Definition: render_target.h:21
impeller::RenderTargetConfig::Hash
constexpr size_t Hash() const
Definition: render_target.h:32
impeller::RenderTargetConfig::has_depth_stencil
bool has_depth_stencil
Definition: render_target.h:25
impeller::RenderTargetConfig::mip_count
size_t mip_count
Definition: render_target.h:23
impeller::RenderTargetConfig::size
ISize size
Definition: render_target.h:22
impeller::RenderTargetConfig::operator==
constexpr bool operator==(const RenderTargetConfig &o) const
Definition: render_target.h:27
impeller::RenderTargetConfig::has_msaa
bool has_msaa
Definition: render_target.h:24
impeller::TSize
Definition: size.h:25
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28