blob: 737bf66a873b33f0ec2f058196ee6b2fc313f07a [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 Ashburn27cd5842015-05-12 17:26:48 -060045#include "gpa_helper.h"
46#include "table_ops.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060047#include "vkIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070048// The following is #included again to catch certain OS-specific functions
49// being used:
50#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080051
Jon Ashburnb8358052014-11-18 09:06:04 -070052struct layer_name_pair {
53 char *layer_name;
54 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060055};
56
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060057struct extension_property {
58 char extName[VK_MAX_EXTENSION_NAME];
59 uint32_t version;
60 bool hosted; // does the extension reside in one driver/layer
61};
62
Jon Ashburn46d1f582015-01-28 11:01:35 -070063struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070064 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060065
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060066 PFN_vkCreateInstance CreateInstance;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060067 PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
Jon Ashburn46d1f582015-01-28 11:01:35 -070068 struct loader_scanned_icds *next;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060069 uint32_t extension_count;
70 struct extension_property *extensions;
Jon Ashburn46d1f582015-01-28 11:01:35 -070071};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060072
Jon Ashburn27cd5842015-05-12 17:26:48 -060073struct loader_struct loader = {0};
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080074
Jon Ashburn27cd5842015-05-12 17:26:48 -060075VkLayerInstanceDispatchTable instance_disp = {
76 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn27cd5842015-05-12 17:26:48 -060077 .CreateInstance = loader_CreateInstance,
78 .DestroyInstance = loader_DestroyInstance,
79 .EnumeratePhysicalDevices = loader_EnumeratePhysicalDevices,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060080 .GetPhysicalDeviceInfo = loader_GetPhysicalDeviceInfo,
81 .CreateDevice = loader_CreateDevice,
Jon Ashburneceb13e2015-05-18 15:28:32 -060082 .GetGlobalExtensionInfo = vkGetGlobalExtensionInfo,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060083 .GetPhysicalDeviceExtensionInfo = loader_GetPhysicalDeviceExtensionInfo,
84 .EnumerateLayers = loader_EnumerateLayers,
85 .GetMultiDeviceCompatibility = loader_GetMultiDeviceCompatibility,
Jon Ashburn27cd5842015-05-12 17:26:48 -060086 .DbgRegisterMsgCallback = loader_DbgRegisterMsgCallback,
87 .DbgUnregisterMsgCallback = loader_DbgUnregisterMsgCallback,
88 .DbgSetGlobalOption = loader_DbgSetGlobalOption,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060089 .GetDisplayInfoWSI = loader_GetDisplayInfoWSI
Jon Ashburn27cd5842015-05-12 17:26:48 -060090};
91
92LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
93LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
94LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070095
Ian Elliott4470a302015-02-17 10:33:47 -070096#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -060097char *loader_get_registry_string(const HKEY hive,
98 const LPCTSTR sub_key,
99 const char *value)
100{
101 DWORD access_flags = KEY_QUERY_VALUE;
102 DWORD value_type;
103 HKEY key;
Ian Elliottf851ddf2015-04-28 15:57:32 -0600104 VkResult rtn_value;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600105 char *rtn_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600106 DWORD rtn_len = 0;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600107 size_t allocated_len = 0;
108
109 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
110 if (rtn_value != ERROR_SUCCESS) {
111 // We didn't find the key. Try the 32-bit hive (where we've seen the
112 // key end up on some people's systems):
113 access_flags |= KEY_WOW64_32KEY;
114 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
115 if (rtn_value != ERROR_SUCCESS) {
116 // We still couldn't find the key, so give up:
117 return NULL;
118 }
119 }
120
121 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600122 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600123 if (rtn_value == ERROR_SUCCESS) {
124 // If we get to here, we found the key, and need to allocate memory
125 // large enough for rtn_str, and query again:
126 allocated_len = rtn_len + 4;
127 rtn_str = malloc(allocated_len);
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 // We added 4 extra bytes to rtn_str, so that we can ensure that
132 // the string is NULL-terminated (albeit, in a brute-force manner):
133 rtn_str[allocated_len-1] = '\0';
134 } else {
135 // This should never occur, but in case it does, clean up:
136 free(rtn_str);
137 rtn_str = NULL;
138 }
139 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
140
141 // Close the registry key that was opened:
142 RegCloseKey(key);
143
144 return rtn_str;
145}
146
147
Ian Elliott4470a302015-02-17 10:33:47 -0700148// For ICD developers, look in the registry, and look for an environment
149// variable for a path(s) where to find the ICD(s):
150static char *loader_get_registry_and_env(const char *env_var,
151 const char *registry_value)
152{
153 char *env_str = getenv(env_var);
154 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600155 char *registry_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600156 size_t registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700157 char *rtn_str = NULL;
158 size_t rtn_len;
159
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600160 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott06ebd752015-04-09 18:07:15 -0600161 "Software\\Vulkan",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600162 registry_value);
Ian Elliottf851ddf2015-04-28 15:57:32 -0600163 registry_len = (registry_str) ? (DWORD) strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700164
165 rtn_len = env_len + registry_len + 1;
166 if (rtn_len <= 2) {
167 // We found neither the desired registry value, nor the environment
168 // variable; return NULL:
169 return NULL;
170 } else {
171 // We found something, and so we need to allocate memory for the string
172 // to return:
173 rtn_str = malloc(rtn_len);
174 }
175
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600176 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700177 // We didn't find the desired registry value, and so we must have found
178 // only the environment variable:
179 _snprintf(rtn_str, rtn_len, "%s", env_str);
180 } else if (env_str != NULL) {
181 // We found both the desired registry value and the environment
182 // variable, so concatenate them both:
183 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
184 } else {
185 // We must have only found the desired registry value:
186 _snprintf(rtn_str, rtn_len, "%s", registry_str);
187 }
188
Ian Elliott2de26bc2015-04-03 13:13:01 -0600189 if (registry_str) {
190 free(registry_str);
191 }
Ian Elliott4470a302015-02-17 10:33:47 -0700192
193 return(rtn_str);
194}
195#endif // WIN32
196
197
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600198static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800199 const char *format, ...)
200{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800201 char msg[256];
202 va_list ap;
203 int ret;
204
205 va_start(ap, format);
206 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700207 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800208 msg[sizeof(msg) - 1] = '\0';
209 }
210 va_end(ap);
211
Jon Ashburnae053e92015-04-24 14:10:50 -0700212#if defined(WIN32)
213 OutputDebugString(msg);
Jon Ashburn1de39402015-05-05 16:20:46 -0600214#endif
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600215 fputs(msg, stderr);
216 fputc('\n', stderr);
Jon Ashburn1de39402015-05-05 16:20:46 -0600217
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800218}
219
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600220static bool has_extension(struct extension_property *exts, uint32_t count,
221 const char *name, bool must_be_hosted)
222{
223 uint32_t i;
224 for (i = 0; i < count; i++) {
225 if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted))
226 return true;
227 }
228 return false;
229}
230
231static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get,
232 uint32_t *count_out,
233 struct extension_property **props_out)
234{
235 uint32_t i, count, cur;
236 size_t siz = sizeof(count);
237 struct extension_property *ext_props;
238 VkExtensionProperties vk_prop;
239 VkResult res;
240
241 *count_out = 0;
242 *props_out = NULL;
243 res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
244 if (res != VK_SUCCESS) {
245 loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD");
246 return;
247 }
248 ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count);
249 if (ext_props == NULL) {
250 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD");
251 return;
252 }
253 siz = sizeof(VkExtensionProperties);
254 cur = 0;
255 for (i = 0; i < count; i++) {
256 res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop);
257 if (res == VK_SUCCESS) {
258 (ext_props + cur)->hosted = false;
259 (ext_props + cur)->version = vk_prop.version;
260 strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME);
261 (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
262 cur++;
263 }
264 *count_out = cur;
265 *props_out = ext_props;
266 }
267 return;
268}
269
270static void loader_init_ext_list()
271{
272 loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *);
273 loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity);
274 memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity);
275 loader.scanned_ext_list_count = 0;
276}
277
278#if 0 // currently no place to call this
279static void loader_destroy_ext_list()
280{
281 free(loader.scanned_ext_list);
282 loader.scanned_ext_list_capacity = 0;
283 loader.scanned_ext_list_count = 0;
284}
285#endif
286
287static void loader_add_to_ext_list(uint32_t count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600288 struct extension_property *prop_list,
289 bool is_layer_ext)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600290{
291 uint32_t i, j;
292 bool duplicate;
293 struct extension_property *cur_ext;
294
295 if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0)
296 loader_init_ext_list();
297
298 if (loader.scanned_ext_list == NULL)
299 return;
300
301 struct extension_property *ext_list, **ext_list_addr;
302
303 for (i = 0; i < count; i++) {
304 cur_ext = prop_list + i;
305
306 // look for duplicates or not
307 duplicate = false;
308 for (j = 0; j < loader.scanned_ext_list_count; j++) {
309 ext_list = loader.scanned_ext_list[j];
310 if (!strcmp(cur_ext->extName, ext_list->extName)) {
311 duplicate = true;
312 ext_list->hosted = false;
313 break;
314 }
315 }
316
317 // add to list at end
318 if (!duplicate) {
319 // check for enough capacity
320 if (loader.scanned_ext_list_count * sizeof(struct extension_property *)
321 >= loader.scanned_ext_list_capacity) {
322 // double capacity
323 loader.scanned_ext_list_capacity *= 2;
324 loader.scanned_ext_list = realloc(loader.scanned_ext_list,
325 loader.scanned_ext_list_capacity);
326 }
327 ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]);
328 *ext_list_addr = cur_ext;
329 cur_ext->hosted = true;
330 }
331
332 }
333}
334
Jon Ashburn27cd5842015-05-12 17:26:48 -0600335bool loader_is_extension_scanned(const char *name)
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600336{
337 uint32_t i;
338
339 for (i = 0; i < loader.scanned_ext_list_count; i++) {
340 if (!strcmp(name, loader.scanned_ext_list[i]->extName))
341 return true;
342 }
343 return false;
344}
345
Jon Ashburn27cd5842015-05-12 17:26:48 -0600346void loader_coalesce_extensions(void)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600347{
348 uint32_t i;
349 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
350
351 // traverse scanned icd list adding non-duplicate extensions to the list
352 while (icd_list != NULL) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600353 loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600354 icd_list = icd_list->next;
355 };
356
357 //Traverse layers list adding non-duplicate extensions to the list
358 for (i = 0; i < loader.scanned_layer_count; i++) {
359 loader_add_to_ext_list(loader.scanned_layers[i].extension_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600360 loader.scanned_layers[i].extensions, true);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600361 }
362}
363
364static void loader_icd_destroy(struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800365{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700366 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800367 free(icd);
368}
369
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600370static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800371{
372 struct loader_icd *icd;
373
374 icd = malloc(sizeof(*icd));
375 if (!icd)
376 return NULL;
377
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600378 memset(icd, 0, sizeof(*icd));
379
Jon Ashburn46d1f582015-01-28 11:01:35 -0700380 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800381
382 return icd;
383}
384
Jon Ashburn46888392015-01-29 15:45:51 -0700385static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
386 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800387{
388 struct loader_icd *icd;
389
Jon Ashburn46d1f582015-01-28 11:01:35 -0700390 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800391 if (!icd)
392 return NULL;
393
Chia-I Wu13a61a52014-08-04 11:18:20 +0800394 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700395 icd->next = ptr_inst->icds;
396 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800397
398 return icd;
399}
400
Jon Ashburn46d1f582015-01-28 11:01:35 -0700401static void loader_scanned_icd_add(const char *filename)
402{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700403 loader_platform_dl_handle handle;
Jon Ashburn3da71f22015-05-14 12:43:38 -0600404 void *fp_create_inst;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600405 void *fp_get_global_ext_info;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700406 struct loader_scanned_icds *new_node;
407
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700408 // Used to call: dlopen(filename, RTLD_LAZY);
409 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700410 if (!handle) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600411 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700412 return;
413 }
414
415#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600416 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700417 if (!func_ptr) { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600418 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700419 return; \
420 } \
421} while (0)
422
Jon Ashburn46888392015-01-29 15:45:51 -0700423 LOOKUP(fp_create_inst, CreateInstance);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600424 LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700425#undef LOOKUP
426
427 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
428 if (!new_node) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600429 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700430 return;
431 }
432
433 new_node->handle = handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700434 new_node->CreateInstance = fp_create_inst;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600435 new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
436 new_node->extension_count = 0;
437 new_node->extensions = NULL;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700438 new_node->next = loader.scanned_icd_list;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700439
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600440 loader.scanned_icd_list = new_node;
441
442 if (fp_get_global_ext_info) {
443 get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
444 &new_node->extension_count,
445 &new_node->extensions);
446 } else {
447 loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD");
448 }
449}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700450
Jon Ashburn3da71f22015-05-14 12:43:38 -0600451static void loader_icd_init_entrys(struct loader_icd *icd,
452 struct loader_scanned_icds *scanned_icds)
453{
454 /* initialize entrypoint function pointers */
455
456 #define LOOKUP(func) do { \
457 icd->func = (PFN_vk ##func) loader_platform_get_proc_address(scanned_icds->handle, "vk" #func); \
458 if (!icd->func) { \
459 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
460 return; \
461 } \
462 } while (0)
463
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600464 /* could change this to use GetInstanceProcAddr in driver instead of dlsym */
465 LOOKUP(GetDeviceProcAddr);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600466 LOOKUP(DestroyInstance);
467 LOOKUP(EnumeratePhysicalDevices);
468 LOOKUP(GetPhysicalDeviceInfo);
469 LOOKUP(CreateDevice);
470 LOOKUP(GetPhysicalDeviceExtensionInfo);
471 LOOKUP(EnumerateLayers);
472 LOOKUP(GetMultiDeviceCompatibility);
473 LOOKUP(DbgRegisterMsgCallback);
474 LOOKUP(DbgUnregisterMsgCallback);
475 LOOKUP(DbgSetGlobalOption);
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600476 LOOKUP(GetDisplayInfoWSI);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600477#undef LOOKUP
478
479 return;
480}
481
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600482/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600483 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600484 *
485 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600486 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
487 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600488 *
489 * \returns
490 * void; but side effect is to set loader_icd_scanned to true
491 */
Jon Ashburn27cd5842015-05-12 17:26:48 -0600492void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800493{
Ian Elliott4470a302015-02-17 10:33:47 -0700494 const char *p, *next;
495 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600496 DIR *sysdir;
497 struct dirent *dent;
498 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600499 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700500 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700501#if defined(WIN32)
502 bool must_free_libPaths;
503 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
504 DRIVER_PATH_REGISTRY_VALUE);
505 if (libPaths != NULL) {
506 must_free_libPaths = true;
507 } else {
508 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600509 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700510 }
511#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600512 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700513 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
514 libPaths = getenv(DRIVER_PATH_ENV);
515 }
516 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600517 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600518 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700519#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800520
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600521 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700522 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600523 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700524 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600525 next = p + len;
526 }
527 else {
Ian Elliott19628802015-02-04 12:06:46 -0700528 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600529 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
530 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600531 next++;
532 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800533
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700534 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
535 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600536 sysdir = opendir(p);
537 if (sysdir) {
538 dent = readdir(sysdir);
539 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600540 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
541 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700542 */
543 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600544 VK_DRIVER_LIBRARY_PREFIX,
545 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700546 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600547 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
548 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700549 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600550 VK_LIBRARY_SUFFIX,
551 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700552 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
553 loader_scanned_icd_add(icd_library);
554 }
555 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600556
557 dent = readdir(sysdir);
558 }
559 closedir(sysdir);
560 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800561 }
562
Ian Elliott4470a302015-02-17 10:33:47 -0700563#if defined(WIN32)
564 // Free any allocated memory:
565 if (must_free_libPaths) {
566 free(libPaths);
567 }
568#endif // WIN32
569
570 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700571 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800572}
573
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600574
Jon Ashburn27cd5842015-05-12 17:26:48 -0600575void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600576{
577 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700578 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600579 DIR *curdir;
580 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700581 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600582 char temp_str[1024];
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600583 uint32_t count;
584 PFN_vkGetGlobalExtensionInfo fp_get_ext;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600585
Ian Elliott4470a302015-02-17 10:33:47 -0700586#if defined(WIN32)
587 bool must_free_libPaths;
588 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
589 LAYERS_PATH_REGISTRY_VALUE);
590 if (libPaths != NULL) {
591 must_free_libPaths = true;
592 } else {
593 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600594 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600595 }
Ian Elliott4470a302015-02-17 10:33:47 -0700596#else // WIN32
597 if (geteuid() == getuid()) {
598 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700599 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700600 }
601 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600602 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700603 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700604#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600605
Ian Elliott4470a302015-02-17 10:33:47 -0700606 if (libPaths == NULL) {
607 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700608 return;
609 }
Ian Elliott4470a302015-02-17 10:33:47 -0700610 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700611 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700612 if (loader.layer_dirs == NULL) {
613 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700614 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700615 }
616 // Alloc passed, so we know there is enough space to hold the string, don't
617 // need strncpy
618 strcpy(loader.layer_dirs, libPaths);
619#if defined(WIN32)
620 // Free any allocated memory:
621 if (must_free_libPaths) {
622 free(libPaths);
623 must_free_libPaths = false;
624 }
625#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700626 libPaths = loader.layer_dirs;
627
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600628 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600629 for (i = 0; i < loader.scanned_layer_count; i++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600630 if (loader.scanned_layers[i].name != NULL)
631 free(loader.scanned_layers[i].name);
632 if (loader.scanned_layers[i].extensions != NULL)
633 free(loader.scanned_layers[i].extensions);
634 loader.scanned_layers[i].name = NULL;
635 loader.scanned_layers[i].extensions = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600636 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600637 loader.scanned_layer_count = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600638 count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600639
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600640 for (p = libPaths; *p; p = next) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600641 next = strchr(p, PATH_SEPERATOR);
642 if (next == NULL) {
643 len = (uint32_t) strlen(p);
644 next = p + len;
645 }
646 else {
647 len = (uint32_t) (next - p);
648 *(char *) next = '\0';
649 next++;
650 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600651
652 curdir = opendir(p);
653 if (curdir) {
654 dent = readdir(curdir);
655 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600656 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
657 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700658 */
659 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600660 VK_LAYER_LIBRARY_PREFIX,
661 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700662 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600663 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
664 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700665 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600666 VK_LIBRARY_SUFFIX,
667 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700668 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600669 snprintf(temp_str, sizeof(temp_str),
670 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700671 // Used to call: dlopen(temp_str, RTLD_LAZY)
672 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
673 dent = readdir(curdir);
674 continue;
675 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600676 if (count == MAX_LAYER_LIBRARIES) {
677 loader_log(VK_DBG_MSG_ERROR, 0,
678 "%s ignored: max layer libraries exceed",
679 temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700680 break;
681 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600682 fp_get_ext = loader_platform_get_proc_address(handle,
683 "vkGetGlobalExtensionInfo");
684
685 if (!fp_get_ext) {
686 loader_log(VK_DBG_MSG_WARNING, 0,
687 "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
688 temp_str);
689 dent = readdir(curdir);
690 loader_platform_close_library(handle);
691 continue;
692 }
693
694 loader.scanned_layers[count].name =
695 malloc(strlen(temp_str) + 1);
696 if (loader.scanned_layers[count].name == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600697 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700698 break;
699 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600700
701 get_global_extensions(fp_get_ext,
702 &loader.scanned_layers[count].extension_count,
703 &loader.scanned_layers[count].extensions);
704
705
706 strcpy(loader.scanned_layers[count].name, temp_str);
707 count++;
708 loader_platform_close_library(handle);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700709 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600710 }
711
712 dent = readdir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600713 } // while (dir_entry)
714 if (count == MAX_LAYER_LIBRARIES)
715 break;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600716 closedir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600717 } // if (curdir))
718 } // for (libpaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600719
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600720 loader.scanned_layer_count = count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600721 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600722}
723
Jon Ashburn27cd5842015-05-12 17:26:48 -0600724static void* VKAPI loader_gpa_device_internal(VkPhysicalDevice physDev, const char * pName)
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600725{
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600726 //physDev is not wrapped
Jon Ashburn27cd5842015-05-12 17:26:48 -0600727 if (physDev == VK_NULL_HANDLE) {
728 return NULL;
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600729 }
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600730 VkLayerDispatchTable* disp_table = * (VkLayerDispatchTable **) physDev;
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600731 void *addr;
732
733 if (disp_table == NULL)
734 return NULL;
735
Jon Ashburn27cd5842015-05-12 17:26:48 -0600736 addr = loader_lookup_device_dispatch_table(disp_table, pName);
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600737 if (addr)
738 return addr;
739 else {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600740 if (disp_table->GetDeviceProcAddr == NULL)
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600741 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600742 return disp_table->GetDeviceProcAddr(physDev, pName);
Jon Ashburn27cd5842015-05-12 17:26:48 -0600743 }
744}
745
746static void* VKAPI loader_gpa_instance_internal(VkInstance inst, const char * pName)
747{
748 // inst is not wrapped
749 if (inst == VK_NULL_HANDLE) {
750 return NULL;
751 }
752 VkLayerInstanceDispatchTable* disp_table = * (VkLayerInstanceDispatchTable **) inst;
753 void *addr;
754
755 if (disp_table == NULL)
756 return NULL;
757
758 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
759 if (addr)
760 return addr;
761 else {
762 if (disp_table->GetInstanceProcAddr == NULL)
763 return NULL;
764 return disp_table->GetInstanceProcAddr(inst, pName);
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600765 }
766}
767
Jon Ashburn1ed042c2015-05-01 18:00:33 -0600768struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600769{
Jon Ashburn4d9f4652015-04-08 21:33:34 -0600770 /*
771 * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else
772 * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so
773 * need to test it against the wrapped GPU's base object.
774 */
Jon Ashburn98bd4542015-01-29 16:44:24 -0700775 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
776 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
777 for (uint32_t i = 0; i < icd->gpu_count; i++)
Mike Stroyanb050c682015-04-17 12:36:38 -0600778 if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -0700779 *gpu_index = i;
780 return icd;
781 }
782 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600783 }
784 return NULL;
785}
786
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600787static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600788{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600789 if (icd->layer_count[gpu_index])
790 return true;
791 else
792 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600793}
794
Jon Ashburn27cd5842015-05-12 17:26:48 -0600795static void loader_init_device_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
Jon Ashburn19c25022015-04-14 14:14:48 -0600796 struct layer_name_pair * pLayerNames,
797 uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600798{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600799 if (!icd)
800 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600801
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600802 struct loader_layers *obj;
803 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600804 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600805 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600806 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600807 if (icd->layer_libs[gpu_index][j].lib_handle &&
808 !strcmp(icd->layer_libs[gpu_index][j].name,
809 (char *) pLayerNames[i].layer_name) &&
810 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600811 foundLib = true;
812 break;
813 }
814 }
815 if (!foundLib) {
816 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700817 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
818 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700819 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
820 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600821 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600822 continue;
823 } else {
Jon Ashburn27cd5842015-05-12 17:26:48 -0600824 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting device layer %s from library %s",
Jon Ashburn19c25022015-04-14 14:14:48 -0600825 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600826 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700827 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600828 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600829 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600830 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600831}
832
Jon Ashburn27cd5842015-05-12 17:26:48 -0600833static void loader_init_instance_layer_libs(struct loader_instance *inst,
834 struct layer_name_pair * pLayerNames,
835 uint32_t count)
836{
837 if (!inst)
838 return;
839
840 struct loader_layers *obj;
841 bool foundLib;
842 for (uint32_t i = 0; i < count; i++) {
843 foundLib = false;
844 for (uint32_t j = 0; j < inst->layer_count; j++) {
845 if (inst->layer_libs[j].lib_handle &&
846 !strcmp(inst->layer_libs[j].name,
847 (char *) pLayerNames[i].layer_name) &&
848 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
849 foundLib = true;
850 break;
851 }
852 }
853 if (!foundLib) {
854 obj = &(inst->layer_libs[i]);
855 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
856 obj->name[sizeof(obj->name) - 1] = '\0';
857 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
858 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
859 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
860 continue;
861 } else {
862 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting instance layer %s from library %s",
863 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
864 }
865 free(pLayerNames[i].layer_name);
866 inst->layer_count++;
867 }
868 }
869}
870
Jon Ashburn3d454752015-05-08 14:35:08 -0600871static bool find_layer_extension(const char *pExtName, uint32_t *out_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600872 char *lib_name[MAX_LAYER_LIBRARIES])
Jon Ashburnb8358052014-11-18 09:06:04 -0700873{
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700874 char *search_name;
Jon Ashburn19c25022015-04-14 14:14:48 -0600875 uint32_t j, found_count = 0;
876 bool must_be_hosted;
877 bool found = false;
Jon Ashburnb8358052014-11-18 09:06:04 -0700878
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700879 /*
880 * The loader provides the abstraction that make layers and extensions work via
881 * the currently defined extension mechanism. That is, when app queries for an extension
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600882 * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700883 * to see who implements that extension. Then, if the app enables the extension during
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600884 * vkCreateInstance the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700885 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700886
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600887 // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700888
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600889 for (j = 0; j < loader.scanned_layer_count; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600890
891 if (!strcmp("Validation", pExtName))
892 must_be_hosted = false;
893 else
894 must_be_hosted = true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600895 if (has_extension(loader.scanned_layers[j].extensions,
896 loader.scanned_layers[j].extension_count, pExtName,
Jon Ashburn19c25022015-04-14 14:14:48 -0600897 must_be_hosted)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700898
Jon Ashburn19c25022015-04-14 14:14:48 -0600899 found = true;
900 lib_name[found_count] = loader.scanned_layers[j].name;
901 found_count++;
902 } else {
903 // Extension not found in list for the layer, so test the layer name
904 // as if it is an extension name. Use default layer name based on
905 // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
906 char *pEnd;
907 size_t siz;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700908
Jon Ashburn19c25022015-04-14 14:14:48 -0600909 search_name = loader.scanned_layers[j].name;
910 search_name = basename(search_name);
911 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
912 pEnd = strrchr(search_name, '.');
913 siz = (int) (pEnd - search_name);
914 if (siz != strlen(pExtName))
915 continue;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700916
Jon Ashburn19c25022015-04-14 14:14:48 -0600917 if (strncmp(search_name, pExtName, siz) == 0) {
918 found = true;
919 lib_name[found_count] = loader.scanned_layers[j].name;
920 found_count++;
921 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700922 }
923 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600924
925 *out_count = found_count;
926 return found;
Jon Ashburnb8358052014-11-18 09:06:04 -0700927}
928
Jon Ashburn3d454752015-05-08 14:35:08 -0600929static uint32_t loader_get_layer_env(struct layer_name_pair *pLayerNames)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600930{
Ian Elliott4470a302015-02-17 10:33:47 -0700931 char *layerEnv;
Jon Ashburn19c25022015-04-14 14:14:48 -0600932 uint32_t i, len, found_count, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600933 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600934
Ian Elliott4470a302015-02-17 10:33:47 -0700935#if defined(WIN32)
936 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
937 LAYER_NAMES_REGISTRY_VALUE);
938#else // WIN32
939 layerEnv = getenv(LAYER_NAMES_ENV);
940#endif // WIN32
941 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600942 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700943 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600944 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700945 if (p == NULL) {
946#if defined(WIN32)
947 free(layerEnv);
948#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600949 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700950 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600951 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700952#if defined(WIN32)
953 free(layerEnv);
954#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600955 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600956
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600957 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600958 char *lib_name[MAX_LAYER_LIBRARIES];
959 //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700960 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600961 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700962 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600963 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700964 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700965 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600966 *(char *) next = '\0';
967 next++;
968 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600969 name = basename(p);
Jon Ashburn3d454752015-05-08 14:35:08 -0600970 if (!find_layer_extension(name, &found_count, lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600971 p = next;
972 continue;
973 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600974
Jon Ashburn19c25022015-04-14 14:14:48 -0600975 for (i = 0; i < found_count; i++) {
976 len = (uint32_t) strlen(name);
977 pLayerNames[count].layer_name = malloc(len + 1);
978 if (!pLayerNames[count].layer_name) {
979 free(pOrig);
980 return count;
981 }
982 strncpy((char *) pLayerNames[count].layer_name, name, len);
983 pLayerNames[count].layer_name[len] = '\0';
984 pLayerNames[count].lib_name = lib_name[i];
985 count++;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600986 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600987 p = next;
988
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700989 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600990
Jon Ashburnd09bd102014-10-22 21:15:26 -0600991 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600992 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600993}
994
Jon Ashburn3d454752015-05-08 14:35:08 -0600995static uint32_t loader_get_layer_libs(uint32_t ext_count, const char *const* ext_names, struct layer_name_pair **ppLayerNames)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600996{
Jon Ashburnb8358052014-11-18 09:06:04 -0700997 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn19c25022015-04-14 14:14:48 -0600998 char *lib_name[MAX_LAYER_LIBRARIES];
999 uint32_t found_count, count = 0;
1000 bool skip;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001001
1002 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -07001003 /* Load any layers specified in the environment first */
Jon Ashburn3d454752015-05-08 14:35:08 -06001004 count = loader_get_layer_env(layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001005
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001006 for (uint32_t i = 0; i < ext_count; i++) {
1007 const char *pExtName = ext_names[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001008
Jon Ashburn19c25022015-04-14 14:14:48 -06001009 skip = false;
1010 for (uint32_t j = 0; j < count; j++) {
1011 if (!strcmp(pExtName, layerNames[j].layer_name) ) {
1012 // Extension / Layer already on the list skip it
1013 skip = true;
1014 break;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001015 }
Jon Ashburn19c25022015-04-14 14:14:48 -06001016 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001017
Jon Ashburn3d454752015-05-08 14:35:08 -06001018 if (!skip && find_layer_extension(pExtName, &found_count, lib_name)) {
Jon Ashburn19c25022015-04-14 14:14:48 -06001019
1020 for (uint32_t j = 0; j < found_count; j++) {
1021 uint32_t len;
1022 len = (uint32_t) strlen(pExtName);
1023
1024
1025 layerNames[count].layer_name = malloc(len + 1);
1026 if (!layerNames[count].layer_name)
1027 return count;
1028 strncpy((char *) layerNames[count].layer_name, pExtName, len);
1029 layerNames[count].layer_name[len] = '\0';
1030 layerNames[count].lib_name = lib_name[j];
1031 count++;
1032 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001033 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001034 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001035
1036 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001037}
1038
Jon Ashburn27cd5842015-05-12 17:26:48 -06001039//TODO static void loader_deactivate_device_layer(device)
1040
1041static void loader_deactivate_instance_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001042{
1043 struct loader_icd *icd;
1044 struct loader_layers *libs;
1045
Jon Ashburn46d1f582015-01-28 11:01:35 -07001046 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001047 if (icd->gpus)
1048 free(icd->gpus);
1049 icd->gpus = NULL;
1050 if (icd->loader_dispatch)
1051 free(icd->loader_dispatch);
1052 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001053 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001054 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001055 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001056 libs = &(icd->layer_libs[j][i]);
1057 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001058 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001059 libs->lib_handle = NULL;
1060 }
Jon Ashburnd09bd102014-10-22 21:15:26 -06001061 if (icd->wrappedGpus[j])
1062 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001063 }
1064 icd->layer_count[j] = 0;
1065 }
1066 icd->gpu_count = 0;
1067 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06001068
1069 free(instance->wrappedInstance);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001070}
1071
Jon Ashburn27cd5842015-05-12 17:26:48 -06001072uint32_t loader_activate_instance_layers(struct loader_instance *inst)
1073{
1074 uint32_t count;
1075 struct layer_name_pair *pLayerNames;
1076 if (inst == NULL)
1077 return 0;
1078
1079 // NOTE inst is unwrapped at this point in time
1080 VkObject baseObj = (VkObject) inst;
1081 VkObject nextObj = (VkObject) inst;
1082 VkBaseLayerObject *nextInstObj;
1083 PFN_vkGetInstanceProcAddr nextGPA = loader_gpa_instance_internal;
1084
1085 count = loader_get_layer_libs(inst->extension_count, (const char *const*) inst->extension_names, &pLayerNames);
1086 if (!count)
1087 return 0;
1088 loader_init_instance_layer_libs(inst, pLayerNames, count);
1089
1090 inst->wrappedInstance = malloc(sizeof(VkBaseLayerObject) * count);
1091 if (! inst->wrappedInstance) {
1092 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Instance objects for layer");
1093 return 0;
1094 }
1095 for (int32_t i = count - 1; i >= 0; i--) {
1096 nextInstObj = (inst->wrappedInstance + i);
1097 nextInstObj->pGPA = nextGPA;
1098 nextInstObj->baseObject = baseObj;
1099 nextInstObj->nextObject = nextObj;
1100 nextObj = (VkObject) nextInstObj;
1101
1102 char funcStr[256];
1103 snprintf(funcStr, 256, "%sGetInstanceProcAddr",inst->layer_libs[i].name);
1104 if ((nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(inst->layer_libs[i].lib_handle, funcStr)) == NULL)
1105 nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(inst->layer_libs[i].lib_handle, "vkGetInstanceProcAddr");
1106 if (!nextGPA) {
1107 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetInstanceProcAddr in layer %s", inst->layer_libs[i].name);
1108 continue;
1109 }
1110
1111 if (i == 0) {
1112 loader_init_instance_dispatch_table(inst->disp, nextGPA, (VkInstance) nextObj);
1113 //Insert the new wrapped objects into the list with loader object at head
1114 nextInstObj = inst->wrappedInstance + inst->layer_count - 1;
1115 nextInstObj->nextObject = baseObj;
1116 nextInstObj->pGPA = loader_gpa_instance_internal;
1117 }
1118
1119 }
1120
1121 return inst->layer_count;
1122}
1123
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001124uint32_t loader_activate_device_layers(VkDevice device, struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001125{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001126 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -07001127 struct layer_name_pair *pLayerNames;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001128 if (!icd)
1129 return 0;
Jon Ashburn83a64252015-04-15 11:31:12 -06001130 assert(gpu_index < MAX_GPUS_FOR_LAYER);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001131
1132 /* activate any layer libraries */
1133 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001134 VkObject nextObj = (VkObject) device;
1135 VkObject baseObj = nextObj;
1136 VkBaseLayerObject *nextGpuObj;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001137 PFN_vkGetDeviceProcAddr nextGPA = loader_gpa_device_internal;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001138
Jon Ashburn3d454752015-05-08 14:35:08 -06001139 count = loader_get_layer_libs(ext_count, ext_names, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001140 if (!count)
1141 return 0;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001142 loader_init_device_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001143
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001144 icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001145 if (! icd->wrappedGpus[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001146 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn27cd5842015-05-12 17:26:48 -06001147 return 0;
1148 }
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001149 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -06001150 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001151 nextGpuObj->pGPA = nextGPA;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001152 nextGpuObj->baseObject = baseObj;
1153 nextGpuObj->nextObject = nextObj;
1154 nextObj = (VkObject) nextGpuObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001155
Jon Ashburn79113cc2014-12-01 14:22:40 -07001156 char funcStr[256];
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001157 snprintf(funcStr, 256, "%sGetDeviceProcAddr",icd->layer_libs[gpu_index][i].name);
1158 if ((nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1159 nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetDeviceProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001160 if (!nextGPA) {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001161 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetDeviceProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001162 continue;
1163 }
1164
Jon Ashburnf2610012014-10-24 15:48:55 -06001165 if (i == 0) {
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001166 loader_init_device_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) nextObj);
Jon Ashburnf2610012014-10-24 15:48:55 -06001167 //Insert the new wrapped objects into the list with loader object at head
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001168 nextGpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
1169 nextGpuObj->nextObject = baseObj;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001170 nextGpuObj->pGPA = icd->GetDeviceProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -06001171 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001172
1173 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001174 }
1175 else {
1176 //make sure requested Layers matches currently activated Layers
Jon Ashburn3d454752015-05-08 14:35:08 -06001177 count = loader_get_layer_libs(ext_count, ext_names, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001178 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -07001179 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001180 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001181 break;
1182 }
1183 }
1184 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001185 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001186 }
1187 }
1188 return icd->layer_count[gpu_index];
1189}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001190
Jon Ashburn27cd5842015-05-12 17:26:48 -06001191VkResult loader_CreateInstance(
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001192 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001193 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001194{
Jon Ashburn27cd5842015-05-12 17:26:48 -06001195 struct loader_instance *ptr_instance = (struct loader_instance *) pInstance;
Jon Ashburn46888392015-01-29 15:45:51 -07001196 struct loader_scanned_icds *scanned_icds;
1197 struct loader_icd *icd;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001198 VkResult res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001199
Jon Ashburn46888392015-01-29 15:45:51 -07001200 scanned_icds = loader.scanned_icd_list;
1201 while (scanned_icds) {
1202 icd = loader_icd_add(ptr_instance, scanned_icds);
1203 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001204 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn3da71f22015-05-14 12:43:38 -06001205 &(icd->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001206 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001207 {
1208 ptr_instance->icds = ptr_instance->icds->next;
1209 loader_icd_destroy(icd);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001210 icd->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001211 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001212 "ICD ignored: failed to CreateInstance on device");
Jon Ashburn3da71f22015-05-14 12:43:38 -06001213 } else
1214 {
1215 loader_icd_init_entrys(icd, scanned_icds);
Jon Ashburn46888392015-01-29 15:45:51 -07001216 }
1217 }
1218 scanned_icds = scanned_icds->next;
1219 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001220
Ian Elliotteb450762015-02-05 15:19:15 -07001221 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001222 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001223 }
Jon Ashburn46888392015-01-29 15:45:51 -07001224
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001225 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001226}
1227
Jon Ashburn27cd5842015-05-12 17:26:48 -06001228VkResult loader_DestroyInstance(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001229 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001230{
1231 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001232 struct loader_icd *icds = ptr_instance->icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001233 VkResult res;
Jon Ashburn340d6662015-04-08 15:49:00 -06001234 uint32_t i;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001235
1236 // Remove this instance from the list of instances:
1237 struct loader_instance *prev = NULL;
1238 struct loader_instance *next = loader.instances;
1239 while (next != NULL) {
1240 if (next == ptr_instance) {
1241 // Remove this instance from the list:
Jon Ashburn340d6662015-04-08 15:49:00 -06001242 for (i = 0; i < ptr_instance->extension_count; i++) {
1243 free(ptr_instance->extension_names[i]);
1244 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001245 if (prev)
1246 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001247 else
1248 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001249 break;
1250 }
1251 prev = next;
1252 next = next->next;
1253 }
1254 if (next == NULL) {
1255 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001256 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001257 }
1258
Jon Ashburn27cd5842015-05-12 17:26:48 -06001259 loader_deactivate_instance_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001260
Jon Ashburn3da71f22015-05-14 12:43:38 -06001261 while (icds) {
1262 if (icds->instance) {
1263 res = icds->DestroyInstance(icds->instance);
Tony Barbourf20f87b2015-04-22 09:02:32 -06001264 if (res != VK_SUCCESS)
1265 loader_log(VK_DBG_MSG_WARNING, 0,
1266 "ICD ignored: failed to DestroyInstance on device");
1267 }
Jon Ashburn3da71f22015-05-14 12:43:38 -06001268 icds->instance = VK_NULL_HANDLE;
1269 icds = icds->next;
Jon Ashburn46888392015-01-29 15:45:51 -07001270 }
1271
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001272 free(ptr_instance);
1273
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001274 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001275}
1276
Jon Ashburn27cd5842015-05-12 17:26:48 -06001277VkResult loader_EnumeratePhysicalDevices(
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001278 VkInstance instance,
1279 uint32_t* pPhysicalDeviceCount,
1280 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001281{
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001282 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1283 struct loader_icd *icd;
Jon Ashburn83a64252015-04-15 11:31:12 -06001284 uint32_t n, count = 0;
Tony Barbourf20f87b2015-04-22 09:02:32 -06001285 VkResult res = VK_ERROR_UNKNOWN;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001286
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001287 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001288 icd = ptr_instance->icds;
Jon Ashburn83a64252015-04-15 11:31:12 -06001289 if (pPhysicalDevices == NULL) {
1290 while (icd) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001291 res = icd->EnumeratePhysicalDevices(
1292 icd->instance,
Jon Ashburn83a64252015-04-15 11:31:12 -06001293 &n, NULL);
1294 if (res != VK_SUCCESS)
1295 return res;
1296 icd->gpu_count = n;
1297 count += n;
1298 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001299 }
1300
Jon Ashburn83a64252015-04-15 11:31:12 -06001301 ptr_instance->total_gpu_count = count;
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001302
Jon Ashburn83a64252015-04-15 11:31:12 -06001303 } else
1304 {
Tony Barbourd1c35722015-04-16 15:59:00 -06001305 VkPhysicalDevice* gpus;
Jon Ashburn83a64252015-04-15 11:31:12 -06001306 if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
1307 return VK_ERROR_INVALID_VALUE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001308 gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount);
Jon Ashburn83a64252015-04-15 11:31:12 -06001309 if (!gpus)
Tony Barbourd1c35722015-04-16 15:59:00 -06001310 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn83a64252015-04-15 11:31:12 -06001311 while (icd) {
1312 VkBaseLayerObject * wrapped_gpus;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001313 PFN_vkGetDeviceProcAddr get_proc_addr = icd->GetDeviceProcAddr;
Jon Ashburn83a64252015-04-15 11:31:12 -06001314
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001315 n = *pPhysicalDeviceCount;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001316 res = icd->EnumeratePhysicalDevices(
1317 icd->instance,
Jon Ashburn83a64252015-04-15 11:31:12 -06001318 &n,
1319 gpus);
1320 if (res == VK_SUCCESS && n) {
1321 wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1322 sizeof(VkBaseLayerObject));
1323 icd->gpus = wrapped_gpus;
1324 icd->gpu_count = n;
1325 icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1326 sizeof(VkLayerDispatchTable));
1327 for (unsigned int i = 0; i < n; i++) {
1328 (wrapped_gpus + i)->baseObject = gpus[i];
1329 (wrapped_gpus + i)->pGPA = get_proc_addr;
1330 (wrapped_gpus + i)->nextObject = gpus[i];
1331 memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
Jon Ashburnfbb4e252015-05-04 16:27:53 -06001332 loader_init_device_dispatch_table(icd->loader_dispatch + i,
Jon Ashburn83a64252015-04-15 11:31:12 -06001333 get_proc_addr, gpus[i]);
1334
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001335 loader_init_dispatch(gpus[i], ptr_instance->disp);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001336 }
1337
Jon Ashburn83a64252015-04-15 11:31:12 -06001338 count += n;
1339
1340 if (count >= *pPhysicalDeviceCount) {
1341 break;
1342 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001343 }
1344
Jon Ashburn83a64252015-04-15 11:31:12 -06001345 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001346 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001347 }
1348
Jon Ashburn83a64252015-04-15 11:31:12 -06001349 *pPhysicalDeviceCount = count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001350
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001351 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001352}
1353
Jon Ashburn3da71f22015-05-14 12:43:38 -06001354VkResult loader_GetPhysicalDeviceInfo(
1355 VkPhysicalDevice gpu,
1356 VkPhysicalDeviceInfoType infoType,
1357 size_t* pDataSize,
1358 void* pData)
1359{
1360 uint32_t gpu_index;
1361 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1362 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1363
1364 if (icd->GetPhysicalDeviceInfo)
1365 res = icd->GetPhysicalDeviceInfo(gpu, infoType, pDataSize, pData);
1366
1367 return res;
1368}
1369
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001370VkResult loader_CreateDevice(
1371 VkPhysicalDevice gpu,
1372 const VkDeviceCreateInfo* pCreateInfo,
1373 VkDevice* pDevice)
1374{
1375 uint32_t gpu_index;
1376 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1377 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1378 struct loader_instance *ptr_instance;
1379
1380 ptr_instance = loader.instances;
1381 if (icd->CreateDevice) {
1382 res = icd->CreateDevice(gpu, pCreateInfo, pDevice);
1383 if (res == VK_SUCCESS) {
1384 VkLayerDispatchTable *dev_disp = icd->loader_dispatch + gpu_index;
1385 loader_init_dispatch(*pDevice, dev_disp);
1386 }
1387 //TODO fix this extension parameters once support GetDeviceExtensionInfo()
1388 // don't know which instance we are on with this call
1389
1390 loader_activate_device_layers(*pDevice, icd, gpu_index, ptr_instance->extension_count,
1391 (const char *const*) ptr_instance->extension_names);
1392 }
1393
1394 return res;
1395}
1396
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001397LOADER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char * pName)
1398{
1399 if (instance != VK_NULL_HANDLE) {
1400
1401 /* return entrypoint addresses that are global (in the loader)*/
1402 return globalGetProcAddr(pName);
1403 }
1404
1405 return NULL;
1406}
1407
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001408LOADER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001409{
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001410 if (device == VK_NULL_HANDLE) {
1411 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001412 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001413
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001414 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001415
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001416 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1417 make sure the loader entrypoint is returned */
1418 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001419 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001420 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001421 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001422
1423 /* return the dispatch table entrypoint for the fastest case */
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001424 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) device;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001425 if (disp_table == NULL)
1426 return NULL;
1427
Jon Ashburn27cd5842015-05-12 17:26:48 -06001428 addr = loader_lookup_device_dispatch_table(disp_table, pName);
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001429 if (addr)
1430 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001431 else {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001432 if (disp_table->GetDeviceProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001433 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001434 return disp_table->GetDeviceProcAddr(device, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001435 }
1436}
1437
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001438//TODO make sure createInstance enables extensions that are valid (loader does)
1439//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1440
1441//TODO how is layer extension going to be enabled?
1442//Need to call createInstance on the layer or something
1443
Jon Ashburneceb13e2015-05-18 15:28:32 -06001444LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001445 VkExtensionInfoType infoType,
1446 uint32_t extensionIndex,
1447 size_t* pDataSize,
1448 void* pData)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001449{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001450 VkExtensionProperties *ext_props;
1451 uint32_t *count;
1452 /* Scan/discover all ICD libraries in a single-threaded manner */
1453 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001454
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001455 /* get layer libraries in a single-threaded manner */
1456 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001457
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001458 /* merge any duplicate extensions */
1459 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1460
1461
1462 if (pDataSize == NULL)
1463 return VK_ERROR_INVALID_POINTER;
1464
1465 switch (infoType) {
1466 case VK_EXTENSION_INFO_TYPE_COUNT:
1467 *pDataSize = sizeof(uint32_t);
1468 if (pData == NULL)
1469 return VK_SUCCESS;
1470 count = (uint32_t *) pData;
1471 *count = loader.scanned_ext_list_count;
1472 break;
1473 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1474 *pDataSize = sizeof(VkExtensionProperties);
1475 if (pData == NULL)
1476 return VK_SUCCESS;
1477 if (extensionIndex >= loader.scanned_ext_list_count)
1478 return VK_ERROR_INVALID_VALUE;
1479 ext_props = (VkExtensionProperties *) pData;
1480 ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1481 strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1482 , VK_MAX_EXTENSION_NAME);
1483 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1484 break;
1485 default:
1486 loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1487 return VK_ERROR_INVALID_VALUE;
1488 };
1489
1490 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001491}
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001492VkResult loader_GetPhysicalDeviceExtensionInfo(
1493 VkPhysicalDevice gpu,
1494 VkExtensionInfoType infoType,
1495 uint32_t extensionIndex,
1496 size_t* pDataSize,
1497 void* pData)
1498{
1499 uint32_t gpu_index;
1500 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1501 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001502
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001503 if (icd->GetPhysicalDeviceExtensionInfo)
1504 res = icd->GetPhysicalDeviceExtensionInfo(gpu, infoType, extensionIndex,
1505 pDataSize, pData);
1506
1507 return res;
1508}
1509
1510VkResult loader_EnumerateLayers(
1511 VkPhysicalDevice gpu,
1512 size_t maxStringSize,
1513 size_t* pLayerCount,
1514 char* const* pOutLayers,
1515 void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001516{
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001517 size_t maxLayerCount;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001518 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001519 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001520 char *lib_name;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001521 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001522 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001523 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001524 char layer_buf[16][256];
1525 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001526
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001527 if (pLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001528 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001529
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001530 maxLayerCount = *pLayerCount;
1531
Jon Ashburn46d1f582015-01-28 11:01:35 -07001532 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001533 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001534
Jon Ashburn1323eb72014-12-02 13:03:09 -07001535 for (int i = 0; i < 16; i++)
1536 layers[i] = &layer_buf[i][0];
1537
1538 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001539 lib_name = loader.scanned_layers[j].name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001540 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1541 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001542 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001543 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1544 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001545 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001546 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001547 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001548 lib_name = basename(lib_name);
1549 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001550 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001551 if (pEnd == NULL || siz <= 0)
1552 continue;
1553 cpyStr = malloc(siz);
1554 if (cpyStr == NULL) {
1555 free(cpyStr);
1556 continue;
1557 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001558 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001559 cpyStr[siz - 1] = '\0';
1560 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001561 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001562 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1563 pOutLayers[count][siz - 1] = '\0';
1564 count++;
1565 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001566 } else {
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001567 size_t cnt = 16; /* only allow 16 layers, for now */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001568 uint32_t n;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001569 VkResult res;
Ian Elliott19628802015-02-04 12:06:46 -07001570 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001571 res = fpEnumerateLayers((VkPhysicalDevice) NULL, n, &cnt, layers, (char *) icd->gpus + gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001572 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001573 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001574 continue;
1575 if (cnt + count > maxLayerCount)
1576 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001577 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001578 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1579 if (n > 0)
1580 pOutLayers[i - count][n - 1] = '\0';
1581 }
1582 count += cnt;
1583 }
1584 }
1585
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001586 *pLayerCount = count;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001587
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001588 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001589}
1590
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001591VkResult loader_GetMultiDeviceCompatibility(
1592 VkPhysicalDevice gpu0,
1593 VkPhysicalDevice gpu1,
1594 VkPhysicalDeviceCompatibilityInfo* pInfo)
1595{
1596 uint32_t gpu_index;
1597 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu0, &gpu_index);
1598 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1599
1600 if (icd->GetMultiDeviceCompatibility)
1601 res = icd->GetMultiDeviceCompatibility(gpu0, gpu1, pInfo);
1602
1603 return res;
1604}
1605
Jon Ashburn27cd5842015-05-12 17:26:48 -06001606VkResult loader_DbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001607{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001608 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001609 struct loader_instance *inst;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001610 VkResult res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001611
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001612 if (instance == VK_NULL_HANDLE)
1613 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001614
1615 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001616
Jon Ashburn98bd4542015-01-29 16:44:24 -07001617 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001618 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001619 break;
1620 }
1621
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001622 if (inst == VK_NULL_HANDLE)
1623 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001624
1625 for (icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001626 if (!icd->DbgRegisterMsgCallback)
1627 continue;
1628 res = icd->DbgRegisterMsgCallback(icd->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001629 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001630 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001631 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001632 }
1633
Jon Ashburnbb510252015-03-17 13:47:55 -06001634
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001635 /* roll back on errors */
1636 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001637 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001638 tmp = tmp->next) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001639 if (!tmp->DbgUnregisterMsgCallback)
1640 continue;
1641 tmp->DbgUnregisterMsgCallback(tmp->instance,
1642 pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001643 }
1644
1645 return res;
1646 }
1647
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001648 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001649}
1650
Jon Ashburn27cd5842015-05-12 17:26:48 -06001651VkResult loader_DbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001652{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001653 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001654 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001655 if (instance == VK_NULL_HANDLE)
1656 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001657
1658 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001659
Jon Ashburnbb510252015-03-17 13:47:55 -06001660 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001661 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001662 break;
1663 }
1664
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001665 if (inst == VK_NULL_HANDLE)
1666 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001667
1668 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001669 VkResult r;
1670 if (!icd->DbgUnregisterMsgCallback)
1671 continue;
1672 r = icd->DbgUnregisterMsgCallback(icd->instance, pfnMsgCallback);
1673 if (r != VK_SUCCESS) {
1674 res = r;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001675 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001676 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001677 return res;
1678}
1679
Jon Ashburn27cd5842015-05-12 17:26:48 -06001680VkResult loader_DbgSetGlobalOption(VkInstance instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001681{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001682 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001683 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001684 if (instance == VK_NULL_HANDLE)
1685 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001686
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001687 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001688
Jon Ashburnbb510252015-03-17 13:47:55 -06001689 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001690 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001691 break;
1692 }
1693
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001694 if (inst == VK_NULL_HANDLE)
1695 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001696 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001697 VkResult r;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001698 if (!icd->DbgSetGlobalOption)
1699 continue;
1700 r = icd->DbgSetGlobalOption(icd->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001701 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001702 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001703 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001704 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001705 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001706 }
1707
1708 return res;
1709}
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001710
1711VkResult loader_GetDisplayInfoWSI(
1712 VkDisplayWSI display,
1713 VkDisplayInfoTypeWSI infoType,
1714 size_t* pDataSize,
1715 void* pData)
1716{
1717 uint32_t gpu_index;
1718 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) display, &gpu_index);
1719 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1720
1721 if (icd->GetDisplayInfoWSI)
1722 res = icd->GetDisplayInfoWSI(display, infoType, pDataSize, pData);
1723
1724 return res;
1725}