Skip to content

Commit 04f23b0

Browse files
committed
vkcube: Check if gpu support the surface
Fix bug on checking and refine logic of selection.
1 parent 84b6549 commit 04f23b0

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

cube/cube.c

+27-22
Original file line numberDiff line numberDiff line change
@@ -4184,37 +4184,42 @@ static void demo_select_physical_device(struct demo* demo) {
41844184
} else {
41854185
/* Try to auto select most suitable device */
41864186
if (demo->gpu_number == -1) {
4187-
uint32_t count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU + 1];
4188-
memset(count_device_type, 0, sizeof(count_device_type));
4189-
41904187
VkPhysicalDeviceProperties physicalDeviceProperties;
4188+
int prev_priority = 0;
41914189
for (uint32_t i = 0; i < gpu_count; i++) {
41924190
vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
41934191
assert(physicalDeviceProperties.deviceType <= VK_PHYSICAL_DEVICE_TYPE_CPU);
4192+
4193+
// Continue next gpu if this gpu does not support the surface.
41944194
VkBool32 supported = 0;
41954195
vkGetPhysicalDeviceSurfaceSupportKHR(physical_devices[i], 0, demo->surface, &supported);
4196-
if (supported)
4197-
count_device_type[physicalDeviceProperties.deviceType]++;
4198-
}
4196+
if (!supported) continue;
41994197

4200-
VkPhysicalDeviceType search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
4201-
if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU]) {
4202-
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
4203-
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU]) {
4204-
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
4205-
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU]) {
4206-
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
4207-
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU]) {
4208-
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_CPU;
4209-
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_OTHER]) {
4210-
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_OTHER;
4211-
}
4198+
int priority = 0;
4199+
switch (physicalDeviceProperties.deviceType) {
4200+
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
4201+
priority = 5;
4202+
break;
4203+
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
4204+
priority = 4;
4205+
break;
4206+
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
4207+
priority = 3;
4208+
break;
4209+
case VK_PHYSICAL_DEVICE_TYPE_CPU:
4210+
priority = 2;
4211+
break;
4212+
case VK_PHYSICAL_DEVICE_TYPE_OTHER:
4213+
priority = 1;
4214+
break;
4215+
default:
4216+
priority = -1;
4217+
break;
4218+
}
42124219

4213-
for (uint32_t i = 0; i < gpu_count; i++) {
4214-
vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
4215-
if (physicalDeviceProperties.deviceType == search_for_device_type) {
4220+
if (priority > prev_priority) {
42164221
demo->gpu_number = i;
4217-
break;
4222+
prev_priority = priority;
42184223
}
42194224
}
42204225
}

0 commit comments

Comments
 (0)