diff options
author | Charles Giessen <charles@lunarg.com> | 2022-12-01 11:25:11 -0600 |
---|---|---|
committer | Charles Giessen <46324611+charles-lunarg@users.noreply.github.com> | 2022-12-01 14:35:36 -0600 |
commit | b17374498fcd4612e758a911fb11a8c2d0451dc0 (patch) | |
tree | c968a85dc1cb4b7e0c30a75d51e50b459652bcbc | |
parent | 8455f816a3663aaf31c5cf4ef10a2a6c15d36e5b (diff) | |
download | vulkan-tools-b17374498fcd4612e758a911fb11a8c2d0451dc0.tar.gz |
cube & cubepp: Handle negative gpu selections
Previously, a negative gpu number (other than -1) would cause the program to
assert. It is simple enough to handle it gracefully, so best to do so.
-rw-r--r-- | cube/cube.c | 5 | ||||
-rw-r--r-- | cube/cube.cpp | 5 |
2 files changed, 6 insertions, 4 deletions
diff --git a/cube/cube.c b/cube/cube.c index 5cd67866..08581070 100644 --- a/cube/cube.c +++ b/cube/cube.c @@ -366,6 +366,7 @@ struct demo { bool use_staging_buffer; bool separate_present_queue; bool is_minimized; + bool invalid_gpu_selection; int32_t gpu_number; bool VK_KHR_incremental_present_enabled; @@ -3391,7 +3392,7 @@ static void demo_init_vk(struct demo *demo) { VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count); err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices); assert(!err); - if (demo->gpu_number >= 0 && !((uint32_t)demo->gpu_number < gpu_count)) { + if (demo->invalid_gpu_selection || (demo->gpu_number >= 0 && !((uint32_t)demo->gpu_number < gpu_count))) { fprintf(stderr, "GPU %d specified is not present, GPU count = %u\n", demo->gpu_number, gpu_count); ERR_EXIT("Specified GPU number is not present", "User Error"); } @@ -4041,7 +4042,7 @@ static void demo_init(struct demo *demo, int argc, char **argv) { } if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) { demo->gpu_number = atoi(argv[i + 1]); - assert(demo->gpu_number >= 0); + if (demo->gpu_number < 0) demo->invalid_gpu_selection = true; i++; continue; } diff --git a/cube/cube.cpp b/cube/cube.cpp index 94f91274..95dd5dc6 100644 --- a/cube/cube.cpp +++ b/cube/cube.cpp @@ -330,6 +330,7 @@ struct Demo { bool use_staging_buffer = false; bool use_xlib = false; bool separate_present_queue = false; + bool invalid_gpu_selection = false; int32_t gpu_number = 0; vk::Instance inst; @@ -902,7 +903,7 @@ void Demo::init(int argc, char **argv) { } if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) { gpu_number = atoi(argv[i + 1]); - assert(gpu_number >= 0); + if (gpu_number < 0) invalid_gpu_selection = true; i++; continue; } @@ -1295,7 +1296,7 @@ void Demo::init_vk() { "vkEnumeratePhysicalDevices Failure"); } - if (gpu_number >= 0 && !(static_cast<uint32_t>(gpu_number) < physical_devices.size())) { + if (invalid_gpu_selection || (gpu_number >= 0 && !(static_cast<uint32_t>(gpu_number) < physical_devices.size()))) { fprintf(stderr, "GPU %d specified is not present, GPU count = %zu\n", gpu_number, physical_devices.size()); ERR_EXIT("Specified GPU number is not present", "User Error"); } |