Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2015 LunarG, Inc. |
| 5 | * Copyright 2014 Valve Software |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included |
| 16 | * in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | * Authors: |
| 27 | * Ian Elliott <ian@lunarg.com> |
| 28 | */ |
| 29 | |
| 30 | #ifndef LOADER_PLATFORM_H |
| 31 | #define LOADER_PLATFORM_H |
| 32 | |
| 33 | #if defined(__linux__) |
| 34 | /* Linux-specific common code: */ |
| 35 | |
| 36 | // Headers: |
| 37 | //#define _GNU_SOURCE 1 |
| 38 | // TBD: Are the contents of the following file used? |
| 39 | #include <unistd.h> |
| 40 | // Note: The following file is for dynamic loading: |
| 41 | #include <dlfcn.h> |
| 42 | #include <pthread.h> |
| 43 | #include <assert.h> |
| 44 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 45 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 46 | #define PATH_SEPERATOR ':' |
| 47 | #define DIRECTORY_SYMBOL "/" |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 48 | #define DRIVER_PATH_ENV "LIBVK_DRIVERS_PATH" |
| 49 | #define LAYERS_PATH_ENV "LIBVK_LAYERS_PATH" |
| 50 | #define LAYER_NAMES_ENV "LIBVK_LAYER_NAMES" |
| 51 | #ifndef DEFAULT_VK_DRIVERS_PATH |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 52 | // TODO: Is this a good default location? |
| 53 | // Need to search for both 32bit and 64bit ICDs |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 54 | #define DEFAULT_VK_DRIVERS_PATH "/usr/lib/i386-linux-gnu/vk:/usr/lib/x86_64-linux-gnu/vk" |
| 55 | #define VK_DRIVER_LIBRARY_PREFIX "libVK_" |
| 56 | #define VK_DRIVER_LIBRARY_PREFIX_LEN 6 |
| 57 | #define VK_LAYER_LIBRARY_PREFIX "libVKLayer" |
| 58 | #define VK_LAYER_LIBRARY_PREFIX_LEN 10 |
| 59 | #define VK_LIBRARY_SUFFIX ".so" |
| 60 | #define VK_LIBRARY_SUFFIX_LEN 3 |
| 61 | #endif // DEFAULT_VK_DRIVERS_PATH |
| 62 | #ifndef DEFAULT_VK_LAYERS_PATH |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 63 | // TODO: Are these good default locations? |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 64 | #define DEFAULT_VK_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/vk:/usr/lib/x86_64-linux-gnu/vk" |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 65 | #endif |
| 66 | |
Ian Elliott | e536946 | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 67 | // C99: |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 68 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
| 69 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 70 | // Dynamic Loading: |
| 71 | typedef void * loader_platform_dl_handle; |
| 72 | static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 73 | { |
Chia-I Wu | 6a21834 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 74 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 75 | } |
| 76 | static inline char * loader_platform_open_library_error(const char* libPath) |
| 77 | { |
| 78 | return dlerror(); |
| 79 | } |
| 80 | static inline void loader_platform_close_library(loader_platform_dl_handle library) |
| 81 | { |
| 82 | dlclose(library); |
| 83 | } |
| 84 | static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 85 | const char *name) |
| 86 | { |
| 87 | assert(library); |
| 88 | assert(name); |
| 89 | return dlsym(library, name); |
| 90 | } |
| 91 | static inline char * loader_platform_get_proc_address_error(const char *name) |
| 92 | { |
| 93 | return dlerror(); |
| 94 | } |
| 95 | |
| 96 | // Threads: |
| 97 | typedef pthread_t loader_platform_thread; |
| 98 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 99 | pthread_once_t var = PTHREAD_ONCE_INIT; |
| 100 | static inline void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 101 | { |
| 102 | assert(func != NULL); |
| 103 | assert(ctl != NULL); |
| 104 | pthread_once((pthread_once_t *) ctl, func); |
| 105 | } |
| 106 | |
| 107 | // Thread IDs: |
| 108 | typedef pthread_t loader_platform_thread_id; |
| 109 | static inline loader_platform_thread_id loader_platform_get_thread_id() |
| 110 | { |
| 111 | return pthread_self(); |
| 112 | } |
| 113 | |
| 114 | // Thread mutex: |
| 115 | typedef pthread_mutex_t loader_platform_thread_mutex; |
| 116 | static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 117 | { |
| 118 | pthread_mutex_init(pMutex, NULL); |
| 119 | } |
| 120 | static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 121 | { |
| 122 | pthread_mutex_lock(pMutex); |
| 123 | } |
| 124 | static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 125 | { |
| 126 | pthread_mutex_unlock(pMutex); |
| 127 | } |
| 128 | static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 129 | { |
| 130 | pthread_mutex_destroy(pMutex); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | #elif defined(_WIN32) // defined(__linux__) |
| 135 | /* Windows-specific common code: */ |
| 136 | |
| 137 | // Headers: |
Piers Daniell | 188f820 | 2015-02-24 13:58:47 -0700 | [diff] [blame] | 138 | #include <WinSock2.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 139 | #include <windows.h> |
| 140 | #include <assert.h> |
| 141 | #ifdef __cplusplus |
| 142 | #include <iostream> |
| 143 | #include <string> |
| 144 | using namespace std; |
| 145 | #endif // __cplusplus |
| 146 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 147 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 148 | #define PATH_SEPERATOR ';' |
| 149 | #define DIRECTORY_SYMBOL "\\" |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 150 | #define DRIVER_PATH_REGISTRY_VALUE "VK_DRIVERS_PATH" |
| 151 | #define LAYERS_PATH_REGISTRY_VALUE "VK_LAYERS_PATH" |
| 152 | #define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES" |
| 153 | #define DRIVER_PATH_ENV "VK_DRIVERS_PATH" |
| 154 | #define LAYERS_PATH_ENV "VK_LAYERS_PATH" |
| 155 | #define LAYER_NAMES_ENV "VK_LAYER_NAMES" |
| 156 | #ifndef DEFAULT_VK_DRIVERS_PATH |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 157 | // TODO: Is this a good default location? |
| 158 | // Need to search for both 32bit and 64bit ICDs |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 159 | #define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32" |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 160 | // TODO/TBD: Is this an appropriate prefix for Windows? |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 161 | #define VK_DRIVER_LIBRARY_PREFIX "VK_" |
| 162 | #define VK_DRIVER_LIBRARY_PREFIX_LEN 4 |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 163 | // TODO/TBD: Is this an appropriate suffix for Windows? |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 164 | #define VK_LAYER_LIBRARY_PREFIX "VKLayer" |
| 165 | #define VK_LAYER_LIBRARY_PREFIX_LEN 8 |
| 166 | #define VK_LIBRARY_SUFFIX ".dll" |
| 167 | #define VK_LIBRARY_SUFFIX_LEN 4 |
| 168 | #endif // DEFAULT_VK_DRIVERS_PATH |
| 169 | #ifndef DEFAULT_VK_LAYERS_PATH |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 170 | // TODO: Is this a good default location? |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 171 | #define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32" |
| 172 | #endif // DEFAULT_VK_LAYERS_PATH |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 173 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 174 | // C99: |
| 175 | // Microsoft didn't implement C99 in Visual Studio; but started adding it with |
| 176 | // VS2013. However, VS2013 still didn't have snprintf(). The following is a |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 177 | // work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the |
| 178 | // "CMakeLists.txt" file). |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 179 | #define snprintf _snprintf |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 180 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 181 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 182 | // so this is just a temporary solution in order to get us compiling, so that we |
| 183 | // can test some scenarios, and develop the correct solution for Windows. |
| 184 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 185 | // temporary code: |
| 186 | static char *basename(char *pathname) |
| 187 | { |
| 188 | char *current, *next; |
| 189 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 190 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 191 | |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 192 | #define DIRECTORY_SYMBOL_CHAR '\\' |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 193 | for (current = pathname; *current != '\0'; current = next) { |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 194 | next = strchr(current, DIRECTORY_SYMBOL_CHAR); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 195 | if (next == NULL) { |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 196 | // No more DIRECTORY_SYMBOL_CHAR's so return p: |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 197 | return current; |
| 198 | } else { |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 199 | // Point one character past the DIRECTORY_SYMBOL_CHAR: |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 200 | next++; |
| 201 | } |
| 202 | } |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 203 | // We shouldn't get to here, but this makes the compiler happy: |
| 204 | return current; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Dynamic Loading: |
| 208 | typedef HMODULE loader_platform_dl_handle; |
| 209 | static loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 210 | { |
| 211 | return LoadLibrary(libPath); |
| 212 | } |
| 213 | static char * loader_platform_open_library_error(const char* libPath) |
| 214 | { |
| 215 | static char errorMsg[120]; |
| 216 | snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath); |
| 217 | return errorMsg; |
| 218 | } |
| 219 | static void loader_platform_close_library(loader_platform_dl_handle library) |
| 220 | { |
| 221 | FreeLibrary(library); |
| 222 | } |
| 223 | static void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 224 | const char *name) |
| 225 | { |
| 226 | assert(library); |
| 227 | assert(name); |
| 228 | return GetProcAddress(library, name); |
| 229 | } |
| 230 | static char * loader_platform_get_proc_address_error(const char *name) |
| 231 | { |
| 232 | static char errorMsg[120]; |
| 233 | snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name); |
| 234 | return errorMsg; |
| 235 | } |
| 236 | |
| 237 | // Threads: |
| 238 | typedef HANDLE loader_platform_thread; |
| 239 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 240 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 241 | static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) |
| 242 | { |
| 243 | void (*func)(void) = (void (*)(void))Parameter; |
| 244 | func(); |
| 245 | return TRUE; |
| 246 | } |
| 247 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 248 | static void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 249 | { |
| 250 | assert(func != NULL); |
| 251 | assert(ctl != NULL); |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 252 | InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Thread IDs: |
| 256 | typedef DWORD loader_platform_thread_id; |
| 257 | static loader_platform_thread_id loader_platform_get_thread_id() |
| 258 | { |
| 259 | return GetCurrentThreadId(); |
| 260 | } |
| 261 | |
| 262 | // Thread mutex: |
| 263 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
| 264 | static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 265 | { |
| 266 | InitializeCriticalSection(pMutex); |
| 267 | } |
| 268 | static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 269 | { |
| 270 | EnterCriticalSection(pMutex); |
| 271 | } |
| 272 | static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 273 | { |
| 274 | LeaveCriticalSection(pMutex); |
| 275 | } |
| 276 | static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 277 | { |
| 278 | DeleteCriticalSection(pMutex); |
| 279 | } |
| 280 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 281 | // Windows Registry: |
| 282 | char *loader_get_registry_string(const HKEY hive, |
| 283 | const LPCTSTR sub_key, |
| 284 | const char *value); |
| 285 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 286 | #else // defined(_WIN32) |
| 287 | |
| 288 | #error The "loader_platform.h" file must be modified for this OS. |
| 289 | |
| 290 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 291 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 292 | // contents of this file must be created. |
| 293 | |
| 294 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 295 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 296 | |
| 297 | #endif // defined(_WIN32) |
| 298 | |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 299 | #else /* LOADER_PLATFORM_H */ |
| 300 | #ifndef LOADER_PLATFORM_H_TEMP |
| 301 | #define LOADER_PLATFORM_H_TEMP |
| 302 | |
| 303 | // NOTE: The following are hopefully-temporary macros to ensure that people |
| 304 | // don't forget to use the loader_platform_*() functions above: |
| 305 | |
| 306 | #if defined(__linux__) |
| 307 | /* Linux-specific common code: */ |
| 308 | |
| 309 | // Dynamic Loading: |
| 310 | #define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION |
| 311 | #define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY |
| 312 | #define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION |
| 313 | #define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION |
| 314 | |
| 315 | // Threads: |
| 316 | #define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION |
| 317 | #define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION |
| 318 | |
| 319 | // Thread mutex: |
| 320 | #define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION |
| 321 | #define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION |
| 322 | #define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION |
| 323 | #define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION |
| 324 | |
| 325 | |
| 326 | #elif defined(_WIN32) // defined(__linux__) |
| 327 | /* Windows-specific common code: */ |
| 328 | |
| 329 | // Dynamic Loading: |
| 330 | //#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION |
| 331 | #define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION |
| 332 | #define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION |
| 333 | |
| 334 | // Threads: |
| 335 | #define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION |
| 336 | #define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION |
| 337 | |
| 338 | // Thread mutex: |
| 339 | #define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION |
| 340 | #define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION |
| 341 | #define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION |
| 342 | #define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION |
| 343 | |
| 344 | |
| 345 | #endif // defined(_WIN32) |
| 346 | #endif /* LOADER_PLATFORM_H_TEMP */ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 347 | #endif /* LOADER_PLATFORM_H */ |