Coverage Report

Created: 2026-09-02 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vulkan-loader/loader/loader_linux.c
Line
Count
Source
1
/*
2
 *
3
 * Copyright (c) 2021-2022 The Khronos Group Inc.
4
 * Copyright (c) 2021-2022 Valve Corporation
5
 * Copyright (c) 2021-2022 LunarG, Inc.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * Author: Mark Young <marky@lunarg.com>
20
 *
21
 */
22
23
// Non-windows and non-apple only header file, guard it so that accidental
24
// inclusion doesn't cause unknown header include errors
25
#if defined(LOADER_ENABLE_LINUX_SORT)
26
27
#include <stdio.h>
28
#include <stdlib.h>
29
30
#include "loader_linux.h"
31
32
#include "allocation.h"
33
#include "loader_environment.h"
34
#include "loader.h"
35
#include "log.h"
36
#include "stack_allocation.h"
37
38
// Determine a priority based on device type with the higher value being higher priority.
39
0
uint32_t determine_priority_type_value(VkPhysicalDeviceType type) {
40
0
    switch (type) {
41
0
        case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
42
0
            return 10;
43
0
        case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
44
0
            return 5;
45
0
        case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
46
0
            return 3;
47
0
        case VK_PHYSICAL_DEVICE_TYPE_OTHER:
48
0
            return 2;
49
0
        case VK_PHYSICAL_DEVICE_TYPE_CPU:
50
0
            return 1;
51
0
        case VK_PHYSICAL_DEVICE_TYPE_MAX_ENUM:  // Not really an enum, but throws warning if it's not here
52
0
            break;
53
0
    }
54
0
    return 0;
55
0
}
56
57
// Compare the two device types.
58
// This behaves similar to a qsort compare.
59
0
int32_t device_type_compare(VkPhysicalDeviceType a, VkPhysicalDeviceType b) {
60
0
    uint32_t a_value = determine_priority_type_value(a);
61
0
    uint32_t b_value = determine_priority_type_value(b);
62
0
    if (a_value > b_value) {
63
0
        return -1;
64
0
    }
65
0
    if (b_value > a_value) {
66
0
        return 1;
67
0
    }
68
0
    return 0;
69
0
}
70
71
// Used to compare two devices and determine which one should have priority.  The criteria is
72
// simple:
73
//   1) Default device ALWAYS wins
74
//   2) Sort by type
75
//   3) Sort by PCI bus ID
76
//   4) Ties broken by device_ID XOR vendor_ID comparison
77
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) - qsort comparator signature, order is fixed by the C standard library API
78
0
int32_t compare_devices(const void *a, const void *b) {
79
0
    struct LinuxSortedDeviceInfo *left = (struct LinuxSortedDeviceInfo *)a;
80
0
    struct LinuxSortedDeviceInfo *right = (struct LinuxSortedDeviceInfo *)b;
81
82
    // Default device always gets priority
83
0
    if (left->default_device) {
84
0
        return -1;
85
0
    }
86
0
    if (right->default_device) {
87
0
        return 1;
88
0
    }
89
90
    // Order by device type next
91
0
    int32_t dev_type_comp = device_type_compare(left->device_type, right->device_type);
92
0
    if (0 != dev_type_comp) {
93
0
        return dev_type_comp;
94
0
    }
95
96
    // Sort by PCI info (prioritize devices that have info over those that don't)
97
0
    if (left->has_pci_bus_info && !right->has_pci_bus_info) {
98
0
        return -1;
99
0
    }
100
0
    if (!left->has_pci_bus_info && right->has_pci_bus_info) {
101
0
        return 1;
102
0
    }
103
0
    if (left->has_pci_bus_info && right->has_pci_bus_info) {
104
        // Sort low to high PCI domain
105
0
        if (left->pci_domain < right->pci_domain) {
106
0
            return -1;
107
0
        }
108
0
        if (left->pci_domain > right->pci_domain) {
109
0
            return 1;
110
0
        }
111
        // Sort low to high PCI bus
112
0
        if (left->pci_bus < right->pci_bus) {
113
0
            return -1;
114
0
        }
115
0
        if (left->pci_bus > right->pci_bus) {
116
0
            return 1;
117
0
        }
118
        // Sort low to high PCI device
119
0
        if (left->pci_device < right->pci_device) {
120
0
            return -1;
121
0
        }
122
0
        if (left->pci_device > right->pci_device) {
123
0
            return 1;
124
0
        }
125
        // Sort low to high PCI function
126
0
        if (left->pci_function < right->pci_function) {
127
0
            return -1;
128
0
        }
129
0
        if (left->pci_function > right->pci_function) {
130
0
            return 1;
131
0
        }
132
0
    }
133
134
    // Somehow we have a tie above, so XOR vendorID and deviceID and compare
135
0
    uint32_t left_xord_dev_vend = left->device_id ^ left->vendor_id;
136
0
    uint32_t right_xord_dev_vend = right->device_id ^ right->vendor_id;
137
0
    if (left_xord_dev_vend < right_xord_dev_vend) {
138
0
        return -1;
139
0
    }
140
0
    if (right_xord_dev_vend < left_xord_dev_vend) {
141
0
        return 1;
142
0
    }
143
0
    return 0;
144
0
}
145
146
// Used to compare two device groups and determine which one should have priority.
147
// NOTE: This assumes that devices in each group have already been sorted.
148
// The group sort criteria is simple:
149
//   1) Group with the default device ALWAYS wins
150
//   2) Group with the best device type for device 0 wins
151
//   3) Group with best PCI bus ID for device 0 wins
152
//   4) Ties broken by group device 0 device_ID XOR vendor_ID comparison
153
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) - qsort comparator signature, order is fixed by the C standard library API
154
0
int32_t compare_device_groups(const void *a, const void *b) {
155
0
    struct loader_physical_device_group_term *grp_a = (struct loader_physical_device_group_term *)a;
156
0
    struct loader_physical_device_group_term *grp_b = (struct loader_physical_device_group_term *)b;
157
158
    // Use the first GPU's info from each group to sort the groups by
159
0
    struct LinuxSortedDeviceInfo *left = &grp_a->internal_device_info[0];
160
0
    struct LinuxSortedDeviceInfo *right = &grp_b->internal_device_info[0];
161
162
    // Default device always gets priority
163
0
    if (left->default_device) {
164
0
        return -1;
165
0
    }
166
0
    if (right->default_device) {
167
0
        return 1;
168
0
    }
169
170
    // Order by device type next
171
0
    int32_t dev_type_comp = device_type_compare(left->device_type, right->device_type);
172
0
    if (0 != dev_type_comp) {
173
0
        return dev_type_comp;
174
0
    }
175
176
    // Sort by PCI info (prioritize devices that have info over those that don't)
177
0
    if (left->has_pci_bus_info && !right->has_pci_bus_info) {
178
0
        return -1;
179
0
    }
180
0
    if (!left->has_pci_bus_info && right->has_pci_bus_info) {
181
0
        return 1;
182
0
    }
183
0
    if (left->has_pci_bus_info && right->has_pci_bus_info) {
184
        // Sort low to high PCI domain
185
0
        if (left->pci_domain < right->pci_domain) {
186
0
            return -1;
187
0
        }
188
0
        if (left->pci_domain > right->pci_domain) {
189
0
            return 1;
190
0
        }
191
        // Sort low to high PCI bus
192
0
        if (left->pci_bus < right->pci_bus) {
193
0
            return -1;
194
0
        }
195
0
        if (left->pci_bus > right->pci_bus) {
196
0
            return 1;
197
0
        }
198
        // Sort low to high PCI device
199
0
        if (left->pci_device < right->pci_device) {
200
0
            return -1;
201
0
        }
202
0
        if (left->pci_device > right->pci_device) {
203
0
            return 1;
204
0
        }
205
        // Sort low to high PCI function
206
0
        if (left->pci_function < right->pci_function) {
207
0
            return -1;
208
0
        }
209
0
        if (left->pci_function > right->pci_function) {
210
0
            return 1;
211
0
        }
212
0
    }
213
214
    // Somehow we have a tie above, so XOR vendorID and deviceID and compare
215
0
    uint32_t left_xord_dev_vend = left->device_id ^ left->vendor_id;
216
0
    uint32_t right_xord_dev_vend = right->device_id ^ right->vendor_id;
217
0
    if (left_xord_dev_vend < right_xord_dev_vend) {
218
0
        return -1;
219
0
    }
220
0
    if (right_xord_dev_vend < left_xord_dev_vend) {
221
0
        return 1;
222
0
    }
223
0
    return 0;
224
0
}
225
226
// Search for the default device using the loader environment variable.
227
void linux_env_var_default_device(struct loader_instance *inst, uint32_t device_count,
228
0
                                  struct LinuxSortedDeviceInfo *sorted_device_info) {
229
0
    char *selection = loader_getenv("VK_LOADER_DEVICE_SELECT", inst);
230
0
    if (NULL != selection) {
231
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT | VULKAN_LOADER_DRIVER_BIT, 0,
232
0
                   "linux_env_var_default_device:  Found \'VK_LOADER_DEVICE_SELECT\' set to %s", selection);
233
234
        // The environment variable exists, so grab the vendor ID and device ID of the
235
        // selected default device
236
0
        unsigned vendor_id;
237
0
        unsigned device_id;
238
        // NOLINTNEXTLINE(cert-err34-c, bugprone-unchecked-string-to-number-conversion) - checked below via 'matched == 2'
239
0
        int32_t matched = sscanf(selection, "%x:%x", &vendor_id, &device_id);
240
0
        if (matched == 2) {
241
0
            for (int32_t i = 0; i < (int32_t)device_count; ++i) {
242
0
                if (sorted_device_info[i].vendor_id == vendor_id && sorted_device_info[i].device_id == device_id) {
243
0
                    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0,
244
0
                               "linux_env_var_default_device:  Found default at index %u \'%s\'", i,
245
0
                               sorted_device_info[i].device_name);
246
0
                    sorted_device_info[i].default_device = true;
247
0
                    break;
248
0
                }
249
0
            }
250
0
        }
