diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 0f19d2ec..3743a8fa 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -1,7 +1,6 @@ cmake_minimum_required(VERSION 3.30) -set(name ruisapp) -project(${name}) +project(ruisapp) # !!! find_package must go after project() declaration !!! # Otherwise VCPKG does not set the CMAKE_PREFIX_PATH to find packages. @@ -10,27 +9,27 @@ find_package(myci CONFIG REQUIRED) set(srcs) myci_add_source_files(srcs DIRECTORY - ../../src/${name} + ../../src/${PROJECT_NAME} RECURSIVE ) if(IOS) enable_language(OBJCXX) - list(APPEND srcs ../../src/${name}/glue/ios/glue.mm) + list(APPEND srcs ../../src/${PROJECT_NAME}/glue/ios/glue.mm) set(ruisapp_build_opengl_variant False) set(ruisapp_build_opengles_variant True) set(ruisapp_opengl_install_include_directory) # opengl variant will not be built, no headers to install - set(ruisapp_opengles_install_include_directory ../../src/${name}) + set(ruisapp_opengles_install_include_directory ../../src/${PROJECT_NAME}) elseif(APPLE) enable_language(OBJCXX) - list(APPEND srcs ../../src/${name}/glue/macos/glue.mm) + list(APPEND srcs ../../src/${PROJECT_NAME}/glue/macos/glue.mm) set(ruisapp_build_opengl_variant True) set(ruisapp_build_opengles_variant False) - set(ruisapp_opengl_install_include_directory ../../src/${name}) + set(ruisapp_opengl_install_include_directory ../../src/${PROJECT_NAME}) set(ruisapp_opengles_install_include_directory) # opengles variant will not be built, no headers to install elseif(LINUX) set(ruisapp_build_opengl_variant True) @@ -40,12 +39,22 @@ elseif(LINUX) # the headers are installed by the opengl-xorg variant, see below. set(ruisapp_opengl_install_include_directory) set(ruisapp_opengles_install_include_directory) -else() +elseif(WIN32) + set(ruisapp_build_opengl_variant True) + set(ruisapp_build_opengles_variant True) + set(ruisapp_build_vulkan_variant True) # VK_KHR_win32_surface + + set(ruisapp_opengl_install_include_directory ../../src/${PROJECT_NAME}) + set(ruisapp_opengles_install_include_directory) # opengl variant is also built and will install the headers + set(ruisapp_vulkan_install_include_directory) # opengl variant is also built and will install the headers +elseif(EMSCRIPTEN) set(ruisapp_build_opengl_variant True) set(ruisapp_build_opengles_variant True) - set(ruisapp_opengl_install_include_directory ../../src/${name}) + set(ruisapp_opengl_install_include_directory ../../src/${PROJECT_NAME}) set(ruisapp_opengles_install_include_directory) # opengl variant is also built and will install the headers +else() + message(FATAL_ERROR "Unknown OS") endif() # ==================== @@ -133,10 +142,10 @@ if(LINUX) set(install_include_directories) if(arg_INSTALL_HEADERS) - set(install_include_directories ../../src/${name}) + set(install_include_directories ../../src/${PROJECT_NAME}) endif() - myci_declare_library(${name}-${renderer}-${backend} + myci_declare_library(${PROJECT_NAME}-${renderer}-${backend} SOURCES ${srcs} PUBLIC_INCLUDE_DIRECTORIES @@ -163,7 +172,7 @@ if(LINUX) if(backend STREQUAL "wayland") # the generated xdg-shell client protocol source is added after the library # declaration, so that it is not picked up by the format targets - target_sources(${name}-${renderer}-${backend} + target_sources(${PROJECT_NAME}-${renderer}-${backend} PRIVATE ${wayland_xdg_shell_protocol_source} ) @@ -182,14 +191,14 @@ if(LINUX) # Compatibility aliases: for now ruisapp-opengl / ruisapp-opengles refer to the xorg variants. # Note: the namespaced alias refers to the plain (non-namespaced) target, the same # convention that myci_declare_library() uses for the ${PROJECT_NAME}:: alias. - add_library(${name}-opengl ALIAS ${name}-opengl-xorg) - add_library(${PROJECT_NAME}::${name}-opengl ALIAS ${name}-opengl-xorg) - add_library(${name}-opengles ALIAS ${name}-opengles-xorg) - add_library(${PROJECT_NAME}::${name}-opengles ALIAS ${name}-opengles-xorg) + add_library(${PROJECT_NAME}-opengl ALIAS ${PROJECT_NAME}-opengl-xorg) + add_library(${PROJECT_NAME}::${PROJECT_NAME}-opengl ALIAS ${PROJECT_NAME}-opengl-xorg) + add_library(${PROJECT_NAME}-opengles ALIAS ${PROJECT_NAME}-opengles-xorg) + add_library(${PROJECT_NAME}::${PROJECT_NAME}-opengles ALIAS ${PROJECT_NAME}-opengles-xorg) else() if(ruisapp_build_opengl_variant) - myci_declare_library(${name}-opengl + myci_declare_library(${PROJECT_NAME}-opengl SOURCES ${srcs} PUBLIC_INCLUDE_DIRECTORIES @@ -213,7 +222,7 @@ else() if(APPLE) # link to Cocoa framework - target_link_libraries(${name}-opengl + target_link_libraries(${PROJECT_NAME}-opengl PRIVATE # Link the Cocoa framework directly using the -framework flag string. # The quotes are important so CMake passes it as a single linker argument. @@ -221,15 +230,15 @@ else() ) # TODO: Define the GL_SILENCE_DEPRECATION macro to silence OpenGL deprecation warnings on Apple platforms. Need to move to Metal eventually. - target_compile_definitions(${name}-opengl PRIVATE GL_SILENCE_DEPRECATION) + target_compile_definitions(${PROJECT_NAME}-opengl PRIVATE GL_SILENCE_DEPRECATION) # TODO: some stuff (e.g. NSOpenGLContext) is deprecated on MacOS, so suppress the warnings for now. Need to use new APIs eventually. - target_compile_options(${name}-opengl PRIVATE -Wno-deprecated-declarations) + target_compile_options(${PROJECT_NAME}-opengl PRIVATE -Wno-deprecated-declarations) endif() endif() if(ruisapp_build_opengles_variant) - myci_declare_library(${name}-opengles + myci_declare_library(${PROJECT_NAME}-opengles SOURCES ${srcs} PUBLIC_INCLUDE_DIRECTORIES @@ -255,7 +264,7 @@ else() ) if(IOS) - target_link_libraries(${name}-opengles + target_link_libraries(${PROJECT_NAME}-opengles PRIVATE # Link the iOS frameworks directly using the -framework flag string. # The quotes are important so CMake passes it as a single linker argument. @@ -265,12 +274,33 @@ else() ) # TODO: Define the GLES_SILENCE_DEPRECATION macro to silence OpenGL ES deprecation warnings on Apple platforms. Need to move to Metal eventually. - target_compile_definitions(${name}-opengles PRIVATE GLES_SILENCE_DEPRECATION) + target_compile_definitions(${PROJECT_NAME}-opengles PRIVATE GLES_SILENCE_DEPRECATION) # TODO: some stuff (e.g. UI_USER_INTERFACE_IDIOM) is deprecated on iOS, so suppress the warnings for now. Need to use new APIs eventually. - target_compile_options(${name}-opengles PRIVATE -Wno-deprecated-declarations) + target_compile_options(${PROJECT_NAME}-opengles PRIVATE -Wno-deprecated-declarations) endif() endif() + + if(ruisapp_build_vulkan_variant) + myci_declare_library(${PROJECT_NAME}-vulkan + SOURCES + ${srcs} + PUBLIC_INCLUDE_DIRECTORIES + ../../src + INSTALL_INCLUDE_DIRECTORIES + ${ruisapp_vulkan_install_include_directory} + PREPROCESSOR_DEFINITIONS + "RUISAPP_RENDER_VULKAN" + DEPENDENCIES + nitki + ruis + ruis-render-vulkan + opros + setka + tml + NO_EXPORT + ) + endif() endif() # ==================== @@ -279,30 +309,29 @@ endif() if(LINUX) myci_export( TARGETS - ${name}-opengl-xorg - ${name}-opengles-xorg - ${name}-opengl-wayland - ${name}-opengles-wayland - ${name}-opengl-sdl - ${name}-opengles-sdl - ${name}-vulkan-xorg - ${name}-vulkan-wayland + ${PROJECT_NAME}-opengl-xorg + ${PROJECT_NAME}-opengles-xorg + ${PROJECT_NAME}-opengl-wayland + ${PROJECT_NAME}-opengles-wayland + ${PROJECT_NAME}-opengl-sdl + ${PROJECT_NAME}-opengles-sdl + ${PROJECT_NAME}-vulkan-xorg + ${PROJECT_NAME}-vulkan-wayland ) -elseif(ruisapp_build_opengl_variant AND ruisapp_build_opengles_variant) - myci_export( - TARGETS - ${name}-opengl - ${name}-opengles - ) -elseif(ruisapp_build_opengl_variant) - myci_export( - TARGETS - ${name}-opengl - ) -elseif(ruisapp_build_opengles_variant) +else() + set(ruisapp_export_targets) + if(ruisapp_build_opengl_variant) + list(APPEND ruisapp_export_targets ${PROJECT_NAME}-opengl) + endif() + if(ruisapp_build_opengles_variant) + list(APPEND ruisapp_export_targets ${PROJECT_NAME}-opengles) + endif() + if(ruisapp_build_vulkan_variant) + list(APPEND ruisapp_export_targets ${PROJECT_NAME}-vulkan) + endif() myci_export( TARGETS - ${name}-opengles + ${ruisapp_export_targets} ) endif() @@ -317,7 +346,7 @@ myci_add_source_files(app_srcs ) # Declare a GUI test app for the given ruisapp-- library variant. function(ruisapp_declare_test_app renderer backend) - set(test_app_name "${name}-${renderer}-${backend}-test") + set(test_app_name "${PROJECT_NAME}-${renderer}-${backend}-test") myci_declare_application(${test_app_name} GUI SOURCES @@ -325,7 +354,7 @@ function(ruisapp_declare_test_app renderer backend) RESOURCE_DIRECTORY ../../tests/app/res DEPENDENCIES - ${PROJECT_NAME}::${name}-${renderer}-${backend} + ${PROJECT_NAME}::${PROJECT_NAME}-${renderer}-${backend} ) endfunction() @@ -341,7 +370,7 @@ if(LINUX) ruisapp_declare_test_app(vulkan wayland) else() if(ruisapp_build_opengl_variant) - myci_declare_application(${name}-opengl-test + myci_declare_application(${PROJECT_NAME}-opengl-test GUI SOURCES ${app_srcs} @@ -352,14 +381,14 @@ else() ) if(APPLE) - set_target_properties(${name}-opengl-test PROPERTIES + set_target_properties(${PROJECT_NAME}-opengl-test PROPERTIES XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "io.github.cppfw.ruisapp-opengl-test" ) endif() endif() if(ruisapp_build_opengles_variant) - myci_declare_application(${name}-opengles-test + myci_declare_application(${PROJECT_NAME}-opengles-test GUI SOURCES ${app_srcs} @@ -370,12 +399,24 @@ else() ) if(APPLE) - set_target_properties(${name}-opengles-test PROPERTIES - MACOSX_BUNDLE_GUI_IDENTIFIER "io.github.cppfw.${name}-opengles-test" - XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "io.github.cppfw.${name}-opengles-test" + set_target_properties(${PROJECT_NAME}-opengles-test PROPERTIES + MACOSX_BUNDLE_GUI_IDENTIFIER "io.github.cppfw.${PROJECT_NAME}-opengles-test" + XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "io.github.cppfw.${PROJECT_NAME}-opengles-test" XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "948FQ58W9M" XCODE_ATTRIBUTE_CODE_SIGN_STYLE "Automatic" ) endif() endif() + + if(ruisapp_build_vulkan_variant) + myci_declare_application(${PROJECT_NAME}-vulkan-test + GUI + SOURCES + ${app_srcs} + RESOURCE_DIRECTORY + ../../tests/app/res + DEPENDENCIES + ruisapp::${PROJECT_NAME}-vulkan + ) + endif() endif() diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index fca24d20..4d7f3720 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -101,6 +101,7 @@ ruisapp::application::directories get_application_directories(std::string_view a application_glue::application_glue(const utki::version_duplet& gl_version) : gl_version(gl_version), +#ifndef RUISAPP_RENDER_VULKAN shared_gl_context_native_window(utki::make_shared( this->display, this->gl_version, @@ -110,18 +111,29 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : }, nullptr // no shared gl context )), +#endif +#ifdef RUISAPP_RENDER_VULKAN + device(make_vulkan_device()), +#endif render_factory([&]() { +#ifdef RUISAPP_RENDER_VULKAN + // Vulkan has no "current context" — a null surface is sufficient for the factory (resource creation) + return utki::make_shared( + utki::make_shared(this->device.get(), VK_NULL_HANDLE) + ); +#else utki::logcat_debug("application_glue::application_glue(): creating shared gl context", '\n'); -#ifdef RUISAPP_RENDER_OPENGL +# ifdef RUISAPP_RENDER_OPENGL using factory_type = ruis::render::opengl::factory; -#elif defined(RUISAPP_RENDER_OPENGLES) +# elif defined(RUISAPP_RENDER_OPENGLES) using factory_type = ruis::render::opengles::factory; -#else -# error "Unknown graphics API" -#endif +# else +# error "Unknown graphics API" +# endif auto c = utki::make_shared(this->shared_gl_context_native_window); utki::logcat_debug("application_glue::application_glue(): shared gl context created", '\n'); return c; +#endif }()), common_shaders(this->render_factory.get().make_shaders()), common_render_objects(utki::make_shared(this->render_factory.get())), @@ -180,12 +192,16 @@ app_window* application_glue::get_window(native_window::window_id_type id) ruisapp::window& application_glue::make_window(ruisapp::window_parameters window_params) { +#ifdef RUISAPP_RENDER_VULKAN + auto ruis_native_window = utki::make_shared(this->display, window_params); +#else auto ruis_native_window = utki::make_shared( this->display, this->gl_version, window_params, &this->shared_gl_context_native_window.get() ); +#endif auto ruis_context = utki::make_shared(ruis::context::parameters{ .post_to_ui_thread_function = @@ -194,7 +210,28 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window }, .updater = this->updater, .renderer = utki::make_shared( -#ifdef RUISAPP_RENDER_OPENGL +#ifdef RUISAPP_RENDER_VULKAN + utki::make_shared( + ruis_native_window, + [&]() { + // Create a VK_KHR_win32_surface from the window handle. The surface is owned + // by the surface provider (destroyed together with the render context). + VkWin32SurfaceCreateInfoKHR sci{}; + sci.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; + sci.hinstance = GetModuleHandle(nullptr); + sci.hwnd = ruis_native_window.get().get_hwnd(); + VkSurfaceKHR vk_surface = VK_NULL_HANDLE; + vkCreateWin32SurfaceKHR( + this->device.get().get_instance(), // + &sci, + nullptr, + &vk_surface + ); + return utki::make_shared(this->device.get(), vk_surface); + }(), + this->render_factory + ), +#elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) utki::make_shared(ruis_native_window, this->render_factory), diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index 8da8a90d..9f55ca18 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -24,9 +24,17 @@ along with this program. If not, see . #include #include #include +#include #include "../../application.hpp" +#ifdef RUISAPP_RENDER_VULKAN +# include +# include +# include +# include +#endif + #include "display.hxx" #include "window.hxx" @@ -68,14 +76,44 @@ public: } // namespace namespace { +#ifdef RUISAPP_RENDER_VULKAN +// The Vulkan device (instance, physical device, logical device and graphics queue) is shared +// across the render factory and all the windows, the same way it is done by the other +// ruisapp glue implementations that support Vulkan (e.g. Wayland and X11). +const utki::shared_ref make_vulkan_device() +{ + std::vector instance_extensions = {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME}; + std::vector device_extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + return utki::make_shared( + instance_extensions, // + device_extensions // + ); +} +#endif + class application_glue : public utki::destructable { const utki::version_duplet gl_version; utki::shared_ref display = utki::make_shared(); +#ifndef RUISAPP_RENDER_VULKAN const utki::shared_ref shared_gl_context_native_window; - const utki::shared_ref render_factory; +#endif +#ifdef RUISAPP_RENDER_VULKAN + // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). + const utki::shared_ref device; +#endif + // concrete factory type, #ifdef by renderer +#ifdef RUISAPP_RENDER_VULKAN + const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_OPENGL) + const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_OPENGLES) + const utki::shared_ref render_factory; +#else +# error "Unknown graphics API" +#endif const utki::shared_ref common_shaders; const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; diff --git a/src/ruisapp/glue/windows/glue.cxx b/src/ruisapp/glue/windows/glue.cxx index 16793eff..9cedd602 100644 --- a/src/ruisapp/glue/windows/glue.cxx +++ b/src/ruisapp/glue/windows/glue.cxx @@ -27,7 +27,15 @@ along with this program. If not, see . #include #include -#ifdef RUISAPP_RENDER_OPENGL +#ifdef RUISAPP_RENDER_VULKAN +# include +# include + +# include +# include +# include +# include +#elif defined(RUISAPP_RENDER_OPENGL) # include # include #elif defined(RUISAPP_RENDER_OPENGLES) diff --git a/src/ruisapp/glue/windows/window.cxx b/src/ruisapp/glue/windows/window.cxx index 89f9cb66..5f52e9a7 100644 --- a/src/ruisapp/glue/windows/window.cxx +++ b/src/ruisapp/glue/windows/window.cxx @@ -323,6 +323,24 @@ native_window::opengl_context_wrapper::~opengl_context_wrapper() } #endif +#ifdef RUISAPP_RENDER_VULKAN +native_window::native_window( + utki::shared_ref display, + const ruisapp::window_parameters& window_params +) : + display(std::move(display)), + window( + this->display.get().regular_window_class, // + window_params, + true // visible + ), + device_context(this->window) +{ + // No rendering context is created here: the Vulkan renderer creates a + // VkSurfaceKHR from the window handle (see application.cxx) and presentation + // is handled by the Vulkan render context (vkQueuePresentKHR). +} +#else native_window::native_window( utki::shared_ref display, const utki::version_duplet& gl_version, @@ -337,7 +355,7 @@ native_window::native_window( shared_gl_context_native_window != nullptr ), device_context(this->window), -#ifdef RUISAPP_RENDER_OPENGL +# ifdef RUISAPP_RENDER_OPENGL opengl_context( this->display, this->device_context, // @@ -345,7 +363,7 @@ native_window::native_window( gl_version, shared_gl_context_native_window ? shared_gl_context_native_window->opengl_context.context : NULL ) -#elif defined(RUISAPP_RENDER_OPENGLES) +# elif defined(RUISAPP_RENDER_OPENGLES) egl_config( this->display.get().egl_display, // gl_version, @@ -362,9 +380,9 @@ native_window::native_window( this->egl_config, shared_gl_context_native_window ? shared_gl_context_native_window->egl_context.context : EGL_NO_CONTEXT ) -#endif +# endif { -#ifdef RUISAPP_RENDER_OPENGL +# ifdef RUISAPP_RENDER_OPENGL if (wglGetCurrentContext() == NULL) { auto res = wglMakeCurrent( this->device_context.context, // @@ -379,12 +397,16 @@ native_window::native_window( throw std::runtime_error("GLEW initialization failed"); } } -#endif +# endif } +#endif void native_window::bind_rendering_context() { -#ifdef RUISAPP_RENDER_OPENGL +#ifdef RUISAPP_RENDER_VULKAN + // For Vulkan, there's no concept of binding a rendering context. + // The Vulkan context is thread-safe and doesn't need to be made current. +#elif defined(RUISAPP_RENDER_OPENGL) auto res = wglMakeCurrent( this->device_context.context, // this->opengl_context.context @@ -409,7 +431,10 @@ void native_window::bind_rendering_context() void native_window::swap_frame_buffers() { -#ifdef RUISAPP_RENDER_OPENGL +#ifdef RUISAPP_RENDER_VULKAN + // Not used by the Vulkan renderer: presentation is handled by the Vulkan + // render context itself (vkQueuePresentKHR). +#elif defined(RUISAPP_RENDER_OPENGL) SwapBuffers(this->device_context.context); #elif defined(RUISAPP_RENDER_OPENGLES) this->egl_surface.swap_frame_buffers(); diff --git a/src/ruisapp/glue/windows/window.hxx b/src/ruisapp/glue/windows/window.hxx index 6908b7c0..e49f7d4c 100644 --- a/src/ruisapp/glue/windows/window.hxx +++ b/src/ruisapp/glue/windows/window.hxx @@ -112,12 +112,27 @@ public: return this->window.handle; } + /** + * @brief The native window handle. + * Used by the Vulkan renderer to create a VK_KHR_win32_surface for presenting frames. + */ + HWND get_hwnd() const noexcept + { + return this->window.handle; + } + +#ifdef RUISAPP_RENDER_VULKAN + // The Vulkan renderer does not create any GL context, so the constructor + // does not take a GL version or a shared GL context window. + native_window(utki::shared_ref display, const ruisapp::window_parameters& window_params); +#else native_window( utki::shared_ref display, const utki::version_duplet& gl_version, const ruisapp::window_parameters& window_params, native_window* shared_gl_context_native_window ); +#endif void bind_rendering_context() override;