| Flutter Impeller |

formats.cc
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 #include "impeller/core/formats.h"
6
7 #include <sstream>
8
9 #include "impeller/base/strings.h"
10 #include "impeller/base/validation.h"
11 #include "impeller/core/texture.h"
12
13 namespace impeller {
14
15 constexpr bool StoreActionNeedsResolveTexture(StoreAction action) {
16 switch (action) {
17 case StoreAction::kDontCare:
18 case StoreAction::kStore:
19 return false;
20 case StoreAction::kMultisampleResolve:
21 case StoreAction::kStoreAndMultisampleResolve:
22 return true;
23 }
24 }
25
26 bool Attachment::IsValid() const {
27 if (!texture || !texture->IsValid()) {
28 VALIDATION_LOG << "Attachment has no texture.";
29 return false;
30 }
31
32 if (StoreActionNeedsResolveTexture(store_action)) {
33 if (!resolve_texture || !resolve_texture->IsValid()) {
34 VALIDATION_LOG << "Store action needs resolve but no valid resolve "
35 "texture specified.";
36 return false;
37 }
38 }
39
40 if (resolve_texture) {
41 if (store_action != StoreAction::kMultisampleResolve &&
42 store_action != StoreAction::kStoreAndMultisampleResolve) {
43 VALIDATION_LOG << "A resolve texture was specified, but the store action "
44 "doesn't include multisample resolve.";
45 return false;
46 }
47
48 if (texture->GetTextureDescriptor().storage_mode ==
49 StorageMode::kDeviceTransient &&
50 store_action == StoreAction::kStoreAndMultisampleResolve) {
51 VALIDATION_LOG
52 << "The multisample texture cannot be transient when "
53 "specifying the StoreAndMultisampleResolve StoreAction.";
54 }
55 }
56
57 auto storage_mode = resolve_texture
58 ? resolve_texture->GetTextureDescriptor().storage_mode
59 : texture->GetTextureDescriptor().storage_mode;
60
61 if (storage_mode == StorageMode::kDeviceTransient) {
62 if (load_action == LoadAction::kLoad) {
63 VALIDATION_LOG << "The LoadAction cannot be Load when attaching a device "
64 "transient " +
65 std::string(resolve_texture ? "resolve texture."
66 : "texture.");
67 return false;
68 }
69 if (store_action != StoreAction::kDontCare) {
70 VALIDATION_LOG << "The StoreAction must be DontCare when attaching a "
71 "device transient " +
72 std::string(resolve_texture ? "resolve texture."
73 : "texture.");
74 return false;
75 }
76 }
77
78 return true;
79 }
80
81 std::string TextureUsageMaskToString(TextureUsageMask mask) {
82 std::vector<TextureUsage> usages;
83 if (mask & TextureUsage::kShaderRead) {
84 usages.push_back(TextureUsage::kShaderRead);
85 }
86 if (mask & TextureUsage::kShaderWrite) {
87 usages.push_back(TextureUsage::kShaderWrite);
88 }
89 if (mask & TextureUsage::kRenderTarget) {
90 usages.push_back(TextureUsage::kRenderTarget);
91 }
92 std::stringstream stream;
93 stream << "{ ";
94 for (size_t i = 0; i < usages.size(); i++) {
95 stream << TextureUsageToString(usages[i]);
96 if (i != usages.size() - 1u) {
97 stream << ", ";
98 }
99 }
100 stream << " }";
101 return stream.str();
102 }
103
104 std::string AttachmentToString(const Attachment& attachment) {
105 std::stringstream stream;
106 if (attachment.texture) {
107 stream << "Texture=("
108 << TextureDescriptorToString(
109 attachment.texture->GetTextureDescriptor())
110 << "),";
111 }
112 if (attachment.resolve_texture) {
113 stream << "ResolveTexture=("
114 << TextureDescriptorToString(
115 attachment.resolve_texture->GetTextureDescriptor())
116 << "),";
117 }
118 stream << "LoadAction=" << LoadActionToString(attachment.load_action) << ",";
119 stream << "StoreAction=" << StoreActionToString(attachment.store_action);
120 return stream.str();
121 }
122
123 std::string ColorAttachmentToString(const ColorAttachment& color) {
124 std::stringstream stream;
125 stream << AttachmentToString(color) << ",";
126 stream << "ClearColor=(" << ColorToString(color.clear_color) << ")";
127 return stream.str();
128 }
129
130 std::string DepthAttachmentToString(const DepthAttachment& depth) {
131 std::stringstream stream;
132 stream << AttachmentToString(depth) << ",";
133 stream << "ClearDepth=" << SPrintF("%.2f", depth.clear_depth);
134 return stream.str();
135 }
136
137 std::string StencilAttachmentToString(const StencilAttachment& stencil) {
138 std::stringstream stream;
139 stream << AttachmentToString(stencil) << ",";
140 stream << "ClearStencil=" << stencil.clear_stencil;
141 return stream.str();
142 }
143
144 } // namespace impeller
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::LoadAction::kLoad
@ kLoad
impeller::TextureUsage::kShaderWrite
@ kShaderWrite
impeller::StoreAction
StoreAction
Definition: formats.h:208
impeller::StoreAction::kStoreAndMultisampleResolve
@ kStoreAndMultisampleResolve
impeller::ColorToString
std::string ColorToString(const Color &color)
Definition: color.cc:333
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:660
impeller::ColorAttachment
Definition: formats.h:665
impeller::LoadActionToString
constexpr const char * LoadActionToString(LoadAction action)
Definition: formats.h:215
formats.h
impeller::StoreAction::kDontCare
@ kDontCare
impeller::TextureDescriptorToString
std::string TextureDescriptorToString(const TextureDescriptor &desc)
Definition: texture_descriptor.cc:11
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
validation.h
impeller::Mask< TextureUsage >
impeller::StencilAttachment
Definition: formats.h:673
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::Attachment::texture
std::shared_ptr< Texture > texture
Definition: formats.h:657
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::AttachmentToString
std::string AttachmentToString(const Attachment &attachment)
Definition: formats.cc:104
impeller::ColorAttachmentToString
std::string ColorAttachmentToString(const ColorAttachment &color)
Definition: formats.cc:123
impeller::Attachment
Definition: formats.h:656
impeller::StoreAction::kStore
@ kStore
impeller::StencilAttachment::clear_stencil
uint32_t clear_stencil
Definition: formats.h:674
strings.h
impeller::TextureUsageMaskToString
std::string TextureUsageMaskToString(TextureUsageMask mask)
Definition: formats.cc:81
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::Attachment::resolve_texture
std::shared_ptr< Texture > resolve_texture
Definition: formats.h:658
impeller::DepthAttachmentToString
std::string DepthAttachmentToString(const DepthAttachment &depth)
Definition: formats.cc:130
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:659
impeller::TextureUsage::kShaderRead
@ kShaderRead
texture.h
impeller::Attachment::IsValid
bool IsValid() const
Definition: formats.cc:26
impeller::DepthAttachment::clear_depth
double clear_depth
Definition: formats.h:670
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
impeller::StoreActionToString
constexpr const char * StoreActionToString(StoreAction action)
Definition: formats.h:226
impeller::DepthAttachment
Definition: formats.h:669
impeller::StoreActionNeedsResolveTexture
constexpr bool StoreActionNeedsResolveTexture(StoreAction action)
Definition: formats.cc:15
impeller::TextureUsageToString
constexpr const char * TextureUsageToString(TextureUsage usage)
Definition: formats.h:310
impeller
Definition: allocation.cc:12
impeller::StencilAttachmentToString
std::string StencilAttachmentToString(const StencilAttachment &stencil)
Definition: formats.cc:137