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 | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 10 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 12 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 18 | * |
| 19 | * Author: Ian Elliot <ian@lunarg.com> |
| 20 | * Author: Jon Ashburn <jon@lunarg.com> |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 21 | * |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 22 | */ |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 23 | #pragma once |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 24 | |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 25 | #if defined(_WIN32) |
| 26 | // WinSock2.h must be included *BEFORE* windows.h |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 27 | #include <WinSock2.h> |
| 28 | #endif // _WIN32 |
| 29 | |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 30 | #include "vulkan/vk_platform.h" |
| 31 | #include "vulkan/vk_sdk_platform.h" |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 32 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 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> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 44 | #include <string.h> |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 45 | #include <stdbool.h> |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 46 | #include <stdlib.h> |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 47 | #include <libgen.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 48 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 49 | // VK Library Filenames, Paths, etc.: |
Frank Henigman | 5717310 | 2016-11-24 22:15:20 -0500 | [diff] [blame] | 50 | #define PATH_SEPARATOR ':' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 51 | #define DIRECTORY_SYMBOL '/' |
| 52 | |
John Drinkwater | 9ac3f4f | 2016-08-01 17:00:00 +0100 | [diff] [blame] | 53 | #define VULKAN_DIR "/vulkan/" |
| 54 | #define VULKAN_ICDCONF_DIR "icd.d" |
| 55 | #define VULKAN_ICD_DIR "icd" |
| 56 | #define VULKAN_ELAYERCONF_DIR "explicit_layer.d" |
| 57 | #define VULKAN_ILAYERCONF_DIR "implicit_layer.d" |
| 58 | #define VULKAN_LAYER_DIR "layer" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 59 | |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 60 | #if defined(EXTRASYSCONFDIR) |
| 61 | #define EXTRA_DRIVERS_SYSCONFDIR_INFO ":" \ |
| 62 | EXTRASYSCONFDIR VULKAN_DIR VULKAN_ICDCONF_DIR |
| 63 | #define EXTRA_ELAYERS_SYSCONFDIR_INFO ":" \ |
| 64 | EXTRASYSCONFDIR VULKAN_DIR VULKAN_ELAYERCONF_DIR |
| 65 | #define EXTRA_ILAYERS_SYSCONFDIR_INFO ":" \ |
| 66 | EXTRASYSCONFDIR VULKAN_DIR VULKAN_ILAYERCONF_DIR |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 67 | #else |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 68 | #define EXTRA_DRIVERS_SYSCONFDIR_INFO |
| 69 | #define EXTRA_ELAYERS_SYSCONFDIR_INFO |
| 70 | #define EXTRA_ILAYERS_SYSCONFDIR_INFO |
| 71 | #endif |
| 72 | |
| 73 | #if defined(EXTRADATADIR) |
| 74 | #define EXTRA_DRIVERS_DATADIR_INFO ":" \ |
| 75 | EXTRADATADIR VULKAN_DIR VULKAN_ICDCONF_DIR |
| 76 | #define EXTRA_ELAYERS_DATADIR_INFO ":" \ |
| 77 | EXTRADATADIR VULKAN_DIR VULKAN_ELAYERCONF_DIR |
| 78 | #define EXTRA_ILAYERS_DATADIR_INFO ":" \ |
| 79 | EXTRADATADIR VULKAN_DIR VULKAN_ILAYERCONF_DIR |
| 80 | #else |
| 81 | #define EXTRA_DRIVERS_DATADIR_INFO |
| 82 | #define EXTRA_ELAYERS_DATADIR_INFO |
| 83 | #define EXTRA_ILAYERS_DATADIR_INFO |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 86 | #define DEFAULT_VK_DRIVERS_INFO \ |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 87 | SYSCONFDIR VULKAN_DIR VULKAN_ICDCONF_DIR ":" \ |
| 88 | DATADIR VULKAN_DIR VULKAN_ICDCONF_DIR \ |
| 89 | EXTRA_DRIVERS_SYSCONFDIR_INFO \ |
| 90 | EXTRA_DRIVERS_DATADIR_INFO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 91 | #define DEFAULT_VK_ELAYERS_INFO \ |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 92 | SYSCONFDIR VULKAN_DIR VULKAN_ELAYERCONF_DIR ":" \ |
| 93 | DATADIR VULKAN_DIR VULKAN_ELAYERCONF_DIR \ |
| 94 | EXTRA_ELAYERS_SYSCONFDIR_INFO \ |
| 95 | EXTRA_ELAYERS_DATADIR_INFO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 96 | #define DEFAULT_VK_ILAYERS_INFO \ |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 97 | SYSCONFDIR VULKAN_DIR VULKAN_ILAYERCONF_DIR ":" \ |
| 98 | DATADIR VULKAN_DIR VULKAN_ILAYERCONF_DIR \ |
| 99 | EXTRA_ILAYERS_SYSCONFDIR_INFO \ |
| 100 | EXTRA_ILAYERS_DATADIR_INFO |
| 101 | |
| 102 | #define DEFAULT_VK_DRIVERS_PATH "" |
Frank Henigman | 5ecb4f6 | 2016-11-24 17:47:26 -0500 | [diff] [blame] | 103 | #if !defined(DEFAULT_VK_LAYERS_PATH) |
Jamie Madill | 2fcbd15 | 2016-04-27 16:33:23 -0400 | [diff] [blame] | 104 | #define DEFAULT_VK_LAYERS_PATH "" |
Frank Henigman | 5ecb4f6 | 2016-11-24 17:47:26 -0500 | [diff] [blame] | 105 | #endif |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 106 | |
Jamie Madill | 00c3c91 | 2016-04-06 18:26:46 -0400 | [diff] [blame] | 107 | #if !defined(LAYERS_SOURCE_PATH) |
| 108 | #define LAYERS_SOURCE_PATH NULL |
| 109 | #endif |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 110 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 111 | |
John Drinkwater | 9ac3f4f | 2016-08-01 17:00:00 +0100 | [diff] [blame] | 112 | #define HOME_VK_DRIVERS_INFO VULKAN_DIR VULKAN_ICDCONF_DIR |
| 113 | #define HOME_VK_ELAYERS_INFO VULKAN_DIR VULKAN_ELAYERCONF_DIR |
| 114 | #define HOME_VK_ILAYERS_INFO VULKAN_DIR VULKAN_ILAYERCONF_DIR |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 115 | |
Ian Elliott | e536946 | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 116 | // C99: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 117 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 118 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 119 | // File IO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 120 | static inline bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 121 | if (access(path, F_OK)) |
| 122 | return false; |
| 123 | else |
| 124 | return true; |
| 125 | } |
| 126 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 127 | static inline bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 128 | if (path[0] == '/') |
| 129 | return true; |
| 130 | else |
| 131 | return false; |
| 132 | } |
| 133 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 134 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 135 | return dirname(path); |
| 136 | } |
| 137 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 138 | // Dynamic Loading of libraries: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 139 | typedef void *loader_platform_dl_handle; |
| 140 | static inline loader_platform_dl_handle |
| 141 | loader_platform_open_library(const char *libPath) { |
Chia-I Wu | 6a21834 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 142 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 143 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 144 | static inline const char * |
| 145 | loader_platform_open_library_error(const char *libPath) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 146 | return dlerror(); |
| 147 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 148 | static inline void |
| 149 | loader_platform_close_library(loader_platform_dl_handle library) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 150 | dlclose(library); |
| 151 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 152 | static inline void * |
| 153 | loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 154 | const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 155 | assert(library); |
| 156 | assert(name); |
| 157 | return dlsym(library, name); |
| 158 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 159 | static inline const char * |
| 160 | loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 161 | return dlerror(); |
| 162 | } |
| 163 | |
| 164 | // Threads: |
| 165 | typedef pthread_t loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 166 | #define THREAD_LOCAL_DECL __thread |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 167 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 168 | pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 169 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var; |
| 170 | static inline void loader_platform_thread_once(pthread_once_t *ctl, |
| 171 | void (*func)(void)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 172 | assert(func != NULL); |
| 173 | assert(ctl != NULL); |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 174 | pthread_once(ctl, func); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Thread IDs: |
| 178 | typedef pthread_t loader_platform_thread_id; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 179 | static inline loader_platform_thread_id loader_platform_get_thread_id() { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 180 | return pthread_self(); |
| 181 | } |
| 182 | |
| 183 | // Thread mutex: |
| 184 | typedef pthread_mutex_t loader_platform_thread_mutex; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 185 | static inline void |
| 186 | loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 187 | pthread_mutex_init(pMutex, NULL); |
| 188 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 189 | static inline void |
| 190 | loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 191 | pthread_mutex_lock(pMutex); |
| 192 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 193 | static inline void |
| 194 | loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 195 | pthread_mutex_unlock(pMutex); |
| 196 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 197 | static inline void |
| 198 | loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 199 | pthread_mutex_destroy(pMutex); |
| 200 | } |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 201 | typedef pthread_cond_t loader_platform_thread_cond; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 202 | static inline void |
| 203 | loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 204 | pthread_cond_init(pCond, NULL); |
| 205 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 206 | static inline void |
| 207 | loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, |
| 208 | loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 209 | pthread_cond_wait(pCond, pMutex); |
| 210 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 211 | static inline void |
| 212 | loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 213 | pthread_cond_broadcast(pCond); |
| 214 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 215 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 216 | #define loader_stack_alloc(size) alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 217 | |
| 218 | #elif defined(_WIN32) // defined(__linux__) |
| 219 | /* Windows-specific common code: */ |
Tobin Ehlis | 890adf1 | 2015-11-02 15:45:19 -0700 | [diff] [blame] | 220 | // WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent |
| 221 | // undefine them to avoid conflicts with VkLayerDispatchTable struct members. |
| 222 | #ifdef CreateSemaphore |
| 223 | #undef CreateSemaphore |
| 224 | #endif |
| 225 | #ifdef CreateEvent |
| 226 | #undef CreateEvent |
| 227 | #endif |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 228 | #include <assert.h> |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 229 | #include <stdio.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 230 | #include <string.h> |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 231 | #include <io.h> |
| 232 | #include <stdbool.h> |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 233 | #include <shlwapi.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 234 | #ifdef __cplusplus |
| 235 | #include <iostream> |
| 236 | #include <string> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 237 | #endif // __cplusplus |
| 238 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 239 | // VK Library Filenames, Paths, etc.: |
Frank Henigman | 5717310 | 2016-11-24 22:15:20 -0500 | [diff] [blame] | 240 | #define PATH_SEPARATOR ';' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 241 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 242 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
Lenny Komow | 367c4ea | 2016-11-07 11:40:00 -0700 | [diff] [blame] | 243 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\Drivers" |
Jamie Madill | 2fcbd15 | 2016-04-27 16:33:23 -0400 | [diff] [blame] | 244 | #define DEFAULT_VK_DRIVERS_PATH "" |
Lenny Komow | 367c4ea | 2016-11-07 11:40:00 -0700 | [diff] [blame] | 245 | #define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\ExplicitLayers" |
| 246 | #define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\ImplicitLayers" |
Jamie Madill | 00c3c91 | 2016-04-06 18:26:46 -0400 | [diff] [blame] | 247 | #if !defined(DEFAULT_VK_LAYERS_PATH) |
Jamie Madill | 2fcbd15 | 2016-04-27 16:33:23 -0400 | [diff] [blame] | 248 | #define DEFAULT_VK_LAYERS_PATH "" |
Jamie Madill | 00c3c91 | 2016-04-06 18:26:46 -0400 | [diff] [blame] | 249 | #endif |
| 250 | #if !defined(LAYERS_SOURCE_PATH) |
| 251 | #define LAYERS_SOURCE_PATH NULL |
| 252 | #endif |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 253 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Jon Ashburn | 8365786 | 2016-02-17 13:18:08 -0700 | [diff] [blame] | 254 | #define HOME_VK_DRIVERS_INFO "" |
| 255 | #define HOME_VK_ELAYERS_INFO "" |
| 256 | #define HOME_VK_ILAYERS_INFO "" |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 257 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
Courtney Goeltzenleuchter | 22d8d79 | 2015-10-07 17:03:42 -0600 | [diff] [blame] | 258 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 259 | // File IO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 260 | static bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 261 | if ((_access(path, 0)) == -1) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 262 | return false; |
| 263 | else |
| 264 | return true; |
| 265 | } |
| 266 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 267 | static bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 268 | return !PathIsRelative(path); |
| 269 | } |
| 270 | |
| 271 | // WIN32 runtime doesn't have dirname(). |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 272 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 273 | char *current, *next; |
| 274 | |
| 275 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 276 | |
| 277 | for (current = path; *current != '\0'; current = next) { |
| 278 | next = strchr(current, DIRECTORY_SYMBOL); |
| 279 | if (next == NULL) { |
| 280 | if (current != path) |
| 281 | *(current - 1) = '\0'; |
| 282 | return path; |
| 283 | } else { |
| 284 | // Point one character past the DIRECTORY_SYMBOL: |
| 285 | next++; |
| 286 | } |
| 287 | } |
| 288 | return path; |
| 289 | } |
| 290 | |
| 291 | // WIN32 runtime doesn't have basename(). |
| 292 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 293 | // so this is just a temporary solution in order to get us compiling, so that we |
| 294 | // can test some scenarios, and develop the correct solution for Windows. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 295 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 296 | // temporary code: |
| 297 | static char *loader_platform_basename(char *pathname) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 298 | char *current, *next; |
| 299 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 300 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 301 | |
| 302 | for (current = pathname; *current != '\0'; current = next) { |
| 303 | next = strchr(current, DIRECTORY_SYMBOL); |
| 304 | if (next == NULL) { |
| 305 | // No more DIRECTORY_SYMBOL's so return p: |
| 306 | return current; |
| 307 | } else { |
| 308 | // Point one character past the DIRECTORY_SYMBOL: |
| 309 | next++; |
| 310 | } |
| 311 | } |
| 312 | // We shouldn't get to here, but this makes the compiler happy: |
| 313 | return current; |
| 314 | } |
| 315 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 316 | // Dynamic Loading: |
| 317 | typedef HMODULE loader_platform_dl_handle; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 318 | static loader_platform_dl_handle |
| 319 | loader_platform_open_library(const char *libPath) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 320 | return LoadLibrary(libPath); |
| 321 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 322 | static char *loader_platform_open_library_error(const char *libPath) { |
Tobin Ehlis | b2dfb55 | 2016-06-07 06:07:13 -0600 | [diff] [blame] | 323 | static char errorMsg[164]; |
Karl Schultz | e2ef9e6 | 2017-01-13 14:01:35 -0700 | [diff] [blame] | 324 | (void)snprintf(errorMsg, 163, "Failed to open dynamic library \"%s\"", |
| 325 | libPath); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 326 | return errorMsg; |
| 327 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 328 | static void loader_platform_close_library(loader_platform_dl_handle library) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 329 | FreeLibrary(library); |
| 330 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 331 | static void *loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 332 | const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 333 | assert(library); |
| 334 | assert(name); |
| 335 | return GetProcAddress(library, name); |
| 336 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 337 | static char *loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 338 | static char errorMsg[120]; |
Karl Schultz | e2ef9e6 | 2017-01-13 14:01:35 -0700 | [diff] [blame] | 339 | (void)snprintf(errorMsg, 119, |
| 340 | "Failed to find function \"%s\" in dynamic library", name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 341 | return errorMsg; |
| 342 | } |
| 343 | |
| 344 | // Threads: |
| 345 | typedef HANDLE loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 346 | #define THREAD_LOCAL_DECL __declspec(thread) |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 347 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 348 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 349 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var; |
| 350 | static BOOL CALLBACK |
| 351 | InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 352 | void (*func)(void) = (void (*)(void))Parameter; |
| 353 | func(); |
| 354 | return TRUE; |
| 355 | } |
| 356 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 357 | static void loader_platform_thread_once(void *ctl, void (*func)(void)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 358 | assert(func != NULL); |
| 359 | assert(ctl != NULL); |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 360 | InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Thread IDs: |
| 364 | typedef DWORD loader_platform_thread_id; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 365 | static loader_platform_thread_id loader_platform_get_thread_id() { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 366 | return GetCurrentThreadId(); |
| 367 | } |
| 368 | |
| 369 | // Thread mutex: |
| 370 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 371 | static void |
| 372 | loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 373 | InitializeCriticalSection(pMutex); |
| 374 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 375 | static void |
| 376 | loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 377 | EnterCriticalSection(pMutex); |
| 378 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 379 | static void |
| 380 | loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 381 | LeaveCriticalSection(pMutex); |
| 382 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 383 | static void |
| 384 | loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 385 | DeleteCriticalSection(pMutex); |
| 386 | } |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 387 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 388 | static void |
| 389 | loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 390 | InitializeConditionVariable(pCond); |
| 391 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 392 | static void |
| 393 | loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, |
| 394 | loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 395 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 396 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 397 | static void |
| 398 | loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 399 | WakeAllConditionVariable(pCond); |
| 400 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 401 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 402 | // Windows Registry: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 403 | char *loader_get_registry_string(const HKEY hive, const LPCTSTR sub_key, |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 404 | const char *value); |
| 405 | |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 406 | #define loader_stack_alloc(size) _alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 407 | #else // defined(_WIN32) |
| 408 | |
| 409 | #error The "loader_platform.h" file must be modified for this OS. |
| 410 | |
| 411 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 412 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 413 | // contents of this file must be created. |
| 414 | |
| 415 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 416 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 417 | |
| 418 | #endif // defined(_WIN32) |
| 419 | |
jon | 547bbee | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 420 | // returns true if the given string appears to be a relative or absolute |
| 421 | // path, as opposed to a bare filename. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 422 | static inline bool loader_platform_is_path(const char *path) { |
jon | 547bbee | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 423 | return strchr(path, DIRECTORY_SYMBOL) != NULL; |
| 424 | } |