device-select: do all getenv during instance creation

Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Georg Lehmann <dadschoorse@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36717>
This commit is contained in:
Rhys Perry 2025-08-08 16:38:02 +01:00 committed by Marge Bot
parent e15e7738ac
commit 1ff868adcf
3 changed files with 44 additions and 44 deletions

View file

@ -232,18 +232,15 @@ ends_with_exclamation_mark(const char *str)
}
uint32_t
device_select_get_default(const struct instance_info *info, const char *selection,
uint32_t physical_device_count, VkPhysicalDevice *pPhysicalDevices,
bool *expose_only_one_dev)
device_select_get_default(const struct instance_info *info, uint32_t physical_device_count,
VkPhysicalDevice *pPhysicalDevices, bool *expose_only_one_dev)
{
int default_idx = -1;
const char *dri_prime = getenv("DRI_PRIME");
bool debug = device_select_should_debug();
int dri_prime_as_int = -1;
int cpu_count = 0;
if (dri_prime) {
if (strchr(dri_prime, ':') == NULL)
dri_prime_as_int = atoi(dri_prime);
if (info->dri_prime) {
if (strchr(info->dri_prime, ':') == NULL)
dri_prime_as_int = atoi(info->dri_prime);
if (dri_prime_as_int < 0)
dri_prime_as_int = 0;
@ -258,22 +255,22 @@ device_select_get_default(const struct instance_info *info, const char *selectio
cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0;
}
if (selection)
if (info->selection)
default_idx =
device_select_find_explicit_default(pci_infos, physical_device_count, selection);
device_select_find_explicit_default(pci_infos, physical_device_count, info->selection);
if (default_idx != -1) {
*expose_only_one_dev = ends_with_exclamation_mark(selection);
*expose_only_one_dev = ends_with_exclamation_mark(info->selection);
}
if (default_idx == -1 && dri_prime && dri_prime_as_int == 0) {
if (default_idx == -1 && info->dri_prime && dri_prime_as_int == 0) {
/* Try DRI_PRIME=vendor_id:device_id */
default_idx =
device_select_find_explicit_default(pci_infos, physical_device_count, dri_prime);
device_select_find_explicit_default(pci_infos, physical_device_count, info->dri_prime);
if (default_idx != -1) {
if (debug)
if (info->debug)
fprintf(stderr, "device-select: device_select_find_explicit_default selected %i\n",
default_idx);
*expose_only_one_dev = ends_with_exclamation_mark(dri_prime);
*expose_only_one_dev = ends_with_exclamation_mark(info->dri_prime);
}
if (default_idx == -1) {
@ -282,26 +279,26 @@ device_select_get_default(const struct instance_info *info, const char *selectio
fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n");
else
default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count,
dri_prime);
info->dri_prime);
if (default_idx != -1) {
if (debug)
if (info->debug)
fprintf(stderr,
"device-select: device_select_find_dri_prime_tag_default selected %i\n",
default_idx);
*expose_only_one_dev = ends_with_exclamation_mark(dri_prime);
*expose_only_one_dev = ends_with_exclamation_mark(info->dri_prime);
}
}
}
if (default_idx == -1 && info->has_wayland) {
default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count);
if (debug && default_idx != -1)
if (info->debug && default_idx != -1)
fprintf(stderr, "device-select: device_select_find_wayland_pci_default selected %i\n",
default_idx);
}
if (default_idx == -1 && info->has_xcb) {
default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count);
if (debug && default_idx != -1)
if (info->debug && default_idx != -1)
fprintf(stderr, "device-select: device_select_find_xcb_pci_default selected %i\n",
default_idx);
}
@ -310,12 +307,12 @@ device_select_get_default(const struct instance_info *info, const char *selectio
default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count);
else
default_idx = device_select_find_boot_vga_vid_did(pci_infos, physical_device_count);
if (debug && default_idx != -1)
if (info->debug && default_idx != -1)
fprintf(stderr, "device-select: device_select_find_boot_vga selected %i\n", default_idx);
}
if (default_idx == -1 && cpu_count) {
default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
if (debug && default_idx != -1)
if (info->debug && default_idx != -1)
fprintf(stderr, "device-select: device_select_find_non_cpu selected %i\n", default_idx);
}
/* If no GPU has been selected so far, select the first non-CPU device. If none are available,
@ -324,14 +321,14 @@ device_select_get_default(const struct instance_info *info, const char *selectio
if (default_idx == -1) {
default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
if (default_idx != -1) {
if (debug)
if (info->debug)
fprintf(stderr, "device-select: device_select_find_non_cpu selected %i\n", default_idx);
} else if (cpu_count) {
default_idx = 0;
}
}
/* DRI_PRIME=n handling - pick any other device than default. */
if (dri_prime_as_int > 0 && debug)
if (dri_prime_as_int > 0 && info->debug)
fprintf(stderr, "device-select: DRI_PRIME=%d, default_idx so far: %i\n", dri_prime_as_int,
default_idx);
if (dri_prime_as_int > 0 && physical_device_count > (cpu_count + 1)) {
@ -339,9 +336,9 @@ device_select_get_default(const struct instance_info *info, const char *selectio
default_idx =
find_non_cpu_skip(pci_infos, physical_device_count, default_idx, dri_prime_as_int);
if (default_idx != -1) {
if (debug)
if (info->debug)
fprintf(stderr, "device-select: find_non_cpu_skip selected %i\n", default_idx);
*expose_only_one_dev = ends_with_exclamation_mark(dri_prime);
*expose_only_one_dev = ends_with_exclamation_mark(info->dri_prime);
}
}
}

