aboutsummaryrefslogtreecommitdiff
path: root/libs/vkjson/vkjson_info.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libs/vkjson/vkjson_info.cc')
-rw-r--r--libs/vkjson/vkjson_info.cc151
1 files changed, 67 insertions, 84 deletions
diff --git a/libs/vkjson/vkjson_info.cc b/libs/vkjson/vkjson_info.cc
index 670eabbf8..38a0655c2 100644
--- a/libs/vkjson/vkjson_info.cc
+++ b/libs/vkjson/vkjson_info.cc
@@ -28,6 +28,7 @@
#define VK_PROTOTYPES
#include "vkjson.h"
+#include <assert.h>
#include <stdio.h>
#include <string.h>
@@ -37,6 +38,7 @@
const uint32_t unsignedNegOne = (uint32_t)(-1);
struct Options {
+ bool instance = false;
uint32_t device_index = unsignedNegOne;
std::string device_name;
std::string output_file;
@@ -45,7 +47,9 @@ struct Options {
bool ParseOptions(int argc, char* argv[], Options* options) {
for (int i = 1; i < argc; ++i) {
std::string arg(argv[i]);
- if (arg == "--first" || arg == "-f") {
+ if (arg == "--instance" || arg == "-i") {
+ options->instance = true;
+ } else if (arg == "--first" || arg == "-f") {
options->device_index = 0;
} else {
++i;
@@ -71,26 +75,64 @@ bool ParseOptions(int argc, char* argv[], Options* options) {
}
}
}
+ if (options->instance && (options->device_index != unsignedNegOne ||
+ !options->device_name.empty())) {
+ std::cerr << "Specifying a specific device is incompatible with dumping "
+ "the whole instance." << std::endl;
+ return false;
+ }
if (options->device_index != unsignedNegOne && !options->device_name.empty()) {
std::cerr << "Must specify only one of device index and device name."
<< std::endl;
return false;
}
- if (!options->output_file.empty() && options->device_index == unsignedNegOne &&
- options->device_name.empty()) {
- std::cerr << "Must specify device index or device name when specifying "
- "output file"
+ if (options->instance && options->output_file.empty()) {
+ std::cerr << "Must specify an output file when dumping the whole instance."
<< std::endl;
return false;
}
+ if (!options->output_file.empty() && !options->instance &&
+ options->device_index == unsignedNegOne && options->device_name.empty()) {
+ std::cerr << "Must specify instance, device index, or device name when "
+ "specifying "
+ "output file." << std::endl;
+ return false;
+ }
return true;
}
-bool DumpProperties(const VkJsonAllProperties& props, const Options& options) {
- std::string device_name(props.properties.deviceName);
- std::string output_file = options.output_file;
- if (output_file.empty())
- output_file = device_name + ".json";
+bool Dump(const VkJsonInstance& instance, const Options& options) {
+ const VkJsonDevice* out_device = nullptr;
+ if (options.device_index != unsignedNegOne) {
+ if (static_cast<uint32_t>(options.device_index) >=
+ instance.devices.size()) {
+ std::cerr << "Error: device " << options.device_index
+ << " requested but only " << instance.devices.size()
+ << " devices found." << std::endl;
+ return false;
+ }
+ out_device = &instance.devices[options.device_index];
+ } else if (!options.device_name.empty()) {
+ for (const auto& device : instance.devices) {
+ if (device.properties.deviceName == options.device_name) {
+ out_device = &device;
+ }
+ }
+ if (!out_device) {
+ std::cerr << "Error: device '" << options.device_name
+ << "' requested but not found." << std::endl;
+ return false;
+ }
+ }
+
+ std::string output_file;
+ if (options.output_file.empty()) {
+ assert(out_device);
+ output_file.assign(out_device->properties.deviceName);
+ output_file.append(".json");
+ } else {
+ output_file = options.output_file;
+ }
FILE* file = nullptr;
if (output_file == "-") {
file = stdout;
@@ -102,13 +144,17 @@ bool DumpProperties(const VkJsonAllProperties& props, const Options& options) {
}
}
- std::string json = VkJsonAllPropertiesToJson(props) + '\n';
+ std::string json = out_device ? VkJsonDeviceToJson(*out_device)
+ : VkJsonInstanceToJson(instance);
fwrite(json.data(), 1, json.size(), file);
+ fputc('\n', file);
if (output_file != "-") {
fclose(file);
- std::cout << "Wrote file " << output_file << " for device " << device_name
- << "." << std::endl;
+ std::cout << "Wrote file " << output_file;
+ if (out_device)
+ std::cout << " for device " << out_device->properties.deviceName;
+ std::cout << "." << std::endl;
}
return true;
}
@@ -118,79 +164,16 @@ int main(int argc, char* argv[]) {
if (!ParseOptions(argc, argv, &options))
return 1;
- const VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO,
- nullptr,
- "vkjson_info",
- 1,
- "",
- 0,
- VK_API_VERSION_1_0};
- VkInstanceCreateInfo instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
- nullptr,
- 0,
- &app_info,
- 0,
- nullptr,
- 0,
- nullptr};
- VkInstance instance;
- VkResult result = vkCreateInstance(&instance_info, nullptr, &instance);
- if (result != VK_SUCCESS) {
- std::cerr << "Error: vkCreateInstance failed with error: " << result
- << "." << std::endl;
- return 1;
- }
-
- uint32_t device_count = 0;
- result = vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
- if (result != VK_SUCCESS) {
- std::cerr << "Error: vkEnumeratePhysicalDevices failed with error "
- << result << "." << std::endl;
- return 1;
- }
- if (device_count == 0) {
- std::cerr << "Error: no Vulkan device found.";
- return 1;
- }
-
- std::vector<VkPhysicalDevice> physical_devices(device_count,
- VkPhysicalDevice());
- result = vkEnumeratePhysicalDevices(instance, &device_count,
- physical_devices.data());
- if (result != VK_SUCCESS) {
- std::cerr << "Error: vkEnumeratePhysicalDevices failed with error "
- << result << std::endl;
- return 1;
- }
-
- if (options.device_index != unsignedNegOne) {
- if (static_cast<uint32_t>(options.device_index) >= device_count) {
- std::cerr << "Error: device " << options.device_index
- << " requested but only " << device_count << " found."
- << std::endl;
- return 1;
+ VkJsonInstance instance = VkJsonGetInstance();
+ if (options.instance || options.device_index != unsignedNegOne ||
+ !options.device_name.empty()) {
+ Dump(instance, options);
+ } else {
+ for (uint32_t i = 0, n = instance.devices.size(); i < n; i++) {
+ options.device_index = i;
+ Dump(instance, options);
}
- auto props = VkJsonGetAllProperties(physical_devices[options.device_index]);
- if (!DumpProperties(props, options))
- return 1;
- return 0;
}
- bool found = false;
- for (auto physical_device : physical_devices) {
- auto props = VkJsonGetAllProperties(physical_device);
- if (!options.device_name.empty() &&
- options.device_name != props.properties.deviceName)
- continue;
- if (!DumpProperties(props, options))
- return 1;
- found = true;
- }
-
- if (!found) {
- std::cerr << "Error: device " << options.device_name << " not found."
- << std::endl;
- return 1;
- }
return 0;
}