loader.c revision a938abb9d45b990819e37f09c3d107a461114c8e
1/*
2 * Vulkan
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *   Chia-I Wu <olv@lunarg.com>
26 *   Jon Ashburn <jon@lunarg.com>
27 *   Courtney Goeltzenleuchter <courtney@lunarg.com>
28 *   Ian Elliott <ian@lunarg.com>
29 */
30#define _GNU_SOURCE
31#include <stdio.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdbool.h>
35#include <string.h>
36
37#include <sys/types.h>
38#if defined(WIN32)
39#include "dirent_on_windows.h"
40#else // WIN32
41#include <dirent.h>
42#endif // WIN32
43#include "loader_platform.h"
44#include "table_ops.h"
45#include "gpa_helper.h"
46#include "loader.h"
47#include "vkIcd.h"
48// The following is #included again to catch certain OS-specific functions
49// being used:
50#include "loader_platform.h"
51
52struct loader_layers {
53    loader_platform_dl_handle lib_handle;
54    char name[256];
55};
56
57struct layer_name_pair {
58    char *layer_name;
59    const char *lib_name;
60};
61
62struct 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
68struct loader_icd {
69    const struct loader_scanned_icds *scanned_icds;
70
71    VkLayerDispatchTable *loader_dispatch;
72    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];
75    uint32_t gpu_count;
76    VkBaseLayerObject *gpus;
77
78    struct loader_icd *next;
79};
80
81
82struct loader_scanned_icds {
83    loader_platform_dl_handle handle;
84
85    PFN_vkGetProcAddr GetProcAddr;
86    PFN_vkCreateInstance CreateInstance;
87    PFN_vkDestroyInstance DestroyInstance;
88    PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
89    PFN_vkGetGlobalExtensionInfo GetGlobalExtensionInfo;
90    VkInstance instance;
91    struct loader_scanned_icds *next;
92    uint32_t extension_count;
93    struct extension_property *extensions;
94};
95
96struct loader_scanned_layers {
97    char *name;
98    uint32_t extension_count;
99    struct extension_property *extensions;
100};
101// Note: Since the following is a static structure, all members are initialized
102// to zero.
103static struct {
104    struct loader_instance *instances;
105    bool icds_scanned;
106    struct loader_scanned_icds *scanned_icd_list;
107    bool layer_scanned;
108    char *layer_dirs;
109    unsigned int scanned_layer_count;
110    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;
114} loader;
115
116static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
117static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
118static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
119
120#if defined(WIN32)
121char *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;
128    LONG  rtn_value;
129    char *rtn_str = NULL;
130    DWORD rtn_len = 0;
131    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,
146                                (PVOID) rtn_str, &rtn_len);
147    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,
153                                    (PVOID) rtn_str, &rtn_len);
154        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
172// 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);
179    char *registry_str = NULL;
180    size_t registry_len = 0;
181    char *rtn_str = NULL;
182    size_t rtn_len;
183
184    registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
185                                              "Software\\Vulkan",
186                                              registry_value);
187    registry_len = (registry_str) ? strlen(registry_str) : 0;
188
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
200    if (registry_len == 0) {
201        // 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
213    if (registry_str) {
214      free(registry_str);
215    }
216
217    return(rtn_str);
218}
219#endif // WIN32
220
221
222static void loader_log(VK_DBG_MSG_TYPE msg_type, int32_t msg_code,
223                       const char *format, ...)
224{
225    char msg[256];
226    va_list ap;
227    int ret;
228
229    va_start(ap, format);
230    ret = vsnprintf(msg, sizeof(msg), format, ap);
231    if ((ret >= (int) sizeof(msg)) || ret < 0) {
232        msg[sizeof(msg) - 1] = '\0';
233    }
234    va_end(ap);
235
236    fputs(msg, stderr);
237    fputc('\n', stderr);
238}
239
240static bool has_extension(struct extension_property *exts, uint32_t count,
241                          const char *name, bool must_be_hosted)
242{
243    uint32_t i;
244    for (i = 0; i < count; i++) {
245        if (!strcmp(name, exts[i].extName) && (!must_be_hosted || exts[i].hosted))
246            return true;
247    }
248    return false;
249}
250
251static void get_global_extensions(PFN_vkGetGlobalExtensionInfo fp_get,
252                                  uint32_t *count_out,
253                                  struct extension_property **props_out)
254{
255    uint32_t i, count, cur;
256    size_t siz = sizeof(count);
257    struct extension_property *ext_props;
258    VkExtensionProperties vk_prop;
259    VkResult res;
260
261    *count_out = 0;
262    *props_out = NULL;
263    res = fp_get(VK_EXTENSION_INFO_TYPE_COUNT, 0, &siz, &count);
264    if (res != VK_SUCCESS) {
265        loader_log(VK_DBG_MSG_WARNING, 0, "Error getting global extension count from ICD");
266        return;
267    }
268    ext_props = (struct extension_property *) malloc(sizeof(struct extension_property) * count);
269    if (ext_props == NULL) {
270        loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory didn't get global extensions from ICD");
271        return;
272    }
273    siz = sizeof(VkExtensionProperties);
274    cur = 0;
275    for (i = 0; i < count; i++) {
276        res = fp_get(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &siz, &vk_prop);
277        if (res == VK_SUCCESS) {
278            (ext_props + cur)->hosted = false;
279            (ext_props + cur)->version = vk_prop.version;
280            strncpy((ext_props + cur)->extName, vk_prop.extName, VK_MAX_EXTENSION_NAME);
281            (ext_props + cur)->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
282            cur++;
283        }
284        *count_out = cur;
285        *props_out = ext_props;
286    }
287    return;
288}
289
290static void loader_init_ext_list()
291{
292    loader.scanned_ext_list_capacity = 256 * sizeof(struct extension_property *);
293    loader.scanned_ext_list = malloc(loader.scanned_ext_list_capacity);
294    memset(loader.scanned_ext_list, 0, loader.scanned_ext_list_capacity);
295    loader.scanned_ext_list_count = 0;
296}
297
298#if 0 // currently no place to call this
299static void loader_destroy_ext_list()
300{
301    free(loader.scanned_ext_list);
302    loader.scanned_ext_list_capacity = 0;
303    loader.scanned_ext_list_count = 0;
304}
305#endif
306
307static void loader_add_to_ext_list(uint32_t count,
308                                   struct extension_property *prop_list,
309                                   bool is_layer_ext)
310{
311    uint32_t i, j;
312    bool duplicate;
313    struct extension_property *cur_ext;
314
315    if (loader.scanned_ext_list == NULL || loader.scanned_ext_list_capacity == 0)
316        loader_init_ext_list();
317
318    if (loader.scanned_ext_list == NULL)
319        return;
320
321    struct extension_property *ext_list, **ext_list_addr;
322
323    for (i = 0; i < count; i++) {
324        cur_ext = prop_list + i;
325
326        // look for duplicates or not
327        duplicate = false;
328        for (j = 0; j < loader.scanned_ext_list_count; j++) {
329            ext_list = loader.scanned_ext_list[j];
330            if (!strcmp(cur_ext->extName, ext_list->extName)) {
331                duplicate = true;
332                ext_list->hosted = false;
333                break;
334            }
335        }
336
337        // add to list at end
338        if (!duplicate) {
339            // check for enough capacity
340            if (loader.scanned_ext_list_count * sizeof(struct extension_property *)
341                            >= loader.scanned_ext_list_capacity) {
342                // double capacity
343                loader.scanned_ext_list_capacity *= 2;
344                loader.scanned_ext_list = realloc(loader.scanned_ext_list,
345                        loader.scanned_ext_list_capacity);
346            }
347            ext_list_addr = &(loader.scanned_ext_list[loader.scanned_ext_list_count++]);
348            *ext_list_addr = cur_ext;
349            cur_ext->hosted = true;
350        }
351
352    }
353}
354
355static bool loader_is_extension_scanned(const char *name)
356{
357    uint32_t i;
358
359    for (i = 0; i < loader.scanned_ext_list_count; i++) {
360        if (!strcmp(name, loader.scanned_ext_list[i]->extName))
361            return true;
362    }
363    return false;
364}
365
366static void loader_coalesce_extensions(void)
367{
368    uint32_t i;
369    struct loader_scanned_icds *icd_list = loader.scanned_icd_list;
370
371    // traverse scanned icd list adding non-duplicate extensions to the list
372    while (icd_list != NULL) {
373        loader_add_to_ext_list(icd_list->extension_count, icd_list->extensions, false);
374        icd_list = icd_list->next;
375    };
376
377    //Traverse layers list adding non-duplicate extensions to the list
378    for (i = 0; i < loader.scanned_layer_count; i++) {
379        loader_add_to_ext_list(loader.scanned_layers[i].extension_count,
380                loader.scanned_layers[i].extensions, true);
381    }
382}
383
384static void loader_icd_destroy(struct loader_icd *icd)
385{
386    loader_platform_close_library(icd->scanned_icds->handle);
387    free(icd);
388}
389
390static struct loader_icd * loader_icd_create(const struct loader_scanned_icds *scanned)
391{
392    struct loader_icd *icd;
393
394    icd = malloc(sizeof(*icd));
395    if (!icd)
396        return NULL;
397
398    memset(icd, 0, sizeof(*icd));
399
400    icd->scanned_icds = scanned;
401
402    return icd;
403}
404
405static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
406                                     const struct loader_scanned_icds *scanned)
407{
408    struct loader_icd *icd;
409
410    icd = loader_icd_create(scanned);
411    if (!icd)
412        return NULL;
413
414    /* prepend to the list */
415    icd->next = ptr_inst->icds;
416    ptr_inst->icds = icd;
417
418    return icd;
419}
420
421static void loader_scanned_icd_add(const char *filename)
422{
423    loader_platform_dl_handle handle;
424    void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
425    void *fp_get_global_ext_info;
426    struct loader_scanned_icds *new_node;
427
428    // Used to call: dlopen(filename, RTLD_LAZY);
429    handle = loader_platform_open_library(filename);
430    if (!handle) {
431        loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
432        return;
433    }
434
435#define LOOKUP(func_ptr, func) do {                            \
436    func_ptr = (PFN_vk ##func) loader_platform_get_proc_address(handle, "vk" #func); \
437    if (!func_ptr) {                                           \
438        loader_log(VK_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("vk" #func)); \
439        return;                                                \
440    }                                                          \
441} while (0)
442
443    LOOKUP(fp_gpa, GetProcAddr);
444    LOOKUP(fp_create_inst, CreateInstance);
445    LOOKUP(fp_destroy_inst, DestroyInstance);
446    LOOKUP(fp_enumerate, EnumeratePhysicalDevices);
447    LOOKUP(fp_get_global_ext_info, GetGlobalExtensionInfo);
448#undef LOOKUP
449
450    new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
451    if (!new_node) {
452        loader_log(VK_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
453        return;
454    }
455
456    new_node->handle = handle;
457    new_node->GetProcAddr = fp_gpa;
458    new_node->CreateInstance = fp_create_inst;
459    new_node->DestroyInstance = fp_destroy_inst;
460    new_node->EnumeratePhysicalDevices = fp_enumerate;
461    new_node->GetGlobalExtensionInfo = fp_get_global_ext_info;
462    new_node->extension_count = 0;
463    new_node->extensions = NULL;
464    new_node->next = loader.scanned_icd_list;
465
466    loader.scanned_icd_list = new_node;
467
468    if (fp_get_global_ext_info) {
469        get_global_extensions((PFN_vkGetGlobalExtensionInfo) fp_get_global_ext_info,
470                       &new_node->extension_count,
471                       &new_node->extensions);
472    } else {
473        loader_log(VK_DBG_MSG_WARNING, 0, "Couldn't get global extensions from ICD");
474    }
475}
476
477/**
478 * Try to \c loader_icd_scan VK driver(s).
479 *
480 * This function scans the default system path or path
481 * specified by the \c LIBVK_DRIVERS_PATH environment variable in
482 * order to find loadable VK ICDs with the name of libVK_*.
483 *
484 * \returns
485 * void; but side effect is to set loader_icd_scanned to true
486 */
487static void loader_icd_scan(void)
488{
489    const char *p, *next;
490    char *libPaths = NULL;
491    DIR *sysdir;
492    struct dirent *dent;
493    char icd_library[1024];
494    char path[1024];
495    uint32_t len;
496#if defined(WIN32)
497    bool must_free_libPaths;
498    libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
499                                           DRIVER_PATH_REGISTRY_VALUE);
500    if (libPaths != NULL) {
501        must_free_libPaths = true;
502    } else {
503        must_free_libPaths = false;
504        libPaths = DEFAULT_VK_DRIVERS_PATH;
505    }
506#else  // WIN32
507    if (geteuid() == getuid()) {
508        /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
509        libPaths = getenv(DRIVER_PATH_ENV);
510    }
511    if (libPaths == NULL) {
512        libPaths = DEFAULT_VK_DRIVERS_PATH;
513    }
514#endif // WIN32
515
516    for (p = libPaths; *p; p = next) {
517       next = strchr(p, PATH_SEPERATOR);
518       if (next == NULL) {
519          len = (uint32_t) strlen(p);
520          next = p + len;
521       }
522       else {
523          len = (uint32_t) (next - p);
524          sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
525          p = path;
526          next++;
527       }
528
529       // TODO/TBD: Do we want to do this on Windows, or just let Windows take
530       // care of its own search path (which it apparently has)?
531       sysdir = opendir(p);
532       if (sysdir) {
533          dent = readdir(sysdir);
534          while (dent) {
535             /* Look for ICDs starting with VK_DRIVER_LIBRARY_PREFIX and
536              * ending with VK_LIBRARY_SUFFIX
537              */
538              if (!strncmp(dent->d_name,
539                          VK_DRIVER_LIBRARY_PREFIX,
540                          VK_DRIVER_LIBRARY_PREFIX_LEN)) {
541                 uint32_t nlen = (uint32_t) strlen(dent->d_name);
542                 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
543                 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
544                     !strncmp(suf,
545                              VK_LIBRARY_SUFFIX,
546                              VK_LIBRARY_SUFFIX_LEN)) {
547                    snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
548                    loader_scanned_icd_add(icd_library);
549                 }
550              }
551
552             dent = readdir(sysdir);
553          }
554          closedir(sysdir);
555       }
556    }
557
558#if defined(WIN32)
559    // Free any allocated memory:
560    if (must_free_libPaths) {
561        free(libPaths);
562    }
563#endif // WIN32
564
565    // Note that we've scanned for ICDs:
566    loader.icds_scanned = true;
567}
568
569
570static void layer_lib_scan(void)
571{
572    const char *p, *next;
573    char *libPaths = NULL;
574    DIR *curdir;
575    struct dirent *dent;
576    size_t len, i;
577    char temp_str[1024];
578    uint32_t count;
579    PFN_vkGetGlobalExtensionInfo fp_get_ext;
580
581#if defined(WIN32)
582    bool must_free_libPaths;
583    libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
584                                           LAYERS_PATH_REGISTRY_VALUE);
585    if (libPaths != NULL) {
586        must_free_libPaths = true;
587    } else {
588        must_free_libPaths = false;
589        libPaths = DEFAULT_VK_LAYERS_PATH;
590    }
591#else  // WIN32
592    if (geteuid() == getuid()) {
593        /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
594        libPaths = getenv(LAYERS_PATH_ENV);
595    }
596    if (libPaths == NULL) {
597        libPaths = DEFAULT_VK_LAYERS_PATH;
598    }
599#endif // WIN32
600
601    if (libPaths == NULL) {
602        // Have no paths to search:
603        return;
604    }
605    len = strlen(libPaths);
606    loader.layer_dirs = malloc(len+1);
607    if (loader.layer_dirs == NULL) {
608        free(libPaths);
609        return;
610    }
611    // Alloc passed, so we know there is enough space to hold the string, don't
612    // need strncpy
613    strcpy(loader.layer_dirs, libPaths);
614#if defined(WIN32)
615    // Free any allocated memory:
616    if (must_free_libPaths) {
617        free(libPaths);
618        must_free_libPaths = false;
619    }
620#endif // WIN32
621    libPaths = loader.layer_dirs;
622
623    /* cleanup any previously scanned libraries */
624    for (i = 0; i < loader.scanned_layer_count; i++) {
625        if (loader.scanned_layers[i].name != NULL)
626            free(loader.scanned_layers[i].name);
627        if (loader.scanned_layers[i].extensions != NULL)
628            free(loader.scanned_layers[i].extensions);
629        loader.scanned_layers[i].name = NULL;
630        loader.scanned_layers[i].extensions = NULL;
631    }
632    loader.scanned_layer_count = 0;
633    count = 0;
634
635    for (p = libPaths; *p; p = next) {
636        next = strchr(p, PATH_SEPERATOR);
637        if (next == NULL) {
638           len = (uint32_t) strlen(p);
639           next = p + len;
640        }
641        else {
642           len = (uint32_t) (next - p);
643           *(char *) next = '\0';
644           next++;
645        }
646
647       curdir = opendir(p);
648       if (curdir) {
649          dent = readdir(curdir);
650          while (dent) {
651             /* Look for layers starting with VK_LAYER_LIBRARY_PREFIX and
652              * ending with VK_LIBRARY_SUFFIX
653              */
654              if (!strncmp(dent->d_name,
655                          VK_LAYER_LIBRARY_PREFIX,
656                          VK_LAYER_LIBRARY_PREFIX_LEN)) {
657                 uint32_t nlen = (uint32_t) strlen(dent->d_name);
658                 const char *suf = dent->d_name + nlen - VK_LIBRARY_SUFFIX_LEN;
659                 if ((nlen > VK_LIBRARY_SUFFIX_LEN) &&
660                     !strncmp(suf,
661                              VK_LIBRARY_SUFFIX,
662                              VK_LIBRARY_SUFFIX_LEN)) {
663                     loader_platform_dl_handle handle;
664                     snprintf(temp_str, sizeof(temp_str),
665                              "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
666                     // Used to call: dlopen(temp_str, RTLD_LAZY)
667                     if ((handle = loader_platform_open_library(temp_str)) == NULL) {
668                         dent = readdir(curdir);
669                         continue;
670                     }
671                     if (count == MAX_LAYER_LIBRARIES) {
672                         loader_log(VK_DBG_MSG_ERROR, 0,
673                                    "%s ignored: max layer libraries exceed",
674                                    temp_str);
675                         break;
676                     }
677                     fp_get_ext = loader_platform_get_proc_address(handle,
678                                               "vkGetGlobalExtensionInfo");
679
680                     if (!fp_get_ext) {
681                        loader_log(VK_DBG_MSG_WARNING, 0,
682                              "Couldn't dlsym vkGetGlobalExtensionInfo from library %s",
683                               temp_str);
684                        dent = readdir(curdir);
685                        loader_platform_close_library(handle);
686                        continue;
687                     }
688
689                     loader.scanned_layers[count].name =
690                                                   malloc(strlen(temp_str) + 1);
691                     if (loader.scanned_layers[count].name == NULL) {
692                         loader_log(VK_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
693                         break;
694                     }
695
696                     get_global_extensions(fp_get_ext,
697                             &loader.scanned_layers[count].extension_count,
698                             &loader.scanned_layers[count].extensions);
699
700
701                    strcpy(loader.scanned_layers[count].name, temp_str);
702                    count++;
703                    loader_platform_close_library(handle);
704                 }
705             }
706
707             dent = readdir(curdir);
708          } // while (dir_entry)
709          if (count == MAX_LAYER_LIBRARIES)
710             break;
711          closedir(curdir);
712       } // if (curdir))
713    } // for (libpaths)
714
715    loader.scanned_layer_count = count;
716    loader.layer_scanned = true;
717}
718
719static void loader_init_dispatch_table(VkLayerDispatchTable *tab, PFN_vkGetProcAddr fpGPA, VkPhysicalDevice gpu)
720{
721    loader_initialize_dispatch_table(tab, fpGPA, gpu);
722
723    if (tab->EnumerateLayers == NULL)
724        tab->EnumerateLayers = vkEnumerateLayers;
725}
726
727static void * VKAPI loader_gpa_internal(VkPhysicalDevice gpu, const char * pName)
728{
729    if (gpu == VK_NULL_HANDLE) {
730        return NULL;;
731    }
732    VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
733    VkLayerDispatchTable * disp_table = * (VkLayerDispatchTable **) gpuw->baseObject;
734    void *addr;
735
736    if (disp_table == NULL)
737        return NULL;
738
739    addr = loader_lookup_dispatch_table(disp_table, pName);
740    if (addr)
741        return addr;
742    else  {
743        if (disp_table->GetProcAddr == NULL)
744            return NULL;
745        return disp_table->GetProcAddr(gpuw->nextObject, pName);
746    }
747}
748
749extern struct loader_icd * loader_get_icd(const VkBaseLayerObject *gpu, uint32_t *gpu_index)
750{
751    /*
752     * NOTE: at this time icd->gpus is pointing to wrapped GPUs, but no where else
753     * are wrapped gpus used. Should go away. The incoming gpu is NOT wrapped so
754     * need to test it against the wrapped GPU's base object.
755     */
756    for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
757        for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
758            for (uint32_t i = 0; i < icd->gpu_count; i++)
759                if ((icd->gpus + i) == gpu || (void*)(icd->gpus +i)->baseObject == gpu) {
760                    *gpu_index = i;
761                    return icd;
762                }
763        }
764    }
765    return NULL;
766}
767
768static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
769{
770    if (icd->layer_count[gpu_index])
771        return true;
772    else
773        return false;
774}
775
776static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index,
777                                   struct layer_name_pair * pLayerNames,
778                                   uint32_t count)
779{
780    if (!icd)
781        return;
782
783    struct loader_layers *obj;
784    bool foundLib;
785    for (uint32_t i = 0; i < count; i++) {
786        foundLib = false;
787        for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
788            if (icd->layer_libs[gpu_index][j].lib_handle &&
789                !strcmp(icd->layer_libs[gpu_index][j].name,
790                (char *) pLayerNames[i].layer_name) &&
791                strcmp("Validation", (char *) pLayerNames[i].layer_name)) {
792                foundLib = true;
793                break;
794            }
795        }
796        if (!foundLib) {
797            obj = &(icd->layer_libs[gpu_index][i]);
798            strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
799            obj->name[sizeof(obj->name) - 1] = '\0';
800            // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
801            if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
802                loader_log(VK_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
803                continue;
804            } else {
805                loader_log(VK_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s",
806                           pLayerNames[i].layer_name, pLayerNames[i].lib_name);
807            }
808            free(pLayerNames[i].layer_name);
809            icd->layer_count[gpu_index]++;
810        }
811    }
812}
813
814static bool find_layer_extension(struct loader_icd *icd, uint32_t gpu_index,
815                                 const char *pExtName, uint32_t *out_count,
816                                 char *lib_name[MAX_LAYER_LIBRARIES])
817{
818    char *search_name;
819    uint32_t j, found_count = 0;
820    bool must_be_hosted;
821    bool found = false;
822
823    /*
824     * The loader provides the abstraction that make layers and extensions work via
825     * the currently defined extension mechanism. That is, when app queries for an extension
826     * via vkGetGlobalExtensionInfo, the loader will call both the driver as well as any layers
827     * to see who implements that extension. Then, if the app enables the extension during
828     * vkCreateInstance the loader will find and load any layers that implement that extension.
829     */
830
831    // TODO: what about GetPhysicalDeviceExtension for device specific layers/extensions
832
833    for (j = 0; j < loader.scanned_layer_count; j++) {
834
835        if (!strcmp("Validation", pExtName))
836            must_be_hosted = false;
837        else
838            must_be_hosted = true;
839        if (has_extension(loader.scanned_layers[j].extensions,
840                          loader.scanned_layers[j].extension_count, pExtName,
841                          must_be_hosted)) {
842
843            found = true;
844            lib_name[found_count] = loader.scanned_layers[j].name;
845            found_count++;
846        } else {
847            // Extension not found in list for the layer, so test the layer name
848            // as if it is an extension name. Use default layer name based on
849            // library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
850            char *pEnd;
851            size_t siz;
852
853            search_name = loader.scanned_layers[j].name;
854            search_name = basename(search_name);
855            search_name += strlen(VK_LAYER_LIBRARY_PREFIX);
856            pEnd = strrchr(search_name, '.');
857            siz = (int) (pEnd - search_name);
858            if (siz != strlen(pExtName))
859                continue;
860
861            if (strncmp(search_name, pExtName, siz) == 0) {
862                found = true;
863                lib_name[found_count] = loader.scanned_layers[j].name;
864                found_count++;
865            }
866        }
867    }
868
869    *out_count = found_count;
870    return found;
871}
872
873static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames)
874{
875    char *layerEnv;
876    uint32_t i, len, found_count, count = 0;
877    char *p, *pOrig, *next, *name;
878
879#if defined(WIN32)
880    layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
881                                           LAYER_NAMES_REGISTRY_VALUE);
882#else  // WIN32
883    layerEnv = getenv(LAYER_NAMES_ENV);
884#endif // WIN32
885    if (layerEnv == NULL) {
886        return 0;
887    }
888    p = malloc(strlen(layerEnv) + 1);
889    if (p == NULL) {
890#if defined(WIN32)
891        free(layerEnv);
892#endif // WIN32
893        return 0;
894    }
895    strcpy(p, layerEnv);
896#if defined(WIN32)
897    free(layerEnv);
898#endif // WIN32
899    pOrig = p;
900
901    while (p && *p && count < MAX_LAYER_LIBRARIES) {
902        char *lib_name[MAX_LAYER_LIBRARIES];
903        //memset(&lib_name[0], 0, sizeof(const char *) * MAX_LAYER_LIBRARIES);
904        next = strchr(p, PATH_SEPERATOR);
905        if (next == NULL) {
906            len = (uint32_t) strlen(p);
907            next = p + len;
908        } else {
909            len = (uint32_t) (next - p);
910            *(char *) next = '\0';
911            next++;
912        }
913        name = basename(p);
914        if (!find_layer_extension(icd, gpu_index, name, &found_count, lib_name)) {
915            p = next;
916            continue;
917        }
918
919        for (i = 0; i < found_count; i++) {
920            len = (uint32_t) strlen(name);
921            pLayerNames[count].layer_name = malloc(len + 1);
922            if (!pLayerNames[count].layer_name) {
923                free(pOrig);
924                return count;
925            }
926            strncpy((char *) pLayerNames[count].layer_name, name, len);
927            pLayerNames[count].layer_name[len] = '\0';
928            pLayerNames[count].lib_name = lib_name[i];
929            count++;
930        }
931        p = next;
932
933    }
934
935    free(pOrig);
936    return count;
937}
938
939static 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)
940{
941    static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
942    char *lib_name[MAX_LAYER_LIBRARIES];
943    uint32_t found_count, count = 0;
944    bool skip;
945
946    *ppLayerNames =  &layerNames[0];
947    /* Load any layers specified in the environment first */
948    count = loader_get_layer_env(icd, gpu_index, layerNames);
949
950    for (uint32_t i = 0; i < ext_count; i++) {
951        const char *pExtName = ext_names[i];
952
953        skip = false;
954        for (uint32_t j = 0; j < count; j++) {
955            if (!strcmp(pExtName, layerNames[j].layer_name) ) {
956                // Extension / Layer already on the list skip it
957                skip = true;
958                break;
959            }
960        }
961
962        if (!skip && find_layer_extension(icd, gpu_index, pExtName, &found_count, lib_name)) {
963
964            for (uint32_t j = 0; j < found_count; j++) {
965                uint32_t len;
966                len = (uint32_t) strlen(pExtName);
967
968
969                layerNames[count].layer_name = malloc(len + 1);
970                if (!layerNames[count].layer_name)
971                    return count;
972                strncpy((char *) layerNames[count].layer_name, pExtName, len);
973                layerNames[count].layer_name[len] = '\0';
974                layerNames[count].lib_name = lib_name[j];
975                count++;
976            }
977        }
978    }
979
980    return count;
981}
982
983static void loader_deactivate_layer(const struct loader_instance *instance)
984{
985    struct loader_icd *icd;
986    struct loader_layers *libs;
987
988    for (icd = instance->icds; icd; icd = icd->next) {
989        if (icd->gpus)
990            free(icd->gpus);
991        icd->gpus = NULL;
992        if (icd->loader_dispatch)
993            free(icd->loader_dispatch);
994        icd->loader_dispatch = NULL;
995        for (uint32_t j = 0; j < icd->gpu_count; j++) {
996            if (icd->layer_count[j] > 0) {
997                for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
998                    libs = &(icd->layer_libs[j][i]);
999                    if (libs->lib_handle)
1000                        loader_platform_close_library(libs->lib_handle);
1001                    libs->lib_handle = NULL;
1002                }
1003                if (icd->wrappedGpus[j])
1004                    free(icd->wrappedGpus[j]);
1005            }
1006            icd->layer_count[j] = 0;
1007        }
1008        icd->gpu_count = 0;
1009    }
1010}
1011
1012extern uint32_t loader_activate_layers(struct loader_icd *icd, uint32_t gpu_index, uint32_t ext_count, const char *const* ext_names)
1013{
1014    uint32_t count;
1015    VkBaseLayerObject *gpu;
1016    struct layer_name_pair *pLayerNames;
1017    if (!icd)
1018        return 0;
1019    assert(gpu_index < MAX_GPUS_FOR_LAYER);
1020
1021    gpu = icd->gpus + gpu_index;
1022    /* activate any layer libraries */
1023    if (!loader_layers_activated(icd, gpu_index)) {
1024        VkBaseLayerObject *gpuObj = gpu;
1025        VkBaseLayerObject *nextGpuObj, *baseObj = (VkBaseLayerObject *) gpuObj->baseObject;
1026        PFN_vkGetProcAddr nextGPA = loader_gpa_internal;
1027
1028        count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
1029        if (!count)
1030            return 0;
1031        loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
1032
1033        icd->wrappedGpus[gpu_index] = malloc(sizeof(VkBaseLayerObject) * icd->layer_count[gpu_index]);
1034        if (! icd->wrappedGpus[gpu_index])
1035                loader_log(VK_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
1036        for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
1037            nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
1038            nextGpuObj->pGPA = nextGPA;
1039            nextGpuObj->baseObject = (VkObject) baseObj;
1040            nextGpuObj->nextObject = (VkObject) gpuObj;
1041            gpuObj = nextGpuObj;
1042
1043            char funcStr[256];
1044            snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
1045            if ((nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
1046                nextGPA = (PFN_vkGetProcAddr) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "vkGetProcAddr");
1047            if (!nextGPA) {
1048                loader_log(VK_DBG_MSG_ERROR, 0, "Failed to find vkGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
1049                continue;
1050            }
1051
1052            if (i == 0) {
1053                loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, (VkPhysicalDevice) gpuObj);
1054                //Insert the new wrapped objects into the list with loader object at head
1055                gpu->nextObject = (VkObject) gpuObj;
1056                gpu->pGPA = nextGPA;
1057                gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
1058                gpuObj->nextObject = (VkObject) baseObj;
1059                gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
1060            }
1061
1062        }
1063    }
1064    else {
1065        //make sure requested Layers matches currently activated Layers
1066        count = loader_get_layer_libs(icd, gpu_index, ext_count, ext_names, &pLayerNames);
1067        for (uint32_t i = 0; i < count; i++) {
1068            if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
1069                loader_log(VK_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
1070                break;
1071            }
1072        }
1073        if (count != icd->layer_count[gpu_index]) {
1074            loader_log(VK_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
1075        }
1076    }
1077    return icd->layer_count[gpu_index];
1078}
1079
1080LOADER_EXPORT VkResult VKAPI vkCreateInstance(
1081        const VkInstanceCreateInfo*         pCreateInfo,
1082        VkInstance*                           pInstance)
1083{
1084    struct loader_instance *ptr_instance = NULL;
1085    struct loader_scanned_icds *scanned_icds;
1086    struct loader_icd *icd;
1087    VkResult res = VK_ERROR_INITIALIZATION_FAILED;
1088    uint32_t i;
1089
1090    /* Scan/discover all ICD libraries in a single-threaded manner */
1091    loader_platform_thread_once(&once_icd, loader_icd_scan);
1092
1093    /* get layer libraries in a single-threaded manner */
1094    loader_platform_thread_once(&once_layer, layer_lib_scan);
1095
1096    /* merge any duplicate extensions */
1097    loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1098
1099    ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
1100    if (ptr_instance == NULL) {
1101        return VK_ERROR_OUT_OF_HOST_MEMORY;
1102    }
1103    memset(ptr_instance, 0, sizeof(struct loader_instance));
1104    ptr_instance->extension_count = pCreateInfo->extensionCount;
1105    ptr_instance->extension_names = (ptr_instance->extension_count > 0) ?
1106                malloc(sizeof (char *) * ptr_instance->extension_count) : NULL;
1107    if (ptr_instance->extension_names == NULL && (ptr_instance->extension_count > 0))
1108        return VK_ERROR_OUT_OF_HOST_MEMORY;
1109    for (i = 0; i < ptr_instance->extension_count; i++) {
1110        if (!loader_is_extension_scanned(pCreateInfo->ppEnabledExtensionNames[i]))
1111            return VK_ERROR_INVALID_EXTENSION;
1112        ptr_instance->extension_names[i] = malloc(strlen(pCreateInfo->ppEnabledExtensionNames[i]) + 1);
1113        if (ptr_instance->extension_names[i] == NULL)
1114            return VK_ERROR_OUT_OF_HOST_MEMORY;
1115        strcpy(ptr_instance->extension_names[i], pCreateInfo->ppEnabledExtensionNames[i]);
1116    }
1117    ptr_instance->next = loader.instances;
1118    loader.instances = ptr_instance;
1119
1120    scanned_icds = loader.scanned_icd_list;
1121    while (scanned_icds) {
1122        icd = loader_icd_add(ptr_instance, scanned_icds);
1123        if (icd) {
1124            res = scanned_icds->CreateInstance(pCreateInfo,
1125                                           &(scanned_icds->instance));
1126            if (res != VK_SUCCESS)
1127            {
1128                ptr_instance->icds = ptr_instance->icds->next;
1129                loader_icd_destroy(icd);
1130                scanned_icds->instance = VK_NULL_HANDLE;
1131                loader_log(VK_DBG_MSG_WARNING, 0,
1132                        "ICD ignored: failed to CreateInstance on device");
1133            }
1134        }
1135        scanned_icds = scanned_icds->next;
1136    }
1137
1138    if (ptr_instance->icds == NULL) {
1139        return VK_ERROR_INCOMPATIBLE_DRIVER;
1140    }
1141
1142    *pInstance = (VkInstance) ptr_instance;
1143    return VK_SUCCESS;
1144}
1145
1146LOADER_EXPORT VkResult VKAPI vkDestroyInstance(
1147        VkInstance                                instance)
1148{
1149    struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1150    struct loader_scanned_icds *scanned_icds;
1151    VkResult res;
1152    uint32_t i;
1153
1154    // Remove this instance from the list of instances:
1155    struct loader_instance *prev = NULL;
1156    struct loader_instance *next = loader.instances;
1157    while (next != NULL) {
1158        if (next == ptr_instance) {
1159            // Remove this instance from the list:
1160            for (i = 0; i < ptr_instance->extension_count; i++) {
1161                free(ptr_instance->extension_names[i]);
1162            }
1163            if (prev)
1164                prev->next = next->next;
1165            else
1166                loader.instances = next->next;
1167            break;
1168        }
1169        prev = next;
1170        next = next->next;
1171    }
1172    if (next  == NULL) {
1173        // This must be an invalid instance handle or empty list
1174        return VK_ERROR_INVALID_HANDLE;
1175    }
1176
1177    // cleanup any prior layer initializations
1178    loader_deactivate_layer(ptr_instance);
1179
1180    scanned_icds = loader.scanned_icd_list;
1181    while (scanned_icds) {
1182        if (scanned_icds->instance) {
1183            res = scanned_icds->DestroyInstance(scanned_icds->instance);
1184            if (res != VK_SUCCESS)
1185                loader_log(VK_DBG_MSG_WARNING, 0,
1186                            "ICD ignored: failed to DestroyInstance on device");
1187        }
1188        scanned_icds->instance = VK_NULL_HANDLE;
1189        scanned_icds = scanned_icds->next;
1190    }
1191
1192    free(ptr_instance);
1193
1194    return VK_SUCCESS;
1195}
1196
1197LOADER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
1198        VkInstance                              instance,
1199        uint32_t*                               pPhysicalDeviceCount,
1200        VkPhysicalDevice*                       pPhysicalDevices)
1201{
1202    struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1203    struct loader_icd *icd;
1204    uint32_t n, count = 0;
1205    VkResult res = VK_ERROR_UNKNOWN;
1206
1207    //in spirit of VK don't error check on the instance parameter
1208    icd = ptr_instance->icds;
1209    if (pPhysicalDevices == NULL) {
1210        while (icd) {
1211            res = icd->scanned_icds->EnumeratePhysicalDevices(
1212                                                icd->scanned_icds->instance,
1213                                                &n, NULL);
1214            if (res != VK_SUCCESS)
1215                return res;
1216            icd->gpu_count = n;
1217            count += n;
1218            icd = icd->next;
1219        }
1220
1221        ptr_instance->total_gpu_count = count;
1222
1223    } else
1224    {
1225        VkPhysicalDevice* gpus;
1226        if (*pPhysicalDeviceCount < ptr_instance->total_gpu_count)
1227            return VK_ERROR_INVALID_VALUE;
1228        gpus = malloc( sizeof(VkPhysicalDevice) *  *pPhysicalDeviceCount);
1229        if (!gpus)
1230            return VK_ERROR_OUT_OF_HOST_MEMORY;
1231        while (icd) {
1232            VkBaseLayerObject * wrapped_gpus;
1233            PFN_vkGetProcAddr get_proc_addr = icd->scanned_icds->GetProcAddr;
1234
1235            n = *pPhysicalDeviceCount;
1236            res = icd->scanned_icds->EnumeratePhysicalDevices(
1237                                            icd->scanned_icds->instance,
1238                                            &n,
1239                                            gpus);
1240            if (res == VK_SUCCESS && n) {
1241                wrapped_gpus = (VkBaseLayerObject*) malloc(n *
1242                                            sizeof(VkBaseLayerObject));
1243                icd->gpus = wrapped_gpus;
1244                icd->gpu_count = n;
1245                icd->loader_dispatch = (VkLayerDispatchTable *) malloc(n *
1246                                        sizeof(VkLayerDispatchTable));
1247                for (unsigned int i = 0; i < n; i++) {
1248                    (wrapped_gpus + i)->baseObject = gpus[i];
1249                    (wrapped_gpus + i)->pGPA = get_proc_addr;
1250                    (wrapped_gpus + i)->nextObject = gpus[i];
1251                    memcpy(pPhysicalDevices + count, gpus, sizeof(*pPhysicalDevices));
1252                    loader_init_dispatch_table(icd->loader_dispatch + i,
1253                                               get_proc_addr, gpus[i]);
1254
1255                    /* Verify ICD compatibility */
1256                    if (!valid_loader_magic_value(gpus[i])) {
1257                        loader_log(VK_DBG_MSG_WARNING, 0,
1258                            "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
1259                        assert(0);
1260                    }
1261
1262                    const VkLayerDispatchTable **disp;
1263                    disp = (const VkLayerDispatchTable **) gpus[i];
1264                    *disp = icd->loader_dispatch + i;
1265                    loader_activate_layers(icd, i, ptr_instance->extension_count,
1266                        (const char *const*) ptr_instance->extension_names);
1267                }
1268
1269                count += n;
1270
1271                if (count >= *pPhysicalDeviceCount) {
1272                    break;
1273                }
1274            }
1275
1276            icd = icd->next;
1277        }
1278    }
1279
1280    *pPhysicalDeviceCount = count;
1281
1282    return (count > 0) ? VK_SUCCESS : res;
1283}
1284
1285LOADER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char * pName)
1286{
1287    if (gpu == VK_NULL_HANDLE) {
1288
1289        /* return entrypoint addresses that are global (in the loader)*/
1290        return globalGetProcAddr(pName);
1291    }
1292
1293    void *addr;
1294
1295    /* for entrypoints that loader must handle (ie non-dispatchable or create object)
1296       make sure the loader entrypoint is returned */
1297    addr = loader_non_passthrough_gpa(pName);
1298    if (addr) {
1299        return addr;
1300    }
1301
1302    /* return the dispatch table entrypoint for the fastest case */
1303    const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) gpu;
1304    if (disp_table == NULL)
1305        return NULL;
1306
1307    addr = loader_lookup_dispatch_table(disp_table, pName);
1308    if (addr)
1309        return addr;
1310    else  {
1311        if (disp_table->GetProcAddr == NULL)
1312            return NULL;
1313        return disp_table->GetProcAddr(gpu, pName);
1314    }
1315}
1316
1317//TODO make sure createInstance enables extensions that are valid (loader does)
1318//TODO make sure CreateDevice enables extensions that are valid (left for layers/drivers to do)
1319
1320//TODO how is layer extension going to be enabled?
1321//Need to call createInstance on the layer or something
1322
1323LOADER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
1324                                               VkExtensionInfoType infoType,
1325                                               uint32_t extensionIndex,
1326                                               size_t*  pDataSize,
1327                                               void*    pData)
1328{
1329    VkExtensionProperties *ext_props;
1330    uint32_t *count;
1331    /* Scan/discover all ICD libraries in a single-threaded manner */
1332    loader_platform_thread_once(&once_icd, loader_icd_scan);
1333
1334    /* get layer libraries in a single-threaded manner */
1335    loader_platform_thread_once(&once_layer, layer_lib_scan);
1336
1337    /* merge any duplicate extensions */
1338    loader_platform_thread_once(&once_exts, loader_coalesce_extensions);
1339
1340
1341    if (pDataSize == NULL)
1342        return VK_ERROR_INVALID_POINTER;
1343
1344    switch (infoType) {
1345        case VK_EXTENSION_INFO_TYPE_COUNT:
1346            *pDataSize = sizeof(uint32_t);
1347            if (pData == NULL)
1348                return VK_SUCCESS;
1349            count = (uint32_t *) pData;
1350            *count = loader.scanned_ext_list_count;
1351            break;
1352        case VK_EXTENSION_INFO_TYPE_PROPERTIES:
1353            *pDataSize = sizeof(VkExtensionProperties);
1354            if (pData == NULL)
1355                return VK_SUCCESS;
1356            if (extensionIndex >= loader.scanned_ext_list_count)
1357                return VK_ERROR_INVALID_VALUE;
1358            ext_props = (VkExtensionProperties *) pData;
1359            ext_props->version = loader.scanned_ext_list[extensionIndex]->version;
1360            strncpy(ext_props->extName, loader.scanned_ext_list[extensionIndex]->extName
1361                                            , VK_MAX_EXTENSION_NAME);
1362            ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
1363            break;
1364        default:
1365            loader_log(VK_DBG_MSG_WARNING, 0, "Invalid infoType in vkGetGlobalExtensionInfo");
1366            return VK_ERROR_INVALID_VALUE;
1367    };
1368
1369    return VK_SUCCESS;
1370}
1371
1372LOADER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
1373{
1374    size_t maxLayerCount;
1375    uint32_t gpu_index;
1376    size_t count = 0;
1377    char *lib_name;
1378    struct loader_icd *icd = loader_get_icd((const VkBaseLayerObject *) gpu, &gpu_index);
1379    loader_platform_dl_handle handle;
1380    PFN_vkEnumerateLayers fpEnumerateLayers;
1381    char layer_buf[16][256];
1382    char * layers[16];
1383
1384    if (pLayerCount == NULL || pOutLayers == NULL)
1385        return VK_ERROR_INVALID_POINTER;
1386
1387    maxLayerCount = *pLayerCount;
1388
1389    if (!icd)
1390        return VK_ERROR_UNAVAILABLE;
1391
1392    for (int i = 0; i < 16; i++)
1393         layers[i] = &layer_buf[i][0];
1394
1395    for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1396        lib_name = loader.scanned_layers[j].name;
1397        // Used to call: dlopen(*lib_name, RTLD_LAZY)
1398        if ((handle = loader_platform_open_library(lib_name)) == NULL)
1399            continue;
1400        if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "vkEnumerateLayers")) == NULL) {
1401            //use default layer name based on library name VK_LAYER_LIBRARY_PREFIX<name>.VK_LIBRARY_SUFFIX
1402            char *pEnd, *cpyStr;
1403            size_t siz;
1404            loader_platform_close_library(handle);
1405            lib_name = basename(lib_name);
1406            pEnd = strrchr(lib_name, '.');
1407            siz = (int) (pEnd - lib_name - strlen(VK_LAYER_LIBRARY_PREFIX) + 1);
1408            if (pEnd == NULL || siz <= 0)
1409                continue;
1410            cpyStr = malloc(siz);
1411            if (cpyStr == NULL) {
1412                free(cpyStr);
1413                continue;
1414            }
1415            strncpy(cpyStr, lib_name + strlen(VK_LAYER_LIBRARY_PREFIX), siz);
1416            cpyStr[siz - 1] = '\0';
1417            if (siz > maxStringSize)
1418                siz = (int) maxStringSize;
1419            strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1420            pOutLayers[count][siz - 1] = '\0';
1421            count++;
1422            free(cpyStr);
1423        } else {
1424            size_t cnt = 16; /* only allow 16 layers, for now */
1425            uint32_t n;
1426            VkResult res;
1427            n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
1428            res = fpEnumerateLayers((VkPhysicalDevice) NULL, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1429            loader_platform_close_library(handle);
1430            if (res != VK_SUCCESS)
1431                continue;
1432            if (cnt + count > maxLayerCount)
1433                cnt = maxLayerCount - count;
1434            for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
1435                strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1436                if (n > 0)
1437                    pOutLayers[i - count][n - 1] = '\0';
1438            }
1439            count += cnt;
1440        }
1441    }
1442
1443    *pLayerCount = count;
1444
1445    return VK_SUCCESS;
1446}
1447
1448LOADER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
1449{
1450    const struct loader_icd *icd;
1451    struct loader_instance *inst;
1452    VkResult res;
1453    uint32_t gpu_idx;
1454
1455    if (instance == VK_NULL_HANDLE)
1456        return VK_ERROR_INVALID_HANDLE;
1457
1458    assert(loader.icds_scanned);
1459
1460    for (inst = loader.instances; inst; inst = inst->next) {
1461        if ((VkInstance) inst == instance)
1462            break;
1463    }
1464
1465    if (inst == VK_NULL_HANDLE)
1466        return VK_ERROR_INVALID_HANDLE;
1467
1468    for (icd = inst->icds; icd; icd = icd->next) {
1469        for (uint32_t i = 0; i < icd->gpu_count; i++) {
1470            res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
1471                                                   pfnMsgCallback, pUserData);
1472            if (res != VK_SUCCESS) {
1473                gpu_idx = i;
1474                break;
1475            }
1476        }
1477        if (res != VK_SUCCESS)
1478            break;
1479    }
1480
1481
1482    /* roll back on errors */
1483    if (icd) {
1484        for (const struct loader_icd *tmp = inst->icds; tmp != icd;
1485                                                      tmp = tmp->next) {
1486            for (uint32_t i = 0; i < icd->gpu_count; i++)
1487                (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
1488        }
1489        /* and gpus on current icd */
1490        for (uint32_t i = 0; i < gpu_idx; i++)
1491            (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
1492
1493        return res;
1494    }
1495
1496    return VK_SUCCESS;
1497}
1498
1499LOADER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
1500{
1501    VkResult res = VK_SUCCESS;
1502    struct loader_instance *inst;
1503    if (instance == VK_NULL_HANDLE)
1504        return VK_ERROR_INVALID_HANDLE;
1505
1506    assert(loader.icds_scanned);
1507
1508    for (inst = loader.instances; inst; inst = inst->next) {
1509        if ((VkInstance) inst == instance)
1510            break;
1511    }
1512
1513    if (inst == VK_NULL_HANDLE)
1514        return VK_ERROR_INVALID_HANDLE;
1515
1516    for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1517        for (uint32_t i = 0; i < icd->gpu_count; i++) {
1518            VkResult r;
1519            r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
1520            if (r != VK_SUCCESS) {
1521                res = r;
1522            }
1523        }
1524    }
1525    return res;
1526}
1527
1528LOADER_EXPORT VkResult VKAPI vkDbgSetGlobalOption(VkInstance instance, VK_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
1529{
1530    VkResult res = VK_SUCCESS;
1531    struct loader_instance *inst;
1532    if (instance == VK_NULL_HANDLE)
1533        return VK_ERROR_INVALID_HANDLE;
1534
1535    assert(loader.icds_scanned);
1536
1537    for (inst = loader.instances; inst; inst = inst->next) {
1538        if ((VkInstance) inst == instance)
1539            break;
1540    }
1541
1542    if (inst == VK_NULL_HANDLE)
1543        return VK_ERROR_INVALID_HANDLE;
1544    for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1545        for (uint32_t i = 0; i < icd->gpu_count; i++) {
1546            VkResult r;
1547            r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
1548                                                           dataSize, pData);
1549            /* unfortunately we cannot roll back */
1550            if (r != VK_SUCCESS) {
1551               res = r;
1552            }
1553        }
1554    }
1555
1556    return res;
1557}
1558