251
252
0
        loader_free_getenv(selection, inst);
253
0
    }
254
0
}
255
256
// This function allocates an array in sorted_devices which must be freed by the caller if not null
257
VkResult linux_read_sorted_physical_devices(struct loader_instance *inst, uint32_t icd_count,
258
                                            struct loader_icd_physical_devices *icd_devices, uint32_t phys_dev_count,
259
0
                                            struct loader_physical_device_term **sorted_device_term) {
260
0
    VkResult res = VK_SUCCESS;
261
0
    bool app_is_vulkan_1_1 = loader_check_version_meets_required(LOADER_VERSION_1_1_0, inst->app_api_version);
262
263
0
    struct LinuxSortedDeviceInfo *sorted_device_info = loader_instance_heap_calloc(
264
0
        inst, phys_dev_count * sizeof(struct LinuxSortedDeviceInfo), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
265
0
    if (NULL == sorted_device_info) {
266
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
267
0
        goto out;
268
0
    }
269
270
0
    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "linux_read_sorted_physical_devices:");
271
0
    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "     Original order:");
272
273
    // Grab all the necessary info we can about each device
274
0
    uint32_t index = 0;
275
0
    for (uint32_t icd_idx = 0; icd_idx < icd_count; ++icd_idx) {
276
0
        for (uint32_t phys_dev = 0; phys_dev < icd_devices[icd_idx].device_count; ++phys_dev) {
277
0
            struct loader_icd_term *icd_term = icd_devices[icd_idx].icd_term;
278
0
            VkPhysicalDeviceProperties dev_props = {};
279
280
0
            sorted_device_info[index].physical_device = icd_devices[icd_idx].physical_devices[phys_dev];
281
0
            sorted_device_info[index].icd_term = icd_term;
282
0
            sorted_device_info[index].has_pci_bus_info = false;
283
284
0
            icd_term->dispatch.GetPhysicalDeviceProperties(sorted_device_info[index].physical_device, &dev_props);
285
0
            sorted_device_info[index].device_type = dev_props.deviceType;
286
0
            strncpy(sorted_device_info[index].device_name, dev_props.deviceName, VK_MAX_PHYSICAL_DEVICE_NAME_SIZE - 1);
287
0
            sorted_device_info[index].device_name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE - 1] = '\0';
288
0
            sorted_device_info[index].vendor_id = dev_props.vendorID;
289
0
            sorted_device_info[index].device_id = dev_props.deviceID;
290
291
0
            bool device_is_1_1_capable =
292
0
                loader_check_version_meets_required(LOADER_VERSION_1_1_0, loader_make_version(dev_props.apiVersion));
293
0
            if (!sorted_device_info[index].has_pci_bus_info) {
294
0
                uint32_t ext_count = 0;
295
0
                icd_term->dispatch.EnumerateDeviceExtensionProperties(sorted_device_info[index].physical_device, NULL, &ext_count,
296
0
                                                                      NULL);
297
0
                if (ext_count > 0) {
298
0
                    VkExtensionProperties *ext_props =
299
0
                        (VkExtensionProperties *)loader_stack_alloc(sizeof(VkExtensionProperties) * ext_count);
300
0
                    if (NULL == ext_props) {
301
0
                        res = VK_ERROR_OUT_OF_HOST_MEMORY;
302
0
                        goto out;
303
0
                    }
304
0
                    uint32_t allocated_count = ext_count;
305
0
                    icd_term->dispatch.EnumerateDeviceExtensionProperties(sorted_device_info[index].physical_device, NULL,
306
0
                                                                          &ext_count, ext_props);
307
                    // The count returned by the second call sizes the array, but never read past what we actually allocated.
308
0
                    for (uint32_t ext = 0; ext < ext_count && ext < allocated_count; ++ext) {
309
0
                        if (!strcmp(ext_props[ext].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME)) {
310
0
                            sorted_device_info[index].has_pci_bus_info = true;
311
0
                            break;
312
0
                        }
313
0
                    }
314
0
                }
315
0
            }
316
317
0
            if (sorted_device_info[index].has_pci_bus_info) {
318
0
                VkPhysicalDevicePCIBusInfoPropertiesEXT pci_props = (VkPhysicalDevicePCIBusInfoPropertiesEXT){
319
0
                    .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT};
320
0
                VkPhysicalDeviceProperties2 dev_props2 =
321
0
                    (VkPhysicalDeviceProperties2){.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &pci_props};
322
323
0
                PFN_vkGetPhysicalDeviceProperties2 GetPhysDevProps2 = NULL;
324
0
                if (app_is_vulkan_1_1 && device_is_1_1_capable) {
325
0
                    GetPhysDevProps2 = icd_term->dispatch.GetPhysicalDeviceProperties2;
326
0
                } else {
327
0
                    GetPhysDevProps2 = (PFN_vkGetPhysicalDeviceProperties2)icd_term->dispatch.GetPhysicalDeviceProperties2KHR;
328
0
                }
329
0
                if (NULL != GetPhysDevProps2) {
330
0
                    GetPhysDevProps2(sorted_device_info[index].physical_device, &dev_props2);
331
0
                    sorted_device_info[index].pci_domain = pci_props.pciDomain;
332
0
                    sorted_device_info[index].pci_bus = pci_props.pciBus;
333
0
                    sorted_device_info[index].pci_device = pci_props.pciDevice;
334
0
                    sorted_device_info[index].pci_function = pci_props.pciFunction;
335
0
                } else {
336
0
                    sorted_device_info[index].has_pci_bus_info = false;
337
0
                }
338
0
            }
339
0
            loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           [%u] %s", index,
340
0
                       sorted_device_info[index].device_name);
