blob: a30ddf3826dd1c77cb7519f1a91fc19c81ac35ee [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
Courtney Goeltzenleuchter9cc421e2015-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 Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn406a0fe2014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu44e42362014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Ian Elliott76005132015-03-31 15:32:41 -060028 * Ian Elliott <ian@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080029 */
Jon Ashburn183dfd02014-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 Wu894a1172014-08-04 11:18:20 +080037#include <sys/types.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -070038#if defined(WIN32)
39#include "dirent_on_windows.h"
40#else // WIN32
Chia-I Wu894a1172014-08-04 11:18:20 +080041#include <dirent.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -070042#endif // WIN32
43#include "loader_platform.h"
Chia-I Wu38e5a2c2015-01-04 11:12:47 +080044#include "table_ops.h"
Jon Ashburne18431b2015-04-13 18:10:06 -060045#include "gpa_helper.h"
Chia-I Wu468e3c32014-08-04 08:03:57 +080046#include "loader.h"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060047#include "vkIcd.h"
Ian Elliott20f06872015-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 Ashburn183dfd02014-10-22 18:13:16 -060052struct loader_layers {
Ian Elliott81ac44c2015-01-13 17:52:38 -070053 loader_platform_dl_handle lib_handle;
Jon Ashburnead95c52014-11-18 09:06:04 -070054 char name[256];
55};
56
57struct layer_name_pair {
58 char *layer_name;
59 const char *lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -060060};
61
Jon Ashburneb2728b2015-04-10 14:33:07 -060062struct extension_property {
63 char extName[VK_MAX_EXTENSION_NAME];
64 uint32_t version;
65 bool hosted; // does the extension reside in one driver/layer
66};
67
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080068struct loader_icd {
Jon Ashburn14275da2015-01-28 11:01:35 -070069 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080070
Jon Ashburn301c5f02015-04-06 10:58:22 -060071 VkLayerDispatchTable *loader_dispatch;
Jon Ashburn07b309a2015-04-15 11:31:12 -060072 uint32_t layer_count[MAX_GPUS_FOR_LAYER];
73 struct loader_layers layer_libs[MAX_GPUS_FOR_LAYER][MAX_LAYER_LIBRARIES];
74 VkBaseLayerObject *wrappedGpus[MAX_GPUS_FOR_LAYER];
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060075 uint32_t gpu_count;
Jon Ashburn301c5f02015-04-06 10:58:22 -060076 VkBaseLayerObject *gpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060077
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080078 struct loader_icd *next;
79};
80
Jon Ashburnd43f9b62014-10-14 19:15:22 -060081
Jon Ashburn14275da2015-01-28 11:01:35 -070082struct loader_scanned_icds {
Ian Elliott81ac44c2015-01-13 17:52:38 -070083 loader_platform_dl_handle handle;
Jon Ashburneb2728b2015-04-10 14:33:07 -060084
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060085 PFN_vkGetProcAddr GetProcAddr;
86 PFN_vkCreateInstance CreateInstance;
87 PFN_vkDestroyInstance DestroyInstance;
Jon Ashburn07b309a2015-04-15 11:31:12 -060088 PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
Jon Ashburneb2728b2015-04-10 14:33:07 -060089 PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060090 VkInstance instance;
Jon Ashburn14275da2015-01-28 11:01:35 -070091 struct loader_scanned_icds *next;
Jon Ashburneb2728b2015-04-10 14:33:07 -060092 uint32_t extension_count;
93 struct extension_property *extensions;
Jon Ashburn14275da2015-01-28 11:01:35 -070094};
Jon Ashburnd43f9b62014-10-14 19:15:22 -060095
Jon Ashburneb2728b2015-04-10 14:33:07 -060096struct loader_scanned_layers {
97 char *name;
98 uint32_t extension_count;
99 struct extension_property *extensions;
100};
Jon Ashburn349508d2015-01-26 14:51:40 -0700101// Note: Since the following is a static structure, all members are initialized
102// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800103static struct {
Jon Ashburn349508d2015-01-26 14:51:40 -0700104 struct loader_instance *instances;
Jon Ashburn14275da2015-01-28 11:01:35 -0700105 bool icds_scanned;
106 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600107 bool layer_scanned;
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700108 char *layer_dirs;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600109 unsigned int scanned_layer_count;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600110 struct loader_scanned_layers scanned_layers[MAX_LAYER_LIBRARIES];
111 size_t scanned_ext_list_capacity;
112 uint32_t scanned_ext_list_count; // coalesced from all layers/drivers
113 struct extension_property **scanned_ext_list;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800114} loader;
115
Jon Ashburneb2728b2015-04-10 14:33:07 -0600116static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
117static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
118static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700119
Ian Elliott225188f2015-02-17 10:33:47 -0700120#if defined(WIN32)
Ian Elliott76005132015-03-31 15:32:41 -0600121char *loader_get_registry_string(const HKEY hive,
122 const LPCTSTR sub_key,
123 const char *value)
124{
125 DWORD access_flags = KEY_QUERY_VALUE;
126 DWORD value_type;
127 HKEY key;
Ian Elliott464711a2015-04-28 15:57:32 -0600128 VkResult rtn_value;
Ian Elliott76005132015-03-31 15:32:41 -0600129 char *rtn_str = NULL;
Tony Barboura938abb2015-04-22 11:36:22 -0600130 DWORD rtn_len = 0;
Ian Elliott76005132015-03-31 15:32:41 -0600131 size_t allocated_len = 0;
132
133 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
134 if (rtn_value != ERROR_SUCCESS) {
135 // We didn't find the key. Try the 32-bit hive (where we've seen the
136 // key end up on some people's systems):
137 access_flags |= KEY_WOW64_32KEY;
138 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
139 if (rtn_value != ERROR_SUCCESS) {
140 // We still couldn't find the key, so give up:
141 return NULL;
142 }
143 }
144
145 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliott464711a2015-04-28 15:57:32 -0600146 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott76005132015-03-31 15:32:41 -0600147 if (rtn_value == ERROR_SUCCESS) {
148 // If we get to here, we found the key, and need to allocate memory
149 // large enough for rtn_str, and query again:
150 allocated_len = rtn_len + 4;
151 rtn_str = malloc(allocated_len);
152 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
Ian Elliott464711a2015-04-28 15:57:32 -0600153 (PVOID) rtn_str, (LPDWORD) &rtn_len);
Ian Elliott76005132015-03-31 15:32:41 -0600154 if (rtn_value == ERROR_SUCCESS) {
155 // We added 4 extra bytes to rtn_str, so that we can ensure that
156 // the string is NULL-terminated (albeit, in a brute-force manner):
157 rtn_str[allocated_len-1] = '\0';
158 } else {
159 // This should never occur, but in case it does, clean up:
160 free(rtn_str);
161 rtn_str = NULL;
162 }
163 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
164
165 // Close the registry key that was opened:
166 RegCloseKey(key);
167
168 return rtn_str;
169}
170
171
Ian Elliott225188f2015-02-17 10:33:47 -0700172// For ICD developers, look in the registry, and look for an environment
173// variable for a path(s) where to find the ICD(s):
174static char *loader_get_registry_and_env(const char *env_var,
175 const char *registry_value)
176{
177 char *env_str = getenv(env_var);
178 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott76005132015-03-31 15:32:41 -0600179 char *registry_str = NULL;
Tony Barboura938abb2015-04-22 11:36:22 -0600180 size_t registry_len = 0;
Ian Elliott225188f2015-02-17 10:33:47 -0700181 char *rtn_str = NULL;
182 size_t rtn_len;
183
Ian Elliott76005132015-03-31 15:32:41 -0600184 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
Ian Elliott10ec9622015-04-09 18:07:15 -0600185 "Software\\Vulkan",
Ian Elliott76005132015-03-31 15:32:41 -0600186 registry_value);
Ian Elliott464711a2015-04-28 15:57:32 -0600187 registry_len = (registry_str) ? (DWORD) strlen(registry_str) : 0;
Ian Elliott225188f2015-02-17 10:33:47 -0700188
189 rtn_len = env_len + registry_len + 1;
190 if (rtn_len <= 2) {
191 // We found neither the desired registry value, nor the environment
192 // variable; return NULL:
193 return NULL;
194 } else {
195 // We found something, and so we need to allocate memory for the string
196 // to return:
197 rtn_str = malloc(rtn_len);
198 }
199
Ian Elliott76005132015-03-31 15:32:41 -0600200 if (registry_len == 0) {
Ian Elliott225188f2015-02-17 10:33:47 -0700201 // We didn't find the desired registry value, and so we must have found
202 // only the environment variable:
203 _snprintf(rtn_str, rtn_len, "%s", env_str);
204 } else if (env_str != NULL) {
205 // We found both the desired registry value and the environment
206 // variable, so concatenate them both:
207 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
208 } else {
209 // We must have only found the desired registry value:
210 _snprintf(rtn_str, rtn_len, "%s", registry_str);
211 }
212
Ian Elliott7518ecd2015-04-03 13:13:01 -0600213 if (registry_str) {
214 free(registry_str);
215 }
Ian Elliott225188f2015-02-17 10:33:47 -0700216
217 return(rtn_str);
218}
219#endif // WIN32
220
221
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600222static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800223 const char *format, ...)
224{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800225 char msg[256];
226 va_list ap;
227 int ret;
228
229 va_start(ap, format);
230 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott642f8922015-02-13 14:29:21 -0700231 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800232 msg[sizeof(msg) - 1] = '\0';
233 }
234 va_end(ap);
235
Jon Ashburnb67859d2015-04-24 14:10:50 -0700236#if defined(WIN32)
237 OutputDebugString(msg);
238#else
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -0600239 fputs(msg, stderr);
240 fputc('\n', stderr);
Jon Ashburnb67859d2015-04-24 14:10:50 -0700241#endif
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242}
243
Jon Ashburneb2728b2015-04-10 14:33:07 -0600244static bool has_extension(struct extension_property *exts, uint32_t count,
245 const char *name, bool must_be_hosted)
246{
247 uint32_t i;
248 for (i = 0; i < count; i++) {
249 if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted))
250 return true;
251 }
252 return false;
253}
254
255static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get,
256 uint32_t *count_out,
257 struct extension_property **props_out)
258{
259 uint32_t i, count, cur;
260 size_t siz = sizeof(count);
261 struct extension_property *ext_props;
262 VkExtensionProperties vk_prop;
263 VkResult res;
264
265 *count_out = 0;
266 *props_out = NULL;
267 res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
268 if (res != VK_SUCCESS) {
269 loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD");
270 return;
271 }
272 ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count);
273 if (ext_props == NULL) {
274 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD");
275 return;
276 }
277 siz = sizeof(VkExtensionProperties);
278 cur = 0;
279 for (i = 0; i < count; i++) {
280 res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop);
281 if (res == VK_SUCCESS) {
282 (ext_props + cur)->hosted = false;
283 (ext_props + cur)->version = vk_prop.version;
284 strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME);
285 (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
286 cur++;
287 }
288 *count_out = cur;
289 *props_out = ext_props;
290 }
291 return;
292}
293
294static void loader_init_ext_list()
295{
296 loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *);
297 loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity);
298 memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity);
299 loader.scanned_ext_list_count = 0;
300}
301
302#if 0 // currently no place to call this
303static void loader_destroy_ext_list()
304{
305 free(loader.scanned_ext_list);
306 loader.scanned_ext_list_capacity = 0;
307 loader.scanned_ext_list_count = 0;
308}
309#endif
310
311static void loader_add_to_ext_list(uint32_t count,
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600312 struct extension_property *prop_list,
313 bool is_layer_ext)
Jon Ashburneb2728b2015-04-10 14:33:07 -0600314{
315 uint32_t i, j;
316 bool duplicate;
317 struct extension_property *cur_ext;
318
319 if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0)
320 loader_init_ext_list();
321
322 if (loader.scanned_ext_list == NULL)
323 return;
324
325 struct extension_property *ext_list, **ext_list_addr;
326
327 for (i = 0; i < count; i++) {
328 cur_ext = prop_list + i;
329
330 // look for duplicates or not
331 duplicate = false;
332 for (j = 0; j < loader.scanned_ext_list_count; j++) {
333 ext_list = loader.scanned_ext_list[j];
334 if (!strcmp(cur_ext->extName, ext_list->extName)) {
335 duplicate = true;
336 ext_list->hosted = false;
337 break;
338 }
339 }
340
341 // add to list at end
342 if (!duplicate) {
343 // check for enough capacity
344 if (loader.scanned_ext_list_count * sizeof(struct extension_property *)
345 >= loader.scanned_ext_list_capacity) {
346 // double capacity
347 loader.scanned_ext_list_capacity *= 2;
348 loader.scanned_ext_list = realloc(loader.scanned_ext_list,
349 loader.scanned_ext_list_capacity);
350 }
351 ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]);
352 *ext_list_addr = cur_ext;
353 cur_ext->hosted = true;
354 }
355
356 }
357}
358
Jon Ashburn42e41032015-04-14 09:15:32 -0600359static bool loader_is_extension_scanned(const char *name)
360{
361 uint32_t i;
362
363 for (i = 0; i < loader.scanned_ext_list_count; i++) {
364 if (!strcmp(name, loader.scanned_ext_list[i]->extName))
365 return true;
366 }
367 return false;
368}
369
Tony Barboura938abb2015-04-22 11:36:22 -0600370static void loader_coalesce_extensions(void)
Jon Ashburneb2728b2015-04-10 14:33:07 -0600371{
372 uint32_t i;
373 struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
374
375 // traverse scanned icd list adding non-duplicate extensions to the list
376 while (icd_list != NULL) {
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600377 loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600378 icd_list = icd_list->next;
379 };
380
381 //Traverse layers list adding non-duplicate extensions to the list
382 for (i = 0; i < loader.scanned_layer_count; i++) {
383 loader_add_to_ext_list(loader.scanned_layers[i].extension_count,
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600384 loader.scanned_layers[i].extensions, true);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600385 }
386}
387
388static void loader_icd_destroy(struct loader_icd *icd)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800389{
Ian Elliott81ac44c2015-01-13 17:52:38 -0700390 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800391 free(icd);
392}
393
Jon Ashburneb2728b2015-04-10 14:33:07 -0600394static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800395{
396 struct loader_icd *icd;
397
398 icd = malloc(sizeof(*icd));
399 if (!icd)
400 return NULL;
401
Courtney Goeltzenleuchter6f928162014-10-28 10:29:27 -0600402 memset(icd, 0, sizeof(*icd));
403
Jon Ashburn14275da2015-01-28 11:01:35 -0700404 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800405
406 return icd;
407}
408
Jon Ashburn3336df82015-01-29 15:45:51 -0700409static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
410 const struct loader_scanned_icds *scanned)
Chia-I Wu894a1172014-08-04 11:18:20 +0800411{
412 struct loader_icd *icd;
413
Jon Ashburn14275da2015-01-28 11:01:35 -0700414 icd = loader_icd_create(scanned);
Chia-I Wu894a1172014-08-04 11:18:20 +0800415 if (!icd)
416 return NULL;
417
Chia-I Wu894a1172014-08-04 11:18:20 +0800418 /* prepend to the list */
Jon Ashburn3336df82015-01-29 15:45:51 -0700419 icd->next = ptr_inst->icds;
420 ptr_inst->icds = icd;
Chia-I Wu894a1172014-08-04 11:18:20 +0800421
422 return icd;
423}
424
Jon Ashburn14275da2015-01-28 11:01:35 -0700425static void loader_scanned_icd_add(const char *filename)
426{
Ian Elliott81ac44c2015-01-13 17:52:38 -0700427 loader_platform_dl_handle handle;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600428 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
429 void *fp_get_global_ext_info;
Jon Ashburn14275da2015-01-28 11:01:35 -0700430 struct loader_scanned_icds *new_node;
431
Ian Elliott81ac44c2015-01-13 17:52:38 -0700432 // Used to call: dlopen(filename, RTLD_LAZY);
433 handle = loader_platform_open_library(filename);
Jon Ashburn14275da2015-01-28 11:01:35 -0700434 if (!handle) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn14275da2015-01-28 11:01:35 -0700436 return;
437 }
438
439#define LOOKUP(func_ptr, func) do { \
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600440 func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
Jon Ashburn14275da2015-01-28 11:01:35 -0700441 if (!func_ptr) { \
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600442 loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
Jon Ashburn14275da2015-01-28 11:01:35 -0700443 return; \
444 } \
445} while (0)
446
447 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn3336df82015-01-29 15:45:51 -0700448 LOOKUP(fp_create_inst, CreateInstance);
449 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn07b309a2015-04-15 11:31:12 -0600450 LOOKUP(fp_enumerate, EnumeratePhysicalDevices);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600451 LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
Jon Ashburn14275da2015-01-28 11:01:35 -0700452#undef LOOKUP
453
454 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
455 if (!new_node) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
Jon Ashburn14275da2015-01-28 11:01:35 -0700457 return;
458 }
459
460 new_node->handle = handle;
461 new_node->GetProcAddr = fp_gpa;
Jon Ashburn3336df82015-01-29 15:45:51 -0700462 new_node->CreateInstance = fp_create_inst;
463 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn07b309a2015-04-15 11:31:12 -0600464 new_node->EnumeratePhysicalDevices = fp_enumerate;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600465 new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
466 new_node->extension_count = 0;
467 new_node->extensions = NULL;
Jon Ashburn14275da2015-01-28 11:01:35 -0700468 new_node->next = loader.scanned_icd_list;
Jon Ashburn14275da2015-01-28 11:01:35 -0700469
Jon Ashburneb2728b2015-04-10 14:33:07 -0600470 loader.scanned_icd_list = new_node;
471
472 if (fp_get_global_ext_info) {
473 get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
474 &new_node->extension_count,
475 &new_node->extensions);
476 } else {
477 loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD");
478 }
479}
Ian Elliott81ac44c2015-01-13 17:52:38 -0700480
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600481/**
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600482 * Try to \c loader_icd_scan VK driver(s).
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600483 *
484 * This function scans the default system path or path
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600485 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
486 * order to find loadable VK ICDs with the name of libVK_*.
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600487 *
488 * \returns
489 * void; but side effect is to set loader_icd_scanned to true
490 */
491static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800492{
Ian Elliott225188f2015-02-17 10:33:47 -0700493 const char *p, *next;
494 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600495 DIR *sysdir;
496 struct dirent *dent;
497 char icd_library[1024];
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600498 char path[1024];
Ian Elliott64f74a82015-02-04 12:06:46 -0700499 uint32_t len;
Ian Elliott225188f2015-02-17 10:33:47 -0700500#if defined(WIN32)
501 bool must_free_libPaths;
502 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
503 DRIVER_PATH_REGISTRY_VALUE);
504 if (libPaths != NULL) {
505 must_free_libPaths = true;
506 } else {
507 must_free_libPaths = false;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600508 libPaths = DEFAULT_VK_DRIVERS_PATH;
Ian Elliott225188f2015-02-17 10:33:47 -0700509 }
510#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600511 if (geteuid() == getuid()) {
Ian Elliott225188f2015-02-17 10:33:47 -0700512 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
513 libPaths = getenv(DRIVER_PATH_ENV);
514 }
515 if (libPaths == NULL) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600516 libPaths = DEFAULT_VK_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600517 }
Ian Elliott81ac44c2015-01-13 17:52:38 -0700518#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800519
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600520 for (p = libPaths; *p; p = next) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700521 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600522 if (next == NULL) {
Ian Elliott64f74a82015-02-04 12:06:46 -0700523 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600524 next = p + len;
525 }
526 else {
Ian Elliott64f74a82015-02-04 12:06:46 -0700527 len = (uint32_t) (next - p);
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600528 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
529 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600530 next++;
531 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800532
Ian Elliott81ac44c2015-01-13 17:52:38 -0700533 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
534 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600535 sysdir = opendir(p);
536 if (sysdir) {
537 dent = readdir(sysdir);
538 while (dent) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600539 /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
540 * ending with VK_LIBRARY_SUFFIX
Ian Elliott81ac44c2015-01-13 17:52:38 -0700541 */
542 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600543 VK_DRIVER_LIBRARY_PREFIX,
544 VK_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott64f74a82015-02-04 12:06:46 -0700545 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600546 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
547 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott81ac44c2015-01-13 17:52:38 -0700548 !strncmp(suf,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600549 VK_LIBRARY_SUFFIX,
550 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700551 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
552 loader_scanned_icd_add(icd_library);
553 }
554 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600555
556 dent = readdir(sysdir);
557 }
558 closedir(sysdir);
559 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800560 }
561
Ian Elliott225188f2015-02-17 10:33:47 -0700562#if defined(WIN32)
563 // Free any allocated memory:
564 if (must_free_libPaths) {
565 free(libPaths);
566 }
567#endif // WIN32
568
569 // Note that we've scanned for ICDs:
Jon Ashburn14275da2015-01-28 11:01:35 -0700570 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800571}
572
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600573
Ian Elliott225188f2015-02-17 10:33:47 -0700574static void layer_lib_scan(void)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600575{
576 const char *p, *next;
Ian Elliott225188f2015-02-17 10:33:47 -0700577 char *libPaths = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600578 DIR *curdir;
579 struct dirent *dent;
Ian Elliott225188f2015-02-17 10:33:47 -0700580 size_t len, i;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600581 char temp_str[1024];
Jon Ashburneb2728b2015-04-10 14:33:07 -0600582 uint32_t count;
583 PFN_vkGetGlobalExtensionInfo fp_get_ext;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600584
Ian Elliott225188f2015-02-17 10:33:47 -0700585#if defined(WIN32)
586 bool must_free_libPaths;
587 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
588 LAYERS_PATH_REGISTRY_VALUE);
589 if (libPaths != NULL) {
590 must_free_libPaths = true;
591 } else {
592 must_free_libPaths = false;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600593 libPaths = DEFAULT_VK_LAYERS_PATH;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600594 }
Ian Elliott225188f2015-02-17 10:33:47 -0700595#else // WIN32
596 if (geteuid() == getuid()) {
597 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter4ee01712015-02-18 20:03:02 -0700598 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott225188f2015-02-17 10:33:47 -0700599 }
600 if (libPaths == NULL) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600601 libPaths = DEFAULT_VK_LAYERS_PATH;
Ian Elliott225188f2015-02-17 10:33:47 -0700602 }
Ian Elliott81ac44c2015-01-13 17:52:38 -0700603#endif // WIN32
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600604
Ian Elliott225188f2015-02-17 10:33:47 -0700605 if (libPaths == NULL) {
606 // Have no paths to search:
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700607 return;
608 }
Ian Elliott225188f2015-02-17 10:33:47 -0700609 len = strlen(libPaths);
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700610 loader.layer_dirs = malloc(len+1);
Ian Elliott225188f2015-02-17 10:33:47 -0700611 if (loader.layer_dirs == NULL) {
612 free(libPaths);
Courtney Goeltzenleuchter688c74b2014-12-02 18:12:51 -0700613 return;
Ian Elliott225188f2015-02-17 10:33:47 -0700614 }
615 // Alloc passed, so we know there is enough space to hold the string, don't
616 // need strncpy
617 strcpy(loader.layer_dirs, libPaths);
618#if defined(WIN32)
619 // Free any allocated memory:
620 if (must_free_libPaths) {
621 free(libPaths);
622 must_free_libPaths = false;
623 }
624#endif // WIN32
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700625 libPaths = loader.layer_dirs;
626
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600627 /* cleanup any previously scanned libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600628 for (i = 0; i < loader.scanned_layer_count; i++) {
Jon Ashburneb2728b2015-04-10 14:33:07 -0600629 if (loader.scanned_layers[i].name != NULL)
630 free(loader.scanned_layers[i].name);
631 if (loader.scanned_layers[i].extensions != NULL)
632 free(loader.scanned_layers[i].extensions);
633 loader.scanned_layers[i].name = NULL;
634 loader.scanned_layers[i].extensions = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600635 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600636 loader.scanned_layer_count = 0;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600637 count = 0;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600638
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600639 for (p = libPaths; *p; p = next) {
Jon Ashburneb2728b2015-04-10 14:33:07 -0600640 next = strchr(p, PATH_SEPERATOR);
641 if (next == NULL) {
642 len = (uint32_t) strlen(p);
643 next = p + len;
644 }
645 else {
646 len = (uint32_t) (next - p);
647 *(char *) next = '\0';
648 next++;
649 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600650
651 curdir = opendir(p);
652 if (curdir) {
653 dent = readdir(curdir);
654 while (dent) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600655 /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
656 * ending with VK_LIBRARY_SUFFIX
Ian Elliott81ac44c2015-01-13 17:52:38 -0700657 */
658 if (!strncmp(dent->d_name,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600659 VK_LAYER_LIBRARY_PREFIX,
660 VK_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott64f74a82015-02-04 12:06:46 -0700661 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600662 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
663 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
Ian Elliott81ac44c2015-01-13 17:52:38 -0700664 !strncmp(suf,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600665 VK_LIBRARY_SUFFIX,
666 VK_LIBRARY_SUFFIX_LEN)) {
Ian Elliott81ac44c2015-01-13 17:52:38 -0700667 loader_platform_dl_handle handle;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600668 snprintf(temp_str, sizeof(temp_str),
669 "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700670 // Used to call: dlopen(temp_str, RTLD_LAZY)
671 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
672 dent = readdir(curdir);
673 continue;
674 }
Jon Ashburneb2728b2015-04-10 14:33:07 -0600675 if (count == MAX_LAYER_LIBRARIES) {
676 loader_log(VK_DBG_MSG_ERROR, 0,
677 "%s ignored: max layer libraries exceed",
678 temp_str);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700679 break;
680 }
Jon Ashburneb2728b2015-04-10 14:33:07 -0600681 fp_get_ext = loader_platform_get_proc_address(handle,
682 "vkGetGlobalExtensionInfo");
683
684 if (!fp_get_ext) {
685 loader_log(VK_DBG_MSG_WARNING, 0,
686 "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
687 temp_str);
688 dent = readdir(curdir);
689 loader_platform_close_library(handle);
690 continue;
691 }
692
693 loader.scanned_layers[count].name =
694 malloc(strlen(temp_str) + 1);
695 if (loader.scanned_layers[count].name == NULL) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600696 loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700697 break;
698 }
Jon Ashburneb2728b2015-04-10 14:33:07 -0600699
700 get_global_extensions(fp_get_ext,
701 &loader.scanned_layers[count].extension_count,
702 &loader.scanned_layers[count].extensions);
703
704
705 strcpy(loader.scanned_layers[count].name, temp_str);
706 count++;
707 loader_platform_close_library(handle);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700708 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600709 }
710
711 dent = readdir(curdir);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600712 } // while (dir_entry)
713 if (count == MAX_LAYER_LIBRARIES)
714 break;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600715 closedir(curdir);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600716 } // if (curdir))
717 } // for (libpaths)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600718
Jon Ashburneb2728b2015-04-10 14:33:07 -0600719 loader.scanned_layer_count = count;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600720 loader.layer_scanned = true;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600721}
722
Tony Barbour8205d902015-04-16 15:59:00 -0600723static void loader_init_dispatch_table(VkLayerDispatchTable *tab, PFN_vkGetProcAddr fpGPA, VkPhysicalDevice gpu)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600724{
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800725 loader_initialize_dispatch_table(tab, fpGPA, gpu);
726
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600727 if (tab->EnumerateLayers == NULL)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600728 tab->EnumerateLayers = vkEnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600729}
730
Chia-I Wu72259962015-04-17 10:49:15 +0800731static void * VKAPI loader_gpa_internal(VkPhysicalDevice gpu, const char * pName)
Jon Ashburne18431b2015-04-13 18:10:06 -0600732{
Mike Stroyan230e6252015-04-17 12:36:38 -0600733 if (gpu == VK_NULL_HANDLE) {
Jon Ashburne18431b2015-04-13 18:10:06 -0600734 return NULL;;
735 }
736 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
737 VkLayerDispatchTable * disp_table = * (VkLayerDispatchTable **) gpuw->baseObject;
738 void *addr;
739
740 if (disp_table == NULL)
741 return NULL;
742
743 addr = loader_lookup_dispatch_table(disp_table, pName);
744 if (addr)
745 return addr;
746 else {
747 if (disp_table->GetProcAddr == NULL)
748 return NULL;
Jon Ashburn54681562015-04-24 17:15:00 -0700749 if (gpuw->baseObject == gpuw->nextObject)
750 return gpuw->pGPA(gpuw->baseObject, pName);
Jon Ashburne18431b2015-04-13 18:10:06 -0600751 return disp_table->GetProcAddr(gpuw->nextObject, pName);
752 }
753}
754
Jon Ashburn301c5f02015-04-06 10:58:22 -0600755extern struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600756{
Jon Ashburn630e44f2015-04-08 21:33:34 -0600757 /*
758 * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else
759 * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so
760 * need to test it against the wrapped GPU's base object.
761 */
Jon Ashburndc67ef52015-01-29 16:44:24 -0700762 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
763 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
764 for (uint32_t i = 0; i < icd->gpu_count; i++)
Mike Stroyan230e6252015-04-17 12:36:38 -0600765 if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) {
Jon Ashburndc67ef52015-01-29 16:44:24 -0700766 *gpu_index = i;
767 return icd;
768 }
769 }
Jon Ashburnb55278a2014-10-17 15:09:07 -0600770 }
771 return NULL;
772}
773
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600774static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600775{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600776 if (icd->layer_count[gpu_index])
777 return true;
778 else
779 return false;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600780}
781
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600782static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
783 struct layer_name_pair * pLayerNames,
784 uint32_t count)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600785{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600786 if (!icd)
787 return;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600788
Jon Ashburn183dfd02014-10-22 18:13:16 -0600789 struct loader_layers *obj;
790 bool foundLib;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600791 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600792 foundLib = false;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600793 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600794 if (icd->layer_libs[gpu_index][j].lib_handle &&
795 !strcmp(icd->layer_libs[gpu_index][j].name,
796 (char *) pLayerNames[i].layer_name) &&
797 strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600798 foundLib = true;
799 break;
800 }
801 }
802 if (!foundLib) {
803 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnead95c52014-11-18 09:06:04 -0700804 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
805 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott81ac44c2015-01-13 17:52:38 -0700806 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
807 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600808 loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600809 continue;
810 } else {
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600811 loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s",
812 pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600813 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700814 free(pLayerNames[i].layer_name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600815 icd->layer_count[gpu_index]++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600816 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600817 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600818}
819
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600820static bool find_layer_extension(struct loader_icd *icd, uint32_t gpu_index,
821 const char *pExtName, uint32_t *out_count,
822 char *lib_name[MAX_LAYER_LIBRARIES])
Jon Ashburnead95c52014-11-18 09:06:04 -0700823{
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700824 char *search_name;
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600825 uint32_t j, found_count = 0;
826 bool must_be_hosted;
827 bool found = false;
Jon Ashburnead95c52014-11-18 09:06:04 -0700828
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700829 /*
830 * The loader provides the abstraction that make layers and extensions work via
831 * the currently defined extension mechanism. That is, when app queries for an extension
Jon Ashburneb2728b2015-04-10 14:33:07 -0600832 * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700833 * to see who implements that extension. Then, if the app enables the extension during
Jon Ashburneb2728b2015-04-10 14:33:07 -0600834 * vkCreateInstance the loader will find and load any layers that implement that extension.
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700835 */
Jon Ashburnead95c52014-11-18 09:06:04 -0700836
Jon Ashburneb2728b2015-04-10 14:33:07 -0600837 // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700838
Jon Ashburneb2728b2015-04-10 14:33:07 -0600839 for (j = 0; j < loader.scanned_layer_count; j++) {
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600840
841 if (!strcmp("Validation", pExtName))
842 must_be_hosted = false;
843 else
844 must_be_hosted = true;
Jon Ashburneb2728b2015-04-10 14:33:07 -0600845 if (has_extension(loader.scanned_layers[j].extensions,
846 loader.scanned_layers[j].extension_count, pExtName,
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600847 must_be_hosted)) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700848
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600849 found = true;
850 lib_name[found_count] = loader.scanned_layers[j].name;
851 found_count++;
852 } else {
853 // Extension not found in list for the layer, so test the layer name
854 // as if it is an extension name. Use default layer name based on
855 // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
856 char *pEnd;
857 size_t siz;
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700858
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600859 search_name = loader.scanned_layers[j].name;
860 search_name = basename(search_name);
861 search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
862 pEnd = strrchr(search_name, '.');
863 siz = (int) (pEnd - search_name);
864 if (siz != strlen(pExtName))
865 continue;
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700866
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600867 if (strncmp(search_name, pExtName, siz) == 0) {
868 found = true;
869 lib_name[found_count] = loader.scanned_layers[j].name;
870 found_count++;
871 }
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700872 }
873 }
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600874
875 *out_count = found_count;
876 return found;
Jon Ashburnead95c52014-11-18 09:06:04 -0700877}
878
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600879static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600880{
Ian Elliott225188f2015-02-17 10:33:47 -0700881 char *layerEnv;
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600882 uint32_t i, len, found_count, count = 0;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600883 char *p, *pOrig, *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600884
Ian Elliott225188f2015-02-17 10:33:47 -0700885#if defined(WIN32)
886 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
887 LAYER_NAMES_REGISTRY_VALUE);
888#else // WIN32
889 layerEnv = getenv(LAYER_NAMES_ENV);
890#endif // WIN32
891 if (layerEnv == NULL) {
Jon Ashburn4fbcb032014-10-23 10:29:09 -0600892 return 0;
Ian Elliott225188f2015-02-17 10:33:47 -0700893 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600894 p = malloc(strlen(layerEnv) + 1);
Ian Elliott225188f2015-02-17 10:33:47 -0700895 if (p == NULL) {
896#if defined(WIN32)
897 free(layerEnv);
898#endif // WIN32
Jon Ashburn183dfd02014-10-22 18:13:16 -0600899 return 0;
Ian Elliott225188f2015-02-17 10:33:47 -0700900 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600901 strcpy(p, layerEnv);
Ian Elliott225188f2015-02-17 10:33:47 -0700902#if defined(WIN32)
903 free(layerEnv);
904#endif // WIN32
Jon Ashburnb4d00532014-10-22 21:15:26 -0600905 pOrig = p;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600906
Jon Ashburn183dfd02014-10-22 18:13:16 -0600907 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600908 char *lib_name[MAX_LAYER_LIBRARIES];
909 //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700910 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600911 if (next == NULL) {
Ian Elliott64f74a82015-02-04 12:06:46 -0700912 len = (uint32_t) strlen(p);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600913 next = p + len;
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700914 } else {
Ian Elliott64f74a82015-02-04 12:06:46 -0700915 len = (uint32_t) (next - p);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600916 *(char *) next = '\0';
917 next++;
918 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600919 name = basename(p);
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600920 if (!find_layer_extension(icd, gpu_index, name, &found_count, lib_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600921 p = next;
922 continue;
923 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600924
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600925 for (i = 0; i < found_count; i++) {
926 len = (uint32_t) strlen(name);
927 pLayerNames[count].layer_name = malloc(len + 1);
928 if (!pLayerNames[count].layer_name) {
929 free(pOrig);
930 return count;
931 }
932 strncpy((char *) pLayerNames[count].layer_name, name, len);
933 pLayerNames[count].layer_name[len] = '\0';
934 pLayerNames[count].lib_name = lib_name[i];
935 count++;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600936 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600937 p = next;
938
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700939 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600940
Jon Ashburnb4d00532014-10-22 21:15:26 -0600941 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600942 return count;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600943}
944
Jon Ashburn301c5f02015-04-06 10:58:22 -0600945static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names, struct layer_name_pair **ppLayerNames)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600946{
Jon Ashburnead95c52014-11-18 09:06:04 -0700947 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600948 char *lib_name[MAX_LAYER_LIBRARIES];
949 uint32_t found_count, count = 0;
950 bool skip;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600951
952 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter34e66672015-02-17 14:21:21 -0700953 /* Load any layers specified in the environment first */
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700954 count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600955
Jon Ashburn301c5f02015-04-06 10:58:22 -0600956 for (uint32_t i = 0; i < ext_count; i++) {
957 const char *pExtName = ext_names[i];
Jon Ashburn183dfd02014-10-22 18:13:16 -0600958
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600959 skip = false;
960 for (uint32_t j = 0; j < count; j++) {
961 if (!strcmp(pExtName, layerNames[j].layer_name) ) {
962 // Extension / Layer already on the list skip it
963 skip = true;
964 break;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600965 }
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600966 }
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700967
Jon Ashburn4d3b7522015-04-14 14:14:48 -0600968 if (!skip && find_layer_extension(icd, gpu_index, pExtName, &found_count, lib_name)) {
969
970 for (uint32_t j = 0; j < found_count; j++) {
971 uint32_t len;
972 len = (uint32_t) strlen(pExtName);
973
974
975 layerNames[count].layer_name = malloc(len + 1);
976 if (!layerNames[count].layer_name)
977 return count;
978 strncpy((char *) layerNames[count].layer_name, pExtName, len);
979 layerNames[count].layer_name[len] = '\0';
980 layerNames[count].lib_name = lib_name[j];
981 count++;
982 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600983 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600984 }
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -0700985
986 return count;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600987}
988
Jon Ashburn14275da2015-01-28 11:01:35 -0700989static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600990{
991 struct loader_icd *icd;
992 struct loader_layers *libs;
993
Jon Ashburn14275da2015-01-28 11:01:35 -0700994 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600995 if (icd->gpus)
996 free(icd->gpus);
997 icd->gpus = NULL;
998 if (icd->loader_dispatch)
999 free(icd->loader_dispatch);
1000 icd->loader_dispatch = NULL;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001001 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn183dfd02014-10-22 18:13:16 -06001002 if (icd->layer_count[j] > 0) {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001003 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn183dfd02014-10-22 18:13:16 -06001004 libs = &(icd->layer_libs[j][i]);
1005 if (libs->lib_handle)
Ian Elliott81ac44c2015-01-13 17:52:38 -07001006 loader_platform_close_library(libs->lib_handle);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001007 libs->lib_handle = NULL;
1008 }
Jon Ashburnb4d00532014-10-22 21:15:26 -06001009 if (icd->wrappedGpus[j])
1010 free(icd->wrappedGpus[j]);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001011 }
1012 icd->layer_count[j] = 0;
1013 }
1014 icd->gpu_count = 0;
1015 }
1016}
1017
Jon Ashburn301c5f02015-04-06 10:58:22 -06001018extern uint32_t loader_activate_layers(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names)
Jon Ashburn183dfd02014-10-22 18:13:16 -06001019{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001020 uint32_t count;
Jon Ashburn301c5f02015-04-06 10:58:22 -06001021 VkBaseLayerObject *gpu;
Jon Ashburnead95c52014-11-18 09:06:04 -07001022 struct layer_name_pair *pLayerNames;
Jon Ashburn183dfd02014-10-22 18:13:16 -06001023 if (!icd)
1024 return 0;
Jon Ashburn07b309a2015-04-15 11:31:12 -06001025 assert(gpu_index < MAX_GPUS_FOR_LAYER);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001026
Jon Ashburn301c5f02015-04-06 10:58:22 -06001027 gpu = icd->gpus + gpu_index;
Jon Ashburn183dfd02014-10-22 18:13:16 -06001028 /* activate any layer libraries */
1029 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn301c5f02015-04-06 10:58:22 -06001030 VkBaseLayerObject *gpuObj = gpu;
Mike Stroyan230e6252015-04-17 12:36:38 -06001031 VkBaseLayerObject *nextGpuObj, *baseObj = (VkBaseLayerObject *) gpuObj->baseObject;
Jon Ashburne18431b2015-04-13 18:10:06 -06001032 PFN_vkGetProcAddr nextGPA = loader_gpa_internal;
Jon Ashburn183dfd02014-10-22 18:13:16 -06001033
Jon Ashburn301c5f02015-04-06 10:58:22 -06001034 count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001035 if (!count)
1036 return 0;
Jon Ashburnead95c52014-11-18 09:06:04 -07001037 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001038
Jon Ashburn301c5f02015-04-06 10:58:22 -06001039 icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
Jon Ashburnb4d00532014-10-22 21:15:26 -06001040 if (! icd->wrappedGpus[gpu_index])
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001041 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001042 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnb4d00532014-10-22 21:15:26 -06001043 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001044 nextGpuObj->pGPA = nextGPA;
Mike Stroyan230e6252015-04-17 12:36:38 -06001045 nextGpuObj->baseObject = (VkObject) baseObj;
1046 nextGpuObj->nextObject = (VkObject) gpuObj;
Jon Ashburn183dfd02014-10-22 18:13:16 -06001047 gpuObj = nextGpuObj;
1048
Jon Ashburn8d8dad02014-12-01 14:22:40 -07001049 char funcStr[256];
1050 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001051 if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1052 nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
Jon Ashburn183dfd02014-10-22 18:13:16 -06001053 if (!nextGPA) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001054 loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn183dfd02014-10-22 18:13:16 -06001055 continue;
1056 }
1057
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001058 if (i == 0) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001059 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) gpuObj);
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001060 //Insert the new wrapped objects into the list with loader object at head
Mike Stroyan230e6252015-04-17 12:36:38 -06001061 gpu->nextObject = (VkObject) gpuObj;
Jon Ashburn301c5f02015-04-06 10:58:22 -06001062 gpu->pGPA = nextGPA;
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001063 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
Mike Stroyan230e6252015-04-17 12:36:38 -06001064 gpuObj->nextObject = (VkObject) baseObj;
Jon Ashburn14275da2015-01-28 11:01:35 -07001065 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001066 }
Jon Ashburn183dfd02014-10-22 18:13:16 -06001067
1068 }
Jon Ashburn183dfd02014-10-22 18:13:16 -06001069 }
1070 else {
1071 //make sure requested Layers matches currently activated Layers
Jon Ashburn301c5f02015-04-06 10:58:22 -06001072 count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001073 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -07001074 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001075 loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
Jon Ashburn183dfd02014-10-22 18:13:16 -06001076 break;
1077 }
1078 }
1079 if (count != icd->layer_count[gpu_index]) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001080 loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn183dfd02014-10-22 18:13:16 -06001081 }
1082 }
1083 return icd->layer_count[gpu_index];
1084}
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001085
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001086LOADER_EXPORT VkResult VKAPI vkCreateInstance(
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001087 const VkInstanceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001088 VkInstance* pInstance)
Jon Ashburn349508d2015-01-26 14:51:40 -07001089{
1090 struct loader_instance *ptr_instance = NULL;
Jon Ashburn3336df82015-01-29 15:45:51 -07001091 struct loader_scanned_icds *scanned_icds;
1092 struct loader_icd *icd;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001093 VkResult res = VK_ERROR_INITIALIZATION_FAILED;
Jon Ashburn063cd372015-04-08 15:49:00 -06001094 uint32_t i;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001095
Ian Elliott81ac44c2015-01-13 17:52:38 -07001096 /* Scan/discover all ICD libraries in a single-threaded manner */
1097 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn3336df82015-01-29 15:45:51 -07001098
Ian Elliott81ac44c2015-01-13 17:52:38 -07001099 /* get layer libraries in a single-threaded manner */
1100 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn3336df82015-01-29 15:45:51 -07001101
Jon Ashburneb2728b2015-04-10 14:33:07 -06001102 /* merge any duplicate extensions */
1103 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1104
Jon Ashburn349508d2015-01-26 14:51:40 -07001105 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
1106 if (ptr_instance == NULL) {
Tony Barbour8205d902015-04-16 15:59:00 -06001107 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn349508d2015-01-26 14:51:40 -07001108 }
1109 memset(ptr_instance, 0, sizeof(struct loader_instance));
Jon Ashburn063cd372015-04-08 15:49:00 -06001110 ptr_instance->extension_count = pCreateInfo->extensionCount;
1111 ptr_instance->extension_names = (ptr_instance->extension_count > 0) ?
1112 malloc(sizeof (char *) * ptr_instance->extension_count) : NULL;
Jon Ashburn42e41032015-04-14 09:15:32 -06001113 if (ptr_instance->extension_names == NULL && (ptr_instance->extension_count > 0))
Tony Barbour8205d902015-04-16 15:59:00 -06001114 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn063cd372015-04-08 15:49:00 -06001115 for (i = 0; i < ptr_instance->extension_count; i++) {
Jon Ashburn42e41032015-04-14 09:15:32 -06001116 if (!loader_is_extension_scanned(pCreateInfo->ppEnabledExtensionNames[i]))
1117 return VK_ERROR_INVALID_EXTENSION;
1118 ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i]) + 1);
Jon Ashburn063cd372015-04-08 15:49:00 -06001119 if (ptr_instance->extension_names[i] == NULL)
Tony Barbour8205d902015-04-16 15:59:00 -06001120 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn063cd372015-04-08 15:49:00 -06001121 strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]);
1122 }
Jon Ashburn349508d2015-01-26 14:51:40 -07001123 ptr_instance->next = loader.instances;
1124 loader.instances = ptr_instance;
1125
Jon Ashburn3336df82015-01-29 15:45:51 -07001126 scanned_icds = loader.scanned_icd_list;
1127 while (scanned_icds) {
1128 icd = loader_icd_add(ptr_instance, scanned_icds);
1129 if (icd) {
Jon Ashburn29669a42015-04-04 14:52:07 -06001130 res = scanned_icds->CreateInstance(pCreateInfo,
Jon Ashburn3336df82015-01-29 15:45:51 -07001131 &(scanned_icds->instance));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001132 if (res != VK_SUCCESS)
Jon Ashburn3336df82015-01-29 15:45:51 -07001133 {
1134 ptr_instance->icds = ptr_instance->icds->next;
1135 loader_icd_destroy(icd);
Mike Stroyan230e6252015-04-17 12:36:38 -06001136 scanned_icds->instance = VK_NULL_HANDLE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001137 loader_log(VK_DBG_MSG_WARNING, 0,
Jon Ashburn3336df82015-01-29 15:45:51 -07001138 "ICD ignored: failed to CreateInstance on device");
1139 }
1140 }
1141 scanned_icds = scanned_icds->next;
1142 }
Jon Ashburn349508d2015-01-26 14:51:40 -07001143
Ian Elliott617fdec2015-02-05 15:19:15 -07001144 if (ptr_instance->icds == NULL) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001145 return VK_ERROR_INCOMPATIBLE_DRIVER;
Ian Elliott617fdec2015-02-05 15:19:15 -07001146 }
Jon Ashburn3336df82015-01-29 15:45:51 -07001147
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001148 *pInstance = (VkInstance) ptr_instance;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001149 return VK_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -07001150}
1151
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001152LOADER_EXPORT VkResult VKAPI vkDestroyInstance(
1153 VkInstance instance)
Jon Ashburn349508d2015-01-26 14:51:40 -07001154{
1155 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn3336df82015-01-29 15:45:51 -07001156 struct loader_scanned_icds *scanned_icds;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001157 VkResult res;
Jon Ashburn063cd372015-04-08 15:49:00 -06001158 uint32_t i;
Jon Ashburn349508d2015-01-26 14:51:40 -07001159
1160 // Remove this instance from the list of instances:
1161 struct loader_instance *prev = NULL;
1162 struct loader_instance *next = loader.instances;
1163 while (next != NULL) {
1164 if (next == ptr_instance) {
1165 // Remove this instance from the list:
Jon Ashburn063cd372015-04-08 15:49:00 -06001166 for (i = 0; i < ptr_instance->extension_count; i++) {
1167 free(ptr_instance->extension_names[i]);
1168 }
Jon Ashburn349508d2015-01-26 14:51:40 -07001169 if (prev)
1170 prev->next = next->next;
Jon Ashburn2cabd252015-02-03 09:26:59 -07001171 else
1172 loader.instances = next->next;
Jon Ashburn349508d2015-01-26 14:51:40 -07001173 break;
1174 }
1175 prev = next;
1176 next = next->next;
1177 }
1178 if (next == NULL) {
1179 // This must be an invalid instance handle or empty list
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001180 return VK_ERROR_INVALID_HANDLE;
Jon Ashburn349508d2015-01-26 14:51:40 -07001181 }
1182
Jon Ashburn3336df82015-01-29 15:45:51 -07001183 // cleanup any prior layer initializations
1184 loader_deactivate_layer(ptr_instance);
Jon Ashburn349508d2015-01-26 14:51:40 -07001185
Jon Ashburn3336df82015-01-29 15:45:51 -07001186 scanned_icds = loader.scanned_icd_list;
1187 while (scanned_icds) {
Tony Barbour22a30862015-04-22 09:02:32 -06001188 if (scanned_icds->instance) {
Jon Ashburn3336df82015-01-29 15:45:51 -07001189 res = scanned_icds->DestroyInstance(scanned_icds->instance);
Tony Barbour22a30862015-04-22 09:02:32 -06001190 if (res != VK_SUCCESS)
1191 loader_log(VK_DBG_MSG_WARNING, 0,
1192 "ICD ignored: failed to DestroyInstance on device");
1193 }
Mike Stroyan230e6252015-04-17 12:36:38 -06001194 scanned_icds->instance = VK_NULL_HANDLE;
Jon Ashburn3336df82015-01-29 15:45:51 -07001195 scanned_icds = scanned_icds->next;
1196 }
1197
Jon Ashburn349508d2015-01-26 14:51:40 -07001198 free(ptr_instance);
1199
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001200 return VK_SUCCESS;
Jon Ashburn349508d2015-01-26 14:51:40 -07001201}
1202
Jon Ashburn07b309a2015-04-15 11:31:12 -06001203LOADER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
Courtney Goeltzenleuchter9f530cb2015-04-20 12:48:54 -06001204 VkInstance instance,
1205 uint32_t* pPhysicalDeviceCount,
1206 VkPhysicalDevice* pPhysicalDevices)
Jon Ashburn349508d2015-01-26 14:51:40 -07001207{
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001208 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1209 struct loader_icd *icd;
Jon Ashburn07b309a2015-04-15 11:31:12 -06001210 uint32_t n, count = 0;
Tony Barbour22a30862015-04-22 09:02:32 -06001211 VkResult res = VK_ERROR_UNKNOWN;
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001212
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001213 //in spirit of VK don't error check on the instance parameter
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001214 icd = ptr_instance->icds;
Jon Ashburn07b309a2015-04-15 11:31:12 -06001215 if (pPhysicalDevices == NULL) {
1216 while (icd) {
1217 res = icd->scanned_icds->EnumeratePhysicalDevices(
1218 icd->scanned_icds->instance,
1219 &n, NULL);
1220 if (res != VK_SUCCESS)
1221 return res;
1222 icd->gpu_count = n;
1223 count += n;
1224 icd = icd->next;
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001225 }
1226
Jon Ashburn07b309a2015-04-15 11:31:12 -06001227 ptr_instance->total_gpu_count = count;
Courtney Goeltzenleuchterba7133b2015-02-10 18:40:14 -07001228
Jon Ashburn07b309a2015-04-15 11:31:12 -06001229 } else
1230 {
Tony Barbour8205d902015-04-16 15:59:00 -06001231 VkPhysicalDevice* gpus;
Jon Ashburn07b309a2015-04-15 11:31:12 -06001232 if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
1233 return VK_ERROR_INVALID_VALUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001234 gpus = malloc( sizeof(VkPhysicalDevice) * *pPhysicalDeviceCount);
Jon Ashburn07b309a2015-04-15 11:31:12 -06001235 if (!gpus)
Tony Barbour8205d902015-04-16 15:59:00 -06001236 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jon Ashburn07b309a2015-04-15 11:31:12 -06001237 while (icd) {
1238 VkBaseLayerObject * wrapped_gpus;
1239 PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
1240
Courtney Goeltzenleuchter9f530cb2015-04-20 12:48:54 -06001241 n = *pPhysicalDeviceCount;
Jon Ashburn07b309a2015-04-15 11:31:12 -06001242 res = icd->scanned_icds->EnumeratePhysicalDevices(
1243 icd->scanned_icds->instance,
1244 &n,
1245 gpus);
1246 if (res == VK_SUCCESS && n) {
1247 wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1248 sizeof(VkBaseLayerObject));
1249 icd->gpus = wrapped_gpus;
1250 icd->gpu_count = n;
1251 icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1252 sizeof(VkLayerDispatchTable));
1253 for (unsigned int i = 0; i < n; i++) {
1254 (wrapped_gpus + i)->baseObject = gpus[i];
1255 (wrapped_gpus + i)->pGPA = get_proc_addr;
1256 (wrapped_gpus + i)->nextObject = gpus[i];
1257 memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
1258 loader_init_dispatch_table(icd->loader_dispatch + i,
1259 get_proc_addr, gpus[i]);
1260
1261 /* Verify ICD compatibility */
Tony Barbour11e76ac2015-04-20 16:28:46 -06001262 if (!valid_loader_magic_value(gpus[i])) {
Jon Ashburn07b309a2015-04-15 11:31:12 -06001263 loader_log(VK_DBG_MSG_WARNING, 0,
Courtney Goeltzenleuchterba7133b2015-02-10 18:40:14 -07001264 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
Jon Ashburn07b309a2015-04-15 11:31:12 -06001265 assert(0);
1266 }
1267
1268 const VkLayerDispatchTable **disp;
1269 disp = (const VkLayerDispatchTable **) gpus[i];
1270 *disp = icd->loader_dispatch + i;
1271 loader_activate_layers(icd, i, ptr_instance->extension_count,
1272 (const char *const*) ptr_instance->extension_names);
Courtney Goeltzenleuchterba7133b2015-02-10 18:40:14 -07001273 }
1274
Jon Ashburn07b309a2015-04-15 11:31:12 -06001275 count += n;
1276
1277 if (count >= *pPhysicalDeviceCount) {
1278 break;
1279 }
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001280 }
1281
Jon Ashburn07b309a2015-04-15 11:31:12 -06001282 icd = icd->next;
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001283 }
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001284 }
1285
Jon Ashburn07b309a2015-04-15 11:31:12 -06001286 *pPhysicalDeviceCount = count;
Jon Ashburnb048a9b2015-01-28 19:57:09 -07001287
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001288 return (count > 0) ? VK_SUCCESS : res;
Jon Ashburn349508d2015-01-26 14:51:40 -07001289}
1290
Tony Barbour8205d902015-04-16 15:59:00 -06001291LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char * pName)
Jon Ashburn349508d2015-01-26 14:51:40 -07001292{
Mike Stroyan230e6252015-04-17 12:36:38 -06001293 if (gpu == VK_NULL_HANDLE) {
Jon Ashburne18431b2015-04-13 18:10:06 -06001294
1295 /* return entrypoint addresses that are global (in the loader)*/
1296 return globalGetProcAddr(pName);
Ian Elliott81ac44c2015-01-13 17:52:38 -07001297 }
Jon Ashburne18431b2015-04-13 18:10:06 -06001298
Chia-I Wu38e5a2c2015-01-04 11:12:47 +08001299 void *addr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001300
Jon Ashburne18431b2015-04-13 18:10:06 -06001301 /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1302 make sure the loader entrypoint is returned */
1303 addr = loader_non_passthrough_gpa(pName);
Ian Elliottfdf00b62015-04-15 12:53:19 -06001304 if (addr) {
Jon Ashburne18431b2015-04-13 18:10:06 -06001305 return addr;
Ian Elliottfdf00b62015-04-15 12:53:19 -06001306 }
Jon Ashburne18431b2015-04-13 18:10:06 -06001307
1308 /* return the dispatch table entrypoint for the fastest case */
1309 const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) gpu;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001310 if (disp_table == NULL)
1311 return NULL;
1312
Chia-I Wu38e5a2c2015-01-04 11:12:47 +08001313 addr = loader_lookup_dispatch_table(disp_table, pName);
1314 if (addr)
1315 return addr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001316 else {
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001317 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001318 return NULL;
Jon Ashburne18431b2015-04-13 18:10:06 -06001319 return disp_table->GetProcAddr(gpu, pName);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001320 }
1321}
1322
Jon Ashburneb2728b2015-04-10 14:33:07 -06001323//TODO make sure createInstance enables extensions that are valid (loader does)
1324//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1325
1326//TODO how is layer extension going to be enabled?
1327//Need to call createInstance on the layer or something
1328
1329LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
1330 VkExtensionInfoType infoType,
1331 uint32_t extensionIndex,
1332 size_t* pDataSize,
1333 void* pData)
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07001334{
Jon Ashburneb2728b2015-04-10 14:33:07 -06001335 VkExtensionProperties *ext_props;
1336 uint32_t *count;
1337 /* Scan/discover all ICD libraries in a single-threaded manner */
1338 loader_platform_thread_once(&once_icd, loader_icd_scan);
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07001339
Jon Ashburneb2728b2015-04-10 14:33:07 -06001340 /* get layer libraries in a single-threaded manner */
1341 loader_platform_thread_once(&once_layer, layer_lib_scan);
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07001342
Jon Ashburneb2728b2015-04-10 14:33:07 -06001343 /* merge any duplicate extensions */
1344 loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1345
1346
1347 if (pDataSize == NULL)
1348 return VK_ERROR_INVALID_POINTER;
1349
1350 switch (infoType) {
1351 case VK_EXTENSION_INFO_TYPE_COUNT:
1352 *pDataSize = sizeof(uint32_t);
1353 if (pData == NULL)
1354 return VK_SUCCESS;
1355 count = (uint32_t *) pData;
1356 *count = loader.scanned_ext_list_count;
1357 break;
1358 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1359 *pDataSize = sizeof(VkExtensionProperties);
1360 if (pData == NULL)
1361 return VK_SUCCESS;
1362 if (extensionIndex >= loader.scanned_ext_list_count)
1363 return VK_ERROR_INVALID_VALUE;
1364 ext_props = (VkExtensionProperties *) pData;
1365 ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1366 strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1367 , VK_MAX_EXTENSION_NAME);
1368 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1369 break;
1370 default:
1371 loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1372 return VK_ERROR_INVALID_VALUE;
1373 };
1374
1375 return VK_SUCCESS;
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07001376}
1377
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001378LOADER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001379{
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001380 size_t maxLayerCount;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001381 uint32_t gpu_index;
Ian Elliott64f74a82015-02-04 12:06:46 -07001382 size_t count = 0;
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001383 char *lib_name;
Jon Ashburn301c5f02015-04-06 10:58:22 -06001384 struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
Ian Elliott81ac44c2015-01-13 17:52:38 -07001385 loader_platform_dl_handle handle;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001386 PFN_vkEnumerateLayers fpEnumerateLayers;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001387 char layer_buf[16][256];
1388 char * layers[16];
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001389
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001390 if (pLayerCount == NULL || pOutLayers == NULL)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001391 return VK_ERROR_INVALID_POINTER;
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001392
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001393 maxLayerCount = *pLayerCount;
1394
Jon Ashburn14275da2015-01-28 11:01:35 -07001395 if (!icd)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001396 return VK_ERROR_UNAVAILABLE;
Jon Ashburn14275da2015-01-28 11:01:35 -07001397
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001398 for (int i = 0; i < 16; i++)
1399 layers[i] = &layer_buf[i][0];
1400
1401 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
Jon Ashburneb2728b2015-04-10 14:33:07 -06001402 lib_name = loader.scanned_layers[j].name;
Ian Elliott81ac44c2015-01-13 17:52:38 -07001403 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1404 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001405 continue;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001406 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1407 //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001408 char *pEnd, *cpyStr;
Ian Elliott642f8922015-02-13 14:29:21 -07001409 size_t siz;
Ian Elliott81ac44c2015-01-13 17:52:38 -07001410 loader_platform_close_library(handle);
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001411 lib_name = basename(lib_name);
1412 pEnd = strrchr(lib_name, '.');
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001413 siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001414 if (pEnd == NULL || siz <= 0)
1415 continue;
1416 cpyStr = malloc(siz);
1417 if (cpyStr == NULL) {
1418 free(cpyStr);
1419 continue;
1420 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001421 strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001422 cpyStr[siz - 1] = '\0';
1423 if (siz > maxStringSize)
Ian Elliott64f74a82015-02-04 12:06:46 -07001424 siz = (int) maxStringSize;
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001425 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1426 pOutLayers[count][siz - 1] = '\0';
1427 count++;
1428 free(cpyStr);
Courtney Goeltzenleuchter0199e952015-02-27 15:19:33 -07001429 } else {
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001430 size_t cnt = 16; /* only allow 16 layers, for now */
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001431 uint32_t n;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001432 VkResult res;
Ian Elliott64f74a82015-02-04 12:06:46 -07001433 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001434 res = fpEnumerateLayers((VkPhysicalDevice) NULL, n, &cnt, layers, (char *) icd->gpus + gpu_index);
Ian Elliott81ac44c2015-01-13 17:52:38 -07001435 loader_platform_close_library(handle);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001436 if (res != VK_SUCCESS)
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001437 continue;
1438 if (cnt + count > maxLayerCount)
1439 cnt = maxLayerCount - count;
Ian Elliott64f74a82015-02-04 12:06:46 -07001440 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001441 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1442 if (n > 0)
1443 pOutLayers[i - count][n - 1] = '\0';
1444 }
1445 count += cnt;
1446 }
1447 }
1448
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -06001449 *pLayerCount = count;
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001450
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001451 return VK_SUCCESS;
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001452}
1453
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001454LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001455{
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001456 const struct loader_icd *icd;
Jon Ashburndc67ef52015-01-29 16:44:24 -07001457 struct loader_instance *inst;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001458 VkResult res;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001459 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001460
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001461 if (instance == VK_NULL_HANDLE)
1462 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -06001463
1464 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001465
Jon Ashburndc67ef52015-01-29 16:44:24 -07001466 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001467 if ((VkInstance) inst == instance)
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001468 break;
1469 }
1470
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001471 if (inst == VK_NULL_HANDLE)
1472 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001473
1474 for (icd = inst->icds; icd; icd = icd->next) {
1475 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1476 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburndc67ef52015-01-29 16:44:24 -07001477 pfnMsgCallback, pUserData);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001478 if (res != VK_SUCCESS) {
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001479 gpu_idx = i;
Jon Ashburndc67ef52015-01-29 16:44:24 -07001480 break;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001481 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001482 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001483 if (res != VK_SUCCESS)
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001484 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001485 }
1486
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001487
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001488 /* roll back on errors */
1489 if (icd) {
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001490 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburndc67ef52015-01-29 16:44:24 -07001491 tmp = tmp->next) {
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001492 for (uint32_t i = 0; i < icd->gpu_count; i++)
1493 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001494 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001495 /* and gpus on current icd */
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001496 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -06001497 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001498
1499 return res;
1500 }
1501
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001502 return VK_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001503}
1504
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001505LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001506{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001507 VkResult res = VK_SUCCESS;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001508 struct loader_instance *inst;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001509 if (instance == VK_NULL_HANDLE)
1510 return VK_ERROR_INVALID_HANDLE;
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -06001511
1512 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001513
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001514 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001515 if ((VkInstance) inst == instance)
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001516 break;
1517 }
1518
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001519 if (inst == VK_NULL_HANDLE)
1520 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001521
1522 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1523 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001524 VkResult r;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001525 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001526 if (r != VK_SUCCESS) {
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001527 res = r;
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001528 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001529 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001530 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001531 return res;
1532}
1533
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001534LOADER_EXPORT VkResult VKAPI vkDbgSetGlobalOption(VkInstance instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001535{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001536 VkResult res = VK_SUCCESS;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001537 struct loader_instance *inst;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001538 if (instance == VK_NULL_HANDLE)
1539 return VK_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001540
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -06001541 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001542
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001543 for (inst = loader.instances; inst; inst = inst->next) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001544 if ((VkInstance) inst == instance)
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001545 break;
1546 }
1547
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001548 if (inst == VK_NULL_HANDLE)
1549 return VK_ERROR_INVALID_HANDLE;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001550 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1551 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001552 VkResult r;
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001553 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburndc67ef52015-01-29 16:44:24 -07001554 dataSize, pData);
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001555 /* unfortunately we cannot roll back */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001556 if (r != VK_SUCCESS) {
Jon Ashburnc0db2af2015-03-17 13:47:55 -06001557 res = r;
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001558 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001559 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001560 }
1561
1562 return res;
1563}