View file

@ -40,6 +40,11 @@ struct instance_info {
bool has_pci_bus, has_vulkan11;
bool has_wayland, has_xcb;
bool zink, xwayland, xserver;
bool debug;
char *selection;
char *dri_prime;
bool force_default_device;
};
/* We don't use `drmPciDeviceInfo` because it uses 16-bit ids,
@ -76,13 +81,10 @@ device_select_find_wayland_pci_default(struct device_pci_info *devices, uint32_t
}
#endif
bool device_select_should_debug(void);
void device_select_get_properties(const struct instance_info *info, VkPhysicalDevice device,
VkPhysicalDeviceProperties2 *properties);
uint32_t device_select_get_default(const struct instance_info *info, const char *selection,
uint32_t physical_device_count,
uint32_t device_select_get_default(const struct instance_info *info, uint32_t physical_device_count,
VkPhysicalDevice *pPhysicalDevices, bool *expose_only_one_dev);
#endif

View file

@ -151,6 +151,15 @@ device_select_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
info->has_vulkan11 = pCreateInfo->pApplicationInfo &&
pCreateInfo->pApplicationInfo->apiVersion >= VK_MAKE_VERSION(1, 1, 0);
info->debug = debug_get_bool_option("MESA_VK_DEVICE_SELECT_DEBUG", false) ||
debug_get_bool_option("DRI_PRIME_DEBUG", false);
info->selection = getenv("MESA_VK_DEVICE_SELECT");
info->dri_prime = getenv("DRI_PRIME");
const char *force_default_device = getenv("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE");
info->force_default_device = force_default_device && !strcmp(force_default_device, "1");
info->selection = info->selection ? strdup(info->selection) : NULL;
info->dri_prime = info->dri_prime ? strdup(info->dri_prime) : NULL;
#define DEVSEL_GET_CB(func) \
info->func = (PFN_vk##func)info->GetInstanceProcAddr(*pInstance, "vk" #func)
DEVSEL_GET_CB(DestroyInstance);
@ -174,16 +183,11 @@ device_select_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *
device_select_layer_remove_instance(instance);
info->DestroyInstance(instance, pAllocator);
free(info->dri_prime);
free(info->selection);
free(info);
}
bool
device_select_should_debug(void)
{
return debug_get_bool_option("MESA_VK_DEVICE_SELECT_DEBUG", false) ||
debug_get_bool_option("DRI_PRIME_DEBUG", false);
}
void
device_select_get_properties(const struct instance_info *info, VkPhysicalDevice device,
VkPhysicalDeviceProperties2 *properties)
@ -243,7 +247,6 @@ device_select_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalD
struct instance_info *info = device_select_layer_get_instance(instance);
uint32_t physical_device_count = 0;
uint32_t selected_physical_device_count = 0;
const char *selection = getenv("MESA_VK_DEVICE_SELECT");
bool expose_only_one_dev = false;
if (info->zink && info->xwayland)
return info->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
@ -281,16 +284,16 @@ device_select_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalD
free(extensions);
}
}
if (device_select_should_debug() || (selection && strcmp(selection, "list") == 0)) {
if (info->debug || (info->selection && strcmp(info->selection, "list") == 0)) {
fprintf(stderr, "selectable devices:\n");
for (unsigned i = 0; i < physical_device_count; ++i)
print_gpu(info, i, physical_devices[i]);
if (selection && strcmp(selection, "list") == 0)
if (info->selection && strcmp(info->selection, "list") == 0)
exit(0);
}
unsigned selected_index = device_select_get_default(info, selection, physical_device_count,
unsigned selected_index = device_select_get_default(info, physical_device_count,
physical_devices, &expose_only_one_dev);
selected_physical_device_count = physical_device_count;
selected_physical_devices[0] = physical_devices[selected_index];
@ -306,9 +309,7 @@ device_select_EnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalD
assert(result == VK_SUCCESS);
/* do not give multiple device option to app if force default device */
const char *force_default_device = getenv("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE");
if (force_default_device && !strcmp(force_default_device, "1") &&
selected_physical_device_count != 0)
if (info->force_default_device && selected_physical_device_count != 0)
expose_only_one_dev = true;
if (expose_only_one_dev)