fixing some bad memory usage

This commit is contained in:
Kbz-8
2024-03-25 13:57:20 +01:00
parent 57c5ae5415
commit be52578d37
35 changed files with 1294 additions and 1298 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/16 20:44:29 by maldavid #+# #+# */
/* Updated: 2024/01/11 05:12:42 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:28:08 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,31 +31,13 @@ namespace mlx
public:
CmdResource() : _uuid() {}
inline void recordedInCmdBuffer() noexcept { _state = state::in_cmd_buffer; }
inline void removedFromCmdBuffer() noexcept
{
_state = state::out_cmd_buffer;
if(_destroy_required && _destroyer)
{
_destroyer();
_destroy_required = false;
}
}
inline void setDestroyer(func::function<void(void)> functor) { _destroyer = functor; }
inline void requireDestroy() noexcept
{
if(_state == state::out_cmd_buffer && _destroyer)
_destroyer();
else
_destroy_required = true;
}
inline void removedFromCmdBuffer() noexcept { _state = state::out_cmd_buffer; }
inline UUID getUUID() const noexcept { return _uuid; }
virtual ~CmdResource() = default;
private:
UUID _uuid;
state _state = state::out_cmd_buffer;
func::function<void(void)> _destroyer;
bool _destroy_required = false;
};
}

View File

@@ -6,14 +6,13 @@
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */
/* Updated: 2024/01/18 10:11:02 by maldavid ### ########.fr */
/* Updated: 2024/03/14 16:34:53 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <mlx_profile.h>
#include <core/errors.h>
#include <core/profiler.h>
#include <cstdio>
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
@@ -177,7 +176,7 @@ namespace mlx
file.close();
id++;
}
void GPUallocator::flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept
{
MLX_PROFILE_FUNCTION();
@@ -187,9 +186,11 @@ namespace mlx
void GPUallocator::destroy() noexcept
{
if(_active_images_allocations != 0)
core::error::report(e_kind::error, "Graphics allocator : some user-dependant allocations were not freed before destroying the display (%d active allocations)", _active_images_allocations);
core::error::report(e_kind::error, "Graphics allocator : some user-dependant allocations were not freed before destroying the display (%d active allocations). You may have not destroyed all the MLX resources you've created", _active_images_allocations);
else if(_active_buffers_allocations != 0)
core::error::report(e_kind::error, "Graphics allocator : some MLX-dependant allocations were not freed before destroying the display (%d active allocations), please report, this should not happen", _active_buffers_allocations);
core::error::report(e_kind::error, "Graphics allocator : some MLX-dependant allocations were not freed before destroying the display (%d active allocations). This is an error in the MLX, please report this should not happen", _active_buffers_allocations);
if(_active_images_allocations < 0 || _active_buffers_allocations < 0)
core::error::report(e_kind::warning, "Graphics allocator : the impossible happened, the MLX has freed more allocations than it has made (wtf)");
vmaDestroyAllocator(_allocator);
_active_buffers_allocations = 0;
_active_images_allocations = 0;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:14:29 by maldavid #+# #+# */
/* Updated: 2024/01/20 05:34:15 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:23:45 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,8 +17,6 @@
#include <set>
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
#include <iostream>
#include <algorithm>
namespace mlx
{
@@ -31,7 +29,7 @@ namespace mlx
Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().getFamilies();
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
std::set<std::uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
std::set<std::uint32_t> uniqueQueueFamilies = { indices.graphics_family.value(), indices.present_family.value() };
float queuePriority = 1.0f;
for(std::uint32_t queueFamily : uniqueQueueFamilies)
@@ -59,7 +57,7 @@ namespace mlx
createInfo.enabledLayerCount = 0;
VkResult res;
if((res = vkCreateDevice(_physicalDevice, &createInfo, nullptr, &_device)) != VK_SUCCESS)
if((res = vkCreateDevice(_physical_device, &createInfo, nullptr, &_device)) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create logcal device, %s", RCore::verbaliseResultVk(res));
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : created new logical device");
@@ -94,16 +92,16 @@ namespace mlx
}
if(devices_score.rbegin()->first > 0)
_physicalDevice = devices_score.rbegin()->second;
_physical_device = devices_score.rbegin()->second;
else
core::error::report(e_kind::fatal_error, "Vulkan : failed to find a suitable GPU");
#ifdef DEBUG
VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties(_physicalDevice, &props);
vkGetPhysicalDeviceProperties(_physical_device, &props);
core::error::report(e_kind::message, "Vulkan : picked a physical device, %s", props.deviceName);
#endif
Render_Core::get().getQueue().findQueueFamilies(_physicalDevice, surface); // update queue indicies to current physical device
Render_Core::get().getQueue().findQueueFamilies(_physical_device, surface); // update queue indicies to current physical device
vkDestroySurfaceKHR(Render_Core::get().getInstance().get(), surface, nullptr);
SDL_DestroyWindow(window);
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:13:42 by maldavid #+# #+# */
/* Updated: 2024/01/03 15:26:14 by maldavid ### ########.fr */
/* Updated: 2024/03/14 16:59:54 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,7 +15,6 @@
#include <mlx_profile.h>
#include <volk.h>
#include "vk_queues.h"
namespace mlx
{
@@ -28,7 +27,7 @@ namespace mlx
inline VkDevice& operator()() noexcept { return _device; }
inline VkDevice& get() noexcept { return _device; }
inline VkPhysicalDevice& getPhysicalDevice() noexcept { return _physicalDevice; }
inline VkPhysicalDevice& getPhysicalDevice() noexcept { return _physical_device; }
private:
void pickPhysicalDevice();
@@ -36,7 +35,7 @@ namespace mlx
int deviceScore(VkPhysicalDevice device, VkSurfaceKHR surface);
private:
VkPhysicalDevice _physicalDevice = VK_NULL_HANDLE;
VkPhysicalDevice _physical_device = VK_NULL_HANDLE;
VkDevice _device = VK_NULL_HANDLE;
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:02:42 by maldavid #+# #+# */
/* Updated: 2024/01/10 21:54:54 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:01:10 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,13 +29,13 @@ namespace mlx
for(const auto& queueFamily : queueFamilies)
{
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
_families->graphicsFamily = i;
_families->graphics_family = i;
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
if(presentSupport)
_families->presentFamily = i;
_families->present_family = i;
if(_families->isComplete())
return *_families;
@@ -62,8 +62,8 @@ namespace mlx
vkDestroySurfaceKHR(Render_Core::get().getInstance().get(), surface, nullptr);
SDL_DestroyWindow(window);
}
vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->graphicsFamily.value(), 0, &_graphicsQueue);
vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->presentFamily.value(), 0, &_presentQueue);
vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->graphics_family.value(), 0, &_graphics_queue);
vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->present_family.value(), 0, &_present_queue);
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : got graphics and present queues");
#endif

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:01:49 by maldavid #+# #+# */
/* Updated: 2024/01/08 23:46:23 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:00:48 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,18 +26,18 @@ namespace mlx
public:
struct QueueFamilyIndices
{
std::optional<std::uint32_t> graphicsFamily;
std::optional<std::uint32_t> presentFamily;
std::optional<std::uint32_t> graphics_family;
std::optional<std::uint32_t> present_family;
inline bool isComplete() { return graphicsFamily.has_value() && presentFamily.has_value(); }
inline bool isComplete() { return graphics_family.has_value() && present_family.has_value(); }
};
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface);
void init();
inline VkQueue& getGraphic() noexcept { return _graphicsQueue; }
inline VkQueue& getPresent() noexcept { return _presentQueue; }
inline VkQueue& getGraphic() noexcept { return _graphics_queue; }
inline VkQueue& getPresent() noexcept { return _present_queue; }
inline QueueFamilyIndices getFamilies() noexcept
{
if(_families.has_value())
@@ -47,8 +47,8 @@ namespace mlx
}
private:
VkQueue _graphicsQueue;
VkQueue _presentQueue;
VkQueue _graphics_queue;
VkQueue _present_queue;
std::optional<QueueFamilyIndices> _families;
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:01:08 by maldavid #+# #+# */
/* Updated: 2024/01/10 21:55:12 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:02:36 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,8 +22,8 @@ namespace mlx
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VkResult res;
if( (res = vkCreateSemaphore(Render_Core::get().getDevice().get(), &semaphoreInfo, nullptr, &_imageAvailableSemaphores)) != VK_SUCCESS ||
(res = vkCreateSemaphore(Render_Core::get().getDevice().get(), &semaphoreInfo, nullptr, &_renderFinishedSemaphores)) != VK_SUCCESS)
if( (res = vkCreateSemaphore(Render_Core::get().getDevice().get(), &semaphoreInfo, nullptr, &_image_available_semaphore)) != VK_SUCCESS ||
(res = vkCreateSemaphore(Render_Core::get().getDevice().get(), &semaphoreInfo, nullptr, &_render_finished_semaphore)) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a synchronization object (semaphore), %s", RCore::verbaliseResultVk(res));
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : created new semaphores");
@@ -32,10 +32,10 @@ namespace mlx
void Semaphore::destroy() noexcept
{
vkDestroySemaphore(Render_Core::get().getDevice().get(), _renderFinishedSemaphores, nullptr);
_renderFinishedSemaphores = VK_NULL_HANDLE;
vkDestroySemaphore(Render_Core::get().getDevice().get(), _imageAvailableSemaphores, nullptr);
_imageAvailableSemaphores = VK_NULL_HANDLE;
vkDestroySemaphore(Render_Core::get().getDevice().get(), _render_finished_semaphore, nullptr);
_render_finished_semaphore = VK_NULL_HANDLE;
vkDestroySemaphore(Render_Core::get().getDevice().get(), _image_available_semaphore, nullptr);
_image_available_semaphore = VK_NULL_HANDLE;
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : destroyed semaphores");
#endif

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 18:59:38 by maldavid #+# #+# */
/* Updated: 2024/01/03 15:26:39 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:01:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,7 +15,6 @@
#include <mlx_profile.h>
#include <volk.h>
#include <vector>
namespace mlx
{
@@ -25,12 +24,12 @@ namespace mlx
void init();
void destroy() noexcept;
inline VkSemaphore& getImageSemaphore() noexcept { return _imageAvailableSemaphores; }
inline VkSemaphore& getRenderImageSemaphore() noexcept { return _renderFinishedSemaphores; }
inline VkSemaphore& getImageSemaphore() noexcept { return _image_available_semaphore; }
inline VkSemaphore& getRenderImageSemaphore() noexcept { return _render_finished_semaphore; }
private:
VkSemaphore _imageAvailableSemaphores = VK_NULL_HANDLE;
VkSemaphore _renderFinishedSemaphores = VK_NULL_HANDLE;
VkSemaphore _image_available_semaphore = VK_NULL_HANDLE;
VkSemaphore _render_finished_semaphore = VK_NULL_HANDLE;
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 14:05:25 by maldavid #+# #+# */
/* Updated: 2024/01/10 21:55:54 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:03:24 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,6 @@
#include "vulkan/vulkan_core.h"
#include <core/errors.h>
#include <iostream>
#include <cstring>
#include <algorithm>
@@ -45,8 +44,8 @@ namespace mlx
core::error::report(e_kind::message, "Vulkan : enabled validation layers");
#endif
real_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(Render_Core::get().getInstance().get(), "vkSetDebugUtilsObjectNameEXT");
if(!real_vkSetDebugUtilsObjectNameEXT)
_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(Render_Core::get().getInstance().get(), "vkSetDebugUtilsObjectNameEXT");
if(!_vkSetDebugUtilsObjectNameEXT)
core::error::report(e_kind::warning, "Vulkan : failed to set up debug object names, %s", RCore::verbaliseResultVk(VK_ERROR_EXTENSION_NOT_PRESENT));
#ifdef DEBUG
else
@@ -75,7 +74,7 @@ namespace mlx
VkResult ValidationLayers::setDebugUtilsObjectNameEXT(VkObjectType object_type, std::uint64_t object_handle, const char* object_name)
{
if(!real_vkSetDebugUtilsObjectNameEXT)
if(!_vkSetDebugUtilsObjectNameEXT)
return VK_ERROR_EXTENSION_NOT_PRESENT;
VkDebugUtilsObjectNameInfoEXT name_info{};
@@ -83,7 +82,7 @@ namespace mlx
name_info.objectType = object_type;
name_info.objectHandle = object_handle;
name_info.pObjectName = object_name;
return real_vkSetDebugUtilsObjectNameEXT(Render_Core::get().getDevice().get(), &name_info);
return _vkSetDebugUtilsObjectNameEXT(Render_Core::get().getDevice().get(), &name_info);
}
void ValidationLayers::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo)
@@ -109,7 +108,7 @@ namespace mlx
VkResult ValidationLayers::createDebugUtilsMessengerEXT(const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator)
{
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(Render_Core::get().getInstance().get(), "vkCreateDebugUtilsMessengerEXT");
return func != nullptr ? func(Render_Core::get().getInstance().get(), pCreateInfo, pAllocator, &_debugMessenger) : VK_ERROR_EXTENSION_NOT_PRESENT;
return func != nullptr ? func(Render_Core::get().getInstance().get(), pCreateInfo, pAllocator, &_debug_messenger) : VK_ERROR_EXTENSION_NOT_PRESENT;
}
VKAPI_ATTR VkBool32 VKAPI_CALL ValidationLayers::debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, [[maybe_unused]] VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, [[maybe_unused]] void* pUserData)
@@ -125,7 +124,7 @@ namespace mlx
{
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(Render_Core::get().getInstance().get(), "vkDestroyDebugUtilsMessengerEXT");
if(func != nullptr)
func(Render_Core::get().getInstance().get(), _debugMessenger, pAllocator);
func(Render_Core::get().getInstance().get(), _debug_messenger, pAllocator);
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 14:04:25 by maldavid #+# #+# */
/* Updated: 2024/01/07 00:21:42 by maldavid ### ########.fr */
/* Updated: 2024/03/14 17:02:55 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,8 +39,8 @@ namespace mlx
void destroyDebugUtilsMessengerEXT(const VkAllocationCallbacks* pAllocator);
private:
VkDebugUtilsMessengerEXT _debugMessenger;
PFN_vkSetDebugUtilsObjectNameEXT real_vkSetDebugUtilsObjectNameEXT = nullptr;
VkDebugUtilsMessengerEXT _debug_messenger;
PFN_vkSetDebugUtilsObjectNameEXT _vkSetDebugUtilsObjectNameEXT = nullptr;
};
}