Skip to content

Commit d221f17

Browse files
committed
vkcubepp: Check if gpu support the surface
1 parent 04f23b0 commit d221f17

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

cube/cube.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <sstream>
3030
#include <iostream>
3131
#include <memory>
32+
#include <map>
3233

3334
#if defined(VK_USE_PLATFORM_XLIB_KHR)
3435
#include "xlib_loader.h"
@@ -317,6 +318,7 @@ struct Demo {
317318
void init(int argc, char **argv);
318319
void check_and_set_wsi_platform();
319320
void init_vk();
321+
void select_physical_device();
320322
void init_vk_swapchain();
321323
void prepare();
322324
void prepare_buffers();
@@ -1700,7 +1702,9 @@ void Demo::init_vk() {
17001702
VERIFY(create_debug_messenger_return.result == vk::Result::eSuccess);
17011703
debug_messenger = create_debug_messenger_return.value;
17021704
}
1705+
}
17031706

1707+
void Demo::select_physical_device() {
17041708
auto physical_device_return = inst.enumeratePhysicalDevices();
17051709
VERIFY(physical_device_return.result == vk::Result::eSuccess);
17061710
auto physical_devices = physical_device_return.value;
@@ -1734,32 +1738,27 @@ void Demo::init_vk() {
17341738
} else {
17351739
/* Try to auto select most suitable device */
17361740
if (gpu_number == -1) {
1737-
constexpr uint32_t device_type_count = static_cast<uint32_t>(vk::PhysicalDeviceType::eCpu) + 1;
1738-
std::array<uint32_t, device_type_count> count_device_type{};
1739-
1741+
int prev_priority = 0;
17401742
for (uint32_t i = 0; i < physical_devices.size(); i++) {
17411743
const auto physicalDeviceProperties = physical_devices[i].getProperties();
17421744
assert(physicalDeviceProperties.deviceType <= vk::PhysicalDeviceType::eCpu);
1743-
count_device_type[static_cast<int>(physicalDeviceProperties.deviceType)]++;
1744-
}
17451745

1746-
std::array<vk::PhysicalDeviceType, device_type_count> const device_type_preference = {
1747-
vk::PhysicalDeviceType::eDiscreteGpu, vk::PhysicalDeviceType::eIntegratedGpu, vk::PhysicalDeviceType::eVirtualGpu,
1748-
vk::PhysicalDeviceType::eCpu, vk::PhysicalDeviceType::eOther};
1749-
1750-
vk::PhysicalDeviceType search_for_device_type = vk::PhysicalDeviceType::eDiscreteGpu;
1751-
for (uint32_t i = 0; i < sizeof(device_type_preference) / sizeof(vk::PhysicalDeviceType); i++) {
1752-
if (count_device_type[static_cast<int>(device_type_preference[i])]) {
1753-
search_for_device_type = device_type_preference[i];
1754-
break;
1746+
std::map<vk::PhysicalDeviceType, int> device_type_priorities = {
1747+
{vk::PhysicalDeviceType::eDiscreteGpu, 5},
1748+
{vk::PhysicalDeviceType::eIntegratedGpu, 4},
1749+
{vk::PhysicalDeviceType::eVirtualGpu, 3},
1750+
{vk::PhysicalDeviceType::eCpu, 2},
1751+
{vk::PhysicalDeviceType::eOther, 1},
1752+
};
1753+
int priority = -1;
1754+
if (device_type_priorities.find(physicalDeviceProperties.deviceType) !=
1755+
device_type_priorities.end()) {
1756+
priority = device_type_priorities[physicalDeviceProperties.deviceType];
17551757
}
1756-
}
17571758

1758-
for (uint32_t i = 0; i < physical_devices.size(); i++) {
1759-
const auto physicalDeviceProperties = physical_devices[i].getProperties();
1760-
if (physicalDeviceProperties.deviceType == search_for_device_type) {
1759+
if (priority > prev_priority) {
17611760
gpu_number = i;
1762-
break;
1761+
prev_priority = priority;
17631762
}
17641763
}
17651764
}
@@ -1877,7 +1876,6 @@ void Demo::create_surface() {
18771876
}
18781877

18791878
void Demo::init_vk_swapchain() {
1880-
create_surface();
18811879
// Iterate over each queue to learn whether it supports presenting:
18821880
std::vector<vk::Bool32> supportsPresent;
18831881
for (uint32_t i = 0; i < static_cast<uint32_t>(queue_props.size()); i++) {
@@ -3744,6 +3742,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
37443742
demo.connection = hInstance;
37453743
demo.name = "Vulkan Cube";
37463744
demo.create_window();
3745+
demo.create_surface();
3746+
demo.select_physical_device();
37473747
demo.init_vk_swapchain();
37483748

37493749
demo.prepare();
@@ -3825,6 +3825,10 @@ int main(int argc, char **argv) {
38253825
#endif
38263826
}
38273827

3828+
demo.create_surface();
3829+
3830+
demo.select_physical_device();
3831+
38283832
demo.init_vk_swapchain();
38293833

38303834
demo.prepare();
@@ -3879,6 +3883,8 @@ int main(int argc, char **argv) {
38793883
// Global function invoked from NS or UI views and controllers to create demo
38803884
static void demo_main(Demo &demo, void *caMetalLayer, int argc, const char *argv[]) {
38813885
demo.init(argc, (char **)argv);
3886+
demo.create_surface();
3887+
demo.select_physical_device();
38823888
demo.caMetalLayer = caMetalLayer;
38833889
demo.init_vk_swapchain();
38843890
demo.prepare();

0 commit comments

Comments
 (0)