Skip to content

Commit

Permalink
Cocoa: Add fully dynamic loading of Vulkan loader
Browse files Browse the repository at this point in the history
If the application is not linked against the Vulkan loader and relies on
a third-party loader library or glfwGetInstanceProcAddress, then our
call to dlopen will fail without a custom dyld environment variable.

This adds a fallback of looking in the directory of the main executable,
which matches the bundle structure recommended by the Vulkan SDK, making
that finally work out of the box for fully dynamic loading.
  • Loading branch information
elmindreda committed Jan 16, 2020
1 parent 15d9180 commit 7da87aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ information on what to include when reporting a bug.
window (#1499)
- [Win32] Bugfix: Disabled cursor mode interfered with some non-client actions
- [Cocoa] Added support for `VK_EXT_metal_surface` (#1619)
- [Cocoa] Added locating the Vulkan loader at runtime in an application bundle
- [Cocoa] Removed dependency on the CoreVideo framework
- [Cocoa] Bugfix: `glfwSetWindowSize` used a bottom-left anchor point (#1553)
- [Cocoa] Bugfix: Window remained on screen after destruction until event poll
Expand Down
26 changes: 26 additions & 0 deletions src/cocoa_init.m
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,32 @@ - (void)applicationDidHide:(NSNotification *)notification
@end // GLFWApplicationDelegate


//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////

void* _glfwLoadLocalVulkanLoaderNS(void)
{
CFBundleRef bundle = CFBundleGetMainBundle();
if (!bundle)
return NULL;

CFURLRef url =
CFBundleCopyAuxiliaryExecutableURL(bundle, CFSTR("libvulkan.1.dylib"));
if (!url)
return NULL;

char path[PATH_MAX];
void* handle = NULL;

if (CFURLGetFileSystemRepresentation(url, true, (UInt8*) path, sizeof(path) - 1))
handle = _glfw_dlopen(path);

CFRelease(url);
return handle;
}


//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions src/cocoa_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,5 @@ void _glfwRestoreVideoModeNS(_GLFWmonitor* monitor);

float _glfwTransformYNS(float y);

void* _glfwLoadLocalVulkanLoaderNS(void);

2 changes: 2 additions & 0 deletions src/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ GLFWbool _glfwInitVulkan(int mode)
_glfw.vk.handle = _glfw_dlopen("vulkan-1.dll");
#elif defined(_GLFW_COCOA)
_glfw.vk.handle = _glfw_dlopen("libvulkan.1.dylib");
if (!_glfw.vk.handle)
_glfw.vk.handle = _glfwLoadLocalVulkanLoaderNS();
#else
_glfw.vk.handle = _glfw_dlopen("libvulkan.so.1");
#endif
Expand Down

0 comments on commit 7da87aa

Please sign in to comment.