Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vkcube and vkcubepp: Check if GPU support the surface #1047

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 58 additions & 23 deletions cube/cube.c
Original file line number Diff line number Diff line change
Expand Up @@ -4143,7 +4143,10 @@ static void demo_init_vk(struct demo *demo) {
}

volkLoadInstance(demo->inst);
}

static void demo_select_physical_device(struct demo *demo) {
VkResult err;
/* Make initial call to query gpu_count, then second call for gpu info */
uint32_t gpu_count = 0;
err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Expand Down Expand Up @@ -4181,34 +4184,42 @@ static void demo_init_vk(struct demo *demo) {
} else {
/* Try to auto select most suitable device */
if (demo->gpu_number == -1) {
uint32_t count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU + 1];
memset(count_device_type, 0, sizeof(count_device_type));

VkPhysicalDeviceProperties physicalDeviceProperties;
int prev_priority = 0;
for (uint32_t i = 0; i < gpu_count; i++) {
vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
assert(physicalDeviceProperties.deviceType <= VK_PHYSICAL_DEVICE_TYPE_CPU);
count_device_type[physicalDeviceProperties.deviceType]++;
}

VkPhysicalDeviceType search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU]) {
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU]) {
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU]) {
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU]) {
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_CPU;
} else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_OTHER]) {
search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_OTHER;
}
// Continue next gpu if this gpu does not support the surface.
VkBool32 supported = VK_FALSE;
VkResult result = vkGetPhysicalDeviceSurfaceSupportKHR(physical_devices[i], 0, demo->surface, &supported);
if (result != VK_SUCCESS || !supported) continue;

for (uint32_t i = 0; i < gpu_count; i++) {
vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
if (physicalDeviceProperties.deviceType == search_for_device_type) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use priority to select physical device so that only one loop is needed. It will be more convenient to filter GPU if only one loop existed.

int priority = 0;
switch (physicalDeviceProperties.deviceType) {
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
priority = 5;
break;
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
priority = 4;
break;
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
priority = 3;
break;
case VK_PHYSICAL_DEVICE_TYPE_CPU:
priority = 2;
break;
case VK_PHYSICAL_DEVICE_TYPE_OTHER:
priority = 1;
break;
default:
priority = -1;
break;
}

if (priority > prev_priority) {
demo->gpu_number = i;
break;
prev_priority = priority;
}
}
}
Expand Down Expand Up @@ -4298,6 +4309,24 @@ static void demo_init_vk(struct demo *demo) {
}

