diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index fbddd714..3116b889 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -789,11 +789,63 @@ void VulkanExampleBase::initVulkan() vkTools::exitFatal("Could not enumerate physical devices : \n" + vkTools::errorString(err), "Fatal error"); } - // Note : - // This example will always use the first physical device reported, - // change the vector index if you have multiple Vulkan devices installed - // and want to use another one - physicalDevice = physicalDevices[0]; + // GPU selection + + // Select physical device to be used for the Vulkan example + // Defaults to the first device unless specified by command line + uint32_t selectedDevice = 0; + +#if !defined(__ANDROID__) + // GPU selection via command line argument + for (size_t i = 0; i < args.size(); i++) + { + // Select GPU + if ((args[i] == std::string("-g")) || (args[i] == std::string("-gpu"))) + { + char* endptr; + uint32_t index = strtol(args[i + 1], &endptr, 10); + if (endptr != args[i + 1]) + { + if (index > gpuCount - 1) + { + std::cerr << "Selected device index " << index << " is out of range, reverting to device 0 (use -listgpus to show available Vulkan devices)" << std::endl; + } + else + { + std::cout << "Selected Vulkan device " << index << std::endl; + selectedDevice = index; + } + }; + break; + } + // List available GPUs + if (args[i] == std::string("-listgpus")) + { + uint32_t gpuCount = 0; + VK_CHECK_RESULT(vkEnumeratePhysicalDevices(instance, &gpuCount, nullptr)); + if (gpuCount == 0) + { + std::cerr << "No Vulkan devices found!" << std::endl; + } + else + { + // Enumerate devices + std::cout << "Available Vulkan devices" << std::endl; + std::vector devices(gpuCount); + VK_CHECK_RESULT(vkEnumeratePhysicalDevices(instance, &gpuCount, devices.data())); + for (uint32_t i = 0; i < gpuCount; i++) { + VkPhysicalDeviceProperties deviceProperties; + vkGetPhysicalDeviceProperties(devices[i], &deviceProperties); + std::cout << "Device [" << i << "] : " << deviceProperties.deviceName << std::endl; + std::cout << " Type: " << vkTools::physicalDeviceTypeString(deviceProperties.deviceType) << std::endl; + std::cout << " API: " << (deviceProperties.apiVersion >> 22) << "." << ((deviceProperties.apiVersion >> 12) & 0x3ff) << "." << (deviceProperties.apiVersion & 0xfff) << std::endl; + } + } + } + } +#endif + + physicalDevice = physicalDevices[selectedDevice]; // Vulkan device creation // This is handled by a separate class that gets a logical device representation @@ -851,6 +903,7 @@ void VulkanExampleBase::setupConsole(std::string title) AttachConsole(GetCurrentProcessId()); FILE *stream; freopen_s(&stream, "CONOUT$", "w+", stdout); + freopen_s(&stream, "CONOUT$", "w+", stderr); SetConsoleTitle(TEXT(title.c_str())); }