Skip to content

Commit 4fc417d

Browse files
committed
vkcube: Check if gpu support the surface
1 parent 5689add commit 4fc417d

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

cube/cube.c

+35-5
Original file line numberDiff line numberDiff line change
@@ -4143,7 +4143,10 @@ static void demo_init_vk(struct demo *demo) {
41434143
}
41444144

41454145
volkLoadInstance(demo->inst);
4146+
}
41464147

4148+
static void demo_select_physical_device(struct demo* demo) {
4149+
VkResult err;
41474150
/* Make initial call to query gpu_count, then second call for gpu info */
41484151
uint32_t gpu_count = 0;
41494152
err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
@@ -4188,7 +4191,10 @@ static void demo_init_vk(struct demo *demo) {
41884191
for (uint32_t i = 0; i < gpu_count; i++) {
41894192
vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
41904193
assert(physicalDeviceProperties.deviceType <= VK_PHYSICAL_DEVICE_TYPE_CPU);
4191-
count_device_type[physicalDeviceProperties.deviceType]++;
4194+
VkBool32 supported = 0;
4195+
vkGetPhysicalDeviceSurfaceSupportKHR(physical_devices[i], 0, demo->surface, &supported);
4196+
if (supported)
4197+
count_device_type[physicalDeviceProperties.deviceType]++;
41924198
}
41934199

41944200
VkPhysicalDeviceType search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
@@ -4298,6 +4304,24 @@ static void demo_init_vk(struct demo *demo) {
42984304
}
42994305

43004306
if (demo->validate) {
4307+
/*
4308+
* This is info for a temp callback to use during CreateInstance.
4309+
* After the instance is created, we use the instance-based
4310+
* function to register the final callback.
4311+
*/
4312+
VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
4313+
// VK_EXT_debug_utils style
4314+
dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
4315+
dbg_messenger_create_info.pNext = NULL;
4316+
dbg_messenger_create_info.flags = 0;
4317+
dbg_messenger_create_info.messageSeverity =
4318+
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
4319+
dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
4320+
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
4321+
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
4322+
dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
4323+
dbg_messenger_create_info.pUserData = demo;
4324+
43014325
err = vkCreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
43024326
switch (err) {
43034327
case VK_SUCCESS:
@@ -4492,8 +4516,6 @@ static VkSurfaceFormatKHR pick_surface_format(const VkSurfaceFormatKHR *surfaceF
44924516
static void demo_init_vk_swapchain(struct demo *demo) {
44934517
VkResult U_ASSERT_ONLY err;
44944518

4495-
demo_create_surface(demo);
4496-
44974519
// Iterate over each queue to learn whether it supports presenting:
44984520
VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
44994521
for (uint32_t i = 0; i < demo->queue_family_count; i++) {
@@ -4853,7 +4875,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
48534875
argv = NULL;
48544876
}
48554877

4856-
demo_init(&demo, argc, argv);
48574878

48584879
// Free up the items we had to allocate for the command line arguments.
48594880
if (argc > 0 && argv != NULL) {
@@ -4868,6 +4889,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
48684889
demo.connection = hInstance;
48694890
strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
48704891
demo_create_window(&demo);
4892+
demo_init(&demo, argc, argv);
4893+
demo_create_surface(&demo);
4894+
demo_select_physical_device(&demo);
48714895
demo_init_vk_swapchain(&demo);
48724896

48734897
demo_prepare(&demo);
@@ -4906,6 +4930,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
49064930
#if defined(VK_USE_PLATFORM_METAL_EXT)
49074931
static void demo_main(struct demo *demo, void *caMetalLayer, int argc, const char *argv[]) {
49084932
demo_init(demo, argc, (char **)argv);
4933+
demo_create_surface(demo);
4934+
demo_select_physical_device(demo);
49094935
demo->caMetalLayer = caMetalLayer;
49104936
demo_init_vk_swapchain(demo);
49114937
demo_prepare(demo);
@@ -4953,12 +4979,14 @@ static void processCommand(struct android_app *app, int32_t cmd) {
49534979
__android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
49544980
for (int i = 0; i < argc; i++) __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
49554981

4982+
demo.window = (void *)app->window;
49564983
demo_init(&demo, argc, argv);
4984+
demo_create_surface(&demo);
4985+
demo_select_physical_device(&demo);
49574986

49584987
// Free the argv malloc'd by get_args
49594988
for (int i = 0; i < argc; i++) free(argv[i]);
49604989

4961-
demo.window = (void *)app->window;
49624990
demo_init_vk_swapchain(&demo);
49634991
demo_prepare(&demo);
49644992
initialized = true;
@@ -5044,6 +5072,8 @@ int main(int argc, char **argv) {
50445072
break;
50455073
#endif
50465074
}
5075+
demo_create_surface(&demo);
5076+
demo_select_physical_device(&demo);
50475077

50485078
demo_init_vk_swapchain(&demo);
50495079

0 commit comments

Comments
 (0)