cube: Select phy device for DISPLAY
authorTony-LunarG <tony@lunarg.com>
Thu, 11 Mar 2021 23:28:19 +0000 (16:28 -0700)
committerTony Barbour <tony@lunarg.com>
Mon, 15 Mar 2021 23:01:23 +0000 (17:01 -0600)
Change-Id: I3075c06220d9b02a3eb9b2e6846a13b9ceea1abe

cube/cube.c
cube/cube.cpp

index f00eae7..8302230 100644 (file)
@@ -2884,15 +2884,6 @@ static VkResult demo_create_display_surface(struct demo *demo) {
     VkDisplaySurfaceCreateInfoKHR create_info;
 
     // Get the first display
-    err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
-    assert(!err);
-
-    if (display_count == 0) {
-        printf("Cannot find any display!\n");
-        fflush(stdout);
-        exit(1);
-    }
-
     display_count = 1;
     err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
     assert(!err || (err == VK_INCOMPLETE));
@@ -3039,7 +3030,30 @@ static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint
     }
     return 1;
 }
-
+#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
+int find_display_gpu(int gpu_number, uint32_t gpu_count, VkPhysicalDevice *physical_devices) {
+    uint32_t display_count = 0;
+    VkResult result;
+    int gpu_return = gpu_number;
+    if (gpu_number >= 0) {
+        result = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_devices[gpu_number], &display_count, NULL);
+        assert(!result);
+    } else {
+        for (uint32_t i = 0; i < gpu_count; i++) {
+            result = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_devices[i], &display_count, NULL);
+            assert(!result);
+            if (display_count) {
+                gpu_return = i;
+                break;
+            }
+        }
+    }
+    if (display_count > 0)
+        return gpu_return;
+    else
+        return -1;
+}
+#endif
 static void demo_init_vk(struct demo *demo) {
     VkResult err;
     uint32_t instance_extension_count = 0;
@@ -3288,6 +3302,14 @@ static void demo_init_vk(struct demo *demo) {
         ERR_EXIT("Specified GPU number is not present", "User Error");
     }
 
+#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
+    demo->gpu_number = find_display_gpu(demo->gpu_number, gpu_count, physical_devices);
+    if (demo->gpu_number < 0) {
+        printf("Cannot find any display!\n");
+        fflush(stdout);
+        exit(1);
+    }
+#else
     /* Try to auto select most suitable device */
     if (demo->gpu_number == -1) {
         uint32_t count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU + 1];
@@ -3321,6 +3343,7 @@ static void demo_init_vk(struct demo *demo) {
             }
         }
     }
+#endif
     assert(demo->gpu_number >= 0);
     demo->gpu = physical_devices[demo->gpu_number];
     {
index 69da9a9..ce59462 100644 (file)
@@ -1069,7 +1069,30 @@ void Demo::init_connection() {
     wl_display_dispatch(display);
 #endif
 }
-
+#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
+int find_display_gpu(int gpu_number, uint32_t gpu_count, std::unique_ptr<vk::PhysicalDevice[]> &physical_devices) {
+    uint32_t display_count = 0;
+    vk::Result result;
+    int gpu_return = gpu_number;
+    if (gpu_number >= 0) {
+        result = physical_devices[gpu_number].getDisplayPropertiesKHR(&display_count, nullptr);
+        VERIFY(result == vk::Result::eSuccess);
+    } else {
+        for (uint32_t i = 0; i < gpu_count; i++) {
+            result = physical_devices[i].getDisplayPropertiesKHR(&display_count, nullptr);
+            VERIFY(result == vk::Result::eSuccess);
+            if (display_count) {
+                gpu_return = i;
+                break;
+            }
+        }
+    }
+    if (display_count > 0)
+        return gpu_return;
+    else
+        return -1;
+}
+#endif
 void Demo::init_vk() {
     uint32_t instance_extension_count = 0;
     uint32_t instance_layer_count = 0;
@@ -1273,7 +1296,14 @@ void Demo::init_vk() {
         fprintf(stderr, "GPU %d specified is not present, GPU count = %u\n", gpu_number, gpu_count);
         ERR_EXIT("Specified GPU number is not present", "User Error");
     }
-
+#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
+    gpu_number = find_display_gpu(gpu_number, gpu_count, physical_devices);
+    if (gpu_number < 0) {
+        printf("Cannot find any display!\n");
+        fflush(stdout);
+        exit(1);
+    }
+#else
     /* Try to auto select most suitable device */
     if (gpu_number == -1) {
         uint32_t count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU + 1];
@@ -1304,6 +1334,7 @@ void Demo::init_vk() {
             }
         }
     }
+#endif
     assert(gpu_number >= 0);
     gpu = physical_devices[gpu_number];
     {
@@ -2897,16 +2928,6 @@ vk::Result Demo::create_display_surface() {
     uint32_t plane_index;
     vk::Extent2D image_extent;
 
-    // Get the first display
-    result = gpu.getDisplayPropertiesKHR(&display_count, nullptr);
-    VERIFY(result == vk::Result::eSuccess);
-
-    if (display_count == 0) {
-        printf("Cannot find any display!\n");
-        fflush(stdout);
-        exit(1);
-    }
-
     display_count = 1;
     result = gpu.getDisplayPropertiesKHR(&display_count, &display_props);
     VERIFY((result == vk::Result::eSuccess) || (result == vk::Result::eIncomplete));