Debian Codegraph @

libs/matdbg/src/TextWriter.cpp

raw
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <filaflat/ChunkContainer.h>
18
19 #include <filament/MaterialEnums.h>
20
21 #include <backend/DriverEnums.h>
22
23 #include <matdbg/TextWriter.h>
24 #include <matdbg/ShaderInfo.h>
25
26 #include <sstream>
27 #include <iomanip>
28
29 #include "CommonWriter.h"
30
31 using namespace filament;
32 using namespace backend;
33 using namespace filaflat;
34 using namespace filamat;
35 using namespace std;
36 using namespace utils;
37
38 namespace filament {
39 namespace matdbg {
40
41 constexpr int alignment = 32;
42 constexpr int shortAlignment = 15;
43
44 static string arraySizeToString(uint64_t size) {
45 if (size > 1) {
46 string s = "[";
47 s += size;
48 s += "]";
49 return s;
50 }
51 return "";
52 }
53
54 template<typename T, typename V>
55 static void printChunk(ostream& text, const ChunkContainer& container, ChunkType type,
56 const char* title) {
57 T value;
58 if (read(container, type, reinterpret_cast<V*>(&value))) {
59 text << " " << setw(alignment) << left << title;
60 text << toString(value) << endl;
61 }
62 }
63
64 static void printFloatChunk(ostream& text, const ChunkContainer& container, ChunkType type,
65 const char* title) {
66 float value;
67 if (read(container, type, &value)) {
68 text << " " << setw(alignment) << left << title;
69 text << setprecision(2) << value << endl;
70 }
71 }
72
73 static void printUint32Chunk(ostream& text, const ChunkContainer& container,
74 ChunkType type, const char* title) {
75 uint32_t value;
76 if (read(container, type, &value)) {
77 text << "\"" << title << "\": " << value << ",\n";
78 }
79 }
80
81 static void printStringChunk(ostream& text, const ChunkContainer& container,
82 ChunkType type, const char* title) {
83 CString value;
84 if (read(container, type, &value)) {
85 text << "\"" << title << "\": \"" << value.c_str() << "\",\n";
86 }
87 }
88
89 static bool printMaterial(ostream& text, const ChunkContainer& container) {
90 text << "Material:" << endl;
91
92 uint32_t version;
93 if (read(container, MaterialVersion, &version)) {
94 text << " " << setw(alignment) << left << "Version: ";
95 text << version << endl;
96 }
97
98 CString name;
99 if (read(container, MaterialName, &name)) {
100 text << " " << setw(alignment) << left << "Name: ";
101 text << name.c_str() << endl;
102 }
103
104 text << endl;
105
106 text << "Shading:" << endl;
107 printChunk<Shading, uint8_t>(text, container, MaterialShading, "Model: ");
108 printChunk<MaterialDomain, uint8_t>(text, container, ChunkType::MaterialDomain, "Material domain: ");
109 printChunk<VertexDomain, uint8_t>(text, container, MaterialVertexDomain, "Vertex domain: ");
110 printChunk<Interpolation, uint8_t>(text, container, MaterialInterpolation, "Interpolation: ");
111 printChunk<bool, bool>(text, container, MaterialShadowMultiplier, "Shadow multiply: ");
112 printChunk<bool, bool>(text, container, MaterialSpecularAntiAliasing, "Specular anti-aliasing: ");
113 printFloatChunk(text, container, MaterialSpecularAntiAliasingVariance, " Variance: ");
114 printFloatChunk(text, container, MaterialSpecularAntiAliasingThreshold, " Threshold: ");
115 printChunk<bool, bool>(text, container, MaterialClearCoatIorChange, "Clear coat IOR change: ");
116
117 text << endl;
118
119 text << "Raster state:" << endl;
120 printChunk<BlendingMode, uint8_t>(text, container, MaterialBlendingMode, "Blending: ");
121 printFloatChunk(text, container, MaterialMaskThreshold, "Mask threshold: ");
122 printChunk<bool, bool>(text, container, MaterialColorWrite, "Color write: ");
123 printChunk<bool, bool>(text, container, MaterialDepthWrite, "Depth write: ");
124 printChunk<bool, bool>(text, container, MaterialDepthTest, "Depth test: ");
125 printChunk<bool, bool>(text, container, MaterialDoubleSided, "Double sided: ");
126 printChunk<CullingMode, uint8_t>(text, container, MaterialCullingMode, "Culling: ");
127 printChunk<TransparencyMode, uint8_t>(text, container, MaterialTransparencyMode, "Transparency: ");
128
129 text << endl;
130
131 uint32_t requiredAttributes;
132 if (read(container, MaterialRequiredAttributes, &requiredAttributes)) {
133 AttributeBitset bitset;
134 bitset.setValue(requiredAttributes);
135
136 if (bitset.count() > 0) {
137 text << "Required attributes:" << endl;
138 for (size_t i = 0; i < bitset.size(); i++) {
139 if (bitset.test(i)) {
140 text << " " << toString(static_cast<VertexAttribute>(i)) << endl;
141 }
142 }
143 text << endl;
144 }
145 }
146
147 return true;
148 }
149
150 static bool printParametersInfo(ostream& text, const ChunkContainer& container) {
151 if (!container.hasChunk(ChunkType::MaterialUib)) {
152 return true;
153 }
154
155 Unflattener uib(
156 container.getChunkStart(ChunkType::MaterialUib),
157 container.getChunkEnd(ChunkType::MaterialUib));
158
159 CString name;
160 if (!uib.read(&name)) {
161 return false;
162 }
163
164 uint64_t uibCount;
165 if (!uib.read(&uibCount)) {
166 return false;
167 }
168
169 Unflattener sib(
170 container.getChunkStart(ChunkType::MaterialSib),
171 container.getChunkEnd(ChunkType::MaterialSib));
172
173 if (!sib.read(&name)) {
174 return false;
175 }
176
177 uint64_t sibCount;
178 if (!sib.read(&sibCount)) {
179 return false;
180 }
181
182 if (uibCount == 0 && sibCount == 0) {
183 return true;
184 }
185
186 text << "Parameters:" << endl;
187
188 for (uint64_t i = 0; i < uibCount; i++) {
189 CString fieldName;
190 uint64_t fieldSize;
191 uint8_t fieldType;
192 uint8_t fieldPrecision;
193
194 if (!uib.read(&fieldName)) {
195 return false;
196 }
197
198 if (!uib.read(&fieldSize)) {
199 return false;
200 }
201
202 if (!uib.read(&fieldType)) {
203 return false;
204 }
205
206 if (!uib.read(&fieldPrecision)) {
207 return false;
208 }
209
210 text << " "
211 << setw(alignment) << fieldName.c_str()
212 << setw(shortAlignment) << toString(UniformType(fieldType))
213 << arraySizeToString(fieldSize)
214 << setw(shortAlignment) << toString(Precision(fieldPrecision))
215 << endl;
216 }
217
218 for (uint64_t i = 0; i < sibCount; i++) {
219 CString fieldName;
220 uint8_t fieldType;
221 uint8_t fieldFormat;
222 uint8_t fieldPrecision;
223 bool fieldMultisample;
224
225 if (!sib.read(&fieldName)) {
226 return false;
227 }
228
229 if (!sib.read(&fieldType)) {
230 return false;
231 }
232
233 if (!sib.read(&fieldFormat))
234 return false;
235
236 if (!sib.read(&fieldPrecision)) {
237 return false;
238 }
239
240 if (!sib.read(&fieldMultisample)) {
241 return false;
242 }
243
244 text << " "
245 << setw(alignment) << fieldName.c_str()
246 << setw(shortAlignment) << toString(SamplerType(fieldType))
247 << setw(shortAlignment) << toString(Precision(fieldPrecision))
248 << toString(SamplerFormat(fieldFormat))
249 << endl;
250 }
251
252 // Subpasses are optional.
253 if (container.hasChunk(ChunkType::MaterialSubpass)) {
254 Unflattener subpasses(
255 container.getChunkStart(ChunkType::MaterialSubpass),
256 container.getChunkEnd(ChunkType::MaterialSubpass));
257
258 CString name;
259 if (!subpasses.read(&name)) {
260 return false;
261 }
262
263 uint64_t subpassCount;
264 subpasses.read(&subpassCount);
265
266 for (uint64_t i = 0; i < subpassCount; i++) {
267 CString fieldName;
268 uint8_t fieldType;
269 uint8_t fieldFormat;
270 uint8_t fieldPrecision;
271 uint8_t attachmentIndex;
272 uint8_t binding;
273
274 if (!subpasses.read(&fieldName)) {
275 return false;
276 }
277
278 if (!subpasses.read(&fieldType)) {
279 return false;
280 }
281
282 if (!subpasses.read(&fieldFormat))
283 return false;
284
285 if (!subpasses.read(&fieldPrecision)) {
286 return false;
287 }
288
289 if (!subpasses.read(&attachmentIndex)) {
290 return false;
291 }
292
293 if (!subpasses.read(&binding)) {
294 return false;
295 }
296
297 text << " "
298 << setw(alignment) << fieldName.c_str()
299 << setw(shortAlignment) << toString(SubpassType(fieldType))
300 << setw(shortAlignment) << toString(Precision(fieldPrecision))
301 << toString(SamplerFormat(fieldFormat))
302 << endl;
303 }
304 }
305
306 text << endl;
307
308 return true;
309 }
310
311 // Unpack a 64 bit integer into a string
312 inline utils::CString typeToString(uint64_t v) {
313 uint8_t* raw = (uint8_t*) &v;
314 char str[9];
315 for (size_t i = 0; i < 8; i++) {
316 str[7 - i] = raw[i];
317 }
318 str[8] = '\0';
319 return utils::CString(str, strnlen(str, 8));
320 }
321
322 static void printChunks(ostream& text, const ChunkContainer& container) {
323 text << "Chunks:" << endl;
324
325 text << " " << setw(9) << left << "Name ";
326 text << setw(7) << right << "Size" << endl;
327
328 size_t count = container.getChunkCount();
329 for (size_t i = 0; i < count; i++) {
330 auto chunk = container.getChunk(i);
331 text << " " << typeToString(chunk.type).c_str() << " ";
332 text << setw(7) << right << chunk.desc.size << endl;
333 }
334 }
335
336 static void printShaderInfo(ostream& text, const vector<ShaderInfo>& info,
337 const ChunkContainer& container) {
338 MaterialDomain domain = MaterialDomain::SURFACE;
339 read(container, ChunkType::MaterialDomain, reinterpret_cast<uint8_t*>(&domain));
340 for (uint64_t i = 0; i < info.size(); ++i) {
341 const auto& item = info[i];
342 text << " #";
343 text << setw(4) << left << i;
344 text << setw(6) << left << toString(item.shaderModel);
345 text << " ";
346 text << setw(2) << left << toString(item.pipelineStage);
347 text << " ";
348 text << "0x" << hex << setfill('0') << setw(2)
349 << right << (int) item.variant;
350 text << setfill(' ') << dec;
351 text << " ";
352 text << formatVariantString(item.variant, domain);
353 text << endl;
354 }
355 text << endl;
356 }
357
358 static bool printGlslInfo(ostream& text, const ChunkContainer& container) {
359 vector<ShaderInfo> info;
360 info.resize(getShaderCount(container, ChunkType::MaterialGlsl));
361 if (!getGlShaderInfo(container, info.data())) {
362 return false;
363 }
364 text << "GLSL shaders:" << endl;
365 printShaderInfo(text, info, container);
366 return true;
367 }
368
369 static bool printVkInfo(ostream& text, const ChunkContainer& container) {
370 vector<ShaderInfo> info;
371 info.resize(getShaderCount(container, ChunkType::MaterialSpirv));
372 if (!getVkShaderInfo(container, info.data())) {
373 return false;
374 }
375 text << "Vulkan shaders:" << endl;
376 printShaderInfo(text, info, container);
377 return true;
378 }
379
380 static bool printMetalInfo(ostream& text, const ChunkContainer& container) {
381 vector<ShaderInfo> info;
382 info.resize(getShaderCount(container, ChunkType::MaterialMetal));
383 if (!getMetalShaderInfo(container, info.data())) {
384 return false;
385 }
386 text << "Metal shaders:" << endl;
387 printShaderInfo(text, info, container);
388 return true;
389 }
390
391 bool TextWriter::writeMaterialInfo(const filaflat::ChunkContainer& container) {
392 ostringstream text;
393 if (!printMaterial(text, container)) {
394 return false;
395 }
396 if (!printParametersInfo(text, container)) {
397 return false;
398 }
399 if (!printGlslInfo(text, container)) {
400 return false;
401 }
402 if (!printVkInfo(text, container)) {
403 return false;
404 }
405 if (!printMetalInfo(text, container)) {
406 return false;
407 }
408
409 printChunks(text, container);
410
411 text << endl;
412
413 mTextString = CString(text.str().c_str());
414 return true;
415 }
416
417 const char* TextWriter::getString() const {
418 return mTextString.c_str();
419 }
420
421 size_t TextWriter::getSize() const {
422 return mTextString.size();
423 }
424
425 } // namespace matdbg
426 } // namespace filament