1///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015-2016 The Khronos Group Inc.
4// Copyright (c) 2015-2016 Valve Corporation
5// Copyright (c) 2015-2016 LunarG, Inc.
6// Copyright (c) 2015-2016 Google, Inc.
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and/or associated documentation files (the "Materials"), to
10// deal in the Materials without restriction, including without limitation the
11// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12// sell copies of the Materials, and to permit persons to whom the Materials are
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice(s) and this permission notice shall be included in
16// all copies or substantial portions of the Materials.
17//
18// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21//
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25// USE OR OTHER DEALINGS IN THE MATERIALS.
26///////////////////////////////////////////////////////////////////////////////
27
28#define VK_PROTOTYPES
29#include "vkjson.h"
30
31#include <assert.h>
32#include <stdio.h>
33#include <string.h>
34
35#include <iostream>
36#include <vector>
37
38const uint32_t unsignedNegOne = (uint32_t)(-1);
39
40struct Options {
41  bool instance = false;
42  uint32_t device_index = unsignedNegOne;
43  std::string device_name;
44  std::string output_file;
45};
46
47bool ParseOptions(int argc, char* argv[], Options* options) {
48  for (int i = 1; i < argc; ++i) {
49    std::string arg(argv[i]);
50    if (arg == "--instance" || arg == "-i") {
51      options->instance = true;
52    } else if (arg == "--first" || arg == "-f") {
53      options->device_index = 0;
54    } else {
55      ++i;
56      if (i >= argc) {
57        std::cerr << "Missing parameter after: " << arg << std::endl;
58        return false;
59      }
60      std::string arg2(argv[i]);
61      if (arg == "--device-index" || arg == "-d") {
62        int result = sscanf(arg2.c_str(), "%u", &options->device_index);
63        if (result != 1) {
64          options->device_index = -1;
65          std::cerr << "Unable to parse index: " << arg2 << std::endl;
66          return false;
67        }
68      } else if (arg == "--device-name" || arg == "-n") {
69        options->device_name = arg2;
70      } else if (arg == "--output" || arg == "-o") {
71        options->output_file = arg2;
72      } else {
73        std::cerr << "Unknown argument: " << arg << std::endl;
74        return false;
75      }
76    }
77  }
78  if (options->instance && (options->device_index != unsignedNegOne ||
79                            !options->device_name.empty())) {
80    std::cerr << "Specifying a specific device is incompatible with dumping "
81                 "the whole instance." << std::endl;
82    return false;
83  }
84  if (options->device_index != unsignedNegOne && !options->device_name.empty()) {
85    std::cerr << "Must specify only one of device index and device name."
86              << std::endl;
87    return false;
88  }
89  if (options->instance && options->output_file.empty()) {
90    std::cerr << "Must specify an output file when dumping the whole instance."
91              << std::endl;
92    return false;
93  }
94  if (!options->output_file.empty() && !options->instance &&
95      options->device_index == unsignedNegOne && options->device_name.empty()) {
96    std::cerr << "Must specify instance, device index, or device name when "
97                 "specifying "
98                 "output file." << std::endl;
99    return false;
100  }
101  return true;
102}
103
104bool Dump(const VkJsonInstance& instance, const Options& options) {
105  const VkJsonDevice* out_device = nullptr;
106  if (options.device_index != unsignedNegOne) {
107    if (static_cast<uint32_t>(options.device_index) >=
108        instance.devices.size()) {
109      std::cerr << "Error: device " << options.device_index
110                << " requested but only " << instance.devices.size()
111                << " devices found." << std::endl;
112      return false;
113    }
114    out_device = &instance.devices[options.device_index];
115  } else if (!options.device_name.empty()) {
116    for (const auto& device : instance.devices) {
117      if (device.properties.deviceName == options.device_name) {
118        out_device = &device;
119      }
120    }
121    if (!out_device) {
122      std::cerr << "Error: device '" << options.device_name
123                << "' requested but not found." << std::endl;
124      return false;
125    }
126  }
127
128  std::string output_file;
129  if (options.output_file.empty()) {
130    assert(out_device);
131    output_file.assign(out_device->properties.deviceName);
132    output_file.append(".json");
133  } else {
134    output_file = options.output_file;
135  }
136  FILE* file = nullptr;
137  if (output_file == "-") {
138    file = stdout;
139  } else {
140    file = fopen(output_file.c_str(), "w");
141    if (!file) {
142      std::cerr << "Unable to open file " << output_file << "." << std::endl;
143      return false;
144    }
145  }
146
147  std::string json = out_device ? VkJsonDeviceToJson(*out_device)
148                                : VkJsonInstanceToJson(instance);
149  fwrite(json.data(), 1, json.size(), file);
150  fputc('\n', file);
151
152  if (output_file != "-") {
153    fclose(file);
154    std::cout << "Wrote file " << output_file;
155    if (out_device)
156      std::cout << " for device " << out_device->properties.deviceName;
157    std::cout << "." << std::endl;
158  }
159  return true;
160}
161
162int main(int argc, char* argv[]) {
163  Options options;
164  if (!ParseOptions(argc, argv, &options))
165    return 1;
166
167  VkJsonInstance instance = VkJsonGetInstance();
168  if (options.instance || options.device_index != unsignedNegOne ||
169      !options.device_name.empty()) {
170    Dump(instance, options);
171  } else {
172    for (uint32_t i = 0, n = instance.devices.size(); i < n; i++) {
173      options.device_index = i;
174      Dump(instance, options);
175    }
176  }
177
178  return 0;
179}
180