From 5ad0ac7fedbe2ce6cf616e6a38e29d064981979d Mon Sep 17 00:00:00 2001 From: Piotr Krygier Date: Fri, 20 Jun 2025 15:45:55 +0200 Subject: [PATCH] Fix termination errors Cleanup for objects destruction and process termination. This is still work in progress, I don't like the way it's handled right now. --- graphics/src/descriptor_builder.c | 6 ++++++ graphics/src/descriptor_builder.h | 1 + graphics/src/graphics_context.h | 6 +++++- graphics/src/pipeline_builder.c | 2 ++ graphics/src/rse_graphics.c | 8 +++----- graphics/src/vulkan_base.c | 2 ++ graphics/src/vulkan_buffers.c | 12 ++++++++++++ graphics/src/vulkan_image.c | 8 +++++--- graphics/src/window.c | 2 -- red_scarf_engine/src/main.c | 2 ++ 10 files changed, 38 insertions(+), 11 deletions(-) diff --git a/graphics/src/descriptor_builder.c b/graphics/src/descriptor_builder.c index 7c65103e..4ea80bac 100644 --- a/graphics/src/descriptor_builder.c +++ b/graphics/src/descriptor_builder.c @@ -443,3 +443,9 @@ rse_err_t descriptor_attach_buffer(struct graphics_context_t* context, struct de return RSE_ERROR_NO_ERROR; } + +void destroy_descriptors(struct graphics_context_t* context) +{ + vkDestroyDescriptorSetLayout(context->device, context->descriptor_sets->descriptor_sets_layout, NULL); + vkDestroyDescriptorPool(context->device, context->descriptor_sets->descriptor_pool, NULL); +} diff --git a/graphics/src/descriptor_builder.h b/graphics/src/descriptor_builder.h index d3e38f18..fcfaebdc 100644 --- a/graphics/src/descriptor_builder.h +++ b/graphics/src/descriptor_builder.h @@ -80,4 +80,5 @@ rse_err_t descriptor_attach_buffer(struct graphics_context_t* context, struct de rse_err_t descriptor_attach_image(struct graphics_context_t* context, struct descriptor_set_handle_t* descriptor_set_handle, uint16_t texture_id, VkSampler sampler, uint32_t binding); +void destroy_descriptors(struct graphics_context_t* context); #endif // !DESCRIPTOR_BUILDER_H diff --git a/graphics/src/graphics_context.h b/graphics/src/graphics_context.h index 4fcba758..9eeac91c 100644 --- a/graphics/src/graphics_context.h +++ b/graphics/src/graphics_context.h @@ -22,7 +22,7 @@ #include "vulkan/vulkan_core.h" #include "vulkan_commons.h" -#define MAX_MESH_NUMBER 256 +#define MAX_MESH_NUMBER 2 #define MAX_INSTANCE_NUMBER 256 /** @@ -34,6 +34,8 @@ #define MAX_PIPELINE_COUNT 16 RSE_LINKED_LIST_DEFINE(shader_module_list_t, VkShaderModule); +RSE_LINKED_LIST_DEFINE(vulkan_buffer_list_t, struct vulkan_buffer_t); +RSE_LINKED_LIST_DEFINE(descriptor_sets_list_t, VkDescriptorSet); /** * @brief Represents data within vertex or instance buffer. @@ -94,6 +96,7 @@ struct pipeline_internal_t struct graphics_context_t { SDL_Window* window_handle; + VkSampler sampler; VkDevice device; VkSurfaceKHR surface; VkInstance instance; @@ -125,6 +128,7 @@ struct graphics_context_t struct vulkan_buffer_t index_buffer; struct vulkan_buffer_t instance_buffers[MAX_MESH_NUMBER]; struct vulkan_buffer_t draw_indirect_command_buffer; + struct vulkan_buffer_list_t* uniform_buffers; struct vulkan_image_t texture_images[RSE_MAX_IMAGE_COUNT]; struct vulkan_image_t depth_image; struct vulkan_image_t color_image; diff --git a/graphics/src/pipeline_builder.c b/graphics/src/pipeline_builder.c index 073ad40b..3e5f104c 100644 --- a/graphics/src/pipeline_builder.c +++ b/graphics/src/pipeline_builder.c @@ -545,6 +545,8 @@ void destroy_pipelines(struct graphics_context_t* context) size_t i = 0; size_t shader_modules_count = RSE_LINKED_LIST_SIZE(context->shader_modules); + vkDestroyPipelineLayout(context->device, context->pipelines_data.pipeline_layout, NULL); + for (i = 0; i < context->pipelines_data.pipelines_count; i++) { if (context->pipelines_data.pipelines[i] != VK_NULL_HANDLE) { vkDestroyPipeline(context->device, context->pipelines_data.pipelines[i], NULL); diff --git a/graphics/src/rse_graphics.c b/graphics/src/rse_graphics.c index 60671fd2..e9a84125 100644 --- a/graphics/src/rse_graphics.c +++ b/graphics/src/rse_graphics.c @@ -120,15 +120,13 @@ void rse_graphics_test_function(struct rse_graphics_context_t* rse_context) pipeline_add_dynamic_state(pipeline, VK_DYNAMIC_STATE_VIEWPORT); pipeline_add_dynamic_state(pipeline, VK_DYNAMIC_STATE_SCISSOR); - VkSampler sampler = VK_NULL_HANDLE; - - sampler_create(context, &sampler); + sampler_create(context, &context->sampler); descriptor_initialize_handle(&descriptor_set_handle); descriptor_add_set(descriptor_set_handle, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, NULL); descriptor_add_set(descriptor_set_handle, 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, - VK_SHADER_STAGE_FRAGMENT_BIT, &sampler); + VK_SHADER_STAGE_FRAGMENT_BIT, &context->sampler); descriptors_build(context, descriptor_set_handle); @@ -140,7 +138,7 @@ void rse_graphics_test_function(struct rse_graphics_context_t* rse_context) update_uniform_buffers(context, &ubo); descriptor_attach_buffer(context, descriptor_set_handle, &ubo, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); - descriptor_attach_image(context, descriptor_set_handle, tex_id_2, sampler, 1); + descriptor_attach_image(context, descriptor_set_handle, tex_id_2, context->sampler, 1); // TODO: Add a shader for text rendering // pipeline_add_shader_stage("../graphics/shaders/on_screen_text.frag.num", VK_SHADER_STAGE_FRAGMENT_BIT); diff --git a/graphics/src/vulkan_base.c b/graphics/src/vulkan_base.c index 21aba44c..2b4cf607 100644 --- a/graphics/src/vulkan_base.c +++ b/graphics/src/vulkan_base.c @@ -16,6 +16,7 @@ #include #include +#include "src/descriptor_builder.h" #include "utilities/commons.h" #include "utilities/errors_common.h" #include "utilities/logger.h" @@ -659,6 +660,7 @@ void deinit_vulkan(struct graphics_context_t* context) destroy_render_pass(context); cleanup_swapchain(context); destroy_pipelines(context); + destroy_descriptors(context); vmaDestroyAllocator(context->allocator); vkDestroyDevice(context->device, NULL); diff --git a/graphics/src/vulkan_buffers.c b/graphics/src/vulkan_buffers.c index 1fdc3ce7..4721dbc9 100644 --- a/graphics/src/vulkan_buffers.c +++ b/graphics/src/vulkan_buffers.c @@ -10,6 +10,7 @@ #include "src/vulkan_commons.h" #include "utilities/commons.h" #include "utilities/errors_common.h" +#include "utilities/linked_list.h" #include "utilities/localization.h" #include "utilities/logger.h" #include "vulkan_commands.h" @@ -130,10 +131,13 @@ rse_err_t create_uniform_buffer(struct graphics_context_t* context, struct vulka VkDeviceSize buffer_size = sizeof(struct uniform_buffer_object_t); // TODO: Change later. Uniform buffer is not only MVP + STATUS_CHECK(create_buffer( context, buffer_size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST, VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, buffer)); + RSE_LINKED_LIST_PUSH_BACK(context->uniform_buffers, *buffer); + return RSE_ERROR_NO_ERROR; } @@ -168,6 +172,9 @@ rse_err_t create_buffer(struct graphics_context_t* context, const VkDeviceSize s return VULKAN_ERROR_BUFFER_CREATION_FAILED; } + static int count = 1; + LOGI("Created buffers: %d\n", count++); + buffer->allocated_size = size; return RSE_ERROR_NO_ERROR; @@ -176,6 +183,7 @@ rse_err_t create_buffer(struct graphics_context_t* context, const VkDeviceSize s void destroy_buffer(struct graphics_context_t* context, struct vulkan_buffer_t* buffer) { vmaDestroyBuffer(context->allocator, buffer->buffer, buffer->allocation); + } void update_uniform_buffers(struct graphics_context_t* context, const struct vulkan_buffer_t* buffer) @@ -412,4 +420,8 @@ void destroy_buffers(struct graphics_context_t* context) vmaDestroyBuffer(context->allocator, context->instance_buffers[i].buffer, context->instance_buffers[i].allocation); } + + RSE_LINKED_LIST_FOREACH(context->uniform_buffers, buffer) { + vmaDestroyBuffer(context->allocator, buffer->data.buffer, buffer->data.allocation); + } } diff --git a/graphics/src/vulkan_image.c b/graphics/src/vulkan_image.c index 3043baed..bc44eeed 100644 --- a/graphics/src/vulkan_image.c +++ b/graphics/src/vulkan_image.c @@ -119,6 +119,9 @@ static rse_err_t create_image(struct graphics_context_t* context, struct vulkan_ return VULKAN_ERROR_IMAGE_NOT_CREATED; } + static int count = 1; + LOGI("Created image: %d\n", count++); + return RSE_ERROR_NO_ERROR; } @@ -387,9 +390,6 @@ void destroy_textures(struct graphics_context_t* context) { size_t i = 0U; - //FIXME: This is not working for now - // vkDestroySampler(context->device, context->sampler, NULL); - for (i = 0U; i < RSE_MAX_IMAGE_COUNT; ++i) { if (context->texture_images[i].id_taken == IMAGE_TAKEN) { vkDestroyImageView(context->device, context->texture_images[i].image_view, NULL); @@ -397,6 +397,8 @@ void destroy_textures(struct graphics_context_t* context) context->texture_images[i].allocation); } } + + vkDestroySampler(context->device, context->sampler, NULL); } void destroy_depth_resource(struct graphics_context_t* context) diff --git a/graphics/src/window.c b/graphics/src/window.c index 986ad603..61d59f02 100644 --- a/graphics/src/window.c +++ b/graphics/src/window.c @@ -62,6 +62,4 @@ rse_err_t window_loop(struct graphics_context_t* context) void window_terminate(SDL_Window* window_handle) { SDL_DestroyWindow(window_handle); - - SDL_Quit(); } diff --git a/red_scarf_engine/src/main.c b/red_scarf_engine/src/main.c index 86de0185..00ebd7f3 100644 --- a/red_scarf_engine/src/main.c +++ b/red_scarf_engine/src/main.c @@ -1,3 +1,4 @@ +#include #include "graphics/rse_graphics.h" #include "utilities/errors_common.h" #include "utilities/logger.h" @@ -23,6 +24,7 @@ int main(int argc, char** argv) } SDL_WaitThread(graphics_task, NULL); + SDL_Quit(); return 0; }