• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //* Copyright 2021 The Dawn Authors
2 //*
3 //* Licensed under the Apache License, Version 2.0 (the "License");
4 //* you may not use this file except in compliance with the License.
5 //* You may obtain a copy of the License at
6 //*
7 //*     http://www.apache.org/licenses/LICENSE-2.0
8 //*
9 //* Unless required by applicable law or agreed to in writing, software
10 //* distributed under the License is distributed on an "AS IS" BASIS,
11 //* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //* See the License for the specific language governing permissions and
13 //* limitations under the License.
14 
15 #include "dawn_native/webgpu_absl_format_autogen.h"
16 
17 #include "dawn_native/Device.h"
18 #include "dawn_native/ObjectBase.h"
19 #include "dawn_native/ObjectType_autogen.h"
20 #include "dawn_native/Texture.h"
21 
22 namespace dawn_native {
23 
24     //
25     // Structs (Manually written)
26     //
27 
28     absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const Color * value,const absl::FormatConversionSpec & spec,absl::FormatSink * s)29     AbslFormatConvert(const Color* value,
30                       const absl::FormatConversionSpec& spec,
31                       absl::FormatSink* s) {
32         if (value == nullptr) {
33             s->Append("[null]");
34             return {true};
35         }
36         s->Append(absl::StrFormat("[Color r:%f, g:%f, b:%f, a:%f]",
37             value->r, value->g, value->b, value->a));
38         return {true};
39     }
40 
41     absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const Extent3D * value,const absl::FormatConversionSpec & spec,absl::FormatSink * s)42     AbslFormatConvert(const Extent3D* value,
43                       const absl::FormatConversionSpec& spec,
44                       absl::FormatSink* s) {
45         if (value == nullptr) {
46             s->Append("[null]");
47             return {true};
48         }
49         s->Append(absl::StrFormat("[Extent3D width:%u, height:%u, depthOrArrayLayers:%u]",
50             value->width, value->height, value->depthOrArrayLayers));
51         return {true};
52     }
53 
54     absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const Origin3D * value,const absl::FormatConversionSpec & spec,absl::FormatSink * s)55     AbslFormatConvert(const Origin3D* value,
56                       const absl::FormatConversionSpec& spec,
57                       absl::FormatSink* s) {
58         if (value == nullptr) {
59             s->Append("[null]");
60             return {true};
61         }
62         s->Append(absl::StrFormat("[Origin3D x:%u, y:%u, z:%u]",
63             value->x, value->y, value->z));
64         return {true};
65     }
66 
67     //
68     // Objects
69     //
70 
71     absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const DeviceBase * value,const absl::FormatConversionSpec & spec,absl::FormatSink * s)72     AbslFormatConvert(const DeviceBase* value,
73                       const absl::FormatConversionSpec& spec,
74                       absl::FormatSink* s) {
75         if (value == nullptr) {
76             s->Append("[null]");
77             return {true};
78         }
79         s->Append("[Device");
80         const std::string& label = value->GetLabel();
81         if (!label.empty()) {
82             s->Append(absl::StrFormat(" \"%s\"", label));
83         }
84         s->Append("]");
85         return {true};
86     }
87 
88     absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const ApiObjectBase * value,const absl::FormatConversionSpec & spec,absl::FormatSink * s)89     AbslFormatConvert(const ApiObjectBase* value,
90                       const absl::FormatConversionSpec& spec,
91                       absl::FormatSink* s) {
92         if (value == nullptr) {
93             s->Append("[null]");
94             return {true};
95         }
96         s->Append("[");
97         if (value->IsError()) {
98             s->Append("Invalid ");
99         }
100         s->Append(ObjectTypeAsString(value->GetType()));
101         const std::string& label = value->GetLabel();
102         if (!label.empty()) {
103             s->Append(absl::StrFormat(" \"%s\"", label));
104         }
105         s->Append("]");
106         return {true};
107     }
108 
109     absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const TextureViewBase * value,const absl::FormatConversionSpec & spec,absl::FormatSink * s)110     AbslFormatConvert(const TextureViewBase* value,
111                       const absl::FormatConversionSpec& spec,
112                       absl::FormatSink* s) {
113         if (value == nullptr) {
114             s->Append("[null]");
115             return {true};
116         }
117         s->Append("[");
118         if (value->IsError()) {
119             s->Append("Invalid ");
120         }
121         s->Append(ObjectTypeAsString(value->GetType()));
122         const std::string& label = value->GetLabel();
123         if (!label.empty()) {
124             s->Append(absl::StrFormat(" \"%s\"", label));
125         }
126         const std::string& textureLabel = value->GetTexture()->GetLabel();
127         if (!textureLabel.empty()) {
128             s->Append(absl::StrFormat(" of Texture \"%s\"", textureLabel));
129         }
130         s->Append("]");
131         return {true};
132     }
133 
134     //
135     // Descriptors
136     //
137 
138     {% for type in by_category["structure"] %}
139         {% for member in type.members %}
140             {% if member.name.canonical_case() == "label" %}
141                 absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
142                 AbslFormatConvert(const {{as_cppType(type.name)}}* value,
143                                     const absl::FormatConversionSpec& spec,
144                                     absl::FormatSink* s) {
145                     if (value == nullptr) {
146                         s->Append("[null]");
147                         return {true};
148                     }
149                     s->Append("[{{as_cppType(type.name)}}");
150                     if (value->label != nullptr) {
151                         s->Append(absl::StrFormat(" \"%s\"", value->label));
152                     }
153                     s->Append("]");
154                     return {true};
155                 }
156             {% endif %}
157         {% endfor %}
158     {% endfor %}
159 
160 }  // namespace dawn_native
161 
162 namespace wgpu {
163 
164     //
165     // Enums
166     //
167 
168     {% for type in by_category["enum"] %}
169         absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
170         AbslFormatConvert({{as_cppType(type.name)}} value,
171                             const absl::FormatConversionSpec& spec,
172                             absl::FormatSink* s) {
173             s->Append("{{as_cppType(type.name)}}::");
174             switch (value) {
175             {% for value in type.values %}
176                 case {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
177                 s->Append("{{as_cppEnum(value.name)}}");
178                 break;
179             {% endfor %}
180                 default:
181                 s->Append(absl::StrFormat("%x", static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value)));
182             }
183             return {true};
184         }
185     {% endfor %}
186 
187     //
188     // Bitmasks
189     //
190 
191     {% for type in by_category["bitmask"] %}
192         absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
193         AbslFormatConvert({{as_cppType(type.name)}} value,
194                             const absl::FormatConversionSpec& spec,
195                             absl::FormatSink* s) {
196             s->Append("{{as_cppType(type.name)}}::");
197             if (!static_cast<bool>(value)) {
198                 {% for value in type.values if value.value == 0 %}
199                     // 0 is often explicitly declared as None.
200                     s->Append("{{as_cppEnum(value.name)}}");
201                 {% else %}
202                     s->Append(absl::StrFormat("{{as_cppType(type.name)}}::%x", 0));
203                 {% endfor %}
204                 return {true};
205             }
206 
207             bool moreThanOneBit = !HasZeroOrOneBits(value);
208             if (moreThanOneBit) {
209                 s->Append("(");
210             }
211 
212             bool first = true;
213             {% for value in type.values if value.value != 0 %}
214                 if (value & {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}) {
215                     if (!first) {
216                         s->Append("|");
217                     }
218                     first = false;
219                     s->Append("{{as_cppEnum(value.name)}}");
220                     value &= ~{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}};
221                 }
222             {% endfor %}
223 
224             if (static_cast<bool>(value)) {
225                 if (!first) {
226                     s->Append("|");
227                 }
228                 s->Append(absl::StrFormat("{{as_cppType(type.name)}}::%x", static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value)));
229             }
230 
231             if (moreThanOneBit) {
232                 s->Append(")");
233             }
234 
235             return {true};
236         }
237     {% endfor %}
238 
239 }  // namespace wgpu
240