• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 //     http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 ///////////////////////////////////////////////////////////////////////////////
20 
21 #define VK_PROTOTYPES
22 #include "vkjson.h"
23 
24 #include <assert.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include <iostream>
29 #include <vector>
30 
31 const uint32_t unsignedNegOne = (uint32_t)(-1);
32 
33 struct Options {
34   bool instance = false;
35   uint32_t device_index = unsignedNegOne;
36   std::string device_name;
37   std::string output_file;
38 };
39 
ParseOptions(int argc,char * argv[],Options * options)40 bool ParseOptions(int argc, char* argv[], Options* options) {
41   for (int i = 1; i < argc; ++i) {
42     std::string arg(argv[i]);
43     if (arg == "--instance" || arg == "-i") {
44       options->instance = true;
45     } else if (arg == "--first" || arg == "-f") {
46       options->device_index = 0;
47     } else {
48       ++i;
49       if (i >= argc) {
50         std::cerr << "Missing parameter after: " << arg << std::endl;
51         return false;
52       }
53       std::string arg2(argv[i]);
54       if (arg == "--device-index" || arg == "-d") {
55         int result = sscanf(arg2.c_str(), "%u", &options->device_index);
56         if (result != 1) {
57           options->device_index = -1;
58           std::cerr << "Unable to parse index: " << arg2 << std::endl;
59           return false;
60         }
61       } else if (arg == "--device-name" || arg == "-n") {
62         options->device_name = arg2;
63       } else if (arg == "--output" || arg == "-o") {
64         options->output_file = arg2;
65       } else {
66         std::cerr << "Unknown argument: " << arg << std::endl;
67         return false;
68       }
69     }
70   }
71   if (options->instance && (options->device_index != unsignedNegOne ||
72                             !options->device_name.empty())) {
73     std::cerr << "Specifying a specific device is incompatible with dumping "
74                  "the whole instance." << std::endl;
75     return false;
76   }
77   if (options->device_index != unsignedNegOne && !options->device_name.empty()) {
78     std::cerr << "Must specify only one of device index and device name."
79               << std::endl;
80     return false;
81   }
82   if (options->instance && options->output_file.empty()) {
83     std::cerr << "Must specify an output file when dumping the whole instance."
84               << std::endl;
85     return false;
86   }
87   if (!options->output_file.empty() && !options->instance &&
88       options->device_index == unsignedNegOne && options->device_name.empty()) {
89     std::cerr << "Must specify instance, device index, or device name when "
90                  "specifying "
91                  "output file." << std::endl;
92     return false;
93   }
94   return true;
95 }
96 
Dump(const VkJsonInstance & instance,const Options & options)97 bool Dump(const VkJsonInstance& instance, const Options& options) {
98   const VkJsonDevice* out_device = nullptr;
99   if (options.device_index != unsignedNegOne) {
100     if (static_cast<uint32_t>(options.device_index) >=
101         instance.devices.size()) {
102       std::cerr << "Error: device " << options.device_index
103                 << " requested but only " << instance.devices.size()
104                 << " devices found." << std::endl;
105       return false;
106     }
107     out_device = &instance.devices[options.device_index];
108   } else if (!options.device_name.empty()) {
109     for (const auto& device : instance.devices) {
110       if (device.properties.deviceName == options.device_name) {
111         out_device = &device;
112       }
113     }
114     if (!out_device) {
115       std::cerr << "Error: device '" << options.device_name
116                 << "' requested but not found." << std::endl;
117       return false;
118     }
119   }
120 
121   std::string output_file;
122   if (options.output_file.empty()) {
123     assert(out_device);
124     output_file.assign(out_device->properties.deviceName);
125     output_file.append(".json");
126   } else {
127     output_file = options.output_file;
128   }
129   FILE* file = nullptr;
130   if (output_file == "-") {
131     file = stdout;
132   } else {
133     file = fopen(output_file.c_str(), "w");
134     if (!file) {
135       std::cerr << "Unable to open file " << output_file << "." << std::endl;
136       return false;
137     }
138   }
139 
140   std::string json = out_device ? VkJsonDeviceToJson(*out_device)
141                                 : VkJsonInstanceToJson(instance);
142   fwrite(json.data(), 1, json.size(), file);
143   fputc('\n', file);
144 
145   if (output_file != "-") {
146     fclose(file);
147     std::cout << "Wrote file " << output_file;
148     if (out_device)
149       std::cout << " for device " << out_device->properties.deviceName;
150     std::cout << "." << std::endl;
151   }
152   return true;
153 }
154 
main(int argc,char * argv[])155 int main(int argc, char* argv[]) {
156   Options options;
157   if (!ParseOptions(argc, argv, &options))
158     return 1;
159 
160   VkJsonInstance instance = VkJsonGetInstance();
161   if (options.instance || options.device_index != unsignedNegOne ||
162       !options.device_name.empty()) {
163     Dump(instance, options);
164   } else {
165     for (uint32_t i = 0, n = static_cast<uint32_t>(instance.devices.size()); i < n; i++) {
166       options.device_index = i;
167       Dump(instance, options);
168     }
169   }
170 
171   return 0;
172 }
173