fixing valgrind issues, injecting garbage collector inside vulkan's funtions

This commit is contained in:
Kbz-8
2024-12-13 02:51:52 +01:00
parent 5a04ad7781
commit 5d4d97e05d
8 changed files with 165 additions and 160 deletions

View File

@@ -9,6 +9,7 @@ namespace mlx
MemManager();
static void* Malloc(std::size_t size);
static void* AlignedMalloc(std::size_t alignment, std::size_t size);
static void* Calloc(std::size_t n, std::size_t size);
static void* Realloc(void* ptr, std::size_t size);
static void Free(void* ptr);

View File

@@ -24,6 +24,8 @@ namespace mlx
void FontRegistry::Reset()
{
for(auto& font: m_fonts_registry)
font->Destroy();
m_fonts_registry.clear();
}
}

View File

@@ -67,10 +67,14 @@
#include <math.h> // sincos
#endif
#include <Core/Memory.h>
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#define VMA_VULKAN_VERSION 1000000
#define VMA_ASSERT(expr) ((void)0) // Because why not
#define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (mlx::MemManager::AlignedMalloc(alignment, size))
#define VMA_SYSTEM_ALIGNED_FREE(ptr) (mlx::MemManager::Free(ptr))
#define VMA_ASSERT_LEAK(expr) ((void)0) // Because why not
#ifdef MLX_COMPILER_CLANG
#pragma clang diagnostic push

View File

@@ -8,7 +8,7 @@ namespace mlx
public:
GPUAllocator() = default;
void Init() noexcept;
void Init(const VkAllocationCallbacks* callbacks) noexcept;
void Destroy() noexcept;
VmaAllocation CreateBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, const char* name = nullptr) noexcept;

View File

@@ -19,6 +19,14 @@ namespace mlx
return ptr;
}
void* MemManager::AlignedMalloc(std::size_t alignment, std::size_t size)
{
void* ptr = std::aligned_alloc(alignment, size);
if(ptr != nullptr)
s_blocks.push_back(ptr);
return ptr;
}
void* MemManager::Calloc(std::size_t n, std::size_t size)
{
void* ptr = std::calloc(n, size);

View File

@@ -1,3 +1,4 @@
#include "vulkan/vulkan_core.h"
#include <PreCompiled.h>
#define VMA_IMPLEMENTATION
#ifdef MLX_COMPILER_CLANG
@@ -22,7 +23,7 @@
namespace mlx
{
void GPUAllocator::Init() noexcept
void GPUAllocator::Init(const VkAllocationCallbacks* callbacks) noexcept
{
MLX_PROFILE_FUNCTION();
VmaVulkanFunctions vma_vulkan_func{};
@@ -49,6 +50,7 @@ namespace mlx
allocator_create_info.physicalDevice = RenderCore::Get().GetPhysicalDevice();
allocator_create_info.device = RenderCore::Get().GetDevice();
allocator_create_info.instance = RenderCore::Get().GetInstance();
allocator_create_info.pAllocationCallbacks = callbacks;
allocator_create_info.pVulkanFunctions = &vma_vulkan_func;
kvfCheckVk(vmaCreateAllocator(&allocator_create_info, &m_allocator));

View File

@@ -31,6 +31,7 @@
#include <Platform/Window.h>
#include <Maths/Mat4.h>
namespace mlx
{
static std::unique_ptr<VulkanLoader> loader;
@@ -53,6 +54,21 @@ namespace mlx
std::cout << std::endl;
}
void* VulkanAllocationFunction(void*, std::size_t size, std::size_t alignment, VkSystemAllocationScope)
{
return MemManager::AlignedMalloc(alignment, size);
}
void* VulkanReallocationFunction(void*, void* ptr, std::size_t size, std::size_t, VkSystemAllocationScope)
{
return MemManager::Realloc(ptr, size);
}
void VulkanFreeFunction(void*, void* ptr)
{
MemManager::Free(ptr);
}
RenderCore* RenderCore::s_instance = nullptr;
RenderCore::RenderCore()
@@ -102,7 +118,17 @@ namespace mlx
vkDestroySurfaceKHR(m_instance, surface, nullptr);
m_allocator.Init();
VkAllocationCallbacks callbacks;
callbacks.pUserData = nullptr;
callbacks.pfnAllocation = VulkanAllocationFunction;
callbacks.pfnReallocation = VulkanReallocationFunction;
callbacks.pfnFree = VulkanFreeFunction;
callbacks.pfnInternalAllocation = nullptr;
callbacks.pfnInternalFree = nullptr;
kvfSetAllocationCallbacks(m_device, &callbacks);
m_allocator.Init(&callbacks);
}
#undef MLX_LOAD_FUNCTION