#pragma once
#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <sys/types.h>
#include <sys/select.h>
#endif
#include <assert.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#if defined(__Fuchsia__)
#include "dlopen_fuchsia.h"
#endif
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNX__) || defined(__FreeBSD__) || \
defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__)
#define COMMON_UNIX_PLATFORMS …
#else
#define COMMON_UNIX_PLATFORMS …
#endif
#if COMMON_UNIX_PLATFORMS
#include <unistd.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdlib.h>
#include <libgen.h>
#elif defined(_WIN32)
#if defined(CreateSemaphore)
#undef CreateSemaphore
#endif
#if defined(CreateEvent)
#undef CreateEvent
#endif
#include <stdio.h>
#include <io.h>
#include <shlwapi.h>
#include <direct.h>
#endif
#include "stack_allocation.h"
#if defined(APPLE_STATIC_LOADER) && !defined(__APPLE__)
#error "APPLE_STATIC_LOADER can only be defined on Apple platforms!"
#endif
#if defined(APPLE_STATIC_LOADER)
#define LOADER_EXPORT
#elif defined(__GNUC__) && __GNUC__ >= 4
#define LOADER_EXPORT …
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
#define LOADER_EXPORT …
#else
#define LOADER_EXPORT
#endif
#define MAX_STRING_SIZE …
#if !defined(MAX_NUM_UNKNOWN_EXTS)
#define MAX_NUM_UNKNOWN_EXTS …
#endif
#define VK_ICD_FILENAMES_ENV_VAR …
#define VK_DRIVER_FILES_ENV_VAR …
#define VK_LAYER_PATH_ENV_VAR …
#define VK_ADDITIONAL_DRIVER_FILES_ENV_VAR …
#define VK_ADDITIONAL_LAYER_PATH_ENV_VAR …
#define VK_LAYERS_ENABLE_ENV_VAR …
#define VK_LAYERS_DISABLE_ENV_VAR …
#define VK_LAYERS_ALLOW_ENV_VAR …
#define VK_DRIVERS_SELECT_ENV_VAR …
#define VK_DRIVERS_DISABLE_ENV_VAR …
#define VK_LOADER_DISABLE_ALL_LAYERS_VAR_1 …
#define VK_LOADER_DISABLE_ALL_LAYERS_VAR_2 …
#define VK_LOADER_DISABLE_ALL_LAYERS_VAR_3 …
#define VK_LOADER_DISABLE_IMPLICIT_LAYERS_VAR …
#define VK_LOADER_DISABLE_EXPLICIT_LAYERS_VAR …
#define VK_OVERRIDE_LAYER_NAME …
#define VK_LOADER_SETTINGS_FILENAME …
#define LAYERS_PATH_ENV …
#define ENABLED_LAYERS_ENV …
#if COMMON_UNIX_PLATFORMS
#define PATH_SEPARATOR …
#define DIRECTORY_SYMBOL …
#define VULKAN_DIR …
#define VULKAN_ICDCONF_DIR …
#define VULKAN_ICD_DIR …
#define VULKAN_SETTINGSCONF_DIR …
#define VULKAN_ELAYERCONF_DIR …
#define VULKAN_ILAYERCONF_DIR …
#define VULKAN_LAYER_DIR …
#define VK_DRIVERS_INFO_RELATIVE_DIR …
#define VK_SETTINGS_INFO_RELATIVE_DIR …
#define VK_ELAYERS_INFO_RELATIVE_DIR …
#define VK_ILAYERS_INFO_RELATIVE_DIR …
#define VK_DRIVERS_INFO_REGISTRY_LOC …
#define VK_ELAYERS_INFO_REGISTRY_LOC …
#define VK_ILAYERS_INFO_REGISTRY_LOC …
#define VK_SETTINGS_INFO_REGISTRY_LOC …
#if defined(__QNX__)
#ifndef SYSCONFDIR
#define SYSCONFDIR …
#endif
#endif
#define PRINTF_SIZE_T_SPECIFIER …
loader_platform_dl_handle;
loader_platform_thread;
loader_platform_thread_id;
loader_platform_thread_mutex;
loader_platform_thread_cond;
#elif defined(_WIN32)
#define PATH_SEPARATOR …
#define DIRECTORY_SYMBOL …
#define DEFAULT_VK_REGISTRY_HIVE …
#define DEFAULT_VK_REGISTRY_HIVE_STR …
#define SECONDARY_VK_REGISTRY_HIVE …
#define SECONDARY_VK_REGISTRY_HIVE_STR …
#define VK_DRIVERS_INFO_RELATIVE_DIR …
#define VK_SETTINGS_INFO_RELATIVE_DIR …
#define VK_ELAYERS_INFO_RELATIVE_DIR …
#define VK_ILAYERS_INFO_RELATIVE_DIR …
#define VK_VARIANT_REG_STR …
#define VK_VARIANT_REG_STR_W …
#define VK_DRIVERS_INFO_REGISTRY_LOC …
#define VK_ELAYERS_INFO_REGISTRY_LOC …
#define VK_ILAYERS_INFO_REGISTRY_LOC …
#define VK_SETTINGS_INFO_REGISTRY_LOC …
#define PRINTF_SIZE_T_SPECIFIER …
typedef HMODULE loader_platform_dl_handle;
typedef HANDLE loader_platform_thread;
typedef DWORD loader_platform_thread_id;
typedef CRITICAL_SECTION loader_platform_thread_mutex;
typedef CONDITION_VARIABLE loader_platform_thread_cond;
#else
#warning The "vk_loader_platform.h" file must be modified for this OS.
#endif
extern bool loader_disable_dynamic_library_unloading;
static inline bool loader_platform_is_path(const char *path) { … }
#if defined(APPLE_STATIC_LOADER)
static inline void loader_platform_thread_once_fn(pthread_once_t *ctl, void (*func)(void)) {
assert(func != NULL);
assert(ctl != NULL);
pthread_once(ctl, func);
}
#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION …
#define LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION …
#define LOADER_PLATFORM_THREAD_ONCE …
#else
#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) …
#define LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION(var) …
#define LOADER_PLATFORM_THREAD_ONCE(ctl, func) …
#endif
#if COMMON_UNIX_PLATFORMS
static inline bool loader_platform_file_exists(const char *path) { … }
static inline bool loader_platform_is_path_absolute(const char *path) { … }
static inline char *loader_platform_dirname(char *path) { … }
#if defined(__linux__) || defined(__GNU__)
static inline char *loader_platform_executable_path(char *buffer, size_t size) { … }
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
(void)size;
buffer[0] = '\0';
return buffer;
}
#endif
#if TARGET_OS_OSX
#include <libproc.h>
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
if (size > UINT32_MAX) {
return NULL;
}
pid_t pid = getpid();
int ret = proc_pidpath(pid, buffer, (uint32_t)size);
if (ret <= 0) {
return NULL;
}
buffer[ret] = '\0';
return buffer;
}
#endif
#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/sysctl.h>
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
int mib[] = {
CTL_KERN,
#if defined(__NetBSD__)
KERN_PROC_ARGS,
-1,
KERN_PROC_PATHNAME,
#else
KERN_PROC,
KERN_PROC_PATHNAME,
-1,
#endif
};
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buffer, &size, NULL, 0) < 0) {
return NULL;
}
return buffer;
}
#elif defined(__Fuchsia__) || defined(__OpenBSD__)
static inline char *loader_platform_executable_path(char *buffer, size_t size) { return NULL; }
#elif defined(__QNX__)
#ifndef SYSCONFDIR
#define SYSCONFDIR …
#endif
#include <fcntl.h>
#include <sys/stat.h>
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
int fd = open("/proc/self/exefile", O_RDONLY);
size_t rdsize;
if (fd == -1) {
return NULL;
}
rdsize = read(fd, buffer, size);
if (rdsize == size) {
return NULL;
}
buffer[rdsize] = 0x00;
close(fd);
return buffer;
}
#endif
#if !defined(__has_feature)
#define __has_feature …
#endif
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#define LOADER_ADDRESS_SANITIZER_ACTIVE …
#endif
#define LOADER_DLOPEN_MODE …
#if defined(__Fuchsia__)
static inline loader_platform_dl_handle loader_platform_open_driver(const char *libPath) {
return dlopen_fuchsia(libPath, LOADER_DLOPEN_MODE, true);
}
static inline loader_platform_dl_handle loader_platform_open_library(const char *libPath) {
return dlopen_fuchsia(libPath, LOADER_DLOPEN_MODE, false);
}
#else
static inline loader_platform_dl_handle loader_platform_open_library(const char *libPath) { … }
#endif
static inline const char *loader_platform_open_library_error(const char *libPath) { … }
static inline void loader_platform_close_library(loader_platform_dl_handle library) { … }
static inline void *loader_platform_get_proc_address(loader_platform_dl_handle library, const char *name) { … }
static inline const char *loader_platform_get_proc_address_error(const char *name) { … }
static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { … }
static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { … }
static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { … }
static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { … }
static inline void *thread_safe_strtok(char *str, const char *delim, char **saveptr) { … }
static inline FILE *loader_fopen(const char *fileName, const char *mode) { … }
static inline char *loader_strncat(char *dest, size_t dest_sz, const char *src, size_t count) { … }
static inline char *loader_strncpy(char *dest, size_t dest_sz, const char *src, size_t count) { … }
#elif defined(_WIN32)
static inline const char *LoaderPnpDriverRegistry() {
BOOL is_wow;
IsWow64Process(GetCurrentProcess(), &is_wow);
return is_wow ? "Vulkan" VK_VARIANT_REG_STR "DriverNameWow" : "Vulkan" VK_VARIANT_REG_STR "DriverName";
}
static inline const wchar_t *LoaderPnpDriverRegistryWide() {
BOOL is_wow;
IsWow64Process(GetCurrentProcess(), &is_wow);
return is_wow ? L"Vulkan" VK_VARIANT_REG_STR_W L"DriverNameWow" : L"Vulkan" VK_VARIANT_REG_STR_W L"DriverName";
}
static inline const char *LoaderPnpELayerRegistry() {
BOOL is_wow;
IsWow64Process(GetCurrentProcess(), &is_wow);
return is_wow ? "Vulkan" VK_VARIANT_REG_STR "ExplicitLayersWow" : "Vulkan" VK_VARIANT_REG_STR "ExplicitLayers";
}
static inline const wchar_t *LoaderPnpELayerRegistryWide() {
BOOL is_wow;
IsWow64Process(GetCurrentProcess(), &is_wow);
return is_wow ? L"Vulkan" VK_VARIANT_REG_STR_W L"ExplicitLayersWow" : L"Vulkan" VK_VARIANT_REG_STR_W L"ExplicitLayers";
}
static inline const char *LoaderPnpILayerRegistry() {
BOOL is_wow;
IsWow64Process(GetCurrentProcess(), &is_wow);
return is_wow ? "Vulkan" VK_VARIANT_REG_STR "ImplicitLayersWow" : "Vulkan" VK_VARIANT_REG_STR "ImplicitLayers";
}
static inline const wchar_t *LoaderPnpILayerRegistryWide() {
BOOL is_wow;
IsWow64Process(GetCurrentProcess(), &is_wow);
return is_wow ? L"Vulkan" VK_VARIANT_REG_STR_W L"ImplicitLayersWow" : L"Vulkan" VK_VARIANT_REG_STR_W L"ImplicitLayers";
}
static inline bool loader_platform_file_exists(const char *path) {
int path_utf16_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
if (path_utf16_size <= 0) {
return false;
}
wchar_t *path_utf16 = (wchar_t *)loader_stack_alloc(path_utf16_size * sizeof(wchar_t));
if (MultiByteToWideChar(CP_UTF8, 0, path, -1, path_utf16, path_utf16_size) != path_utf16_size) {
return false;
}
if (_waccess(path_utf16, 0) == -1)
return false;
else
return true;
}
static inline bool loader_platform_is_path_absolute(const char *path) {
if (!path || !*path) {
return false;
}
if (*path == DIRECTORY_SYMBOL || path[1] == ':') {
return true;
}
return false;
}
static inline char *loader_platform_dirname(char *path) {
char *current, *next;
for (current = path; *current != '\0'; current = next) {
next = strchr(current, DIRECTORY_SYMBOL);
if (next == NULL) {
if (current != path) *(current - 1) = '\0';
return path;
} else {
next++;
}
}
return path;
}
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
wchar_t *buffer_utf16 = (wchar_t *)loader_stack_alloc(size * sizeof(wchar_t));
DWORD ret = GetModuleFileNameW(NULL, buffer_utf16, (DWORD)size);
if (ret == 0) {
return NULL;
}
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
return NULL;
}
int buffer_utf8_size = WideCharToMultiByte(CP_UTF8, 0, buffer_utf16, -1, NULL, 0, NULL, NULL);
if (buffer_utf8_size <= 0 || (size_t)buffer_utf8_size > size) {
return NULL;
}
if (WideCharToMultiByte(CP_UTF8, 0, buffer_utf16, -1, buffer, buffer_utf8_size, NULL, NULL) != buffer_utf8_size) {
return NULL;
}
return buffer;
}
static inline loader_platform_dl_handle loader_platform_open_library(const char *lib_path) {
int lib_path_utf16_size = MultiByteToWideChar(CP_UTF8, 0, lib_path, -1, NULL, 0);
if (lib_path_utf16_size <= 0) {
return NULL;
}
wchar_t *lib_path_utf16 = (wchar_t *)loader_stack_alloc(lib_path_utf16_size * sizeof(wchar_t));
if (MultiByteToWideChar(CP_UTF8, 0, lib_path, -1, lib_path_utf16, lib_path_utf16_size) != lib_path_utf16_size) {
return NULL;
}
loader_platform_dl_handle lib_handle = LoadLibraryW(lib_path_utf16);
if (lib_handle == NULL && GetLastError() == ERROR_MOD_NOT_FOUND) {
lib_handle = LoadLibraryExW(lib_path_utf16, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
}
return lib_handle;
}
static inline const char *loader_platform_open_library_error(const char *libPath) {
static char errorMsg[512];
(void)snprintf(errorMsg, 511, "Failed to open dynamic library \"%s\" with error %lu", libPath, GetLastError());
return errorMsg;
}
static inline void loader_platform_close_library(loader_platform_dl_handle library) {
if (!loader_disable_dynamic_library_unloading) {
FreeLibrary(library);
} else {
(void)library;
}
}
static inline void *loader_platform_get_proc_address(loader_platform_dl_handle library, const char *name) {
assert(library);
assert(name);
return (void *)GetProcAddress(library, name);
}
static inline const char *loader_platform_get_proc_address_error(const char *name) {
static char errorMsg[120];
(void)snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
return errorMsg;
}
static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); }
static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); }
static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); }
static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); }
static inline void *thread_safe_strtok(char *str, const char *delimiters, char **context) {
return strtok_s(str, delimiters, context);
}
static inline FILE *loader_fopen(const char *fileName, const char *mode) {
FILE *file = NULL;
errno_t err = fopen_s(&file, fileName, mode);
if (err != 0) return NULL;
return file;
}
static inline char *loader_strncat(char *dest, size_t dest_sz, const char *src, size_t count) {
errno_t err = strncat_s(dest, dest_sz, src, count);
if (err != 0) return NULL;
return dest;
}
static inline char *loader_strncpy(char *dest, size_t dest_sz, const char *src, size_t count) {
errno_t err = strncpy_s(dest, dest_sz, src, count);
if (err != 0) return NULL;
return dest;
}
#else
#warning The "vk_loader_platform.h" file must be modified for this OS.
#endif