From 39555882f24f3639264e5cdd8f3b48003f394533 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 27 Sep 2024 00:20:25 +0200 Subject: [PATCH] this is so ugly I'm killing myself --- src/ptk_vk/init.c | 17 +++++++------- src/ptk_vk/swapchain.c | 50 ++++++++++++++++++++++++++++-------------- src/ptk_vk/swapchain.h | 10 ++++----- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/ptk_vk/init.c b/src/ptk_vk/init.c index efd5707..39cebdb 100644 --- a/src/ptk_vk/init.c +++ b/src/ptk_vk/init.c @@ -121,13 +121,13 @@ bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .pNext = NULL, .renderPass = m_render_pass, - .framebuffer = g_swapchain_framebuffers.data[image_index], + .framebuffer = vk_current_swapchain_framebuffers().data[image_index], .renderArea = (VkRect2D){ .offset = (VkOffset2D){ .x = 0, .y = 0, }, - .extent = g_swapchain_extent, + .extent = vk_current_swapchain_extent(), }, .clearValueCount = 1, .pClearValues = &(VkClearValue){ @@ -162,8 +162,9 @@ bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32 } bool vk_update_uniform_buffer(const size_t current_frame) { - m_uniform_buffer_object.window_size.w = g_swapchain_extent.width; - m_uniform_buffer_object.window_size.h = g_swapchain_extent.height; + const VkExtent2D current_extent = vk_current_swapchain_extent(); + m_uniform_buffer_object.window_size.w = current_extent.width; + m_uniform_buffer_object.window_size.h = current_extent.height; memcpy(m_uniform_buffers_mapped.data[current_frame], &m_uniform_buffer_object, sizeof(m_uniform_buffer_object)); @@ -217,7 +218,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const return false; } - PTK_OPTION(VkRenderPass) render_pass_opt = vk_create_render_pass(m_dev, g_swapchain_image_format); + PTK_OPTION(VkRenderPass) render_pass_opt = vk_create_render_pass(m_dev, vk_current_swapchain_image_format()); if (!render_pass_opt.exists) { PTK_ERR("failed creating render pass"); @@ -235,7 +236,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_descriptor_set_layout = descriptor_set_layout_opt.value; - PTK_OPTION(PipelineStuff) pipeline_stuff_opt = vk_create_pipeline(m_dev, m_render_pass, m_descriptor_set_layout, g_swapchain_extent); + PTK_OPTION(PipelineStuff) pipeline_stuff_opt = vk_create_pipeline(m_dev, m_render_pass, m_descriptor_set_layout, vk_current_swapchain_extent()); if (!pipeline_stuff_opt.exists) { PTK_ERR("failed creating graphics pipeline"); @@ -367,7 +368,7 @@ bool vk_draw_frame(void) { uint32_t image_index; const VkResult acquire_next_image_result = vkAcquireNextImageKHR( m_dev, - g_swapchain, + vk_current_swapchain(), UINT64_MAX, m_image_available_semaphores.data[m_current_frame], VK_NULL_HANDLE, @@ -421,7 +422,7 @@ bool vk_draw_frame(void) { .waitSemaphoreCount = 1, .pWaitSemaphores = signal_semaphores, .swapchainCount = 1, - .pSwapchains = &g_swapchain, + .pSwapchains = &(VkSwapchainKHR){vk_current_swapchain()}, .pImageIndices = &image_index, .pResults = NULL, }); diff --git a/src/ptk_vk/swapchain.c b/src/ptk_vk/swapchain.c index d8275e3..8503a68 100644 --- a/src/ptk_vk/swapchain.c +++ b/src/ptk_vk/swapchain.c @@ -15,7 +15,7 @@ // TODO: clean up globals here -VkSwapchainKHR g_swapchain = VK_NULL_HANDLE; +static VkSwapchainKHR m_swapchain; PTK_LIST_DEFINE(VkImage); PTK_LIST_DEFINE(VkImageView); @@ -23,10 +23,10 @@ PTK_LIST_DEFINE(VkImageView); static PTK_LIST(VkImage) m_swapchain_images; static PTK_LIST(VkImageView) m_swapchain_image_views; -VkFormat g_swapchain_image_format; -VkExtent2D g_swapchain_extent; +static VkFormat m_swapchain_image_format; +static VkExtent2D m_swapchain_extent; -PTK_LIST(VkFramebuffer) g_swapchain_framebuffers; +static PTK_LIST(VkFramebuffer) m_swapchain_framebuffers; VkSurfaceFormatKHR select_swap_surface_format(const PTK_LIST(VkSurfaceFormatKHR) available_formats) { PTK_LIST_FOR_EACH(const VkSurfaceFormatKHR, available_formats, current_format, { @@ -125,7 +125,7 @@ bool vk_create_image_views(VkDevice dev) { .flags = 0, .image = swapchain_image, .viewType = VK_IMAGE_VIEW_TYPE_2D, - .format = g_swapchain_image_format, + .format = m_swapchain_image_format, .components = (VkComponentMapping){ .r = VK_COMPONENT_SWIZZLE_IDENTITY, .g = VK_COMPONENT_SWIZZLE_IDENTITY, @@ -150,6 +150,22 @@ bool vk_create_image_views(VkDevice dev) { return true; } +VkSwapchainKHR vk_current_swapchain(void) { + return m_swapchain; +} + +VkFormat vk_current_swapchain_image_format(void) { + return m_swapchain_image_format; +} + +VkExtent2D vk_current_swapchain_extent(void) { + return m_swapchain_extent; +} + +PTK_LIST(VkFramebuffer) vk_current_swapchain_framebuffers(void) { + return m_swapchain_framebuffers; +} + bool vk_create_swapchain(GLFWwindow *window, VkDevice dev, VkPhysicalDevice physical_dev, VkSurfaceKHR surface, QueueFamilyIndices queue_family_indices) { const SwapchainSupportInfo swapchain_support = query_swapchain_support(physical_dev, surface); @@ -201,17 +217,17 @@ bool vk_create_swapchain(GLFWwindow *window, VkDevice dev, VkPhysicalDevice phys .oldSwapchain = VK_NULL_HANDLE, }, NULL, - &g_swapchain + &m_swapchain ) ); - vkGetSwapchainImagesKHR(dev, g_swapchain, &m_swapchain_images.allocated, NULL); + vkGetSwapchainImagesKHR(dev, m_swapchain, &m_swapchain_images.allocated, NULL); m_swapchain_images = PTK_LIST_NEW(VkImage, m_swapchain_images.allocated); - vkGetSwapchainImagesKHR(dev, g_swapchain, &m_swapchain_images.allocated, m_swapchain_images.data); + vkGetSwapchainImagesKHR(dev, m_swapchain, &m_swapchain_images.allocated, m_swapchain_images.data); PTK_LIST_FILLED(m_swapchain_images); - g_swapchain_image_format = surface_format.format; - g_swapchain_extent = extent; + m_swapchain_image_format = surface_format.format; + m_swapchain_extent = extent; PTK_LIST_FREE(swapchain_support.formats); PTK_LIST_FREE(swapchain_support.present_modes); @@ -251,7 +267,7 @@ bool vk_recreate_swapchain(GLFWwindow *window, VkDevice dev, VkPhysicalDevice ph } bool vk_create_framebuffers(VkDevice dev, VkRenderPass render_pass) { - g_swapchain_framebuffers = PTK_LIST_NEW(VkFramebuffer, m_swapchain_image_views.size); + m_swapchain_framebuffers = PTK_LIST_NEW(VkFramebuffer, m_swapchain_image_views.size); VkFramebuffer fb; @@ -266,15 +282,15 @@ bool vk_create_framebuffers(VkDevice dev, VkRenderPass render_pass) { .renderPass = render_pass, .attachmentCount = 1, .pAttachments = &swapchain_image_view, - .width = g_swapchain_extent.width, - .height = g_swapchain_extent.height, + .width = m_swapchain_extent.width, + .height = m_swapchain_extent.height, .layers = 1, }, NULL, &fb ) ); - PTK_LIST_ADD(VkFramebuffer, g_swapchain_framebuffers, fb); + PTK_LIST_ADD(VkFramebuffer, m_swapchain_framebuffers, fb); }) return true; @@ -282,10 +298,10 @@ bool vk_create_framebuffers(VkDevice dev, VkRenderPass render_pass) { void vk_cleanup_swapchain(VkDevice dev) { - PTK_LIST_FOR_EACH(VkFramebuffer, g_swapchain_framebuffers, fb, { + PTK_LIST_FOR_EACH(VkFramebuffer, m_swapchain_framebuffers, fb, { vkDestroyFramebuffer(dev, fb, NULL); }) - PTK_LIST_FREE(g_swapchain_framebuffers); + PTK_LIST_FREE(m_swapchain_framebuffers); PTK_LIST_FREE(m_swapchain_images); PTK_LIST_FOR_EACH(VkImageView, m_swapchain_image_views, iv, { @@ -293,5 +309,5 @@ void vk_cleanup_swapchain(VkDevice dev) { }) PTK_LIST_FREE(m_swapchain_image_views); - vkDestroySwapchainKHR(dev, g_swapchain, NULL); + vkDestroySwapchainKHR(dev, m_swapchain, NULL); } diff --git a/src/ptk_vk/swapchain.h b/src/ptk_vk/swapchain.h index 631f4a5..3f506fa 100644 --- a/src/ptk_vk/swapchain.h +++ b/src/ptk_vk/swapchain.h @@ -13,11 +13,6 @@ PTK_LIST_DEFINE(VkFramebuffer); -extern VkSwapchainKHR g_swapchain; -extern VkFormat g_swapchain_image_format; -extern VkExtent2D g_swapchain_extent; -extern PTK_LIST(VkFramebuffer) g_swapchain_framebuffers; - PTK_LIST_DEFINE(VkSurfaceFormatKHR); PTK_LIST_DEFINE(VkPresentModeKHR); @@ -29,6 +24,11 @@ typedef struct { SwapchainSupportInfo query_swapchain_support(const VkPhysicalDevice dev, const VkSurfaceKHR surface); +VkSwapchainKHR vk_current_swapchain(void); +VkFormat vk_current_swapchain_image_format(void); +VkExtent2D vk_current_swapchain_extent(void); +PTK_LIST(VkFramebuffer) vk_current_swapchain_framebuffers(void); + bool vk_create_swapchain(GLFWwindow *window, VkDevice dev, VkPhysicalDevice physical_dev, VkSurfaceKHR surface, QueueFamilyIndices queue_family_indices); bool vk_create_image_views(VkDevice dev);