blob: 56df3524533225808a3004dbb2debd20729420d1 [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08003 *
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 Wu701f3f62014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn01e2d662014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu701f3f62014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Ian Elliott5aa4ea22015-03-31 15:32:41 -060028 * Ian Elliott <ian@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080029 */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060030#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080031#include <stdio.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdbool.h>
35#include <string.h>
36
Chia-I Wu13a61a52014-08-04 11:18:20 +080037#include <sys/types.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070038#if defined(WIN32)
39#include "dirent_on_windows.h"
40#else // WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080041#include <dirent.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070042#endif // WIN32
43#include "loader_platform.h"
Chia-I Wu19300602014-08-04 08:03:57 +080044#include "loader.h"
Jon Ashburn07daee72015-05-21 18:13:33 -060045#include "wsi_lunarg.h"
Jon Ashburn27cd5842015-05-12 17:26:48 -060046#include "gpa_helper.h"
47#include "table_ops.h"
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060048#include "debug_report.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060049#include "vkIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070050// The following is #included again to catch certain OS-specific functions
51// being used:
52#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080053
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060054void 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
Courtney Goeltzenleuchtered488302015-06-01 14:09:34 -060059static loader_platform_dl_handle loader_add_layer_lib(
60 const char *chain_type,
61 struct loader_extension_property *ext_prop);
62
63static void loader_remove_layer_lib(
64 struct loader_instance *inst,
65 struct loader_extension_property *ext_prop);
66
Jon Ashburn27cd5842015-05-12 17:26:48 -060067struct loader_struct loader = {0};
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080068
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -060069enum loader_debug {
70 LOADER_INFO_BIT = VK_BIT(0),
71 LOADER_WARN_BIT = VK_BIT(1),
72 LOADER_PERF_BIT = VK_BIT(2),
73 LOADER_ERROR_BIT = VK_BIT(3),
74 LOADER_DEBUG_BIT = VK_BIT(4),
75};
76
77uint32_t g_loader_debug = 0;
78uint32_t g_loader_log_msgs = 0;
79
Jon Ashburn6301a0f2015-05-29 13:15:39 -060080//thread safety lock for accessing global data structures such as "loader"
81// all entrypoints on the instance chain need to be locked except GPA
82// additionally DestroyDevice needs to be locked
83loader_platform_thread_mutex loader_lock;
84
85const VkLayerInstanceDispatchTable instance_disp = {
Jon Ashburn27cd5842015-05-12 17:26:48 -060086 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn27cd5842015-05-12 17:26:48 -060087 .CreateInstance = loader_CreateInstance,
88 .DestroyInstance = loader_DestroyInstance,
89 .EnumeratePhysicalDevices = loader_EnumeratePhysicalDevices,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060090 .GetPhysicalDeviceInfo = loader_GetPhysicalDeviceInfo,
91 .CreateDevice = loader_CreateDevice,
Jon Ashburn6301a0f2015-05-29 13:15:39 -060092 .GetPhysicalDeviceExtensionInfo = loader_GetPhysicalDeviceExtensionInfo,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060093 .GetMultiDeviceCompatibility = loader_GetMultiDeviceCompatibility,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -060094 .GetDisplayInfoWSI = loader_GetDisplayInfoWSI,
95 .DbgCreateMsgCallback = loader_DbgCreateMsgCallback,
96 .DbgDestroyMsgCallback = loader_DbgDestroyMsgCallback,
Jon Ashburn27cd5842015-05-12 17:26:48 -060097};
98
99LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
100LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
101LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700102
Ian Elliott4470a302015-02-17 10:33:47 -0700103#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600104char *loader_get_registry_string(const HKEY hive,
105 const LPCTSTR sub_key,
106 const char *value)
107{
108 DWORD access_flags = KEY_QUERY_VALUE;
109 DWORD value_type;
110 HKEY key;
Ian Elliottf851ddf2015-04-28 15:57:32 -0600111 VkResult rtn_value;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600112 char *rtn_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600113 DWORD rtn_len = 0;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600114 size_t allocated_len = 0;
115
116 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
117 if (rtn_value != ERROR_SUCCESS) {
118 // We didn't find the key. Try the 32-bit hive (where we've seen the
119 // key end up on some people's systems):
120 access_flags |= KEY_WOW64_32KEY;
121 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
122 if (rtn_value != ERROR_SUCCESS) {
123 // We still couldn't find the key, so give up:
124 return NULL;
125 }
126 }
127
128 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600129 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600130 if (rtn_value == ERROR_SUCCESS) {
131 // If we get to here, we found the key, and need to allocate memory
132 // large enough for rtn_str, and query again:
133 allocated_len = rtn_len + 4;
134 rtn_str = malloc(allocated_len);
135 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600136 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600137 if (rtn_value == ERROR_SUCCESS) {
138 // We added 4 extra bytes to rtn_str, so that we can ensure that
139 // the string is NULL-terminated (albeit, in a brute-force manner):
140 rtn_str[allocated_len-1] = '\0';
141 } else {
142 // This should never occur, but in case it does, clean up:
143 free(rtn_str);
144 rtn_str = NULL;
145 }
146 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
147
148 // Close the registry key that was opened:
149 RegCloseKey(key);
150
151 return rtn_str;
152}
153
154
Ian Elliott4470a302015-02-17 10:33:47 -0700155// For ICD developers, look in the registry, and look for an environment
156// variable for a path(s) where to find the ICD(s):
157static char *loader_get_registry_and_env(const char *env_var,
158 const char *registry_value)
159{
160 char *env_str = getenv(env_var);
161 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600162 char *registry_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600163 size_t registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700164 char *rtn_str = NULL;
165 size_t rtn_len;
166
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600167 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott06ebd752015-04-09 18:07:15 -0600168 "Software\\Vulkan",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600169 registry_value);
Ian Elliottf851ddf2015-04-28 15:57:32 -0600170 registry_len = (registry_str) ? (DWORD) strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700171
172 rtn_len = env_len + registry_len + 1;
173 if (rtn_len <= 2) {
174 // We found neither the desired registry value, nor the environment
175 // variable; return NULL:
176 return NULL;
177 } else {
178 // We found something, and so we need to allocate memory for the string
179 // to return:
180 rtn_str = malloc(rtn_len);
181 }
182
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600183 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700184 // We didn't find the desired registry value, and so we must have found
185 // only the environment variable:
186 _snprintf(rtn_str, rtn_len, "%s", env_str);
187 } else if (env_str != NULL) {
188 // We found both the desired registry value and the environment
189 // variable, so concatenate them both:
190 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
191 } else {
192 // We must have only found the desired registry value:
193 _snprintf(rtn_str, rtn_len, "%s", registry_str);
194 }
195
Ian Elliott2de26bc2015-04-03 13:13:01 -0600196 if (registry_str) {
197 free(registry_str);
198 }
Ian Elliott4470a302015-02-17 10:33:47 -0700199
200 return(rtn_str);
201}
202#endif // WIN32
203
204
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600205static void loader_log(VkFlags msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800206 const char *format, ...)
207{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800208 char msg[256];
209 va_list ap;
210 int ret;
211
Courtney Goeltzenleuchtera585ecb2015-06-09 09:42:23 -0600212 if (!(msg_type & g_loader_log_msgs)) {
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600213 return;
214 }
215
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216 va_start(ap, format);
217 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700218 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800219 msg[sizeof(msg) - 1] = '\0';
220 }
221 va_end(ap);
222
Jon Ashburnae053e92015-04-24 14:10:50 -0700223#if defined(WIN32)
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600224 OutputDebugString(msg);
Jon Ashburn1de39402015-05-05 16:20:46 -0600225#endif
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600226 fputs(msg, stderr);
227 fputc('\n', stderr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800228}
229
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600230bool compare_vk_extension_properties(const VkExtensionProperties *op1, const VkExtensionProperties *op2)
231{
232 return memcmp(op1, op2, sizeof(VkExtensionProperties)) == 0 ? true : false;
233}
234
235/*
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600236 * Search the given ext_list for an extension
237 * matching the given vk_ext_prop
238 */
239bool has_vk_extension_property(
240 const VkExtensionProperties *vk_ext_prop,
241 const struct loader_extension_list *ext_list)
242{
243 for (uint32_t i = 0; i < ext_list->count; i++) {
244 if (compare_vk_extension_properties(&ext_list->list[i].info, vk_ext_prop))
245 return true;
246 }
247 return false;
248}
249
250/*
251 * Search the given ext_list for an extension
252 * matching the given vk_ext_prop
253 */
254static struct loader_extension_property *get_extension_property_from_vkext(
255 const VkExtensionProperties *vk_ext_prop,
256 const struct loader_extension_list *ext_list)
257{
258 for (uint32_t i = 0; i < ext_list->count; i++) {
259 if (compare_vk_extension_properties(&ext_list->list[i].info, vk_ext_prop))
260 return &ext_list->list[i];
261 }
262 return NULL;
263}
264
265static void get_global_extensions(
266 const PFN_vkGetGlobalExtensionInfo fp_get,
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600267 const char *get_extension_info_name,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600268 const char *lib_name,
269 const enum extension_origin origin,
270 struct loader_extension_list *ext_list)
271{
272 uint32_t i, count;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600273 size_t siz = sizeof(count);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600274 struct loader_extension_property ext_props;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600275 VkResult res;
276
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600277 res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
278 if (res != VK_SUCCESS) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600279 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Error getting global extension count from ICD");
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600280 return;
281 }
282 siz = sizeof(VkExtensionProperties);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600283 for (i = 0; i < count; i++) {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600284 memset(&ext_props, 0, sizeof(ext_props));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600285 res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &ext_props.info);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600286 if (res == VK_SUCCESS) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600287 ext_props.origin = origin;
288 ext_props.lib_name = lib_name;
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600289 strncpy(ext_props.get_extension_info_name, get_extension_info_name, MAX_EXTENSION_NAME_SIZE);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600290 loader_add_to_ext_list(ext_list, 1, &ext_props);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600291 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600292 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600293
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600294 return;
295}
296
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600297static void get_physical_device_layer_extensions(
298 struct loader_instance *ptr_instance,
299 VkPhysicalDevice physical_device,
300 const uint32_t layer_index,
301 struct loader_extension_list *ext_list)
302{
303 uint32_t i, count;
304 size_t siz = sizeof(count);
305 VkResult res;
306 loader_platform_dl_handle lib_handle;
307 PFN_vkGetPhysicalDeviceExtensionInfo fp_get;
308 struct loader_extension_property ext_props;
309
310 if (!loader.scanned_layers[layer_index].physical_device_extensions_supported) {
311 return;
312 }
313
314 ext_props.origin = VK_EXTENSION_ORIGIN_LAYER;
315 ext_props.lib_name = loader.scanned_layers[layer_index].lib_name;
Jon Ashburn128f9422015-05-28 19:16:58 -0600316 char funcStr[MAX_EXTENSION_NAME_SIZE+1]; // add one character for 0 termination
317 snprintf(funcStr, MAX_EXTENSION_NAME_SIZE, "%sGetPhysicalDeviceExtensionInfo", ext_props.info.name);
318 funcStr[MAX_EXTENSION_NAME_SIZE] = 0; // make sure string is 0 terminated
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600319 lib_handle = loader_add_layer_lib("device", &ext_props);
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600320
321 /* first try extension specific function, then generic */
322 fp_get = (PFN_vkGetPhysicalDeviceExtensionInfo) loader_platform_get_proc_address(lib_handle, funcStr);
323 if (!fp_get) {
Jon Ashburn128f9422015-05-28 19:16:58 -0600324 sprintf(funcStr, "vkGetPhysicalDeviceExtensionInfo");
325 fp_get = (PFN_vkGetPhysicalDeviceExtensionInfo) loader_platform_get_proc_address(lib_handle, funcStr);
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600326 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600327 if (fp_get) {
328 res = fp_get(physical_device, VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
329 if (res == VK_SUCCESS) {
330 siz = sizeof(VkExtensionProperties);
331 for (i = 0; i < count; i++) {
332 memset(&ext_props, 0, sizeof(ext_props));
333 res = fp_get(physical_device, VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &ext_props.info);
334 if (res == VK_SUCCESS && (ext_props.info.sType == VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES)) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600335 ext_props.origin = VK_EXTENSION_ORIGIN_LAYER;
Jon Ashburn128f9422015-05-28 19:16:58 -0600336 strcpy(ext_props.get_extension_info_name, funcStr);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600337 ext_props.lib_name = loader.scanned_layers[layer_index].lib_name;
338 loader_add_to_ext_list(ext_list, 1, &ext_props);
339 }
340 }
341 } else {
342 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Error getting physical device extension info count from Layer %s", ext_props.lib_name);
343 }
344 }
345
346 loader_remove_layer_lib(ptr_instance, &ext_props);
347 return;
348}
349
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600350static bool loader_init_ext_list(struct loader_extension_list *ext_info)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600351{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600352 ext_info->capacity = 32 * sizeof(struct loader_extension_property);
353 ext_info->list = malloc(ext_info->capacity);
354 if (ext_info->list == NULL) {
355 return false;
356 }
357 memset(ext_info->list, 0, ext_info->capacity);
358 ext_info->count = 0;
359 return true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600360}
361
Courtney Goeltzenleuchter7d0023c2015-06-08 15:09:22 -0600362void loader_destroy_ext_list(struct loader_extension_list *ext_info)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600363{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600364 free(ext_info->list);
365 ext_info->count = 0;
366 ext_info->capacity = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600367}
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600368
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600369static void loader_add_vk_ext_to_ext_list(
370 struct loader_extension_list *ext_list,
371 uint32_t prop_list_count,
372 const VkExtensionProperties *props,
373 const struct loader_extension_list *search_list)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600374{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600375 struct loader_extension_property *ext_prop;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600376
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600377 for (uint32_t i = 0; i < prop_list_count; i++) {
378 // look for duplicates
379 if (has_vk_extension_property(&props[i], ext_list)) {
380 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600381 }
382
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600383 ext_prop = get_extension_property_from_vkext(&props[i], search_list);
384 if (!ext_prop) {
385 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Unable to find extension %s", props[i].name);
386 continue;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600387 }
388
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600389 loader_add_to_ext_list(ext_list, 1, ext_prop);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600390 }
391}
392
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600393/*
394 * Append non-duplicate extension properties defined in prop_list
395 * to the given ext_info list
396 */
397void loader_add_to_ext_list(
398 struct loader_extension_list *ext_list,
399 uint32_t prop_list_count,
400 const struct loader_extension_property *props)
401{
402 uint32_t i;
403 struct loader_extension_property *cur_ext;
404
405 if (ext_list->list == NULL || ext_list->capacity == 0) {
406 loader_init_ext_list(ext_list);
407 }
408
409 if (ext_list->list == NULL)
410 return;
411
412 for (i = 0; i < prop_list_count; i++) {
413 cur_ext = (struct loader_extension_property *) &props[i];
414
415 // look for duplicates
416 if (has_vk_extension_property(&cur_ext->info, ext_list)) {
417 continue;
418 }
419
420 // add to list at end
421 // check for enough capacity
422 if (ext_list->count * sizeof(struct loader_extension_property)
423 >= ext_list->capacity) {
424 // double capacity
425 ext_list->capacity *= 2;
426 ext_list->list = realloc(ext_list->list, ext_list->capacity);
427 }
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600428
429 /*
430 * Check if any extensions already on the list come from the same
431 * library and use the same Get*ExtensionInfo. If so, link this
432 * extension to the previous as an alias. That way when we activate
433 * extensions we only activiate the associated layer once no
434 * matter how many extensions are used.
435 */
436 for (uint32_t j = 0; j < ext_list->count; j++) {
437 struct loader_extension_property *active_property = &ext_list->list[j];
438 if (cur_ext->lib_name &&
439 cur_ext->origin == VK_EXTENSION_ORIGIN_LAYER &&
440 active_property->origin == VK_EXTENSION_ORIGIN_LAYER &&
441 strcmp(cur_ext->lib_name, active_property->lib_name) == 0 &&
442 strcmp(cur_ext->get_extension_info_name, active_property->get_extension_info_name) == 0 &&
443 active_property->alias == NULL) {
444 cur_ext->alias = active_property;
445 break;
446 }
447 }
448
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600449 memcpy(&ext_list->list[ext_list->count], cur_ext, sizeof(struct loader_extension_property));
450 ext_list->count++;
451 }
452}
453
454/*
455 * Search the search_list for any extension with
456 * a name that matches the given ext_name.
457 * Add all matching extensions to the found_list
458 * Do not add if found VkExtensionProperties is already
459 * on the found_list
460 */
461static void loader_search_ext_list_for_name(
462 const char *ext_name,
463 const struct loader_extension_list *search_list,
464 struct loader_extension_list *found_list)
465{
466 for (uint32_t i = 0; i < search_list->count; i++) {
467 struct loader_extension_property *ext_prop = &search_list->list[i];
Courtney Goeltzenleuchtera6743522015-06-09 09:14:48 -0600468 if (ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER &&
469 0 == strcmp(ext_prop->info.name, ext_name)) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600470 /* Found an extension with the same name, add to found_list */
471 loader_add_to_ext_list(found_list, 1, &search_list->list[i]);
472 }
473 }
474}
475
476bool loader_is_extension_scanned(const VkExtensionProperties *ext_prop)
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600477{
478 uint32_t i;
479
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600480 for (i = 0; i < loader.global_extensions.count; i++) {
481 if (compare_vk_extension_properties(&loader.global_extensions.list[i].info, ext_prop))
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600482 return true;
483 }
484 return false;
485}
486
Jon Ashburn27cd5842015-05-12 17:26:48 -0600487void loader_coalesce_extensions(void)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600488{
489 uint32_t i;
490 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
491
492 // traverse scanned icd list adding non-duplicate extensions to the list
493 while (icd_list != NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600494 loader_add_to_ext_list(&loader.global_extensions,
495 icd_list->global_extension_list.count,
496 icd_list->global_extension_list.list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600497 icd_list = icd_list->next;
498 };
499
500 //Traverse layers list adding non-duplicate extensions to the list
501 for (i = 0; i < loader.scanned_layer_count; i++) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600502 loader_add_to_ext_list(&loader.global_extensions,
503 loader.scanned_layers[i].global_extension_list.count,
504 loader.scanned_layers[i].global_extension_list.list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600505 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600506
507 // Traverse loader's extensions, adding non-duplicate extensions to the list
508 debug_report_add_instance_extensions(&loader.global_extensions);
Jon Ashburn0c5d4ab2015-05-26 13:57:35 -0600509 wsi_lunarg_add_instance_extensions(&loader.global_extensions);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600510}
511
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600512static void loader_icd_destroy(
513 struct loader_instance *ptr_inst,
514 struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800515{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600516 ptr_inst->total_icd_count--;
Jon Ashburn128f9422015-05-28 19:16:58 -0600517 free(icd->gpus);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800518 free(icd);
519}
520
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600521static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800522{
523 struct loader_icd *icd;
524
525 icd = malloc(sizeof(*icd));
526 if (!icd)
527 return NULL;
528
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600529 memset(icd, 0, sizeof(*icd));
530
Jon Ashburn46d1f582015-01-28 11:01:35 -0700531 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800532
533 return icd;
534}
535
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600536static struct loader_icd *loader_icd_add(
537 struct loader_instance *ptr_inst,
538 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800539{
540 struct loader_icd *icd;
541
Jon Ashburn46d1f582015-01-28 11:01:35 -0700542 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800543 if (!icd)
544 return NULL;
545
Chia-I Wu13a61a52014-08-04 11:18:20 +0800546 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700547 icd->next = ptr_inst->icds;
548 ptr_inst->icds = icd;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600549 ptr_inst->total_icd_count++;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800550
551 return icd;
552}
553
Jon Ashburn46d1f582015-01-28 11:01:35 -0700554static void loader_scanned_icd_add(const char *filename)
555{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700556 loader_platform_dl_handle handle;
Jon Ashburn3da71f22015-05-14 12:43:38 -0600557 void *fp_create_inst;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600558 void *fp_get_global_ext_info;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600559 void *fp_get_device_ext_info;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700560 struct loader_scanned_icds *new_node;
561
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700562 // Used to call: dlopen(filename, RTLD_LAZY);
563 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700564 if (!handle) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600565 loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700566 return;
567 }
568
569#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600570 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700571 if (!func_ptr) { \
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600572 loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700573 return; \
574 } \
575} while (0)
576
Jon Ashburn46888392015-01-29 15:45:51 -0700577 LOOKUP(fp_create_inst, CreateInstance);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600578 LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600579 LOOKUP(fp_get_device_ext_info, GetPhysicalDeviceExtensionInfo);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700580#undef LOOKUP
581
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600582 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds)
583 + strlen(filename) + 1);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700584 if (!new_node) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600585 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700586 return;
587 }
588
589 new_node->handle = handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700590 new_node->CreateInstance = fp_create_inst;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600591 new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600592 loader_init_ext_list(&new_node->global_extension_list);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600593 loader_init_ext_list(&new_node->device_extension_list);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700594 new_node->next = loader.scanned_icd_list;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700595
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600596 new_node->lib_name = (char *) (new_node + 1);
597 if (!new_node->lib_name) {
598 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add icd");
599 return;
600 }
601 strcpy(new_node->lib_name, filename);
602
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600603 loader.scanned_icd_list = new_node;
604
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600605 get_global_extensions(
606 (PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600607 "vkGetGlobalExtensionInfo",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600608 new_node->lib_name,
609 VK_EXTENSION_ORIGIN_ICD,
610 &new_node->global_extension_list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600611}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700612
Jon Ashburn3da71f22015-05-14 12:43:38 -0600613static void loader_icd_init_entrys(struct loader_icd *icd,
614 struct loader_scanned_icds *scanned_icds)
615{
616 /* initialize entrypoint function pointers */
617
618 #define LOOKUP(func) do { \
619 icd->func = (PFN_vk ##func) loader_platform_get_proc_address(scanned_icds->handle, "vk" #func); \
620 if (!icd->func) { \
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600621 loader_log(VK_DBG_REPORT_WARN_BIT, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn3da71f22015-05-14 12:43:38 -0600622 return; \
623 } \
624 } while (0)
625
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600626 /* could change this to use GetInstanceProcAddr in driver instead of dlsym */
627 LOOKUP(GetDeviceProcAddr);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600628 LOOKUP(DestroyInstance);
629 LOOKUP(EnumeratePhysicalDevices);
630 LOOKUP(GetPhysicalDeviceInfo);
631 LOOKUP(CreateDevice);
632 LOOKUP(GetPhysicalDeviceExtensionInfo);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600633 LOOKUP(GetMultiDeviceCompatibility);
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600634 LOOKUP(GetDisplayInfoWSI);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600635 LOOKUP(DbgCreateMsgCallback);
636 LOOKUP(DbgDestroyMsgCallback);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600637#undef LOOKUP
638
639 return;
640}
641
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600642static void loader_debug_init(void)
643{
644 const char *env;
645
646 if (g_loader_debug > 0)
647 return;
648
649 g_loader_debug = 0;
650
651 /* parse comma-separated debug options */
652 env = getenv("LOADER_DEBUG");
653 while (env) {
654 const char *p = strchr(env, ',');
655 size_t len;
656
657 if (p)
658 len = p - env;
659 else
660 len = strlen(env);
661
662 if (len > 0) {
663 if (strncmp(env, "warn", len) == 0) {
664 g_loader_debug |= LOADER_WARN_BIT;
665 g_loader_log_msgs |= VK_DBG_REPORT_WARN_BIT;
666 } else if (strncmp(env, "info", len) == 0) {
667 g_loader_debug |= LOADER_INFO_BIT;
668 g_loader_log_msgs |= VK_DBG_REPORT_INFO_BIT;
669 } else if (strncmp(env, "perf", len) == 0) {
670 g_loader_debug |= LOADER_PERF_BIT;
671 g_loader_log_msgs |= VK_DBG_REPORT_PERF_WARN_BIT;
672 } else if (strncmp(env, "error", len) == 0) {
673 g_loader_debug |= LOADER_ERROR_BIT;
674 g_loader_log_msgs |= VK_DBG_REPORT_ERROR_BIT;
675 } else if (strncmp(env, "debug", len) == 0) {
676 g_loader_debug |= LOADER_DEBUG_BIT;
677 g_loader_log_msgs |= VK_DBG_REPORT_DEBUG_BIT;
678 }
679 }
680
681 if (!p)
682 break;
683
684 env = p + 1;
685 }
686}
687
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600688/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600689 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600690 *
691 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600692 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
693 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600694 *
695 * \returns
696 * void; but side effect is to set loader_icd_scanned to true
697 */
Jon Ashburn27cd5842015-05-12 17:26:48 -0600698void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800699{
Ian Elliott4470a302015-02-17 10:33:47 -0700700 const char *p, *next;
701 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600702 DIR *sysdir;
703 struct dirent *dent;
704 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600705 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700706 uint32_t len;
Jon Ashburn6301a0f2015-05-29 13:15:39 -0600707
708 // convenient place to initialize a mutex
709 loader_platform_thread_create_mutex(&loader_lock);
710
Ian Elliott4470a302015-02-17 10:33:47 -0700711#if defined(WIN32)
712 bool must_free_libPaths;
713 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
714 DRIVER_PATH_REGISTRY_VALUE);
715 if (libPaths != NULL) {
716 must_free_libPaths = true;
717 } else {
718 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600719 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700720 }
721#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600722 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700723 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
724 libPaths = getenv(DRIVER_PATH_ENV);
725 }
726 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600727 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600728 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700729#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800730
Courtney Goeltzenleuchter880a2a72015-06-08 15:11:18 -0600731 loader_debug_init();
732
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600733 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700734 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600735 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700736 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600737 next = p + len;
738 }
739 else {
Ian Elliott19628802015-02-04 12:06:46 -0700740 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600741 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
742 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600743 next++;
744 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800745
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700746 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
747 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600748 sysdir = opendir(p);
749 if (sysdir) {
750 dent = readdir(sysdir);
751 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600752 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
753 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700754 */
755 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600756 VK_DRIVER_LIBRARY_PREFIX,
757 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700758 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600759 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
760 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700761 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600762 VK_LIBRARY_SUFFIX,
763 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700764 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
765 loader_scanned_icd_add(icd_library);
766 }
767 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600768
769 dent = readdir(sysdir);
770 }
771 closedir(sysdir);
772 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800773 }
774
Ian Elliott4470a302015-02-17 10:33:47 -0700775#if defined(WIN32)
776 // Free any allocated memory:
777 if (must_free_libPaths) {
778 free(libPaths);
779 }
780#endif // WIN32
781
782 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700783 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800784}
785
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600786
Jon Ashburn27cd5842015-05-12 17:26:48 -0600787void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600788{
789 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700790 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600791 DIR *curdir;
792 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700793 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600794 char temp_str[1024];
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600795 uint32_t count;
796 PFN_vkGetGlobalExtensionInfo fp_get_ext;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600797
Ian Elliott4470a302015-02-17 10:33:47 -0700798#if defined(WIN32)
799 bool must_free_libPaths;
800 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
801 LAYERS_PATH_REGISTRY_VALUE);
802 if (libPaths != NULL) {
803 must_free_libPaths = true;
804 } else {
805 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600806 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600807 }
Ian Elliott4470a302015-02-17 10:33:47 -0700808#else // WIN32
809 if (geteuid() == getuid()) {
810 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700811 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700812 }
813 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600814 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700815 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700816#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600817
Ian Elliott4470a302015-02-17 10:33:47 -0700818 if (libPaths == NULL) {
819 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700820 return;
821 }
Ian Elliott4470a302015-02-17 10:33:47 -0700822 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700823 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700824 if (loader.layer_dirs == NULL) {
Jon Ashburn90c6a0e2015-06-04 15:30:58 -0600825 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Out of memory can't add layer directories");
826
Ian Elliott4470a302015-02-17 10:33:47 -0700827 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700828 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700829 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600830 // Alloc passed, so we know there is enough space to hold the string
Ian Elliott4470a302015-02-17 10:33:47 -0700831 strcpy(loader.layer_dirs, libPaths);
832#if defined(WIN32)
833 // Free any allocated memory:
834 if (must_free_libPaths) {
835 free(libPaths);
836 must_free_libPaths = false;
837 }
838#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700839 libPaths = loader.layer_dirs;
840
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600841 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600842 for (i = 0; i < loader.scanned_layer_count; i++) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600843 if (loader.scanned_layers[i].lib_name != NULL)
844 free(loader.scanned_layers[i].lib_name);
845 loader_destroy_ext_list(&loader.scanned_layers[i].global_extension_list);
846 loader.scanned_layers[i].lib_name = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600847 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600848 loader.scanned_layer_count = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600849 count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600850
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600851 for (p = libPaths; *p; p = next) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600852 next = strchr(p, PATH_SEPERATOR);
853 if (next == NULL) {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600854 len = (uint32_t) strlen(p);
855 next = p + len;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600856 }
857 else {
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600858 len = (uint32_t) (next - p);
859 *(char *) next = '\0';
860 next++;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600861 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600862
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600863 curdir = opendir(p);
864 if (curdir) {
865 dent = readdir(curdir);
866 while (dent) {
867 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
868 * ending with VK_LIBRARY_SUFFIX
869 */
870 if (!strncmp(dent->d_name,
871 VK_LAYER_LIBRARY_PREFIX,
872 VK_LAYER_LIBRARY_PREFIX_LEN)) {
873 uint32_t nlen = (uint32_t) strlen(dent->d_name);
874 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
875 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
876 !strncmp(suf,
877 VK_LIBRARY_SUFFIX,
878 VK_LIBRARY_SUFFIX_LEN)) {
879 loader_platform_dl_handle handle;
880 snprintf(temp_str, sizeof(temp_str),
881 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
882 // Used to call: dlopen(temp_str, RTLD_LAZY)
883 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -0600884 "Attempt to open library: %s", temp_str);
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600885 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -0600886 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0, "open library failed");
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600887 dent = readdir(curdir);
888 continue;
889 }
890 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0,
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -0600891 "Opened library: %s", temp_str);
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -0600892
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600893 /* TODO: Remove fixed count */
894 if (count == MAX_LAYER_LIBRARIES) {
895 loader_log(VK_DBG_REPORT_ERROR_BIT, 0,
896 "%s ignored: max layer libraries exceed",
897 temp_str);
898 break;
899 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600900
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600901 fp_get_ext = loader_platform_get_proc_address(handle, "vkGetGlobalExtensionInfo");
902 if (!fp_get_ext) {
903 loader_log(VK_DBG_REPORT_WARN_BIT, 0,
904 "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
905 temp_str);
906 dent = readdir(curdir);
907 loader_platform_close_library(handle);
908 continue;
909 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600910
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600911 loader.scanned_layers[count].lib_name =
912 malloc(strlen(temp_str) + 1);
913 if (loader.scanned_layers[count].lib_name == NULL) {
914 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "%s ignored: out of memory", temp_str);
915 break;
916 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600917
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600918 strcpy(loader.scanned_layers[count].lib_name, temp_str);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600919
Courtney Goeltzenleuchter1e4602f2015-06-09 09:44:13 -0600920 loader_log(VK_DBG_REPORT_DEBUG_BIT, 0, "Collecting global extensions for %s", temp_str);
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600921 get_global_extensions(
922 fp_get_ext,
923 "vkGetGlobalExtensionInfo",
924 loader.scanned_layers[count].lib_name,
925 VK_EXTENSION_ORIGIN_LAYER,
926 &loader.scanned_layers[count].global_extension_list);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600927
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600928 fp_get_ext = loader_platform_get_proc_address(handle,
929 "vkGetPhysicalDeviceExtensionInfo");
930 if (fp_get_ext) {
931 loader.scanned_layers[count].physical_device_extensions_supported = true;
932 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -0600933
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600934 count++;
935 loader_platform_close_library(handle);
936 }
937 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600938
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -0600939 dent = readdir(curdir);
940 } // while (dir_entry)
941 if (count == MAX_LAYER_LIBRARIES)
942 break;
943 closedir(curdir);
944 } // if (curdir))
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600945 } // for (libpaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600946
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600947 loader.scanned_layer_count = count;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600948 loader.layers_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600949}
950
Jon Ashburn27cd5842015-05-12 17:26:48 -0600951static void* VKAPI loader_gpa_instance_internal(VkInstance inst, const char * pName)
952{
953 // inst is not wrapped
954 if (inst == VK_NULL_HANDLE) {
955 return NULL;
956 }
957 VkLayerInstanceDispatchTable* disp_table = * (VkLayerInstanceDispatchTable **) inst;
958 void *addr;
959
Jon Ashburn8fd08252015-05-28 16:25:02 -0600960 if (!strcmp(pName, "vkGetInstanceProcAddr"))
961 return (void *) loader_gpa_instance_internal;
962
Jon Ashburn27cd5842015-05-12 17:26:48 -0600963 if (disp_table == NULL)
964 return NULL;
965
966 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600967 if (addr) {
Jon Ashburn27cd5842015-05-12 17:26:48 -0600968 return addr;
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600969 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600970
971 if (disp_table->GetInstanceProcAddr == NULL) {
972 return NULL;
973 }
974 return disp_table->GetInstanceProcAddr(inst, pName);
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600975}
976
Jon Ashburn128f9422015-05-28 19:16:58 -0600977struct loader_icd * loader_get_icd(const VkPhysicalDevice gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600978{
Jon Ashburn128f9422015-05-28 19:16:58 -0600979
Jon Ashburn98bd4542015-01-29 16:44:24 -0700980 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
981 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
982 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn128f9422015-05-28 19:16:58 -0600983 if (icd->gpus[i] == gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -0700984 *gpu_index = i;
985 return icd;
986 }
987 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600988 }
989 return NULL;
990}
991
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600992static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600993{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600994 if (icd->layer_count[gpu_index])
995 return true;
996 else
997 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600998}
999
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001000static loader_platform_dl_handle loader_add_layer_lib(
Jon Ashburn4f67d742015-05-27 13:19:22 -06001001 const char *chain_type,
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001002 struct loader_extension_property *ext_prop)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001003{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001004 struct loader_lib_info *new_layer_lib_list, *my_lib;
1005
1006 /* Only loader layer libraries here */
1007 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
1008 return NULL;
1009 }
1010
1011 for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) {
1012 if (strcmp(loader.loaded_layer_lib_list[i].lib_name, ext_prop->lib_name) == 0) {
1013 /* Have already loaded this library, just increment ref count */
1014 loader.loaded_layer_lib_list[i].ref_count++;
Jon Ashburne68a9ff2015-05-25 14:11:37 -06001015 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001016 "%s Chain: Increment layer reference count for layer library %s",
1017 chain_type, ext_prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001018 return loader.loaded_layer_lib_list[i].lib_handle;
1019 }
1020 }
1021
1022 /* Haven't seen this library so load it */
1023 new_layer_lib_list = realloc(loader.loaded_layer_lib_list,
1024 (loader.loaded_layer_lib_count + 1) * sizeof(struct loader_lib_info));
1025 if (!new_layer_lib_list) {
1026 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "loader: malloc failed");
1027 return NULL;
1028 }
1029
1030 my_lib = &new_layer_lib_list[loader.loaded_layer_lib_count];
1031
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001032 /* NOTE: We require that the extension property be immutable */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001033 my_lib->lib_name = ext_prop->lib_name;
1034 my_lib->ref_count = 0;
1035 my_lib->lib_handle = NULL;
1036
1037 if ((my_lib->lib_handle = loader_platform_open_library(my_lib->lib_name)) == NULL) {
1038 loader_log(VK_DBG_REPORT_ERROR_BIT, 0,
1039 loader_platform_open_library_error(my_lib->lib_name));
1040 return NULL;
1041 } else {
1042 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001043 "Chain: %s: Loading layer library %s",
1044 chain_type, ext_prop->lib_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001045 }
1046 loader.loaded_layer_lib_count++;
1047 loader.loaded_layer_lib_list = new_layer_lib_list;
1048 my_lib->ref_count++;
1049
1050 return my_lib->lib_handle;
1051}
1052
1053static void loader_remove_layer_lib(
1054 struct loader_instance *inst,
1055 struct loader_extension_property *ext_prop)
1056{
1057 uint32_t idx;
1058 struct loader_lib_info *new_layer_lib_list, *my_lib;
1059
1060 /* Only loader layer libraries here */
1061 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001062 return;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001063 }
Jon Ashburndf7d5842014-10-16 15:48:50 -06001064
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001065 for (uint32_t i = 0; i < loader.loaded_layer_lib_count; i++) {
1066 if (strcmp(loader.loaded_layer_lib_list[i].lib_name, ext_prop->lib_name) == 0) {
1067 /* found matching library */
1068 idx = i;
1069 my_lib = &loader.loaded_layer_lib_list[i];
1070 break;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001071 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001072 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001073
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001074 my_lib->ref_count--;
1075 inst->layer_count--;
1076 if (my_lib->ref_count > 0) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001077 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
1078 "Decrement reference count for layer library %s", ext_prop->lib_name);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001079 return;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001080 }
Jon Ashburn19c25022015-04-14 14:14:48 -06001081
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001082 loader_platform_close_library(my_lib->lib_handle);
1083 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
1084 "Unloading layer library %s", ext_prop->lib_name);
1085
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001086 /* Need to remove unused library from list */
1087 new_layer_lib_list = malloc((loader.loaded_layer_lib_count - 1) * sizeof(struct loader_lib_info));
1088 if (!new_layer_lib_list) {
1089 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "loader: malloc failed");
1090 return;
1091 }
1092
1093 if (idx > 0) {
1094 /* Copy records before idx */
1095 memcpy(new_layer_lib_list, &loader.loaded_layer_lib_list[0],
1096 sizeof(struct loader_lib_info) * idx);
1097 }
1098 if (idx < (loader.loaded_layer_lib_count - 1)) {
1099 /* Copy records after idx */
1100 memcpy(&new_layer_lib_list[idx], &loader.loaded_layer_lib_list[idx+1],
1101 sizeof(struct loader_lib_info) * (loader.loaded_layer_lib_count - idx - 1));
1102 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001103
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001104 free(loader.loaded_layer_lib_list);
1105 loader.loaded_layer_lib_count--;
1106 loader.loaded_layer_lib_list = new_layer_lib_list;
Jon Ashburnb8358052014-11-18 09:06:04 -07001107}
1108
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001109static void loader_add_layer_env(
1110 struct loader_extension_list *ext_list,
1111 const struct loader_extension_list *search_list)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001112{
Ian Elliott4470a302015-02-17 10:33:47 -07001113 char *layerEnv;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001114 uint32_t len;
Jon Ashburnd09bd102014-10-22 21:15:26 -06001115 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001116
Ian Elliott4470a302015-02-17 10:33:47 -07001117#if defined(WIN32)
1118 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
1119 LAYER_NAMES_REGISTRY_VALUE);
1120#else // WIN32
1121 layerEnv = getenv(LAYER_NAMES_ENV);
1122#endif // WIN32
1123 if (layerEnv == NULL) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001124 return;
Ian Elliott4470a302015-02-17 10:33:47 -07001125 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001126 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -07001127 if (p == NULL) {
1128#if defined(WIN32)
1129 free(layerEnv);
1130#endif // WIN32
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001131 return;
Ian Elliott4470a302015-02-17 10:33:47 -07001132 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001133 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -07001134#if defined(WIN32)
1135 free(layerEnv);
1136#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -06001137 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001138
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001139 while (p && *p ) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001140 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001141 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -07001142 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001143 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001144 } else {
Ian Elliott19628802015-02-04 12:06:46 -07001145 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001146 *(char *) next = '\0';
1147 next++;
1148 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001149 name = basename(p);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001150 loader_search_ext_list_for_name(name, search_list, ext_list);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001151 p = next;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001152 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001153
Jon Ashburnd09bd102014-10-22 21:15:26 -06001154 free(pOrig);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001155 return;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001156}
1157
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001158
Courtney Goeltzenleuchter7d0023c2015-06-08 15:09:22 -06001159void loader_deactivate_instance_layers(struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001160{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001161 if (!instance->layer_count) {
1162 return;
1163 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001164
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001165 /* Create instance chain of enabled layers */
Courtney Goeltzenleuchter7d0023c2015-06-08 15:09:22 -06001166 for (uint32_t i = 0; i < instance->activated_layer_list.count; i++) {
1167 struct loader_extension_property *ext_prop = &instance->activated_layer_list.list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001168
1169 loader_remove_layer_lib(instance, ext_prop);
1170
1171 instance->layer_count--;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001172 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06001173
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001174}
1175
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001176void loader_enable_instance_layers(struct loader_instance *inst)
1177{
1178 if (inst == NULL)
1179 return;
1180
1181 /* Add any layers specified in the environment first */
1182 loader_add_layer_env(&inst->enabled_instance_extensions, &loader.global_extensions);
1183
1184 /* Add layers / extensions specified by the application */
1185 loader_add_vk_ext_to_ext_list(
1186 &inst->enabled_instance_extensions,
1187 inst->app_extension_count,
1188 inst->app_extension_props,
1189 &loader.global_extensions);
1190}
1191
Jon Ashburn27cd5842015-05-12 17:26:48 -06001192uint32_t loader_activate_instance_layers(struct loader_instance *inst)
1193{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001194 uint32_t layer_idx;
Jon Ashburn128f9422015-05-28 19:16:58 -06001195 VkBaseLayerObject *wrappedInstance;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001196
Jon Ashburn27cd5842015-05-12 17:26:48 -06001197 if (inst == NULL)
1198 return 0;
1199
1200 // NOTE inst is unwrapped at this point in time
1201 VkObject baseObj = (VkObject) inst;
1202 VkObject nextObj = (VkObject) inst;
1203 VkBaseLayerObject *nextInstObj;
1204 PFN_vkGetInstanceProcAddr nextGPA = loader_gpa_instance_internal;
1205
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001206 /*
1207 * Figure out how many actual layers will need to be wrapped.
1208 */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001209 for (uint32_t i = 0; i < inst->enabled_instance_extensions.count; i++) {
1210 struct loader_extension_property *ext_prop = &inst->enabled_instance_extensions.list[i];
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001211 if (ext_prop->alias) {
1212 ext_prop = ext_prop->alias;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001213 }
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001214 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
1215 continue;
1216 }
1217 loader_add_to_ext_list(&inst->activated_layer_list, 1, ext_prop);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001218 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06001219
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001220 inst->layer_count = inst->activated_layer_list.count;
1221
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001222 if (!inst->layer_count) {
Jon Ashburn27cd5842015-05-12 17:26:48 -06001223 return 0;
1224 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001225
Jon Ashburn128f9422015-05-28 19:16:58 -06001226 wrappedInstance = malloc(sizeof(VkBaseLayerObject)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001227 * inst->layer_count);
Jon Ashburn128f9422015-05-28 19:16:58 -06001228 if (!wrappedInstance) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001229 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc Instance objects for layer");
1230 return 0;
1231 }
1232
1233 /* Create instance chain of enabled layers */
1234 layer_idx = inst->layer_count - 1;
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001235 for (int32_t i = inst->activated_layer_list.count - 1; i >= 0; i--) {
1236 struct loader_extension_property *ext_prop = &inst->activated_layer_list.list[i];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001237 loader_platform_dl_handle lib_handle;
1238
1239 /*
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001240 * For global exenstions implemented within the loader (i.e. WSI, DEBUG_REPORT
1241 * the extension must provide two entry points for the loader to use:
1242 * - "trampoline" entry point - this is the address returned by GetProcAddr
1243 * and will always do what's necessary to support a global call.
1244 * - "terminator" function - this function will be put at the end of the
1245 * instance chain and will contain the necessary logica to call / process
1246 * the extension for the appropriate ICDs that are available.
1247 * There is no generic mechanism for including these functions, the references
1248 * must be placed into the appropriate loader entry points.
1249 * GetInstanceProcAddr: call extension GetInstanceProcAddr to check for GetProcAddr requests
1250 * loader_coalesce_extensions(void) - add extension records to the list of global
1251 * extension available to the app.
1252 * instance_disp - add function pointer for terminator function to this array.
1253 * The extension itself should be in a separate file that will be
1254 * linked directly with the loader.
1255 * Note: An extension's Get*ProcAddr should not return a function pointer for
1256 * any extension entry points until the extension has been enabled.
1257 * To do this requires a different behavior from Get*ProcAddr functions implemented
1258 * in layers.
1259 * The very first call to a layer will be it's Get*ProcAddr function requesting
1260 * the layer's vkGet*ProcAddr. The layer should intialize it's internal dispatch table
1261 * with the wrapped object given (either Instance or Device) and return the layer's
1262 * Get*ProcAddr function. The layer should also use this opportunity to record the
1263 * baseObject so that it can find the correct local dispatch table on future calls.
1264 * Subsequent calls to Get*ProcAddr, CreateInstance, CreateDevice
1265 * will not use a wrapped object and must look up their local dispatch table from
1266 * the given baseObject.
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001267 */
Courtney Goeltzenleuchter0f1d8eb2015-06-07 17:28:17 -06001268 assert(ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001269
Jon Ashburn128f9422015-05-28 19:16:58 -06001270 nextInstObj = (wrappedInstance + layer_idx);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001271 nextInstObj->pGPA = nextGPA;
1272 nextInstObj->baseObject = baseObj;
1273 nextInstObj->nextObject = nextObj;
1274 nextObj = (VkObject) nextInstObj;
1275
1276 char funcStr[256];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001277 snprintf(funcStr, 256, "%sGetInstanceProcAddr", ext_prop->info.name);
Jon Ashburn4f67d742015-05-27 13:19:22 -06001278 lib_handle = loader_add_layer_lib("instance", ext_prop);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001279 if ((nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL)
1280 nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetInstanceProcAddr");
Jon Ashburn27cd5842015-05-12 17:26:48 -06001281 if (!nextGPA) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001282 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to find vkGetInstanceProcAddr in layer %s", ext_prop->lib_name);
Courtney Goeltzenleuchtera9e4af42015-06-01 14:49:17 -06001283
1284 /* TODO: Should we return nextObj, nextGPA to previous? */
Jon Ashburn27cd5842015-05-12 17:26:48 -06001285 continue;
1286 }
1287
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001288 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
1289 "Insert instance layer library %s for extension: %s",
1290 ext_prop->lib_name, ext_prop->info.name);
1291
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001292 layer_idx--;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001293 }
1294
Jon Ashburn8fd08252015-05-28 16:25:02 -06001295 loader_init_instance_core_dispatch_table(inst->disp, nextGPA, (VkInstance) nextObj, (VkInstance) baseObj);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001296
Jon Ashburn128f9422015-05-28 19:16:58 -06001297 free(wrappedInstance);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001298 return inst->layer_count;
1299}
1300
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001301void loader_activate_instance_layer_extensions(struct loader_instance *inst)
1302{
1303
1304 loader_init_instance_extension_dispatch_table(inst->disp,
1305 inst->disp->GetInstanceProcAddr,
Jon Ashburn128f9422015-05-28 19:16:58 -06001306 (VkInstance) inst);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001307}
1308
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001309void loader_enable_device_layers(struct loader_icd *icd, uint32_t gpu_index)
1310{
1311 if (icd == NULL)
1312 return;
1313
1314 /* Add any layers specified in the environment first */
1315 loader_add_layer_env(&icd->enabled_device_extensions[gpu_index], &loader.global_extensions);
1316
1317 /* Add layers / extensions specified by the application */
1318 loader_add_vk_ext_to_ext_list(
1319 &icd->enabled_device_extensions[gpu_index],
1320 icd->app_extension_count[gpu_index],
1321 icd->app_extension_props[gpu_index],
1322 &loader.global_extensions);
1323}
1324
1325extern uint32_t loader_activate_device_layers(
1326 VkDevice device,
1327 struct loader_icd *icd,
1328 uint32_t gpu_index,
1329 uint32_t ext_count,
1330 const VkExtensionProperties *ext_props)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001331{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001332 uint32_t count;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001333 uint32_t layer_idx;
1334
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001335 if (!icd)
1336 return 0;
Jon Ashburn83a64252015-04-15 11:31:12 -06001337 assert(gpu_index < MAX_GPUS_FOR_LAYER);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001338
1339 /* activate any layer libraries */
1340 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001341 VkObject nextObj = (VkObject) device;
1342 VkObject baseObj = nextObj;
1343 VkBaseLayerObject *nextGpuObj;
Jon Ashburn7c096122015-05-22 09:19:49 -06001344 PFN_vkGetDeviceProcAddr nextGPA = icd->GetDeviceProcAddr;
Jon Ashburnce94f312015-05-28 19:25:20 -06001345 VkBaseLayerObject *wrappedGpus;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001346 count = 0;
1347 for (uint32_t i = 0; i < icd->enabled_device_extensions[gpu_index].count; i++) {
1348 struct loader_extension_property *ext_prop = &icd->enabled_device_extensions[gpu_index].list[i];
1349 if (ext_prop->origin == VK_EXTENSION_ORIGIN_LAYER) {
1350 count++;
1351 }
1352 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001353 if (!count)
1354 return 0;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001355
1356 icd->layer_count[gpu_index] = count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001357
Jon Ashburnce94f312015-05-28 19:25:20 -06001358 wrappedGpus = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
1359 if (! wrappedGpus) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001360 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn27cd5842015-05-12 17:26:48 -06001361 return 0;
1362 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001363 layer_idx = count - 1;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001364 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001365 struct loader_extension_property *ext_prop = &icd->enabled_device_extensions[gpu_index].list[i];
1366 loader_platform_dl_handle lib_handle;
1367
1368 if (ext_prop->origin != VK_EXTENSION_ORIGIN_LAYER) {
1369 continue;
1370 }
1371
Jon Ashburnce94f312015-05-28 19:25:20 -06001372 nextGpuObj = (wrappedGpus + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001373 nextGpuObj->pGPA = nextGPA;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001374 nextGpuObj->baseObject = baseObj;
1375 nextGpuObj->nextObject = nextObj;
1376 nextObj = (VkObject) nextGpuObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001377
Jon Ashburn79113cc2014-12-01 14:22:40 -07001378 char funcStr[256];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001379 snprintf(funcStr, 256, "%sGetDeviceProcAddr", ext_prop->info.name);
Jon Ashburn4f67d742015-05-27 13:19:22 -06001380 lib_handle = loader_add_layer_lib("device", ext_prop);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001381 if ((nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, funcStr)) == NULL)
1382 nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(lib_handle, "vkGetDeviceProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001383 if (!nextGPA) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001384 loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Failed to find vkGetDeviceProcAddr in layer %s", ext_prop->info.name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001385 continue;
1386 }
1387
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001388 loader_log(VK_DBG_REPORT_INFO_BIT, 0,
1389 "Insert device layer library %s for extension: %s",
1390 ext_prop->lib_name, ext_prop->info.name);
1391
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001392 layer_idx--;
1393 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001394
Jon Ashburn8fd08252015-05-28 16:25:02 -06001395 loader_init_device_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA,
1396 (VkPhysicalDevice) nextObj, (VkPhysicalDevice) baseObj);
Jon Ashburnce94f312015-05-28 19:25:20 -06001397 free(wrappedGpus);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001398 } else {
1399 // TODO: Check that active layers match requested?
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001400 }
1401 return icd->layer_count[gpu_index];
1402}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001403
Jon Ashburn27cd5842015-05-12 17:26:48 -06001404VkResult loader_CreateInstance(
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001405 const VkInstanceCreateInfo* pCreateInfo,
1406 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001407{
Jon Ashburneed0c002015-05-21 17:42:17 -06001408 struct loader_instance *ptr_instance = *(struct loader_instance **) pInstance;
Jon Ashburn46888392015-01-29 15:45:51 -07001409 struct loader_scanned_icds *scanned_icds;
1410 struct loader_icd *icd;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001411 VkResult res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001412
Jon Ashburn46888392015-01-29 15:45:51 -07001413 scanned_icds = loader.scanned_icd_list;
1414 while (scanned_icds) {
1415 icd = loader_icd_add(ptr_instance, scanned_icds);
1416 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001417 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn3da71f22015-05-14 12:43:38 -06001418 &(icd->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001419 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001420 {
1421 ptr_instance->icds = ptr_instance->icds->next;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001422 loader_icd_destroy(ptr_instance, icd);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001423 icd->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001424 loader_log(VK_DBG_REPORT_WARN_BIT, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001425 "ICD ignored: failed to CreateInstance on device");
Jon Ashburn3da71f22015-05-14 12:43:38 -06001426 } else
1427 {
1428 loader_icd_init_entrys(icd, scanned_icds);
Jon Ashburn46888392015-01-29 15:45:51 -07001429 }
1430 }
1431 scanned_icds = scanned_icds->next;
1432 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001433
Ian Elliotteb450762015-02-05 15:19:15 -07001434 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001435 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001436 }
Jon Ashburn46888392015-01-29 15:45:51 -07001437
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001438 return res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001439}
1440
Jon Ashburn27cd5842015-05-12 17:26:48 -06001441VkResult loader_DestroyInstance(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001442 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001443{
Courtney Goeltzenleuchterdeceded2015-06-08 15:04:02 -06001444 struct loader_instance *ptr_instance = loader_instance(instance);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001445 struct loader_icd *icds = ptr_instance->icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001446 VkResult res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001447
1448 // Remove this instance from the list of instances:
1449 struct loader_instance *prev = NULL;
1450 struct loader_instance *next = loader.instances;
1451 while (next != NULL) {
1452 if (next == ptr_instance) {
1453 // Remove this instance from the list:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001454 free(ptr_instance->app_extension_props);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001455 if (prev)
1456 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001457 else
1458 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001459 break;
1460 }
1461 prev = next;
1462 next = next->next;
1463 }
1464 if (next == NULL) {
1465 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001466 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001467 }
1468
Jon Ashburn3da71f22015-05-14 12:43:38 -06001469 while (icds) {
1470 if (icds->instance) {
1471 res = icds->DestroyInstance(icds->instance);
Tony Barbourf20f87b2015-04-22 09:02:32 -06001472 if (res != VK_SUCCESS)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001473 loader_log(VK_DBG_REPORT_WARN_BIT, 0,
Tony Barbourf20f87b2015-04-22 09:02:32 -06001474 "ICD ignored: failed to DestroyInstance on device");
1475 }
Jon Ashburn128f9422015-05-28 19:16:58 -06001476 loader_icd_destroy(ptr_instance, icds);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001477 icds->instance = VK_NULL_HANDLE;
1478 icds = icds->next;
Jon Ashburn46888392015-01-29 15:45:51 -07001479 }
1480
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001481
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001482 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001483}
1484
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001485VkResult loader_init_physical_device_info(
1486 struct loader_instance *ptr_instance)
1487{
1488 struct loader_icd *icd;
1489 uint32_t n, count = 0;
1490 VkResult res = VK_ERROR_UNKNOWN;
1491
1492 icd = ptr_instance->icds;
1493 while (icd) {
1494 res = icd->EnumeratePhysicalDevices(icd->instance, &n, NULL);
1495 if (res != VK_SUCCESS)
1496 return res;
1497 icd->gpu_count = n;
1498 count += n;
1499 icd = icd->next;
1500 }
1501
1502 ptr_instance->total_gpu_count = count;
1503
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001504 icd = ptr_instance->icds;
1505 while (icd) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001506
1507 n = icd->gpu_count;
Jon Ashburn128f9422015-05-28 19:16:58 -06001508 icd->gpus = (VkPhysicalDevice *) malloc(n * sizeof(VkPhysicalDevice));
1509 if (!icd->gpus) {
1510 /* TODO: Add cleanup code here */
1511 return VK_ERROR_OUT_OF_HOST_MEMORY;
1512 }
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001513 res = icd->EnumeratePhysicalDevices(
1514 icd->instance,
1515 &n,
Jon Ashburn128f9422015-05-28 19:16:58 -06001516 icd->gpus);
1517 if ((res == VK_SUCCESS) && (n == icd->gpu_count)) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001518
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001519 for (unsigned int i = 0; i < n; i++) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001520
Jon Ashburn128f9422015-05-28 19:16:58 -06001521 loader_init_dispatch(icd->gpus[i], ptr_instance->disp);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001522
1523 if (!loader_init_ext_list(&icd->device_extension_cache[i])) {
1524 /* TODO: Add cleanup code here */
1525 res = VK_ERROR_OUT_OF_HOST_MEMORY;
1526 }
1527 if (res == VK_SUCCESS && icd->GetPhysicalDeviceExtensionInfo) {
1528 size_t data_size;
1529 uint32_t extension_count;
1530
1531 data_size = sizeof(extension_count);
Jon Ashburn128f9422015-05-28 19:16:58 -06001532 res = icd->GetPhysicalDeviceExtensionInfo(icd->gpus[i], VK_EXTENSION_INFO_TYPE_COUNT, 0, &data_size, &extension_count);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001533 if (data_size == sizeof(extension_count) && res == VK_SUCCESS) {
1534 struct loader_extension_property ext_props;
1535
1536 /* Gather all the ICD extensions */
1537 for (uint32_t extension_id = 0; extension_id < extension_count; extension_id++) {
1538 data_size = sizeof(VkExtensionProperties);
Jon Ashburn128f9422015-05-28 19:16:58 -06001539 res = icd->GetPhysicalDeviceExtensionInfo(icd->gpus[i], VK_EXTENSION_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001540 extension_id, &data_size, &ext_props.info);
1541 if (data_size == sizeof(VkExtensionProperties) && res == VK_SUCCESS) {
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001542 ext_props.origin = VK_EXTENSION_ORIGIN_ICD;
1543 ext_props.lib_name = icd->scanned_icds->lib_name;
Jon Ashburn128f9422015-05-28 19:16:58 -06001544 // For ICDs, this is the only option
1545 strcpy(ext_props.get_extension_info_name, "vkGetPhysicalDeviceExtensionInfo");
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001546 loader_add_to_ext_list(&icd->device_extension_cache[i], 1, &ext_props);
1547 }
1548 }
1549
1550 // Traverse layers list adding non-duplicate extensions to the list
1551 for (uint32_t l = 0; l < loader.scanned_layer_count; l++) {
Jon Ashburn128f9422015-05-28 19:16:58 -06001552 get_physical_device_layer_extensions(ptr_instance, icd->gpus[i], l, &icd->device_extension_cache[i]);
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001553 }
1554 }
1555 }
1556
1557 if (res != VK_SUCCESS) {
1558 /* clean up any extension lists previously created before this request failed */
1559 for (uint32_t j = 0; j < i; j++) {
1560 loader_destroy_ext_list(&icd->device_extension_cache[i]);
1561 }
1562 return res;
1563 }
1564 }
1565
1566 count += n;
1567 }
1568
1569 icd = icd->next;
1570 }
1571
1572 return VK_SUCCESS;
1573}
1574
Jon Ashburn27cd5842015-05-12 17:26:48 -06001575VkResult loader_EnumeratePhysicalDevices(
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001576 VkInstance instance,
1577 uint32_t* pPhysicalDeviceCount,
1578 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001579{
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001580 uint32_t index = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001581 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001582 struct loader_icd *icd = ptr_instance->icds;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001583
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001584 if (ptr_instance->total_gpu_count == 0) {
1585 loader_init_physical_device_info(ptr_instance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001586 }
1587
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001588 *pPhysicalDeviceCount = ptr_instance->total_gpu_count;
1589 if (!pPhysicalDevices) {
1590 return VK_SUCCESS;
1591 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001592
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001593 while (icd) {
1594 assert((index + icd->gpu_count) <= *pPhysicalDeviceCount);
Jon Ashburn128f9422015-05-28 19:16:58 -06001595 memcpy(&pPhysicalDevices[index], icd->gpus, icd->gpu_count * sizeof(VkPhysicalDevice));
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001596 index += icd->gpu_count;
1597 icd = icd->next;
1598 }
1599
1600 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001601}
1602
Jon Ashburn3da71f22015-05-14 12:43:38 -06001603VkResult loader_GetPhysicalDeviceInfo(
1604 VkPhysicalDevice gpu,
1605 VkPhysicalDeviceInfoType infoType,
1606 size_t* pDataSize,
1607 void* pData)
1608{
1609 uint32_t gpu_index;
Jon Ashburn128f9422015-05-28 19:16:58 -06001610 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001611 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1612
1613 if (icd->GetPhysicalDeviceInfo)
1614 res = icd->GetPhysicalDeviceInfo(gpu, infoType, pDataSize, pData);
1615
1616 return res;
1617}
1618
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001619VkResult loader_CreateDevice(
1620 VkPhysicalDevice gpu,
1621 const VkDeviceCreateInfo* pCreateInfo,
1622 VkDevice* pDevice)
1623{
1624 uint32_t gpu_index;
Jon Ashburn128f9422015-05-28 19:16:58 -06001625 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001626 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001627
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001628 if (icd->CreateDevice) {
1629 res = icd->CreateDevice(gpu, pCreateInfo, pDevice);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001630 if (res != VK_SUCCESS) {
1631 return res;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001632 }
Jon Ashburnd6a5b672015-06-08 16:38:48 -06001633 PFN_vkGetDeviceProcAddr get_proc_addr = icd->GetDeviceProcAddr;
1634 loader_init_device_dispatch_table(icd->loader_dispatch + gpu_index,
1635 get_proc_addr, icd->gpus[gpu_index], icd->gpus[gpu_index]);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001636
1637 VkLayerDispatchTable *dev_disp = icd->loader_dispatch + gpu_index;
1638 loader_init_dispatch(*pDevice, dev_disp);
1639
1640 icd->app_extension_count[gpu_index] = pCreateInfo->extensionCount;
1641 icd->app_extension_props[gpu_index] = (VkExtensionProperties *) malloc(sizeof(VkExtensionProperties) * pCreateInfo->extensionCount);
1642 if (icd->app_extension_props[gpu_index] == NULL && (icd->app_extension_count[gpu_index] > 0)) {
1643 return VK_ERROR_OUT_OF_HOST_MEMORY;
1644 }
1645
1646 /* Make local copy of extension list */
1647 if (icd->app_extension_count[gpu_index] > 0 && icd->app_extension_props[gpu_index] != NULL) {
1648 memcpy(icd->app_extension_props[gpu_index], pCreateInfo->pEnabledExtensions, sizeof(VkExtensionProperties) * pCreateInfo->extensionCount);
1649 }
1650
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001651 /*
1652 * Put together the complete list of extensions to enable
1653 * This includes extensions requested via environment variables.
1654 */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001655 loader_enable_device_layers(icd, gpu_index);
1656
Courtney Goeltzenleuchteree3b16a2015-06-01 14:12:42 -06001657 /*
1658 * Load the libraries needed by the extensions on the
1659 * enabled extension list. This will build the
1660 * device instance chain terminating with the
1661 * selected device.
1662 */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001663 loader_activate_device_layers(*pDevice, icd, gpu_index,
1664 icd->app_extension_count[gpu_index],
1665 icd->app_extension_props[gpu_index]);
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001666 }
1667
1668 return res;
1669}
1670
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001671LOADER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char * pName)
1672{
Jon Ashburn07daee72015-05-21 18:13:33 -06001673 if (instance == VK_NULL_HANDLE)
1674 return NULL;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001675
Jon Ashburn07daee72015-05-21 18:13:33 -06001676 void *addr;
1677 /* get entrypoint addresses that are global (in the loader)*/
1678 addr = globalGetProcAddr(pName);
1679 if (addr)
1680 return addr;
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001681
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001682 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1683
1684 addr = debug_report_instance_gpa(ptr_instance, pName);
1685 if (addr) {
1686 return addr;
1687 }
1688
Jon Ashburn07daee72015-05-21 18:13:33 -06001689 /* return any extension global entrypoints */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001690 addr = wsi_lunarg_GetInstanceProcAddr(instance, pName);
Jon Ashburn07daee72015-05-21 18:13:33 -06001691 if (addr)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001692 return (ptr_instance->wsi_lunarg_enabled) ? addr : NULL;
Jon Ashburn07daee72015-05-21 18:13:33 -06001693
1694 /* return the instance dispatch table entrypoint for extensions */
1695 const VkLayerInstanceDispatchTable *disp_table = * (VkLayerInstanceDispatchTable **) instance;
1696 if (disp_table == NULL)
1697 return NULL;
1698
1699 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
1700 if (addr)
1701 return addr;
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001702
1703 return NULL;
1704}
1705
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001706LOADER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001707{
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001708 if (device == VK_NULL_HANDLE) {
1709 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001710 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001711
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001712 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001713
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001714 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1715 make sure the loader entrypoint is returned */
1716 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001717 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001718 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001719 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001720
Jon Ashburn07daee72015-05-21 18:13:33 -06001721 /* return any extension device entrypoints the loader knows about */
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001722 addr = wsi_lunarg_GetDeviceProcAddr(device, pName);
Jon Ashburn07daee72015-05-21 18:13:33 -06001723 if (addr)
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001724 return addr;
Jon Ashburn07daee72015-05-21 18:13:33 -06001725
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001726 /* return the dispatch table entrypoint for the fastest case */
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001727 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) device;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001728 if (disp_table == NULL)
1729 return NULL;
1730
Jon Ashburn27cd5842015-05-12 17:26:48 -06001731 addr = loader_lookup_device_dispatch_table(disp_table, pName);
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001732 if (addr)
1733 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001734 else {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001735 if (disp_table->GetDeviceProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001736 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001737 return disp_table->GetDeviceProcAddr(device, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001738 }
1739}
1740
Jon Ashburneceb13e2015-05-18 15:28:32 -06001741LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001742 VkExtensionInfoType infoType,
1743 uint32_t extensionIndex,
1744 size_t* pDataSize,
1745 void* pData)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001746{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001747 uint32_t *count;
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001748 VkResult res = VK_SUCCESS;
1749
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001750 /* Scan/discover all ICD libraries in a single-threaded manner */
1751 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001752
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001753 /* get layer libraries in a single-threaded manner */
1754 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001755
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001756 /* merge any duplicate extensions */
1757 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1758
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001759 if (pDataSize == NULL)
1760 return VK_ERROR_INVALID_POINTER;
1761
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001762 loader_platform_thread_lock_mutex(&loader_lock);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001763 switch (infoType) {
1764 case VK_EXTENSION_INFO_TYPE_COUNT:
1765 *pDataSize = sizeof(uint32_t);
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001766 if (pData == NULL) {
1767 loader_platform_thread_unlock_mutex(&loader_lock);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001768 return VK_SUCCESS;
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001769 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001770 count = (uint32_t *) pData;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001771 *count = loader.global_extensions.count;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001772 break;
1773 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1774 *pDataSize = sizeof(VkExtensionProperties);
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001775 if (pData == NULL) {
1776 loader_platform_thread_unlock_mutex(&loader_lock);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001777 return VK_SUCCESS;
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001778 }
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001779 if (extensionIndex >= loader.global_extensions.count)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001780 return VK_ERROR_INVALID_VALUE;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001781 memcpy((VkExtensionProperties *) pData,
1782 &loader.global_extensions.list[extensionIndex],
1783 sizeof(VkExtensionProperties));
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001784 break;
1785 default:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -06001786 loader_log(VK_DBG_REPORT_WARN_BIT, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001787 res = VK_ERROR_INVALID_VALUE;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001788 };
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001789 loader_platform_thread_unlock_mutex(&loader_lock);
1790 return res;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001791}
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001792
Jon Ashburn6301a0f2015-05-29 13:15:39 -06001793VkResult loader_GetPhysicalDeviceExtensionInfo(
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001794 VkPhysicalDevice gpu,
1795 VkExtensionInfoType infoType,
1796 uint32_t extensionIndex,
1797 size_t* pDataSize,
1798 void* pData)
1799{
1800 uint32_t gpu_index;
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001801 uint32_t *count;
Jon Ashburn128f9422015-05-28 19:16:58 -06001802 struct loader_icd *icd = loader_get_icd(gpu, &gpu_index);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001803
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001804 if (pDataSize == NULL)
1805 return VK_ERROR_INVALID_POINTER;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001806
Courtney Goeltzenleuchterd971b612015-06-17 20:51:59 -06001807 switch (infoType) {
1808 case VK_EXTENSION_INFO_TYPE_COUNT:
1809 *pDataSize = sizeof(uint32_t);
1810 if (pData == NULL)
1811 return VK_SUCCESS;
1812 count = (uint32_t *) pData;
1813 *count = icd->device_extension_cache[gpu_index].count;
1814 break;
1815 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1816 *pDataSize = sizeof(VkExtensionProperties);
1817 if (pData == NULL)
1818 return VK_SUCCESS;
1819 if (extensionIndex >= icd->device_extension_cache[gpu_index].count)
1820 return VK_ERROR_INVALID_VALUE;
1821 memcpy((VkExtensionProperties *) pData,
1822 &icd->device_extension_cache[gpu_index].list[extensionIndex],
1823 sizeof(VkExtensionProperties));
1824 break;
1825 default:
1826 return VK_ERROR_INVALID_VALUE;
1827 };
1828
1829 return VK_SUCCESS;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001830}
1831
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001832VkResult loader_GetMultiDeviceCompatibility(
1833 VkPhysicalDevice gpu0,
1834 VkPhysicalDevice gpu1,
1835 VkPhysicalDeviceCompatibilityInfo* pInfo)
1836{
1837 uint32_t gpu_index;
Jon Ashburn128f9422015-05-28 19:16:58 -06001838 struct loader_icd *icd = loader_get_icd(gpu0, &gpu_index);
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001839 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1840
1841 if (icd->GetMultiDeviceCompatibility)
1842 res = icd->GetMultiDeviceCompatibility(gpu0, gpu1, pInfo);
1843
1844 return res;
1845}