From 4598028fde993d7c50effa8d204494bc60780bb1 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Wed, 2 Oct 2024 02:57:35 +0200 Subject: [PATCH] device_select: Allow shortcut names for device types Add a bunch of shortcut names to select physical devices by their device type. In particular this aims to make switching between igpu and dgpu easy as well as testing with lavapipe. v2: - rebase and reformat - use strncasecmp and VkPhysicalDeviceType - only print debug message when enabled Signed-off-by: Benjamin Otte Signed-off-by: Rhys Perry (v2) Reviewed-by: Georg Lehmann Part-of: --- .../device-select-layer/device_select.c | 58 +++++++++++++++++-- .../device-select-layer/device_select.h | 2 +- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/vulkan/device-select-layer/device_select.c b/src/vulkan/device-select-layer/device_select.c index cd568988dab..f52df3eca1b 100644 --- a/src/vulkan/device-select-layer/device_select.c +++ b/src/vulkan/device-select-layer/device_select.c @@ -35,6 +35,7 @@ #include #include +#include "util/macros.h" #include "device_select.h" static bool @@ -52,7 +53,7 @@ fill_drm_device_info(const struct instance_info *info, struct device_pci_info *d properties.pNext = &ext_pci_properties; device_select_get_properties(info, device, &properties); - drm_device->cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU; + drm_device->device_type = properties.properties.deviceType; drm_device->dev_info.vendor_id = properties.properties.vendorID; drm_device->dev_info.device_id = properties.properties.deviceID; if (info->has_vulkan11 && info->has_pci_bus) { @@ -62,7 +63,7 @@ fill_drm_device_info(const struct instance_info *info, struct device_pci_info *d drm_device->bus_info.dev = ext_pci_properties.pciDevice; drm_device->bus_info.func = ext_pci_properties.pciFunction; } - return drm_device->cpu_device; + return drm_device->device_type == VK_PHYSICAL_DEVICE_TYPE_CPU; } static int @@ -83,6 +84,36 @@ device_select_find_explicit_default(struct device_pci_info *pci_infos, uint32_t return default_idx; } +static int +device_select_find_typed_default(struct device_pci_info *pci_infos, uint32_t device_count, + const char *selection) +{ + static struct { + const char *name; + VkPhysicalDeviceType type; + } names[] = { + {"other", VK_PHYSICAL_DEVICE_TYPE_OTHER}, + {"integrated", VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU}, + {"igpu", VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU}, + {"discrete", VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU}, + {"dgpu", VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU}, + {"virtual", VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU}, + {"vgpu", VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU}, + {"cpu", VK_PHYSICAL_DEVICE_TYPE_CPU}, + }; + + for (unsigned i = 0; i < ARRAY_SIZE(names); ++i) { + if (strncasecmp(names[i].name, selection, strlen(names[i].name)) == 0) { + for (unsigned j = 0; j < device_count; ++j) { + if (pci_infos[j].device_type == names[i].type) + return j; + } + } + } + + return -1; +} + static int device_select_find_dri_prime_tag_default(struct device_pci_info *pci_infos, uint32_t device_count, const char *dri_prime) @@ -198,7 +229,7 @@ device_select_find_non_cpu(struct device_pci_info *pci_infos, uint32_t device_co /* pick first GPU device */ for (unsigned i = 0; i < device_count; ++i) { - if (!pci_infos[i].cpu_device) { + if (pci_infos[i].device_type != VK_PHYSICAL_DEVICE_TYPE_CPU) { default_idx = i; break; } @@ -213,7 +244,7 @@ find_non_cpu_skip(struct device_pci_info *pci_infos, uint32_t device_count, int for (unsigned i = 0; i < device_count; ++i) { if (i == skip_idx) continue; - if (pci_infos[i].cpu_device) + if (pci_infos[i].device_type == VK_PHYSICAL_DEVICE_TYPE_CPU) continue; skip_count--; if (skip_count > 0) @@ -237,9 +268,26 @@ get_selected(const struct instance_info *info, uint32_t count, struct device_pci { int default_idx = -1; + if (info->selection) + default_idx = device_select_find_typed_default(pci_infos, count, info->selection); + if (default_idx != -1) { + if (info->debug) + fprintf(stderr, + "device-select: device_select_find_typed_default for MESA_VK_DEVICE_SELECT " + "selected %i\n", + default_idx); + *expose_only_one_dev = ends_with_exclamation_mark(info->selection); + return default_idx; + } + if (info->selection) default_idx = device_select_find_explicit_default(pci_infos, count, info->selection); if (default_idx != -1) { + if (info->debug) + fprintf(stderr, + "device-select: device_select_find_explicit_default for MESA_VK_DEVICE_SELECT " + "selected %i\n", + default_idx); *expose_only_one_dev = ends_with_exclamation_mark(info->selection); return default_idx; } @@ -280,7 +328,7 @@ get_default(const struct instance_info *info, uint32_t count, struct device_pci_ bool has_cpu = false; for (unsigned i = 0; i < count; ++i) - has_cpu |= pci_infos[i].cpu_device; + has_cpu |= pci_infos[i].device_type == VK_PHYSICAL_DEVICE_TYPE_CPU; if (default_idx == -1 && info->has_wayland) { default_idx = device_select_find_wayland_pci_default(pci_infos, count); diff --git a/src/vulkan/device-select-layer/device_select.h b/src/vulkan/device-select-layer/device_select.h index 89a4d0bf921..f4423b495c5 100644 --- a/src/vulkan/device-select-layer/device_select.h +++ b/src/vulkan/device-select-layer/device_select.h @@ -58,7 +58,7 @@ struct device_pci_info { struct device_info dev_info; drmPciBusInfo bus_info; bool has_bus_info; - bool cpu_device; + VkPhysicalDeviceType device_type; }; #ifdef VK_USE_PLATFORM_XCB_KHR