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 
46 struct 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 
59 static struct hash_table *device_select_instance_ht = NULL;
60 static mtx_t device_select_mutex;
61 
62 static once_flag device_select_is_init = ONCE_FLAG_INIT;
63 
device_select_once_init(void)64 static void device_select_once_init(void) {
65    mtx_init(&device_select_mutex, mtx_plain);
66 }
67 
68 static void
device_select_init_instances(void)69 device_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 
80 static void
device_select_try_free_ht(void)81 device_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 
93 static void
device_select_layer_add_instance(VkInstance instance,struct instance_info * info)94 device_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 
102 static struct instance_info *
device_select_layer_get_instance(VkInstance instance)103 device_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 
115 static void
device_select_layer_remove_instance(VkInstance instance)116 device_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 
device_select_CreateInstance(const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkInstance * pInstance)124 static 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 
device_select_DestroyInstance(VkInstance instance,const VkAllocationCallbacks * pAllocator)187 static 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 
get_device_properties(const struct instance_info * info,VkPhysicalDevice device,VkPhysicalDeviceProperties2 * properties)196 static 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 
print_gpu(const struct instance_info * info,unsigned index,VkPhysicalDevice device)204 static 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_vulkan11 && 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 
fill_drm_device_info(const struct instance_info * info,struct device_pci_info * drm_device,VkPhysicalDevice device)244 static 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 
device_select_find_explicit_default(struct device_pci_info * pci_infos,uint32_t device_count,const char * selection)273 static 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 
device_select_find_dri_prime_tag_default(struct device_pci_info * pci_infos,uint32_t device_count,const char * dri_prime)291 static 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 
device_select_find_boot_vga_vid_did(struct device_pci_info * pci_infos,uint32_t device_count)311 static int device_select_find_boot_vga_vid_did(struct device_pci_info *pci_infos,
312                                                uint32_t device_count)
313 {
314    char path[1024];
315    int fd;
316    int default_idx = -1;
317    uint8_t boot_vga = 0;
318    ssize_t size_ret;
319    #pragma pack(push, 1)
320    struct id {
321       uint16_t vid;
322       uint16_t did;
323    }id;
324    #pragma pack(pop)
325 
326    for (unsigned i = 0; i < 64; i++) {
327       snprintf(path, 1023, "/sys/class/drm/card%d/device/boot_vga", i);
328       fd = open(path, O_RDONLY);
329       if (fd != -1) {
330          uint8_t val;
331          size_ret = read(fd, &val, 1);
332          close(fd);
333          if (size_ret == 1 && val == '1')
334             boot_vga = 1;
335       } else {
336          return default_idx;
337       }
338 
339       if (boot_vga) {
340          snprintf(path, 1023, "/sys/class/drm/card%d/device/config", i);
341          fd = open(path, O_RDONLY);
342          if (fd != -1) {
343             size_ret = read(fd, &id, 4);
344             close(fd);
345             if (size_ret != 4)
346                return default_idx;
347          } else {
348             return default_idx;
349          }
350          break;
351       }
352    }
353 
354    if (!boot_vga)
355       return default_idx;
356 
357    for (unsigned i = 0; i < device_count; ++i) {
358       if (id.vid == pci_infos[i].dev_info.vendor_id &&
359           id.did == pci_infos[i].dev_info.device_id) {
360          default_idx = i;
361          break;
362       }
363    }
364 
365    return default_idx;
366 }
367 
device_select_find_boot_vga_default(struct device_pci_info * pci_infos,uint32_t device_count)368 static int device_select_find_boot_vga_default(struct device_pci_info *pci_infos,
369                                                uint32_t device_count)
370 {
371    char boot_vga_path[1024];
372    int default_idx = -1;
373    for (unsigned i = 0; i < device_count; ++i) {
374       /* fallback to probing the pci bus boot_vga device. */
375       snprintf(boot_vga_path, 1023, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/boot_vga", pci_infos[i].bus_info.domain,
376                pci_infos[i].bus_info.bus, pci_infos[i].bus_info.dev, pci_infos[i].bus_info.func);
377       int fd = open(boot_vga_path, O_RDONLY);
378       if (fd != -1) {
379          uint8_t val;
380          if (read(fd, &val, 1) == 1) {
381             if (val == '1')
382                default_idx = i;
383          }
384          close(fd);
385       }
386       if (default_idx != -1)
387          break;
388    }
389    return default_idx;
390 }
391 
device_select_find_non_cpu(struct device_pci_info * pci_infos,uint32_t device_count)392 static int device_select_find_non_cpu(struct device_pci_info *pci_infos,
393                                       uint32_t device_count)
394 {
395    int default_idx = -1;
396 
397    /* pick first GPU device */
398    for (unsigned i = 0; i < device_count; ++i) {
399       if (!pci_infos[i].cpu_device){
400          default_idx = i;
401          break;
402       }
403    }
404    return default_idx;
405 }
406 
find_non_cpu_skip(struct device_pci_info * pci_infos,uint32_t device_count,int skip_idx)407 static int find_non_cpu_skip(struct device_pci_info *pci_infos,
408                         uint32_t device_count,
409                         int skip_idx)
410 {
411    for (unsigned i = 0; i < device_count; ++i) {
412       if (i == skip_idx)
413          continue;
414       if (pci_infos[i].cpu_device)
415          continue;
416       return i;
417    }
418    return -1;
419 }
420 
get_default_device(const struct instance_info * info,const char * selection,uint32_t physical_device_count,VkPhysicalDevice * pPhysicalDevices)421 static uint32_t get_default_device(const struct instance_info *info,
422                                    const char *selection,
423                                    uint32_t physical_device_count,
424                                    VkPhysicalDevice *pPhysicalDevices)
425 {
426    int default_idx = -1;
427    const char *dri_prime = getenv("DRI_PRIME");
428    bool dri_prime_is_one = false;
429    int cpu_count = 0;
430    if (dri_prime && !strcmp(dri_prime, "1"))
431       dri_prime_is_one = true;
432 
433    if (dri_prime && !dri_prime_is_one && !info->has_vulkan11 && !info->has_pci_bus) {
434       fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n");
435    }
436 
437    struct device_pci_info *pci_infos = (struct device_pci_info *)calloc(physical_device_count, sizeof(struct device_pci_info));
438    if (!pci_infos)
439      return 0;
440 
441    for (unsigned i = 0; i < physical_device_count; ++i) {
442       cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0;
443    }
444 
445    if (selection)
446       default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, selection);
447    if (default_idx == -1 && info->has_vulkan11 && info->has_pci_bus && dri_prime && !dri_prime_is_one)
448       default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count, dri_prime);
449    if (default_idx == -1 && info->has_wayland)
450       default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count);
451    if (default_idx == -1 && info->has_xcb)
452       default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count);
453    if (default_idx == -1) {
454       if (info->has_vulkan11 && info->has_pci_bus)
455          default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count);
456       else
457          default_idx = device_select_find_boot_vga_vid_did(pci_infos, physical_device_count);
458    }
459    if (default_idx == -1 && cpu_count)
460       default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
461    /* DRI_PRIME=1 handling - pick any other device than default. */
462    if (default_idx != -1 && dri_prime_is_one && physical_device_count > (cpu_count + 1)) {
463       if (default_idx == 0 || default_idx == 1)
464          default_idx = find_non_cpu_skip(pci_infos, physical_device_count, default_idx);
465    }
466    free(pci_infos);
467    return default_idx == -1 ? 0 : default_idx;
468 }
469 
device_select_EnumeratePhysicalDevices(VkInstance instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)470 static VkResult device_select_EnumeratePhysicalDevices(VkInstance instance,
471 						       uint32_t* pPhysicalDeviceCount,
472 						       VkPhysicalDevice *pPhysicalDevices)
473 {
474    struct instance_info *info = device_select_layer_get_instance(instance);
475    uint32_t physical_device_count = 0;
476    uint32_t selected_physical_device_count = 0;
477    const char* selection = getenv("MESA_VK_DEVICE_SELECT");
478    VkResult result = info->EnumeratePhysicalDevices(instance, &physical_device_count, NULL);
479    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
480    if (result != VK_SUCCESS)
481       return result;
482 
483    VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),  physical_device_count);
484    VkPhysicalDevice *selected_physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),
485                                                                            physical_device_count);
486 
487    if (!physical_devices || !selected_physical_devices) {
488       result = VK_ERROR_OUT_OF_HOST_MEMORY;
489       goto out;
490    }
491 
492    result = info->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices);
493    if (result != VK_SUCCESS)
494       goto out;
495 
496    for (unsigned i = 0; i < physical_device_count; i++) {
497       uint32_t count;
498       info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, NULL);
499       if (count > 0) {
500 	 VkExtensionProperties *extensions = calloc(count, sizeof(VkExtensionProperties));
501          if (info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, extensions) == VK_SUCCESS) {
502 	    for (unsigned j = 0; j < count; j++) {
503                if (!strcmp(extensions[j].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME))
504                   info->has_pci_bus = true;
505             }
506          }
507 	 free(extensions);
508       }
509    }
510    if (selection && strcmp(selection, "list") == 0) {
511       fprintf(stderr, "selectable devices:\n");
512       for (unsigned i = 0; i < physical_device_count; ++i)
513          print_gpu(info, i, physical_devices[i]);
514       exit(0);
515    } else {
516       unsigned selected_index = get_default_device(info, selection, physical_device_count, physical_devices);
517       selected_physical_device_count = physical_device_count;
518       selected_physical_devices[0] = physical_devices[selected_index];
519       for (unsigned i = 0; i < physical_device_count - 1; ++i) {
520          unsigned  this_idx = i < selected_index ? i : i + 1;
521          selected_physical_devices[i + 1] = physical_devices[this_idx];
522       }
523    }
524 
525    if (selected_physical_device_count == 0) {
526       fprintf(stderr, "WARNING: selected no devices with MESA_VK_DEVICE_SELECT\n");
527    }
528 
529    assert(result == VK_SUCCESS);
530 
531    for (unsigned i = 0; i < selected_physical_device_count; i++) {
532       vk_outarray_append_typed(VkPhysicalDevice, &out, ent) {
533          *ent = selected_physical_devices[i];
534       }
535    }
536    result = vk_outarray_status(&out);
537  out:
538    free(physical_devices);
539    free(selected_physical_devices);
540    return result;
541 }
542 
device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,uint32_t * pPhysicalDeviceGroupCount,VkPhysicalDeviceGroupProperties * pPhysicalDeviceGroups)543 static VkResult device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,
544                                                             uint32_t* pPhysicalDeviceGroupCount,
545                                                             VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroups)
546 {
547    struct instance_info *info = device_select_layer_get_instance(instance);
548    uint32_t physical_device_group_count = 0;
549    uint32_t selected_physical_device_group_count = 0;
550    VkResult result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, NULL);
551    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, pPhysicalDeviceGroups, pPhysicalDeviceGroupCount);
552 
553    if (result != VK_SUCCESS)
554       return result;
555 
556    VkPhysicalDeviceGroupProperties *physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
557    VkPhysicalDeviceGroupProperties *selected_physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
558 
559    if (!physical_device_groups || !selected_physical_device_groups) {
560       result = VK_ERROR_OUT_OF_HOST_MEMORY;
561       goto out;
562    }
563 
564    result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, physical_device_groups);
565    if (result != VK_SUCCESS)
566       goto out;
567 
568    /* just sort groups with CPU devices to the end? - assume nobody will mix these */
569    int num_gpu_groups = 0;
570    int num_cpu_groups = 0;
571    selected_physical_device_group_count = physical_device_group_count;
572    for (unsigned i = 0; i < physical_device_group_count; i++) {
573       bool group_has_cpu_device = false;
574       for (unsigned j = 0; j < physical_device_groups[i].physicalDeviceCount; j++) {
575          VkPhysicalDevice physical_device = physical_device_groups[i].physicalDevices[j];
576          VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){
577             .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR
578          };
579          info->GetPhysicalDeviceProperties(physical_device, &properties.properties);
580          group_has_cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
581       }
582 
583       if (group_has_cpu_device) {
584          selected_physical_device_groups[physical_device_group_count - num_cpu_groups - 1] = physical_device_groups[i];
585          num_cpu_groups++;
586       } else {
587          selected_physical_device_groups[num_gpu_groups] = physical_device_groups[i];
588          num_gpu_groups++;
589       }
590    }
591 
592    assert(result == VK_SUCCESS);
593 
594    for (unsigned i = 0; i < selected_physical_device_group_count; i++) {
595       vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, ent) {
596          *ent = selected_physical_device_groups[i];
597       }
598    }
599    result = vk_outarray_status(&out);
600 out:
601    free(physical_device_groups);
602    free(selected_physical_device_groups);
603    return result;
604 }
605 
get_pdevice_proc_addr(VkInstance instance,const char * name)606 static void  (*get_pdevice_proc_addr(VkInstance instance, const char* name))()
607 {
608    struct instance_info *info = device_select_layer_get_instance(instance);
609    return info->GetPhysicalDeviceProcAddr(instance, name);
610 }
611 
get_instance_proc_addr(VkInstance instance,const char * name)612 static void  (*get_instance_proc_addr(VkInstance instance, const char* name))()
613 {
614    if (strcmp(name, "vkGetInstanceProcAddr") == 0)
615       return (void(*)())get_instance_proc_addr;
616    if (strcmp(name, "vkCreateInstance") == 0)
617       return (void(*)())device_select_CreateInstance;
618    if (strcmp(name, "vkDestroyInstance") == 0)
619       return (void(*)())device_select_DestroyInstance;
620    if (strcmp(name, "vkEnumeratePhysicalDevices") == 0)
621       return (void(*)())device_select_EnumeratePhysicalDevices;
622    if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)
623       return (void(*)())device_select_EnumeratePhysicalDeviceGroups;
624 
625    struct instance_info *info = device_select_layer_get_instance(instance);
626    return info->GetInstanceProcAddr(instance, name);
627 }
628 
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface * pVersionStruct)629 VK_LAYER_EXPORT VkResult vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)
630 {
631    if (pVersionStruct->loaderLayerInterfaceVersion < 2)
632       return VK_ERROR_INITIALIZATION_FAILED;
633    pVersionStruct->loaderLayerInterfaceVersion = 2;
634 
635    pVersionStruct->pfnGetInstanceProcAddr = get_instance_proc_addr;
636    pVersionStruct->pfnGetPhysicalDeviceProcAddr = get_pdevice_proc_addr;
637 
638    return VK_SUCCESS;
639 }
640