blob: 1f7adc13cb7bfb49a284850028b86231a6e6234e [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 Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060048#include "vkIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070049// The following is #included again to catch certain OS-specific functions
50// being used:
51#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080052
Jon Ashburnb8358052014-11-18 09:06:04 -070053struct layer_name_pair {
54 char *layer_name;
55 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060056};
57
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060058struct extension_property {
59 char extName[VK_MAX_EXTENSION_NAME];
60 uint32_t version;
61 bool hosted; // does the extension reside in one driver/layer
62};
63
Jon Ashburn46d1f582015-01-28 11:01:35 -070064struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070065 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060066
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060067 PFN_vkCreateInstance CreateInstance;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060068 PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
Jon Ashburn46d1f582015-01-28 11:01:35 -070069 struct loader_scanned_icds *next;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -060070 uint32_t extension_count;
71 struct extension_property *extensions;
Jon Ashburn46d1f582015-01-28 11:01:35 -070072};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060073
Jon Ashburn27cd5842015-05-12 17:26:48 -060074struct loader_struct loader = {0};
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080075
Jon Ashburn27cd5842015-05-12 17:26:48 -060076VkLayerInstanceDispatchTable instance_disp = {
77 .GetInstanceProcAddr = vkGetInstanceProcAddr,
Jon Ashburn27cd5842015-05-12 17:26:48 -060078 .CreateInstance = loader_CreateInstance,
79 .DestroyInstance = loader_DestroyInstance,
80 .EnumeratePhysicalDevices = loader_EnumeratePhysicalDevices,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060081 .GetPhysicalDeviceInfo = loader_GetPhysicalDeviceInfo,
82 .CreateDevice = loader_CreateDevice,
Jon Ashburneceb13e2015-05-18 15:28:32 -060083 .GetGlobalExtensionInfo = vkGetGlobalExtensionInfo,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060084 .GetPhysicalDeviceExtensionInfo = loader_GetPhysicalDeviceExtensionInfo,
85 .EnumerateLayers = loader_EnumerateLayers,
86 .GetMultiDeviceCompatibility = loader_GetMultiDeviceCompatibility,
Jon Ashburn27cd5842015-05-12 17:26:48 -060087 .DbgRegisterMsgCallback = loader_DbgRegisterMsgCallback,
88 .DbgUnregisterMsgCallback = loader_DbgUnregisterMsgCallback,
89 .DbgSetGlobalOption = loader_DbgSetGlobalOption,
Jon Ashburn95a77ba2015-05-15 15:09:35 -060090 .GetDisplayInfoWSI = loader_GetDisplayInfoWSI
Jon Ashburn27cd5842015-05-12 17:26:48 -060091};
92
93LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
94LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
95LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070096
Ian Elliott4470a302015-02-17 10:33:47 -070097#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -060098char *loader_get_registry_string(const HKEY hive,
99 const LPCTSTR sub_key,
100 const char *value)
101{
102 DWORD access_flags = KEY_QUERY_VALUE;
103 DWORD value_type;
104 HKEY key;
Ian Elliottf851ddf2015-04-28 15:57:32 -0600105 VkResult rtn_value;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600106 char *rtn_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600107 DWORD rtn_len = 0;
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600108 size_t allocated_len = 0;
109
110 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
111 if (rtn_value != ERROR_SUCCESS) {
112 // We didn't find the key. Try the 32-bit hive (where we've seen the
113 // key end up on some people's systems):
114 access_flags |= KEY_WOW64_32KEY;
115 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
116 if (rtn_value != ERROR_SUCCESS) {
117 // We still couldn't find the key, so give up:
118 return NULL;
119 }
120 }
121
122 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600123 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600124 if (rtn_value == ERROR_SUCCESS) {
125 // If we get to here, we found the key, and need to allocate memory
126 // large enough for rtn_str, and query again:
127 allocated_len = rtn_len + 4;
128 rtn_str = malloc(allocated_len);
129 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliottf851ddf2015-04-28 15:57:32 -0600130 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600131 if (rtn_value == ERROR_SUCCESS) {
132 // We added 4 extra bytes to rtn_str, so that we can ensure that
133 // the string is NULL-terminated (albeit, in a brute-force manner):
134 rtn_str[allocated_len-1] = '\0';
135 } else {
136 // This should never occur, but in case it does, clean up:
137 free(rtn_str);
138 rtn_str = NULL;
139 }
140 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
141
142 // Close the registry key that was opened:
143 RegCloseKey(key);
144
145 return rtn_str;
146}
147
148
Ian Elliott4470a302015-02-17 10:33:47 -0700149// For ICD developers, look in the registry, and look for an environment
150// variable for a path(s) where to find the ICD(s):
151static char *loader_get_registry_and_env(const char *env_var,
152 const char *registry_value)
153{
154 char *env_str = getenv(env_var);
155 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600156 char *registry_str = NULL;
Tony Barbour18f71552015-04-22 11:36:22 -0600157 size_t registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700158 char *rtn_str = NULL;
159 size_t rtn_len;
160
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600161 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott06ebd752015-04-09 18:07:15 -0600162 "Software\\Vulkan",
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600163 registry_value);
Ian Elliottf851ddf2015-04-28 15:57:32 -0600164 registry_len = (registry_str) ? (DWORD) strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700165
166 rtn_len = env_len + registry_len + 1;
167 if (rtn_len <= 2) {
168 // We found neither the desired registry value, nor the environment
169 // variable; return NULL:
170 return NULL;
171 } else {
172 // We found something, and so we need to allocate memory for the string
173 // to return:
174 rtn_str = malloc(rtn_len);
175 }
176
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600177 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700178 // We didn't find the desired registry value, and so we must have found
179 // only the environment variable:
180 _snprintf(rtn_str, rtn_len, "%s", env_str);
181 } else if (env_str != NULL) {
182 // We found both the desired registry value and the environment
183 // variable, so concatenate them both:
184 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
185 } else {
186 // We must have only found the desired registry value:
187 _snprintf(rtn_str, rtn_len, "%s", registry_str);
188 }
189
Ian Elliott2de26bc2015-04-03 13:13:01 -0600190 if (registry_str) {
191 free(registry_str);
192 }
Ian Elliott4470a302015-02-17 10:33:47 -0700193
194 return(rtn_str);
195}
196#endif // WIN32
197
198
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600199static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800200 const char *format, ...)
201{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800202 char msg[256];
203 va_list ap;
204 int ret;
205
206 va_start(ap, format);
207 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700208 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800209 msg[sizeof(msg) - 1] = '\0';
210 }
211 va_end(ap);
212
Jon Ashburnae053e92015-04-24 14:10:50 -0700213#if defined(WIN32)
214 OutputDebugString(msg);
Jon Ashburn1de39402015-05-05 16:20:46 -0600215#endif
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600216 fputs(msg, stderr);
217 fputc('\n', stderr);
Jon Ashburn1de39402015-05-05 16:20:46 -0600218
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800219}
220
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600221static bool has_extension(struct extension_property *exts, uint32_t count,
222 const char *name, bool must_be_hosted)
223{
224 uint32_t i;
225 for (i = 0; i < count; i++) {
226 if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted))
227 return true;
228 }
229 return false;
230}
231
232static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get,
233 uint32_t *count_out,
234 struct extension_property **props_out)
235{
236 uint32_t i, count, cur;
237 size_t siz = sizeof(count);
238 struct extension_property *ext_props;
239 VkExtensionProperties vk_prop;
240 VkResult res;
241
242 *count_out = 0;
243 *props_out = NULL;
244 res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
245 if (res != VK_SUCCESS) {
246 loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD");
247 return;
248 }
249 ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count);
250 if (ext_props == NULL) {
251 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD");
252 return;
253 }
254 siz = sizeof(VkExtensionProperties);
255 cur = 0;
256 for (i = 0; i < count; i++) {
257 res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop);
258 if (res == VK_SUCCESS) {
259 (ext_props + cur)->hosted = false;
260 (ext_props + cur)->version = vk_prop.version;
261 strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME);
262 (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
263 cur++;
264 }
265 *count_out = cur;
266 *props_out = ext_props;
267 }
268 return;
269}
270
271static void loader_init_ext_list()
272{
273 loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *);
274 loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity);
275 memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity);
276 loader.scanned_ext_list_count = 0;
277}
278
279#if 0 // currently no place to call this
280static void loader_destroy_ext_list()
281{
282 free(loader.scanned_ext_list);
283 loader.scanned_ext_list_capacity = 0;
284 loader.scanned_ext_list_count = 0;
285}
286#endif
287
288static void loader_add_to_ext_list(uint32_t count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600289 struct extension_property *prop_list,
290 bool is_layer_ext)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600291{
292 uint32_t i, j;
293 bool duplicate;
294 struct extension_property *cur_ext;
295
296 if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0)
297 loader_init_ext_list();
298
299 if (loader.scanned_ext_list == NULL)
300 return;
301
302 struct extension_property *ext_list, **ext_list_addr;
303
304 for (i = 0; i < count; i++) {
305 cur_ext = prop_list + i;
306
307 // look for duplicates or not
308 duplicate = false;
309 for (j = 0; j < loader.scanned_ext_list_count; j++) {
310 ext_list = loader.scanned_ext_list[j];
311 if (!strcmp(cur_ext->extName, ext_list->extName)) {
312 duplicate = true;
313 ext_list->hosted = false;
314 break;
315 }
316 }
317
318 // add to list at end
319 if (!duplicate) {
320 // check for enough capacity
321 if (loader.scanned_ext_list_count * sizeof(struct extension_property *)
322 >= loader.scanned_ext_list_capacity) {
323 // double capacity
324 loader.scanned_ext_list_capacity *= 2;
325 loader.scanned_ext_list = realloc(loader.scanned_ext_list,
326 loader.scanned_ext_list_capacity);
327 }
328 ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]);
329 *ext_list_addr = cur_ext;
330 cur_ext->hosted = true;
331 }
332
333 }
334}
335
Jon Ashburn27cd5842015-05-12 17:26:48 -0600336bool loader_is_extension_scanned(const char *name)
Jon Ashburnfc2e38c2015-04-14 09:15:32 -0600337{
338 uint32_t i;
339
340 for (i = 0; i < loader.scanned_ext_list_count; i++) {
341 if (!strcmp(name, loader.scanned_ext_list[i]->extName))
342 return true;
343 }
344 return false;
345}
346
Jon Ashburn27cd5842015-05-12 17:26:48 -0600347void loader_coalesce_extensions(void)
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600348{
349 uint32_t i;
350 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
351
352 // traverse scanned icd list adding non-duplicate extensions to the list
353 while (icd_list != NULL) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600354 loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600355 icd_list = icd_list->next;
356 };
357
358 //Traverse layers list adding non-duplicate extensions to the list
359 for (i = 0; i < loader.scanned_layer_count; i++) {
360 loader_add_to_ext_list(loader.scanned_layers[i].extension_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600361 loader.scanned_layers[i].extensions, true);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600362 }
363}
364
365static void loader_icd_destroy(struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800366{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700367 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800368 free(icd);
369}
370
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600371static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800372{
373 struct loader_icd *icd;
374
375 icd = malloc(sizeof(*icd));
376 if (!icd)
377 return NULL;
378
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600379 memset(icd, 0, sizeof(*icd));
380
Jon Ashburn46d1f582015-01-28 11:01:35 -0700381 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800382
383 return icd;
384}
385
Jon Ashburn46888392015-01-29 15:45:51 -0700386static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
387 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800388{
389 struct loader_icd *icd;
390
Jon Ashburn46d1f582015-01-28 11:01:35 -0700391 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800392 if (!icd)
393 return NULL;
394
Chia-I Wu13a61a52014-08-04 11:18:20 +0800395 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700396 icd->next = ptr_inst->icds;
397 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800398
399 return icd;
400}
401
Jon Ashburn46d1f582015-01-28 11:01:35 -0700402static void loader_scanned_icd_add(const char *filename)
403{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700404 loader_platform_dl_handle handle;
Jon Ashburn3da71f22015-05-14 12:43:38 -0600405 void *fp_create_inst;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600406 void *fp_get_global_ext_info;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700407 struct loader_scanned_icds *new_node;
408
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700409 // Used to call: dlopen(filename, RTLD_LAZY);
410 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700411 if (!handle) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600412 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700413 return;
414 }
415
416#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600417 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700418 if (!func_ptr) { \
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600419 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700420 return; \
421 } \
422} while (0)
423
Jon Ashburn46888392015-01-29 15:45:51 -0700424 LOOKUP(fp_create_inst, CreateInstance);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600425 LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700426#undef LOOKUP
427
428 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
429 if (!new_node) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600430 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn46d1f582015-01-28 11:01:35 -0700431 return;
432 }
433
434 new_node->handle = handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700435 new_node->CreateInstance = fp_create_inst;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600436 new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
437 new_node->extension_count = 0;
438 new_node->extensions = NULL;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700439 new_node->next = loader.scanned_icd_list;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700440
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600441 loader.scanned_icd_list = new_node;
442
443 if (fp_get_global_ext_info) {
444 get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
445 &new_node->extension_count,
446 &new_node->extensions);
447 } else {
448 loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD");
449 }
450}
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700451
Jon Ashburn3da71f22015-05-14 12:43:38 -0600452static void loader_icd_init_entrys(struct loader_icd *icd,
453 struct loader_scanned_icds *scanned_icds)
454{
455 /* initialize entrypoint function pointers */
456
457 #define LOOKUP(func) do { \
458 icd->func = (PFN_vk ##func) loader_platform_get_proc_address(scanned_icds->handle, "vk" #func); \
459 if (!icd->func) { \
460 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
461 return; \
462 } \
463 } while (0)
464
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600465 /* could change this to use GetInstanceProcAddr in driver instead of dlsym */
466 LOOKUP(GetDeviceProcAddr);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600467 LOOKUP(DestroyInstance);
468 LOOKUP(EnumeratePhysicalDevices);
469 LOOKUP(GetPhysicalDeviceInfo);
470 LOOKUP(CreateDevice);
471 LOOKUP(GetPhysicalDeviceExtensionInfo);
472 LOOKUP(EnumerateLayers);
473 LOOKUP(GetMultiDeviceCompatibility);
474 LOOKUP(DbgRegisterMsgCallback);
475 LOOKUP(DbgUnregisterMsgCallback);
476 LOOKUP(DbgSetGlobalOption);
Jon Ashburn95a77ba2015-05-15 15:09:35 -0600477 LOOKUP(GetDisplayInfoWSI);
Jon Ashburn3da71f22015-05-14 12:43:38 -0600478#undef LOOKUP
479
480 return;
481}
482
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600483/**
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600484 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600485 *
486 * This function scans the default system path or path
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600487 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
488 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600489 *
490 * \returns
491 * void; but side effect is to set loader_icd_scanned to true
492 */
Jon Ashburn27cd5842015-05-12 17:26:48 -0600493void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800494{
Ian Elliott4470a302015-02-17 10:33:47 -0700495 const char *p, *next;
496 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600497 DIR *sysdir;
498 struct dirent *dent;
499 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600500 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700501 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700502#if defined(WIN32)
503 bool must_free_libPaths;
504 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
505 DRIVER_PATH_REGISTRY_VALUE);
506 if (libPaths != NULL) {
507 must_free_libPaths = true;
508 } else {
509 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600510 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700511 }
512#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600513 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700514 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
515 libPaths = getenv(DRIVER_PATH_ENV);
516 }
517 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600518 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600519 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700520#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800521
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600522 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700523 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600524 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700525 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600526 next = p + len;
527 }
528 else {
Ian Elliott19628802015-02-04 12:06:46 -0700529 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600530 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
531 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600532 next++;
533 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800534
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700535 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
536 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600537 sysdir = opendir(p);
538 if (sysdir) {
539 dent = readdir(sysdir);
540 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600541 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
542 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700543 */
544 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600545 VK_DRIVER_LIBRARY_PREFIX,
546 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700547 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600548 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
549 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700550 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600551 VK_LIBRARY_SUFFIX,
552 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700553 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
554 loader_scanned_icd_add(icd_library);
555 }
556 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600557
558 dent = readdir(sysdir);
559 }
560 closedir(sysdir);
561 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800562 }
563
Ian Elliott4470a302015-02-17 10:33:47 -0700564#if defined(WIN32)
565 // Free any allocated memory:
566 if (must_free_libPaths) {
567 free(libPaths);
568 }
569#endif // WIN32
570
571 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700572 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800573}
574
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600575
Jon Ashburn27cd5842015-05-12 17:26:48 -0600576void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600577{
578 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700579 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600580 DIR *curdir;
581 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700582 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600583 char temp_str[1024];
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600584 uint32_t count;
585 PFN_vkGetGlobalExtensionInfo fp_get_ext;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600586
Ian Elliott4470a302015-02-17 10:33:47 -0700587#if defined(WIN32)
588 bool must_free_libPaths;
589 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
590 LAYERS_PATH_REGISTRY_VALUE);
591 if (libPaths != NULL) {
592 must_free_libPaths = true;
593 } else {
594 must_free_libPaths = false;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600595 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600596 }
Ian Elliott4470a302015-02-17 10:33:47 -0700597#else // WIN32
598 if (geteuid() == getuid()) {
599 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700600 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700601 }
602 if (libPaths == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600603 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott4470a302015-02-17 10:33:47 -0700604 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700605#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600606
Ian Elliott4470a302015-02-17 10:33:47 -0700607 if (libPaths == NULL) {
608 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700609 return;
610 }
Ian Elliott4470a302015-02-17 10:33:47 -0700611 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700612 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700613 if (loader.layer_dirs == NULL) {
614 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700615 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700616 }
617 // Alloc passed, so we know there is enough space to hold the string, don't
618 // need strncpy
619 strcpy(loader.layer_dirs, libPaths);
620#if defined(WIN32)
621 // Free any allocated memory:
622 if (must_free_libPaths) {
623 free(libPaths);
624 must_free_libPaths = false;
625 }
626#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700627 libPaths = loader.layer_dirs;
628
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600629 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600630 for (i = 0; i < loader.scanned_layer_count; i++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600631 if (loader.scanned_layers[i].name != NULL)
632 free(loader.scanned_layers[i].name);
633 if (loader.scanned_layers[i].extensions != NULL)
634 free(loader.scanned_layers[i].extensions);
635 loader.scanned_layers[i].name = NULL;
636 loader.scanned_layers[i].extensions = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600637 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600638 loader.scanned_layer_count = 0;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600639 count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600640
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600641 for (p = libPaths; *p; p = next) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600642 next = strchr(p, PATH_SEPERATOR);
643 if (next == NULL) {
644 len = (uint32_t) strlen(p);
645 next = p + len;
646 }
647 else {
648 len = (uint32_t) (next - p);
649 *(char *) next = '\0';
650 next++;
651 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600652
653 curdir = opendir(p);
654 if (curdir) {
655 dent = readdir(curdir);
656 while (dent) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600657 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
658 * ending with VK_LIBRARY_SUFFIX
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700659 */
660 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600661 VK_LAYER_LIBRARY_PREFIX,
662 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700663 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600664 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
665 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700666 !strncmp(suf,
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600667 VK_LIBRARY_SUFFIX,
668 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700669 loader_platform_dl_handle handle;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600670 snprintf(temp_str, sizeof(temp_str),
671 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700672 // Used to call: dlopen(temp_str, RTLD_LAZY)
673 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
674 dent = readdir(curdir);
675 continue;
676 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600677 if (count == MAX_LAYER_LIBRARIES) {
678 loader_log(VK_DBG_MSG_ERROR, 0,
679 "%s ignored: max layer libraries exceed",
680 temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700681 break;
682 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600683 fp_get_ext = loader_platform_get_proc_address(handle,
684 "vkGetGlobalExtensionInfo");
685
686 if (!fp_get_ext) {
687 loader_log(VK_DBG_MSG_WARNING, 0,
688 "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
689 temp_str);
690 dent = readdir(curdir);
691 loader_platform_close_library(handle);
692 continue;
693 }
694
695 loader.scanned_layers[count].name =
696 malloc(strlen(temp_str) + 1);
697 if (loader.scanned_layers[count].name == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600698 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700699 break;
700 }
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600701
702 get_global_extensions(fp_get_ext,
703 &loader.scanned_layers[count].extension_count,
704 &loader.scanned_layers[count].extensions);
705
706
707 strcpy(loader.scanned_layers[count].name, temp_str);
708 count++;
709 loader_platform_close_library(handle);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700710 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600711 }
712
713 dent = readdir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600714 } // while (dir_entry)
715 if (count == MAX_LAYER_LIBRARIES)
716 break;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600717 closedir(curdir);
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600718 } // if (curdir))
719 } // for (libpaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600720
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600721 loader.scanned_layer_count = count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600722 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600723}
724
Jon Ashburn27cd5842015-05-12 17:26:48 -0600725static void* VKAPI loader_gpa_instance_internal(VkInstance inst, const char * pName)
726{
727 // inst is not wrapped
728 if (inst == VK_NULL_HANDLE) {
729 return NULL;
730 }
731 VkLayerInstanceDispatchTable* disp_table = * (VkLayerInstanceDispatchTable **) inst;
732 void *addr;
733
734 if (disp_table == NULL)
735 return NULL;
736
737 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
738 if (addr)
739 return addr;
740 else {
741 if (disp_table->GetInstanceProcAddr == NULL)
742 return NULL;
743 return disp_table->GetInstanceProcAddr(inst, pName);
Jon Ashburn3d526cb2015-04-13 18:10:06 -0600744 }
745}
746
Jon Ashburn1ed042c2015-05-01 18:00:33 -0600747struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600748{
Jon Ashburn4d9f4652015-04-08 21:33:34 -0600749 /*
750 * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else
751 * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so
752 * need to test it against the wrapped GPU's base object.
753 */
Jon Ashburn98bd4542015-01-29 16:44:24 -0700754 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
755 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
756 for (uint32_t i = 0; i < icd->gpu_count; i++)
Mike Stroyanb050c682015-04-17 12:36:38 -0600757 if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) {
Jon Ashburn98bd4542015-01-29 16:44:24 -0700758 *gpu_index = i;
759 return icd;
760 }
761 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600762 }
763 return NULL;
764}
765
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600766static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600767{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600768 if (icd->layer_count[gpu_index])
769 return true;
770 else
771 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600772}
773
Jon Ashburn27cd5842015-05-12 17:26:48 -0600774static void loader_init_device_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
Jon Ashburn19c25022015-04-14 14:14:48 -0600775 struct layer_name_pair * pLayerNames,
776 uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600777{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600778 if (!icd)
779 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600780
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600781 struct loader_layers *obj;
782 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600783 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600784 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600785 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600786 if (icd->layer_libs[gpu_index][j].lib_handle &&
787 !strcmp(icd->layer_libs[gpu_index][j].name,
788 (char *) pLayerNames[i].layer_name) &&
789 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600790 foundLib = true;
791 break;
792 }
793 }
794 if (!foundLib) {
795 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700796 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
797 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700798 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
799 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600800 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600801 continue;
802 } else {
Jon Ashburn27cd5842015-05-12 17:26:48 -0600803 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting device layer %s from library %s",
Jon Ashburn19c25022015-04-14 14:14:48 -0600804 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600805 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700806 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600807 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600808 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600809 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600810}
811
Jon Ashburn27cd5842015-05-12 17:26:48 -0600812static void loader_init_instance_layer_libs(struct loader_instance *inst,
813 struct layer_name_pair * pLayerNames,
814 uint32_t count)
815{
816 if (!inst)
817 return;
818
819 struct loader_layers *obj;
820 bool foundLib;
821 for (uint32_t i = 0; i < count; i++) {
822 foundLib = false;
823 for (uint32_t j = 0; j < inst->layer_count; j++) {
824 if (inst->layer_libs[j].lib_handle &&
825 !strcmp(inst->layer_libs[j].name,
826 (char *) pLayerNames[i].layer_name) &&
827 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
828 foundLib = true;
829 break;
830 }
831 }
832 if (!foundLib) {
833 obj = &(inst->layer_libs[i]);
834 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
835 obj->name[sizeof(obj->name) - 1] = '\0';
836 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
837 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
838 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
839 continue;
840 } else {
841 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting instance layer %s from library %s",
842 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
843 }
844 free(pLayerNames[i].layer_name);
845 inst->layer_count++;
846 }
847 }
848}
849
Jon Ashburn3d454752015-05-08 14:35:08 -0600850static bool find_layer_extension(const char *pExtName, uint32_t *out_count,
Jon Ashburn19c25022015-04-14 14:14:48 -0600851 char *lib_name[MAX_LAYER_LIBRARIES])
Jon Ashburnb8358052014-11-18 09:06:04 -0700852{
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700853 char *search_name;
Jon Ashburn19c25022015-04-14 14:14:48 -0600854 uint32_t j, found_count = 0;
855 bool must_be_hosted;
856 bool found = false;
Jon Ashburnb8358052014-11-18 09:06:04 -0700857
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700858 /*
859 * The loader provides the abstraction that make layers and extensions work via
860 * the currently defined extension mechanism. That is, when app queries for an extension
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600861 * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700862 * to see who implements that extension. Then, if the app enables the extension during
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600863 * vkCreateInstance the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700864 */
Jon Ashburnb8358052014-11-18 09:06:04 -0700865
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600866 // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700867
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600868 for (j = 0; j < loader.scanned_layer_count; j++) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600869
870 if (!strcmp("Validation", pExtName))
871 must_be_hosted = false;
872 else
873 must_be_hosted = true;
Jon Ashburn9fd4cc42015-04-10 14:33:07 -0600874 if (has_extension(loader.scanned_layers[j].extensions,
875 loader.scanned_layers[j].extension_count, pExtName,
Jon Ashburn19c25022015-04-14 14:14:48 -0600876 must_be_hosted)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700877
Jon Ashburn19c25022015-04-14 14:14:48 -0600878 found = true;
879 lib_name[found_count] = loader.scanned_layers[j].name;
880 found_count++;
881 } else {
882 // Extension not found in list for the layer, so test the layer name
883 // as if it is an extension name. Use default layer name based on
884 // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
885 char *pEnd;
886 size_t siz;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700887
Jon Ashburn19c25022015-04-14 14:14:48 -0600888 search_name = loader.scanned_layers[j].name;
889 search_name = basename(search_name);
890 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
891 pEnd = strrchr(search_name, '.');
892 siz = (int) (pEnd - search_name);
893 if (siz != strlen(pExtName))
894 continue;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700895
Jon Ashburn19c25022015-04-14 14:14:48 -0600896 if (strncmp(search_name, pExtName, siz) == 0) {
897 found = true;
898 lib_name[found_count] = loader.scanned_layers[j].name;
899 found_count++;
900 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700901 }
902 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600903
904 *out_count = found_count;
905 return found;
Jon Ashburnb8358052014-11-18 09:06:04 -0700906}
907
Jon Ashburn3d454752015-05-08 14:35:08 -0600908static uint32_t loader_get_layer_env(struct layer_name_pair *pLayerNames)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600909{
Ian Elliott4470a302015-02-17 10:33:47 -0700910 char *layerEnv;
Jon Ashburn19c25022015-04-14 14:14:48 -0600911 uint32_t i, len, found_count, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600912 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600913
Ian Elliott4470a302015-02-17 10:33:47 -0700914#if defined(WIN32)
915 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
916 LAYER_NAMES_REGISTRY_VALUE);
917#else // WIN32
918 layerEnv = getenv(LAYER_NAMES_ENV);
919#endif // WIN32
920 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600921 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700922 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600923 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700924 if (p == NULL) {
925#if defined(WIN32)
926 free(layerEnv);
927#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600928 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700929 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600930 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700931#if defined(WIN32)
932 free(layerEnv);
933#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600934 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600935
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600936 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600937 char *lib_name[MAX_LAYER_LIBRARIES];
938 //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700939 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600940 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700941 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600942 next = p + len;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700943 } else {
Ian Elliott19628802015-02-04 12:06:46 -0700944 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600945 *(char *) next = '\0';
946 next++;
947 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600948 name = basename(p);
Jon Ashburn3d454752015-05-08 14:35:08 -0600949 if (!find_layer_extension(name, &found_count, lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600950 p = next;
951 continue;
952 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600953
Jon Ashburn19c25022015-04-14 14:14:48 -0600954 for (i = 0; i < found_count; i++) {
955 len = (uint32_t) strlen(name);
956 pLayerNames[count].layer_name = malloc(len + 1);
957 if (!pLayerNames[count].layer_name) {
958 free(pOrig);
959 return count;
960 }
961 strncpy((char *) pLayerNames[count].layer_name, name, len);
962 pLayerNames[count].layer_name[len] = '\0';
963 pLayerNames[count].lib_name = lib_name[i];
964 count++;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600965 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600966 p = next;
967
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700968 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600969
Jon Ashburnd09bd102014-10-22 21:15:26 -0600970 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600971 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600972}
973
Jon Ashburn3d454752015-05-08 14:35:08 -0600974static 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 -0600975{
Jon Ashburnb8358052014-11-18 09:06:04 -0700976 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn19c25022015-04-14 14:14:48 -0600977 char *lib_name[MAX_LAYER_LIBRARIES];
978 uint32_t found_count, count = 0;
979 bool skip;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600980
981 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700982 /* Load any layers specified in the environment first */
Jon Ashburn3d454752015-05-08 14:35:08 -0600983 count = loader_get_layer_env(layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600984
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600985 for (uint32_t i = 0; i < ext_count; i++) {
986 const char *pExtName = ext_names[i];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600987
Jon Ashburn19c25022015-04-14 14:14:48 -0600988 skip = false;
989 for (uint32_t j = 0; j < count; j++) {
990 if (!strcmp(pExtName, layerNames[j].layer_name) ) {
991 // Extension / Layer already on the list skip it
992 skip = true;
993 break;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600994 }
Jon Ashburn19c25022015-04-14 14:14:48 -0600995 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -0700996
Jon Ashburn3d454752015-05-08 14:35:08 -0600997 if (!skip && find_layer_extension(pExtName, &found_count, lib_name)) {
Jon Ashburn19c25022015-04-14 14:14:48 -0600998
999 for (uint32_t j = 0; j < found_count; j++) {
1000 uint32_t len;
1001 len = (uint32_t) strlen(pExtName);
1002
1003
1004 layerNames[count].layer_name = malloc(len + 1);
1005 if (!layerNames[count].layer_name)
1006 return count;
1007 strncpy((char *) layerNames[count].layer_name, pExtName, len);
1008 layerNames[count].layer_name[len] = '\0';
1009 layerNames[count].lib_name = lib_name[j];
1010 count++;
1011 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001012 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001013 }
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001014
1015 return count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001016}
1017
Jon Ashburn27cd5842015-05-12 17:26:48 -06001018//TODO static void loader_deactivate_device_layer(device)
1019
1020static void loader_deactivate_instance_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001021{
1022 struct loader_icd *icd;
1023 struct loader_layers *libs;
1024
Jon Ashburn46d1f582015-01-28 11:01:35 -07001025 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001026 if (icd->gpus)
1027 free(icd->gpus);
1028 icd->gpus = NULL;
1029 if (icd->loader_dispatch)
1030 free(icd->loader_dispatch);
1031 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001032 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001033 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001034 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001035 libs = &(icd->layer_libs[j][i]);
1036 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001037 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001038 libs->lib_handle = NULL;
1039 }
Jon Ashburnd09bd102014-10-22 21:15:26 -06001040 if (icd->wrappedGpus[j])
1041 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001042 }
1043 icd->layer_count[j] = 0;
1044 }
1045 icd->gpu_count = 0;
1046 }
Jon Ashburn27cd5842015-05-12 17:26:48 -06001047
1048 free(instance->wrappedInstance);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001049}
1050
Jon Ashburn27cd5842015-05-12 17:26:48 -06001051uint32_t loader_activate_instance_layers(struct loader_instance *inst)
1052{
1053 uint32_t count;
1054 struct layer_name_pair *pLayerNames;
1055 if (inst == NULL)
1056 return 0;
1057
1058 // NOTE inst is unwrapped at this point in time
1059 VkObject baseObj = (VkObject) inst;
1060 VkObject nextObj = (VkObject) inst;
1061 VkBaseLayerObject *nextInstObj;
1062 PFN_vkGetInstanceProcAddr nextGPA = loader_gpa_instance_internal;
1063
1064 count = loader_get_layer_libs(inst->extension_count, (const char *const*) inst->extension_names, &pLayerNames);
1065 if (!count)
1066 return 0;
1067 loader_init_instance_layer_libs(inst, pLayerNames, count);
1068
1069 inst->wrappedInstance = malloc(sizeof(VkBaseLayerObject) * count);
1070 if (! inst->wrappedInstance) {
1071 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Instance objects for layer");
1072 return 0;
1073 }
1074 for (int32_t i = count - 1; i >= 0; i--) {
1075 nextInstObj = (inst->wrappedInstance + i);
1076 nextInstObj->pGPA = nextGPA;
1077 nextInstObj->baseObject = baseObj;
1078 nextInstObj->nextObject = nextObj;
1079 nextObj = (VkObject) nextInstObj;
1080
1081 char funcStr[256];
1082 snprintf(funcStr, 256, "%sGetInstanceProcAddr",inst->layer_libs[i].name);
1083 if ((nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(inst->layer_libs[i].lib_handle, funcStr)) == NULL)
1084 nextGPA = (PFN_vkGetInstanceProcAddr) loader_platform_get_proc_address(inst->layer_libs[i].lib_handle, "vkGetInstanceProcAddr");
1085 if (!nextGPA) {
1086 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetInstanceProcAddr in layer %s", inst->layer_libs[i].name);
1087 continue;
1088 }
1089
1090 if (i == 0) {
1091 loader_init_instance_dispatch_table(inst->disp, nextGPA, (VkInstance) nextObj);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001092 }
1093
1094 }
1095
1096 return inst->layer_count;
1097}
1098
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001099uint32_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 -06001100{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001101 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -07001102 struct layer_name_pair *pLayerNames;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001103 if (!icd)
1104 return 0;
Jon Ashburn83a64252015-04-15 11:31:12 -06001105 assert(gpu_index < MAX_GPUS_FOR_LAYER);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001106
1107 /* activate any layer libraries */
1108 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001109 VkObject nextObj = (VkObject) device;
1110 VkObject baseObj = nextObj;
1111 VkBaseLayerObject *nextGpuObj;
Jon Ashburn7c096122015-05-22 09:19:49 -06001112 PFN_vkGetDeviceProcAddr nextGPA = icd->GetDeviceProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001113
Jon Ashburn3d454752015-05-08 14:35:08 -06001114 count = loader_get_layer_libs(ext_count, ext_names, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001115 if (!count)
1116 return 0;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001117 loader_init_device_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001118
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001119 icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
Jon Ashburn27cd5842015-05-12 17:26:48 -06001120 if (! icd->wrappedGpus[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001121 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn27cd5842015-05-12 17:26:48 -06001122 return 0;
1123 }
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001124 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -06001125 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001126 nextGpuObj->pGPA = nextGPA;
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001127 nextGpuObj->baseObject = baseObj;
1128 nextGpuObj->nextObject = nextObj;
1129 nextObj = (VkObject) nextGpuObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001130
Jon Ashburn79113cc2014-12-01 14:22:40 -07001131 char funcStr[256];
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001132 snprintf(funcStr, 256, "%sGetDeviceProcAddr",icd->layer_libs[gpu_index][i].name);
1133 if ((nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1134 nextGPA = (PFN_vkGetDeviceProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetDeviceProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001135 if (!nextGPA) {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001136 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 -06001137 continue;
1138 }
1139
Jon Ashburnf2610012014-10-24 15:48:55 -06001140 if (i == 0) {
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001141 loader_init_device_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) nextObj);
Jon Ashburnf2610012014-10-24 15:48:55 -06001142 //Insert the new wrapped objects into the list with loader object at head
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001143 nextGpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
1144 nextGpuObj->nextObject = baseObj;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001145 nextGpuObj->pGPA = icd->GetDeviceProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -06001146 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001147
1148 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001149 }
1150 else {
1151 //make sure requested Layers matches currently activated Layers
Jon Ashburn3d454752015-05-08 14:35:08 -06001152 count = loader_get_layer_libs(ext_count, ext_names, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001153 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -07001154 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001155 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001156 break;
1157 }
1158 }
1159 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001160 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001161 }
1162 }
1163 return icd->layer_count[gpu_index];
1164}
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001165
Jon Ashburn27cd5842015-05-12 17:26:48 -06001166VkResult loader_CreateInstance(
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001167 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001168 VkInstance* pInstance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001169{
Jon Ashburneed0c002015-05-21 17:42:17 -06001170 struct loader_instance *ptr_instance = *(struct loader_instance **) pInstance;
Jon Ashburn46888392015-01-29 15:45:51 -07001171 struct loader_scanned_icds *scanned_icds;
1172 struct loader_icd *icd;
Jon Ashburn27cd5842015-05-12 17:26:48 -06001173 VkResult res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001174
Jon Ashburn46888392015-01-29 15:45:51 -07001175 scanned_icds = loader.scanned_icd_list;
1176 while (scanned_icds) {
1177 icd = loader_icd_add(ptr_instance, scanned_icds);
1178 if (icd) {
Jon Ashburnb317fad2015-04-04 14:52:07 -06001179 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn3da71f22015-05-14 12:43:38 -06001180 &(icd->instance));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001181 if (res != VK_SUCCESS)
Jon Ashburn46888392015-01-29 15:45:51 -07001182 {
1183 ptr_instance->icds = ptr_instance->icds->next;
1184 loader_icd_destroy(icd);
Jon Ashburn3da71f22015-05-14 12:43:38 -06001185 icd->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001186 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn46888392015-01-29 15:45:51 -07001187 "ICD ignored: failed to CreateInstance on device");
Jon Ashburn3da71f22015-05-14 12:43:38 -06001188 } else
1189 {
1190 loader_icd_init_entrys(icd, scanned_icds);
Jon Ashburn46888392015-01-29 15:45:51 -07001191 }
1192 }
1193 scanned_icds = scanned_icds->next;
1194 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001195
Ian Elliotteb450762015-02-05 15:19:15 -07001196 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001197 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliotteb450762015-02-05 15:19:15 -07001198 }
Jon Ashburn46888392015-01-29 15:45:51 -07001199
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001200 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001201}
1202
Jon Ashburn27cd5842015-05-12 17:26:48 -06001203VkResult loader_DestroyInstance(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001204 VkInstance instance)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001205{
1206 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001207 struct loader_icd *icds = ptr_instance->icds;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001208 VkResult res;
Jon Ashburn340d6662015-04-08 15:49:00 -06001209 uint32_t i;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001210
1211 // Remove this instance from the list of instances:
1212 struct loader_instance *prev = NULL;
1213 struct loader_instance *next = loader.instances;
1214 while (next != NULL) {
1215 if (next == ptr_instance) {
1216 // Remove this instance from the list:
Jon Ashburn340d6662015-04-08 15:49:00 -06001217 for (i = 0; i < ptr_instance->extension_count; i++) {
1218 free(ptr_instance->extension_names[i]);
1219 }
Jon Ashburnf3474392015-05-19 14:33:18 -06001220 free(ptr_instance->disp);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001221 if (prev)
1222 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001223 else
1224 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001225 break;
1226 }
1227 prev = next;
1228 next = next->next;
1229 }
1230 if (next == NULL) {
1231 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001232 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001233 }
1234
Jon Ashburn27cd5842015-05-12 17:26:48 -06001235 loader_deactivate_instance_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001236
Jon Ashburn3da71f22015-05-14 12:43:38 -06001237 while (icds) {
1238 if (icds->instance) {
1239 res = icds->DestroyInstance(icds->instance);
Tony Barbourf20f87b2015-04-22 09:02:32 -06001240 if (res != VK_SUCCESS)
1241 loader_log(VK_DBG_MSG_WARNING, 0,
1242 "ICD ignored: failed to DestroyInstance on device");
1243 }
Jon Ashburn3da71f22015-05-14 12:43:38 -06001244 icds->instance = VK_NULL_HANDLE;
1245 icds = icds->next;
Jon Ashburn46888392015-01-29 15:45:51 -07001246 }
1247
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001248 free(ptr_instance);
1249
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001250 return VK_SUCCESS;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001251}
1252
Jon Ashburn27cd5842015-05-12 17:26:48 -06001253VkResult loader_EnumeratePhysicalDevices(
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001254 VkInstance instance,
1255 uint32_t* pPhysicalDeviceCount,
1256 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001257{
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001258 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1259 struct loader_icd *icd;
Jon Ashburn83a64252015-04-15 11:31:12 -06001260 uint32_t n, count = 0;
Tony Barbourf20f87b2015-04-22 09:02:32 -06001261 VkResult res = VK_ERROR_UNKNOWN;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001262
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001263 //in spirit of VK don't error check on the instance parameter
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001264 icd = ptr_instance->icds;
Jon Ashburn83a64252015-04-15 11:31:12 -06001265 if (pPhysicalDevices == NULL) {
1266 while (icd) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001267 res = icd->EnumeratePhysicalDevices(
1268 icd->instance,
Jon Ashburn83a64252015-04-15 11:31:12 -06001269 &n, NULL);
1270 if (res != VK_SUCCESS)
1271 return res;
1272 icd->gpu_count = n;
1273 count += n;
1274 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001275 }
1276
Jon Ashburn83a64252015-04-15 11:31:12 -06001277 ptr_instance->total_gpu_count = count;
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001278
Jon Ashburn83a64252015-04-15 11:31:12 -06001279 } else
1280 {
Tony Barbourd1c35722015-04-16 15:59:00 -06001281 VkPhysicalDevice* gpus;
Jon Ashburn83a64252015-04-15 11:31:12 -06001282 if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
1283 return VK_ERROR_INVALID_VALUE;
Tony Barbourd1c35722015-04-16 15:59:00 -06001284 gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount);
Jon Ashburn83a64252015-04-15 11:31:12 -06001285 if (!gpus)
Tony Barbourd1c35722015-04-16 15:59:00 -06001286 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn83a64252015-04-15 11:31:12 -06001287 while (icd) {
1288 VkBaseLayerObject * wrapped_gpus;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001289 PFN_vkGetDeviceProcAddr get_proc_addr = icd->GetDeviceProcAddr;
Jon Ashburn83a64252015-04-15 11:31:12 -06001290
Courtney Goeltzenleuchter5e41f1d2015-04-20 12:48:54 -06001291 n = *pPhysicalDeviceCount;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001292 res = icd->EnumeratePhysicalDevices(
1293 icd->instance,
Jon Ashburn83a64252015-04-15 11:31:12 -06001294 &n,
1295 gpus);
1296 if (res == VK_SUCCESS && n) {
1297 wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1298 sizeof(VkBaseLayerObject));
1299 icd->gpus = wrapped_gpus;
1300 icd->gpu_count = n;
1301 icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1302 sizeof(VkLayerDispatchTable));
1303 for (unsigned int i = 0; i < n; i++) {
1304 (wrapped_gpus + i)->baseObject = gpus[i];
1305 (wrapped_gpus + i)->pGPA = get_proc_addr;
1306 (wrapped_gpus + i)->nextObject = gpus[i];
1307 memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
Jon Ashburnfbb4e252015-05-04 16:27:53 -06001308 loader_init_device_dispatch_table(icd->loader_dispatch + i,
Jon Ashburn83a64252015-04-15 11:31:12 -06001309 get_proc_addr, gpus[i]);
1310
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001311 loader_init_dispatch(gpus[i], ptr_instance->disp);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001312 }
1313
Jon Ashburn83a64252015-04-15 11:31:12 -06001314 count += n;
1315
1316 if (count >= *pPhysicalDeviceCount) {
1317 break;
1318 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001319 }
1320
Jon Ashburn83a64252015-04-15 11:31:12 -06001321 icd = icd->next;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001322 }
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001323 }
1324
Jon Ashburn83a64252015-04-15 11:31:12 -06001325 *pPhysicalDeviceCount = count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001326
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001327 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001328}
1329
Jon Ashburn3da71f22015-05-14 12:43:38 -06001330VkResult loader_GetPhysicalDeviceInfo(
1331 VkPhysicalDevice gpu,
1332 VkPhysicalDeviceInfoType infoType,
1333 size_t* pDataSize,
1334 void* pData)
1335{
1336 uint32_t gpu_index;
1337 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1338 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1339
1340 if (icd->GetPhysicalDeviceInfo)
1341 res = icd->GetPhysicalDeviceInfo(gpu, infoType, pDataSize, pData);
1342
1343 return res;
1344}
1345
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001346VkResult loader_CreateDevice(
1347 VkPhysicalDevice gpu,
1348 const VkDeviceCreateInfo* pCreateInfo,
1349 VkDevice* pDevice)
1350{
1351 uint32_t gpu_index;
1352 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1353 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1354 struct loader_instance *ptr_instance;
1355
1356 ptr_instance = loader.instances;
1357 if (icd->CreateDevice) {
1358 res = icd->CreateDevice(gpu, pCreateInfo, pDevice);
1359 if (res == VK_SUCCESS) {
1360 VkLayerDispatchTable *dev_disp = icd->loader_dispatch + gpu_index;
1361 loader_init_dispatch(*pDevice, dev_disp);
1362 }
1363 //TODO fix this extension parameters once support GetDeviceExtensionInfo()
1364 // don't know which instance we are on with this call
1365
1366 loader_activate_device_layers(*pDevice, icd, gpu_index, ptr_instance->extension_count,
1367 (const char *const*) ptr_instance->extension_names);
1368 }
1369
1370 return res;
1371}
1372
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001373LOADER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char * pName)
1374{
Jon Ashburn07daee72015-05-21 18:13:33 -06001375 if (instance == VK_NULL_HANDLE)
1376 return NULL;
1377 void *addr;
1378 /* get entrypoint addresses that are global (in the loader)*/
1379 addr = globalGetProcAddr(pName);
1380 if (addr)
1381 return addr;
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001382
Jon Ashburn07daee72015-05-21 18:13:33 -06001383 /* return any extension global entrypoints */
Jon Ashburn35842a32015-05-22 09:51:03 -06001384 bool wsi_enabled;
1385 addr = wsi_lunarg_GetInstanceProcAddr(instance, pName, &wsi_enabled);
Jon Ashburn07daee72015-05-21 18:13:33 -06001386 if (addr)
Jon Ashburn35842a32015-05-22 09:51:03 -06001387 return (wsi_enabled) ? addr : NULL;
Jon Ashburn07daee72015-05-21 18:13:33 -06001388
1389 /* return the instance dispatch table entrypoint for extensions */
1390 const VkLayerInstanceDispatchTable *disp_table = * (VkLayerInstanceDispatchTable **) instance;
1391 if (disp_table == NULL)
1392 return NULL;
1393
1394 addr = loader_lookup_instance_dispatch_table(disp_table, pName);
1395 if (addr)
1396 return addr;
Jon Ashburnb0fbe912015-05-06 10:15:07 -06001397
1398 return NULL;
1399}
1400
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001401LOADER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char * pName)
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001402{
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001403 if (device == VK_NULL_HANDLE) {
1404 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001405 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001406
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001407 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001408
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001409 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1410 make sure the loader entrypoint is returned */
1411 addr = loader_non_passthrough_gpa(pName);
Ian Elliotte19c9152015-04-15 12:53:19 -06001412 if (addr) {
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001413 return addr;
Ian Elliotte19c9152015-04-15 12:53:19 -06001414 }
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001415
Jon Ashburn07daee72015-05-21 18:13:33 -06001416 /* return any extension device entrypoints the loader knows about */
Jon Ashburn35842a32015-05-22 09:51:03 -06001417 bool wsi_enabled;
1418 addr = wsi_lunarg_GetDeviceProcAddr(device, pName, &wsi_enabled);
Jon Ashburn07daee72015-05-21 18:13:33 -06001419 if (addr)
Jon Ashburn35842a32015-05-22 09:51:03 -06001420 return (wsi_enabled) ? addr : NULL;
Jon Ashburn07daee72015-05-21 18:13:33 -06001421
Jon Ashburn3d526cb2015-04-13 18:10:06 -06001422 /* return the dispatch table entrypoint for the fastest case */
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001423 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) device;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001424 if (disp_table == NULL)
1425 return NULL;
1426
Jon Ashburn27cd5842015-05-12 17:26:48 -06001427 addr = loader_lookup_device_dispatch_table(disp_table, pName);
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001428 if (addr)
1429 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001430 else {
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001431 if (disp_table->GetDeviceProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001432 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -06001433 return disp_table->GetDeviceProcAddr(device, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001434 }
1435}
1436
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001437//TODO make sure createInstance enables extensions that are valid (loader does)
1438//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1439
1440//TODO how is layer extension going to be enabled?
1441//Need to call createInstance on the layer or something
1442
Jon Ashburneceb13e2015-05-18 15:28:32 -06001443LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001444 VkExtensionInfoType infoType,
1445 uint32_t extensionIndex,
1446 size_t* pDataSize,
1447 void* pData)
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001448{
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001449 VkExtensionProperties *ext_props;
1450 uint32_t *count;
1451 /* Scan/discover all ICD libraries in a single-threaded manner */
1452 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001453
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001454 /* get layer libraries in a single-threaded manner */
1455 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001456
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001457 /* merge any duplicate extensions */
1458 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1459
1460
1461 if (pDataSize == NULL)
1462 return VK_ERROR_INVALID_POINTER;
1463
1464 switch (infoType) {
1465 case VK_EXTENSION_INFO_TYPE_COUNT:
1466 *pDataSize = sizeof(uint32_t);
1467 if (pData == NULL)
1468 return VK_SUCCESS;
1469 count = (uint32_t *) pData;
1470 *count = loader.scanned_ext_list_count;
1471 break;
1472 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1473 *pDataSize = sizeof(VkExtensionProperties);
1474 if (pData == NULL)
1475 return VK_SUCCESS;
1476 if (extensionIndex >= loader.scanned_ext_list_count)
1477 return VK_ERROR_INVALID_VALUE;
1478 ext_props = (VkExtensionProperties *) pData;
1479 ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1480 strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1481 , VK_MAX_EXTENSION_NAME);
1482 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1483 break;
1484 default:
1485 loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1486 return VK_ERROR_INVALID_VALUE;
1487 };
1488
1489 return VK_SUCCESS;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001490}
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001491VkResult loader_GetPhysicalDeviceExtensionInfo(
1492 VkPhysicalDevice gpu,
1493 VkExtensionInfoType infoType,
1494 uint32_t extensionIndex,
1495 size_t* pDataSize,
1496 void* pData)
1497{
1498 uint32_t gpu_index;
1499 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1500 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001501
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001502 if (icd->GetPhysicalDeviceExtensionInfo)
1503 res = icd->GetPhysicalDeviceExtensionInfo(gpu, infoType, extensionIndex,
1504 pDataSize, pData);
1505
1506 return res;
1507}
1508
1509VkResult loader_EnumerateLayers(
1510 VkPhysicalDevice gpu,
1511 size_t maxStringSize,
1512 size_t* pLayerCount,
1513 char* const* pOutLayers,
1514 void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001515{
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001516 size_t maxLayerCount;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001517 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001518 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001519 char *lib_name;
Jon Ashburnbacb0f52015-04-06 10:58:22 -06001520 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001521 loader_platform_dl_handle handle;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001522 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001523 char layer_buf[16][256];
1524 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001525
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001526 if (pLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001527 return VK_ERROR_INVALID_POINTER;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001528
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001529 maxLayerCount = *pLayerCount;
1530
Jon Ashburn46d1f582015-01-28 11:01:35 -07001531 if (!icd)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001532 return VK_ERROR_UNAVAILABLE;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001533
Jon Ashburn1323eb72014-12-02 13:03:09 -07001534 for (int i = 0; i < 16; i++)
1535 layers[i] = &layer_buf[i][0];
1536
1537 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
Jon Ashburn9fd4cc42015-04-10 14:33:07 -06001538 lib_name = loader.scanned_layers[j].name;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001539 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1540 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001541 continue;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001542 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1543 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001544 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001545 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001546 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001547 lib_name = basename(lib_name);
1548 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001549 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001550 if (pEnd == NULL || siz <= 0)
1551 continue;
1552 cpyStr = malloc(siz);
1553 if (cpyStr == NULL) {
1554 free(cpyStr);
1555 continue;
1556 }
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001557 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001558 cpyStr[siz - 1] = '\0';
1559 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001560 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001561 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1562 pOutLayers[count][siz - 1] = '\0';
1563 count++;
1564 free(cpyStr);
Courtney Goeltzenleuchter499b3ba2015-02-27 15:19:33 -07001565 } else {
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001566 size_t cnt = 16; /* only allow 16 layers, for now */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001567 uint32_t n;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001568 VkResult res;
Ian Elliott19628802015-02-04 12:06:46 -07001569 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001570 res = fpEnumerateLayers((VkPhysicalDevice) NULL, n, &cnt, layers, (char *) icd->gpus + gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001571 loader_platform_close_library(handle);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001572 if (res != VK_SUCCESS)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001573 continue;
1574 if (cnt + count > maxLayerCount)
1575 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001576 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001577 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1578 if (n > 0)
1579 pOutLayers[i - count][n - 1] = '\0';
1580 }
1581 count += cnt;
1582 }
1583 }
1584
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -06001585 *pLayerCount = count;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001586
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001587 return VK_SUCCESS;
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001588}
1589
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001590VkResult loader_GetMultiDeviceCompatibility(
1591 VkPhysicalDevice gpu0,
1592 VkPhysicalDevice gpu1,
1593 VkPhysicalDeviceCompatibilityInfo* pInfo)
1594{
1595 uint32_t gpu_index;
1596 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu0, &gpu_index);
1597 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1598
1599 if (icd->GetMultiDeviceCompatibility)
1600 res = icd->GetMultiDeviceCompatibility(gpu0, gpu1, pInfo);
1601
1602 return res;
1603}
1604
Jon Ashburn27cd5842015-05-12 17:26:48 -06001605VkResult loader_DbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001606{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001607 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001608 struct loader_instance *inst;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001609 VkResult res;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001610
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001611 if (instance == VK_NULL_HANDLE)
1612 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001613
1614 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001615
Jon Ashburn98bd4542015-01-29 16:44:24 -07001616 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001617 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001618 break;
1619 }
1620
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001621 if (inst == VK_NULL_HANDLE)
1622 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001623
1624 for (icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001625 if (!icd->DbgRegisterMsgCallback)
1626 continue;
1627 res = icd->DbgRegisterMsgCallback(icd->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001628 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001629 if (res != VK_SUCCESS)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001630 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001631 }
1632
Jon Ashburnbb510252015-03-17 13:47:55 -06001633
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001634 /* roll back on errors */
1635 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001636 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001637 tmp = tmp->next) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001638 if (!tmp->DbgUnregisterMsgCallback)
1639 continue;
1640 tmp->DbgUnregisterMsgCallback(tmp->instance,
1641 pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001642 }
1643
1644 return res;
1645 }
1646
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001647 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001648}
1649
Jon Ashburn27cd5842015-05-12 17:26:48 -06001650VkResult loader_DbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001651{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001652 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001653 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001654 if (instance == VK_NULL_HANDLE)
1655 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001656
1657 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001658
Jon Ashburnbb510252015-03-17 13:47:55 -06001659 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001660 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001661 break;
1662 }
1663
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001664 if (inst == VK_NULL_HANDLE)
1665 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001666
1667 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
Jon Ashburn3da71f22015-05-14 12:43:38 -06001668 VkResult r;
1669 if (!icd->DbgUnregisterMsgCallback)
1670 continue;
1671 r = icd->DbgUnregisterMsgCallback(icd->instance, pfnMsgCallback);
1672 if (r != VK_SUCCESS) {
1673 res = r;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001674 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001675 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001676 return res;
1677}
1678
Jon Ashburn27cd5842015-05-12 17:26:48 -06001679VkResult loader_DbgSetGlobalOption(VkInstance instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001680{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001681 VkResult res = VK_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001682 struct loader_instance *inst;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001683 if (instance == VK_NULL_HANDLE)
1684 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001685
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001686 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001687
Jon Ashburnbb510252015-03-17 13:47:55 -06001688 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyanb050c682015-04-17 12:36:38 -06001689 if ((VkInstance) inst == instance)
Jon Ashburnbb510252015-03-17 13:47:55 -06001690 break;
1691 }
1692
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001693 if (inst == VK_NULL_HANDLE)
1694 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnbb510252015-03-17 13:47:55 -06001695 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001696 VkResult r;
Jon Ashburn3da71f22015-05-14 12:43:38 -06001697 if (!icd->DbgSetGlobalOption)
1698 continue;
1699 r = icd->DbgSetGlobalOption(icd->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001700 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001701 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001702 if (r != VK_SUCCESS) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001703 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001704 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001705 }
1706
1707 return res;
1708}
Jon Ashburn95a77ba2015-05-15 15:09:35 -06001709