From fee0e530aa17af4336f55ca82c7f6cb5931793c3 Mon Sep 17 00:00:00 2001 From: secrart <79920995+secrart@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:58:08 -0400 Subject: [PATCH 1/4] feat: Add new glfwAddCocoaMTKSubview function this new function allows glfw to better interface with Apple's metal API. Before one had to directly interface with the native cocoa window and replace GLFW's content view. Thus removing GLFW's ability to detect input from the window. Now this function gives user's the ability to add a Metal MTKView as a "subview" which allows the default glfw window view to still capture input while also displaying a Metal view. --- README.md | 1 + include/GLFW/glfw3native.h | 14 ++++++++++++++ src/cocoa_window.m | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index fe5a2c76..a990d108 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ information on what to include when reporting a bug. ## Changelog + - Added `glfwAddCocoaMTKSubview` function for better metal support - Added `GLFW_PLATFORM` init hint for runtime platform selection (#1958) - Added `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, `GLFW_PLATFORM_COCOA`, `GLFW_PLATFORM_WAYLAND`, `GLFW_PLATFORM_X11` and `GLFW_PLATFORM_NULL` symbols to diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index 36934120..2669603e 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -275,6 +275,20 @@ GLFWAPI CGDirectDisplayID glfwGetCocoaMonitor(GLFWmonitor* monitor); * @ingroup native */ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* window); + +/*! @brief Adds an 'MTKView' to the window's main NSView as a subview + * + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED. + * + * @thread_safety Call this function from the main thread. + * + * @since Added in version 3.3. + * + * @ingroup native + */ +GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* window, void* view); + #endif #if defined(GLFW_EXPOSE_NATIVE_NSGL) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 3726f782..81bd9aec 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -30,6 +30,7 @@ #include #include +#import // Returns the style mask corresponding to the window settings // @@ -1950,3 +1951,20 @@ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle) return window->ns.object; } +GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* handle, void* view) { + + _GLFWwindow* window = (_GLFWwindow*) handle; + _GLFW_REQUIRE_INIT_OR_RETURN(); + + if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA) + { + _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, + "Cocoa: Platform not initialized"); + return; + } + + [window->ns.view addSubview: (MTKView*) view]; + + +} + From a857a1d1b5d9dedfbd1e5ed2c8c91ee464cd28ef Mon Sep 17 00:00:00 2001 From: secrart <79920995+secrart@users.noreply.github.com> Date: Mon, 13 Jun 2022 13:00:30 -0400 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a990d108..86d66f8d 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ information on what to include when reporting a bug. ## Changelog - - Added `glfwAddCocoaMTKSubview` function for better metal support + - [Cocoa] Added `glfwAddCocoaMTKSubview` function for better metal support - Added `GLFW_PLATFORM` init hint for runtime platform selection (#1958) - Added `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, `GLFW_PLATFORM_COCOA`, `GLFW_PLATFORM_WAYLAND`, `GLFW_PLATFORM_X11` and `GLFW_PLATFORM_NULL` symbols to From 389affc86bade42b8c394500fd1f14729d826af1 Mon Sep 17 00:00:00 2001 From: secrart <79920995+secrart@users.noreply.github.com> Date: Mon, 13 Jun 2022 19:06:09 -0400 Subject: [PATCH 3/4] Add new function glfwResetCocoaMTKFrame - glfwResetCocoaFrame is a helper function that allows glfw to resize an MTKView - also adds CMake configurations to allow Metal integration to be optional. --- CMakeLists.txt | 2 ++ include/GLFW/glfw3native.h | 14 ++++++++++++ src/CMakeLists.txt | 3 +++ src/cocoa_window.m | 47 +++++++++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5e538bf..79737bf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,8 @@ cmake_dependent_option(GLFW_USE_HYBRID_HPG "Force use of high-performance GPU on cmake_dependent_option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON "MSVC" OFF) +cmake_dependent_option(GLFW_USE_METALKIT "Allow GLFW to include MetalKit.h for metal support" OFF GLFW_BUILD_COCOA ON) + set(GLFW_LIBRARY_TYPE "${GLFW_LIBRARY_TYPE}" CACHE STRING "Library type override for GLFW (SHARED, STATIC, OBJECT, or empty to follow BUILD_SHARED_LIBS)") diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index 2669603e..99b4894d 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -289,6 +289,20 @@ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* window); */ GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* window, void* view); + +/*! @brief Resizes an 'MTKView' to the window's framebuffer size -helpful because the Apple metal-cpp API lacks the ability to do this + * + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED. + * + * @thread_safety Call this function from the main thread. + * + * @since Added in version 3.3. + * + * @ingroup native + */ +GLFWAPI void glfwResetCocoaMTKFramesize(GLFWwindow* window, void* view); + #endif #if defined(GLFW_EXPOSE_NATIVE_NSGL) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 01f191c9..030f4e1c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,6 +31,9 @@ set_target_properties(update_mappings PROPERTIES FOLDER "GLFW3") if (GLFW_BUILD_COCOA) target_compile_definitions(glfw PRIVATE _GLFW_COCOA) + if(GLFW_USE_METALKIT) + target_compile_definitions(glfw PRIVATE _GLFW_BUILD_METAL_DEPENDENCIES) + endif() target_sources(glfw PRIVATE cocoa_platform.h cocoa_joystick.h cocoa_init.m cocoa_joystick.m cocoa_monitor.m cocoa_window.m nsgl_context.m) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 81bd9aec..bca520b9 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -30,7 +30,6 @@ #include #include -#import // Returns the style mask corresponding to the window settings // @@ -276,6 +275,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; window->ns.height = contentRect.size.height; _glfwInputWindowSize(window, contentRect.size.width, contentRect.size.height); } + } - (void)windowDidMove:(NSNotification *)notification @@ -363,6 +363,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; [self updateTrackingAreas]; [self registerForDraggedTypes:@[NSPasteboardTypeURL]]; + } return self; @@ -1951,6 +1952,9 @@ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle) return window->ns.object; } + +#ifdef _GLFW_BUILD_METAL_DEPENDENCIES +#import GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* handle, void* view) { _GLFWwindow* window = (_GLFWwindow*) handle; @@ -1968,3 +1972,44 @@ GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* handle, void* view) { } +GLFWAPI void glfwResetCocoaMTKFramesize(GLFWwindow* handle, void* view) { + + _GLFWwindow* window = (_GLFWwindow*) handle; + _GLFW_REQUIRE_INIT_OR_RETURN(); + + if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA) + { + _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, + "Cocoa: Platform not initialized"); + return; + } + + MTKView* mtkView = (MTKView*) view; + int width, height; + _glfwGetFramebufferSizeCocoa(window, &width, &height); + NSSize size = NSMakeSize(width, height); + [mtkView setFrameSize: size]; + + +} + +#else +GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* handle, void* view) { + + _GLFW_REQUIRE_INIT_OR_RETURN(); + _glfwInputError(GLFW_PLATFORM_ERROR, + "GLFW was compiled without \"GLFW_USE_METALKIT\" enabled"); + + +} + +GLFWAPI void glfwResetCocoaMTKFramesize(GLFWwindow* handle, void* view) { + + _GLFW_REQUIRE_INIT_OR_RETURN(); + _glfwInputError(GLFW_PLATFORM_ERROR, + "GLFW was compiled without \"GLFW_USE_METALKIT\" enabled"); + +} + + +#endif From 2771b709ea66c1b15437a69a6b4d6ba97c82b0f7 Mon Sep 17 00:00:00 2001 From: secrart <79920995+secrart@users.noreply.github.com> Date: Mon, 13 Jun 2022 23:12:17 -0400 Subject: [PATCH 4/4] Add CMake options for metal support metal support is fully optional now updated README with changelog --- README.md | 2 ++ src/CMakeLists.txt | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 86d66f8d..6958f183 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ information on what to include when reporting a bug. ## Changelog + - Added `GLFW_USE_METALKIT` to cmake build options to make metal support optional + - [Cocoa] Added `glfwResetCocoaMTKFramesize` function for metal view resizing - [Cocoa] Added `glfwAddCocoaMTKSubview` function for better metal support - Added `GLFW_PLATFORM` init hint for runtime platform selection (#1958) - Added `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, `GLFW_PLATFORM_COCOA`, diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 030f4e1c..5bcb5993 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -164,6 +164,15 @@ if (GLFW_BUILD_COCOA) "-framework IOKit" "-framework CoreFoundation") + if(GLFW_USE_METALKIT) + + target_link_libraries(glfw PRIVATE "-framework Metal" + "-framework MetalKit") + + endif() + + + set(glfw_PKG_DEPS "") set(glfw_PKG_LIBS "-framework Cocoa -framework IOKit -framework CoreFoundation") endif()