Skip to content

Commit 39aa800

Browse files
committed
fixing memory management issue, VMA uses custom asserts
1 parent 5dce6a2 commit 39aa800

File tree

10 files changed

+31
-16
lines changed

10 files changed

+31
-16
lines changed

src/core/application.inl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include <core/application.h>
14+
1315
namespace mlx::core
1416
{
1517
void Application::getMousePos(int* x, int* y) noexcept

src/core/bridge.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
9-
/* Updated: 2023/04/25 15:23:05 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 11:43:30 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,7 +20,14 @@ extern "C"
2020
{
2121
void* mlx_init()
2222
{
23+
static bool init = false;
24+
if(init)
25+
{
26+
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
27+
return NULL;
28+
}
2329
mlx::Render_Core::get().init();
30+
init = true;
2431
return new mlx::core::Application();
2532
}
2633

src/core/graphics.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
9-
/* Updated: 2023/11/08 20:41:29 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 11:39:55 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -38,13 +38,12 @@ namespace mlx
3838
inline std::shared_ptr<MLX_Window> getWindow();
3939

4040
inline void beginRender() noexcept;
41+
void endRender() noexcept;
4142

4243
inline void clearRenderData() noexcept;
4344
inline void pixelPut(int x, int y, uint32_t color) noexcept;
4445
inline void stringPut(int x, int y, int color, std::string str);
4546
inline void texturePut(Texture* texture, int x, int y);
46-
47-
void endRender() noexcept;
4847

4948
~GraphicsSupport();
5049

src/renderer/buffers/vk_buffer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
9-
/* Updated: 2023/11/14 07:35:22 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 09:31:58 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -54,7 +54,9 @@ namespace mlx
5454
{
5555
if(_is_mapped)
5656
unmapMem();
57-
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
57+
if(_buffer != VK_NULL_HANDLE)
58+
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
59+
_buffer = VK_NULL_HANDLE;
5860
}
5961

6062
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name)

src/renderer/core/memory.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */
9-
/* Updated: 2023/11/14 06:25:19 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 12:45:29 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include <core/profile.h>
14+
#include <core/errors.h>
1415
#include <cstdio>
1516

1617
#define VMA_STATIC_VULKAN_FUNCTIONS 0
1718
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
1819
#define VMA_VULKAN_VERSION 1002000
20+
#define VMA_ASSERT(expr) (static_cast<bool>(expr) ? void(0) : mlx::core::error::report(e_kind::fatal_error, "Graphics allocator : an assertion has been catched : '%s'", #expr))
1921
#define VMA_IMPLEMENTATION
2022

2123
#ifdef MLX_COMPILER_CLANG
@@ -87,6 +89,7 @@ namespace mlx
8789

8890
void GPUallocator::destroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept
8991
{
92+
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
9093
vmaDestroyBuffer(_allocator, buffer, allocation);
9194
#ifdef DEBUG
9295
core::error::report(e_kind::message, "Graphics Allocator : destroyed buffer");
@@ -108,6 +111,7 @@ namespace mlx
108111

109112
void GPUallocator::destroyImage(VmaAllocation allocation, VkImage image) noexcept
110113
{
114+
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
111115
vmaDestroyImage(_allocator, image, allocation);
112116
#ifdef DEBUG
113117
core::error::report(e_kind::message, "Graphics Allocator : destroyed image");

src/renderer/core/memory.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */
9-
/* Updated: 2023/11/14 03:12:59 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 09:46:32 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -15,7 +15,6 @@
1515

1616
#include <volk.h>
1717
#include <vma.h>
18-
#include <cstdint>
1918

2019
namespace mlx
2120
{

src/renderer/core/render_core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */
9-
/* Updated: 2023/10/21 00:06:36 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/11/14 08:15:42 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -24,7 +24,7 @@
2424
#include <mutex>
2525

2626
#ifdef DEBUG
27-
#warning "MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages which may impact rendering performances"
27+
#warning MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages which may impact rendering performances
2828
#endif
2929

3030
namespace mlx

src/renderer/images/vk_image.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */
9-
/* Updated: 2023/11/14 03:15:33 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 09:31:39 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -200,7 +200,9 @@ namespace mlx
200200
if(_image_view != VK_NULL_HANDLE)
201201
vkDestroyImageView(Render_Core::get().getDevice().get(), _image_view, nullptr);
202202

203-
Render_Core::get().getAllocator().destroyImage(_allocation, _image);
203+
if(_image != VK_NULL_HANDLE)
204+
Render_Core::get().getAllocator().destroyImage(_allocation, _image);
205+
_image = VK_NULL_HANDLE;
204206
if(_transfer_cmd.isInit())
205207
_transfer_cmd.destroy();
206208
_pool.destroy();

src/renderer/text_pipeline.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
9-
/* Updated: 2023/04/12 13:25:33 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 12:43:45 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -59,7 +59,7 @@ namespace mlx
5959
void init(Renderer* renderer) noexcept;
6060
void put(int x, int y, int color, std::string str);
6161
inline VkDescriptorSet getDescriptorSet() noexcept { return _atlas.getSet(); }
62-
inline void clear() { _drawlist.clear(); }
62+
inline void clear() { _drawlist.clear(); _library.clearLibrary(); }
6363
void render();
6464
void destroy() noexcept;
6565

test/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
9-
/* Updated: 2023/08/28 10:52:33 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 11:14:16 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

0 commit comments

Comments
 (0)