From 0ff40948dcaefd381353c740e87f15ccb51e16e2 Mon Sep 17 00:00:00 2001 From: Philip Rideout Date: Wed, 12 Sep 2018 17:33:08 -0700 Subject: [PATCH] Allow macOS developers to optionally use an SDK version of Vulkan. This looks for VULKAN_SDK at build time, and if present it tells Filament to use the `libvulkan.1.dylib` that's located there. Otherwise it falls back to our old loading strategy, which uses a bundled version of MoltenVK that allows us to avoid requiring installation of the LunarG SDK. I tried this out an it works for me... Closes issue #238. --- CMakeLists.txt | 13 +++++++++++++ libs/bluevk/src/BlueVKDarwin.cpp | 6 ++++++ samples/CMakeLists.txt | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67f076d618..53488f7d50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,6 +247,19 @@ else() set(IMPORT_EXECUTABLES ${FILAMENT}/${IMPORT_EXECUTABLES_DIR}/ImportExecutables-${CMAKE_BUILD_TYPE}.cmake) endif() +# ================================================================================================== +# Try to find Vulkan if the SDK is installed, otherwise fall back to the bundled version. +# This needs to stay in our top-level CMakeLists because it sets up variables that are used by the +# "bluevk" and "samples" targets. +# ================================================================================================== + +find_library(Vulkan_LIBRARY NAMES vulkan HINTS "$ENV{VULKAN_SDK}/lib" "$ENV{VULKAN_SDK}/macOS/lib") +if (Vulkan_LIBRARY) + set(Vulkan_FOUND ON) + message(STATUS "Found Vulkan library in SDK: ${Vulkan_LIBRARY}.") + add_definitions(-DFILAMENT_VKLIBRARY_PATH=\"${Vulkan_LIBRARY}\") +endif() + # ================================================================================================== # Sub-projects # ================================================================================================== diff --git a/libs/bluevk/src/BlueVKDarwin.cpp b/libs/bluevk/src/BlueVKDarwin.cpp index 13ebf04606..b925ee374e 100644 --- a/libs/bluevk/src/BlueVKDarwin.cpp +++ b/libs/bluevk/src/BlueVKDarwin.cpp @@ -28,12 +28,18 @@ static const char* VKLIBRARY_PATH = "libvulkan.1.dylib"; static void* module = nullptr; bool loadLibrary() { + +#ifndef FILAMENT_VKLIBRARY_PATH // Rather than looking in the working directory, look for the dylib in the same folder that the // executable lives in. This allows MacOS users to run Vulkan-based Filament apps from anywhere. const Path executableFolder = Path::getCurrentExecutable().getParent(); const Path dylibPath = executableFolder.concat(VKLIBRARY_PATH); const Path jsonPath = executableFolder.concat("MoltenVK_icd.json"); setenv("VK_ICD_FILENAMES", jsonPath.c_str(), 1); +#else + const Path dylibPath = FILAMENT_VKLIBRARY_PATH; +#endif + module = dlopen(dylibPath.c_str(), RTLD_NOW | RTLD_LOCAL); return module != nullptr; } diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 68eea6d24c..6a85778420 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -156,7 +156,8 @@ endif() # Copy the MoltenVK dylibs and JSON on MacOS # ================================================================================================== -if (APPLE) +if (APPLE AND NOT Vulkan_LIBRARY) + message(STATUS "No Vulkan SDK was found, using prebuilt MoltenVK.") set(MOLTENVK_DIR "../third_party/moltenvk") configure_file( ${MOLTENVK_DIR}/libvulkan.1.dylib