| 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 #pragma once
6
7 #include <functional>
8 #include <map>
9 #include <optional>
10
11 #include "flutter/fml/macros.h"
12 #include "impeller/core/allocator.h"
13 #include "impeller/core/formats.h"
14 #include "impeller/geometry/size.h"
15
16 namespace impeller {
17
18 class Context;
19
20 /// @brief a wrapper around the impeller [Allocator] instance that can be used
21 /// to provide caching of allocated render target textures.
22 class RenderTargetAllocator {
23 public:
24 explicit RenderTargetAllocator(std::shared_ptr<Allocator> allocator);
25
26 virtual ~RenderTargetAllocator() = default;
27
28 /// @brief Create a new render target texture, or recycle a previously
29 /// allocated render
30 /// target texture.
31 virtual std::shared_ptr<Texture> CreateTexture(const TextureDescriptor& desc);
32
33 /// @brief Mark the beginning of a frame workload.
34 ///
35 /// This may be used to reset any tracking state on whether or not a
36 /// particular texture instance is still in use.
37 virtual void Start();
38
39 /// @brief Mark the end of a frame workload.
40 ///
41 /// This may be used to deallocate any unused textures.
42 virtual void End();
43
44 private:
45 std::shared_ptr<Allocator> allocator_;
46 };
47
48 class RenderTarget final {
49 public:
50 struct AttachmentConfig {
51 StorageMode storage_mode;
52 LoadAction load_action;
53 StoreAction store_action;
54 Color clear_color;
55 };
56
57 struct AttachmentConfigMSAA {
58 StorageMode storage_mode;
59 StorageMode resolve_storage_mode;
60 LoadAction load_action;
61 StoreAction store_action;
62 Color clear_color;
63 };
64
65 static constexpr AttachmentConfig kDefaultColorAttachmentConfig = {
66 .storage_mode = StorageMode::kDevicePrivate,
67 .load_action = LoadAction::kClear,
68 .store_action = StoreAction::kStore,
69 .clear_color = Color::BlackTransparent()};
70
71 static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA = {
72 .storage_mode = StorageMode::kDeviceTransient,
73 .resolve_storage_mode = StorageMode::kDevicePrivate,
74 .load_action = LoadAction::kClear,
75 .store_action = StoreAction::kMultisampleResolve,
76 .clear_color = Color::BlackTransparent()};
77
78 static constexpr AttachmentConfig kDefaultStencilAttachmentConfig = {
79 .storage_mode = StorageMode::kDeviceTransient,
80 .load_action = LoadAction::kClear,
81 .store_action = StoreAction::kDontCare,
82 .clear_color = Color::BlackTransparent()};
83
84 static RenderTarget CreateOffscreen(
85 const Context& context,
86 RenderTargetAllocator& allocator,
87 ISize size,
88 const std::string& label = "Offscreen",
89 AttachmentConfig color_attachment_config = kDefaultColorAttachmentConfig,
90 std::optional<AttachmentConfig> stencil_attachment_config =
91 kDefaultStencilAttachmentConfig);
92
93 static RenderTarget CreateOffscreenMSAA(
94 const Context& context,
95 RenderTargetAllocator& allocator,
96 ISize size,
97 const std::string& label = "Offscreen MSAA",
98 AttachmentConfigMSAA color_attachment_config =
99 kDefaultColorAttachmentConfigMSAA,
100 std::optional<AttachmentConfig> stencil_attachment_config =
101 kDefaultStencilAttachmentConfig);
102
103 RenderTarget();
104
105 ~RenderTarget();
106
107 bool IsValid() const;
108
109 void SetupStencilAttachment(const Context& context,
110 RenderTargetAllocator& allocator,
111 ISize size,
112 bool msaa,
113 const std::string& label = "Offscreen",
114 AttachmentConfig stencil_attachment_config =
115 kDefaultStencilAttachmentConfig);
116
117 SampleCount GetSampleCount() const;
118
119 bool HasColorAttachment(size_t index) const;
120
121 ISize GetRenderTargetSize() const;
122
123 std::shared_ptr<Texture> GetRenderTargetTexture() const;
124
125 PixelFormat GetRenderTargetPixelFormat() const;
126
127 std::optional<ISize> GetColorAttachmentSize(size_t index) const;
128
129 RenderTarget& SetColorAttachment(const ColorAttachment& attachment,
130 size_t index);
131
132 RenderTarget& SetDepthAttachment(std::optional<DepthAttachment> attachment);
133
134 RenderTarget& SetStencilAttachment(
135 std::optional<StencilAttachment> attachment);
136
137 size_t GetMaxColorAttacmentBindIndex() const;
138
139 const std::map<size_t, ColorAttachment>& GetColorAttachments() const;
140
141 const std::optional<DepthAttachment>& GetDepthAttachment() const;
142
143 const std::optional<StencilAttachment>& GetStencilAttachment() const;
144
145 size_t GetTotalAttachmentCount() const;
146
147 void IterateAllAttachments(
148 const std::function<bool(const Attachment& attachment)>& iterator) const;
149
150 std::string ToString() const;
151
152 private:
153 std::map<size_t, ColorAttachment> colors_;
154 std::optional<DepthAttachment> depth_;
155 std::optional<StencilAttachment> stencil_;
156 };
157
158 } // namespace impeller
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::RenderTarget::SetupStencilAttachment
void SetupStencilAttachment(const Context &context, RenderTargetAllocator &allocator, ISize size, bool msaa, const std::string &label="Offscreen", AttachmentConfig stencil_attachment_config=kDefaultStencilAttachmentConfig)
Definition: render_target.cc:349
impeller::RenderTarget::GetTotalAttachmentCount
size_t GetTotalAttachmentCount() const
Definition: render_target.cc:380
impeller::RenderTarget::AttachmentConfig::store_action
StoreAction store_action
Definition: render_target.h:53
impeller::StoreAction
StoreAction
Definition: formats.h:182
impeller::RenderTarget::AttachmentConfig::load_action
LoadAction load_action
Definition: render_target.h:52
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:65
impeller::SampleCount
SampleCount
Definition: formats.h:269
impeller::RenderTargetAllocator::Start
virtual void Start()
Mark the beginning of a frame workload.
Definition: render_target.cc:21
impeller::Color
Definition: color.h:122
impeller::ColorAttachment
Definition: formats.h:599
impeller::RenderTarget::SetColorAttachment
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
Definition: render_target.cc:180
impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
Definition: render_target.h:71
formats.h
impeller::RenderTarget::AttachmentConfigMSAA::store_action
StoreAction store_action
Definition: render_target.h:61
impeller::StoreAction::kDontCare
@ kDontCare
impeller::RenderTarget::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
Definition: render_target.cc:164
impeller::RenderTarget::GetColorAttachmentSize
std::optional< ISize > GetColorAttachmentSize(size_t index) const
Definition: render_target.cc:140
impeller::RenderTargetAllocator::RenderTargetAllocator
RenderTargetAllocator(std::shared_ptr< Allocator > allocator)
Definition: render_target.cc:17
impeller::RenderTarget::GetColorAttachments
const std::map< size_t, ColorAttachment > & GetColorAttachments() const
Definition: render_target.cc:209
impeller::RenderTarget::AttachmentConfigMSAA::load_action
LoadAction load_action
Definition: render_target.h:60
impeller::RenderTarget::AttachmentConfigMSAA::resolve_storage_mode
StorageMode resolve_storage_mode
Definition: render_target.h:59
impeller::RenderTarget::AttachmentConfigMSAA
Definition: render_target.h:57
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:50
impeller::TSize
Definition: size.h:18
impeller::LoadAction::kClear
@ kClear
impeller::RenderTarget::GetDepthAttachment
const std::optional< DepthAttachment > & GetDepthAttachment() const
Definition: render_target.cc:214
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::StorageMode
StorageMode
Specified where the allocation resides and how it is used.
Definition: formats.h:27
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:155
impeller::RenderTarget::IterateAllAttachments
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
Definition: render_target.cc:105
impeller::RenderTarget::~RenderTarget
~RenderTarget()
impeller::LoadAction
LoadAction
Definition: formats.h:176
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:78
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::RenderTarget::AttachmentConfig::storage_mode
StorageMode storage_mode
Definition: render_target.h:51
impeller::RenderTargetAllocator::CreateTexture
virtual std::shared_ptr< Texture > CreateTexture(const TextureDescriptor &desc)
Create a new render target texture, or recycle a previously allocated render target texture.
Definition: render_target.cc:25
impeller::Attachment
Definition: formats.h:590
impeller::RenderTarget
Definition: render_target.h:48
impeller::StoreAction::kStore
@ kStore
impeller::RenderTarget::IsValid
bool IsValid() const
Definition: render_target.cc:34
impeller::RenderTargetAllocator
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
Definition: render_target.h:22
impeller::RenderTarget::AttachmentConfig::clear_color
Color clear_color
Definition: render_target.h:54
impeller::RenderTarget::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_target.cc:150
impeller::RenderTarget::CreateOffscreen
static RenderTarget CreateOffscreen(const Context &context, RenderTargetAllocator &allocator, ISize size, const std::string &label="Offscreen", AttachmentConfig color_attachment_config=kDefaultColorAttachmentConfig, std::optional< AttachmentConfig > stencil_attachment_config=kDefaultStencilAttachmentConfig)
Definition: render_target.cc:223
allocator.h
impeller::RenderTarget::AttachmentConfigMSAA::storage_mode
StorageMode storage_mode
Definition: render_target.h:58
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:199
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
impeller::RenderTargetAllocator::End
virtual void End()
Mark the end of a frame workload.
Definition: render_target.cc:23
impeller::Color::BlackTransparent
static constexpr Color BlackTransparent()
Definition: color.h:260
impeller::RenderTarget::GetMaxColorAttacmentBindIndex
size_t GetMaxColorAttacmentBindIndex() const
Definition: render_target.cc:172
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:39
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:94
impeller::RenderTarget::GetStencilAttachment
const std::optional< StencilAttachment > & GetStencilAttachment() const
Definition: render_target.cc:218
impeller::RenderTarget::SetDepthAttachment
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
Definition: render_target.cc:189
impeller::RenderTarget::GetSampleCount
SampleCount GetSampleCount() const
Definition: render_target.cc:126
impeller::RenderTarget::HasColorAttachment
bool HasColorAttachment(size_t index) const
Definition: render_target.cc:133
impeller
Definition: aiks_context.cc:10
impeller::RenderTarget::ToString
std::string ToString() const
Definition: render_target.cc:399
impeller::RenderTarget::CreateOffscreenMSAA
static RenderTarget CreateOffscreenMSAA(const Context &context, RenderTargetAllocator &allocator, ISize size, const std::string &label="Offscreen MSAA", AttachmentConfigMSAA color_attachment_config=kDefaultColorAttachmentConfigMSAA, std::optional< AttachmentConfig > stencil_attachment_config=kDefaultStencilAttachmentConfig)
Definition: render_target.cc:270
impeller::RenderTargetAllocator::~RenderTargetAllocator
virtual ~RenderTargetAllocator()=default
size.h
impeller::RenderTarget::AttachmentConfigMSAA::clear_color
Color clear_color
Definition: render_target.h:62
impeller::RenderTarget::RenderTarget
RenderTarget()