blob: afa982050d7b12ecd5fdf31218fb762ab88af874 [file] [log] [blame]
Antoine Laboure3795c12015-10-27 12:21:09 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following disclaimer
11// in the documentation and/or other materials provided with the
12// distribution.
13// * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#define VK_PROTOTYPES
30#include "vkjson.h"
31
32#include <stdio.h>
33#include <string.h>
34
35#include <iostream>
36#include <vector>
37
Mark Young8928c062016-01-25 13:37:06 -070038const uint32_t unsignedNegOne = (uint32_t)(-1);
39
Antoine Laboure3795c12015-10-27 12:21:09 -070040struct Options {
Mark Young8928c062016-01-25 13:37:06 -070041 uint32_t device_index = unsignedNegOne;
Antoine Laboure3795c12015-10-27 12:21:09 -070042 std::string device_name;
43 std::string output_file;
44};
45
46bool ParseOptions(int argc, char* argv[], Options* options) {
47 for (int i = 1; i < argc; ++i) {
48 std::string arg(argv[i]);
49 if (arg == "--first" || arg == "-f") {
50 options->device_index = 0;
51 } else {
52 ++i;
53 if (i >= argc) {
54 std::cerr << "Missing parameter after: " << arg << std::endl;
55 return false;
56 }
57 std::string arg2(argv[i]);
58 if (arg == "--device-index" || arg == "-d") {
59 int result = sscanf(arg2.c_str(), "%u", &options->device_index);
60 if (result != 1) {
61 options->device_index = -1;
62 std::cerr << "Unable to parse index: " << arg2 << std::endl;
63 return false;
64 }
65 } else if (arg == "--device-name" || arg == "-n") {
66 options->device_name = arg2;
67 } else if (arg == "--output" || arg == "-o") {
68 options->output_file = arg2;
69 } else {
70 std::cerr << "Unknown argument: " << arg << std::endl;
71 return false;
72 }
73 }
74 }
Mark Young8928c062016-01-25 13:37:06 -070075 if (options->device_index != unsignedNegOne && !options->device_name.empty()) {
Antoine Laboure3795c12015-10-27 12:21:09 -070076 std::cerr << "Must specify only one of device index and device name."
77 << std::endl;
78 return false;
79 }
Mark Young8928c062016-01-25 13:37:06 -070080 if (!options->output_file.empty() && options->device_index == unsignedNegOne &&
Antoine Laboure3795c12015-10-27 12:21:09 -070081 options->device_name.empty()) {
82 std::cerr << "Must specify device index or device name when specifying "
83 "output file"
84 << std::endl;
85 return false;
86 }
87 return true;
88}
89
90bool DumpProperties(const VkJsonAllProperties& props, const Options& options) {
91 std::string device_name(props.properties.deviceName);
92 std::string output_file = options.output_file;
93 if (output_file.empty())
94 output_file = device_name + ".json";
95 FILE* file = nullptr;
96 if (output_file == "-") {
97 file = stdout;
98 } else {
99 file = fopen(output_file.c_str(), "w");
100 if (!file) {
101 std::cerr << "Unable to open file " << output_file << "." << std::endl;
102 return false;
103 }
104 }
105
106 std::string json = VkJsonAllPropertiesToJson(props) + '\n';
107 fwrite(json.data(), 1, json.size(), file);
108
109 if (output_file != "-") {
110 fclose(file);
111 std::cout << "Wrote file " << output_file << " for device " << device_name
112 << "." << std::endl;
113 }
114 return true;
115}
116
117int main(int argc, char* argv[]) {
118 Options options;
119 if (!ParseOptions(argc, argv, &options))
120 return 1;
121
122 const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO,
123 nullptr,
124 "vkjson_info",
125 1,
126 "",
127 0,
128 VK_API_VERSION};
129 VkInstanceCreateInfo instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
130 nullptr,
131 0,
132 &app_info,
133 0,
134 nullptr,
135 0,
136 nullptr};
137 VkInstance instance;
138 VkResult result = vkCreateInstance(&instance_info, nullptr, &instance);
139 if (result != VK_SUCCESS) {
140 std::cerr << "Error: vkCreateInstance failed with error: " << result
141 << "." << std::endl;
142 return 1;
143 }
144
145 uint32_t device_count = 0;
146 result = vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
147 if (result != VK_SUCCESS) {
148 std::cerr << "Error: vkEnumeratePhysicalDevices failed with error "
149 << result << "." << std::endl;
150 return 1;
151 }
152 if (device_count == 0) {
153 std::cerr << "Error: no Vulkan device found.";
154 return 1;
155 }
156
157 std::vector<VkPhysicalDevice> physical_devices(device_count,
158 VkPhysicalDevice());
159 result = vkEnumeratePhysicalDevices(instance, &device_count,
160 physical_devices.data());
161 if (result != VK_SUCCESS) {
162 std::cerr << "Error: vkEnumeratePhysicalDevices failed with error "
163 << result << std::endl;
164 return 1;
165 }
166
Mark Young8928c062016-01-25 13:37:06 -0700167 if (options.device_index != unsignedNegOne) {
Antoine Laboure3795c12015-10-27 12:21:09 -0700168 if (static_cast<uint32_t>(options.device_index) >= device_count) {
169 std::cerr << "Error: device " << options.device_index
170 << " requested but only " << device_count << " found."
171 << std::endl;
172 return 1;
173 }
174 auto props = VkJsonGetAllProperties(physical_devices[options.device_index]);
175 if (!DumpProperties(props, options))
176 return 1;
177 return 0;
178 }
179
180 bool found = false;
181 for (auto physical_device : physical_devices) {
182 auto props = VkJsonGetAllProperties(physical_device);
183 if (!options.device_name.empty() &&
184 options.device_name != props.properties.deviceName)
185 continue;
186 if (!DumpProperties(props, options))
187 return 1;
188 found = true;
189 }
190
191 if (!found) {
192 std::cerr << "Error: device " << options.device_name << " not found."
193 << std::endl;
194 return 1;
195 }
196 return 0;
197}