Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2014 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
Chia-I Wu | 701f3f6 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 26 | * Jon Ashburn <jon@lunarg.com> |
Chia-I Wu | 701f3f6 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 27 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 28 | * Ian Elliott <ian@lunarg.com> |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 29 | */ |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 30 | #define _GNU_SOURCE |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdbool.h> |
| 35 | #include <string.h> |
| 36 | |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 37 | #include <sys/types.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 38 | #if defined(WIN32) |
| 39 | #include "dirent_on_windows.h" |
| 40 | #else // WIN32 |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 41 | #include <dirent.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 42 | #endif // WIN32 |
| 43 | #include "loader_platform.h" |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 44 | #include "table_ops.h" |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 45 | #include "gpa_helper.h" |
Chia-I Wu | 1930060 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 46 | #include "loader.h" |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 47 | #include "vkIcd.h" |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 48 | // The following is #included again to catch certain OS-specific functions |
| 49 | // being used: |
| 50 | #include "loader_platform.h" |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 51 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 52 | struct loader_layers { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 53 | loader_platform_dl_handle lib_handle; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 54 | char name[256]; |
| 55 | }; |
| 56 | |
| 57 | struct layer_name_pair { |
| 58 | char *layer_name; |
| 59 | const char *lib_name; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 60 | }; |
| 61 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 62 | struct extension_property { |
| 63 | char extName[VK_MAX_EXTENSION_NAME]; |
| 64 | uint32_t version; |
| 65 | bool hosted; // does the extension reside in one driver/layer |
| 66 | }; |
| 67 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 68 | struct loader_icd { |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 69 | const struct loader_scanned_icds *scanned_icds; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 70 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 71 | VkLayerDispatchTable *loader_dispatch; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 72 | uint32_t layer_count[MAX_GPUS_FOR_LAYER]; |
| 73 | struct loader_layers layer_libs[MAX_GPUS_FOR_LAYER][MAX_LAYER_LIBRARIES]; |
| 74 | VkBaseLayerObject *wrappedGpus[MAX_GPUS_FOR_LAYER]; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 75 | uint32_t gpu_count; |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 76 | VkBaseLayerObject *gpus; |
Jon Ashburn | df7d584 | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 77 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 78 | struct loader_icd *next; |
| 79 | }; |
| 80 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 81 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 82 | struct loader_scanned_icds { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 83 | loader_platform_dl_handle handle; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 84 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 85 | PFN_vkGetProcAddr GetProcAddr; |
| 86 | PFN_vkCreateInstance CreateInstance; |
| 87 | PFN_vkDestroyInstance DestroyInstance; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 88 | PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 89 | PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 90 | VkInstance instance; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 91 | struct loader_scanned_icds *next; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 92 | uint32_t extension_count; |
| 93 | struct extension_property *extensions; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 94 | }; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 95 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 96 | struct loader_scanned_layers { |
| 97 | char *name; |
| 98 | uint32_t extension_count; |
| 99 | struct extension_property *extensions; |
| 100 | }; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 101 | // Note: Since the following is a static structure, all members are initialized |
| 102 | // to zero. |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 103 | static struct { |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 104 | struct loader_instance *instances; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 105 | bool icds_scanned; |
| 106 | struct loader_scanned_icds *scanned_icd_list; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 107 | bool layer_scanned; |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 108 | char *layer_dirs; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 109 | unsigned int scanned_layer_count; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 110 | struct loader_scanned_layers scanned_layers[MAX_LAYER_LIBRARIES]; |
| 111 | size_t scanned_ext_list_capacity; |
| 112 | uint32_t scanned_ext_list_count; // coalesced from all layers/drivers |
| 113 | struct extension_property **scanned_ext_list; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 114 | } loader; |
| 115 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 116 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd); |
| 117 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer); |
| 118 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 119 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 120 | #if defined(WIN32) |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 121 | char *loader_get_registry_string(const HKEY hive, |
| 122 | const LPCTSTR sub_key, |
| 123 | const char *value) |
| 124 | { |
| 125 | DWORD access_flags = KEY_QUERY_VALUE; |
| 126 | DWORD value_type; |
| 127 | HKEY key; |
| 128 | LONG rtn_value; |
| 129 | char *rtn_str = NULL; |
| 130 | size_t rtn_len = 0; |
| 131 | size_t allocated_len = 0; |
| 132 | |
| 133 | rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key); |
| 134 | if (rtn_value != ERROR_SUCCESS) { |
| 135 | // We didn't find the key. Try the 32-bit hive (where we've seen the |
| 136 | // key end up on some people's systems): |
| 137 | access_flags |= KEY_WOW64_32KEY; |
| 138 | rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key); |
| 139 | if (rtn_value != ERROR_SUCCESS) { |
| 140 | // We still couldn't find the key, so give up: |
| 141 | return NULL; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | rtn_value = RegQueryValueEx(key, value, NULL, &value_type, |
| 146 | (PVOID) rtn_str, &rtn_len); |
| 147 | if (rtn_value == ERROR_SUCCESS) { |
| 148 | // If we get to here, we found the key, and need to allocate memory |
| 149 | // large enough for rtn_str, and query again: |
| 150 | allocated_len = rtn_len + 4; |
| 151 | rtn_str = malloc(allocated_len); |
| 152 | rtn_value = RegQueryValueEx(key, value, NULL, &value_type, |
| 153 | (PVOID) rtn_str, &rtn_len); |
| 154 | if (rtn_value == ERROR_SUCCESS) { |
| 155 | // We added 4 extra bytes to rtn_str, so that we can ensure that |
| 156 | // the string is NULL-terminated (albeit, in a brute-force manner): |
| 157 | rtn_str[allocated_len-1] = '\0'; |
| 158 | } else { |
| 159 | // This should never occur, but in case it does, clean up: |
| 160 | free(rtn_str); |
| 161 | rtn_str = NULL; |
| 162 | } |
| 163 | } // else - shouldn't happen, but if it does, return rtn_str, which is NULL |
| 164 | |
| 165 | // Close the registry key that was opened: |
| 166 | RegCloseKey(key); |
| 167 | |
| 168 | return rtn_str; |
| 169 | } |
| 170 | |
| 171 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 172 | // For ICD developers, look in the registry, and look for an environment |
| 173 | // variable for a path(s) where to find the ICD(s): |
| 174 | static char *loader_get_registry_and_env(const char *env_var, |
| 175 | const char *registry_value) |
| 176 | { |
| 177 | char *env_str = getenv(env_var); |
| 178 | size_t env_len = (env_str == NULL) ? 0 : strlen(env_str); |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 179 | char *registry_str = NULL; |
| 180 | DWORD registry_len = 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 181 | char *rtn_str = NULL; |
| 182 | size_t rtn_len; |
| 183 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 184 | registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE, |
Ian Elliott | 06ebd75 | 2015-04-09 18:07:15 -0600 | [diff] [blame] | 185 | "Software\\Vulkan", |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 186 | registry_value); |
Ian Elliott | 2de26bc | 2015-04-03 13:13:01 -0600 | [diff] [blame] | 187 | registry_len = (registry_str) ? strlen(registry_str) : 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 188 | |
| 189 | rtn_len = env_len + registry_len + 1; |
| 190 | if (rtn_len <= 2) { |
| 191 | // We found neither the desired registry value, nor the environment |
| 192 | // variable; return NULL: |
| 193 | return NULL; |
| 194 | } else { |
| 195 | // We found something, and so we need to allocate memory for the string |
| 196 | // to return: |
| 197 | rtn_str = malloc(rtn_len); |
| 198 | } |
| 199 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 200 | if (registry_len == 0) { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 201 | // We didn't find the desired registry value, and so we must have found |
| 202 | // only the environment variable: |
| 203 | _snprintf(rtn_str, rtn_len, "%s", env_str); |
| 204 | } else if (env_str != NULL) { |
| 205 | // We found both the desired registry value and the environment |
| 206 | // variable, so concatenate them both: |
| 207 | _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str); |
| 208 | } else { |
| 209 | // We must have only found the desired registry value: |
| 210 | _snprintf(rtn_str, rtn_len, "%s", registry_str); |
| 211 | } |
| 212 | |
Ian Elliott | 2de26bc | 2015-04-03 13:13:01 -0600 | [diff] [blame] | 213 | if (registry_str) { |
| 214 | free(registry_str); |
| 215 | } |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 216 | |
| 217 | return(rtn_str); |
| 218 | } |
| 219 | #endif // WIN32 |
| 220 | |
| 221 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 222 | static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code, |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 223 | const char *format, ...) |
| 224 | { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 225 | char msg[256]; |
| 226 | va_list ap; |
| 227 | int ret; |
| 228 | |
| 229 | va_start(ap, format); |
| 230 | ret = vsnprintf(msg, sizeof(msg), format, ap); |
Ian Elliott | 4204584 | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 231 | if ((ret >= (int) sizeof(msg)) || ret < 0) { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 232 | msg[sizeof(msg) - 1] = '\0'; |
| 233 | } |
| 234 | va_end(ap); |
| 235 | |
Courtney Goeltzenleuchter | c80a557 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 236 | fputs(msg, stderr); |
| 237 | fputc('\n', stderr); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 238 | } |
| 239 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 240 | static bool has_extension(struct extension_property *exts, uint32_t count, |
| 241 | const char *name, bool must_be_hosted) |
| 242 | { |
| 243 | uint32_t i; |
| 244 | for (i = 0; i < count; i++) { |
| 245 | if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted)) |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get, |
| 252 | uint32_t *count_out, |
| 253 | struct extension_property **props_out) |
| 254 | { |
| 255 | uint32_t i, count, cur; |
| 256 | size_t siz = sizeof(count); |
| 257 | struct extension_property *ext_props; |
| 258 | VkExtensionProperties vk_prop; |
| 259 | VkResult res; |
| 260 | |
| 261 | *count_out = 0; |
| 262 | *props_out = NULL; |
| 263 | res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count); |
| 264 | if (res != VK_SUCCESS) { |
| 265 | loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD"); |
| 266 | return; |
| 267 | } |
| 268 | ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count); |
| 269 | if (ext_props == NULL) { |
| 270 | loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD"); |
| 271 | return; |
| 272 | } |
| 273 | siz = sizeof(VkExtensionProperties); |
| 274 | cur = 0; |
| 275 | for (i = 0; i < count; i++) { |
| 276 | res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop); |
| 277 | if (res == VK_SUCCESS) { |
| 278 | (ext_props + cur)->hosted = false; |
| 279 | (ext_props + cur)->version = vk_prop.version; |
| 280 | strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME); |
| 281 | (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0'; |
| 282 | cur++; |
| 283 | } |
| 284 | *count_out = cur; |
| 285 | *props_out = ext_props; |
| 286 | } |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | static void loader_init_ext_list() |
| 291 | { |
| 292 | loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *); |
| 293 | loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity); |
| 294 | memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity); |
| 295 | loader.scanned_ext_list_count = 0; |
| 296 | } |
| 297 | |
| 298 | #if 0 // currently no place to call this |
| 299 | static void loader_destroy_ext_list() |
| 300 | { |
| 301 | free(loader.scanned_ext_list); |
| 302 | loader.scanned_ext_list_capacity = 0; |
| 303 | loader.scanned_ext_list_count = 0; |
| 304 | } |
| 305 | #endif |
| 306 | |
| 307 | static void loader_add_to_ext_list(uint32_t count, |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 308 | struct extension_property *prop_list, |
| 309 | bool is_layer_ext) |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 310 | { |
| 311 | uint32_t i, j; |
| 312 | bool duplicate; |
| 313 | struct extension_property *cur_ext; |
| 314 | |
| 315 | if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0) |
| 316 | loader_init_ext_list(); |
| 317 | |
| 318 | if (loader.scanned_ext_list == NULL) |
| 319 | return; |
| 320 | |
| 321 | struct extension_property *ext_list, **ext_list_addr; |
| 322 | |
| 323 | for (i = 0; i < count; i++) { |
| 324 | cur_ext = prop_list + i; |
| 325 | |
| 326 | // look for duplicates or not |
| 327 | duplicate = false; |
| 328 | for (j = 0; j < loader.scanned_ext_list_count; j++) { |
| 329 | ext_list = loader.scanned_ext_list[j]; |
| 330 | if (!strcmp(cur_ext->extName, ext_list->extName)) { |
| 331 | duplicate = true; |
| 332 | ext_list->hosted = false; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // add to list at end |
| 338 | if (!duplicate) { |
| 339 | // check for enough capacity |
| 340 | if (loader.scanned_ext_list_count * sizeof(struct extension_property *) |
| 341 | >= loader.scanned_ext_list_capacity) { |
| 342 | // double capacity |
| 343 | loader.scanned_ext_list_capacity *= 2; |
| 344 | loader.scanned_ext_list = realloc(loader.scanned_ext_list, |
| 345 | loader.scanned_ext_list_capacity); |
| 346 | } |
| 347 | ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]); |
| 348 | *ext_list_addr = cur_ext; |
| 349 | cur_ext->hosted = true; |
| 350 | } |
| 351 | |
| 352 | } |
| 353 | } |
| 354 | |
Jon Ashburn | fc2e38c | 2015-04-14 09:15:32 -0600 | [diff] [blame] | 355 | static bool loader_is_extension_scanned(const char *name) |
| 356 | { |
| 357 | uint32_t i; |
| 358 | |
| 359 | for (i = 0; i < loader.scanned_ext_list_count; i++) { |
| 360 | if (!strcmp(name, loader.scanned_ext_list[i]->extName)) |
| 361 | return true; |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 366 | static void loader_coalesce_extensions() |
| 367 | { |
| 368 | uint32_t i; |
| 369 | struct loader_scanned_icds *icd_list = loader.scanned_icd_list; |
| 370 | |
| 371 | // traverse scanned icd list adding non-duplicate extensions to the list |
| 372 | while (icd_list != NULL) { |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 373 | loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false); |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 374 | icd_list = icd_list->next; |
| 375 | }; |
| 376 | |
| 377 | //Traverse layers list adding non-duplicate extensions to the list |
| 378 | for (i = 0; i < loader.scanned_layer_count; i++) { |
| 379 | loader_add_to_ext_list(loader.scanned_layers[i].extension_count, |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 380 | loader.scanned_layers[i].extensions, true); |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | static void loader_icd_destroy(struct loader_icd *icd) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 385 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 386 | loader_platform_close_library(icd->scanned_icds->handle); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 387 | free(icd); |
| 388 | } |
| 389 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 390 | static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 391 | { |
| 392 | struct loader_icd *icd; |
| 393 | |
| 394 | icd = malloc(sizeof(*icd)); |
| 395 | if (!icd) |
| 396 | return NULL; |
| 397 | |
Courtney Goeltzenleuchter | 55001bb | 2014-10-28 10:29:27 -0600 | [diff] [blame] | 398 | memset(icd, 0, sizeof(*icd)); |
| 399 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 400 | icd->scanned_icds = scanned; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 401 | |
| 402 | return icd; |
| 403 | } |
| 404 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 405 | static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst, |
| 406 | const struct loader_scanned_icds *scanned) |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 407 | { |
| 408 | struct loader_icd *icd; |
| 409 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 410 | icd = loader_icd_create(scanned); |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 411 | if (!icd) |
| 412 | return NULL; |
| 413 | |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 414 | /* prepend to the list */ |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 415 | icd->next = ptr_inst->icds; |
| 416 | ptr_inst->icds = icd; |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 417 | |
| 418 | return icd; |
| 419 | } |
| 420 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 421 | static void loader_scanned_icd_add(const char *filename) |
| 422 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 423 | loader_platform_dl_handle handle; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 424 | void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst; |
| 425 | void *fp_get_global_ext_info; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 426 | struct loader_scanned_icds *new_node; |
| 427 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 428 | // Used to call: dlopen(filename, RTLD_LAZY); |
| 429 | handle = loader_platform_open_library(filename); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 430 | if (!handle) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 431 | loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename)); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | #define LOOKUP(func_ptr, func) do { \ |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 436 | func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \ |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 437 | if (!func_ptr) { \ |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 438 | loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \ |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 439 | return; \ |
| 440 | } \ |
| 441 | } while (0) |
| 442 | |
| 443 | LOOKUP(fp_gpa, GetProcAddr); |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 444 | LOOKUP(fp_create_inst, CreateInstance); |
| 445 | LOOKUP(fp_destroy_inst, DestroyInstance); |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 446 | LOOKUP(fp_enumerate, EnumeratePhysicalDevices); |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 447 | LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 448 | #undef LOOKUP |
| 449 | |
| 450 | new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds)); |
| 451 | if (!new_node) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 452 | loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd"); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | new_node->handle = handle; |
| 457 | new_node->GetProcAddr = fp_gpa; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 458 | new_node->CreateInstance = fp_create_inst; |
| 459 | new_node->DestroyInstance = fp_destroy_inst; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 460 | new_node->EnumeratePhysicalDevices = fp_enumerate; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 461 | new_node->GetGlobalExtensionInfo = fp_get_global_ext_info; |
| 462 | new_node->extension_count = 0; |
| 463 | new_node->extensions = NULL; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 464 | new_node->next = loader.scanned_icd_list; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 465 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 466 | loader.scanned_icd_list = new_node; |
| 467 | |
| 468 | if (fp_get_global_ext_info) { |
| 469 | get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info, |
| 470 | &new_node->extension_count, |
| 471 | &new_node->extensions); |
| 472 | } else { |
| 473 | loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD"); |
| 474 | } |
| 475 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 476 | |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 477 | /** |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 478 | * Try to \c loader_icd_scan VK driver(s). |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 479 | * |
| 480 | * This function scans the default system path or path |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 481 | * specified by the \c LIBVK_DRIVERS_PATH environment variable in |
| 482 | * order to find loadable VK ICDs with the name of libVK_*. |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 483 | * |
| 484 | * \returns |
| 485 | * void; but side effect is to set loader_icd_scanned to true |
| 486 | */ |
| 487 | static void loader_icd_scan(void) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 488 | { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 489 | const char *p, *next; |
| 490 | char *libPaths = NULL; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 491 | DIR *sysdir; |
| 492 | struct dirent *dent; |
| 493 | char icd_library[1024]; |
Jon Ashburn | 5cda59c | 2014-10-03 16:31:35 -0600 | [diff] [blame] | 494 | char path[1024]; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 495 | uint32_t len; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 496 | #if defined(WIN32) |
| 497 | bool must_free_libPaths; |
| 498 | libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV, |
| 499 | DRIVER_PATH_REGISTRY_VALUE); |
| 500 | if (libPaths != NULL) { |
| 501 | must_free_libPaths = true; |
| 502 | } else { |
| 503 | must_free_libPaths = false; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 504 | libPaths = DEFAULT_VK_DRIVERS_PATH; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 505 | } |
| 506 | #else // WIN32 |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 507 | if (geteuid() == getuid()) { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 508 | /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */ |
| 509 | libPaths = getenv(DRIVER_PATH_ENV); |
| 510 | } |
| 511 | if (libPaths == NULL) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 512 | libPaths = DEFAULT_VK_DRIVERS_PATH; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 513 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 514 | #endif // WIN32 |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 515 | |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 516 | for (p = libPaths; *p; p = next) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 517 | next = strchr(p, PATH_SEPERATOR); |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 518 | if (next == NULL) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 519 | len = (uint32_t) strlen(p); |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 520 | next = p + len; |
| 521 | } |
| 522 | else { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 523 | len = (uint32_t) (next - p); |
Jon Ashburn | 5cda59c | 2014-10-03 16:31:35 -0600 | [diff] [blame] | 524 | sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p); |
| 525 | p = path; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 526 | next++; |
| 527 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 528 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 529 | // TODO/TBD: Do we want to do this on Windows, or just let Windows take |
| 530 | // care of its own search path (which it apparently has)? |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 531 | sysdir = opendir(p); |
| 532 | if (sysdir) { |
| 533 | dent = readdir(sysdir); |
| 534 | while (dent) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 535 | /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and |
| 536 | * ending with VK_LIBRARY_SUFFIX |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 537 | */ |
| 538 | if (!strncmp(dent->d_name, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 539 | VK_DRIVER_LIBRARY_PREFIX, |
| 540 | VK_DRIVER_LIBRARY_PREFIX_LEN)) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 541 | uint32_t nlen = (uint32_t) strlen(dent->d_name); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 542 | const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN; |
| 543 | if ((nlen > VK_LIBRARY_SUFFIX_LEN) && |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 544 | !strncmp(suf, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 545 | VK_LIBRARY_SUFFIX, |
| 546 | VK_LIBRARY_SUFFIX_LEN)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 547 | snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name); |
| 548 | loader_scanned_icd_add(icd_library); |
| 549 | } |
| 550 | } |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 551 | |
| 552 | dent = readdir(sysdir); |
| 553 | } |
| 554 | closedir(sysdir); |
| 555 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 556 | } |
| 557 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 558 | #if defined(WIN32) |
| 559 | // Free any allocated memory: |
| 560 | if (must_free_libPaths) { |
| 561 | free(libPaths); |
| 562 | } |
| 563 | #endif // WIN32 |
| 564 | |
| 565 | // Note that we've scanned for ICDs: |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 566 | loader.icds_scanned = true; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 567 | } |
| 568 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 569 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 570 | static void layer_lib_scan(void) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 571 | { |
| 572 | const char *p, *next; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 573 | char *libPaths = NULL; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 574 | DIR *curdir; |
| 575 | struct dirent *dent; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 576 | size_t len, i; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 577 | char temp_str[1024]; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 578 | uint32_t count; |
| 579 | PFN_vkGetGlobalExtensionInfo fp_get_ext; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 580 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 581 | #if defined(WIN32) |
| 582 | bool must_free_libPaths; |
| 583 | libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV, |
| 584 | LAYERS_PATH_REGISTRY_VALUE); |
| 585 | if (libPaths != NULL) { |
| 586 | must_free_libPaths = true; |
| 587 | } else { |
| 588 | must_free_libPaths = false; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 589 | libPaths = DEFAULT_VK_LAYERS_PATH; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 590 | } |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 591 | #else // WIN32 |
| 592 | if (geteuid() == getuid()) { |
| 593 | /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */ |
Courtney Goeltzenleuchter | 66b72f9 | 2015-02-18 20:03:02 -0700 | [diff] [blame] | 594 | libPaths = getenv(LAYERS_PATH_ENV); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 595 | } |
| 596 | if (libPaths == NULL) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 597 | libPaths = DEFAULT_VK_LAYERS_PATH; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 598 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 599 | #endif // WIN32 |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 600 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 601 | if (libPaths == NULL) { |
| 602 | // Have no paths to search: |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 603 | return; |
| 604 | } |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 605 | len = strlen(libPaths); |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 606 | loader.layer_dirs = malloc(len+1); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 607 | if (loader.layer_dirs == NULL) { |
| 608 | free(libPaths); |
Courtney Goeltzenleuchter | a66265b | 2014-12-02 18:12:51 -0700 | [diff] [blame] | 609 | return; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 610 | } |
| 611 | // Alloc passed, so we know there is enough space to hold the string, don't |
| 612 | // need strncpy |
| 613 | strcpy(loader.layer_dirs, libPaths); |
| 614 | #if defined(WIN32) |
| 615 | // Free any allocated memory: |
| 616 | if (must_free_libPaths) { |
| 617 | free(libPaths); |
| 618 | must_free_libPaths = false; |
| 619 | } |
| 620 | #endif // WIN32 |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 621 | libPaths = loader.layer_dirs; |
| 622 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 623 | /* cleanup any previously scanned libraries */ |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 624 | for (i = 0; i < loader.scanned_layer_count; i++) { |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 625 | if (loader.scanned_layers[i].name != NULL) |
| 626 | free(loader.scanned_layers[i].name); |
| 627 | if (loader.scanned_layers[i].extensions != NULL) |
| 628 | free(loader.scanned_layers[i].extensions); |
| 629 | loader.scanned_layers[i].name = NULL; |
| 630 | loader.scanned_layers[i].extensions = NULL; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 631 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 632 | loader.scanned_layer_count = 0; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 633 | count = 0; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 634 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 635 | for (p = libPaths; *p; p = next) { |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 636 | next = strchr(p, PATH_SEPERATOR); |
| 637 | if (next == NULL) { |
| 638 | len = (uint32_t) strlen(p); |
| 639 | next = p + len; |
| 640 | } |
| 641 | else { |
| 642 | len = (uint32_t) (next - p); |
| 643 | *(char *) next = '\0'; |
| 644 | next++; |
| 645 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 646 | |
| 647 | curdir = opendir(p); |
| 648 | if (curdir) { |
| 649 | dent = readdir(curdir); |
| 650 | while (dent) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 651 | /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and |
| 652 | * ending with VK_LIBRARY_SUFFIX |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 653 | */ |
| 654 | if (!strncmp(dent->d_name, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 655 | VK_LAYER_LIBRARY_PREFIX, |
| 656 | VK_LAYER_LIBRARY_PREFIX_LEN)) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 657 | uint32_t nlen = (uint32_t) strlen(dent->d_name); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 658 | const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN; |
| 659 | if ((nlen > VK_LIBRARY_SUFFIX_LEN) && |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 660 | !strncmp(suf, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 661 | VK_LIBRARY_SUFFIX, |
| 662 | VK_LIBRARY_SUFFIX_LEN)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 663 | loader_platform_dl_handle handle; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 664 | snprintf(temp_str, sizeof(temp_str), |
| 665 | "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 666 | // Used to call: dlopen(temp_str, RTLD_LAZY) |
| 667 | if ((handle = loader_platform_open_library(temp_str)) == NULL) { |
| 668 | dent = readdir(curdir); |
| 669 | continue; |
| 670 | } |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 671 | if (count == MAX_LAYER_LIBRARIES) { |
| 672 | loader_log(VK_DBG_MSG_ERROR, 0, |
| 673 | "%s ignored: max layer libraries exceed", |
| 674 | temp_str); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 675 | break; |
| 676 | } |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 677 | fp_get_ext = loader_platform_get_proc_address(handle, |
| 678 | "vkGetGlobalExtensionInfo"); |
| 679 | |
| 680 | if (!fp_get_ext) { |
| 681 | loader_log(VK_DBG_MSG_WARNING, 0, |
| 682 | "Couldn't dlsym vkGetGlobalExtensionInfo from library %s", |
| 683 | temp_str); |
| 684 | dent = readdir(curdir); |
| 685 | loader_platform_close_library(handle); |
| 686 | continue; |
| 687 | } |
| 688 | |
| 689 | loader.scanned_layers[count].name = |
| 690 | malloc(strlen(temp_str) + 1); |
| 691 | if (loader.scanned_layers[count].name == NULL) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 692 | loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 693 | break; |
| 694 | } |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 695 | |
| 696 | get_global_extensions(fp_get_ext, |
| 697 | &loader.scanned_layers[count].extension_count, |
| 698 | &loader.scanned_layers[count].extensions); |
| 699 | |
| 700 | |
| 701 | strcpy(loader.scanned_layers[count].name, temp_str); |
| 702 | count++; |
| 703 | loader_platform_close_library(handle); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 704 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | dent = readdir(curdir); |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 708 | } // while (dir_entry) |
| 709 | if (count == MAX_LAYER_LIBRARIES) |
| 710 | break; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 711 | closedir(curdir); |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 712 | } // if (curdir)) |
| 713 | } // for (libpaths) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 714 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 715 | loader.scanned_layer_count = count; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 716 | loader.layer_scanned = true; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 717 | } |
| 718 | |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 719 | static void loader_init_dispatch_table(VkLayerDispatchTable *tab, PFN_vkGetProcAddr fpGPA, VkPhysicalDevice gpu) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 720 | { |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 721 | loader_initialize_dispatch_table(tab, fpGPA, gpu); |
| 722 | |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 723 | if (tab->EnumerateLayers == NULL) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 724 | tab->EnumerateLayers = vkEnumerateLayers; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 725 | } |
| 726 | |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 727 | static void *loader_gpa_internal(VkPhysicalDevice gpu, const char * pName) |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 728 | { |
| 729 | if (gpu == NULL) { |
| 730 | return NULL;; |
| 731 | } |
| 732 | VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu; |
| 733 | VkLayerDispatchTable * disp_table = * (VkLayerDispatchTable **) gpuw->baseObject; |
| 734 | void *addr; |
| 735 | |
| 736 | if (disp_table == NULL) |
| 737 | return NULL; |
| 738 | |
| 739 | addr = loader_lookup_dispatch_table(disp_table, pName); |
| 740 | if (addr) |
| 741 | return addr; |
| 742 | else { |
| 743 | if (disp_table->GetProcAddr == NULL) |
| 744 | return NULL; |
| 745 | return disp_table->GetProcAddr(gpuw->nextObject, pName); |
| 746 | } |
| 747 | } |
| 748 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 749 | extern struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index) |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 750 | { |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 751 | /* |
| 752 | * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else |
| 753 | * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so |
| 754 | * need to test it against the wrapped GPU's base object. |
| 755 | */ |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 756 | for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) { |
| 757 | for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) { |
| 758 | for (uint32_t i = 0; i < icd->gpu_count; i++) |
| 759 | if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == |
Jon Ashburn | 4d9f465 | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 760 | gpu) { |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 761 | *gpu_index = i; |
| 762 | return icd; |
| 763 | } |
| 764 | } |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 765 | } |
| 766 | return NULL; |
| 767 | } |
| 768 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 769 | static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index) |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 770 | { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 771 | if (icd->layer_count[gpu_index]) |
| 772 | return true; |
| 773 | else |
| 774 | return false; |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 775 | } |
| 776 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 777 | static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index, |
| 778 | struct layer_name_pair * pLayerNames, |
| 779 | uint32_t count) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 780 | { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 781 | if (!icd) |
| 782 | return; |
Jon Ashburn | df7d584 | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 783 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 784 | struct loader_layers *obj; |
| 785 | bool foundLib; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 786 | for (uint32_t i = 0; i < count; i++) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 787 | foundLib = false; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 788 | for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) { |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 789 | if (icd->layer_libs[gpu_index][j].lib_handle && |
| 790 | !strcmp(icd->layer_libs[gpu_index][j].name, |
| 791 | (char *) pLayerNames[i].layer_name) && |
| 792 | strcmp("Validation", (char *) pLayerNames[i].layer_name)) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 793 | foundLib = true; |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | if (!foundLib) { |
| 798 | obj = &(icd->layer_libs[gpu_index][i]); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 799 | strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1); |
| 800 | obj->name[sizeof(obj->name) - 1] = '\0'; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 801 | // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND) |
| 802 | if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 803 | loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name)); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 804 | continue; |
| 805 | } else { |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 806 | loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", |
| 807 | pLayerNames[i].layer_name, pLayerNames[i].lib_name); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 808 | } |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 809 | free(pLayerNames[i].layer_name); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 810 | icd->layer_count[gpu_index]++; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 811 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 812 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 813 | } |
| 814 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 815 | static bool find_layer_extension(struct loader_icd *icd, uint32_t gpu_index, |
| 816 | const char *pExtName, uint32_t *out_count, |
| 817 | char *lib_name[MAX_LAYER_LIBRARIES]) |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 818 | { |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 819 | char *search_name; |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 820 | uint32_t j, found_count = 0; |
| 821 | bool must_be_hosted; |
| 822 | bool found = false; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 823 | |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 824 | /* |
| 825 | * The loader provides the abstraction that make layers and extensions work via |
| 826 | * the currently defined extension mechanism. That is, when app queries for an extension |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 827 | * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 828 | * to see who implements that extension. Then, if the app enables the extension during |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 829 | * vkCreateInstance the loader will find and load any layers that implement that extension. |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 830 | */ |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 831 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 832 | // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 833 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 834 | for (j = 0; j < loader.scanned_layer_count; j++) { |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 835 | |
| 836 | if (!strcmp("Validation", pExtName)) |
| 837 | must_be_hosted = false; |
| 838 | else |
| 839 | must_be_hosted = true; |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 840 | if (has_extension(loader.scanned_layers[j].extensions, |
| 841 | loader.scanned_layers[j].extension_count, pExtName, |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 842 | must_be_hosted)) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 843 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 844 | found = true; |
| 845 | lib_name[found_count] = loader.scanned_layers[j].name; |
| 846 | found_count++; |
| 847 | } else { |
| 848 | // Extension not found in list for the layer, so test the layer name |
| 849 | // as if it is an extension name. Use default layer name based on |
| 850 | // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX |
| 851 | char *pEnd; |
| 852 | size_t siz; |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 853 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 854 | search_name = loader.scanned_layers[j].name; |
| 855 | search_name = basename(search_name); |
| 856 | search_name += strlen(VK_LAYER_LIBRARY_PREFIX); |
| 857 | pEnd = strrchr(search_name, '.'); |
| 858 | siz = (int) (pEnd - search_name); |
| 859 | if (siz != strlen(pExtName)) |
| 860 | continue; |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 861 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 862 | if (strncmp(search_name, pExtName, siz) == 0) { |
| 863 | found = true; |
| 864 | lib_name[found_count] = loader.scanned_layers[j].name; |
| 865 | found_count++; |
| 866 | } |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 869 | |
| 870 | *out_count = found_count; |
| 871 | return found; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 874 | static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 875 | { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 876 | char *layerEnv; |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 877 | uint32_t i, len, found_count, count = 0; |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 878 | char *p, *pOrig, *next, *name; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 879 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 880 | #if defined(WIN32) |
| 881 | layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV, |
| 882 | LAYER_NAMES_REGISTRY_VALUE); |
| 883 | #else // WIN32 |
| 884 | layerEnv = getenv(LAYER_NAMES_ENV); |
| 885 | #endif // WIN32 |
| 886 | if (layerEnv == NULL) { |
Jon Ashburn | 3fb5544 | 2014-10-23 10:29:09 -0600 | [diff] [blame] | 887 | return 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 888 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 889 | p = malloc(strlen(layerEnv) + 1); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 890 | if (p == NULL) { |
| 891 | #if defined(WIN32) |
| 892 | free(layerEnv); |
| 893 | #endif // WIN32 |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 894 | return 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 895 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 896 | strcpy(p, layerEnv); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 897 | #if defined(WIN32) |
| 898 | free(layerEnv); |
| 899 | #endif // WIN32 |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 900 | pOrig = p; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 901 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 902 | while (p && *p && count < MAX_LAYER_LIBRARIES) { |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 903 | char *lib_name[MAX_LAYER_LIBRARIES]; |
| 904 | //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 905 | next = strchr(p, PATH_SEPERATOR); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 906 | if (next == NULL) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 907 | len = (uint32_t) strlen(p); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 908 | next = p + len; |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 909 | } else { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 910 | len = (uint32_t) (next - p); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 911 | *(char *) next = '\0'; |
| 912 | next++; |
| 913 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 914 | name = basename(p); |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 915 | if (!find_layer_extension(icd, gpu_index, name, &found_count, lib_name)) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 916 | p = next; |
| 917 | continue; |
| 918 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 919 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 920 | for (i = 0; i < found_count; i++) { |
| 921 | len = (uint32_t) strlen(name); |
| 922 | pLayerNames[count].layer_name = malloc(len + 1); |
| 923 | if (!pLayerNames[count].layer_name) { |
| 924 | free(pOrig); |
| 925 | return count; |
| 926 | } |
| 927 | strncpy((char *) pLayerNames[count].layer_name, name, len); |
| 928 | pLayerNames[count].layer_name[len] = '\0'; |
| 929 | pLayerNames[count].lib_name = lib_name[i]; |
| 930 | count++; |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 931 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 932 | p = next; |
| 933 | |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 934 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 935 | |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 936 | free(pOrig); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 937 | return count; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 938 | } |
| 939 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 940 | static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names, struct layer_name_pair **ppLayerNames) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 941 | { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 942 | static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES]; |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 943 | char *lib_name[MAX_LAYER_LIBRARIES]; |
| 944 | uint32_t found_count, count = 0; |
| 945 | bool skip; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 946 | |
| 947 | *ppLayerNames = &layerNames[0]; |
Courtney Goeltzenleuchter | 89ba928 | 2015-02-17 14:21:21 -0700 | [diff] [blame] | 948 | /* Load any layers specified in the environment first */ |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 949 | count = loader_get_layer_env(icd, gpu_index, layerNames); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 950 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 951 | for (uint32_t i = 0; i < ext_count; i++) { |
| 952 | const char *pExtName = ext_names[i]; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 953 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 954 | skip = false; |
| 955 | for (uint32_t j = 0; j < count; j++) { |
| 956 | if (!strcmp(pExtName, layerNames[j].layer_name) ) { |
| 957 | // Extension / Layer already on the list skip it |
| 958 | skip = true; |
| 959 | break; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 960 | } |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 961 | } |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 962 | |
Jon Ashburn | 19c2502 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 963 | if (!skip && find_layer_extension(icd, gpu_index, pExtName, &found_count, lib_name)) { |
| 964 | |
| 965 | for (uint32_t j = 0; j < found_count; j++) { |
| 966 | uint32_t len; |
| 967 | len = (uint32_t) strlen(pExtName); |
| 968 | |
| 969 | |
| 970 | layerNames[count].layer_name = malloc(len + 1); |
| 971 | if (!layerNames[count].layer_name) |
| 972 | return count; |
| 973 | strncpy((char *) layerNames[count].layer_name, pExtName, len); |
| 974 | layerNames[count].layer_name[len] = '\0'; |
| 975 | layerNames[count].lib_name = lib_name[j]; |
| 976 | count++; |
| 977 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 978 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 979 | } |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 980 | |
| 981 | return count; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 982 | } |
| 983 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 984 | static void loader_deactivate_layer(const struct loader_instance *instance) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 985 | { |
| 986 | struct loader_icd *icd; |
| 987 | struct loader_layers *libs; |
| 988 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 989 | for (icd = instance->icds; icd; icd = icd->next) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 990 | if (icd->gpus) |
| 991 | free(icd->gpus); |
| 992 | icd->gpus = NULL; |
| 993 | if (icd->loader_dispatch) |
| 994 | free(icd->loader_dispatch); |
| 995 | icd->loader_dispatch = NULL; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 996 | for (uint32_t j = 0; j < icd->gpu_count; j++) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 997 | if (icd->layer_count[j] > 0) { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 998 | for (uint32_t i = 0; i < icd->layer_count[j]; i++) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 999 | libs = &(icd->layer_libs[j][i]); |
| 1000 | if (libs->lib_handle) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1001 | loader_platform_close_library(libs->lib_handle); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1002 | libs->lib_handle = NULL; |
| 1003 | } |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 1004 | if (icd->wrappedGpus[j]) |
| 1005 | free(icd->wrappedGpus[j]); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1006 | } |
| 1007 | icd->layer_count[j] = 0; |
| 1008 | } |
| 1009 | icd->gpu_count = 0; |
| 1010 | } |
| 1011 | } |
| 1012 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1013 | extern uint32_t loader_activate_layers(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1014 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1015 | uint32_t count; |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1016 | VkBaseLayerObject *gpu; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 1017 | struct layer_name_pair *pLayerNames; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1018 | if (!icd) |
| 1019 | return 0; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1020 | assert(gpu_index < MAX_GPUS_FOR_LAYER); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1021 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1022 | gpu = icd->gpus + gpu_index; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1023 | /* activate any layer libraries */ |
| 1024 | if (!loader_layers_activated(icd, gpu_index)) { |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1025 | VkBaseLayerObject *gpuObj = gpu; |
| 1026 | VkBaseLayerObject *nextGpuObj, *baseObj = gpuObj->baseObject; |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1027 | PFN_vkGetProcAddr nextGPA = loader_gpa_internal; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1028 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1029 | count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1030 | if (!count) |
| 1031 | return 0; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 1032 | loader_init_layer_libs(icd, gpu_index, pLayerNames, count); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1033 | |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1034 | icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]); |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 1035 | if (! icd->wrappedGpus[gpu_index]) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1036 | loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer"); |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1037 | for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) { |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 1038 | nextGpuObj = (icd->wrappedGpus[gpu_index] + i); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1039 | nextGpuObj->pGPA = nextGPA; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1040 | nextGpuObj->baseObject = baseObj; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1041 | nextGpuObj->nextObject = gpuObj; |
| 1042 | gpuObj = nextGpuObj; |
| 1043 | |
Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 1044 | char funcStr[256]; |
| 1045 | snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name); |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1046 | if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL) |
| 1047 | nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr"); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1048 | if (!nextGPA) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1049 | loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1050 | continue; |
| 1051 | } |
| 1052 | |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1053 | if (i == 0) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1054 | loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj); |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1055 | //Insert the new wrapped objects into the list with loader object at head |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1056 | gpu->nextObject = gpuObj; |
| 1057 | gpu->pGPA = nextGPA; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1058 | gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1; |
| 1059 | gpuObj->nextObject = baseObj; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1060 | gpuObj->pGPA = icd->scanned_icds->GetProcAddr; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1061 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1062 | |
| 1063 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1064 | } |
| 1065 | else { |
| 1066 | //make sure requested Layers matches currently activated Layers |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1067 | count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames); |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1068 | for (uint32_t i = 0; i < count; i++) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 1069 | if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1070 | loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested"); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | if (count != icd->layer_count[gpu_index]) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1075 | loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested"); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | return icd->layer_count[gpu_index]; |
| 1079 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1080 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1081 | LOADER_EXPORT VkResult VKAPI vkCreateInstance( |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1082 | const VkInstanceCreateInfo* pCreateInfo, |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1083 | VkInstance* pInstance) |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1084 | { |
| 1085 | struct loader_instance *ptr_instance = NULL; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1086 | struct loader_scanned_icds *scanned_icds; |
| 1087 | struct loader_icd *icd; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1088 | VkResult res = VK_ERROR_INITIALIZATION_FAILED; |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1089 | uint32_t i; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1090 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1091 | /* Scan/discover all ICD libraries in a single-threaded manner */ |
| 1092 | loader_platform_thread_once(&once_icd, loader_icd_scan); |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1093 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1094 | /* get layer libraries in a single-threaded manner */ |
| 1095 | loader_platform_thread_once(&once_layer, layer_lib_scan); |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1096 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1097 | /* merge any duplicate extensions */ |
| 1098 | loader_platform_thread_once(&once_exts, loader_coalesce_extensions); |
| 1099 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1100 | ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance)); |
| 1101 | if (ptr_instance == NULL) { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1102 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1103 | } |
| 1104 | memset(ptr_instance, 0, sizeof(struct loader_instance)); |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1105 | ptr_instance->extension_count = pCreateInfo->extensionCount; |
| 1106 | ptr_instance->extension_names = (ptr_instance->extension_count > 0) ? |
| 1107 | malloc(sizeof (char *) * ptr_instance->extension_count) : NULL; |
Jon Ashburn | fc2e38c | 2015-04-14 09:15:32 -0600 | [diff] [blame] | 1108 | if (ptr_instance->extension_names == NULL && (ptr_instance->extension_count > 0)) |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1109 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1110 | for (i = 0; i < ptr_instance->extension_count; i++) { |
Jon Ashburn | fc2e38c | 2015-04-14 09:15:32 -0600 | [diff] [blame] | 1111 | if (!loader_is_extension_scanned(pCreateInfo->ppEnabledExtensionNames[i])) |
| 1112 | return VK_ERROR_INVALID_EXTENSION; |
| 1113 | ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i]) + 1); |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1114 | if (ptr_instance->extension_names[i] == NULL) |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1115 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1116 | strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]); |
| 1117 | } |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1118 | ptr_instance->next = loader.instances; |
| 1119 | loader.instances = ptr_instance; |
| 1120 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1121 | scanned_icds = loader.scanned_icd_list; |
| 1122 | while (scanned_icds) { |
| 1123 | icd = loader_icd_add(ptr_instance, scanned_icds); |
| 1124 | if (icd) { |
Jon Ashburn | b317fad | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1125 | res = scanned_icds->CreateInstance(pCreateInfo, |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1126 | &(scanned_icds->instance)); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1127 | if (res != VK_SUCCESS) |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1128 | { |
| 1129 | ptr_instance->icds = ptr_instance->icds->next; |
| 1130 | loader_icd_destroy(icd); |
| 1131 | scanned_icds->instance = NULL; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1132 | loader_log(VK_DBG_MSG_WARNING, 0, |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1133 | "ICD ignored: failed to CreateInstance on device"); |
| 1134 | } |
| 1135 | } |
| 1136 | scanned_icds = scanned_icds->next; |
| 1137 | } |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1138 | |
Ian Elliott | eb45076 | 2015-02-05 15:19:15 -0700 | [diff] [blame] | 1139 | if (ptr_instance->icds == NULL) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1140 | return VK_ERROR_INCOMPATIBLE_DRIVER; |
Ian Elliott | eb45076 | 2015-02-05 15:19:15 -0700 | [diff] [blame] | 1141 | } |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1142 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1143 | *pInstance = (VkInstance) ptr_instance; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1144 | return VK_SUCCESS; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1147 | LOADER_EXPORT VkResult VKAPI vkDestroyInstance( |
| 1148 | VkInstance instance) |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1149 | { |
| 1150 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1151 | struct loader_scanned_icds *scanned_icds; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1152 | VkResult res; |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1153 | uint32_t i; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1154 | |
| 1155 | // Remove this instance from the list of instances: |
| 1156 | struct loader_instance *prev = NULL; |
| 1157 | struct loader_instance *next = loader.instances; |
| 1158 | while (next != NULL) { |
| 1159 | if (next == ptr_instance) { |
| 1160 | // Remove this instance from the list: |
Jon Ashburn | 340d666 | 2015-04-08 15:49:00 -0600 | [diff] [blame] | 1161 | for (i = 0; i < ptr_instance->extension_count; i++) { |
| 1162 | free(ptr_instance->extension_names[i]); |
| 1163 | } |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1164 | if (prev) |
| 1165 | prev->next = next->next; |
Jon Ashburn | c5c4960 | 2015-02-03 09:26:59 -0700 | [diff] [blame] | 1166 | else |
| 1167 | loader.instances = next->next; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1168 | break; |
| 1169 | } |
| 1170 | prev = next; |
| 1171 | next = next->next; |
| 1172 | } |
| 1173 | if (next == NULL) { |
| 1174 | // This must be an invalid instance handle or empty list |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1175 | return VK_ERROR_INVALID_HANDLE; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1178 | // cleanup any prior layer initializations |
| 1179 | loader_deactivate_layer(ptr_instance); |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1180 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1181 | scanned_icds = loader.scanned_icd_list; |
| 1182 | while (scanned_icds) { |
| 1183 | if (scanned_icds->instance) |
| 1184 | res = scanned_icds->DestroyInstance(scanned_icds->instance); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1185 | if (res != VK_SUCCESS) |
| 1186 | loader_log(VK_DBG_MSG_WARNING, 0, |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1187 | "ICD ignored: failed to DestroyInstance on device"); |
| 1188 | scanned_icds->instance = NULL; |
| 1189 | scanned_icds = scanned_icds->next; |
| 1190 | } |
| 1191 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1192 | free(ptr_instance); |
| 1193 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1194 | return VK_SUCCESS; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1197 | LOADER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices( |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1198 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1199 | VkInstance instance, |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1200 | uint32_t* pPhysicalDeviceCount, |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1201 | VkPhysicalDevice* pPhysicalDevices) |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1202 | { |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1203 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
| 1204 | struct loader_icd *icd; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1205 | uint32_t n, count = 0; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1206 | VkResult res; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1207 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1208 | //in spirit of VK don't error check on the instance parameter |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1209 | icd = ptr_instance->icds; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1210 | if (pPhysicalDevices == NULL) { |
| 1211 | while (icd) { |
| 1212 | res = icd->scanned_icds->EnumeratePhysicalDevices( |
| 1213 | icd->scanned_icds->instance, |
| 1214 | &n, NULL); |
| 1215 | if (res != VK_SUCCESS) |
| 1216 | return res; |
| 1217 | icd->gpu_count = n; |
| 1218 | count += n; |
| 1219 | icd = icd->next; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1222 | ptr_instance->total_gpu_count = count; |
Courtney Goeltzenleuchter | 64ca923 | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 1223 | |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1224 | } else |
| 1225 | { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1226 | VkPhysicalDevice* gpus; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1227 | if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count) |
| 1228 | return VK_ERROR_INVALID_VALUE; |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1229 | gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount); |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1230 | if (!gpus) |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1231 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1232 | while (icd) { |
| 1233 | VkBaseLayerObject * wrapped_gpus; |
| 1234 | PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr; |
| 1235 | |
| 1236 | res = icd->scanned_icds->EnumeratePhysicalDevices( |
| 1237 | icd->scanned_icds->instance, |
| 1238 | &n, |
| 1239 | gpus); |
| 1240 | if (res == VK_SUCCESS && n) { |
| 1241 | wrapped_gpus = (VkBaseLayerObject*) malloc(n * |
| 1242 | sizeof(VkBaseLayerObject)); |
| 1243 | icd->gpus = wrapped_gpus; |
| 1244 | icd->gpu_count = n; |
| 1245 | icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n * |
| 1246 | sizeof(VkLayerDispatchTable)); |
| 1247 | for (unsigned int i = 0; i < n; i++) { |
| 1248 | (wrapped_gpus + i)->baseObject = gpus[i]; |
| 1249 | (wrapped_gpus + i)->pGPA = get_proc_addr; |
| 1250 | (wrapped_gpus + i)->nextObject = gpus[i]; |
| 1251 | memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices)); |
| 1252 | loader_init_dispatch_table(icd->loader_dispatch + i, |
| 1253 | get_proc_addr, gpus[i]); |
| 1254 | |
| 1255 | /* Verify ICD compatibility */ |
| 1256 | if (!valid_loader_magic_value(gpus[i])) { |
| 1257 | loader_log(VK_DBG_MSG_WARNING, 0, |
Courtney Goeltzenleuchter | 64ca923 | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 1258 | "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n"); |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1259 | assert(0); |
| 1260 | } |
| 1261 | |
| 1262 | const VkLayerDispatchTable **disp; |
| 1263 | disp = (const VkLayerDispatchTable **) gpus[i]; |
| 1264 | *disp = icd->loader_dispatch + i; |
| 1265 | loader_activate_layers(icd, i, ptr_instance->extension_count, |
| 1266 | (const char *const*) ptr_instance->extension_names); |
Courtney Goeltzenleuchter | 64ca923 | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1269 | count += n; |
| 1270 | |
| 1271 | if (count >= *pPhysicalDeviceCount) { |
| 1272 | break; |
| 1273 | } |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1276 | icd = icd->next; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1277 | } |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Jon Ashburn | 83a6425 | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1280 | *pPhysicalDeviceCount = count; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1281 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1282 | return (count > 0) ? VK_SUCCESS : res; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1285 | LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char * pName) |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1286 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1287 | if (gpu == NULL) { |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1288 | |
| 1289 | /* return entrypoint addresses that are global (in the loader)*/ |
| 1290 | return globalGetProcAddr(pName); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1291 | } |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1292 | |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 1293 | void *addr; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1294 | |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1295 | /* for entrypoints that loader must handle (ie non-dispatchable or create object) |
| 1296 | make sure the loader entrypoint is returned */ |
| 1297 | addr = loader_non_passthrough_gpa(pName); |
Ian Elliott | e19c915 | 2015-04-15 12:53:19 -0600 | [diff] [blame] | 1298 | if (addr) { |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1299 | return addr; |
Ian Elliott | e19c915 | 2015-04-15 12:53:19 -0600 | [diff] [blame] | 1300 | } |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1301 | |
| 1302 | /* return the dispatch table entrypoint for the fastest case */ |
| 1303 | const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) gpu; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1304 | if (disp_table == NULL) |
| 1305 | return NULL; |
| 1306 | |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 1307 | addr = loader_lookup_dispatch_table(disp_table, pName); |
| 1308 | if (addr) |
| 1309 | return addr; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1310 | else { |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1311 | if (disp_table->GetProcAddr == NULL) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1312 | return NULL; |
Jon Ashburn | 3d526cb | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1313 | return disp_table->GetProcAddr(gpu, pName); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1317 | //TODO make sure createInstance enables extensions that are valid (loader does) |
| 1318 | //TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do) |
| 1319 | |
| 1320 | //TODO how is layer extension going to be enabled? |
| 1321 | //Need to call createInstance on the layer or something |
| 1322 | |
| 1323 | LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
| 1324 | VkExtensionInfoType infoType, |
| 1325 | uint32_t extensionIndex, |
| 1326 | size_t* pDataSize, |
| 1327 | void* pData) |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1328 | { |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1329 | VkExtensionProperties *ext_props; |
| 1330 | uint32_t *count; |
| 1331 | /* Scan/discover all ICD libraries in a single-threaded manner */ |
| 1332 | loader_platform_thread_once(&once_icd, loader_icd_scan); |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1333 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1334 | /* get layer libraries in a single-threaded manner */ |
| 1335 | loader_platform_thread_once(&once_layer, layer_lib_scan); |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1336 | |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1337 | /* merge any duplicate extensions */ |
| 1338 | loader_platform_thread_once(&once_exts, loader_coalesce_extensions); |
| 1339 | |
| 1340 | |
| 1341 | if (pDataSize == NULL) |
| 1342 | return VK_ERROR_INVALID_POINTER; |
| 1343 | |
| 1344 | switch (infoType) { |
| 1345 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 1346 | *pDataSize = sizeof(uint32_t); |
| 1347 | if (pData == NULL) |
| 1348 | return VK_SUCCESS; |
| 1349 | count = (uint32_t *) pData; |
| 1350 | *count = loader.scanned_ext_list_count; |
| 1351 | break; |
| 1352 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 1353 | *pDataSize = sizeof(VkExtensionProperties); |
| 1354 | if (pData == NULL) |
| 1355 | return VK_SUCCESS; |
| 1356 | if (extensionIndex >= loader.scanned_ext_list_count) |
| 1357 | return VK_ERROR_INVALID_VALUE; |
| 1358 | ext_props = (VkExtensionProperties *) pData; |
| 1359 | ext_props->version = loader.scanned_ext_list[extensionIndex]->version; |
| 1360 | strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName |
| 1361 | , VK_MAX_EXTENSION_NAME); |
| 1362 | ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0'; |
| 1363 | break; |
| 1364 | default: |
| 1365 | loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo"); |
| 1366 | return VK_ERROR_INVALID_VALUE; |
| 1367 | }; |
| 1368 | |
| 1369 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame^] | 1372 | LOADER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved) |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1373 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1374 | uint32_t gpu_index; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1375 | size_t count = 0; |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1376 | char *lib_name; |
Jon Ashburn | bacb0f5 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1377 | struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1378 | loader_platform_dl_handle handle; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1379 | PFN_vkEnumerateLayers fpEnumerateLayers; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1380 | char layer_buf[16][256]; |
| 1381 | char * layers[16]; |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1382 | |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1383 | if (pOutLayerCount == NULL || pOutLayers == NULL) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1384 | return VK_ERROR_INVALID_POINTER; |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1385 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1386 | if (!icd) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1387 | return VK_ERROR_UNAVAILABLE; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1388 | |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1389 | for (int i = 0; i < 16; i++) |
| 1390 | layers[i] = &layer_buf[i][0]; |
| 1391 | |
| 1392 | for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) { |
Jon Ashburn | 9fd4cc4 | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1393 | lib_name = loader.scanned_layers[j].name; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1394 | // Used to call: dlopen(*lib_name, RTLD_LAZY) |
| 1395 | if ((handle = loader_platform_open_library(lib_name)) == NULL) |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1396 | continue; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1397 | if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) { |
| 1398 | //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1399 | char *pEnd, *cpyStr; |
Ian Elliott | 4204584 | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 1400 | size_t siz; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1401 | loader_platform_close_library(handle); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1402 | lib_name = basename(lib_name); |
| 1403 | pEnd = strrchr(lib_name, '.'); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1404 | siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1405 | if (pEnd == NULL || siz <= 0) |
| 1406 | continue; |
| 1407 | cpyStr = malloc(siz); |
| 1408 | if (cpyStr == NULL) { |
| 1409 | free(cpyStr); |
| 1410 | continue; |
| 1411 | } |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1412 | strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1413 | cpyStr[siz - 1] = '\0'; |
| 1414 | if (siz > maxStringSize) |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1415 | siz = (int) maxStringSize; |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1416 | strncpy((char *) (pOutLayers[count]), cpyStr, siz); |
| 1417 | pOutLayers[count][siz - 1] = '\0'; |
| 1418 | count++; |
| 1419 | free(cpyStr); |
Courtney Goeltzenleuchter | 499b3ba | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1420 | } else { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1421 | size_t cnt; |
| 1422 | uint32_t n; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1423 | VkResult res; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1424 | n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1425 | res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index); |
| 1426 | loader_platform_close_library(handle); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1427 | if (res != VK_SUCCESS) |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1428 | continue; |
| 1429 | if (cnt + count > maxLayerCount) |
| 1430 | cnt = maxLayerCount - count; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1431 | for (uint32_t i = (uint32_t) count; i < cnt + count; i++) { |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1432 | strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n); |
| 1433 | if (n > 0) |
| 1434 | pOutLayers[i - count][n - 1] = '\0'; |
| 1435 | } |
| 1436 | count += cnt; |
| 1437 | } |
| 1438 | } |
| 1439 | |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1440 | *pOutLayerCount = count; |
| 1441 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1442 | return VK_SUCCESS; |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1443 | } |
| 1444 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1445 | LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1446 | { |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1447 | const struct loader_icd *icd; |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1448 | struct loader_instance *inst; |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1449 | VkResult res; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1450 | uint32_t gpu_idx; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1451 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1452 | if (instance == VK_NULL_HANDLE) |
| 1453 | return VK_ERROR_INVALID_HANDLE; |
Courtney Goeltzenleuchter | c80a557 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 1454 | |
| 1455 | assert(loader.icds_scanned); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1456 | |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1457 | for (inst = loader.instances; inst; inst = inst->next) { |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1458 | if (inst == instance) |
| 1459 | break; |
| 1460 | } |
| 1461 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1462 | if (inst == VK_NULL_HANDLE) |
| 1463 | return VK_ERROR_INVALID_HANDLE; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1464 | |
| 1465 | for (icd = inst->icds; icd; icd = icd->next) { |
| 1466 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
| 1467 | res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance, |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1468 | pfnMsgCallback, pUserData); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1469 | if (res != VK_SUCCESS) { |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1470 | gpu_idx = i; |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1471 | break; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1472 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1473 | } |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1474 | if (res != VK_SUCCESS) |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1475 | break; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1476 | } |
| 1477 | |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1478 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1479 | /* roll back on errors */ |
| 1480 | if (icd) { |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1481 | for (const struct loader_icd *tmp = inst->icds; tmp != icd; |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1482 | tmp = tmp->next) { |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1483 | for (uint32_t i = 0; i < icd->gpu_count; i++) |
| 1484 | (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1485 | } |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1486 | /* and gpus on current icd */ |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1487 | for (uint32_t i = 0; i < gpu_idx; i++) |
Courtney Goeltzenleuchter | c80a557 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 1488 | (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1489 | |
| 1490 | return res; |
| 1491 | } |
| 1492 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1493 | return VK_SUCCESS; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1496 | LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1497 | { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1498 | VkResult res = VK_SUCCESS; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1499 | struct loader_instance *inst; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1500 | if (instance == VK_NULL_HANDLE) |
| 1501 | return VK_ERROR_INVALID_HANDLE; |
Courtney Goeltzenleuchter | c80a557 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 1502 | |
| 1503 | assert(loader.icds_scanned); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1504 | |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1505 | for (inst = loader.instances; inst; inst = inst->next) { |
| 1506 | if (inst == instance) |
| 1507 | break; |
| 1508 | } |
| 1509 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1510 | if (inst == VK_NULL_HANDLE) |
| 1511 | return VK_ERROR_INVALID_HANDLE; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1512 | |
| 1513 | for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) { |
| 1514 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1515 | VkResult r; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1516 | r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1517 | if (r != VK_SUCCESS) { |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1518 | res = r; |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1519 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1520 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1521 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1522 | return res; |
| 1523 | } |
| 1524 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1525 | LOADER_EXPORT VkResult VKAPI vkDbgSetGlobalOption(VkInstance instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1526 | { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1527 | VkResult res = VK_SUCCESS; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1528 | struct loader_instance *inst; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1529 | if (instance == VK_NULL_HANDLE) |
| 1530 | return VK_ERROR_INVALID_HANDLE; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1531 | |
Courtney Goeltzenleuchter | c80a557 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 1532 | assert(loader.icds_scanned); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1533 | |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1534 | for (inst = loader.instances; inst; inst = inst->next) { |
| 1535 | if (inst == instance) |
| 1536 | break; |
| 1537 | } |
| 1538 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1539 | if (inst == VK_NULL_HANDLE) |
| 1540 | return VK_ERROR_INVALID_HANDLE; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1541 | for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) { |
| 1542 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1543 | VkResult r; |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1544 | r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption, |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1545 | dataSize, pData); |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1546 | /* unfortunately we cannot roll back */ |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1547 | if (r != VK_SUCCESS) { |
Jon Ashburn | bb51025 | 2015-03-17 13:47:55 -0600 | [diff] [blame] | 1548 | res = r; |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1549 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1550 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | return res; |
| 1554 | } |