From 5222f35823caf820f83c5222aca26058872c93bd Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 9 Jan 2024 23:19:04 -0700 Subject: [PATCH] Fixed resizing --- src/main.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index a8f903a..633ef71 100644 --- a/src/main.c +++ b/src/main.c @@ -1635,7 +1635,7 @@ VkCommandPool create_command_pool(VkDevice device, uint32_t queue_family) { return command_pool; } -VkResult recreate_swap_chain(VulkanContext* context, VkExtent2D new_extent) { +VkResult recreate_swap_chain(VulkanContext* context) { for(uint32_t i = 0; i < context->swapchain_image_count; i++) { vkDestroyFramebuffer(context->device, context->swapchain_framebuffers[i], 0); vkDestroyImageView(context->device, context->swapchain_image_views[i], 0); @@ -1648,11 +1648,12 @@ VkResult recreate_swap_chain(VulkanContext* context, VkExtent2D new_extent) { free(context->swapchain_details.formats); free(context->swapchain_details.present_modes); - context->swapchain_extent = new_extent; + vkDestroyImageView(context->device, context->depth_image_view, 0); + deallocate_image(context->device, context->depth_image); SwapchainDetails swapchain_details = get_swapchain_details(context->physical_device, context->surface); if(swapchain_details.formats == 0) { - return 0; + return VK_ERROR_INITIALIZATION_FAILED; } else { context->swapchain_details = swapchain_details; } @@ -1661,6 +1662,47 @@ VkResult recreate_swap_chain(VulkanContext* context, VkExtent2D new_extent) { context->swapchain_present_mode = choose_present_mode(context->swapchain_details); context->swapchain_extent = choose_swapchain_extent(context->swapchain_details); + VkExtent3D depth_extent = { + .width = context->swapchain_extent.width, + .height = context->swapchain_extent.height, + .depth = 1, + }; + + AllocatedImage depth_image = allocate_image(context->physical_device, context->device, VK_IMAGE_TYPE_2D, context->depth_format, depth_extent, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + if(depth_image.memory == VK_NULL_HANDLE) { + return VK_ERROR_INITIALIZATION_FAILED; + } else { + context->depth_image = depth_image; + } + + VkImageViewCreateInfo depth_view_info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .image = depth_image.image, + .viewType = VK_IMAGE_VIEW_TYPE_2D, + .format = context->depth_format, + .components = { + .r = VK_COMPONENT_SWIZZLE_IDENTITY, + .g = VK_COMPONENT_SWIZZLE_IDENTITY, + .b = VK_COMPONENT_SWIZZLE_IDENTITY, + .a = VK_COMPONENT_SWIZZLE_IDENTITY, + }, + .subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }, + }; + + VkImageView depth_image_view; + VkResult result = vkCreateImageView(context->device, &depth_view_info, 0, &depth_image_view); + if(result != VK_SUCCESS) { + return VK_ERROR_INITIALIZATION_FAILED; + } else { + context->depth_image_view = depth_image_view; + } + VkSwapchainKHR swapchain = create_swapchain(context->device, context->swapchain_format, context->swapchain_present_mode, context->swapchain_extent, context->surface, context->swapchain_details.capabilities, context->queue_indices, context->swapchain); if(swapchain == VK_NULL_HANDLE) { context->swapchain = VK_NULL_HANDLE; @@ -2965,10 +3007,7 @@ void main_loop(GLFWwindow* window, VulkanContext* context) { VkResult result = draw_frame(context); if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { - int width, height; - glfwGetWindowSize(window, &width, &height); - VkExtent2D window_extent = {width, height}; - recreate_swap_chain(context, window_extent); + recreate_swap_chain(context); } else if(result != VK_SUCCESS) { fprintf(stderr, "draw_frame error %d\n", result); return;