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 | // |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 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: |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 14 | // |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 15 | // The above copyright notice(s) and this permission notice shall be included in |
| 16 | // all copies or substantial portions of the Materials. |
| 17 | // |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 18 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Antoine Labour | 38841df | 2016-01-28 15:05:57 -0800 | [diff] [blame] | 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Karl Schultz | b932f10 | 2016-02-04 09:41:29 -0700 | [diff] [blame] | 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 | /////////////////////////////////////////////////////////////////////////////// |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 27 | |
| 28 | #define VK_PROTOTYPES |
| 29 | #include "vkjson.h" |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include <iostream> |
| 35 | #include <vector> |
| 36 | |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 37 | const uint32_t unsignedNegOne = (uint32_t)(-1); |
| 38 | |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 39 | struct Options { |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 40 | uint32_t device_index = unsignedNegOne; |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 41 | std::string device_name; |
| 42 | std::string output_file; |
| 43 | }; |
| 44 | |
| 45 | bool ParseOptions(int argc, char* argv[], Options* options) { |
| 46 | for (int i = 1; i < argc; ++i) { |
| 47 | std::string arg(argv[i]); |
| 48 | if (arg == "--first" || arg == "-f") { |
| 49 | options->device_index = 0; |
| 50 | } else { |
| 51 | ++i; |
| 52 | if (i >= argc) { |
| 53 | std::cerr << "Missing parameter after: " << arg << std::endl; |
| 54 | return false; |
| 55 | } |
| 56 | std::string arg2(argv[i]); |
| 57 | if (arg == "--device-index" || arg == "-d") { |
| 58 | int result = sscanf(arg2.c_str(), "%u", &options->device_index); |
| 59 | if (result != 1) { |
| 60 | options->device_index = -1; |
| 61 | std::cerr << "Unable to parse index: " << arg2 << std::endl; |
| 62 | return false; |
| 63 | } |
| 64 | } else if (arg == "--device-name" || arg == "-n") { |
| 65 | options->device_name = arg2; |
| 66 | } else if (arg == "--output" || arg == "-o") { |
| 67 | options->output_file = arg2; |
| 68 | } else { |
| 69 | std::cerr << "Unknown argument: " << arg << std::endl; |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | } |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 74 | if (options->device_index != unsignedNegOne && !options->device_name.empty()) { |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 75 | std::cerr << "Must specify only one of device index and device name." |
| 76 | << std::endl; |
| 77 | return false; |
| 78 | } |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 79 | if (!options->output_file.empty() && options->device_index == unsignedNegOne && |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 80 | options->device_name.empty()) { |
| 81 | std::cerr << "Must specify device index or device name when specifying " |
| 82 | "output file" |
| 83 | << std::endl; |
| 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool DumpProperties(const VkJsonAllProperties& props, const Options& options) { |
| 90 | std::string device_name(props.properties.deviceName); |
| 91 | std::string output_file = options.output_file; |
| 92 | if (output_file.empty()) |
| 93 | output_file = device_name + ".json"; |
| 94 | FILE* file = nullptr; |
| 95 | if (output_file == "-") { |
| 96 | file = stdout; |
| 97 | } else { |
| 98 | file = fopen(output_file.c_str(), "w"); |
| 99 | if (!file) { |
| 100 | std::cerr << "Unable to open file " << output_file << "." << std::endl; |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | std::string json = VkJsonAllPropertiesToJson(props) + '\n'; |
| 106 | fwrite(json.data(), 1, json.size(), file); |
| 107 | |
| 108 | if (output_file != "-") { |
| 109 | fclose(file); |
| 110 | std::cout << "Wrote file " << output_file << " for device " << device_name |
| 111 | << "." << std::endl; |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | int main(int argc, char* argv[]) { |
| 117 | Options options; |
| 118 | if (!ParseOptions(argc, argv, &options)) |
| 119 | return 1; |
| 120 | |
| 121 | const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO, |
| 122 | nullptr, |
| 123 | "vkjson_info", |
| 124 | 1, |
| 125 | "", |
| 126 | 0, |
Jon Ashburn | f1ea418 | 2016-03-22 12:57:13 -0600 | [diff] [blame^] | 127 | VK_API_VERSION_1_0}; |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 128 | VkInstanceCreateInfo instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
| 129 | nullptr, |
| 130 | 0, |
| 131 | &app_info, |
| 132 | 0, |
| 133 | nullptr, |
| 134 | 0, |
| 135 | nullptr}; |
| 136 | VkInstance instance; |
| 137 | VkResult result = vkCreateInstance(&instance_info, nullptr, &instance); |
| 138 | if (result != VK_SUCCESS) { |
| 139 | std::cerr << "Error: vkCreateInstance failed with error: " << result |
| 140 | << "." << std::endl; |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | uint32_t device_count = 0; |
| 145 | result = vkEnumeratePhysicalDevices(instance, &device_count, nullptr); |
| 146 | if (result != VK_SUCCESS) { |
| 147 | std::cerr << "Error: vkEnumeratePhysicalDevices failed with error " |
| 148 | << result << "." << std::endl; |
| 149 | return 1; |
| 150 | } |
| 151 | if (device_count == 0) { |
| 152 | std::cerr << "Error: no Vulkan device found."; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | std::vector<VkPhysicalDevice> physical_devices(device_count, |
| 157 | VkPhysicalDevice()); |
| 158 | result = vkEnumeratePhysicalDevices(instance, &device_count, |
| 159 | physical_devices.data()); |
| 160 | if (result != VK_SUCCESS) { |
| 161 | std::cerr << "Error: vkEnumeratePhysicalDevices failed with error " |
| 162 | << result << std::endl; |
| 163 | return 1; |
| 164 | } |
| 165 | |
Mark Young | 8928c06 | 2016-01-25 13:37:06 -0700 | [diff] [blame] | 166 | if (options.device_index != unsignedNegOne) { |
Antoine Labour | e3795c1 | 2015-10-27 12:21:09 -0700 | [diff] [blame] | 167 | if (static_cast<uint32_t>(options.device_index) >= device_count) { |
| 168 | std::cerr << "Error: device " << options.device_index |
| 169 | << " requested but only " << device_count << " found." |
| 170 | << std::endl; |
| 171 | return 1; |
| 172 | } |
| 173 | auto props = VkJsonGetAllProperties(physical_devices[options.device_index]); |
| 174 | if (!DumpProperties(props, options)) |
| 175 | return 1; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | bool found = false; |
| 180 | for (auto physical_device : physical_devices) { |
| 181 | auto props = VkJsonGetAllProperties(physical_device); |
| 182 | if (!options.device_name.empty() && |
| 183 | options.device_name != props.properties.deviceName) |
| 184 | continue; |
| 185 | if (!DumpProperties(props, options)) |
| 186 | return 1; |
| 187 | found = true; |
| 188 | } |
| 189 | |
| 190 | if (!found) { |
| 191 | std::cerr << "Error: device " << options.device_name << " not found." |
| 192 | << std::endl; |
| 193 | return 1; |
| 194 | } |
| 195 | return 0; |
| 196 | } |