From e471585946c8878570f22ddd2dd6a8c38044069c Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 14 Nov 2023 12:46:51 +0100 Subject: [PATCH] fixing memory management issue, VMA uses custom asserts --- src/core/application.inl | 2 ++ src/core/bridge.cpp | 9 ++++++++- src/core/graphics.h | 5 ++--- src/renderer/buffers/vk_buffer.cpp | 6 ++++-- src/renderer/core/memory.cpp | 6 +++++- src/renderer/core/memory.h | 3 +-- src/renderer/core/render_core.cpp | 4 ++-- src/renderer/images/vk_image.cpp | 6 ++++-- src/renderer/text_pipeline.h | 4 ++-- test/main.c | 2 +- 10 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/core/application.inl b/src/core/application.inl index 2f7b083..bcdf48c 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -10,6 +10,8 @@ /* */ /* ************************************************************************** */ +#include + namespace mlx::core { void Application::getMousePos(int* x, int* y) noexcept diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 3f7854d..4957679 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ -/* Updated: 2023/04/25 15:23:05 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 11:43:30 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,14 @@ extern "C" { void* mlx_init() { + static bool init = false; + if(init) + { + mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times"); + return NULL; + } mlx::Render_Core::get().init(); + init = true; return new mlx::core::Application(); } diff --git a/src/core/graphics.h b/src/core/graphics.h index ed8cf3b..2e43bd6 100644 --- a/src/core/graphics.h +++ b/src/core/graphics.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ -/* Updated: 2023/11/08 20:41:29 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 11:39:55 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,13 +38,12 @@ namespace mlx inline std::shared_ptr getWindow(); inline void beginRender() noexcept; + void endRender() noexcept; inline void clearRenderData() noexcept; inline void pixelPut(int x, int y, uint32_t color) noexcept; inline void stringPut(int x, int y, int color, std::string str); inline void texturePut(Texture* texture, int x, int y); - - void endRender() noexcept; ~GraphicsSupport(); diff --git a/src/renderer/buffers/vk_buffer.cpp b/src/renderer/buffers/vk_buffer.cpp index a56522a..678ccf6 100644 --- a/src/renderer/buffers/vk_buffer.cpp +++ b/src/renderer/buffers/vk_buffer.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */ -/* Updated: 2023/11/14 07:35:22 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 09:31:58 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,9 @@ namespace mlx { if(_is_mapped) unmapMem(); - Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer); + if(_buffer != VK_NULL_HANDLE) + Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer); + _buffer = VK_NULL_HANDLE; } void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name) diff --git a/src/renderer/core/memory.cpp b/src/renderer/core/memory.cpp index 0f9616c..3570705 100644 --- a/src/renderer/core/memory.cpp +++ b/src/renderer/core/memory.cpp @@ -6,16 +6,18 @@ /* By: kbz_8 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */ -/* Updated: 2023/11/14 06:25:19 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 12:45:29 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include #define VMA_STATIC_VULKAN_FUNCTIONS 0 #define VMA_DYNAMIC_VULKAN_FUNCTIONS 0 #define VMA_VULKAN_VERSION 1002000 +#define VMA_ASSERT(expr) (static_cast(expr) ? void(0) : mlx::core::error::report(e_kind::fatal_error, "Graphics allocator : an assertion has been catched : '%s'", #expr)) #define VMA_IMPLEMENTATION #ifdef MLX_COMPILER_CLANG @@ -87,6 +89,7 @@ namespace mlx void GPUallocator::destroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept { + vkDeviceWaitIdle(Render_Core::get().getDevice().get()); vmaDestroyBuffer(_allocator, buffer, allocation); #ifdef DEBUG core::error::report(e_kind::message, "Graphics Allocator : destroyed buffer"); @@ -108,6 +111,7 @@ namespace mlx void GPUallocator::destroyImage(VmaAllocation allocation, VkImage image) noexcept { + vkDeviceWaitIdle(Render_Core::get().getDevice().get()); vmaDestroyImage(_allocator, image, allocation); #ifdef DEBUG core::error::report(e_kind::message, "Graphics Allocator : destroyed image"); diff --git a/src/renderer/core/memory.h b/src/renderer/core/memory.h index ef3c637..bb3e2b7 100644 --- a/src/renderer/core/memory.h +++ b/src/renderer/core/memory.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */ -/* Updated: 2023/11/14 03:12:59 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 09:46:32 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ #include #include -#include namespace mlx { diff --git a/src/renderer/core/render_core.cpp b/src/renderer/core/render_core.cpp index a7fd250..7a27d12 100644 --- a/src/renderer/core/render_core.cpp +++ b/src/renderer/core/render_core.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */ -/* Updated: 2023/10/21 00:06:36 by kbz_8 ### ########.fr */ +/* Updated: 2023/11/14 08:15:42 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ #include #ifdef DEBUG - #warning "MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages which may impact rendering performances" + #warning MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages which may impact rendering performances #endif namespace mlx diff --git a/src/renderer/images/vk_image.cpp b/src/renderer/images/vk_image.cpp index 5ccf45f..c5a595c 100644 --- a/src/renderer/images/vk_image.cpp +++ b/src/renderer/images/vk_image.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */ -/* Updated: 2023/11/14 03:15:33 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 09:31:39 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -200,7 +200,9 @@ namespace mlx if(_image_view != VK_NULL_HANDLE) vkDestroyImageView(Render_Core::get().getDevice().get(), _image_view, nullptr); - Render_Core::get().getAllocator().destroyImage(_allocation, _image); + if(_image != VK_NULL_HANDLE) + Render_Core::get().getAllocator().destroyImage(_allocation, _image); + _image = VK_NULL_HANDLE; if(_transfer_cmd.isInit()) _transfer_cmd.destroy(); _pool.destroy(); diff --git a/src/renderer/text_pipeline.h b/src/renderer/text_pipeline.h index ea6c57c..e3e3052 100644 --- a/src/renderer/text_pipeline.h +++ b/src/renderer/text_pipeline.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */ -/* Updated: 2023/04/12 13:25:33 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 12:43:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,7 @@ namespace mlx void init(Renderer* renderer) noexcept; void put(int x, int y, int color, std::string str); inline VkDescriptorSet getDescriptorSet() noexcept { return _atlas.getSet(); } - inline void clear() { _drawlist.clear(); } + inline void clear() { _drawlist.clear(); _library.clearLibrary(); } void render(); void destroy() noexcept; diff --git a/test/main.c b/test/main.c index 6979f96..110239c 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/08/28 10:52:33 by maldavid ### ########.fr */ +/* Updated: 2023/11/14 11:14:16 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */