Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 1 | /////////////////////////////////////////////////////////////////////////////// |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 2 | // |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 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. |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 7 | // |
Jon Ashburn | 43b53e8 | 2016-04-19 11:30:31 -0600 | [diff] [blame^] | 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 |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 11 | // |
Jon Ashburn | 43b53e8 | 2016-04-19 11:30:31 -0600 | [diff] [blame^] | 12 | // http://www.apache.org/licenses/LICENSE-2.0 |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 13 | // |
Jon Ashburn | 43b53e8 | 2016-04-19 11:30:31 -0600 | [diff] [blame^] | 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. |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 19 | /////////////////////////////////////////////////////////////////////////////// |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 20 | |
| 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 Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 30 | const uint32_t unsignedNegOne = (uint32_t)(-1); |
| 31 | |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 32 | struct Options { |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 33 | uint32_t device_index = unsignedNegOne; |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 34 | std::string device_name; |
| 35 | std::string output_file; |
| 36 | }; |
| 37 | |
| 38 | bool 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 Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 67 | if (options->device_index != unsignedNegOne && !options->device_name.empty()) { |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 68 | std::cerr << "Must specify only one of device index and device name." |
| 69 | << std::endl; |
| 70 | return false; |
| 71 | } |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 72 | if (!options->output_file.empty() && options->device_index == unsignedNegOne && |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 73 | 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 | |
| 82 | bool 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 | |
| 109 | int 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 Ashburn | f1ea418 | 2016-03-22 12:57:13 -0600 | [diff] [blame] | 120 | VK_API_VERSION_1_0}; |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 121 | 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 Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 159 | if (options.device_index != unsignedNegOne) { |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 160 | 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 | } |