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