| 1 |
|
| 2 |
/* |
| 3 |
* Copyright (C) 2018 The Android Open Source Project |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <utils/Path.h> |
| 19 |
|
| 20 |
#include <dlfcn.h> |
| 21 |
|
| 22 |
using utils::Path; |
| 23 |
|
| 24 |
namespace bluevk { |
| 25 |
|
| 26 |
#ifdef IOS |
| 27 |
static const char* VKLIBRARY_PATH = "Frameworks/libMoltenVK.dylib"; |
| 28 |
#else |
| 29 |
static const char* VKLIBRARY_PATH = "libvulkan.1.dylib"; |
| 30 |
#endif |
| 31 |
|
| 32 |
static void* module = nullptr; |
| 33 |
|
| 34 |
bool loadLibrary() { |
| 35 |
|
| 36 |
#ifndef FILAMENT_VKLIBRARY_PATH |
| 37 |
// Rather than looking in the working directory, look for the dylib in the same folder that the |
| 38 |
// executable lives in. This allows MacOS users to run Vulkan-based Filament apps from anywhere. |
| 39 |
const Path executableFolder = Path::getCurrentExecutable().getParent(); |
| 40 |
const Path dylibPath = executableFolder.concat(VKLIBRARY_PATH); |
| 41 |
|
| 42 |
// Provide a value for VK_ICD_FILENAMES only if it has not already been set. |
| 43 |
const char* icd = getenv("VK_ICD_FILENAMES"); |
| 44 |
if (icd == nullptr) { |
| 45 |
const Path jsonPath = executableFolder.concat("MoltenVK_icd.json"); |
| 46 |
setenv("VK_ICD_FILENAMES", jsonPath.c_str(), 1); |
| 47 |
} |
| 48 |
#else |
| 49 |
const Path dylibPath = FILAMENT_VKLIBRARY_PATH; |
| 50 |
#endif |
| 51 |
|
| 52 |
module = dlopen(dylibPath.c_str(), RTLD_NOW | RTLD_LOCAL); |
| 53 |
if (module == nullptr) { |
| 54 |
printf("%s", dlerror()); |
| 55 |
} |
| 56 |
return module != nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
void* getInstanceProcAddr() { |
| 60 |
return dlsym(module, "vkGetInstanceProcAddr"); |
| 61 |
} |
| 62 |
|
| 63 |
} // namespace bluevk |