blob: b5b0c3e7acd2ac51e0d921ce8f70852196a14b33 [file] [log] [blame]
Karl Schultzb932f102016-02-04 09:41:29 -07001///////////////////////////////////////////////////////////////////////////////
Antoine Laboure3795c12015-10-27 12:21:09 -07002//
Karl Schultzb932f102016-02-04 09:41:29 -07003// 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.
Antoine Laboure3795c12015-10-27 12:21:09 -07007//
Jon Ashburn43b53e82016-04-19 11:30:31 -06008// 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
Antoine Laboure3795c12015-10-27 12:21:09 -070011//
Jon Ashburn43b53e82016-04-19 11:30:31 -060012// http://www.apache.org/licenses/LICENSE-2.0
Karl Schultzb932f102016-02-04 09:41:29 -070013//
Jon Ashburn43b53e82016-04-19 11:30:31 -060014// 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.
Karl Schultzb932f102016-02-04 09:41:29 -070019///////////////////////////////////////////////////////////////////////////////
Antoine Laboure3795c12015-10-27 12:21:09 -070020
21#define VK_PROTOTYPES
22#include "vkjson.h"
23
24#include <stdio.h>
25#include <string.h>
26
27#include <iostream>
28#include <vector>
29
Mark Young8928c062016-01-25 13:37:06 -070030const uint32_t unsignedNegOne = (uint32_t)(-1);
31
Antoine Laboure3795c12015-10-27 12:21:09 -070032struct Options {
Mark Young8928c062016-01-25 13:37:06 -070033 uint32_t device_index = unsignedNegOne;
Antoine Laboure3795c12015-10-27 12:21:09 -070034 std::string device_name;
35 std::string output_file;
36};
37
38bool ParseOptions(int argc, char* argv[], Options* options) {
39 for (int i = 1; i < argc; ++i) {
40 std::string arg(argv[i]);
41 if (arg == "--first" || arg == "-f") {
42 options->device_index = 0;
43 } else {
44 ++i;
45 if (i >= argc) {
46 std::cerr << "Missing parameter after: " << arg << std::endl;
47 return false;
48 }
49 std::string arg2(argv[i]);
50 if (arg == "--device-index" || arg == "-d") {
51 int result = sscanf(arg2.c_str(), "%u", &options->device_index);
52 if (result != 1) {
53 options->device_index = -1;
54 std::cerr << "Unable to parse index: " << arg2 << std::endl;
55 return false;
56 }
57 } else if (arg == "--device-name" || arg == "-n") {
58 options->device_name = arg2;
59 } else if (arg == "--output" || arg == "-o") {
60 options->output_file = arg2;
61 } else {
62 std::cerr << "Unknown argument: " << arg << std::endl;
63 return false;
64 }
65 }
66 }
Mark Young8928c062016-01-25 13:37:06 -070067 if (options->device_index != unsignedNegOne && !options->device_name.empty()) {
Antoine Laboure3795c12015-10-27 12:21:09 -070068 std::cerr << "Must specify only one of device index and device name."
69 << std::endl;
70 return false;
71 }
Mark Young8928c062016-01-25 13:37:06 -070072 if (!options->output_file.empty() && options->device_index == unsignedNegOne &&
Antoine Laboure3795c12015-10-27 12:21:09 -070073 options->device_name.empty()) {
74 std::cerr << "Must specify device index or device name when specifying "
75 "output file"
76 << std::endl;
77 return false;
78 }
79 return true;
80}
81
82bool DumpProperties(const VkJsonAllProperties& props, const Options& options) {
83 std::string device_name(props.properties.deviceName);
84 std::string output_file = options.output_file;
85 if (output_file.empty())
86 output_file = device_name + ".json";
87 FILE* file = nullptr;
88 if (output_file == "-") {
89 file = stdout;
90 } else {
91 file = fopen(output_file.c_str(), "w");
92 if (!file) {
93 std::cerr << "Unable to open file " << output_file << "." << std::endl;
94 return false;
95 }
96 }
97
98 std::string json = VkJsonAllPropertiesToJson(props) + '\n';
99 fwrite(json.data(), 1, json.size(), file);
100
101 if (output_file != "-") {
102 fclose(file);
103 std::cout << "Wrote file " << output_file << " for device " << device_name
104 << "." << std::endl;
105 }
106 return true;
107}
108
109int main(int argc, char* argv[]) {
110 Options options;
111 if (!ParseOptions(argc, argv, &options))
112 return 1;
113
114 const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO,
115 nullptr,
116 "vkjson_info",
117 1,
118 "",
119 0,
Jon Ashburnf1ea4182016-03-22 12:57:13 -0600120 VK_API_VERSION_1_0};
Antoine Laboure3795c12015-10-27 12:21:09 -0700121 VkInstanceCreateInfo instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
122 nullptr,
123 0,
124 &app_info,
125 0,
126 nullptr,
127 0,
128 nullptr};
129 VkInstance instance;
130 VkResult result = vkCreateInstance(&instance_info, nullptr, &instance);
131 if (result != VK_SUCCESS) {
132 std::cerr << "Error: vkCreateInstance failed with error: " << result
133 << "." << std::endl;
134 return 1;
135 }
136
137 uint32_t device_count = 0;
138 result = vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
139 if (result != VK_SUCCESS) {
140 std::cerr << "Error: vkEnumeratePhysicalDevices failed with error "
141 << result << "." << std::endl;
142 return 1;
143 }
144 if (device_count == 0) {
145 std::cerr << "Error: no Vulkan device found.";
146 return 1;
147 }
148
149 std::vector<VkPhysicalDevice> physical_devices(device_count,
150 VkPhysicalDevice());
151 result = vkEnumeratePhysicalDevices(instance, &device_count,
152 physical_devices.data());
153 if (result != VK_SUCCESS) {
154 std::cerr << "Error: vkEnumeratePhysicalDevices failed with error "
155 << result << std::endl;
156 return 1;
157 }
158
Mark Young8928c062016-01-25 13:37:06 -0700159 if (options.device_index != unsignedNegOne) {
Antoine Laboure3795c12015-10-27 12:21:09 -0700160 if (static_cast<uint32_t>(options.device_index) >= device_count) {
161 std::cerr << "Error: device " << options.device_index
162 << " requested but only " << device_count << " found."
163 << std::endl;
164 return 1;
165 }
166 auto props = VkJsonGetAllProperties(physical_devices[options.device_index]);
167 if (!DumpProperties(props, options))
168 return 1;
169 return 0;
170 }
171
172 bool found = false;
173 for (auto physical_device : physical_devices) {
174 auto props = VkJsonGetAllProperties(physical_device);
175 if (!options.device_name.empty() &&
176 options.device_name != props.properties.deviceName)
177 continue;
178 if (!DumpProperties(props, options))
179 return 1;
180 found = true;
181 }
182
183 if (!found) {
184 std::cerr << "Error: device " << options.device_name << " not found."
185 << std::endl;
186 return 1;
187 }
188 return 0;
189}