341
0
            index++;
342
0
        }
343
0
    }
344
345
    // Select default device if set in the environment variable
346
0
    linux_env_var_default_device(inst, phys_dev_count, sorted_device_info);
347
348
    // Sort devices by PCI info
349
0
    qsort(sorted_device_info, phys_dev_count, sizeof(struct LinuxSortedDeviceInfo), compare_devices);
350
351
    // If we have a selected index, add that first.
352
0
    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "     Sorted order:");
353
354
    // Add all others after (they've already been sorted)
355
0
    for (uint32_t dev = 0; dev < phys_dev_count; ++dev) {
356
0
        sorted_device_term[dev]->this_icd_term = sorted_device_info[dev].icd_term;
357
0
        sorted_device_term[dev]->phys_dev = sorted_device_info[dev].physical_device;
358
0
        loader_set_dispatch((void *)sorted_device_term[dev], inst->disp);
359
0
        loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           [%u] %s  %s", dev,
360
0
                   sorted_device_info[dev].device_name, (sorted_device_info[dev].default_device ? "[default]" : ""));
361
0
    }
362
363
0
out:
364
0
    loader_instance_heap_free(inst, sorted_device_info);
365
366
0
    return res;
367
0
}
368
369
// This function sorts an array of physical device groups
370
VkResult linux_sort_physical_device_groups(struct loader_instance *inst, uint32_t group_count,
371
0
                                           struct loader_physical_device_group_term *sorted_group_term) {
372
0
    VkResult res = VK_SUCCESS;
373
0
    bool app_is_vulkan_1_1 = loader_check_version_meets_required(LOADER_VERSION_1_1_0, inst->app_api_version);
374
375
0
    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "linux_sort_physical_device_groups:  Original order:");
