27 <<
"Render target does not have color attachment at index 0.";
33 std::optional<ISize> size;
34 bool sizes_are_same =
true;
35 auto iterator = [&](
const Attachment& attachment) ->
bool {
36 if (!size.has_value()) {
37 size = attachment.texture->GetSize();
39 if (size != attachment.texture->GetSize()) {
40 sizes_are_same =
false;
46 if (!sizes_are_same) {
48 <<
"Sizes of all render target attachments are not the same.";
55 std::optional<TextureType> texture_type;
56 std::optional<SampleCount> sample_count;
57 bool passes_type_validation =
true;
58 auto iterator = [&](
const Attachment& attachment) ->
bool {
59 if (!texture_type.has_value() || !sample_count.has_value()) {
60 texture_type = attachment.texture->GetTextureDescriptor().type;
61 sample_count = attachment.texture->GetTextureDescriptor().sample_count;
64 if (texture_type != attachment.texture->GetTextureDescriptor().type) {
65 passes_type_validation =
false;
69 attachment.texture->GetTextureDescriptor().type)
75 attachment.texture->GetTextureDescriptor().sample_count) {
76 passes_type_validation =
false;
78 <<
") has incompatible sample counts.";
86 if (!passes_type_validation) {
95 const std::function<
bool(
const Attachment& attachment)>& iterator)
const {
96 for (
const auto&
color : colors_) {
97 if (!iterator(
color.second)) {
102 if (depth_.has_value()) {
103 if (!iterator(depth_.value())) {
108 if (stencil_.has_value()) {
109 if (!iterator(stencil_.value())) {
116 if (
auto found = colors_.find(0u); found != colors_.end()) {
117 return found->second.texture->GetTextureDescriptor().sample_count;
123 if (
auto found = colors_.find(index); found != colors_.end()) {
130 auto found = colors_.find(index);
132 if (found == colors_.end()) {
136 return found->second.texture->GetSize();
141 return size.has_value() ? size.value() :
ISize{};
145 auto found = colors_.find(0u);
146 if (found == colors_.end()) {
149 return found->second.resolve_texture ? found->second.resolve_texture
150 : found->second.texture;
155 return texture->GetTextureDescriptor().format;
163 for (
const auto&
color : colors_) {
173 colors_[index] = attachment;
179 std::optional<DepthAttachment> attachment) {
180 if (!attachment.has_value()) {
181 depth_ = std::nullopt;
182 }
else if (attachment->IsValid()) {
183 depth_ = std::move(attachment);
189 std::optional<StencilAttachment> attachment) {
190 if (!attachment.has_value()) {
191 stencil_ = std::nullopt;
192 }
else if (attachment->IsValid()) {
193 stencil_ = std::move(attachment);
214 for (
const auto& [_,
color] : colors_) {
218 if (
color.resolve_texture) {
222 if (depth_.has_value()) {
225 if (stencil_.has_value()) {
232 std::stringstream stream;
234 for (
const auto& [index,
color] : colors_) {
235 stream <<
SPrintF(
"Color[%zu]=(%s)", index,
240 stream <<
SPrintF(
"Depth=(%s)",
245 stream <<
SPrintF(
"Stencil=(%s)",
252 std::shared_ptr<Allocator> allocator)
253 : allocator_(
std::move(allocator)) {}
263 const std::string& label,
265 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
266 const std::shared_ptr<Texture>& existing_color_texture,
267 const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
268 if (size.IsEmpty()) {
274 std::shared_ptr<Texture> color0_tex;
275 if (existing_color_texture) {
276 color0_tex = existing_color_texture;
282 color0_tex_desc.
format = pixel_format;
283 color0_tex_desc.
size = size;
285 color0_tex_desc.
usage =
287 color0_tex = allocator_->CreateTexture(color0_tex_desc);
292 color0_tex->SetLabel(
SPrintF(
"%s Color Texture", label.c_str()));
299 target.SetColorAttachment(color0, 0u);
301 if (stencil_attachment_config.has_value()) {
302 target.SetupDepthStencilAttachments(
303 context, *allocator_, size,
false, label,
304 stencil_attachment_config.value(), existing_depth_stencil_texture);
306 target.SetStencilAttachment(std::nullopt);
307 target.SetDepthAttachment(std::nullopt);
317 const std::string& label,
319 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
320 const std::shared_ptr<Texture>& existing_color_msaa_texture,
321 const std::shared_ptr<Texture>& existing_color_resolve_texture,
322 const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
323 if (size.IsEmpty()) {
331 std::shared_ptr<Texture> color0_msaa_tex;
332 if (existing_color_msaa_texture) {
333 color0_msaa_tex = existing_color_msaa_texture;
339 color0_tex_desc.
format = pixel_format;
340 color0_tex_desc.
size = size;
346 color0_msaa_tex = allocator_->CreateTexture(color0_tex_desc);
347 if (!color0_msaa_tex) {
352 color0_msaa_tex->SetLabel(
353 SPrintF(
"%s Color Texture (Multisample)", label.c_str()));
356 std::shared_ptr<Texture> color0_resolve_tex;
357 if (existing_color_resolve_texture) {
358 color0_resolve_tex = existing_color_resolve_texture;
363 color0_resolve_tex_desc.
format = pixel_format;
364 color0_resolve_tex_desc.
size = size;
366 color0_resolve_tex_desc.
usage =
368 color0_resolve_tex_desc.
mip_count = mip_count;
369 color0_resolve_tex = allocator_->CreateTexture(color0_resolve_tex_desc);
370 if (!color0_resolve_tex) {
375 color0_resolve_tex->SetLabel(
SPrintF(
"%s Color Texture", label.c_str()));
383 color0.
texture = color0_msaa_tex;
397 target.SetColorAttachment(color0, 0u);
401 if (stencil_attachment_config.has_value()) {
402 target.SetupDepthStencilAttachments(context, *allocator_, size,
true, label,
403 stencil_attachment_config.value(),
404 existing_depth_stencil_texture);
406 target.SetDepthAttachment(std::nullopt);
407 target.SetStencilAttachment(std::nullopt);
418 const std::string& label,
420 const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
421 std::shared_ptr<Texture> depth_stencil_texture;
422 if (existing_depth_stencil_texture) {
423 depth_stencil_texture = existing_depth_stencil_texture;
432 depth_stencil_texture_desc.
format =
434 depth_stencil_texture_desc.
size = size;
436 depth_stencil_texture = allocator.
CreateTexture(depth_stencil_texture_desc);
437 if (!depth_stencil_texture) {
446 depth0.
texture = depth_stencil_texture;
452 stencil0.
texture = std::move(depth_stencil_texture);
455 SPrintF(
"%s Depth+Stencil Texture", label.c_str()));
An object that allocates device memory.
std::shared_ptr< Texture > CreateTexture(const TextureDescriptor &desc)
To do anything rendering related with Impeller, you need a context.
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
RenderTargetAllocator(std::shared_ptr< Allocator > allocator)
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)
virtual void Start()
Mark the beginning of a frame workload.
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)
virtual void End()
Mark the end of a frame workload.
std::shared_ptr< Texture > GetRenderTargetTexture() const
const std::map< size_t, ColorAttachment > & GetColorAttachments() const
SampleCount GetSampleCount() const
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
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)
bool HasColorAttachment(size_t index) const
size_t GetMaxColorAttacmentBindIndex() const
std::string ToString() const
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
PixelFormat GetRenderTargetPixelFormat() const
size_t GetTotalAttachmentCount() const
ISize GetRenderTargetSize() const
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
std::optional< ISize > GetColorAttachmentSize(size_t index) const
const std::optional< DepthAttachment > & GetDepthAttachment() const
const std::optional< StencilAttachment > & GetStencilAttachment() const
static float max(float r, float g, float b)
std::string DepthAttachmentToString(const DepthAttachment &depth)
std::string ColorAttachmentToString(const ColorAttachment &color)
std::string StencilAttachmentToString(const StencilAttachment &stencil)
std::string SPrintF(const char *format,...)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
constexpr const char * TextureTypeToString(TextureType type)
std::shared_ptr< Texture > resolve_texture
std::shared_ptr< Texture > texture
StorageMode resolve_storage_mode
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
CompressionType compression_type