diff --git a/cube/cube.cpp b/cube/cube.cpp index de411cfa9..9ca175f20 100644 --- a/cube/cube.cpp +++ b/cube/cube.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include #if defined(VK_USE_PLATFORM_XLIB_KHR) #include "xlib_loader.h" @@ -223,90 +225,77 @@ enum class WsiPlatform { invalid, // Sentinel just to indicate invalid user input }; -WsiPlatform wsi_from_string(std::string const &str) { - if (str == "auto") return WsiPlatform::auto_; +struct platform_info { + const char* name; + WsiPlatform platform; + const char *surface_extension_name; +}; + +constexpr platform_info platform_data_[] = { + {"auto", WsiPlatform::auto_, ""}, #if defined(VK_USE_PLATFORM_WIN32_KHR) - if (str == "win32") return WsiPlatform::win32; + {"win32", WsiPlatform::win32, vk::KHRWin32SurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_METAL_EXT) - if (str == "metal") return WsiPlatform::metal; + {"metal", WsiPlatform::metal, vk::EXTMetalSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_ANDROID_KHR) - if (str == "android") return WsiPlatform::android; + {"android", WsiPlatform::android, vk::KHRAndroidSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_SCREEN_QNX) - if (str == "qnx") return WsiPlatform::qnx; + {"qnx", WsiPlatform::qnx, vk::QNXScreenSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_XCB_KHR) - if (str == "xcb") return WsiPlatform::xcb; + {"xcb", WsiPlatform::xcb, vk::KHRXcbSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_XLIB_KHR) - if (str == "xlib") return WsiPlatform::xlib; + {"xlib", WsiPlatform::xlib, vk::KHRXlibSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_WAYLAND_KHR) - if (str == "wayland") return WsiPlatform::wayland; + {"wayland", WsiPlatform::wayland, vk::KHRWaylandSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - if (str == "directfb") return WsiPlatform::directfb; + {"directfb", WsiPlatform::directfb, vk::EXTDirectfbSurfaceExtensionName}, #endif #if defined(VK_USE_PLATFORM_DISPLAY_KHR) - if (str == "display") return WsiPlatform::display; + {"display", WsiPlatform::display, vk::KHRDisplayExtensionName}, #endif #if defined(VK_USE_PLATFORM_FUCHSIA) - if (str == "fuchsia_display") return WsiPlatform::fuchsia_display; - if (str == "fuchsia_scenic") return WsiPlatform::fuchsia_scenic; + {"fuchsia_display", WsiPlatform::fuchsia_display, vk::FUCHSIAImagepipeSurfaceExtensionName}, + {"fuchsia_scenic", WsiPlatform::fuchsia_scenic, vk::FUCHSIAImagepipeSurfaceExtensionName}, #endif - return WsiPlatform::invalid; +}; + +template +constexpr auto construct_array(const T (&data)[N]) { + std::array res{}; + + for (size_t i = 0; i < N; ++i) { + res[i] = data[i]; + } + + return res; +} + +constexpr auto platform_data = construct_array(platform_data_); + +WsiPlatform wsi_from_string(std::string const &str) { + auto ite = std::find_if(platform_data.begin(), platform_data.end(), [&str](auto &data) { return data.name == str; }); + + if (ite != platform_data.end()) { + return ite->platform; + } else { + return WsiPlatform::invalid; + } }; const char *wsi_to_string(WsiPlatform wsi_platform) { - switch (wsi_platform) { - case (WsiPlatform::auto_): - return "auto"; -#if defined(VK_USE_PLATFORM_WIN32_KHR) - case (WsiPlatform::win32): - return "win32"; -#endif -#if defined(VK_USE_PLATFORM_METAL_EXT) - case (WsiPlatform::metal): - return "metal"; -#endif -#if defined(VK_USE_PLATFORM_ANDROID_KHR) - case (WsiPlatform::android): - return "android"; -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - case (WsiPlatform::qnx): - return "qnx"; -#endif -#if defined(VK_USE_PLATFORM_XCB_KHR) - case (WsiPlatform::xcb): - return "xcb"; -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - case (WsiPlatform::xlib): - return "xlib"; -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - case (WsiPlatform::wayland): - return "wayland"; -#endif -#if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - case (WsiPlatform::directfb): - return "directfb"; -#endif -#if defined(VK_USE_PLATFORM_DISPLAY_KHR) - case (WsiPlatform::display): - return "display"; -#endif -#if defined(VK_USE_PLATFORM_FUCHSIA) - case (WsiPlatform::fuchsia_display): - return "fuchsia_display"; - case (WsiPlatform::fuchsia_scenic): - return "fuchsia_scenic"; -#endif - default: - return "unknown"; + auto ite = std::find_if(platform_data.begin(), platform_data.end(), [&wsi_platform](auto &data) { return data.platform == wsi_platform; }); + + if (ite != platform_data.end()) { + return ite->name; + } else { + return "unknown"; } }; @@ -1188,47 +1177,13 @@ void Demo::init(int argc, char **argv) { } std::string wsi_platforms; -#if defined(VK_USE_PLATFORM_XCB_KHR) - wsi_platforms.append("xcb"); -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("xlib"); -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("wayland"); -#endif -#if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("directfb"); -#endif -#if defined(VK_USE_PLATFORM_DISPLAY_KHR) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("display"); -#endif -#if defined(VK_USE_PLATFORM_METAL_EXT) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("metal"); -#endif -#if defined(VK_USE_PLATFORM_WIN32_KHR) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("win32"); -#endif -#if defined(VK_USE_PLATFORM_ANDROID_KHR) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("android"); -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("qnx"); -#endif -#if defined(VK_USE_PLATFORM_FUCHSIA) - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("fuchsia_display"); - if (!wsi_platforms.empty()) wsi_platforms.append("|"); - wsi_platforms.append("fuchsia_scenic"); -#endif + + wsi_platforms.append(platform_data[0].name); + for (size_t index = 1; index < platform_data.size(); ++index) { + wsi_platforms.append("|"); + wsi_platforms.append(platform_data[index].name); + } + std::stringstream usage; usage << "Usage:\n " << APP_SHORT_NAME << "\t[--use_staging] [--validate]\n" << "\t[--break] [--c ] [--suppress_popups]\n" @@ -1629,69 +1584,15 @@ void Demo::init_vk() { } else if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extension.extensionName)) { surfaceExtFound = 1; enabled_instance_extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); + } else { + auto ite = std::find_if(platform_data.begin(), platform_data.end(), [platform = wsi_platform, &extension](auto &info) { + return !strcmp(info.surface_extension_name, extension.extensionName) && (platform == WsiPlatform::auto_ || platform == info.platform); + }); + if (ite != platform_data.end()) { + platformSurfaceExtFound = 1; + enabled_instance_extensions.push_back(ite->surface_extension_name); + } } -#if defined(VK_USE_PLATFORM_WIN32_KHR) - else if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::win32)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - else if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::xlib)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_XCB_KHR) - else if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::xcb)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - else if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::wayland)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - else if (!strcmp(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::directfb)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_DISPLAY_KHR) - else if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::display)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_KHR_DISPLAY_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_METAL_EXT) - else if (!strcmp(VK_EXT_METAL_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::metal)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - else if (!strcmp(VK_QNX_SCREEN_SURFACE_EXTENSION_NAME, extension.extensionName) && - (wsi_platform == WsiPlatform::auto_ || wsi_platform == WsiPlatform::qnx)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_QNX_SCREEN_SURFACE_EXTENSION_NAME); - } -#endif -#if defined(VK_USE_PLATFORM_FUCHSIA) - else if (!strcmp(VK_FUCHSIA_IMAGEPIPE_SURFACE_EXTENSION_NAME, extension.extensionName)) { - platformSurfaceExtFound = 1; - enabled_instance_extensions.push_back(VK_FUCHSIA_IMAGEPIPE_SURFACE_EXTENSION_NAME); - } -#endif } if (!surfaceExtFound) { @@ -1703,88 +1604,26 @@ void Demo::init_vk() { } if (!platformSurfaceExtFound) { - switch (wsi_platform) { -#if defined(VK_USE_PLATFORM_WIN32_KHR) - case (WsiPlatform::win32): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform win32 is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_XCB_KHR) - case (WsiPlatform::xcb): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform xcb is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - case (WsiPlatform::wayland): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform wayland is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - case (WsiPlatform::xlib): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform xlib is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - case (WsiPlatform::directfb): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform directfb is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_DISPLAY_KHR) - case (WsiPlatform::display): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform display is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_METAL_EXT) - case (WsiPlatform::metal): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_METAL_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform metal is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - case (WsiPlatform::qnx): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_QNX_SCREEN_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform qnx is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif -#if defined(VK_USE_PLATFORM_FUCHSIA) - case (WsiPlatform::fuchsia_display): - case (WsiPlatform::fuchsia_scenic): - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_FUCHSIA_IMAGEPIPE_SURFACE_EXTENSION_NAME - " instance extension.\n\n" - "The selected WSI platform fuschia is not available, please choose a different WSI platform\n", - "vkCreateInstance Failure"); - break; -#endif - default: - case (WsiPlatform::auto_): - // Getting here indicates we are using the WSI extension that is default on this platform - ERR_EXIT( - "vkEnumerateInstanceExtensionProperties failed to find any supported WSI surface instance extensions.\n\n" - "This indicates that no compatible Vulkan installable client driver (ICD) is present or that the system is not " - "configured to present to the screen. \n", - "vkCreateInstance Failure"); + auto ite = std::find_if( + platform_data.begin(), platform_data.end(), + [platform = wsi_platform](auto &info) { return info.platform == platform; } + ); + if (ite != platform_data.end()) { + char buf[512]; + snprintf(buf, sizeof(buf), + "vkEnumerateInstanceExtensionProperties failed to find the %s" + " extension.\n\n" + "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional information.\n", + ite->surface_extension_name); + ERR_EXIT(buf, + "vkCreateInstance Failure"); + } else { + ERR_EXIT( + "vkEnumerateInstanceExtensionProperties failed to find any supported WSI surface extension.\n\n" + "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional information.\n", + "vkCreateInstance Failure"); } } @@ -4176,57 +4015,75 @@ void Demo::execute() { } #endif +template +auto get_dispatcher_element(Demo& demo) { + return std::pair>{ platform, [&demo](){demo.execute();} }; +} + +constexpr auto get_count_of_platform_run_from_main() { + size_t count = 0; + for (auto& data : platform_data) { + if (data.platform == WsiPlatform::xcb || + data.platform == WsiPlatform::xlib || + data.platform == WsiPlatform::wayland || + data.platform == WsiPlatform::directfb || + data.platform == WsiPlatform::display || + data.platform == WsiPlatform::qnx || + data.platform == WsiPlatform::fuchsia_display || + data.platform == WsiPlatform::fuchsia_scenic) { + count++; + } + } + return count; +} + +constexpr auto get_platform_run_from_main() { + std::array platform_run_from_main{}; + int i = 0; + for (auto& data : platform_data) { + if (data.platform == WsiPlatform::xcb || + data.platform == WsiPlatform::xlib || + data.platform == WsiPlatform::wayland || + data.platform == WsiPlatform::directfb || + data.platform == WsiPlatform::display || + data.platform == WsiPlatform::qnx || + data.platform == WsiPlatform::fuchsia_display || + data.platform == WsiPlatform::fuchsia_scenic) { + platform_run_from_main[i++] = data.platform; + } + } + return platform_run_from_main; +} + +constexpr auto platform_run_from_main = get_platform_run_from_main(); + +template +auto construct_dispatcher(std::map>& map, Demo& demo) { + map.emplace( + get_dispatcher_element(demo) + ); + construct_dispatcher(map, demo); +} +template<> +auto construct_dispatcher(std::map>& map, Demo& demo) { +} + int main(int argc, char **argv) { Demo demo; demo.init(argc, argv); - switch (demo.wsi_platform) { - default: - case (WsiPlatform::auto_): - fprintf(stderr, - "WSI platform should have already been set, indicating a bug. Please set a WSI platform manually with " - "--wsi\n"); - exit(1); - break; -#if defined(VK_USE_PLATFORM_XCB_KHR) - case (WsiPlatform::xcb): - demo.execute(); - break; -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - case (WsiPlatform::xlib): - demo.execute(); - break; -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - case (WsiPlatform::wayland): - demo.execute(); - break; -#endif -#if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - case (WsiPlatform::directfb): - demo.execute(); - break; -#endif -#if defined(VK_USE_PLATFORM_DISPLAY_KHR) - case (WsiPlatform::display): - demo.execute(); - break; -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - case (WsiPlatform::qnx): - demo.execute(); - break; -#endif -#if defined(VK_USE_PLATFORM_FUCHSIA) - case (WsiPlatform::fuchsia_display): - demo.execute(); - break; - case (WsiPlatform::fuchsia_scenic): - demo.execute(); - break; -#endif + auto platform_map = std::map>{}; + construct_dispatcher(platform_map, demo); + + if (platform_map.find(demo.wsi_platform) != platform_map.end()) { + platform_map[demo.wsi_platform](); + } + else { + fprintf(stderr, + "WSI platform should have already been set, indicating a bug. Please set a WSI platform manually with " + "--wsi\n"); + exit(1); } demo.cleanup(); diff --git a/scripts/update_deps.py b/scripts/update_deps.py index 47810a00c..00c84b979 100755 --- a/scripts/update_deps.py +++ b/scripts/update_deps.py @@ -375,7 +375,7 @@ def Clone(self, retries=10, retry_seconds=60): for retry in range(retries): make_or_exist_dirs(self.repo_dir) try: - command_output(['git', 'clone', self.url, '.'], self.repo_dir) + command_output(['git_repo', self.url, '--worktree .'], self.repo_dir) # If we get here, we didn't raise an error return except RuntimeError as e: