Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 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 | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
Jon Ashburn | 406a0fe | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 26 | * Jon Ashburn <jon@lunarg.com> |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 27 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
Ian Elliott | 7600513 | 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 | 183dfd0 | 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 | 894a117 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 37 | #include <sys/types.h> |
Ian Elliott | 81ac44c | 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 | 894a117 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 41 | #include <dirent.h> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 42 | #endif // WIN32 |
| 43 | #include "loader_platform.h" |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 44 | #include "loader.h" |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 45 | #include "wsi_lunarg.h" |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 46 | #include "gpa_helper.h" |
| 47 | #include "table_ops.h" |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 48 | #include "debug_report.h" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 49 | #include "vkIcd.h" |
Ian Elliott | 20f0687 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 50 | // The following is #included again to catch certain OS-specific functions |
| 51 | // being used: |
| 52 | #include "loader_platform.h" |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 53 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 54 | void loader_add_to_ext_list( |
| 55 | struct loader_extension_list *ext_list, |
| 56 | uint32_t prop_list_count, |
| 57 | const struct loader_extension_property *prop_list); |
| 58 | |
| 59 | static void loader_deactivate_instance_layers(struct loader_instance *instance); |
| 60 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 61 | /* TODO: do we need to lock around access to linked lists and such? */ |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 62 | struct loader_struct loader = {0}; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 63 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 64 | VkLayerInstanceDispatchTable instance_disp = { |
| 65 | .GetInstanceProcAddr = vkGetInstanceProcAddr, |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 66 | .CreateInstance = loader_CreateInstance, |
| 67 | .DestroyInstance = loader_DestroyInstance, |
| 68 | .EnumeratePhysicalDevices = loader_EnumeratePhysicalDevices, |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 69 | .GetPhysicalDeviceInfo = loader_GetPhysicalDeviceInfo, |
| 70 | .CreateDevice = loader_CreateDevice, |
Jon Ashburn | 9de9560 | 2015-05-18 15:28:32 -0600 | [diff] [blame] | 71 | .GetGlobalExtensionInfo = vkGetGlobalExtensionInfo, |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 72 | .GetPhysicalDeviceExtensionInfo = loader_GetPhysicalDeviceExtensionInfo, |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 73 | .GetMultiDeviceCompatibility = loader_GetMultiDeviceCompatibility, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 74 | .GetDisplayInfoWSI = loader_GetDisplayInfoWSI, |
| 75 | .DbgCreateMsgCallback = loader_DbgCreateMsgCallback, |
| 76 | .DbgDestroyMsgCallback = loader_DbgDestroyMsgCallback, |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd); |
| 80 | LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer); |
| 81 | LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 82 | |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 83 | #if defined(WIN32) |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 84 | char *loader_get_registry_string(const HKEY hive, |
| 85 | const LPCTSTR sub_key, |
| 86 | const char *value) |
| 87 | { |
| 88 | DWORD access_flags = KEY_QUERY_VALUE; |
| 89 | DWORD value_type; |
| 90 | HKEY key; |
Ian Elliott | 464711a | 2015-04-28 15:57:32 -0600 | [diff] [blame] | 91 | VkResult rtn_value; |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 92 | char *rtn_str = NULL; |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 93 | DWORD rtn_len = 0; |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 94 | size_t allocated_len = 0; |
| 95 | |
| 96 | rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key); |
| 97 | if (rtn_value != ERROR_SUCCESS) { |
| 98 | // We didn't find the key. Try the 32-bit hive (where we've seen the |
| 99 | // key end up on some people's systems): |
| 100 | access_flags |= KEY_WOW64_32KEY; |
| 101 | rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key); |
| 102 | if (rtn_value != ERROR_SUCCESS) { |
| 103 | // We still couldn't find the key, so give up: |
| 104 | return NULL; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | rtn_value = RegQueryValueEx(key, value, NULL, &value_type, |
Ian Elliott | 464711a | 2015-04-28 15:57:32 -0600 | [diff] [blame] | 109 | (PVOID) rtn_str, (LPDWORD) &rtn_len); |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 110 | if (rtn_value == ERROR_SUCCESS) { |
| 111 | // If we get to here, we found the key, and need to allocate memory |
| 112 | // large enough for rtn_str, and query again: |
| 113 | allocated_len = rtn_len + 4; |
| 114 | rtn_str = malloc(allocated_len); |
| 115 | rtn_value = RegQueryValueEx(key, value, NULL, &value_type, |
Ian Elliott | 464711a | 2015-04-28 15:57:32 -0600 | [diff] [blame] | 116 | (PVOID) rtn_str, (LPDWORD) &rtn_len); |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 117 | if (rtn_value == ERROR_SUCCESS) { |
| 118 | // We added 4 extra bytes to rtn_str, so that we can ensure that |
| 119 | // the string is NULL-terminated (albeit, in a brute-force manner): |
| 120 | rtn_str[allocated_len-1] = '\0'; |
| 121 | } else { |
| 122 | // This should never occur, but in case it does, clean up: |
| 123 | free(rtn_str); |
| 124 | rtn_str = NULL; |
| 125 | } |
| 126 | } // else - shouldn't happen, but if it does, return rtn_str, which is NULL |
| 127 | |
| 128 | // Close the registry key that was opened: |
| 129 | RegCloseKey(key); |
| 130 | |
| 131 | return rtn_str; |
| 132 | } |
| 133 | |
| 134 | |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 135 | // For ICD developers, look in the registry, and look for an environment |
| 136 | // variable for a path(s) where to find the ICD(s): |
| 137 | static char *loader_get_registry_and_env(const char *env_var, |
| 138 | const char *registry_value) |
| 139 | { |
| 140 | char *env_str = getenv(env_var); |
| 141 | size_t env_len = (env_str == NULL) ? 0 : strlen(env_str); |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 142 | char *registry_str = NULL; |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 143 | size_t registry_len = 0; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 144 | char *rtn_str = NULL; |
| 145 | size_t rtn_len; |
| 146 | |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 147 | registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE, |
Ian Elliott | 10ec962 | 2015-04-09 18:07:15 -0600 | [diff] [blame] | 148 | "Software\\Vulkan", |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 149 | registry_value); |
Ian Elliott | 464711a | 2015-04-28 15:57:32 -0600 | [diff] [blame] | 150 | registry_len = (registry_str) ? (DWORD) strlen(registry_str) : 0; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 151 | |
| 152 | rtn_len = env_len + registry_len + 1; |
| 153 | if (rtn_len <= 2) { |
| 154 | // We found neither the desired registry value, nor the environment |
| 155 | // variable; return NULL: |
| 156 | return NULL; |
| 157 | } else { |
| 158 | // We found something, and so we need to allocate memory for the string |
| 159 | // to return: |
| 160 | rtn_str = malloc(rtn_len); |
| 161 | } |
| 162 | |
Ian Elliott | 7600513 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 163 | if (registry_len == 0) { |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 164 | // We didn't find the desired registry value, and so we must have found |
| 165 | // only the environment variable: |
| 166 | _snprintf(rtn_str, rtn_len, "%s", env_str); |
| 167 | } else if (env_str != NULL) { |
| 168 | // We found both the desired registry value and the environment |
| 169 | // variable, so concatenate them both: |
| 170 | _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str); |
| 171 | } else { |
| 172 | // We must have only found the desired registry value: |
| 173 | _snprintf(rtn_str, rtn_len, "%s", registry_str); |
| 174 | } |
| 175 | |
Ian Elliott | 7518ecd | 2015-04-03 13:13:01 -0600 | [diff] [blame] | 176 | if (registry_str) { |
| 177 | free(registry_str); |
| 178 | } |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 179 | |
| 180 | return(rtn_str); |
| 181 | } |
| 182 | #endif // WIN32 |
| 183 | |
| 184 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 185 | static void loader_log(VkFlags msg_type, int32_t msg_code, |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 186 | const char *format, ...) |
| 187 | { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 188 | char msg[256]; |
| 189 | va_list ap; |
| 190 | int ret; |
| 191 | |
| 192 | va_start(ap, format); |
| 193 | ret = vsnprintf(msg, sizeof(msg), format, ap); |
Ian Elliott | 642f892 | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 194 | if ((ret >= (int) sizeof(msg)) || ret < 0) { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 195 | msg[sizeof(msg) - 1] = '\0'; |
| 196 | } |
| 197 | va_end(ap); |
| 198 | |
Jon Ashburn | b67859d | 2015-04-24 14:10:50 -0700 | [diff] [blame] | 199 | #if defined(WIN32) |
| 200 | OutputDebugString(msg); |
Jon Ashburn | 1da405d | 2015-05-05 16:20:46 -0600 | [diff] [blame] | 201 | #endif |
Courtney Goeltzenleuchter | 9d36ef4 | 2015-04-13 14:10:06 -0600 | [diff] [blame] | 202 | fputs(msg, stderr); |
| 203 | fputc('\n', stderr); |
Jon Ashburn | 1da405d | 2015-05-05 16:20:46 -0600 | [diff] [blame] | 204 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 205 | } |
| 206 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 207 | bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2) |
| 208 | { |
| 209 | return memcmp(op1, op2, sizeof(VkExtensionProperties)) == 0 ? true : false; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Used to look for an extension with a specific name. |
| 214 | * Ignores all other extension info (i.e. version, origin & dependencies) |
| 215 | */ |
| 216 | static bool has_extension_name( |
| 217 | uint32_t count, |
| 218 | struct loader_extension_property *exts, |
| 219 | const char *target_ext_name, |
| 220 | bool must_be_hosted) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 221 | { |
| 222 | uint32_t i; |
| 223 | for (i = 0; i < count; i++) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 224 | if (!strcmp(exts[i].info.name, target_ext_name) && (!must_be_hosted || exts[i].hosted)) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 230 | static bool has_extension( |
| 231 | uint32_t count, |
| 232 | struct loader_extension_property *exts, |
| 233 | const VkExtensionProperties *target_ext, |
| 234 | bool must_be_hosted) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 235 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 236 | uint32_t i; |
| 237 | for (i = 0; i < count; i++) { |
| 238 | if (compare_vk_extension_properties(&exts[i].info, target_ext) && (!must_be_hosted || exts[i].hosted)) |
| 239 | return true; |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Search the given ext_list for an extension |
| 246 | * matching the given vk_ext_prop |
| 247 | */ |
| 248 | bool has_vk_extension_property( |
| 249 | const VkExtensionProperties *vk_ext_prop, |
| 250 | const struct loader_extension_list *ext_list) |
| 251 | { |
| 252 | for (uint32_t i = 0; i < ext_list->count; i++) { |
| 253 | if (compare_vk_extension_properties(&ext_list->list[i].info, vk_ext_prop)) |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Search the given ext_list for an extension |
| 261 | * matching the given vk_ext_prop |
| 262 | */ |
| 263 | static struct loader_extension_property *get_extension_property_from_vkext( |
| 264 | const VkExtensionProperties *vk_ext_prop, |
| 265 | const struct loader_extension_list *ext_list) |
| 266 | { |
| 267 | for (uint32_t i = 0; i < ext_list->count; i++) { |
| 268 | if (compare_vk_extension_properties(&ext_list->list[i].info, vk_ext_prop)) |
| 269 | return &ext_list->list[i]; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | static void get_global_extensions( |
| 275 | const PFN_vkGetGlobalExtensionInfo fp_get, |
| 276 | const char *lib_name, |
| 277 | const enum extension_origin origin, |
| 278 | struct loader_extension_list *ext_list) |
| 279 | { |
| 280 | uint32_t i, count; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 281 | size_t siz = sizeof(count); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 282 | struct loader_extension_property ext_props; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 283 | VkResult res; |
| 284 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 285 | res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count); |
| 286 | if (res != VK_SUCCESS) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 287 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Error getting global extension count from ICD"); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | siz = sizeof(VkExtensionProperties); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 291 | for (i = 0; i < count; i++) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 292 | memset(&ext_props, 1, sizeof(ext_props)); |
| 293 | res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &ext_props.info); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 294 | if (res == VK_SUCCESS) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 295 | ext_props.hosted = false; |
| 296 | ext_props.origin = origin; |
| 297 | ext_props.lib_name = lib_name; |
| 298 | loader_add_to_ext_list(ext_list, 1, &ext_props); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 299 | } |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 300 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 301 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 305 | static bool loader_init_ext_list(struct loader_extension_list *ext_info) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 306 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 307 | ext_info->capacity = 32 * sizeof(struct loader_extension_property); |
| 308 | ext_info->list = malloc(ext_info->capacity); |
| 309 | if (ext_info->list == NULL) { |
| 310 | return false; |
| 311 | } |
| 312 | memset(ext_info->list, 0, ext_info->capacity); |
| 313 | ext_info->count = 0; |
| 314 | return true; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 317 | static void loader_destroy_ext_list(struct loader_extension_list *ext_info) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 318 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 319 | free(ext_info->list); |
| 320 | ext_info->count = 0; |
| 321 | ext_info->capacity = 0; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 322 | } |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 323 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 324 | static void loader_add_vk_ext_to_ext_list( |
| 325 | struct loader_extension_list *ext_list, |
| 326 | uint32_t prop_list_count, |
| 327 | const VkExtensionProperties *props, |
| 328 | const struct loader_extension_list *search_list) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 329 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 330 | struct loader_extension_property *ext_prop; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 331 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 332 | for (uint32_t i = 0; i < prop_list_count; i++) { |
| 333 | // look for duplicates |
| 334 | if (has_vk_extension_property(&props[i], ext_list)) { |
| 335 | continue; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 336 | } |
| 337 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 338 | ext_prop = get_extension_property_from_vkext(&props[i], search_list); |
| 339 | if (!ext_prop) { |
| 340 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Unable to find extension %s", props[i].name); |
| 341 | continue; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 342 | } |
| 343 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 344 | loader_add_to_ext_list(ext_list, 1, ext_prop); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 348 | /* |
| 349 | * Append non-duplicate extension properties defined in prop_list |
| 350 | * to the given ext_info list |
| 351 | */ |
| 352 | void loader_add_to_ext_list( |
| 353 | struct loader_extension_list *ext_list, |
| 354 | uint32_t prop_list_count, |
| 355 | const struct loader_extension_property *props) |
| 356 | { |
| 357 | uint32_t i; |
| 358 | struct loader_extension_property *cur_ext; |
| 359 | |
| 360 | if (ext_list->list == NULL || ext_list->capacity == 0) { |
| 361 | loader_init_ext_list(ext_list); |
| 362 | } |
| 363 | |
| 364 | if (ext_list->list == NULL) |
| 365 | return; |
| 366 | |
| 367 | for (i = 0; i < prop_list_count; i++) { |
| 368 | cur_ext = (struct loader_extension_property *) &props[i]; |
| 369 | |
| 370 | // look for duplicates |
| 371 | if (has_vk_extension_property(&cur_ext->info, ext_list)) { |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | // add to list at end |
| 376 | // check for enough capacity |
| 377 | if (ext_list->count * sizeof(struct loader_extension_property) |
| 378 | >= ext_list->capacity) { |
| 379 | // double capacity |
| 380 | ext_list->capacity *= 2; |
| 381 | ext_list->list = realloc(ext_list->list, ext_list->capacity); |
| 382 | } |
| 383 | memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(struct loader_extension_property)); |
| 384 | ext_list->count++; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Search the search_list for any extension with |
| 390 | * a name that matches the given ext_name. |
| 391 | * Add all matching extensions to the found_list |
| 392 | * Do not add if found VkExtensionProperties is already |
| 393 | * on the found_list |
| 394 | */ |
| 395 | static void loader_search_ext_list_for_name( |
| 396 | const char *ext_name, |
| 397 | const struct loader_extension_list *search_list, |
| 398 | struct loader_extension_list *found_list) |
| 399 | { |
| 400 | for (uint32_t i = 0; i < search_list->count; i++) { |
| 401 | struct loader_extension_property *ext_prop = &search_list->list[i]; |
| 402 | if (0 == strcmp(ext_prop->info.name, ext_name)) { |
| 403 | /* Found an extension with the same name, add to found_list */ |
| 404 | loader_add_to_ext_list(found_list, 1, &search_list->list[i]); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | bool loader_is_extension_scanned(const VkExtensionProperties *ext_prop) |
Jon Ashburn | 42e4103 | 2015-04-14 09:15:32 -0600 | [diff] [blame] | 410 | { |
| 411 | uint32_t i; |
| 412 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 413 | for (i = 0; i < loader.global_extensions.count; i++) { |
| 414 | if (compare_vk_extension_properties(&loader.global_extensions.list[i].info, ext_prop)) |
Jon Ashburn | 42e4103 | 2015-04-14 09:15:32 -0600 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 420 | void loader_coalesce_extensions(void) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 421 | { |
| 422 | uint32_t i; |
| 423 | struct loader_scanned_icds *icd_list = loader.scanned_icd_list; |
| 424 | |
| 425 | // traverse scanned icd list adding non-duplicate extensions to the list |
| 426 | while (icd_list != NULL) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 427 | /* TODO: convert to use ext_list */ |
| 428 | loader_add_to_ext_list(&loader.global_extensions, |
| 429 | icd_list->global_extension_list.count, |
| 430 | icd_list->global_extension_list.list); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 431 | icd_list = icd_list->next; |
| 432 | }; |
| 433 | |
| 434 | //Traverse layers list adding non-duplicate extensions to the list |
| 435 | for (i = 0; i < loader.scanned_layer_count; i++) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 436 | loader_add_to_ext_list(&loader.global_extensions, |
| 437 | loader.scanned_layers[i].global_extension_list.count, |
| 438 | loader.scanned_layers[i].global_extension_list.list); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 439 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 440 | |
| 441 | // Traverse loader's extensions, adding non-duplicate extensions to the list |
| 442 | debug_report_add_instance_extensions(&loader.global_extensions); |
Jon Ashburn | a445e09 | 2015-05-26 13:57:35 -0600 | [diff] [blame] | 443 | wsi_lunarg_add_instance_extensions(&loader.global_extensions); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 444 | } |
| 445 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 446 | static void loader_icd_destroy( |
| 447 | struct loader_instance *ptr_inst, |
| 448 | struct loader_icd *icd) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 449 | { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 450 | loader_platform_close_library(icd->scanned_icds->handle); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 451 | ptr_inst->total_icd_count--; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 452 | free(icd); |
| 453 | } |
| 454 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 455 | 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] | 456 | { |
| 457 | struct loader_icd *icd; |
| 458 | |
| 459 | icd = malloc(sizeof(*icd)); |
| 460 | if (!icd) |
| 461 | return NULL; |
| 462 | |
Courtney Goeltzenleuchter | 6f92816 | 2014-10-28 10:29:27 -0600 | [diff] [blame] | 463 | memset(icd, 0, sizeof(*icd)); |
| 464 | |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 465 | icd->scanned_icds = scanned; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 466 | |
| 467 | return icd; |
| 468 | } |
| 469 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 470 | static struct loader_icd *loader_icd_add( |
| 471 | struct loader_instance *ptr_inst, |
| 472 | const struct loader_scanned_icds *scanned) |
Chia-I Wu | 894a117 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 473 | { |
| 474 | struct loader_icd *icd; |
| 475 | |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 476 | icd = loader_icd_create(scanned); |
Chia-I Wu | 894a117 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 477 | if (!icd) |
| 478 | return NULL; |
| 479 | |
Chia-I Wu | 894a117 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 480 | /* prepend to the list */ |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 481 | icd->next = ptr_inst->icds; |
| 482 | ptr_inst->icds = icd; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 483 | ptr_inst->total_icd_count++; |
Chia-I Wu | 894a117 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 484 | |
| 485 | return icd; |
| 486 | } |
| 487 | |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 488 | static void loader_scanned_icd_add(const char *filename) |
| 489 | { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 490 | loader_platform_dl_handle handle; |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 491 | void *fp_create_inst; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 492 | void *fp_get_global_ext_info; |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 493 | struct loader_scanned_icds *new_node; |
| 494 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 495 | // Used to call: dlopen(filename, RTLD_LAZY); |
| 496 | handle = loader_platform_open_library(filename); |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 497 | if (!handle) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 498 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_open_library_error(filename)); |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
| 502 | #define LOOKUP(func_ptr, func) do { \ |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 503 | func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \ |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 504 | if (!func_ptr) { \ |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 505 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_get_proc_address_error("vk" #func)); \ |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 506 | return; \ |
| 507 | } \ |
| 508 | } while (0) |
| 509 | |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 510 | LOOKUP(fp_create_inst, CreateInstance); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 511 | LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo); |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 512 | #undef LOOKUP |
| 513 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 514 | new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds) |
| 515 | + strlen(filename) + 1); |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 516 | if (!new_node) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 517 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add icd"); |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | |
| 521 | new_node->handle = handle; |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 522 | new_node->CreateInstance = fp_create_inst; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 523 | new_node->GetGlobalExtensionInfo = fp_get_global_ext_info; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 524 | loader_init_ext_list(&new_node->global_extension_list); |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 525 | new_node->next = loader.scanned_icd_list; |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 526 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 527 | new_node->lib_name = (char *) (new_node + 1); |
| 528 | if (!new_node->lib_name) { |
| 529 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add icd"); |
| 530 | return; |
| 531 | } |
| 532 | strcpy(new_node->lib_name, filename); |
| 533 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 534 | loader.scanned_icd_list = new_node; |
| 535 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 536 | get_global_extensions( |
| 537 | (PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info, |
| 538 | new_node->lib_name, |
| 539 | VK_EXTENSION_ORIGIN_ICD, |
| 540 | &new_node->global_extension_list); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 541 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 542 | |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 543 | static void loader_icd_init_entrys(struct loader_icd *icd, |
| 544 | struct loader_scanned_icds *scanned_icds) |
| 545 | { |
| 546 | /* initialize entrypoint function pointers */ |
| 547 | |
| 548 | #define LOOKUP(func) do { \ |
| 549 | icd->func = (PFN_vk ##func) loader_platform_get_proc_address(scanned_icds->handle, "vk" #func); \ |
| 550 | if (!icd->func) { \ |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 551 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_get_proc_address_error("vk" #func)); \ |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 552 | return; \ |
| 553 | } \ |
| 554 | } while (0) |
| 555 | |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 556 | /* could change this to use GetInstanceProcAddr in driver instead of dlsym */ |
| 557 | LOOKUP(GetDeviceProcAddr); |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 558 | LOOKUP(DestroyInstance); |
| 559 | LOOKUP(EnumeratePhysicalDevices); |
| 560 | LOOKUP(GetPhysicalDeviceInfo); |
| 561 | LOOKUP(CreateDevice); |
| 562 | LOOKUP(GetPhysicalDeviceExtensionInfo); |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 563 | LOOKUP(GetMultiDeviceCompatibility); |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 564 | LOOKUP(GetDisplayInfoWSI); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 565 | LOOKUP(DbgCreateMsgCallback); |
| 566 | LOOKUP(DbgDestroyMsgCallback); |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 567 | #undef LOOKUP |
| 568 | |
| 569 | return; |
| 570 | } |
| 571 | |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 572 | /** |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 573 | * Try to \c loader_icd_scan VK driver(s). |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 574 | * |
| 575 | * This function scans the default system path or path |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 576 | * specified by the \c LIBVK_DRIVERS_PATH environment variable in |
| 577 | * order to find loadable VK ICDs with the name of libVK_*. |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 578 | * |
| 579 | * \returns |
| 580 | * void; but side effect is to set loader_icd_scanned to true |
| 581 | */ |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 582 | void loader_icd_scan(void) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 583 | { |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 584 | const char *p, *next; |
| 585 | char *libPaths = NULL; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 586 | DIR *sysdir; |
| 587 | struct dirent *dent; |
| 588 | char icd_library[1024]; |
Jon Ashburn | 0f45b2a | 2014-10-03 16:31:35 -0600 | [diff] [blame] | 589 | char path[1024]; |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 590 | uint32_t len; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 591 | #if defined(WIN32) |
| 592 | bool must_free_libPaths; |
| 593 | libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV, |
| 594 | DRIVER_PATH_REGISTRY_VALUE); |
| 595 | if (libPaths != NULL) { |
| 596 | must_free_libPaths = true; |
| 597 | } else { |
| 598 | must_free_libPaths = false; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 599 | libPaths = DEFAULT_VK_DRIVERS_PATH; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 600 | } |
| 601 | #else // WIN32 |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 602 | if (geteuid() == getuid()) { |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 603 | /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */ |
| 604 | libPaths = getenv(DRIVER_PATH_ENV); |
| 605 | } |
| 606 | if (libPaths == NULL) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 607 | libPaths = DEFAULT_VK_DRIVERS_PATH; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 608 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 609 | #endif // WIN32 |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 610 | |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 611 | for (p = libPaths; *p; p = next) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 612 | next = strchr(p, PATH_SEPERATOR); |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 613 | if (next == NULL) { |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 614 | len = (uint32_t) strlen(p); |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 615 | next = p + len; |
| 616 | } |
| 617 | else { |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 618 | len = (uint32_t) (next - p); |
Jon Ashburn | 0f45b2a | 2014-10-03 16:31:35 -0600 | [diff] [blame] | 619 | sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p); |
| 620 | p = path; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 621 | next++; |
| 622 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 623 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 624 | // TODO/TBD: Do we want to do this on Windows, or just let Windows take |
| 625 | // care of its own search path (which it apparently has)? |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 626 | sysdir = opendir(p); |
| 627 | if (sysdir) { |
| 628 | dent = readdir(sysdir); |
| 629 | while (dent) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 630 | /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and |
| 631 | * ending with VK_LIBRARY_SUFFIX |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 632 | */ |
| 633 | if (!strncmp(dent->d_name, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 634 | VK_DRIVER_LIBRARY_PREFIX, |
| 635 | VK_DRIVER_LIBRARY_PREFIX_LEN)) { |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 636 | uint32_t nlen = (uint32_t) strlen(dent->d_name); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 637 | const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN; |
| 638 | if ((nlen > VK_LIBRARY_SUFFIX_LEN) && |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 639 | !strncmp(suf, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 640 | VK_LIBRARY_SUFFIX, |
| 641 | VK_LIBRARY_SUFFIX_LEN)) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 642 | snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name); |
| 643 | loader_scanned_icd_add(icd_library); |
| 644 | } |
| 645 | } |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 646 | |
| 647 | dent = readdir(sysdir); |
| 648 | } |
| 649 | closedir(sysdir); |
| 650 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 651 | } |
| 652 | |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 653 | #if defined(WIN32) |
| 654 | // Free any allocated memory: |
| 655 | if (must_free_libPaths) { |
| 656 | free(libPaths); |
| 657 | } |
| 658 | #endif // WIN32 |
| 659 | |
| 660 | // Note that we've scanned for ICDs: |
Jon Ashburn | 14275da | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 661 | loader.icds_scanned = true; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 662 | } |
| 663 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 664 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 665 | void layer_lib_scan(void) |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 666 | { |
| 667 | const char *p, *next; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 668 | char *libPaths = NULL; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 669 | DIR *curdir; |
| 670 | struct dirent *dent; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 671 | size_t len, i; |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 672 | char temp_str[1024]; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 673 | uint32_t count; |
| 674 | PFN_vkGetGlobalExtensionInfo fp_get_ext; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 675 | |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 676 | #if defined(WIN32) |
| 677 | bool must_free_libPaths; |
| 678 | libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV, |
| 679 | LAYERS_PATH_REGISTRY_VALUE); |
| 680 | if (libPaths != NULL) { |
| 681 | must_free_libPaths = true; |
| 682 | } else { |
| 683 | must_free_libPaths = false; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 684 | libPaths = DEFAULT_VK_LAYERS_PATH; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 685 | } |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 686 | #else // WIN32 |
| 687 | if (geteuid() == getuid()) { |
| 688 | /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */ |
Courtney Goeltzenleuchter | 4ee0171 | 2015-02-18 20:03:02 -0700 | [diff] [blame] | 689 | libPaths = getenv(LAYERS_PATH_ENV); |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 690 | } |
| 691 | if (libPaths == NULL) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 692 | libPaths = DEFAULT_VK_LAYERS_PATH; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 693 | } |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 694 | #endif // WIN32 |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 695 | |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 696 | if (libPaths == NULL) { |
| 697 | // Have no paths to search: |
Courtney Goeltzenleuchter | bce445a | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 698 | return; |
| 699 | } |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 700 | len = strlen(libPaths); |
Courtney Goeltzenleuchter | bce445a | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 701 | loader.layer_dirs = malloc(len+1); |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 702 | if (loader.layer_dirs == NULL) { |
| 703 | free(libPaths); |
Courtney Goeltzenleuchter | 688c74b | 2014-12-02 18:12:51 -0700 | [diff] [blame] | 704 | return; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 705 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 706 | // Alloc passed, so we know there is enough space to hold the string |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 707 | strcpy(loader.layer_dirs, libPaths); |
| 708 | #if defined(WIN32) |
| 709 | // Free any allocated memory: |
| 710 | if (must_free_libPaths) { |
| 711 | free(libPaths); |
| 712 | must_free_libPaths = false; |
| 713 | } |
| 714 | #endif // WIN32 |
Courtney Goeltzenleuchter | bce445a | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 715 | libPaths = loader.layer_dirs; |
| 716 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 717 | /* cleanup any previously scanned libraries */ |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 718 | for (i = 0; i < loader.scanned_layer_count; i++) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 719 | if (loader.scanned_layers[i].lib_name != NULL) |
| 720 | free(loader.scanned_layers[i].lib_name); |
| 721 | loader_destroy_ext_list(&loader.scanned_layers[i].global_extension_list); |
| 722 | loader.scanned_layers[i].lib_name = NULL; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 723 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 724 | loader.scanned_layer_count = 0; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 725 | count = 0; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 726 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 727 | for (p = libPaths; *p; p = next) { |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 728 | next = strchr(p, PATH_SEPERATOR); |
| 729 | if (next == NULL) { |
| 730 | len = (uint32_t) strlen(p); |
| 731 | next = p + len; |
| 732 | } |
| 733 | else { |
| 734 | len = (uint32_t) (next - p); |
| 735 | *(char *) next = '\0'; |
| 736 | next++; |
| 737 | } |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 738 | |
| 739 | curdir = opendir(p); |
| 740 | if (curdir) { |
| 741 | dent = readdir(curdir); |
| 742 | while (dent) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 743 | /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and |
| 744 | * ending with VK_LIBRARY_SUFFIX |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 745 | */ |
| 746 | if (!strncmp(dent->d_name, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 747 | VK_LAYER_LIBRARY_PREFIX, |
| 748 | VK_LAYER_LIBRARY_PREFIX_LEN)) { |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 749 | uint32_t nlen = (uint32_t) strlen(dent->d_name); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 750 | const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN; |
| 751 | if ((nlen > VK_LIBRARY_SUFFIX_LEN) && |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 752 | !strncmp(suf, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 753 | VK_LIBRARY_SUFFIX, |
| 754 | VK_LIBRARY_SUFFIX_LEN)) { |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 755 | loader_platform_dl_handle handle; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 756 | snprintf(temp_str, sizeof(temp_str), |
| 757 | "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 758 | // Used to call: dlopen(temp_str, RTLD_LAZY) |
| 759 | if ((handle = loader_platform_open_library(temp_str)) == NULL) { |
| 760 | dent = readdir(curdir); |
| 761 | continue; |
| 762 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 763 | /* TODO: Remove fixed count */ |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 764 | if (count == MAX_LAYER_LIBRARIES) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 765 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 766 | "%s ignored: max layer libraries exceed", |
| 767 | temp_str); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 768 | break; |
| 769 | } |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 770 | fp_get_ext = loader_platform_get_proc_address(handle, |
| 771 | "vkGetGlobalExtensionInfo"); |
| 772 | |
| 773 | if (!fp_get_ext) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 774 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 775 | "Couldn't dlsym vkGetGlobalExtensionInfo from library %s", |
| 776 | temp_str); |
| 777 | dent = readdir(curdir); |
| 778 | loader_platform_close_library(handle); |
| 779 | continue; |
| 780 | } |
| 781 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 782 | loader.scanned_layers[count].lib_name = |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 783 | malloc(strlen(temp_str) + 1); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 784 | if (loader.scanned_layers[count].lib_name == NULL) { |
| 785 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "%s ignored: out of memory", temp_str); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 786 | break; |
| 787 | } |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 788 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 789 | strcpy(loader.scanned_layers[count].lib_name, temp_str); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 790 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 791 | get_global_extensions( |
| 792 | fp_get_ext, |
| 793 | loader.scanned_layers[count].lib_name, |
| 794 | VK_EXTENSION_ORIGIN_LAYER, |
| 795 | &loader.scanned_layers[count].global_extension_list); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 796 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 797 | count++; |
| 798 | loader_platform_close_library(handle); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 799 | } |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | dent = readdir(curdir); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 803 | } // while (dir_entry) |
| 804 | if (count == MAX_LAYER_LIBRARIES) |
| 805 | break; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 806 | closedir(curdir); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 807 | } // if (curdir)) |
| 808 | } // for (libpaths) |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 809 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 810 | loader.scanned_layer_count = count; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 811 | loader.layers_scanned = true; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 812 | } |
| 813 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 814 | static void* VKAPI loader_gpa_instance_internal(VkInstance inst, const char * pName) |
| 815 | { |
| 816 | // inst is not wrapped |
| 817 | if (inst == VK_NULL_HANDLE) { |
| 818 | return NULL; |
| 819 | } |
| 820 | VkLayerInstanceDispatchTable* disp_table = * (VkLayerInstanceDispatchTable **) inst; |
| 821 | void *addr; |
| 822 | |
| 823 | if (disp_table == NULL) |
| 824 | return NULL; |
| 825 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 826 | // addr = debug_report_instance_gpa((struct loader_instance *) inst, pName); |
| 827 | // if (addr) { |
| 828 | // return addr; |
| 829 | // } |
| 830 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 831 | addr = loader_lookup_instance_dispatch_table(disp_table, pName); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 832 | if (addr) { |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 833 | return addr; |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 834 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 835 | |
| 836 | if (disp_table->GetInstanceProcAddr == NULL) { |
| 837 | return NULL; |
| 838 | } |
| 839 | return disp_table->GetInstanceProcAddr(inst, pName); |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 840 | } |
| 841 | |
Jon Ashburn | da7c20d | 2015-05-01 18:00:33 -0600 | [diff] [blame] | 842 | struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index) |
Jon Ashburn | b55278a | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 843 | { |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 844 | /* |
| 845 | * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else |
| 846 | * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so |
| 847 | * need to test it against the wrapped GPU's base object. |
| 848 | */ |
Jon Ashburn | dc67ef5 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 849 | for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) { |
| 850 | for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) { |
| 851 | for (uint32_t i = 0; i < icd->gpu_count; i++) |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 852 | if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) { |
Jon Ashburn | dc67ef5 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 853 | *gpu_index = i; |
| 854 | return icd; |
| 855 | } |
| 856 | } |
Jon Ashburn | b55278a | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 857 | } |
| 858 | return NULL; |
| 859 | } |
| 860 | |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 861 | static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index) |
Jon Ashburn | b55278a | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 862 | { |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 863 | if (icd->layer_count[gpu_index]) |
| 864 | return true; |
| 865 | else |
| 866 | return false; |
Jon Ashburn | b55278a | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 867 | } |
| 868 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 869 | static loader_platform_dl_handle loader_add_layer_lib( |
Jon Ashburn | 9a9de1f | 2015-05-27 13:19:22 -0600 | [diff] [blame^] | 870 | const char *chain_type, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 871 | struct loader_extension_property *ext_prop) |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 872 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 873 | struct loader_lib_info *new_layer_lib_list, *my_lib; |
| 874 | |
| 875 | /* Only loader layer libraries here */ |
| 876 | if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) { |
| 877 | return NULL; |
| 878 | } |
| 879 | |
| 880 | for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) { |
| 881 | if (strcmp(loader.loaded_layer_lib_list[i].lib_name, ext_prop->lib_name) == 0) { |
| 882 | /* Have already loaded this library, just increment ref count */ |
| 883 | loader.loaded_layer_lib_list[i].ref_count++; |
Jon Ashburn | f0615e2 | 2015-05-25 14:11:37 -0600 | [diff] [blame] | 884 | loader_log(VK_DBG_REPORT_INFO_BIT, 0, |
Jon Ashburn | 9a9de1f | 2015-05-27 13:19:22 -0600 | [diff] [blame^] | 885 | "Inserting %s layer %s from library %s", chain_type, |
Jon Ashburn | f0615e2 | 2015-05-25 14:11:37 -0600 | [diff] [blame] | 886 | ext_prop->info.name, ext_prop->lib_name); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 887 | return loader.loaded_layer_lib_list[i].lib_handle; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /* Haven't seen this library so load it */ |
| 892 | new_layer_lib_list = realloc(loader.loaded_layer_lib_list, |
| 893 | (loader.loaded_layer_lib_count + 1) * sizeof(struct loader_lib_info)); |
| 894 | if (!new_layer_lib_list) { |
| 895 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "loader: malloc failed"); |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | my_lib = &new_layer_lib_list[loader.loaded_layer_lib_count]; |
| 900 | |
| 901 | /* NOTE: We require that the extension property to be immutable */ |
| 902 | my_lib->lib_name = ext_prop->lib_name; |
| 903 | my_lib->ref_count = 0; |
| 904 | my_lib->lib_handle = NULL; |
| 905 | |
| 906 | if ((my_lib->lib_handle = loader_platform_open_library(my_lib->lib_name)) == NULL) { |
| 907 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, |
| 908 | loader_platform_open_library_error(my_lib->lib_name)); |
| 909 | return NULL; |
| 910 | } else { |
| 911 | loader_log(VK_DBG_REPORT_INFO_BIT, 0, |
Jon Ashburn | 9a9de1f | 2015-05-27 13:19:22 -0600 | [diff] [blame^] | 912 | "Inserting %s layer %s from library %s", chain_type, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 913 | ext_prop->info.name, ext_prop->lib_name); |
| 914 | } |
| 915 | loader.loaded_layer_lib_count++; |
| 916 | loader.loaded_layer_lib_list = new_layer_lib_list; |
| 917 | my_lib->ref_count++; |
| 918 | |
| 919 | return my_lib->lib_handle; |
| 920 | } |
| 921 | |
| 922 | static void loader_remove_layer_lib( |
| 923 | struct loader_instance *inst, |
| 924 | struct loader_extension_property *ext_prop) |
| 925 | { |
| 926 | uint32_t idx; |
| 927 | struct loader_lib_info *new_layer_lib_list, *my_lib; |
| 928 | |
| 929 | /* Only loader layer libraries here */ |
| 930 | if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) { |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 931 | return; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 932 | } |
Jon Ashburn | 815bddd | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 933 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 934 | for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) { |
| 935 | if (strcmp(loader.loaded_layer_lib_list[i].lib_name, ext_prop->lib_name) == 0) { |
| 936 | /* found matching library */ |
| 937 | idx = i; |
| 938 | my_lib = &loader.loaded_layer_lib_list[i]; |
| 939 | break; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 940 | } |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 941 | } |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 942 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 943 | my_lib->ref_count--; |
| 944 | inst->layer_count--; |
| 945 | if (my_lib->ref_count > 0) { |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 946 | return; |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 947 | } |
Jon Ashburn | 4d3b752 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 948 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 949 | /* Need to remove unused library from list */ |
| 950 | new_layer_lib_list = malloc((loader.loaded_layer_lib_count - 1) * sizeof(struct loader_lib_info)); |
| 951 | if (!new_layer_lib_list) { |
| 952 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "loader: malloc failed"); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | if (idx > 0) { |
| 957 | /* Copy records before idx */ |
| 958 | memcpy(new_layer_lib_list, &loader.loaded_layer_lib_list[0], |
| 959 | sizeof(struct loader_lib_info) * idx); |
| 960 | } |
| 961 | if (idx < (loader.loaded_layer_lib_count - 1)) { |
| 962 | /* Copy records after idx */ |
| 963 | memcpy(&new_layer_lib_list[idx], &loader.loaded_layer_lib_list[idx+1], |
| 964 | sizeof(struct loader_lib_info) * (loader.loaded_layer_lib_count - idx - 1)); |
| 965 | } |
| 966 | free(loader.loaded_layer_lib_list); |
| 967 | loader.loaded_layer_lib_count--; |
| 968 | loader.loaded_layer_lib_list = new_layer_lib_list; |
Jon Ashburn | ead95c5 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 971 | static void loader_add_layer_env( |
| 972 | struct loader_extension_list *ext_list, |
| 973 | const struct loader_extension_list *search_list) |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 974 | { |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 975 | char *layerEnv; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 976 | uint32_t len; |
Jon Ashburn | b4d0053 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 977 | char *p, *pOrig, *next, *name; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 978 | |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 979 | #if defined(WIN32) |
| 980 | layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV, |
| 981 | LAYER_NAMES_REGISTRY_VALUE); |
| 982 | #else // WIN32 |
| 983 | layerEnv = getenv(LAYER_NAMES_ENV); |
| 984 | #endif // WIN32 |
| 985 | if (layerEnv == NULL) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 986 | return; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 987 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 988 | p = malloc(strlen(layerEnv) + 1); |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 989 | if (p == NULL) { |
| 990 | #if defined(WIN32) |
| 991 | free(layerEnv); |
| 992 | #endif // WIN32 |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 993 | return; |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 994 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 995 | strcpy(p, layerEnv); |
Ian Elliott | 225188f | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 996 | #if defined(WIN32) |
| 997 | free(layerEnv); |
| 998 | #endif // WIN32 |
Jon Ashburn | b4d0053 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 999 | pOrig = p; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1000 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1001 | while (p && *p ) { |
Jon Ashburn | 4d3b752 | 2015-04-14 14:14:48 -0600 | [diff] [blame] | 1002 | //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1003 | next = strchr(p, PATH_SEPERATOR); |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1004 | if (next == NULL) { |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1005 | len = (uint32_t) strlen(p); |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1006 | next = p + len; |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1007 | } else { |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1008 | len = (uint32_t) (next - p); |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1009 | *(char *) next = '\0'; |
| 1010 | next++; |
| 1011 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1012 | name = basename(p); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1013 | loader_search_ext_list_for_name(name, search_list, ext_list); |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1014 | p = next; |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1015 | } |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1016 | |
Jon Ashburn | b4d0053 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 1017 | free(pOrig); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1018 | return; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1019 | } |
| 1020 | |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1021 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1022 | //TODO static void loader_deactivate_device_layer(device) |
| 1023 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1024 | static void loader_deactivate_instance_layers(struct loader_instance *instance) |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1025 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1026 | if (!instance->layer_count) { |
| 1027 | return; |
| 1028 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1029 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1030 | /* Create instance chain of enabled layers */ |
| 1031 | for (uint32_t i = 0; i < instance->enabled_instance_extensions.count; i++) { |
| 1032 | struct loader_extension_property *ext_prop = &instance->enabled_instance_extensions.list[i]; |
| 1033 | |
| 1034 | if (ext_prop->origin == VK_EXTENSION_ORIGIN_ICD) { |
| 1035 | continue; |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1036 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1037 | |
| 1038 | loader_remove_layer_lib(instance, ext_prop); |
| 1039 | |
| 1040 | instance->layer_count--; |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1041 | } |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1042 | |
| 1043 | free(instance->wrappedInstance); |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1044 | } |
| 1045 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1046 | void loader_enable_instance_layers(struct loader_instance *inst) |
| 1047 | { |
| 1048 | if (inst == NULL) |
| 1049 | return; |
| 1050 | |
| 1051 | /* Add any layers specified in the environment first */ |
| 1052 | loader_add_layer_env(&inst->enabled_instance_extensions, &loader.global_extensions); |
| 1053 | |
| 1054 | /* Add layers / extensions specified by the application */ |
| 1055 | loader_add_vk_ext_to_ext_list( |
| 1056 | &inst->enabled_instance_extensions, |
| 1057 | inst->app_extension_count, |
| 1058 | inst->app_extension_props, |
| 1059 | &loader.global_extensions); |
| 1060 | } |
| 1061 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1062 | uint32_t loader_activate_instance_layers(struct loader_instance *inst) |
| 1063 | { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1064 | uint32_t layer_idx; |
| 1065 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1066 | if (inst == NULL) |
| 1067 | return 0; |
| 1068 | |
| 1069 | // NOTE inst is unwrapped at this point in time |
| 1070 | VkObject baseObj = (VkObject) inst; |
| 1071 | VkObject nextObj = (VkObject) inst; |
| 1072 | VkBaseLayerObject *nextInstObj; |
| 1073 | PFN_vkGetInstanceProcAddr nextGPA = loader_gpa_instance_internal; |
| 1074 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1075 | /* |
| 1076 | * Figure out how many actual layers will need to be wrapped. |
| 1077 | */ |
| 1078 | inst->layer_count = 0; |
| 1079 | for (uint32_t i = 0; i < inst->enabled_instance_extensions.count; i++) { |
| 1080 | struct loader_extension_property *ext_prop = &inst->enabled_instance_extensions.list[i]; |
| 1081 | if (ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER) { |
| 1082 | inst->layer_count++; |
| 1083 | } |
| 1084 | } |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1085 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1086 | if (!inst->layer_count) { |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1089 | |
| 1090 | inst->wrappedInstance = malloc(sizeof(VkBaseLayerObject) |
| 1091 | * inst->layer_count); |
| 1092 | if (!inst->wrappedInstance) { |
| 1093 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc Instance objects for layer"); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | /* Create instance chain of enabled layers */ |
| 1098 | layer_idx = inst->layer_count - 1; |
| 1099 | for (int32_t i = inst->enabled_instance_extensions.count - 1; i >= 0; i--) { |
| 1100 | struct loader_extension_property *ext_prop = &inst->enabled_instance_extensions.list[i]; |
| 1101 | loader_platform_dl_handle lib_handle; |
| 1102 | |
| 1103 | /* |
| 1104 | * TODO: Need to figure out how to hook in extensions implemented |
| 1105 | * within the loader. |
| 1106 | * What GetProcAddr do we use? |
| 1107 | * How do we make the loader call first in the chain? It may not be first |
| 1108 | * in the list. Does it have to be first? |
| 1109 | * How does the chain for DbgCreateMsgCallback get made? |
| 1110 | * Do we need to add the function pointer to the VkLayerInstanceDispatchTable? |
| 1111 | * Seems like we will. |
| 1112 | * What happens with more than one loader implemented extension? |
| 1113 | * Issue: Process of building instance chain requires that we call GetInstanceProcAddr |
| 1114 | * on the various extension components. However, if we are asking for an extension |
| 1115 | * entry point we won't get it because we haven't enabled the extension yet. |
| 1116 | * Must not call GPA on extensions at this time. |
| 1117 | */ |
| 1118 | if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) { |
| 1119 | continue; |
| 1120 | } |
| 1121 | |
| 1122 | nextInstObj = (inst->wrappedInstance + layer_idx); |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1123 | nextInstObj->pGPA = nextGPA; |
| 1124 | nextInstObj->baseObject = baseObj; |
| 1125 | nextInstObj->nextObject = nextObj; |
| 1126 | nextObj = (VkObject) nextInstObj; |
| 1127 | |
| 1128 | char funcStr[256]; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1129 | snprintf(funcStr, 256, "%sGetInstanceProcAddr", ext_prop->info.name); |
Jon Ashburn | 9a9de1f | 2015-05-27 13:19:22 -0600 | [diff] [blame^] | 1130 | lib_handle = loader_add_layer_lib("instance", ext_prop); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1131 | if ((nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL) |
| 1132 | nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr"); |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1133 | if (!nextGPA) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1134 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to find vkGetInstanceProcAddr in layer %s", ext_prop->lib_name); |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1135 | continue; |
| 1136 | } |
| 1137 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1138 | layer_idx--; |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1139 | } |
| 1140 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1141 | loader_init_instance_core_dispatch_table(inst->disp, nextGPA, (VkInstance) nextObj); |
| 1142 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1143 | return inst->layer_count; |
| 1144 | } |
| 1145 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1146 | void loader_enable_device_layers(struct loader_icd *icd, uint32_t gpu_index) |
| 1147 | { |
| 1148 | if (icd == NULL) |
| 1149 | return; |
| 1150 | |
| 1151 | /* Add any layers specified in the environment first */ |
| 1152 | loader_add_layer_env(&icd->enabled_device_extensions[gpu_index], &loader.global_extensions); |
| 1153 | |
| 1154 | /* Add layers / extensions specified by the application */ |
| 1155 | loader_add_vk_ext_to_ext_list( |
| 1156 | &icd->enabled_device_extensions[gpu_index], |
| 1157 | icd->app_extension_count[gpu_index], |
| 1158 | icd->app_extension_props[gpu_index], |
| 1159 | &loader.global_extensions); |
| 1160 | } |
| 1161 | |
| 1162 | extern uint32_t loader_activate_device_layers( |
| 1163 | VkDevice device, |
| 1164 | struct loader_icd *icd, |
| 1165 | uint32_t gpu_index, |
| 1166 | uint32_t ext_count, |
| 1167 | const VkExtensionProperties *ext_props) |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1168 | { |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1169 | uint32_t count; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1170 | uint32_t layer_idx; |
| 1171 | |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1172 | if (!icd) |
| 1173 | return 0; |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1174 | assert(gpu_index < MAX_GPUS_FOR_LAYER); |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1175 | |
| 1176 | /* activate any layer libraries */ |
| 1177 | if (!loader_layers_activated(icd, gpu_index)) { |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1178 | VkObject nextObj = (VkObject) device; |
| 1179 | VkObject baseObj = nextObj; |
| 1180 | VkBaseLayerObject *nextGpuObj; |
Jon Ashburn | eedbb5e | 2015-05-22 09:19:49 -0600 | [diff] [blame] | 1181 | PFN_vkGetDeviceProcAddr nextGPA = icd->GetDeviceProcAddr; |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1182 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1183 | count = 0; |
| 1184 | for (uint32_t i = 0; i < icd->enabled_device_extensions[gpu_index].count; i++) { |
| 1185 | struct loader_extension_property *ext_prop = &icd->enabled_device_extensions[gpu_index].list[i]; |
| 1186 | if (ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER) { |
| 1187 | count++; |
| 1188 | } |
| 1189 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1190 | if (!count) |
| 1191 | return 0; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1192 | |
| 1193 | icd->layer_count[gpu_index] = count; |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1194 | |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 1195 | icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]); |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1196 | if (! icd->wrappedGpus[gpu_index]) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1197 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc Gpu objects for layer"); |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1198 | return 0; |
| 1199 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1200 | layer_idx = count - 1; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1201 | for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1202 | struct loader_extension_property *ext_prop = &icd->enabled_device_extensions[gpu_index].list[i]; |
| 1203 | loader_platform_dl_handle lib_handle; |
| 1204 | |
| 1205 | if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) { |
| 1206 | continue; |
| 1207 | } |
| 1208 | |
Jon Ashburn | b4d0053 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 1209 | nextGpuObj = (icd->wrappedGpus[gpu_index] + i); |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1210 | nextGpuObj->pGPA = nextGPA; |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1211 | nextGpuObj->baseObject = baseObj; |
| 1212 | nextGpuObj->nextObject = nextObj; |
| 1213 | nextObj = (VkObject) nextGpuObj; |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1214 | |
Jon Ashburn | 8d8dad0 | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 1215 | char funcStr[256]; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1216 | snprintf(funcStr, 256, "%sGetDeviceProcAddr", ext_prop->info.name); |
Jon Ashburn | 9a9de1f | 2015-05-27 13:19:22 -0600 | [diff] [blame^] | 1217 | lib_handle = loader_add_layer_lib("device", ext_prop); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1218 | if ((nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL) |
| 1219 | nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr"); |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1220 | if (!nextGPA) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1221 | loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to find vkGetDeviceProcAddr in layer %s", ext_prop->info.name); |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1222 | continue; |
| 1223 | } |
| 1224 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1225 | layer_idx--; |
| 1226 | } |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1227 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1228 | loader_init_device_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) nextObj); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1229 | } else { |
| 1230 | // TODO: Check that active layers match requested? |
Jon Ashburn | 183dfd0 | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 1231 | } |
| 1232 | return icd->layer_count[gpu_index]; |
| 1233 | } |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1234 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1235 | VkResult loader_CreateInstance( |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1236 | const VkInstanceCreateInfo* pCreateInfo, |
| 1237 | VkInstance* pInstance) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1238 | { |
Jon Ashburn | a179dcf | 2015-05-21 17:42:17 -0600 | [diff] [blame] | 1239 | struct loader_instance *ptr_instance = *(struct loader_instance **) pInstance; |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1240 | struct loader_scanned_icds *scanned_icds; |
| 1241 | struct loader_icd *icd; |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1242 | VkResult res; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1243 | |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1244 | scanned_icds = loader.scanned_icd_list; |
| 1245 | while (scanned_icds) { |
| 1246 | icd = loader_icd_add(ptr_instance, scanned_icds); |
| 1247 | if (icd) { |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1248 | res = scanned_icds->CreateInstance(pCreateInfo, |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1249 | &(icd->instance)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1250 | if (res != VK_SUCCESS) |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1251 | { |
| 1252 | ptr_instance->icds = ptr_instance->icds->next; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1253 | loader_icd_destroy(ptr_instance, icd); |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1254 | icd->instance = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1255 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1256 | "ICD ignored: failed to CreateInstance on device"); |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1257 | } else |
| 1258 | { |
| 1259 | loader_icd_init_entrys(icd, scanned_icds); |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | scanned_icds = scanned_icds->next; |
| 1263 | } |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1264 | |
Ian Elliott | 617fdec | 2015-02-05 15:19:15 -0700 | [diff] [blame] | 1265 | if (ptr_instance->icds == NULL) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1266 | return VK_ERROR_INCOMPATIBLE_DRIVER; |
Ian Elliott | 617fdec | 2015-02-05 15:19:15 -0700 | [diff] [blame] | 1267 | } |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1268 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1269 | return res; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1272 | VkResult loader_DestroyInstance( |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1273 | VkInstance instance) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1274 | { |
| 1275 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1276 | struct loader_icd *icds = ptr_instance->icds; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1277 | VkResult res; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1278 | |
| 1279 | // Remove this instance from the list of instances: |
| 1280 | struct loader_instance *prev = NULL; |
| 1281 | struct loader_instance *next = loader.instances; |
| 1282 | while (next != NULL) { |
| 1283 | if (next == ptr_instance) { |
| 1284 | // Remove this instance from the list: |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1285 | free(ptr_instance->app_extension_props); |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1286 | if (prev) |
| 1287 | prev->next = next->next; |
Jon Ashburn | 2cabd25 | 2015-02-03 09:26:59 -0700 | [diff] [blame] | 1288 | else |
| 1289 | loader.instances = next->next; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1290 | break; |
| 1291 | } |
| 1292 | prev = next; |
| 1293 | next = next->next; |
| 1294 | } |
| 1295 | if (next == NULL) { |
| 1296 | // This must be an invalid instance handle or empty list |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1297 | return VK_ERROR_INVALID_HANDLE; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1300 | loader_deactivate_instance_layers(ptr_instance); |
| 1301 | loader_destroy_ext_list(&ptr_instance->enabled_instance_extensions); |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1302 | |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1303 | while (icds) { |
| 1304 | if (icds->instance) { |
| 1305 | res = icds->DestroyInstance(icds->instance); |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1306 | if (res != VK_SUCCESS) |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1307 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1308 | "ICD ignored: failed to DestroyInstance on device"); |
| 1309 | } |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1310 | icds->instance = VK_NULL_HANDLE; |
| 1311 | icds = icds->next; |
Jon Ashburn | 3336df8 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1314 | free(ptr_instance); |
| 1315 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1316 | return VK_SUCCESS; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1319 | VkResult loader_EnumeratePhysicalDevices( |
Courtney Goeltzenleuchter | 9f530cb | 2015-04-20 12:48:54 -0600 | [diff] [blame] | 1320 | VkInstance instance, |
| 1321 | uint32_t* pPhysicalDeviceCount, |
| 1322 | VkPhysicalDevice* pPhysicalDevices) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1323 | { |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1324 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
| 1325 | struct loader_icd *icd; |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1326 | uint32_t n, count = 0; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1327 | VkResult res = VK_ERROR_UNKNOWN; |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1328 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1329 | //in spirit of VK don't error check on the instance parameter |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1330 | icd = ptr_instance->icds; |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1331 | if (pPhysicalDevices == NULL) { |
| 1332 | while (icd) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1333 | res = icd->EnumeratePhysicalDevices(icd->instance, &n, NULL); |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1334 | if (res != VK_SUCCESS) |
| 1335 | return res; |
| 1336 | icd->gpu_count = n; |
| 1337 | count += n; |
| 1338 | icd = icd->next; |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1341 | ptr_instance->total_gpu_count = count; |
Courtney Goeltzenleuchter | ba7133b | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 1342 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1343 | } else |
| 1344 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1345 | VkPhysicalDevice* gpus; |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1346 | if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count) |
| 1347 | return VK_ERROR_INVALID_VALUE; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1348 | gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount); |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1349 | if (!gpus) |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1350 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1351 | while (icd) { |
| 1352 | VkBaseLayerObject * wrapped_gpus; |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 1353 | PFN_vkGetDeviceProcAddr get_proc_addr = icd->GetDeviceProcAddr; |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1354 | |
Courtney Goeltzenleuchter | 9f530cb | 2015-04-20 12:48:54 -0600 | [diff] [blame] | 1355 | n = *pPhysicalDeviceCount; |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1356 | res = icd->EnumeratePhysicalDevices( |
| 1357 | icd->instance, |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1358 | &n, |
| 1359 | gpus); |
| 1360 | if (res == VK_SUCCESS && n) { |
| 1361 | wrapped_gpus = (VkBaseLayerObject*) malloc(n * |
| 1362 | sizeof(VkBaseLayerObject)); |
| 1363 | icd->gpus = wrapped_gpus; |
| 1364 | icd->gpu_count = n; |
| 1365 | icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n * |
| 1366 | sizeof(VkLayerDispatchTable)); |
| 1367 | for (unsigned int i = 0; i < n; i++) { |
| 1368 | (wrapped_gpus + i)->baseObject = gpus[i]; |
| 1369 | (wrapped_gpus + i)->pGPA = get_proc_addr; |
| 1370 | (wrapped_gpus + i)->nextObject = gpus[i]; |
| 1371 | memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices)); |
Jon Ashburn | 9a9bb64 | 2015-05-04 16:27:53 -0600 | [diff] [blame] | 1372 | loader_init_device_dispatch_table(icd->loader_dispatch + i, |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1373 | get_proc_addr, gpus[i]); |
| 1374 | |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1375 | loader_init_dispatch(gpus[i], ptr_instance->disp); |
Courtney Goeltzenleuchter | ba7133b | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1378 | count += n; |
| 1379 | |
| 1380 | if (count >= *pPhysicalDeviceCount) { |
| 1381 | break; |
| 1382 | } |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1385 | icd = icd->next; |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1386 | } |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1389 | *pPhysicalDeviceCount = count; |
Jon Ashburn | b048a9b | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1390 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1391 | return (count > 0) ? VK_SUCCESS : res; |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Jon Ashburn | 0dd356d | 2015-05-14 12:43:38 -0600 | [diff] [blame] | 1394 | VkResult loader_GetPhysicalDeviceInfo( |
| 1395 | VkPhysicalDevice gpu, |
| 1396 | VkPhysicalDeviceInfoType infoType, |
| 1397 | size_t* pDataSize, |
| 1398 | void* pData) |
| 1399 | { |
| 1400 | uint32_t gpu_index; |
| 1401 | struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index); |
| 1402 | VkResult res = VK_ERROR_INITIALIZATION_FAILED; |
| 1403 | |
| 1404 | if (icd->GetPhysicalDeviceInfo) |
| 1405 | res = icd->GetPhysicalDeviceInfo(gpu, infoType, pDataSize, pData); |
| 1406 | |
| 1407 | return res; |
| 1408 | } |
| 1409 | |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1410 | VkResult loader_CreateDevice( |
| 1411 | VkPhysicalDevice gpu, |
| 1412 | const VkDeviceCreateInfo* pCreateInfo, |
| 1413 | VkDevice* pDevice) |
| 1414 | { |
| 1415 | uint32_t gpu_index; |
| 1416 | struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index); |
| 1417 | VkResult res = VK_ERROR_INITIALIZATION_FAILED; |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1418 | |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1419 | if (icd->CreateDevice) { |
| 1420 | res = icd->CreateDevice(gpu, pCreateInfo, pDevice); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1421 | if (res != VK_SUCCESS) { |
| 1422 | return res; |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1423 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1424 | |
| 1425 | VkLayerDispatchTable *dev_disp = icd->loader_dispatch + gpu_index; |
| 1426 | loader_init_dispatch(*pDevice, dev_disp); |
| 1427 | |
| 1428 | icd->app_extension_count[gpu_index] = pCreateInfo->extensionCount; |
| 1429 | icd->app_extension_props[gpu_index] = (VkExtensionProperties *) malloc(sizeof(VkExtensionProperties) * pCreateInfo->extensionCount); |
| 1430 | if (icd->app_extension_props[gpu_index] == NULL && (icd->app_extension_count[gpu_index] > 0)) { |
| 1431 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1432 | } |
| 1433 | |
| 1434 | /* Make local copy of extension list */ |
| 1435 | if (icd->app_extension_count[gpu_index] > 0 && icd->app_extension_props[gpu_index] != NULL) { |
| 1436 | memcpy(icd->app_extension_props[gpu_index], pCreateInfo->pEnabledExtensions, sizeof(VkExtensionProperties) * pCreateInfo->extensionCount); |
| 1437 | } |
| 1438 | |
| 1439 | // TODO: Add dependency check here. |
| 1440 | |
| 1441 | loader_enable_device_layers(icd, gpu_index); |
| 1442 | |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1443 | //TODO fix this extension parameters once support GetDeviceExtensionInfo() |
| 1444 | // don't know which instance we are on with this call |
| 1445 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1446 | loader_activate_device_layers(*pDevice, icd, gpu_index, |
| 1447 | icd->app_extension_count[gpu_index], |
| 1448 | icd->app_extension_props[gpu_index]); |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | return res; |
| 1452 | } |
| 1453 | |
Jon Ashburn | 53c1677 | 2015-05-06 10:15:07 -0600 | [diff] [blame] | 1454 | LOADER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char * pName) |
| 1455 | { |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1456 | if (instance == VK_NULL_HANDLE) |
| 1457 | return NULL; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1458 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1459 | void *addr; |
| 1460 | /* get entrypoint addresses that are global (in the loader)*/ |
| 1461 | addr = globalGetProcAddr(pName); |
| 1462 | if (addr) |
| 1463 | return addr; |
Jon Ashburn | 53c1677 | 2015-05-06 10:15:07 -0600 | [diff] [blame] | 1464 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1465 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
| 1466 | |
| 1467 | addr = debug_report_instance_gpa(ptr_instance, pName); |
| 1468 | if (addr) { |
| 1469 | return addr; |
| 1470 | } |
| 1471 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1472 | /* return any extension global entrypoints */ |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1473 | addr = wsi_lunarg_GetInstanceProcAddr(instance, pName); |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1474 | if (addr) |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1475 | return (ptr_instance->wsi_lunarg_enabled) ? addr : NULL; |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1476 | |
| 1477 | /* return the instance dispatch table entrypoint for extensions */ |
| 1478 | const VkLayerInstanceDispatchTable *disp_table = * (VkLayerInstanceDispatchTable **) instance; |
| 1479 | if (disp_table == NULL) |
| 1480 | return NULL; |
| 1481 | |
| 1482 | addr = loader_lookup_instance_dispatch_table(disp_table, pName); |
| 1483 | if (addr) |
| 1484 | return addr; |
Jon Ashburn | 53c1677 | 2015-05-06 10:15:07 -0600 | [diff] [blame] | 1485 | |
| 1486 | return NULL; |
| 1487 | } |
| 1488 | |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 1489 | LOADER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char * pName) |
Jon Ashburn | 349508d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1490 | { |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 1491 | if (device == VK_NULL_HANDLE) { |
| 1492 | return NULL; |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1493 | } |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1494 | |
Chia-I Wu | 38e5a2c | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 1495 | void *addr; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1496 | |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1497 | /* for entrypoints that loader must handle (ie non-dispatchable or create object) |
| 1498 | make sure the loader entrypoint is returned */ |
| 1499 | addr = loader_non_passthrough_gpa(pName); |
Ian Elliott | fdf00b6 | 2015-04-15 12:53:19 -0600 | [diff] [blame] | 1500 | if (addr) { |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1501 | return addr; |
Ian Elliott | fdf00b6 | 2015-04-15 12:53:19 -0600 | [diff] [blame] | 1502 | } |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1503 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1504 | /* return any extension device entrypoints the loader knows about */ |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1505 | addr = wsi_lunarg_GetDeviceProcAddr(device, pName); |
| 1506 | /* TODO: Where does device wsi_enabled go? */ |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1507 | if (addr) |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1508 | return addr; |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1509 | |
Jon Ashburn | e18431b | 2015-04-13 18:10:06 -0600 | [diff] [blame] | 1510 | /* return the dispatch table entrypoint for the fastest case */ |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 1511 | const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) device; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1512 | if (disp_table == NULL) |
| 1513 | return NULL; |
| 1514 | |
Jon Ashburn | fce93d9 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 1515 | addr = loader_lookup_device_dispatch_table(disp_table, pName); |
Chia-I Wu | 38e5a2c | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 1516 | if (addr) |
| 1517 | return addr; |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1518 | else { |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 1519 | if (disp_table->GetDeviceProcAddr == NULL) |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1520 | return NULL; |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 1521 | return disp_table->GetDeviceProcAddr(device, pName); |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1525 | //TODO make sure createInstance enables extensions that are valid (loader does) |
| 1526 | //TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do) |
| 1527 | |
| 1528 | //TODO how is layer extension going to be enabled? |
| 1529 | //Need to call createInstance on the layer or something |
| 1530 | |
Jon Ashburn | 9de9560 | 2015-05-18 15:28:32 -0600 | [diff] [blame] | 1531 | LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1532 | VkExtensionInfoType infoType, |
| 1533 | uint32_t extensionIndex, |
| 1534 | size_t* pDataSize, |
| 1535 | void* pData) |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1536 | { |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1537 | uint32_t *count; |
| 1538 | /* Scan/discover all ICD libraries in a single-threaded manner */ |
| 1539 | loader_platform_thread_once(&once_icd, loader_icd_scan); |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1540 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1541 | /* get layer libraries in a single-threaded manner */ |
| 1542 | loader_platform_thread_once(&once_layer, layer_lib_scan); |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1543 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1544 | /* merge any duplicate extensions */ |
| 1545 | loader_platform_thread_once(&once_exts, loader_coalesce_extensions); |
| 1546 | |
| 1547 | |
| 1548 | if (pDataSize == NULL) |
| 1549 | return VK_ERROR_INVALID_POINTER; |
| 1550 | |
| 1551 | switch (infoType) { |
| 1552 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 1553 | *pDataSize = sizeof(uint32_t); |
| 1554 | if (pData == NULL) |
| 1555 | return VK_SUCCESS; |
| 1556 | count = (uint32_t *) pData; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1557 | *count = loader.global_extensions.count; |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1558 | break; |
| 1559 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 1560 | *pDataSize = sizeof(VkExtensionProperties); |
| 1561 | if (pData == NULL) |
| 1562 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1563 | if (extensionIndex >= loader.global_extensions.count) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1564 | return VK_ERROR_INVALID_VALUE; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1565 | memcpy((VkExtensionProperties *) pData, |
| 1566 | &loader.global_extensions.list[extensionIndex], |
| 1567 | sizeof(VkExtensionProperties)); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1568 | break; |
| 1569 | default: |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1570 | loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Invalid infoType in vkGetGlobalExtensionInfo"); |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame] | 1571 | return VK_ERROR_INVALID_VALUE; |
| 1572 | }; |
| 1573 | |
| 1574 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1575 | } |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1576 | VkResult loader_GetPhysicalDeviceExtensionInfo( |
| 1577 | VkPhysicalDevice gpu, |
| 1578 | VkExtensionInfoType infoType, |
| 1579 | uint32_t extensionIndex, |
| 1580 | size_t* pDataSize, |
| 1581 | void* pData) |
| 1582 | { |
| 1583 | uint32_t gpu_index; |
| 1584 | struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index); |
| 1585 | VkResult res = VK_ERROR_INITIALIZATION_FAILED; |
Courtney Goeltzenleuchter | 0199e95 | 2015-02-27 15:19:33 -0700 | [diff] [blame] | 1586 | |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1587 | if (icd->GetPhysicalDeviceExtensionInfo) |
| 1588 | res = icd->GetPhysicalDeviceExtensionInfo(gpu, infoType, extensionIndex, |
| 1589 | pDataSize, pData); |
| 1590 | |
| 1591 | return res; |
| 1592 | } |
| 1593 | |
Jon Ashburn | 2666e2f | 2015-05-15 15:09:35 -0600 | [diff] [blame] | 1594 | VkResult loader_GetMultiDeviceCompatibility( |
| 1595 | VkPhysicalDevice gpu0, |
| 1596 | VkPhysicalDevice gpu1, |
| 1597 | VkPhysicalDeviceCompatibilityInfo* pInfo) |
| 1598 | { |
| 1599 | uint32_t gpu_index; |
| 1600 | struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu0, &gpu_index); |
| 1601 | VkResult res = VK_ERROR_INITIALIZATION_FAILED; |
| 1602 | |
| 1603 | if (icd->GetMultiDeviceCompatibility) |
| 1604 | res = icd->GetMultiDeviceCompatibility(gpu0, gpu1, pInfo); |
| 1605 | |
| 1606 | return res; |
| 1607 | } |