1 // Copyright 2018 The Amber 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 "amber/amber.h"
16
17 #include <stdio.h>
18
19 #include <algorithm>
20 #include <cassert>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <fstream>
24 #include <iomanip>
25 #include <iostream>
26 #include <set>
27 #include <string>
28 #include <utility>
29 #include <vector>
30
31 #include "amber/recipe.h"
32 #include "samples/config_helper.h"
33 #include "samples/ppm.h"
34 #include "samples/timestamp.h"
35 #include "src/build-versions.h"
36 #include "src/make_unique.h"
37
38 #if AMBER_ENABLE_SPIRV_TOOLS
39 #include "spirv-tools/libspirv.hpp"
40 #endif
41
42 #if AMBER_ENABLE_LODEPNG
43 #include "samples/png.h"
44 #endif // AMBER_ENABLE_LODEPNG
45
46 namespace {
47
48 const char* kGeneratedColorBuffer = "framebuffer";
49
50 struct Options {
51 std::vector<std::string> input_filenames;
52
53 std::vector<std::string> image_filenames;
54 std::string buffer_filename;
55 std::vector<std::string> fb_names;
56 std::vector<amber::BufferInfo> buffer_to_dump;
57 uint32_t engine_major = 1;
58 uint32_t engine_minor = 0;
59 int32_t fence_timeout = -1;
60 int32_t selected_device = -1;
61 bool parse_only = false;
62 bool pipeline_create_only = false;
63 bool disable_validation_layer = false;
64 bool quiet = false;
65 bool show_help = false;
66 bool show_version_info = false;
67 bool log_graphics_calls = false;
68 bool log_graphics_calls_time = false;
69 bool log_execute_calls = false;
70 bool disable_spirv_validation = false;
71 std::string shader_filename;
72 amber::EngineType engine = amber::kEngineTypeVulkan;
73 std::string spv_env;
74 };
75
76 const char kUsage[] = R"(Usage: amber [options] SCRIPT [SCRIPTS...]
77
78 options:
79 -p -- Parse input files only; Don't execute.
80 -ps -- Parse input files, create pipelines; Don't execute.
81 -q -- Disable summary output.
82 -d -- Disable validation layers.
83 -D <ID> -- ID of device to run with (Vulkan only).
84 -f <value> -- Sets the fence timeout value to |value|
85 -t <spirv_env> -- The target SPIR-V environment e.g., spv1.3, vulkan1.1, vulkan1.2.
86 If a SPIR-V environment, assume the lowest version of Vulkan that
87 requires support of that version of SPIR-V.
88 If a Vulkan environment, use the highest version of SPIR-V required
89 to be supported by that version of Vulkan.
90 Use vulkan1.1spv1.4 for SPIR-V 1.4 with Vulkan 1.1.
91 Defaults to spv1.0.
92 -i <filename> -- Write rendering to <filename> as a PNG image if it ends with '.png',
93 or as a PPM image otherwise.
94 -I <buffername> -- Name of framebuffer to dump. Defaults to 'framebuffer'.
95 -b <filename> -- Write contents of a UBO or SSBO to <filename>.
96 -B [<pipeline name>:][<desc set>:]<binding> -- Identifier of buffer to write.
97 Default is [first pipeline:][0:]0.
98 -w <filename> -- Write shader assembly to |filename|
99 -e <engine> -- Specify graphics engine: vulkan, dawn. Default is vulkan.
100 -v <engine version> -- Engine version (eg, 1.1 for Vulkan). Default 1.0.
101 -V, --version -- Output version information for Amber and libraries.
102 --log-graphics-calls -- Log graphics API calls (only for Vulkan so far).
103 --log-graphics-calls-time -- Log timing of graphics API calls timing (Vulkan only).
104 --log-execute-calls -- Log each execute call before run.
105 --disable-spirv-val -- Disable SPIR-V validation.
106 -h -- This help text.
107 )";
108
109 // Parses a decimal integer from the given string, and writes it to |retval|.
110 // Returns true if parsing succeeded and consumed the whole string.
ParseOneInt(const char * str,int * retval)111 static bool ParseOneInt(const char* str, int* retval) {
112 char trailing = 0;
113 #if defined(_MSC_VER)
114 return sscanf_s(str, "%d%c", retval, &trailing, 1) == 1;
115 #else
116 return std::sscanf(str, "%d%c", retval, &trailing) == 1;
117 #endif
118 }
119
120 // Parses a decimal integer, then a period (.), then a decimal integer from the
121 // given string, and writes it to |retval|. Returns true if parsing succeeded
122 // and consumed the whole string.
ParseIntDotInt(const char * str,int * retval0,int * retval1)123 static int ParseIntDotInt(const char* str, int* retval0, int* retval1) {
124 char trailing = 0;
125 #if defined(_MSC_VER)
126 return sscanf_s(str, "%d.%d%c", retval0, retval1, &trailing, 1) == 2;
127 #else
128 return std::sscanf(str, "%d.%d%c", retval0, retval1, &trailing) == 2;
129 #endif
130 }
131
ParseArgs(const std::vector<std::string> & args,Options * opts)132 bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
133 for (size_t i = 1; i < args.size(); ++i) {
134 const std::string& arg = args[i];
135 if (arg == "-i") {
136 ++i;
137 if (i >= args.size()) {
138 std::cerr << "Missing value for -i argument." << std::endl;
139 return false;
140 }
141 opts->image_filenames.push_back(args[i]);
142
143 } else if (arg == "-I") {
144 ++i;
145 if (i >= args.size()) {
146 std::cerr << "Missing value for -I argument." << std::endl;
147 return false;
148 }
149 opts->fb_names.push_back(args[i]);
150
151 } else if (arg == "-b") {
152 ++i;
153 if (i >= args.size()) {
154 std::cerr << "Missing value for -b argument." << std::endl;
155 return false;
156 }
157 opts->buffer_filename = args[i];
158
159 } else if (arg == "-B") {
160 ++i;
161 if (i >= args.size()) {
162 std::cerr << "Missing value for -B argument." << std::endl;
163 return false;
164 }
165 opts->buffer_to_dump.emplace_back();
166 opts->buffer_to_dump.back().buffer_name = args[i];
167 } else if (arg == "-w") {
168 ++i;
169 if (i >= args.size()) {
170 std::cerr << "Missing value for -w argument." << std::endl;
171 return false;
172 }
173 opts->shader_filename = args[i];
174 } else if (arg == "-e") {
175 ++i;
176 if (i >= args.size()) {
177 std::cerr << "Missing value for -e argument." << std::endl;
178 return false;
179 }
180 const std::string& engine = args[i];
181 if (engine == "vulkan") {
182 opts->engine = amber::kEngineTypeVulkan;
183 } else if (engine == "dawn") {
184 opts->engine = amber::kEngineTypeDawn;
185 } else {
186 std::cerr
187 << "Invalid value for -e argument. Must be one of: vulkan dawn"
188 << std::endl;
189 return false;
190 }
191 } else if (arg == "-D") {
192 ++i;
193 if (i >= args.size()) {
194 std::cerr << "Missing ID for -D argument." << std::endl;
195 return false;
196 }
197
198 int32_t val = 0;
199 if (!ParseOneInt(args[i].c_str(), &val)) {
200 std::cerr << "Invalid device ID: " << args[i] << std::endl;
201 return false;
202 }
203 if (val < 0) {
204 std::cerr << "Device ID must be non-negative" << std::endl;
205 return false;
206 }
207 opts->selected_device = val;
208
209 } else if (arg == "-f") {
210 ++i;
211 if (i >= args.size()) {
212 std::cerr << "Missing value for -f argument." << std::endl;
213 return false;
214 }
215
216 int32_t val = 0;
217 if (!ParseOneInt(args[i].c_str(), &val)) {
218 std::cerr << "Invalid fence timeout: " << args[i] << std::endl;
219 return false;
220 }
221 if (val < 0) {
222 std::cerr << "Fence timeout must be non-negative" << std::endl;
223 return false;
224 }
225 opts->fence_timeout = val;
226
227 } else if (arg == "-t") {
228 ++i;
229 if (i >= args.size()) {
230 std::cerr << "Missing value for -t argument." << std::endl;
231 return false;
232 }
233 opts->spv_env = args[i];
234 } else if (arg == "-h" || arg == "--help") {
235 opts->show_help = true;
236 } else if (arg == "-v") {
237 ++i;
238 if (i >= args.size()) {
239 std::cerr << "Missing value for -v argument." << std::endl;
240 return false;
241 }
242 const std::string& ver = std::string(args[i]);
243
244 int32_t major = 0;
245 int32_t minor = 0;
246 if (ParseIntDotInt(ver.c_str(), &major, &minor) ||
247 ParseOneInt(ver.c_str(), &major)) {
248 if (major < 0) {
249 std::cerr << "Version major must be non-negative" << std::endl;
250 return false;
251 }
252 if (minor < 0) {
253 std::cerr << "Version minor must be non-negative" << std::endl;
254 return false;
255 }
256 opts->engine_major = static_cast<uint32_t>(major);
257 opts->engine_minor = static_cast<uint32_t>(minor);
258 } else {
259 std::cerr << "Invalid engine version number: " << ver << std::endl;
260 return false;
261 }
262 } else if (arg == "-V" || arg == "--version") {
263 opts->show_version_info = true;
264 } else if (arg == "-p") {
265 opts->parse_only = true;
266 } else if (arg == "-ps") {
267 opts->pipeline_create_only = true;
268 } else if (arg == "-d") {
269 opts->disable_validation_layer = true;
270 } else if (arg == "-s") {
271 // -s is deprecated but still recognized, it inverts the quiet flag.
272 opts->quiet = false;
273 } else if (arg == "-q") {
274 opts->quiet = true;
275 } else if (arg == "--log-graphics-calls") {
276 opts->log_graphics_calls = true;
277 } else if (arg == "--log-graphics-calls-time") {
278 opts->log_graphics_calls_time = true;
279 } else if (arg == "--log-execute-calls") {
280 opts->log_execute_calls = true;
281 } else if (arg == "--disable-spirv-val") {
282 opts->disable_spirv_validation = true;
283 } else if (arg.size() > 0 && arg[0] == '-') {
284 std::cerr << "Unrecognized option " << arg << std::endl;
285 return false;
286 } else if (!arg.empty()) {
287 opts->input_filenames.push_back(arg);
288 }
289 }
290
291 return true;
292 }
293
ReadFile(const std::string & input_file)294 std::vector<char> ReadFile(const std::string& input_file) {
295 FILE* file = nullptr;
296 #if defined(_MSC_VER)
297 fopen_s(&file, input_file.c_str(), "rb");
298 #else
299 file = fopen(input_file.c_str(), "rb");
300 #endif
301 if (!file) {
302 std::cerr << "Failed to open " << input_file << std::endl;
303 return {};
304 }
305
306 fseek(file, 0, SEEK_END);
307 uint64_t tell_file_size = static_cast<uint64_t>(ftell(file));
308 if (tell_file_size <= 0) {
309 std::cerr << "Input file of incorrect size: " << input_file << std::endl;
310 fclose(file);
311 return {};
312 }
313 fseek(file, 0, SEEK_SET);
314
315 size_t file_size = static_cast<size_t>(tell_file_size);
316
317 std::vector<char> data;
318 data.resize(file_size);
319
320 size_t bytes_read = fread(data.data(), sizeof(char), file_size, file);
321 fclose(file);
322 if (bytes_read != file_size) {
323 std::cerr << "Failed to read " << input_file << std::endl;
324 return {};
325 }
326
327 return data;
328 }
329
330 class SampleDelegate : public amber::Delegate {
331 public:
332 SampleDelegate() = default;
333 ~SampleDelegate() override = default;
334
Log(const std::string & message)335 void Log(const std::string& message) override {
336 std::cout << message << std::endl;
337 }
338
LogGraphicsCalls() const339 bool LogGraphicsCalls() const override { return log_graphics_calls_; }
SetLogGraphicsCalls(bool log_graphics_calls)340 void SetLogGraphicsCalls(bool log_graphics_calls) {
341 log_graphics_calls_ = log_graphics_calls;
342 }
343
LogExecuteCalls() const344 bool LogExecuteCalls() const override { return log_execute_calls_; }
SetLogExecuteCalls(bool log_execute_calls)345 void SetLogExecuteCalls(bool log_execute_calls) {
346 log_execute_calls_ = log_execute_calls;
347 }
348
LogGraphicsCallsTime() const349 bool LogGraphicsCallsTime() const override {
350 return log_graphics_calls_time_;
351 }
SetLogGraphicsCallsTime(bool log_graphics_calls_time)352 void SetLogGraphicsCallsTime(bool log_graphics_calls_time) {
353 log_graphics_calls_time_ = log_graphics_calls_time;
354 if (log_graphics_calls_time) {
355 // Make sure regular logging is also enabled
356 log_graphics_calls_ = true;
357 }
358 }
359
GetTimestampNs() const360 uint64_t GetTimestampNs() const override {
361 return timestamp::SampleGetTimestampNs();
362 }
363
SetScriptPath(std::string path)364 void SetScriptPath(std::string path) { path_ = path; }
365
LoadBufferData(const std::string file_name,amber::BufferDataFileType file_type,amber::BufferInfo * buffer) const366 amber::Result LoadBufferData(const std::string file_name,
367 amber::BufferDataFileType file_type,
368 amber::BufferInfo* buffer) const override {
369 if (file_type == amber::BufferDataFileType::kPng) {
370 #if AMBER_ENABLE_LODEPNG
371 return png::LoadPNG(path_ + file_name, &buffer->width, &buffer->height,
372 &buffer->values);
373 #else
374 return amber::Result("PNG support is not enabled in compile options.");
375 #endif // AMBER_ENABLE_LODEPNG
376 } else {
377 auto data = ReadFile(path_ + file_name);
378 if (data.empty())
379 return amber::Result("Failed to load buffer data " + file_name);
380
381 for (auto d : data) {
382 amber::Value v;
383 v.SetIntValue(static_cast<uint64_t>(d));
384 buffer->values.push_back(v);
385 }
386
387 buffer->width = 1;
388 buffer->height = 1;
389 }
390
391 return {};
392 }
393
394 private:
395 bool log_graphics_calls_ = false;
396 bool log_graphics_calls_time_ = false;
397 bool log_execute_calls_ = false;
398 std::string path_ = "";
399 };
400
disassemble(const std::string & env,const std::vector<uint32_t> & data)401 std::string disassemble(const std::string& env,
402 const std::vector<uint32_t>& data) {
403 #if AMBER_ENABLE_SPIRV_TOOLS
404 std::string spv_errors;
405
406 spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0;
407 if (!env.empty()) {
408 if (!spvParseTargetEnv(env.c_str(), &target_env))
409 return "";
410 }
411
412 auto msg_consumer = [&spv_errors](spv_message_level_t level, const char*,
413 const spv_position_t& position,
414 const char* message) {
415 switch (level) {
416 case SPV_MSG_FATAL:
417 case SPV_MSG_INTERNAL_ERROR:
418 case SPV_MSG_ERROR:
419 spv_errors += "error: line " + std::to_string(position.index) + ": " +
420 message + "\n";
421 break;
422 case SPV_MSG_WARNING:
423 spv_errors += "warning: line " + std::to_string(position.index) + ": " +
424 message + "\n";
425 break;
426 case SPV_MSG_INFO:
427 spv_errors += "info: line " + std::to_string(position.index) + ": " +
428 message + "\n";
429 break;
430 case SPV_MSG_DEBUG:
431 break;
432 }
433 };
434
435 spvtools::SpirvTools tools(target_env);
436 tools.SetMessageConsumer(msg_consumer);
437
438 std::string result;
439 tools.Disassemble(data, &result,
440 SPV_BINARY_TO_TEXT_OPTION_INDENT |
441 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
442 return result;
443 #else
444 return "";
445 #endif // AMBER_ENABLE_SPIRV_TOOLS
446 }
447
448 } // namespace
449
main(int argc,const char ** argv)450 int main(int argc, const char** argv) {
451 std::vector<std::string> args(argv, argv + argc);
452 Options options;
453 SampleDelegate delegate;
454
455 if (!ParseArgs(args, &options)) {
456 std::cerr << "Failed to parse arguments." << std::endl;
457 return 1;
458 }
459
460 if (options.show_version_info) {
461 std::cout << "Amber : " << AMBER_VERSION << std::endl;
462 #if AMBER_ENABLE_SPIRV_TOOLS
463 std::cout << "SPIRV-Tools : " << SPIRV_TOOLS_VERSION << std::endl;
464 std::cout << "SPIRV-Headers: " << SPIRV_HEADERS_VERSION << std::endl;
465 #endif // AMBER_ENABLE_SPIRV_TOOLS
466 #if AMBER_ENABLE_SHADERC
467 std::cout << "GLSLang : " << GLSLANG_VERSION << std::endl;
468 std::cout << "Shaderc : " << SHADERC_VERSION << std::endl;
469 #endif // AMBER_ENABLE_SHADERC
470 }
471
472 if (options.show_help) {
473 std::cout << kUsage << std::endl;
474 return 0;
475 }
476
477 amber::Result result;
478 std::vector<std::string> failures;
479 struct RecipeData {
480 std::string file;
481 std::unique_ptr<amber::Recipe> recipe;
482 };
483 std::vector<RecipeData> recipe_data;
484 for (const auto& file : options.input_filenames) {
485 auto char_data = ReadFile(file);
486 auto data = std::string(char_data.begin(), char_data.end());
487 if (data.empty()) {
488 std::cerr << file << " is empty." << std::endl;
489 failures.push_back(file);
490 continue;
491 }
492
493 // Parse file path and set it for delegate to use when loading buffer data.
494 delegate.SetScriptPath(file.substr(0, file.find_last_of("/\\") + 1));
495
496 amber::Amber am(&delegate);
497 std::unique_ptr<amber::Recipe> recipe = amber::MakeUnique<amber::Recipe>();
498
499 result = am.Parse(data, recipe.get());
500 if (!result.IsSuccess()) {
501 std::cerr << file << ": " << result.Error() << std::endl;
502 failures.push_back(file);
503 continue;
504 }
505
506 if (options.fence_timeout > -1)
507 recipe->SetFenceTimeout(static_cast<uint32_t>(options.fence_timeout));
508
509 recipe_data.emplace_back();
510 recipe_data.back().file = file;
511 recipe_data.back().recipe = std::move(recipe);
512 }
513
514 if (options.parse_only)
515 return 0;
516
517 if (options.log_graphics_calls)
518 delegate.SetLogGraphicsCalls(true);
519 if (options.log_graphics_calls_time)
520 delegate.SetLogGraphicsCallsTime(true);
521 if (options.log_execute_calls)
522 delegate.SetLogExecuteCalls(true);
523
524 amber::Options amber_options;
525 amber_options.engine = options.engine;
526 amber_options.spv_env = options.spv_env;
527 amber_options.execution_type = options.pipeline_create_only
528 ? amber::ExecutionType::kPipelineCreateOnly
529 : amber::ExecutionType::kExecute;
530 amber_options.disable_spirv_validation = options.disable_spirv_validation;
531
532 std::set<std::string> required_features;
533 std::set<std::string> required_device_extensions;
534 std::set<std::string> required_instance_extensions;
535 for (const auto& recipe_data_elem : recipe_data) {
536 const auto features = recipe_data_elem.recipe->GetRequiredFeatures();
537 required_features.insert(features.begin(), features.end());
538
539 const auto device_extensions =
540 recipe_data_elem.recipe->GetRequiredDeviceExtensions();
541 required_device_extensions.insert(device_extensions.begin(),
542 device_extensions.end());
543
544 const auto inst_extensions =
545 recipe_data_elem.recipe->GetRequiredInstanceExtensions();
546 required_instance_extensions.insert(inst_extensions.begin(),
547 inst_extensions.end());
548 }
549
550 sample::ConfigHelper config_helper;
551 std::unique_ptr<amber::EngineConfig> config;
552
553 amber::Result r = config_helper.CreateConfig(
554 amber_options.engine, options.engine_major, options.engine_minor,
555 options.selected_device,
556 std::vector<std::string>(required_features.begin(),
557 required_features.end()),
558 std::vector<std::string>(required_instance_extensions.begin(),
559 required_instance_extensions.end()),
560 std::vector<std::string>(required_device_extensions.begin(),
561 required_device_extensions.end()),
562 options.disable_validation_layer, options.show_version_info, &config);
563
564 if (!r.IsSuccess()) {
565 std::cout << r.Error() << std::endl;
566 return 1;
567 }
568
569 amber_options.config = config.get();
570
571 if (!options.buffer_filename.empty()) {
572 // Have a filename to dump, but no explicit buffer, set the default of 0:0.
573 if (options.buffer_to_dump.empty()) {
574 options.buffer_to_dump.emplace_back();
575 options.buffer_to_dump.back().buffer_name = "0:0";
576 }
577
578 amber_options.extractions.insert(amber_options.extractions.end(),
579 options.buffer_to_dump.begin(),
580 options.buffer_to_dump.end());
581 }
582
583 if (options.image_filenames.size() - options.fb_names.size() > 1) {
584 std::cerr << "Need to specify framebuffer names using -I for each output "
585 "image specified by -i."
586 << std::endl;
587 return 1;
588 }
589
590 // Use default frame buffer name when not specified.
591 while (options.image_filenames.size() > options.fb_names.size())
592 options.fb_names.push_back(kGeneratedColorBuffer);
593
594 for (const auto& fb_name : options.fb_names) {
595 amber::BufferInfo buffer_info;
596 buffer_info.buffer_name = fb_name;
597 buffer_info.is_image_buffer = true;
598 amber_options.extractions.push_back(buffer_info);
599 }
600
601 for (const auto& recipe_data_elem : recipe_data) {
602 const auto* recipe = recipe_data_elem.recipe.get();
603 const auto& file = recipe_data_elem.file;
604
605 amber::Amber am(&delegate);
606 result = am.Execute(recipe, &amber_options);
607 if (!result.IsSuccess()) {
608 std::cerr << file << ": " << result.Error() << std::endl;
609 failures.push_back(file);
610 // Note, we continue after failure to allow dumping the buffers which may
611 // give clues as to the failure.
612 }
613
614 // Dump the shader assembly
615 if (!options.shader_filename.empty()) {
616 #if AMBER_ENABLE_SPIRV_TOOLS
617 std::ofstream shader_file;
618 shader_file.open(options.shader_filename, std::ios::out);
619 if (!shader_file.is_open()) {
620 std::cerr << "Cannot open file for shader dump: ";
621 std::cerr << options.shader_filename << std::endl;
622 } else {
623 auto info = recipe->GetShaderInfo();
624 for (const auto& sh : info) {
625 shader_file << ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
626 << std::endl;
627 shader_file << "; " << sh.shader_name << std::endl
628 << ";" << std::endl;
629 shader_file << disassemble(options.spv_env, sh.shader_data)
630 << std::endl;
631 }
632 shader_file.close();
633 }
634 #endif // AMBER_ENABLE_SPIRV_TOOLS
635 }
636
637 for (size_t i = 0; i < options.image_filenames.size(); ++i) {
638 std::vector<uint8_t> out_buf;
639 auto image_filename = options.image_filenames[i];
640 auto pos = image_filename.find_last_of('.');
641 bool usePNG =
642 pos != std::string::npos && image_filename.substr(pos + 1) == "png";
643 for (const amber::BufferInfo& buffer_info : amber_options.extractions) {
644 if (buffer_info.buffer_name == options.fb_names[i]) {
645 if (buffer_info.values.size() !=
646 (buffer_info.width * buffer_info.height)) {
647 result = amber::Result(
648 "Framebuffer (" + buffer_info.buffer_name + ") size (" +
649 std::to_string(buffer_info.values.size()) +
650 ") != " + "width * height (" +
651 std::to_string(buffer_info.width * buffer_info.height) + ")");
652 break;
653 }
654
655 if (buffer_info.values.empty()) {
656 result = amber::Result("Framebuffer (" + buffer_info.buffer_name +
657 ") empty or non-existent.");
658 break;
659 }
660
661 if (usePNG) {
662 #if AMBER_ENABLE_LODEPNG
663 result = png::ConvertToPNG(buffer_info.width, buffer_info.height,
664 buffer_info.values, &out_buf);
665 #else // AMBER_ENABLE_LODEPNG
666 result = amber::Result("PNG support not enabled");
667 #endif // AMBER_ENABLE_LODEPNG
668 } else {
669 ppm::ConvertToPPM(buffer_info.width, buffer_info.height,
670 buffer_info.values, &out_buf);
671 result = {};
672 }
673 break;
674 }
675 }
676 if (result.IsSuccess()) {
677 std::ofstream image_file;
678 image_file.open(image_filename, std::ios::out | std::ios::binary);
679 if (!image_file.is_open()) {
680 std::cerr << "Cannot open file for image dump: ";
681 std::cerr << image_filename << std::endl;
682 continue;
683 }
684 image_file << std::string(out_buf.begin(), out_buf.end());
685 image_file.close();
686 } else {
687 std::cerr << result.Error() << std::endl;
688 }
689 }
690
691 if (!options.buffer_filename.empty()) {
692 std::ofstream buffer_file;
693 buffer_file.open(options.buffer_filename, std::ios::out);
694 if (!buffer_file.is_open()) {
695 std::cerr << "Cannot open file for buffer dump: ";
696 std::cerr << options.buffer_filename << std::endl;
697 } else {
698 for (const amber::BufferInfo& buffer_info : amber_options.extractions) {
699 // Skip frame buffers.
700 if (std::any_of(options.fb_names.begin(), options.fb_names.end(),
701 [&](std::string s) {
702 return s == buffer_info.buffer_name;
703 }) ||
704 buffer_info.buffer_name == kGeneratedColorBuffer) {
705 continue;
706 }
707
708 buffer_file << buffer_info.buffer_name << std::endl;
709 const auto& values = buffer_info.values;
710 for (size_t i = 0; i < values.size(); ++i) {
711 buffer_file << " " << std::setfill('0') << std::setw(2) << std::hex
712 << values[i].AsUint32();
713 if (i % 16 == 15)
714 buffer_file << std::endl;
715 }
716 buffer_file << std::endl;
717 }
718 buffer_file.close();
719 }
720 }
721 }
722
723 if (!options.quiet) {
724 if (!failures.empty()) {
725 std::cout << "\nSummary of Failures:" << std::endl;
726
727 for (const auto& failure : failures)
728 std::cout << " " << failure << std::endl;
729 }
730
731 std::cout << "\nSummary: "
732 << (options.input_filenames.size() - failures.size()) << " pass, "
733 << failures.size() << " fail" << std::endl;
734 }
735
736 return !failures.empty();
737 }
738