diff options
Diffstat (limited to 'cube/cube.cpp')
| -rw-r--r-- | cube/cube.cpp | 197 |
1 files changed, 88 insertions, 109 deletions
diff --git a/cube/cube.cpp b/cube/cube.cpp index abafc024..1d462b0e 100644 --- a/cube/cube.cpp +++ b/cube/cube.cpp @@ -375,48 +375,32 @@ struct Demo { const vk::DebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData); -#if defined(VK_USE_PLATFORM_WIN32_KHR) - void run(); - void create_window(); -#endif + template <WsiPlatform WSI_PLATFORM> + void create_window() = delete; + template <WsiPlatform WSI_PLATFORM> + void run() = delete; + template <WsiPlatform WSI_PLATFORM> + void execute(); #if defined(VK_USE_PLATFORM_XLIB_KHR) const char *init_xlib_connection(); - void create_xlib_window(); void handle_xlib_event(const XEvent *event); - void run_xlib(); #endif #if defined(VK_USE_PLATFORM_XCB_KHR) const char *init_xcb_connection(); void handle_xcb_event(const xcb_generic_event_t *event); - void run_xcb(); - void create_xcb_window(); #endif #if defined(VK_USE_PLATFORM_WAYLAND_KHR) const char *init_wayland_connection(); - void run_wayland(); - void create_wayland_window(); #endif #if defined(VK_USE_PLATFORM_DIRECTFB_EXT) void handle_directfb_event(const DFBInputEvent *event); - void run_directfb(); - void create_directfb_window(); -#endif -#if defined(VK_USE_PLATFORM_METAL_EXT) - void run(); #endif #if defined(VK_USE_PLATFORM_DISPLAY_KHR) vk::Result create_display_surface(); - void run_display(); -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - void run(); - void create_window(); #endif #if defined(VK_USE_PLATFORM_FUCHSIA) // Returns a layer name of static storage duration. const char *get_fuchsia_image_pipe_layer() const; - void create_flatland_view(); - void run(); #endif std::string name = "vkcubepp"; // Name to put on the window/icon @@ -3090,7 +3074,8 @@ vk::SurfaceFormatKHR Demo::pick_surface_format(const std::vector<vk::SurfaceForm } #if defined(VK_USE_PLATFORM_WIN32_KHR) -void Demo::run() { +template <> +void Demo::run<WsiPlatform::win32>() { if (!prepared) { return; } @@ -3103,7 +3088,8 @@ void Demo::run() { } } -void Demo::create_window() { +template <> +void Demo::create_window<WsiPlatform::win32>() { WNDCLASSEX win_class; // Initialize the window class structure: @@ -3159,7 +3145,8 @@ void Demo::create_window() { #endif #if defined(VK_USE_PLATFORM_XLIB_KHR) -void Demo::create_xlib_window() { +template <> +void Demo::create_window<WsiPlatform::xlib>() { const char *display_envar = getenv("DISPLAY"); if (display_envar == nullptr || display_envar[0] == '\0') { printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n"); @@ -3229,7 +3216,8 @@ void Demo::handle_xlib_event(const XEvent *event) { } } -void Demo::run_xlib() { +template <> +void Demo::run<WsiPlatform::xlib>() { while (!quit) { XEvent event; @@ -3295,7 +3283,8 @@ void Demo::handle_xcb_event(const xcb_generic_event_t *event) { } } -void Demo::run_xcb() { +template <> +void Demo::run<WsiPlatform::xcb>() { xcb_flush(connection); while (!quit) { @@ -3320,7 +3309,8 @@ void Demo::run_xcb() { } } -void Demo::create_xcb_window() { +template <> +void Demo::create_window<WsiPlatform::xcb>() { uint32_t value_mask, value_list[32]; xcb_window = xcb_generate_id(connection); @@ -3354,7 +3344,8 @@ void Demo::create_xcb_window() { #endif #if defined(VK_USE_PLATFORM_WAYLAND_KHR) -void Demo::run_wayland() { +template <> +void Demo::run<WsiPlatform::wayland>() { while (!quit) { if (pause) { wl_display_dispatch(wayland_display); @@ -3401,7 +3392,8 @@ static void handle_toplevel_close(void *data, xdg_toplevel *xdg_toplevel) { static const xdg_toplevel_listener toplevel_listener = {handle_toplevel_configure, handle_toplevel_close}; -void Demo::create_wayland_window() { +template <> +void Demo::create_window<WsiPlatform::wayland>() { if (!wm_base) { printf("Compositor did not provide the standard protocol xdg-wm-base\n"); fflush(stdout); @@ -3461,7 +3453,8 @@ void Demo::handle_directfb_event(const DFBInputEvent *event) { } } -void Demo::run_directfb() { +template <> +void Demo::run<WsiPlatform::directfb>() { while (!quit) { DFBInputEvent event; @@ -3480,7 +3473,8 @@ void Demo::run_directfb() { } } -void Demo::create_directfb_window() { +template <> +void Demo::create_window<WsiPlatform::directfb>() { DFBResult ret; ret = DirectFBInit(nullptr, nullptr); @@ -3518,7 +3512,8 @@ void Demo::create_directfb_window() { } #endif #if defined(VK_USE_PLATFORM_METAL_EXT) -void Demo::run() { +template <> +void Demo::run<WsiPlatform::metal>() { draw(); curFrame++; if (frameCount != UINT32_MAX && curFrame == frameCount) { @@ -3623,7 +3618,8 @@ vk::Result Demo::create_display_surface() { return inst.createDisplayPlaneSurfaceKHR(&createInfo, nullptr, &surface); } -void Demo::run_display() { +template <> +void Demo::run<WsiPlatform::display>() { while (!quit) { draw(); curFrame++; @@ -3638,7 +3634,8 @@ void Demo::run_display() { #if defined(VK_USE_PLATFORM_SCREEN_QNX) #include <sys/keycodes.h> -void Demo::run() { +template <> +void Demo::run<WsiPlatform::qnx>() { int size[2] = {0, 0}; screen_window_t win; int val; @@ -3741,7 +3738,8 @@ void Demo::run() { } } -void Demo::create_window() { +template <> +void Demo::create_window<WsiPlatform::qnx>() { const char *idstr = APP_SHORT_NAME; int size[2]; int usage = SCREEN_USAGE_VULKAN; @@ -3814,13 +3812,17 @@ const char *Demo::get_fuchsia_image_pipe_layer() const { } } -void Demo::create_flatland_view() { +void Demo::create_window<WsiPlatform::fuchsia_display>() { + // Nothing to do +} + +void Demo::create_window<WsiPlatform::fuchsia_scenic>() { if (flatland_view) return; zx::result<fidl::ClientEnd<fuchsia_io::Directory> > incoming_result = component::OpenServiceRoot(); if (incoming_result.is_error()) { printf("Failed to open incoming directory: %s\n", incoming_result.status_string()); - ERR_EXIT("Failed to open incoming directory", "create_flatland_view failure"); + ERR_EXIT("Failed to open incoming directory", "create_window<WsiPlatform::fuchsia_scenic> failure"); } incoming = std::move(incoming_result).value(); @@ -3837,7 +3839,7 @@ void Demo::create_flatland_view() { flatland_view = FlatlandView::Create(incoming.borrow(), std::move(*args.view_creation_token()), resize_callback, loop.dispatcher()); - if (!flatland_view) ERR_EXIT("Failed to created FlatlandView", "create_flatland_view failure"); + if (!flatland_view) ERR_EXIT("Failed to created FlatlandView", "create_window<WsiPlatform::fuchsia_scenic> failure"); view_creation_token = flatland_view->TakeChildViewCreationToken(); }; @@ -3850,13 +3852,13 @@ void Demo::create_flatland_view() { }); if (add_protocol_result.is_error()) { printf("Failed to add protocol to outgoing directory: %s\n", add_protocol_result.status_string()); - ERR_EXIT("Failed to add protocol to outgoing directory", "create_flatland_view failure"); + ERR_EXIT("Failed to add protocol to outgoing directory", "create_window<WsiPlatform::fuchsia_scenic> failure"); } zx::result<> serve_result = outgoing->ServeFromStartupInfo(); if (serve_result.is_error()) { printf("Failed to serve outgoing directory: %s\n", serve_result.status_string()); - ERR_EXIT("Failed to serve outgoing directory", "create_flatland_view failure"); + ERR_EXIT("Failed to serve outgoing directory", "create_window<WsiPlatform::fuchsia_scenic> failure"); } zx_status_t loop_status = ZX_OK; @@ -3866,8 +3868,8 @@ void Demo::create_flatland_view() { loop_status = loop.RunUntilIdle(); } } - -void Demo::run() { +template <> +void Demo::run<WsiPlatform::fuchsia_display>() { uint32_t num_frames = 60; uint32_t elapsed_frames = 0; static const float kMsPerSec = 1000; @@ -3907,6 +3909,12 @@ void Demo::run() { } } +template <> +void Demo::run<WsiPlatform::fuchsia_scenic>() { + // Uses the same run function,as fuchsia_display + run<WsiPlatform::fuschia_display>(); +} + #endif #if _WIN32 @@ -3923,7 +3931,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { break; case WM_PAINT: if (!demo.in_callback) { - demo.run(); + demo.run<WsiPlatform::win32>(); } break; case WM_GETMINMAXINFO: // set window's minimum size @@ -4018,7 +4026,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, demo.connection = hInstance; demo.name = "Vulkan Cube"; - demo.create_window(); + demo.create_window<WsiPlatform::win32>(); demo.create_surface(); demo.select_physical_device(); demo.init_vk_swapchain(); @@ -4058,69 +4066,38 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__QNX__) || defined(__GNU__) || \ defined(__Fuchsia__) -int main(int argc, char **argv) { - Demo demo; +template <WsiPlatform WSI_PLATFORM> +void Demo::execute() { + create_window<WSI_PLATFORM>(); - demo.init(argc, argv); + create_surface(); - 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.create_xcb_window(); - break; -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - case (WsiPlatform::xlib): - demo.create_xlib_window(); - break; -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - case (WsiPlatform::wayland): - demo.create_wayland_window(); - break; -#endif -#if defined(VK_USE_PLATFORM_DIRECTFB_EXT) - case (WsiPlatform::directfb): - demo.create_directfb_window(); - break; -#endif -#if defined(VK_USE_PLATFORM_SCREEN_QNX) - case (WsiPlatform::qnx): - demo.create_window(); - break; -#endif -#if defined(VK_USE_PLATFORM_DISPLAY_KHR) - case (WsiPlatform::display): - // select physical device because display surface creation needs a gpu to be selected. - demo.select_physical_device(); - break; -#endif -#if defined(VK_USE_PLATFORM_FUCHSIA) - case (WsiPlatform::fuchsia_display): - // nothing to do here - break; - case (WsiPlatform::fuchsia_scenic): - demo.create_flatland_view(); - break; -#endif - } + select_physical_device(); - demo.create_surface(); + init_vk_swapchain(); - if (demo.wsi_platform != WsiPlatform::display) { - demo.select_physical_device(); - } + prepare(); - demo.init_vk_swapchain(); + run<WSI_PLATFORM>(); +} - demo.prepare(); +template <> +void Demo::execute<WsiPlatform::display>() { + select_physical_device(); + + create_surface(); + + init_vk_swapchain(); + + prepare(); + + run<WsiPlatform::display>(); +} + +int main(int argc, char **argv) { + Demo demo; + + demo.init(argc, argv); switch (demo.wsi_platform) { default: @@ -4132,38 +4109,40 @@ int main(int argc, char **argv) { break; #if defined(VK_USE_PLATFORM_XCB_KHR) case (WsiPlatform::xcb): - demo.run_xcb(); + demo.execute<WsiPlatform::xcb>(); break; #endif #if defined(VK_USE_PLATFORM_XLIB_KHR) case (WsiPlatform::xlib): - demo.run_xlib(); + demo.execute<WsiPlatform::xlib>(); break; #endif #if defined(VK_USE_PLATFORM_WAYLAND_KHR) case (WsiPlatform::wayland): - demo.run_wayland(); + demo.execute<WsiPlatform::wayland>(); break; #endif #if defined(VK_USE_PLATFORM_DIRECTFB_EXT) case (WsiPlatform::directfb): - demo.run_directfb(); + demo.execute<WsiPlatform::directfb>(); break; #endif #if defined(VK_USE_PLATFORM_DISPLAY_KHR) case (WsiPlatform::display): - demo.run_display(); + demo.execute<WsiPlatform::display>(); break; #endif #if defined(VK_USE_PLATFORM_SCREEN_QNX) case (WsiPlatform::qnx): - demo.run(); + demo.execute<WsiPlatform::qnx>(); break; #endif #if defined(VK_USE_PLATFORM_FUCHSIA) case (WsiPlatform::fuchsia_display): + demo.execute<WsiPlatform::fuchsia_display>(); + break; case (WsiPlatform::fuchsia_scenic): - demo.run(); + demo.execute<WsiPlatform::fuchsia_scenic>(); break; #endif } |
