From eff76c5061d6ab054109a0c4a0d39f6264677b58 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Thu, 21 Dec 2023 22:26:23 +0100 Subject: [PATCH 1/3] Start cleaning up the Vulkan code --- .../Sources/kinc/backend/graphics5/Vulkan.c.h | 43 +++++-------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h index c10ea4036..c4d3bb7f6 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h @@ -1,4 +1,5 @@ #include "kinc/graphics4/graphics.h" + #include "vulkan.h" #include @@ -7,26 +8,14 @@ #include #include #include + #include + #include struct vk_funs vk = {0}; struct vk_context vk_ctx = {0}; -#ifdef KORE_WINDOWS -#define ERR_EXIT(err_msg, err_class) \ - do { \ - MessageBoxA(NULL, err_msg, err_class, MB_OK); \ - exit(1); \ - } while (0) -#else -#define ERR_EXIT(err_msg, err_class) \ - do { \ - kinc_log(KINC_LOG_LEVEL_ERROR, "%s", err_msg); \ - exit(1); \ - } while (0) -#endif - void kinc_vulkan_get_instance_extensions(const char **extensions, int *index, int max); VkBool32 kinc_vulkan_get_physical_device_presentation_support(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex); VkResult kinc_vulkan_create_surface(VkInstance instance, int window_index, VkSurfaceKHR *surface); @@ -35,7 +24,7 @@ VkResult kinc_vulkan_create_surface(VkInstance instance, int window_index, VkSur { \ vk.fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(instance, "vk" #entrypoint); \ if (vk.fp##entrypoint == NULL) { \ - ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \ + kinc_error("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \ } \ } @@ -728,21 +717,13 @@ void kinc_g5_internal_init() { err = vkCreateInstance(&info, NULL, &vk_ctx.instance); #endif if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { - ERR_EXIT("Cannot find a compatible Vulkan installable client driver " - "(ICD).\n\nPlease look at the Getting Started guide for " - "additional information.\n", - "vkCreateInstance Failure"); + kinc_error("Vulkan driver is incompatible"); } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) { - ERR_EXIT("Cannot find a specified extension library" - ".\nMake sure your layers path is set appropriately\n", - "vkCreateInstance Failure"); + kinc_error("Vulkan extension not found"); } else if (err) { - ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " - "installable client driver (ICD) installed?\nPlease look at " - "the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + kinc_error("Can not create Vulkan instance"); } uint32_t gpu_count; @@ -759,11 +740,7 @@ void kinc_g5_internal_init() { free(physical_devices); } else { - ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices." - "\n\nDo you have a compatible Vulkan installable client" - " driver (ICD) installed?\nPlease look at the Getting Started" - " guide for additional information.\n", - "vkEnumeratePhysicalDevices Failure"); + kinc_error("No Vulkan device found"); } static const char *wanted_device_layers[64]; @@ -902,7 +879,7 @@ void kinc_g5_internal_init() { // Generate error if could not find both a graphics and a present queue if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) { - ERR_EXIT("Could not find a graphics and a present queue\n", "Swapchain Initialization Failure"); + kinc_error("Graphics or present queue not found"); } // TODO: Add support for separate queues, including presentation, @@ -911,7 +888,7 @@ void kinc_g5_internal_init() { // and a present queues, this demo program assumes it is only using // one: if (graphicsQueueNodeIndex != presentQueueNodeIndex) { - ERR_EXIT("Could not find a common graphics and a present queue\n", "Swapchain Initialization Failure"); + kinc_error("Graphics and present queue do not match"); } graphics_queue_node_index = graphicsQueueNodeIndex; From 5cde663ac388e0c9c22220b6e5ab7889537a5a04 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Thu, 21 Dec 2023 22:33:53 +0100 Subject: [PATCH 2/3] Fix compilation for Linux/Vulkan --- .../Sources/kinc/backend/graphics5/Vulkan.c.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h index c4d3bb7f6..2ca6c8a31 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h @@ -24,7 +24,7 @@ VkResult kinc_vulkan_create_surface(VkInstance instance, int window_index, VkSur { \ vk.fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(instance, "vk" #entrypoint); \ if (vk.fp##entrypoint == NULL) { \ - kinc_error("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \ + kinc_error_message("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \ } \ } @@ -717,13 +717,13 @@ void kinc_g5_internal_init() { err = vkCreateInstance(&info, NULL, &vk_ctx.instance); #endif if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { - kinc_error("Vulkan driver is incompatible"); + kinc_error_message("Vulkan driver is incompatible"); } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) { - kinc_error("Vulkan extension not found"); + kinc_error_message("Vulkan extension not found"); } else if (err) { - kinc_error("Can not create Vulkan instance"); + kinc_error_message("Can not create Vulkan instance"); } uint32_t gpu_count; @@ -740,7 +740,7 @@ void kinc_g5_internal_init() { free(physical_devices); } else { - kinc_error("No Vulkan device found"); + kinc_error_message("No Vulkan device found"); } static const char *wanted_device_layers[64]; @@ -879,7 +879,7 @@ void kinc_g5_internal_init() { // Generate error if could not find both a graphics and a present queue if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) { - kinc_error("Graphics or present queue not found"); + kinc_error_message("Graphics or present queue not found"); } // TODO: Add support for separate queues, including presentation, @@ -888,7 +888,7 @@ void kinc_g5_internal_init() { // and a present queues, this demo program assumes it is only using // one: if (graphicsQueueNodeIndex != presentQueueNodeIndex) { - kinc_error("Graphics and present queue do not match"); + kinc_error_message("Graphics and present queue do not match"); } graphics_queue_node_index = graphicsQueueNodeIndex; From e49069b0bbccfc1f582883040461975defffb9a6 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Fri, 22 Dec 2023 17:45:06 +0100 Subject: [PATCH 3/3] Continue cleaning up the Vulkan code --- .../Sources/kinc/backend/graphics5/Vulkan.c.h | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h index 2ca6c8a31..b009b74dc 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h @@ -24,14 +24,12 @@ VkResult kinc_vulkan_create_surface(VkInstance instance, int window_index, VkSur { \ vk.fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(instance, "vk" #entrypoint); \ if (vk.fp##entrypoint == NULL) { \ - kinc_error_message("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \ + kinc_error_message("vkGetInstanceProcAddr failed to find vk" #entrypoint); \ } \ } #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#define APP_NAME_STR_LEN 80 - void createDescriptorLayout(void); void set_image_layout(VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout, VkImageLayout new_image_layout); @@ -104,10 +102,8 @@ static VKAPI_ATTR void VKAPI_CALL myfree(void *pUserData, void *pMemory) { #endif bool memory_type_from_properties(uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) { - // Search memtypes to find first index with those properties for (uint32_t i = 0; i < 32; i++) { if ((typeBits & 1) == 1) { - // Type is available, does it match user properties? if ((memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { *typeIndex = i; return true; @@ -115,7 +111,6 @@ bool memory_type_from_properties(uint32_t typeBits, VkFlags requirements_mask, u } typeBits >>= 1; } - // No memory types matched, return failure return false; } @@ -370,29 +365,23 @@ void create_swapchain(int window_index) { VkMemoryRequirements mem_reqs = {0}; bool pass; - /* create image */ err = vkCreateImage(vk_ctx.device, &image, NULL, &window->depth.image); assert(!err); - /* get memory requirements for this object */ vkGetImageMemoryRequirements(vk_ctx.device, window->depth.image, &mem_reqs); - /* select memory size and type */ mem_alloc.allocationSize = mem_reqs.size; - pass = memory_type_from_properties(mem_reqs.memoryTypeBits, 0, /* No requirements */ &mem_alloc.memoryTypeIndex); + pass = memory_type_from_properties(mem_reqs.memoryTypeBits, 0, &mem_alloc.memoryTypeIndex); assert(pass); - /* allocate memory */ err = vkAllocateMemory(vk_ctx.device, &mem_alloc, NULL, &window->depth.memory); assert(!err); - /* bind memory */ err = vkBindImageMemory(vk_ctx.device, window->depth.image, window->depth.memory, 0); assert(!err); set_image_layout(window->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); - /* create image view */ view.image = window->depth.image; err = vkCreateImageView(vk_ctx.device, &view, NULL, &window->depth.view); assert(!err); @@ -667,7 +656,7 @@ void kinc_g5_internal_init() { check_extensions(wanted_instance_extensions, wanted_instance_extension_count, instance_extensions, instance_extension_count); if (missing_instance_extensions) { - exit(1); + kinc_error(); } #ifdef VALIDATE