Skip to content

More general vulkan loading #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ option(LOVR_BUILD_SHARED "Build a shared library (takes precedence over LOVR_BUI
option(LOVR_BUILD_BUNDLE "On macOS, build a .app bundle instead of a raw program" OFF)
option(LOVR_BUILD_WITH_SYMBOLS "Build with C function symbols exposed" OFF)

option(LOVR_LINK_VULKAN "Links vulkan with lovr bypassing the libvulkan search" OFF)

# Setup
if(EMSCRIPTEN)
string(CONCAT EMSCRIPTEN_LINKER_FLAGS
Expand Down Expand Up @@ -205,6 +207,10 @@ endif()
# Vulkan
if(LOVR_USE_VULKAN)
list(APPEND LOVR_INCLUDES deps/vulkan-headers/include)

if(LOVR_LINK_VULKAN)
find_package(Vulkan REQUIRED)
endif()
endif()

# OpenXR
Expand Down Expand Up @@ -350,6 +356,10 @@ target_link_libraries(lovr
${EMSCRIPTEN_LINKER_FLAGS}
)

if(LOVR_USE_VULKAN AND LOVR_LINK_VULKAN)
target_link_libraries(lovr PRIVATE Vulkan::Vulkan)
endif()

if(LOVR_SANITIZE)
set(LOVR_SANITIZE_FLAGS "-fsanitize=address,undefined" "-O1" "-fno-omit-frame-pointer")
target_compile_options(lovr PRIVATE ${LOVR_SANITIZE_FLAGS})
Expand Down
22 changes: 19 additions & 3 deletions src/core/gpu_vk.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gpu.h"
#include <string.h>
#include <threads.h>
#include <stdlib.h>
#include <stdatomic.h>

#ifdef _WIN32
Expand Down Expand Up @@ -2524,16 +2525,31 @@ bool gpu_init(gpu_config* config) {
state.library = LoadLibraryA("vulkan-1.dll");
ASSERT(state.library, "Failed to load vulkan library") goto fail;
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) GetProcAddress(state.library, "vkGetInstanceProcAddr");
#elif __APPLE__
state.library = dlopen("libvulkan.1.dylib", RTLD_NOW | RTLD_LOCAL);
#else
// Checking if vulkan was linked in (`-DLOVR_LINK_VULKAN` or `LD_PRELOAD`)
state.library = NULL;
void *selflib = dlopen(NULL, RTLD_NOW);
if (dlsym(selflib, "vkGetInstanceProcAddr"))
state.library = selflib;
else
dlclose(selflib);

#ifdef __APPLE__
if (!state.library) state.library = dlopen("libvulkan.1.dylib", RTLD_NOW | RTLD_LOCAL);
if (!state.library) state.library = dlopen("libvulkan.dylib", RTLD_NOW | RTLD_LOCAL);
if (!state.library) state.library = dlopen("libMoltenVK.dylib", RTLD_NOW | RTLD_LOCAL);
if (!state.library) state.library = dlopen("vulkan.framework/vulkan", RTLD_NOW | RTLD_LOCAL);
if (!state.library) state.library = dlopen("MoltenVK.framework/MoltenVK", RTLD_NOW | RTLD_LOCAL);
if (!state.library && getenv("DYLD_FALLBACK_LIBRARY_PATH") == NULL)
state.library = dlopen("/usr/local/lib/libvulkan.dylib", RTLD_NOW | RTLD_LOCAL);
ASSERT(state.library, "Failed to load vulkan library") goto fail;
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) dlsym(state.library, "vkGetInstanceProcAddr");
#else
state.library = dlopen("libvulkan.so.1", RTLD_NOW | RTLD_LOCAL);
if (!state.library) state.library = dlopen("libvulkan.so.1", RTLD_NOW | RTLD_LOCAL);
if (!state.library) state.library = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
ASSERT(state.library, "Failed to load vulkan library") goto fail;
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) dlsym(state.library, "vkGetInstanceProcAddr");
#endif
#endif
GPU_FOREACH_ANONYMOUS(GPU_LOAD_ANONYMOUS);

Expand Down