Skip to content

Commit

Permalink
fixing memory management issue, VMA uses custom asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Nov 14, 2023
1 parent 5dce6a2 commit 39aa800
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/core/application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/* */
/* ************************************************************************** */

#include <core/application.h>

namespace mlx::core
{
void Application::getMousePos(int* x, int* y) noexcept
Expand Down
9 changes: 8 additions & 1 deletion src/core/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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();
}

Expand Down
5 changes: 2 additions & 3 deletions src/core/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,13 +38,12 @@ namespace mlx
inline std::shared_ptr<MLX_Window> 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();

Expand Down
6 changes: 4 additions & 2 deletions src/renderer/buffers/vk_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <core/profile.h>
#include <core/errors.h>
#include <cstdio>

#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#define VMA_VULKAN_VERSION 1002000
#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))
#define VMA_IMPLEMENTATION

#ifdef MLX_COMPILER_CLANG
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,6 @@

#include <volk.h>
#include <vma.h>
#include <cstdint>

namespace mlx
{
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/core/render_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -24,7 +24,7 @@
#include <mutex>

#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
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/images/vk_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/text_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down

0 comments on commit 39aa800

Please sign in to comment.