Fixed window resizing

This commit is contained in:
Piotr Krygier committed 2025-06-20 13:44:33 +02:00
1 parent f197280e88
commit 3c72c9b387
6 files changed
+61 -17

No files matched your search

+9 -9
View File
@@ -191,7 +191,7 @@ static rse_err_t create_image_view(struct rse_vulkan_image_t* image, VkFormat fo
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
static rse_err_t create_color_resource() rse_err_t rse_create_color_resource()
{ {
rse_err_t status = RSE_ERROR_NO_ERROR; rse_err_t status = RSE_ERROR_NO_ERROR;
VkFormat color_format = IMAGE_FORMAT; VkFormat color_format = IMAGE_FORMAT;
@@ -262,7 +262,7 @@ static rse_err_t format_to_pixel_size(VkFormat format,
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
static rse_err_t transition_image_layout(struct rse_vulkan_image_t* image, VkImageLayout old_layout, VkImageLayout new_layout) static rse_err_t transition_image_layout(struct rse_vulkan_image_t* image, VkImageLayout old_layout, VkImageLayout new_layout)
{ {
VkImageSubresourceRange range = {0}; VkImageSubresourceRange range = {0};
VkImageMemoryBarrier image_barrier = {0}; VkImageMemoryBarrier image_barrier = {0};
@@ -464,7 +464,7 @@ rse_err_t init_vulkan_images()
STATUS_CHECK(create_sampler()); STATUS_CHECK(create_sampler());
STATUS_CHECK(rse_create_depth_resources()); STATUS_CHECK(rse_create_depth_resources());
// STATUS_CHECK(create_color_resource()); STATUS_CHECK(rse_create_color_resource());
return status; return status;
} }
@@ -516,20 +516,20 @@ void rse_destroy_textures()
vmaDestroyImage(g_vulkan_state.allocator, g_texture_images[i].image, g_texture_images[i].allocation); vmaDestroyImage(g_vulkan_state.allocator, g_texture_images[i].image, g_texture_images[i].allocation);
} }
} }
vkDestroyImageView(g_vulkan_state.device, g_depth_image.image_view, NULL);
vkDestroyImageView(g_vulkan_state.device, g_color_image.image_view, NULL);
vmaDestroyImage(g_vulkan_state.allocator, g_depth_image.image, g_depth_image.allocation);
vmaDestroyImage(g_vulkan_state.allocator, g_color_image.image, g_color_image.allocation);
} }
void rse_destroy_depth_resource() void rse_destroy_depth_resource()
{ {
vkDestroyImageView(g_vulkan_state.device, g_depth_image.image_view, NULL); vkDestroyImageView(g_vulkan_state.device, g_depth_image.image_view, NULL);
vmaDestroyImage(g_vulkan_state.allocator, g_depth_image.image, g_depth_image.allocation); vmaDestroyImage(g_vulkan_state.allocator, g_depth_image.image, g_depth_image.allocation);
} }
void rse_destroy_color_resource()
{
vkDestroyImageView(g_vulkan_state.device, g_color_image.image_view, NULL);
vmaDestroyImage(g_vulkan_state.allocator, g_color_image.image, g_color_image.allocation);
}
uint8_t image_exists(uint8_t image_id) { uint8_t image_exists(uint8_t image_id) {
return g_texture_images[image_id].id_taken == IMAGE_TAKEN; return g_texture_images[image_id].id_taken == IMAGE_TAKEN;
} }
+3
View File
@@ -93,6 +93,9 @@ VkImageView rse_get_depth_image_view(void);
VkImageView rse_get_color_image_view(void); VkImageView rse_get_color_image_view(void);
rse_err_t rse_create_color_resource(void);
void rse_destroy_color_resource(void);
rse_err_t rse_create_depth_resources(void); rse_err_t rse_create_depth_resources(void);
void rse_destroy_depth_resource(void); void rse_destroy_depth_resource(void);
+13 -4
View File
@@ -29,6 +29,7 @@
#include "locale_vulkan.h" #include "locale_vulkan.h"
#include "utilities/rse_errors_common.h" #include "utilities/rse_errors_common.h"
#include "utilities/logger.h" #include "utilities/logger.h"
#include "vulkan/vulkan_core.h"
#include "vulkan_errors.h" #include "vulkan_errors.h"
#include "window.h" #include "window.h"
@@ -553,8 +554,8 @@ static rse_err_t create_sync_objects()
if (VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphore_info, NULL, &g_image_available_semaphores[i]) || if (VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphore_info, NULL, &g_image_available_semaphores[i]) ||
VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphore_info, NULL, &g_render_finished_semaphores[i]) || VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphore_info, NULL, &g_render_finished_semaphores[i]) ||
VK_SUCCESS != vkCreateFence(g_vulkan_state.device, &fenceInfo, NULL, &g_in_flight_fences[i])) { VK_SUCCESS != vkCreateFence(g_vulkan_state.device, &fenceInfo, NULL, &g_in_flight_fences[i])) {
LOGF(vulkan_messages[VULKAN_SYNCOBJCTS_CREATION_FAILED]); LOGF(vulkan_messages[VULKAN_SYNCOBJCTS_CREATION_FAILED]);
return VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED; return VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED;
} }
} }
@@ -613,7 +614,7 @@ rse_err_t draw_frame()
result = vkAcquireNextImageKHR(g_vulkan_state.device, g_vulkan_state.swapchain, UINT64_MAX, g_image_available_semaphores[g_vulkan_state.current_frame], result = vkAcquireNextImageKHR(g_vulkan_state.device, g_vulkan_state.swapchain, UINT64_MAX, g_image_available_semaphores[g_vulkan_state.current_frame],
VK_NULL_HANDLE, &image_index); VK_NULL_HANDLE, &image_index);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { if (result == VK_ERROR_OUT_OF_DATE_KHR) {
recreate_swapchain(); recreate_swapchain();
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
@@ -660,7 +661,15 @@ rse_err_t draw_frame()
present_info.pImageIndices = &image_index; present_info.pImageIndices = &image_index;
vkQueuePresentKHR(g_present_queue, &present_info); result = vkQueuePresentKHR(g_present_queue, &present_info);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || is_framebuffer_resized()) {
reset_framebuffer_resized();
recreate_swapchain();
} else if (result != VK_SUCCESS) {
LOGF(vulkan_messages[VULAKN_DRAW_FAILED]);
return VULKAN_ERROR_DRAW_FAILED;
}
g_vulkan_state.current_frame = (g_vulkan_state.current_frame + 1) % SWAP_BUFFER_COUNT; g_vulkan_state.current_frame = (g_vulkan_state.current_frame + 1) % SWAP_BUFFER_COUNT;
+7 -4
View File
@@ -780,16 +780,20 @@ void recreate_swapchain()
{ {
int width = 0; int width = 0;
int height = 0; int height = 0;
glfwGetFramebufferSize(get_window_handle(), &width, &height);
while (width == 0 || height == 0) { while (width == 0 || height == 0) {
glfwGetFramebufferSize(get_window_handle(), &width, &height); glfwGetFramebufferSize(get_window_handle(), &width, &height);
glfwWaitEvents(); glfwWaitEvents();
} }
vkDeviceWaitIdle(g_vulkan_state.device);
cleanup_swapchain(); cleanup_swapchain();
create_swapchain(); create_swapchain();
create_swapchain_image_views(); create_swapchain_image_views();
rse_create_color_resource();
rse_create_depth_resources(); rse_create_depth_resources();
create_framebuffers(); create_framebuffers();
} }
@@ -802,8 +806,7 @@ void cleanup_swapchain()
{ {
size_t i; size_t i;
vkDeviceWaitIdle(g_vulkan_state.device); rse_destroy_color_resource();
rse_destroy_depth_resource(); rse_destroy_depth_resource();
for (i = 0; i < g_swapchain_images_count; i++) { for (i = 0; i < g_swapchain_images_count; i++) {
vkDestroyFramebuffer(g_vulkan_state.device, g_vulkan_state.swapchain_framebuffers[i], NULL); vkDestroyFramebuffer(g_vulkan_state.device, g_vulkan_state.swapchain_framebuffers[i], NULL);
+25
View File
@@ -10,14 +10,19 @@
*/ */
#include "window.h" #include "window.h"
#include <stdint.h>
#include "GLFW/glfw3.h"
#include "locale_window.h" #include "locale_window.h"
#include "rse_window_errors.h" #include "rse_window_errors.h"
#include "src/vulkan_commons.h"
#include "utilities/logger.h" #include "utilities/logger.h"
#include "utilities/rse_errors_common.h" #include "utilities/rse_errors_common.h"
#include "vulkan/vulkan_core.h"
#include "vulkan_base.h" #include "vulkan_base.h"
GLFWwindow *g_window_handle = NULL; GLFWwindow *g_window_handle = NULL;
uint8_t g_is_frambuffer_resized = 0U;
/** /**
* @brief GLFW error callback * @brief GLFW error callback
@@ -51,6 +56,14 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action,
} }
} }
static void framebuffer_resized_callback(GLFWwindow* window, int width, int height)
{
(void)window;
(void)width;
(void)height;
g_is_frambuffer_resized = 1;
}
rse_err_t window_init() rse_err_t window_init()
{ {
@@ -82,6 +95,7 @@ rse_err_t window_init()
} }
glfwSetKeyCallback(g_window_handle, key_callback); glfwSetKeyCallback(g_window_handle, key_callback);
glfwSetFramebufferSizeCallback(g_window_handle, framebuffer_resized_callback);
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
@@ -93,6 +107,7 @@ rse_err_t window_loop()
draw_frame(); draw_frame();
} }
vkDeviceWaitIdle(g_vulkan_state.device);
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
@@ -107,3 +122,13 @@ GLFWwindow* get_window_handle()
{ {
return g_window_handle; return g_window_handle;
} }
uint8_t is_framebuffer_resized()
{
return g_is_frambuffer_resized;
}
void reset_framebuffer_resized()
{
g_is_frambuffer_resized = 0U;
}
+4
View File
@@ -12,6 +12,7 @@
#ifndef RSE_WINDOW_H #ifndef RSE_WINDOW_H
#define RSE_WINDOW_H #define RSE_WINDOW_H
#include <stdint.h>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
/* Vulkan header MUST be included before glfw */ /* Vulkan header MUST be included before glfw */
@@ -45,4 +46,7 @@ void window_terminate();
GLFWwindow* get_window_handle(); GLFWwindow* get_window_handle();
uint8_t is_framebuffer_resized(void);
void reset_framebuffer_resized(void);
#endif /* RSE_WINDOW_H */ #endif /* RSE_WINDOW_H */