if (demo->validate) {
/*
* This is info for a temp callback to use during CreateInstance.
* After the instance is created, we use the instance-based
* function to register the final callback.
*/
VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
// VK_EXT_debug_utils style
dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
dbg_messenger_create_info.pNext = NULL;
dbg_messenger_create_info.flags = 0;
dbg_messenger_create_info.messageSeverity =
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
dbg_messenger_create_info.pUserData = demo;

err = vkCreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
switch (err) {
case VK_SUCCESS:
Expand Down Expand Up @@ -4492,8 +4521,6 @@ static VkSurfaceFormatKHR pick_surface_format(const VkSurfaceFormatKHR *surfaceF
static void demo_init_vk_swapchain(struct demo *demo) {
VkResult U_ASSERT_ONLY err;

demo_create_surface(demo);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move demo_create_surface to time between demo_init and demo_select_physical_device.


// Iterate over each queue to learn whether it supports presenting:
VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Expand Down Expand Up @@ -4868,6 +4895,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
demo.connection = hInstance;
strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
demo_create_window(&demo);
demo_create_surface(&demo);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let demo_create_surface after demo_create_window

demo_select_physical_device(&demo);
demo_init_vk_swapchain(&demo);

demo_prepare(&demo);
Expand Down Expand Up @@ -4906,6 +4935,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
#if defined(VK_USE_PLATFORM_METAL_EXT)
static void demo_main(struct demo *demo, void *caMetalLayer, int argc, const char *argv[]) {
demo_init(demo, argc, (char **)argv);
demo_create_surface(demo);
demo_select_physical_device(demo);
demo->caMetalLayer = caMetalLayer;
demo_init_vk_swapchain(demo);
demo_prepare(demo);
Expand Down Expand Up @@ -4959,6 +4990,8 @@ static void processCommand(struct android_app *app, int32_t cmd) {
for (int i = 0; i < argc; i++) free(argv[i]);

demo.window = (void *)app->window;
demo_create_surface(&demo);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let demo_create_surface after demo.window is inited.

demo_select_physical_device(&demo);
demo_init_vk_swapchain(&demo);
demo_prepare(&demo);
initialized = true;
Expand Down Expand Up @@ -5044,6 +5077,8 @@ int main(int argc, char **argv) {
break;
#endif
}
demo_create_surface(&demo);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let demo_create_surface after window is created

demo_select_physical_device(&demo);

demo_init_vk_swapchain(&demo);

Expand Down
50 changes: 31 additions & 19 deletions cube/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sstream>
#include <iostream>
#include <memory>
#include <map>

#if defined(VK_USE_PLATFORM_XLIB_KHR)
#include "xlib_loader.h"
Expand Down Expand Up @@ -317,6 +318,7 @@ struct Demo {
void init(int argc, char **argv);
void check_and_set_wsi_platform();
void init_vk();
void select_physical_device();
void init_vk_swapchain();
void prepare();
void prepare_buffers();
Expand Down Expand Up @@ -1700,7 +1702,9 @@ void Demo::init_vk() {
VERIFY(create_debug_messenger_return.result == vk::Result::eSuccess);
debug_messenger = create_debug_messenger_return.value;
}
}

void Demo::select_physical_device() {
auto physical_device_return = inst.enumeratePhysicalDevices();
VERIFY(physical_device_return.result == vk::Result::eSuccess);
auto physical_devices = physical_device_return.value;
Expand Down Expand Up @@ -1734,32 +1738,33 @@ void Demo::init_vk() {
} else {
/* Try to auto select most suitable device */
if (gpu_number == -1) {
constexpr uint32_t device_type_count = static_cast<uint32_t>(vk::PhysicalDeviceType::eCpu) + 1;
std::array<uint32_t, device_type_count> count_device_type{};

int prev_priority = 0;
for (uint32_t i = 0; i < physical_devices.size(); i++) {
const auto physicalDeviceProperties = physical_devices[i].getProperties();
assert(physicalDeviceProperties.deviceType <= vk::PhysicalDeviceType::eCpu);
count_device_type[static_cast<int>(physicalDeviceProperties.deviceType)]++;
}

std::array<vk::PhysicalDeviceType, device_type_count> const device_type_preference = {
vk::PhysicalDeviceType::eDiscreteGpu, vk::PhysicalDeviceType::eIntegratedGpu, vk::PhysicalDeviceType::eVirtualGpu,
vk::PhysicalDeviceType::eCpu, vk::PhysicalDeviceType::eOther};
auto support_result = physical_devices[i].getSurfaceSupportKHR(0, surface);
if (support_result.result != vk::Result::eSuccess ||
support_result.value != vk::True) {
continue;
}

vk::PhysicalDeviceType search_for_device_type = vk::PhysicalDeviceType::eDiscreteGpu;
for (uint32_t i = 0; i < sizeof(device_type_preference) / sizeof(vk::PhysicalDeviceType); i++) {
if (count_device_type[static_cast<int>(device_type_preference[i])]) {
search_for_device_type = device_type_preference[i];
break;
std::map<vk::PhysicalDeviceType, int> device_type_priorities = {
{vk::PhysicalDeviceType::eDiscreteGpu, 5},
{vk::PhysicalDeviceType::eIntegratedGpu, 4},
{vk::PhysicalDeviceType::eVirtualGpu, 3},
{vk::PhysicalDeviceType::eCpu, 2},
{vk::PhysicalDeviceType::eOther, 1},
};
int priority = -1;
if (device_type_priorities.find(physicalDeviceProperties.deviceType) !=
device_type_priorities.end()) {
priority = device_type_priorities[physicalDeviceProperties.deviceType];
}
}

for (uint32_t i = 0; i < physical_devices.size(); i++) {
const auto physicalDeviceProperties = physical_devices[i].getProperties();
if (physicalDeviceProperties.deviceType == search_for_device_type) {
if (priority > prev_priority) {
gpu_number = i;
break;
prev_priority = priority;
}
}
}
Expand Down Expand Up @@ -1877,7 +1882,6 @@ void Demo::create_surface() {
}

void Demo::init_vk_swapchain() {
create_surface();
// Iterate over each queue to learn whether it supports presenting:
std::vector<vk::Bool32> supportsPresent;
for (uint32_t i = 0; i < static_cast<uint32_t>(queue_props.size()); i++) {
Expand Down Expand Up @@ -3744,6 +3748,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
demo.connection = hInstance;
demo.name = "Vulkan Cube";
demo.create_window();
demo.create_surface();
demo.select_physical_device();
demo.init_vk_swapchain();

demo.prepare();
Expand Down Expand Up @@ -3825,6 +3831,10 @@ int main(int argc, char **argv) {
#endif
}

demo.create_surface();

demo.select_physical_device();

demo.init_vk_swapchain();

demo.prepare();
Expand Down Expand Up @@ -3879,6 +3889,8 @@ int main(int argc, char **argv) {
// Global function invoked from NS or UI views and controllers to create demo
static void demo_main(Demo &demo, void *caMetalLayer, int argc, const char *argv[]) {
demo.init(argc, (char **)argv);
demo.create_surface();
demo.select_physical_device();
demo.caMetalLayer = caMetalLayer;
demo.init_vk_swapchain();
demo.prepare();
Expand Down
Loading