376
377
0
    for (uint32_t group = 0; group < group_count; ++group) {
378
0
        loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           Group %u", group);
379
380
0
        struct loader_icd_term *icd_term = sorted_group_term[group].this_icd_term;
381
0
        for (uint32_t gpu = 0; gpu < sorted_group_term[group].group_props.physicalDeviceCount; ++gpu) {
382
0
            VkPhysicalDeviceProperties dev_props = {};
383
384
0
            sorted_group_term[group].internal_device_info[gpu].physical_device =
385
0
                sorted_group_term[group].group_props.physicalDevices[gpu];
386
0
            sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info = false;
387
388
0
            icd_term->dispatch.GetPhysicalDeviceProperties(sorted_group_term[group].internal_device_info[gpu].physical_device,
389
0
                                                           &dev_props);
390
0
            sorted_group_term[group].internal_device_info[gpu].device_type = dev_props.deviceType;
391
0
            strncpy(sorted_group_term[group].internal_device_info[gpu].device_name, dev_props.deviceName,
392
0
                    VK_MAX_PHYSICAL_DEVICE_NAME_SIZE - 1);
393
0
            sorted_group_term[group].internal_device_info[gpu].device_name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE - 1] = '\0';
394
0
            sorted_group_term[group].internal_device_info[gpu].vendor_id = dev_props.vendorID;
395
0
            sorted_group_term[group].internal_device_info[gpu].device_id = dev_props.deviceID;
396
397
0
            bool device_is_1_1_capable =
398
0
                loader_check_version_meets_required(LOADER_VERSION_1_1_0, loader_make_version(dev_props.apiVersion));
399
0
            if (!sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info) {
400
0
                uint32_t ext_count;
401
0
                icd_term->dispatch.EnumerateDeviceExtensionProperties(
402
0
                    sorted_group_term[group].internal_device_info[gpu].physical_device, NULL, &ext_count, NULL);
403
0
                if (ext_count > 0) {
404
0
                    VkExtensionProperties *ext_props =
405
0
                        (VkExtensionProperties *)loader_stack_alloc(sizeof(VkExtensionProperties) * ext_count);
406
0
                    if (NULL == ext_props) {
407
0
                        return VK_ERROR_OUT_OF_HOST_MEMORY;
408
0
                    }
409
0
                    uint32_t allocated_count = ext_count;
410
0
                    icd_term->dispatch.EnumerateDeviceExtensionProperties(
411
0
                        sorted_group_term[group].internal_device_info[gpu].physical_device, NULL, &ext_count, ext_props);
412
                    // The count returned by the second call sizes the array, but never read past what we actually allocated.
413
0
                    for (uint32_t ext = 0; ext < ext_count && ext < allocated_count; ++ext) {
414
0
                        if (!strcmp(ext_props[ext].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME)) {
415
0
                            sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info = true;
416
0
                            break;
417
0
                        }
418
0
                    }
419
0
                }
420
0
            }
421
422
0
            if (sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info) {
423
0
                VkPhysicalDevicePCIBusInfoPropertiesEXT pci_props = (VkPhysicalDevicePCIBusInfoPropertiesEXT){
424
0
                    .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT};
425
0
                VkPhysicalDeviceProperties2 dev_props2 =
426
0
                    (VkPhysicalDeviceProperties2){.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &pci_props};
427
428
0
                PFN_vkGetPhysicalDeviceProperties2 GetPhysDevProps2 = NULL;
429
0
                if (app_is_vulkan_1_1 && device_is_1_1_capable) {
430
0
                    GetPhysDevProps2 = icd_term->dispatch.GetPhysicalDeviceProperties2;
431
0
                } else {
432
0
                    GetPhysDevProps2 = (PFN_vkGetPhysicalDeviceProperties2)icd_term->dispatch.GetPhysicalDeviceProperties2KHR;
433
0
                }
434
0
                if (NULL != GetPhysDevProps2) {
435
0
                    GetPhysDevProps2(sorted_group_term[group].internal_device_info[gpu].physical_device, &dev_props2);
436
0
                    sorted_group_term[group].internal_device_info[gpu].pci_domain = pci_props.pciDomain;
437
0
                    sorted_group_term[group].internal_device_info[gpu].pci_bus = pci_props.pciBus;
438
0
                    sorted_group_term[group].internal_device_info[gpu].pci_device = pci_props.pciDevice;
439
0
                    sorted_group_term[group].internal_device_info[gpu].pci_function = pci_props.pciFunction;
440
0
                } else {
441
0
                    sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info = false;
442
0
                }
443
0
            }
444
0
            loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "               [%u] %s", gpu,
445
0
                       sorted_group_term[group].internal_device_info[gpu].device_name);
