Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 2 | * |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 3 | * Copyright (c) 2015-2016 The Khronos Group Inc. |
| 4 | * Copyright (c) 2015-2016 Valve Corporation |
| 5 | * Copyright (c) 2015-2016 LunarG, Inc. |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 6 | * |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and/or associated documentation files (the "Materials"), to |
| 9 | * deal in the Materials without restriction, including without limitation the |
| 10 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | * sell copies of the Materials, and to permit persons to whom the Materials are |
| 12 | * furnished to do so, subject to the following conditions: |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 13 | * |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 14 | * The above copyright notice(s) and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Materials. |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 16 | * |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 17 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 20 | * |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 21 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE MATERIALS. |
| 25 | * |
| 26 | * Author: Ian Elliot <ian@lunarg.com> |
| 27 | * Author: Jon Ashburn <jon@lunarg.com> |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 28 | * |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 29 | */ |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 30 | #pragma once |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 31 | |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 32 | #if defined(_WIN32) |
| 33 | // WinSock2.h must be included *BEFORE* windows.h |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 34 | #include <WinSock2.h> |
| 35 | #endif // _WIN32 |
| 36 | |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 37 | #include "vulkan/vk_platform.h" |
| 38 | #include "vulkan/vk_sdk_platform.h" |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 39 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 40 | #if defined(__linux__) |
| 41 | /* Linux-specific common code: */ |
| 42 | |
| 43 | // Headers: |
| 44 | //#define _GNU_SOURCE 1 |
| 45 | // TBD: Are the contents of the following file used? |
| 46 | #include <unistd.h> |
| 47 | // Note: The following file is for dynamic loading: |
| 48 | #include <dlfcn.h> |
| 49 | #include <pthread.h> |
| 50 | #include <assert.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 51 | #include <string.h> |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 52 | #include <stdbool.h> |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 53 | #include <stdlib.h> |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 54 | #include <libgen.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 55 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 56 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 57 | #define PATH_SEPERATOR ':' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 58 | #define DIRECTORY_SYMBOL '/' |
| 59 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 60 | #define VULKAN_ICDCONF_DIR \ |
| 61 | "/" \ |
| 62 | "vulkan" \ |
| 63 | "/" \ |
| 64 | "icd.d" |
| 65 | #define VULKAN_ICD_DIR \ |
| 66 | "/" \ |
| 67 | "vulkan" \ |
| 68 | "/" \ |
| 69 | "icd" |
| 70 | #define VULKAN_ELAYERCONF_DIR \ |
| 71 | "/" \ |
| 72 | "vulkan" \ |
| 73 | "/" \ |
| 74 | "explicit_layer.d" |
| 75 | #define VULKAN_ILAYERCONF_DIR \ |
| 76 | "/" \ |
| 77 | "vulkan" \ |
| 78 | "/" \ |
| 79 | "implicit_layer.d" |
| 80 | #define VULKAN_LAYER_DIR \ |
| 81 | "/" \ |
| 82 | "vulkan" \ |
| 83 | "/" \ |
| 84 | "layer" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 85 | |
| 86 | #if defined(LOCALPREFIX) |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 87 | #define LOCAL_DRIVERS_INFO \ |
| 88 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" LOCALPREFIX \ |
| 89 | "/" DATADIR VULKAN_ICDCONF_DIR ":" |
| 90 | #define LOCAL_ELAYERS_INFO \ |
| 91 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" LOCALPREFIX \ |
| 92 | "/" DATADIR VULKAN_ELAYERCONF_DIR ":" |
| 93 | #define LOCAL_ILAYERS_INFO \ |
| 94 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" LOCALPREFIX \ |
| 95 | "/" DATADIR VULKAN_ILAYERCONF_DIR ":" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 96 | #else |
| 97 | #define LOCAL_DRIVERS_INFO |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 98 | #define LOCAL_ELAYERS_INFO |
| 99 | #define LOCAL_ILAYERS_INFO |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 102 | #define DEFAULT_VK_DRIVERS_INFO \ |
| 103 | LOCAL_DRIVERS_INFO \ |
| 104 | "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 105 | "/usr/" DATADIR VULKAN_ICDCONF_DIR |
Daniel Dadap | 6e62684 | 2015-09-30 12:09:49 -0500 | [diff] [blame] | 106 | #define DEFAULT_VK_DRIVERS_PATH "" |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 107 | #define DEFAULT_VK_ELAYERS_INFO \ |
| 108 | LOCAL_ELAYERS_INFO \ |
| 109 | "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \ |
Jon Ashburn | b682221 | 2016-02-16 15:34:16 -0700 | [diff] [blame] | 110 | "/usr/" DATADIR VULKAN_ELAYERCONF_DIR |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 111 | #define DEFAULT_VK_ILAYERS_INFO \ |
| 112 | LOCAL_ILAYERS_INFO \ |
| 113 | "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 114 | "/usr/" DATADIR VULKAN_ILAYERCONF_DIR |
Daniel Dadap | 6e62684 | 2015-09-30 12:09:49 -0500 | [diff] [blame] | 115 | #define DEFAULT_VK_LAYERS_PATH "" |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 116 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Jon Ashburn | b682221 | 2016-02-16 15:34:16 -0700 | [diff] [blame] | 117 | #define HOME_VK_DRIVERS_INFO "/.local/share" VULKAN_ICDCONF_DIR |
| 118 | #define HOME_VK_ELAYERS_INFO "/.local/share" VULKAN_ELAYERCONF_DIR |
| 119 | #define HOME_VK_ILAYERS_INFO "/.local/share" VULKAN_ILAYERCONF_DIR |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 120 | |
Ian Elliott | e536946 | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 121 | // C99: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 122 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 123 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 124 | // File IO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 125 | static inline bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 126 | if (access(path, F_OK)) |
| 127 | return false; |
| 128 | else |
| 129 | return true; |
| 130 | } |
| 131 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 132 | static inline bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 133 | if (path[0] == '/') |
| 134 | return true; |
| 135 | else |
| 136 | return false; |
| 137 | } |
| 138 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 139 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 140 | return dirname(path); |
| 141 | } |
| 142 | |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 143 | // Environment variables |
| 144 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 145 | static inline char *loader_getenv(const char *name) { return getenv(name); } |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 146 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 147 | static inline void loader_free_getenv(const char *val) {} |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 148 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 149 | // Dynamic Loading of libraries: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 150 | typedef void *loader_platform_dl_handle; |
| 151 | static inline loader_platform_dl_handle |
| 152 | loader_platform_open_library(const char *libPath) { |
Chia-I Wu | 6a21834 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 153 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 154 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 155 | static inline const char * |
| 156 | loader_platform_open_library_error(const char *libPath) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 157 | return dlerror(); |
| 158 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 159 | static inline void |
| 160 | loader_platform_close_library(loader_platform_dl_handle library) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 161 | dlclose(library); |
| 162 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 163 | static inline void * |
| 164 | loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 165 | const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 166 | assert(library); |
| 167 | assert(name); |
| 168 | return dlsym(library, name); |
| 169 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 170 | static inline const char * |
| 171 | loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 172 | return dlerror(); |
| 173 | } |
| 174 | |
| 175 | // Threads: |
| 176 | typedef pthread_t loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 177 | #define THREAD_LOCAL_DECL __thread |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 178 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 179 | pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 180 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var; |
| 181 | static inline void loader_platform_thread_once(pthread_once_t *ctl, |
| 182 | void (*func)(void)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 183 | assert(func != NULL); |
| 184 | assert(ctl != NULL); |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 185 | pthread_once(ctl, func); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Thread IDs: |
| 189 | typedef pthread_t loader_platform_thread_id; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 190 | static inline loader_platform_thread_id loader_platform_get_thread_id() { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 191 | return pthread_self(); |
| 192 | } |
| 193 | |
| 194 | // Thread mutex: |
| 195 | typedef pthread_mutex_t loader_platform_thread_mutex; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 196 | static inline void |
| 197 | loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 198 | pthread_mutex_init(pMutex, NULL); |
| 199 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 200 | static inline void |
| 201 | loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 202 | pthread_mutex_lock(pMutex); |
| 203 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 204 | static inline void |
| 205 | loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 206 | pthread_mutex_unlock(pMutex); |
| 207 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 208 | static inline void |
| 209 | loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 210 | pthread_mutex_destroy(pMutex); |
| 211 | } |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 212 | typedef pthread_cond_t loader_platform_thread_cond; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 213 | static inline void |
| 214 | loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 215 | pthread_cond_init(pCond, NULL); |
| 216 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 217 | static inline void |
| 218 | loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, |
| 219 | loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 220 | pthread_cond_wait(pCond, pMutex); |
| 221 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 222 | static inline void |
| 223 | loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 224 | pthread_cond_broadcast(pCond); |
| 225 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 226 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 227 | #define loader_stack_alloc(size) alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 228 | |
| 229 | #elif defined(_WIN32) // defined(__linux__) |
| 230 | /* Windows-specific common code: */ |
Tobin Ehlis | 890adf1 | 2015-11-02 15:45:19 -0700 | [diff] [blame] | 231 | // WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent |
| 232 | // undefine them to avoid conflicts with VkLayerDispatchTable struct members. |
| 233 | #ifdef CreateSemaphore |
| 234 | #undef CreateSemaphore |
| 235 | #endif |
| 236 | #ifdef CreateEvent |
| 237 | #undef CreateEvent |
| 238 | #endif |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 239 | #include <assert.h> |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 240 | #include <stdio.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 241 | #include <string.h> |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 242 | #include <io.h> |
| 243 | #include <stdbool.h> |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 244 | #include <shlwapi.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 245 | #ifdef __cplusplus |
| 246 | #include <iostream> |
| 247 | #include <string> |
| 248 | using namespace std; |
| 249 | #endif // __cplusplus |
| 250 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 251 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 252 | #define PATH_SEPERATOR ';' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 253 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 254 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
| 255 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers" |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 256 | // TODO: Are these the correct paths |
scygan | 713b1b0 | 2015-06-01 19:47:08 +0200 | [diff] [blame] | 257 | #define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 258 | #define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers" |
| 259 | #define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers" |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 260 | #define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 261 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Jon Ashburn | 8365786 | 2016-02-17 13:18:08 -0700 | [diff] [blame] | 262 | #define HOME_VK_DRIVERS_INFO "" |
| 263 | #define HOME_VK_ELAYERS_INFO "" |
| 264 | #define HOME_VK_ILAYERS_INFO "" |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 265 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
Courtney Goeltzenleuchter | 22d8d79 | 2015-10-07 17:03:42 -0600 | [diff] [blame] | 266 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 267 | // File IO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 268 | static bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 269 | if ((_access(path, 0)) == -1) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 270 | return false; |
| 271 | else |
| 272 | return true; |
| 273 | } |
| 274 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 275 | static bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 276 | return !PathIsRelative(path); |
| 277 | } |
| 278 | |
| 279 | // WIN32 runtime doesn't have dirname(). |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 280 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 281 | char *current, *next; |
| 282 | |
| 283 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 284 | |
| 285 | for (current = path; *current != '\0'; current = next) { |
| 286 | next = strchr(current, DIRECTORY_SYMBOL); |
| 287 | if (next == NULL) { |
| 288 | if (current != path) |
| 289 | *(current - 1) = '\0'; |
| 290 | return path; |
| 291 | } else { |
| 292 | // Point one character past the DIRECTORY_SYMBOL: |
| 293 | next++; |
| 294 | } |
| 295 | } |
| 296 | return path; |
| 297 | } |
| 298 | |
| 299 | // WIN32 runtime doesn't have basename(). |
| 300 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 301 | // so this is just a temporary solution in order to get us compiling, so that we |
| 302 | // can test some scenarios, and develop the correct solution for Windows. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 303 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 304 | // temporary code: |
| 305 | static char *loader_platform_basename(char *pathname) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 306 | char *current, *next; |
| 307 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 308 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 309 | |
| 310 | for (current = pathname; *current != '\0'; current = next) { |
| 311 | next = strchr(current, DIRECTORY_SYMBOL); |
| 312 | if (next == NULL) { |
| 313 | // No more DIRECTORY_SYMBOL's so return p: |
| 314 | return current; |
| 315 | } else { |
| 316 | // Point one character past the DIRECTORY_SYMBOL: |
| 317 | next++; |
| 318 | } |
| 319 | } |
| 320 | // We shouldn't get to here, but this makes the compiler happy: |
| 321 | return current; |
| 322 | } |
| 323 | |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 324 | // Environment variables |
| 325 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 326 | static inline char *loader_getenv(const char *name) { |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 327 | char *retVal; |
| 328 | DWORD valSize; |
| 329 | |
| 330 | valSize = GetEnvironmentVariableA(name, NULL, 0); |
| 331 | |
| 332 | // valSize DOES include the null terminator, so for any set variable |
| 333 | // will always be at least 1. If it's 0, the variable wasn't set. |
| 334 | if (valSize == 0) |
| 335 | return NULL; |
| 336 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 337 | // TODO; FIXME This should be using any app defined memory allocation |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 338 | retVal = (char *)malloc(valSize); |
| 339 | |
| 340 | GetEnvironmentVariableA(name, retVal, valSize); |
| 341 | |
| 342 | return retVal; |
| 343 | } |
| 344 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 345 | static inline void loader_free_getenv(const char *val) { free((void *)val); } |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 346 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 347 | // Dynamic Loading: |
| 348 | typedef HMODULE loader_platform_dl_handle; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 349 | static loader_platform_dl_handle |
| 350 | loader_platform_open_library(const char *libPath) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 351 | return LoadLibrary(libPath); |
| 352 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 353 | static char *loader_platform_open_library_error(const char *libPath) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 354 | static char errorMsg[120]; |
| 355 | snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath); |
| 356 | return errorMsg; |
| 357 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 358 | static void loader_platform_close_library(loader_platform_dl_handle library) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 359 | FreeLibrary(library); |
| 360 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 361 | static void *loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 362 | const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 363 | assert(library); |
| 364 | assert(name); |
| 365 | return GetProcAddress(library, name); |
| 366 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 367 | static char *loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 368 | static char errorMsg[120]; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 369 | snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", |
| 370 | name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 371 | return errorMsg; |
| 372 | } |
| 373 | |
| 374 | // Threads: |
| 375 | typedef HANDLE loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 376 | #define THREAD_LOCAL_DECL __declspec(thread) |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 377 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 378 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 379 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var; |
| 380 | static BOOL CALLBACK |
| 381 | InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 382 | void (*func)(void) = (void (*)(void))Parameter; |
| 383 | func(); |
| 384 | return TRUE; |
| 385 | } |
| 386 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 387 | static void loader_platform_thread_once(void *ctl, void (*func)(void)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 388 | assert(func != NULL); |
| 389 | assert(ctl != NULL); |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 390 | InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Thread IDs: |
| 394 | typedef DWORD loader_platform_thread_id; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 395 | static loader_platform_thread_id loader_platform_get_thread_id() { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 396 | return GetCurrentThreadId(); |
| 397 | } |
| 398 | |
| 399 | // Thread mutex: |
| 400 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 401 | static void |
| 402 | loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 403 | InitializeCriticalSection(pMutex); |
| 404 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 405 | static void |
| 406 | loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 407 | EnterCriticalSection(pMutex); |
| 408 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 409 | static void |
| 410 | loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 411 | LeaveCriticalSection(pMutex); |
| 412 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 413 | static void |
| 414 | loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 415 | DeleteCriticalSection(pMutex); |
| 416 | } |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 417 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 418 | static void |
| 419 | loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 420 | InitializeConditionVariable(pCond); |
| 421 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 422 | static void |
| 423 | loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, |
| 424 | loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 425 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 426 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 427 | static void |
| 428 | loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 429 | WakeAllConditionVariable(pCond); |
| 430 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 431 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 432 | // Windows Registry: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 433 | char *loader_get_registry_string(const HKEY hive, const LPCTSTR sub_key, |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 434 | const char *value); |
| 435 | |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 436 | #define loader_stack_alloc(size) _alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 437 | #else // defined(_WIN32) |
| 438 | |
| 439 | #error The "loader_platform.h" file must be modified for this OS. |
| 440 | |
| 441 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 442 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 443 | // contents of this file must be created. |
| 444 | |
| 445 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 446 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 447 | |
| 448 | #endif // defined(_WIN32) |
| 449 | |
jon | 547bbee | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 450 | // returns true if the given string appears to be a relative or absolute |
| 451 | // path, as opposed to a bare filename. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 452 | static inline bool loader_platform_is_path(const char *path) { |
jon | 547bbee | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 453 | return strchr(path, DIRECTORY_SYMBOL) != NULL; |
| 454 | } |