1/*
2 * Copyright © 2017 Google
3 * Copyright © 2019 Red Hat
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * 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 DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25/* Rules for device selection.
26 * Is there an X or wayland connection open (or DISPLAY set).
27 * If no - try and find which device was the boot_vga device.
28 * If yes - try and work out which device is the connection primary,
29 * DRI_PRIME tagged overrides only work if bus info, =1 will just pick an alternate.
30 */
31
32#include <vulkan/vk_layer.h>
33
34#include <assert.h>
35#include <stdio.h>
36#include <string.h>
37#include <fcntl.h>
38#include <unistd.h>
39
40#include "device_select.h"
41#include "c99_compat.h"
42#include "hash_table.h"
43#include "vk_util.h"
44#include "c11/threads.h"
45
46struct instance_info {
47   PFN_vkDestroyInstance DestroyInstance;
48   PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
49   PFN_vkEnumeratePhysicalDeviceGroups EnumeratePhysicalDeviceGroups;
50   PFN_vkGetInstanceProcAddr GetInstanceProcAddr;
51   PFN_GetPhysicalDeviceProcAddr  GetPhysicalDeviceProcAddr;
52   PFN_vkEnumerateDeviceExtensionProperties EnumerateDeviceExtensionProperties;
53   PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties;
54   PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2;
55   bool has_pci_bus, has_vulkan11;
56   bool has_wayland, has_xcb;
57};
58
59static struct hash_table *device_select_instance_ht = NULL;
60static mtx_t device_select_mutex;
61
62static once_flag device_select_is_init = ONCE_FLAG_INIT;
63
64static void device_select_once_init(void) {
65   mtx_init(&device_select_mutex, mtx_plain);
66}
67
68static void
69device_select_init_instances(void)
70{
71   call_once(&device_select_is_init, device_select_once_init);
72
73   mtx_lock(&device_select_mutex);
74   if (!device_select_instance_ht)
75      device_select_instance_ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
76							  _mesa_key_pointer_equal);
77   mtx_unlock(&device_select_mutex);
78}
79
80static void
81device_select_try_free_ht(void)
82{
83   mtx_lock(&device_select_mutex);
84   if (device_select_instance_ht) {
85      if (_mesa_hash_table_num_entries(device_select_instance_ht) == 0) {
86	 _mesa_hash_table_destroy(device_select_instance_ht, NULL);
87	 device_select_instance_ht = NULL;
88      }
89   }
90   mtx_unlock(&device_select_mutex);
91}
92
93static void
94device_select_layer_add_instance(VkInstance instance, struct instance_info *info)
95{
96   device_select_init_instances();
97   mtx_lock(&device_select_mutex);
98   _mesa_hash_table_insert(device_select_instance_ht, instance, info);
99   mtx_unlock(&device_select_mutex);
100}
101
102static struct instance_info *
103device_select_layer_get_instance(VkInstance instance)
104{
105   struct hash_entry *entry;
106   struct instance_info *info = NULL;
107   mtx_lock(&device_select_mutex);
108   entry = _mesa_hash_table_search(device_select_instance_ht, (void *)instance);
109   if (entry)
110      info = (struct instance_info *)entry->data;
111   mtx_unlock(&device_select_mutex);
112   return info;
113}
114
115static void
116device_select_layer_remove_instance(VkInstance instance)
117{
118   mtx_lock(&device_select_mutex);
119   _mesa_hash_table_remove_key(device_select_instance_ht, instance);
120   mtx_unlock(&device_select_mutex);
121   device_select_try_free_ht();
122}
123
124static VkResult device_select_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
125					     const VkAllocationCallbacks *pAllocator,
126					     VkInstance *pInstance)
127{
128   VkLayerInstanceCreateInfo *chain_info;
129   for(chain_info = (VkLayerInstanceCreateInfo*)pCreateInfo->pNext; chain_info; chain_info = (VkLayerInstanceCreateInfo*)chain_info->pNext)
130      if(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == VK_LAYER_LINK_INFO)
131         break;
132
133   assert(chain_info->u.pLayerInfo);
134   struct instance_info *info = (struct instance_info *)calloc(1, sizeof(struct instance_info));
135
136   info->GetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
137   PFN_vkCreateInstance fpCreateInstance =
138      (PFN_vkCreateInstance)info->GetInstanceProcAddr(NULL, "vkCreateInstance");
139   if (fpCreateInstance == NULL) {
140      free(info);
141      return VK_ERROR_INITIALIZATION_FAILED;
142   }
143
144   chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
145
146   VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
147   if (result != VK_SUCCESS) {
148      free(info);
149      return result;
150   }
151
152   for (unsigned i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
153#ifdef VK_USE_PLATFORM_WAYLAND_KHR
154      if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME))
155         info->has_wayland = true;
156#endif
157#ifdef VK_USE_PLATFORM_XCB_KHR
158      if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME))
159         info->has_xcb = true;
160#endif
161   }
162
163   /*
164    * The loader is currently not able to handle GetPhysicalDeviceProperties2KHR calls in
165    * EnumeratePhysicalDevices when there are other layers present. To avoid mysterious crashes
166    * for users just use only the vulkan version for now.
167    */
168   info->has_vulkan11 = pCreateInfo->pApplicationInfo &&
169                        pCreateInfo->pApplicationInfo->apiVersion >= VK_MAKE_VERSION(1, 1, 0);
170
171   info->GetPhysicalDeviceProcAddr = (PFN_GetPhysicalDeviceProcAddr)info->GetInstanceProcAddr(*pInstance, "vk_layerGetPhysicalDeviceProcAddr");
172#define DEVSEL_GET_CB(func) info->func = (PFN_vk##func)info->GetInstanceProcAddr(*pInstance, "vk" #func)
173   DEVSEL_GET_CB(DestroyInstance);
174   DEVSEL_GET_CB(EnumeratePhysicalDevices);
175   DEVSEL_GET_CB(EnumeratePhysicalDeviceGroups);
176   DEVSEL_GET_CB(GetPhysicalDeviceProperties);
177   DEVSEL_GET_CB(EnumerateDeviceExtensionProperties);
178   if (info->has_vulkan11)
179      DEVSEL_GET_CB(GetPhysicalDeviceProperties2);
180#undef DEVSEL_GET_CB
181
182   device_select_layer_add_instance(*pInstance, info);
183
184   return VK_SUCCESS;
185}
186
187static void device_select_DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
188{
189   struct instance_info *info = device_select_layer_get_instance(instance);
190
191   device_select_layer_remove_instance(instance);
192   info->DestroyInstance(instance, pAllocator);
193   free(info);
194}
195
196static void get_device_properties(const struct instance_info *info, VkPhysicalDevice device, VkPhysicalDeviceProperties2 *properties)
197{
198    info->GetPhysicalDeviceProperties(device, &properties->properties);
199
200    if (info->GetPhysicalDeviceProperties2 && properties->properties.apiVersion >= VK_API_VERSION_1_1)
201        info->GetPhysicalDeviceProperties2(device, properties);
202}
203
204static void print_gpu(const struct instance_info *info, unsigned index, VkPhysicalDevice device)
205{
206   const char *type = "";
207   VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) {
208      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
209   };
210   VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){
211      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR
212   };
213   if (info->has_vulkan11 && info->has_pci_bus)
214      properties.pNext = &ext_pci_properties;
215   get_device_properties(info, device, &properties);
216
217   switch(properties.properties.deviceType) {
218   case VK_PHYSICAL_DEVICE_TYPE_OTHER:
219   default:
220      type = "other";
221      break;
222   case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
223      type = "integrated GPU";
224      break;
225   case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
226      type = "discrete GPU";
227      break;
228   case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
229      type = "virtual GPU";
230      break;
231   case VK_PHYSICAL_DEVICE_TYPE_CPU:
232      type = "CPU";
233      break;
234   }
235   fprintf(stderr, "  GPU %d: %x:%x \"%s\" %s", index, properties.properties.vendorID,
236           properties.properties.deviceID, properties.properties.deviceName, type);
237   if (info->has_pci_bus)
238      fprintf(stderr, " %04x:%02x:%02x.%x", ext_pci_properties.pciDomain,
239              ext_pci_properties.pciBus, ext_pci_properties.pciDevice,
240              ext_pci_properties.pciFunction);
241   fprintf(stderr, "\n");
242}
243
244static bool fill_drm_device_info(const struct instance_info *info,
245                                 struct device_pci_info *drm_device,
246                                 VkPhysicalDevice device)
247{
248   VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) {
249      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
250   };
251
252   VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){
253      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR
254   };
255
256   if (info->has_vulkan11 && info->has_pci_bus)
257      properties.pNext = &ext_pci_properties;
258   get_device_properties(info, device, &properties);
259
260   drm_device->cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
261   drm_device->dev_info.vendor_id = properties.properties.vendorID;
262   drm_device->dev_info.device_id = properties.properties.deviceID;
263   if (info->has_vulkan11 && info->has_pci_bus) {
264     drm_device->has_bus_info = true;
265     drm_device->bus_info.domain = ext_pci_properties.pciDomain;
266     drm_device->bus_info.bus = ext_pci_properties.pciBus;
267     drm_device->bus_info.dev = ext_pci_properties.pciDevice;
268     drm_device->bus_info.func = ext_pci_properties.pciFunction;
269   }
270   return drm_device->cpu_device;
271}
272
273static int device_select_find_explicit_default(struct device_pci_info *pci_infos,
274                                               uint32_t device_count,
275                                               const char *selection)
276{
277   int default_idx = -1;
278   unsigned vendor_id, device_id;
279   int matched = sscanf(selection, "%x:%x", &vendor_id, &device_id);
280   if (matched != 2)
281      return default_idx;
282
283   for (unsigned i = 0; i < device_count; ++i) {
284      if (pci_infos[i].dev_info.vendor_id == vendor_id &&
285          pci_infos[i].dev_info.device_id == device_id)
286         default_idx = i;
287   }
288   return default_idx;
289}
290
291static int device_select_find_dri_prime_tag_default(struct device_pci_info *pci_infos,
292                                                    uint32_t device_count,
293                                                    const char *dri_prime)
294{
295   int default_idx = -1;
296   for (unsigned i = 0; i < device_count; ++i) {
297      char *tag = NULL;
298      if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
299                   pci_infos[i].bus_info.domain,
300                   pci_infos[i].bus_info.bus,
301                   pci_infos[i].bus_info.dev,
302                   pci_infos[i].bus_info.func) >= 0) {
303         if (strcmp(dri_prime, tag))
304            default_idx = i;
305      }
306      free(tag);
307   }
308   return default_idx;
309}
310
311static int device_select_find_boot_vga_default(struct device_pci_info *pci_infos,
312                                               uint32_t device_count)
313{
314   char boot_vga_path[1024];
315   int default_idx = -1;
316   for (unsigned i = 0; i < device_count; ++i) {
317      /* fallback to probing the pci bus boot_vga device. */
318      snprintf(boot_vga_path, 1023, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/boot_vga", pci_infos[i].bus_info.domain,
319               pci_infos[i].bus_info.bus, pci_infos[i].bus_info.dev, pci_infos[i].bus_info.func);
320      int fd = open(boot_vga_path, O_RDONLY);
321      if (fd != -1) {
322         uint8_t val;
323         if (read(fd, &val, 1) == 1) {
324            if (val == '1')
325               default_idx = i;
326         }
327         close(fd);
328      }
329      if (default_idx != -1)
330         break;
331   }
332   return default_idx;
333}
334
335static int device_select_find_non_cpu(struct device_pci_info *pci_infos,
336                                      uint32_t device_count)
337{
338   int default_idx = -1;
339
340   /* pick first GPU device */
341   for (unsigned i = 0; i < device_count; ++i) {
342      if (!pci_infos[i].cpu_device){
343         default_idx = i;
344         break;
345      }
346   }
347   return default_idx;
348}
349
350static int find_non_cpu_skip(struct device_pci_info *pci_infos,
351                        uint32_t device_count,
352                        int skip_idx)
353{
354   for (unsigned i = 0; i < device_count; ++i) {
355      if (i == skip_idx)
356         continue;
357      if (pci_infos[i].cpu_device)
358         continue;
359      return i;
360   }
361   return -1;
362}
363
364static uint32_t get_default_device(const struct instance_info *info,
365                                   const char *selection,
366                                   uint32_t physical_device_count,
367                                   VkPhysicalDevice *pPhysicalDevices)
368{
369   int default_idx = -1;
370   const char *dri_prime = getenv("DRI_PRIME");
371   bool dri_prime_is_one = false;
372   int cpu_count = 0;
373   if (dri_prime && !strcmp(dri_prime, "1"))
374      dri_prime_is_one = true;
375
376   if (dri_prime && !dri_prime_is_one && !info->has_pci_bus) {
377      fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n");
378   }
379
380   struct device_pci_info *pci_infos = (struct device_pci_info *)calloc(physical_device_count, sizeof(struct device_pci_info));
381   if (!pci_infos)
382     return 0;
383
384   for (unsigned i = 0; i < physical_device_count; ++i) {
385      cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0;
386   }
387
388   if (selection)
389      default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, selection);
390   if (default_idx == -1 && info->has_pci_bus && dri_prime && !dri_prime_is_one)
391      default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count, dri_prime);
392   if (default_idx == -1 && info->has_wayland)
393      default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count);
394   if (default_idx == -1 && info->has_xcb)
395      default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count);
396   if (default_idx == -1 && info->has_pci_bus)
397      default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count);
398   if (default_idx == -1 && cpu_count)
399      default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
400
401   /* DRI_PRIME=1 handling - pick any other device than default. */
402   if (default_idx != -1 && dri_prime_is_one && physical_device_count > (cpu_count + 1)) {
403      if (default_idx == 0 || default_idx == 1)
404         default_idx = find_non_cpu_skip(pci_infos, physical_device_count, default_idx);
405   }
406   free(pci_infos);
407   return default_idx == -1 ? 0 : default_idx;
408}
409
410static VkResult device_select_EnumeratePhysicalDevices(VkInstance instance,
411						       uint32_t* pPhysicalDeviceCount,
412						       VkPhysicalDevice *pPhysicalDevices)
413{
414   struct instance_info *info = device_select_layer_get_instance(instance);
415   uint32_t physical_device_count = 0;
416   uint32_t selected_physical_device_count = 0;
417   const char* selection = getenv("MESA_VK_DEVICE_SELECT");
418   VkResult result = info->EnumeratePhysicalDevices(instance, &physical_device_count, NULL);
419   VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
420   if (result != VK_SUCCESS)
421      return result;
422
423   VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),  physical_device_count);
424   VkPhysicalDevice *selected_physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),
425                                                                           physical_device_count);
426
427   if (!physical_devices || !selected_physical_devices) {
428      result = VK_ERROR_OUT_OF_HOST_MEMORY;
429      goto out;
430   }
431
432   result = info->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices);
433   if (result != VK_SUCCESS)
434      goto out;
435
436   for (unsigned i = 0; i < physical_device_count; i++) {
437      uint32_t count;
438      info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, NULL);
439      if (count > 0) {
440	 VkExtensionProperties *extensions = calloc(count, sizeof(VkExtensionProperties));
441         if (info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, extensions) == VK_SUCCESS) {
442	    for (unsigned j = 0; j < count; j++) {
443               if (!strcmp(extensions[j].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME))
444                  info->has_pci_bus = true;
445            }
446         }
447	 free(extensions);
448      }
449   }
450   if (selection && strcmp(selection, "list") == 0) {
451      fprintf(stderr, "selectable devices:\n");
452      for (unsigned i = 0; i < physical_device_count; ++i)
453         print_gpu(info, i, physical_devices[i]);
454      exit(0);
455   } else {
456      unsigned selected_index = get_default_device(info, selection, physical_device_count, physical_devices);
457      selected_physical_device_count = physical_device_count;
458      selected_physical_devices[0] = physical_devices[selected_index];
459      for (unsigned i = 0; i < physical_device_count - 1; ++i) {
460         unsigned  this_idx = i < selected_index ? i : i + 1;
461         selected_physical_devices[i + 1] = physical_devices[this_idx];
462      }
463   }
464
465   if (selected_physical_device_count == 0) {
466      fprintf(stderr, "WARNING: selected no devices with MESA_VK_DEVICE_SELECT\n");
467   }
468
469   assert(result == VK_SUCCESS);
470
471   for (unsigned i = 0; i < selected_physical_device_count; i++) {
472      vk_outarray_append(&out, ent) {
473         *ent = selected_physical_devices[i];
474      }
475   }
476   result = vk_outarray_status(&out);
477 out:
478   free(physical_devices);
479   free(selected_physical_devices);
480   return result;
481}
482
483static VkResult device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,
484                                                            uint32_t* pPhysicalDeviceGroupCount,
485                                                            VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroups)
486{
487   struct instance_info *info = device_select_layer_get_instance(instance);
488   uint32_t physical_device_group_count = 0;
489   uint32_t selected_physical_device_group_count = 0;
490   VkResult result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, NULL);
491   VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroups, pPhysicalDeviceGroupCount);
492
493   if (result != VK_SUCCESS)
494      return result;
495
496   VkPhysicalDeviceGroupProperties *physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
497   VkPhysicalDeviceGroupProperties *selected_physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
498
499   if (!physical_device_groups || !selected_physical_device_groups) {
500      result = VK_ERROR_OUT_OF_HOST_MEMORY;
501      goto out;
502   }
503
504   result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, physical_device_groups);
505   if (result != VK_SUCCESS)
506      goto out;
507
508   /* just sort groups with CPU devices to the end? - assume nobody will mix these */
509   int num_gpu_groups = 0;
510   int num_cpu_groups = 0;
511   selected_physical_device_group_count = physical_device_group_count;
512   for (unsigned i = 0; i < physical_device_group_count; i++) {
513      bool group_has_cpu_device = false;
514      for (unsigned j = 0; j < physical_device_groups[i].physicalDeviceCount; j++) {
515         VkPhysicalDevice physical_device = physical_device_groups[i].physicalDevices[j];
516         VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){
517            .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR
518         };
519         info->GetPhysicalDeviceProperties(physical_device, &properties.properties);
520         group_has_cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
521      }
522
523      if (group_has_cpu_device) {
524         selected_physical_device_groups[physical_device_group_count - num_cpu_groups - 1] = physical_device_groups[i];
525         num_cpu_groups++;
526      } else {
527         selected_physical_device_groups[num_gpu_groups] = physical_device_groups[i];
528         num_gpu_groups++;
529      }
530   }
531
532   assert(result == VK_SUCCESS);
533
534   for (unsigned i = 0; i < selected_physical_device_group_count; i++) {
535      vk_outarray_append(&out, ent) {
536         *ent = selected_physical_device_groups[i];
537      }
538   }
539   result = vk_outarray_status(&out);
540out:
541   free(physical_device_groups);
542   free(selected_physical_device_groups);
543   return result;
544}
545
546static void  (*get_pdevice_proc_addr(VkInstance instance, const char* name))()
547{
548   struct instance_info *info = device_select_layer_get_instance(instance);
549   return info->GetPhysicalDeviceProcAddr(instance, name);
550}
551
552static void  (*get_instance_proc_addr(VkInstance instance, const char* name))()
553{
554   if (strcmp(name, "vkGetInstanceProcAddr") == 0)
555      return (void(*)())get_instance_proc_addr;
556   if (strcmp(name, "vkCreateInstance") == 0)
557      return (void(*)())device_select_CreateInstance;
558   if (strcmp(name, "vkDestroyInstance") == 0)
559      return (void(*)())device_select_DestroyInstance;
560   if (strcmp(name, "vkEnumeratePhysicalDevices") == 0)
561      return (void(*)())device_select_EnumeratePhysicalDevices;
562   if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)
563      return (void(*)())device_select_EnumeratePhysicalDeviceGroups;
564
565   struct instance_info *info = device_select_layer_get_instance(instance);
566   return info->GetInstanceProcAddr(instance, name);
567}
568
569VK_LAYER_EXPORT VkResult vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)
570{
571   if (pVersionStruct->loaderLayerInterfaceVersion < 2)
572      return VK_ERROR_INITIALIZATION_FAILED;
573   pVersionStruct->loaderLayerInterfaceVersion = 2;
574
575   pVersionStruct->pfnGetInstanceProcAddr = get_instance_proc_addr;
576   pVersionStruct->pfnGetPhysicalDeviceProcAddr = get_pdevice_proc_addr;
577
578   return VK_SUCCESS;
579}
580