446
0
        }
447
448
        // Select default device if set in the environment variable
449
0
        linux_env_var_default_device(inst, sorted_group_term[group].group_props.physicalDeviceCount,
450
0
                                     sorted_group_term[group].internal_device_info);
451
452
        // Sort GPUs in each group
453
0
        qsort(sorted_group_term[group].internal_device_info, sorted_group_term[group].group_props.physicalDeviceCount,
454
0
              sizeof(struct LinuxSortedDeviceInfo), compare_devices);
455
456
        // Match the externally used physical device list with the sorted physical device list for this group.
457
0
        for (uint32_t dev = 0; dev < sorted_group_term[group].group_props.physicalDeviceCount; ++dev) {
458
0
            sorted_group_term[group].group_props.physicalDevices[dev] =
459
0
                sorted_group_term[group].internal_device_info[dev].physical_device;
460
0
        }
461
0
    }
462
463
    // Sort device groups by PCI info
464
0
    qsort(sorted_group_term, group_count, sizeof(struct loader_physical_device_group_term), compare_device_groups);
465
466
0
    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "linux_sort_physical_device_groups:  Sorted order:");
467
0
    for (uint32_t group = 0; group < group_count; ++group) {
468
0
        loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           Group %u", group);
469
0
        for (uint32_t gpu = 0; gpu < sorted_group_term[group].group_props.physicalDeviceCount; ++gpu) {
470
0
            loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "               [%u] %s %p %s", gpu,
471
0
                       sorted_group_term[group].internal_device_info[gpu].device_name,
472
0
                       sorted_group_term[group].internal_device_info[gpu].physical_device,
473
0
                       (sorted_group_term[group].internal_device_info[gpu].default_device ? "[default]" : ""));
474
0
        }
475
0
    }
476
477
0
    return res;
478
0
}
479
480
#endif  // LOADER_ENABLE_LINUX_SORT