Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 2 | * |
Jon Ashburn | 44aed66 | 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 | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 6 | * |
Jon Ashburn | 44aed66 | 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 | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 13 | * |
Jon Ashburn | 44aed66 | 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 | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 16 | * |
Jon Ashburn | 44aed66 | 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 | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 20 | * |
Jon Ashburn | 44aed66 | 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 | 96cd795 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 28 | * |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 29 | */ |
Jon Ashburn | f6620c9 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 30 | #pragma once |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 31 | |
Mark Lobodzinski | 051ae4b | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 32 | #if defined(_WIN32) |
| 33 | // WinSock2.h must be included *BEFORE* windows.h |
Mark Lobodzinski | 051ae4b | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 34 | #include <WinSock2.h> |
| 35 | #endif // _WIN32 |
| 36 | |
David Pinedo | 329ca9e | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 37 | #include "vulkan/vk_platform.h" |
| 38 | #include "vulkan/vk_sdk_platform.h" |
Cody Northrop | 62ac1c5 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 39 | |
Ian Elliott | 81ac44c | 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 | 429e19f | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 51 | #include <string.h> |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 52 | #include <stdbool.h> |
Michael Lentine | 9268944 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 53 | #include <stdlib.h> |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 54 | #include <libgen.h> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 55 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 56 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 57 | #define PATH_SEPERATOR ':' |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 58 | #define DIRECTORY_SYMBOL '/' |
| 59 | |
Jon Ashburn | 44aed66 | 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 | ac945bc | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 85 | |
| 86 | #if defined(LOCALPREFIX) |
Jon Ashburn | 44aed66 | 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 | ac945bc | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 96 | #else |
| 97 | #define LOCAL_DRIVERS_INFO |
Jon Ashburn | d2c905c | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 98 | #define LOCAL_ELAYERS_INFO |
| 99 | #define LOCAL_ILAYERS_INFO |
James Jones | ac945bc | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
Jon Ashburn | 44aed66 | 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 | ac945bc | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 105 | "/usr/" DATADIR VULKAN_ICDCONF_DIR |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 106 | #define DEFAULT_VK_ELAYERS_INFO \ |
| 107 | LOCAL_ELAYERS_INFO \ |
| 108 | "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \ |
Jon Ashburn | a867ee4 | 2016-02-16 15:34:16 -0700 | [diff] [blame] | 109 | "/usr/" DATADIR VULKAN_ELAYERCONF_DIR |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 110 | #define DEFAULT_VK_ILAYERS_INFO \ |
| 111 | LOCAL_ILAYERS_INFO \ |
| 112 | "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \ |
James Jones | ac945bc | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 113 | "/usr/" DATADIR VULKAN_ILAYERCONF_DIR |
Jon Ashburn | 68e2bef | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 114 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Jon Ashburn | a867ee4 | 2016-02-16 15:34:16 -0700 | [diff] [blame] | 115 | #define HOME_VK_DRIVERS_INFO "/.local/share" VULKAN_ICDCONF_DIR |
| 116 | #define HOME_VK_ELAYERS_INFO "/.local/share" VULKAN_ELAYERCONF_DIR |
| 117 | #define HOME_VK_ILAYERS_INFO "/.local/share" VULKAN_ILAYERCONF_DIR |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 118 | |
Ian Elliott | ecb60ec | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 119 | // C99: |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 120 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
Tobin Ehlis | bf0146e | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 121 | |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 122 | // File IO |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 123 | static inline bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 124 | if (access(path, F_OK)) |
| 125 | return false; |
| 126 | else |
| 127 | return true; |
| 128 | } |
| 129 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 130 | static inline bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 131 | if (path[0] == '/') |
| 132 | return true; |
| 133 | else |
| 134 | return false; |
| 135 | } |
| 136 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 137 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 138 | return dirname(path); |
| 139 | } |
| 140 | |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 141 | // Environment variables |
| 142 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 143 | static inline char *loader_getenv(const char *name) { return getenv(name); } |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 144 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 145 | static inline void loader_free_getenv(const char *val) {} |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 146 | |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 147 | // Dynamic Loading of libraries: |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 148 | typedef void *loader_platform_dl_handle; |
| 149 | static inline loader_platform_dl_handle |
| 150 | loader_platform_open_library(const char *libPath) { |
Chia-I Wu | 7397c43 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 151 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 152 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 153 | static inline const char * |
| 154 | loader_platform_open_library_error(const char *libPath) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 155 | return dlerror(); |
| 156 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 157 | static inline void |
| 158 | loader_platform_close_library(loader_platform_dl_handle library) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 159 | dlclose(library); |
| 160 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 161 | static inline void * |
| 162 | loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 163 | const char *name) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 164 | assert(library); |
| 165 | assert(name); |
| 166 | return dlsym(library, name); |
| 167 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 168 | static inline const char * |
| 169 | loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 170 | return dlerror(); |
| 171 | } |
| 172 | |
| 173 | // Threads: |
| 174 | typedef pthread_t loader_platform_thread; |
Jon Ashburn | 413d658 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 175 | #define THREAD_LOCAL_DECL __thread |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 176 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 177 | pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 178 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var; |
| 179 | static inline void loader_platform_thread_once(pthread_once_t *ctl, |
| 180 | void (*func)(void)) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 181 | assert(func != NULL); |
| 182 | assert(ctl != NULL); |
Michael Lentine | 9268944 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 183 | pthread_once(ctl, func); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Thread IDs: |
| 187 | typedef pthread_t loader_platform_thread_id; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 188 | static inline loader_platform_thread_id loader_platform_get_thread_id() { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 189 | return pthread_self(); |
| 190 | } |
| 191 | |
| 192 | // Thread mutex: |
| 193 | typedef pthread_mutex_t loader_platform_thread_mutex; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 194 | static inline void |
| 195 | loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 196 | pthread_mutex_init(pMutex, NULL); |
| 197 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 198 | static inline void |
| 199 | loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 200 | pthread_mutex_lock(pMutex); |
| 201 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 202 | static inline void |
| 203 | loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 204 | pthread_mutex_unlock(pMutex); |
| 205 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 206 | static inline void |
| 207 | loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 208 | pthread_mutex_destroy(pMutex); |
| 209 | } |
Mike Stroyan | 354ed67 | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 210 | typedef pthread_cond_t loader_platform_thread_cond; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 211 | static inline void |
| 212 | loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { |
Mike Stroyan | 354ed67 | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 213 | pthread_cond_init(pCond, NULL); |
| 214 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 215 | static inline void |
| 216 | loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, |
| 217 | loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | 354ed67 | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 218 | pthread_cond_wait(pCond, pMutex); |
| 219 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 220 | static inline void |
| 221 | loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { |
Mike Stroyan | 354ed67 | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 222 | pthread_cond_broadcast(pCond); |
| 223 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 224 | |
Courtney Goeltzenleuchter | b620ace | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 225 | #define loader_stack_alloc(size) alloca(size) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 226 | |
| 227 | #elif defined(_WIN32) // defined(__linux__) |
| 228 | /* Windows-specific common code: */ |
Tobin Ehlis | c8d7593 | 2015-11-02 15:45:19 -0700 | [diff] [blame] | 229 | // WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent |
| 230 | // undefine them to avoid conflicts with VkLayerDispatchTable struct members. |
| 231 | #ifdef CreateSemaphore |
| 232 | #undef CreateSemaphore |
| 233 | #endif |
| 234 | #ifdef CreateEvent |
| 235 | #undef CreateEvent |
| 236 | #endif |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 237 | #include <assert.h> |
Tony Barbour | 6969851 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 238 | #include <stdio.h> |
Jon Ashburn | 429e19f | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 239 | #include <string.h> |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 240 | #include <io.h> |
| 241 | #include <stdbool.h> |
Cody Northrop | 62ac1c5 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 242 | #include <shlwapi.h> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 243 | #ifdef __cplusplus |
| 244 | #include <iostream> |
| 245 | #include <string> |
| 246 | using namespace std; |
| 247 | #endif // __cplusplus |
| 248 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 249 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 250 | #define PATH_SEPERATOR ';' |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 251 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 252 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
| 253 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers" |
Jon Ashburn | d2c905c | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 254 | #define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers" |
| 255 | #define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers" |
Jon Ashburn | 68e2bef | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 256 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Jon Ashburn | 6029943 | 2016-02-17 13:18:08 -0700 | [diff] [blame] | 257 | #define HOME_VK_DRIVERS_INFO "" |
| 258 | #define HOME_VK_ELAYERS_INFO "" |
| 259 | #define HOME_VK_ILAYERS_INFO "" |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 260 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
Courtney Goeltzenleuchter | b5738ca | 2015-10-07 17:03:42 -0600 | [diff] [blame] | 261 | |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 262 | // File IO |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 263 | static bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | ee33ae7 | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 264 | if ((_access(path, 0)) == -1) |
Jon Ashburn | ffd5d67 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 265 | return false; |
| 266 | else |
| 267 | return true; |
| 268 | } |
| 269 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 270 | static bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 271 | return !PathIsRelative(path); |
| 272 | } |
| 273 | |
| 274 | // WIN32 runtime doesn't have dirname(). |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 275 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 276 | char *current, *next; |
| 277 | |
| 278 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 279 | |
| 280 | for (current = path; *current != '\0'; current = next) { |
| 281 | next = strchr(current, DIRECTORY_SYMBOL); |
| 282 | if (next == NULL) { |
| 283 | if (current != path) |
| 284 | *(current - 1) = '\0'; |
| 285 | return path; |
| 286 | } else { |
| 287 | // Point one character past the DIRECTORY_SYMBOL: |
| 288 | next++; |
| 289 | } |
| 290 | } |
| 291 | return path; |
| 292 | } |
| 293 | |
| 294 | // WIN32 runtime doesn't have basename(). |
| 295 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 296 | // so this is just a temporary solution in order to get us compiling, so that we |
| 297 | // can test some scenarios, and develop the correct solution for Windows. |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 298 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 299 | // temporary code: |
| 300 | static char *loader_platform_basename(char *pathname) { |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 301 | char *current, *next; |
| 302 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 303 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
Jon Ashburn | 3814450 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 304 | |
| 305 | for (current = pathname; *current != '\0'; current = next) { |
| 306 | next = strchr(current, DIRECTORY_SYMBOL); |
| 307 | if (next == NULL) { |
| 308 | // No more DIRECTORY_SYMBOL's so return p: |
| 309 | return current; |
| 310 | } else { |
| 311 | // Point one character past the DIRECTORY_SYMBOL: |
| 312 | next++; |
| 313 | } |
| 314 | } |
| 315 | // We shouldn't get to here, but this makes the compiler happy: |
| 316 | return current; |
| 317 | } |
| 318 | |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 319 | // Environment variables |
| 320 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 321 | static inline char *loader_getenv(const char *name) { |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 322 | char *retVal; |
| 323 | DWORD valSize; |
| 324 | |
| 325 | valSize = GetEnvironmentVariableA(name, NULL, 0); |
| 326 | |
| 327 | // valSize DOES include the null terminator, so for any set variable |
| 328 | // will always be at least 1. If it's 0, the variable wasn't set. |
| 329 | if (valSize == 0) |
| 330 | return NULL; |
| 331 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 332 | // TODO; FIXME This should be using any app defined memory allocation |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 333 | retVal = (char *)malloc(valSize); |
| 334 | |
| 335 | GetEnvironmentVariableA(name, retVal, valSize); |
| 336 | |
| 337 | return retVal; |
| 338 | } |
| 339 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 340 | static inline void loader_free_getenv(const char *val) { free((void *)val); } |
Jon Ashburn | 5b25d9f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 341 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 342 | // Dynamic Loading: |
| 343 | typedef HMODULE loader_platform_dl_handle; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 344 | static loader_platform_dl_handle |
| 345 | loader_platform_open_library(const char *libPath) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 346 | return LoadLibrary(libPath); |
| 347 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 348 | static char *loader_platform_open_library_error(const char *libPath) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 349 | static char errorMsg[120]; |
| 350 | snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath); |
| 351 | return errorMsg; |
| 352 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 353 | static void loader_platform_close_library(loader_platform_dl_handle library) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 354 | FreeLibrary(library); |
| 355 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 356 | static void *loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 357 | const char *name) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 358 | assert(library); |
| 359 | assert(name); |
| 360 | return GetProcAddress(library, name); |
| 361 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 362 | static char *loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 363 | static char errorMsg[120]; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 364 | snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", |
| 365 | name); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 366 | return errorMsg; |
| 367 | } |
| 368 | |
| 369 | // Threads: |
| 370 | typedef HANDLE loader_platform_thread; |
Jon Ashburn | 413d658 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 371 | #define THREAD_LOCAL_DECL __declspec(thread) |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 372 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 373 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 374 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var; |
| 375 | static BOOL CALLBACK |
| 376 | InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 377 | void (*func)(void) = (void (*)(void))Parameter; |
| 378 | func(); |
| 379 | return TRUE; |
| 380 | } |
| 381 | |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 382 | static void loader_platform_thread_once(void *ctl, void (*func)(void)) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 383 | assert(func != NULL); |
| 384 | assert(ctl != NULL); |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 385 | InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Thread IDs: |
| 389 | typedef DWORD loader_platform_thread_id; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 390 | static loader_platform_thread_id loader_platform_get_thread_id() { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 391 | return GetCurrentThreadId(); |
| 392 | } |
| 393 | |
| 394 | // Thread mutex: |
| 395 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 396 | static void |
| 397 | loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 398 | InitializeCriticalSection(pMutex); |
| 399 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 400 | static void |
| 401 | loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 402 | EnterCriticalSection(pMutex); |
| 403 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 404 | static void |
| 405 | loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 406 | LeaveCriticalSection(pMutex); |
| 407 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 408 | static void |
| 409 | loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 410 | DeleteCriticalSection(pMutex); |
| 411 | } |
Mike Stroyan | 33053d0 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 412 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 413 | static void |
| 414 | loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { |
Mike Stroyan | 33053d0 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 415 | InitializeConditionVariable(pCond); |
| 416 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 417 | static void |
| 418 | loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, |
| 419 | loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | 33053d0 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 420 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 421 | } |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 422 | static void |
| 423 | loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { |
Mike Stroyan | 33053d0 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 424 | WakeAllConditionVariable(pCond); |
| 425 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 426 | |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 427 | // Windows Registry: |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 428 | char *loader_get_registry_string(const HKEY hive, const LPCTSTR sub_key, |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 429 | const char *value); |
| 430 | |
Jon Ashburn | f6620c9 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 431 | #define loader_stack_alloc(size) _alloca(size) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 432 | #else // defined(_WIN32) |
| 433 | |
| 434 | #error The "loader_platform.h" file must be modified for this OS. |
| 435 | |
| 436 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 437 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 438 | // contents of this file must be created. |
| 439 | |
| 440 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 441 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 442 | |
| 443 | #endif // defined(_WIN32) |
| 444 | |
jon | 5fb79fa | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 445 | // returns true if the given string appears to be a relative or absolute |
| 446 | // path, as opposed to a bare filename. |
Jon Ashburn | 44aed66 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 447 | static inline bool loader_platform_is_path(const char *path) { |
jon | 5fb79fa | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 448 | return strchr(path, DIRECTORY_SYMBOL) != NULL; |
| 449 | } |