Skip to content

Commit

Permalink
new branch for error management
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Dec 16, 2023
1 parent 68d2c63 commit 80e938c
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 38 deletions.
3 changes: 1 addition & 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/12/15 21:48:02 by maldavid ### ########.fr */
/* Updated: 2023/12/16 17:10:17 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -98,7 +98,6 @@ namespace mlx

// TODO, use global cmd buffer pool to manage resources
CmdBuffer& cmd = Render_Core::get().getSingleTimeCmdBuffer();
cmd.reset();
cmd.beginRecord();

VkBufferCopy copyRegion{};
Expand Down
26 changes: 16 additions & 10 deletions src/renderer/command/single_time_cmd_manager.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/12/15 19:57:49 by maldavid #+# #+# */
/* Updated: 2023/12/15 20:21:54 by maldavid ### ########.fr */
/* Updated: 2023/12/16 18:46:26 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,26 +22,32 @@ namespace mlx
{
_pool.init();
for(int i = 0; i < MIN_POOL_SIZE; i++)
_buffers.emplace_back().init(&_pool);
}

void SingleTimeCmdManager::destroy() noexcept
{
std::for_each(_buffers.begin(), _buffers.end(), [](CmdBuffer& buf)
{
buf.destroy();
});
_pool.destroy();
_buffers.emplace_back();
_buffers.back().init(&_pool);
}
}

CmdBuffer& SingleTimeCmdManager::getCmdBuffer() noexcept
{
for(CmdBuffer& buf : _buffers)
{
if(buf.isReadyToBeUsed())
{
buf.reset();
return buf;
}
}
_buffers.emplace_back().init(&_pool);
return _buffers.back();
}

void SingleTimeCmdManager::destroy() noexcept
{
std::for_each(_buffers.begin(), _buffers.end(), [](CmdBuffer& buf)
{
buf.destroy();
});
_pool.destroy();
}
}
4 changes: 3 additions & 1 deletion src/renderer/command/single_time_cmd_manager.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/12/15 18:25:57 by maldavid #+# #+# */
/* Updated: 2023/12/15 19:59:40 by maldavid ### ########.fr */
/* Updated: 2023/12/16 18:09:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,6 +35,8 @@ namespace mlx

inline static constexpr const uint8_t MIN_POOL_SIZE = 8;

private:

private:
std::vector<CmdBuffer> _buffers;
CmdPool _pool;
Expand Down
32 changes: 25 additions & 7 deletions src/renderer/command/vk_cmd_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/06 18:26:06 by maldavid #+# #+# */
/* Updated: 2023/12/15 21:54:11 by maldavid ### ########.fr */
/* Updated: 2023/12/16 18:51:03 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,11 +39,14 @@ namespace mlx
#endif

_fence.init();
_state = state::idle;
}

void CmdBuffer::beginRecord(VkCommandBufferUsageFlags usage)
{
if(_is_recording)
if(!isInit())
core::error::report(e_kind::fatal_error, "Vulkan : begenning record on un uninit command buffer");
if(_state == state::recording)
return;

VkCommandBufferBeginInfo beginInfo{};
Expand All @@ -52,28 +55,41 @@ namespace mlx
if(vkBeginCommandBuffer(_cmd_buffer, &beginInfo) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to begin recording command buffer");

_is_recording = true;
_state = state::recording;
}

void CmdBuffer::endRecord()
{
if(!_is_recording)
if(!isInit())
core::error::report(e_kind::fatal_error, "Vulkan : ending record on un uninit command buffer");
if(_state != state::recording)
return;
if(vkEndCommandBuffer(_cmd_buffer) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to end recording command buffer");

_is_recording = false;
_state = state::idle;
}

void CmdBuffer::submitIdle() noexcept
{
auto device = Render_Core::get().getDevice().get();

VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &_cmd_buffer;

vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, _fence.get());
waitForExecution();
VkFenceCreateInfo fenceCreateInfo = {};
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;

VkFence fence;
vkCreateFence(device, &fenceCreateInfo, nullptr, &fence);
vkResetFences(device, 1, &fence);
vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, fence);
vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX);
vkDestroyFence(device, fence, nullptr);
_state = state::submitted;
_state = state::ready;
}

void CmdBuffer::submit(Semaphore& semaphores) noexcept
Expand All @@ -94,11 +110,13 @@ namespace mlx

if(vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, _fence.get()) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to submit draw command buffer");
_state = state::submitted;
}

void CmdBuffer::destroy() noexcept
{
_fence.destroy();
_cmd_buffer = VK_NULL_HANDLE;
_state = state::uninit;
}
}
24 changes: 18 additions & 6 deletions src/renderer/command/vk_cmd_buffer.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: 2022/10/06 18:25:42 by maldavid #+# #+# */
/* Updated: 2023/12/15 20:19:42 by maldavid ### ########.fr */
/* Updated: 2023/12/16 18:44:48 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,6 +21,16 @@ namespace mlx
{
class CmdBuffer
{
public:
enum class state
{
uninit = 0, // buffer not initialized or destroyed
ready, // buffer ready to be used after having been submitted
idle, // buffer has recorded informations but has not been submitted
recording, // buffer is currently recording
submitted, // buffer has been submitted
};

public:
void init(class CmdManager* manager);
void init(class CmdPool* pool);
Expand All @@ -29,13 +39,15 @@ namespace mlx
void beginRecord(VkCommandBufferUsageFlags usage = 0);
void submit(class Semaphore& semaphores) noexcept;
void submitIdle() noexcept;
inline void waitForExecution() noexcept { _fence.waitAndReset(); }
inline void waitForExecution() noexcept { _fence.waitAndReset(); _state = state::ready; }
inline void reset() noexcept { vkResetCommandBuffer(_cmd_buffer, 0); }
inline bool isReadyToBeUsed() const noexcept { return _fence.isReady(); }
void endRecord();

inline bool isRecording() const noexcept { return _is_recording; }
inline bool isInit() const noexcept { return _cmd_buffer != VK_NULL_HANDLE; }
inline bool isInit() const noexcept { return _state != state::uninit; }
inline bool isReadyToBeUsed() const noexcept { return _state == state::ready; }
inline bool isRecording() const noexcept { return _state == state::recording; }
inline bool hasBeenSubmitted() const noexcept { return _state == state::submitted; }
inline state getCurrentState() const noexcept { return _state; }

inline VkCommandBuffer& operator()() noexcept { return _cmd_buffer; }
inline VkCommandBuffer& get() noexcept { return _cmd_buffer; }
Expand All @@ -45,7 +57,7 @@ namespace mlx
Fence _fence;
VkCommandBuffer _cmd_buffer = VK_NULL_HANDLE;
class CmdPool* _pool = nullptr;
bool _is_recording = false;
state _state = state::uninit;
};
}

Expand Down
14 changes: 12 additions & 2 deletions src/renderer/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */
/* Updated: 2023/12/10 22:44:55 by kbz_8 ### ########.fr */
/* Updated: 2023/12/16 14:47:53 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,7 +17,11 @@
#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))
#ifdef DEBUG
#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))
#else
#define VMA_ASSERT(expr) ((void)0)
#endif
#define VMA_IMPLEMENTATION

#ifdef MLX_COMPILER_CLANG
Expand Down Expand Up @@ -93,6 +97,7 @@ namespace mlx
#ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new buffer");
#endif
_active_allocations++;
return allocation;
}

Expand All @@ -103,6 +108,7 @@ namespace mlx
#ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : destroyed buffer");
#endif
_active_allocations--;
}

VmaAllocation GPUallocator::createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, const char* name) noexcept
Expand All @@ -115,6 +121,7 @@ namespace mlx
#ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new image");
#endif
_active_allocations++;
return allocation;
}

Expand All @@ -125,6 +132,7 @@ namespace mlx
#ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : destroyed image");
#endif
_active_allocations--;
}

void GPUallocator::mapMemory(VmaAllocation allocation, void** data) noexcept
Expand Down Expand Up @@ -164,6 +172,8 @@ namespace mlx

void GPUallocator::destroy() noexcept
{
if(_active_allocations != 0)
core::error::report(e_kind::error, "Graphics allocator : some allocations were not freed before destroying the display (%d active allocations)", _active_allocations);
vmaDestroyAllocator(_allocator);
}
}
3 changes: 2 additions & 1 deletion 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/12/08 19:07:34 by kbz_8 ### ########.fr */
/* Updated: 2023/12/16 14:43:31 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,6 +44,7 @@ namespace mlx

private:
VmaAllocator _allocator;
uint32_t _active_allocations = 0;
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/core/vk_fence.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/04/02 17:53:06 by maldavid #+# #+# */
/* Updated: 2023/12/15 20:31:29 by maldavid ### ########.fr */
/* Updated: 2023/12/16 18:47:36 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,7 +23,7 @@ namespace mlx

VkResult res;
if((res = vkCreateFence(Render_Core::get().getDevice().get(), &fenceInfo, nullptr, &_fence)) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create CPU synchronization object, %s", RCore::verbaliseResultVk(res));
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a synchronization object (fence), %s", RCore::verbaliseResultVk(res));
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : created new fence");
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/core/vk_fence.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 17:52:09 by maldavid #+# #+# */
/* Updated: 2023/12/15 20:31:25 by maldavid ### ########.fr */
/* Updated: 2023/12/16 17:27:28 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/core/vk_semaphore.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 19:01:08 by maldavid #+# #+# */
/* Updated: 2023/12/12 15:51:37 by kbz_8 ### ########.fr */
/* Updated: 2023/12/16 18:47:29 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,7 +24,7 @@ namespace mlx
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)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create GPU synchronization object, %s", RCore::verbaliseResultVk(res));
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 semaphore");
#endif
Expand Down
5 changes: 1 addition & 4 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/12/15 21:46:33 by maldavid ### ########.fr */
/* Updated: 2023/12/16 17:10:33 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -208,7 +208,6 @@ namespace mlx
void Image::copyFromBuffer(Buffer& buffer)
{
CmdBuffer& cmd = Render_Core::get().getSingleTimeCmdBuffer();
cmd.reset();
cmd.beginRecord();

VkImageMemoryBarrier copy_barrier{};
Expand Down Expand Up @@ -258,7 +257,6 @@ namespace mlx
void Image::copyToBuffer(Buffer& buffer)
{
CmdBuffer& cmd = Render_Core::get().getSingleTimeCmdBuffer();
cmd.reset();
cmd.beginRecord();

VkImageMemoryBarrier copy_barrier{};
Expand Down Expand Up @@ -311,7 +309,6 @@ namespace mlx
return;

CmdBuffer& cmd = Render_Core::get().getSingleTimeCmdBuffer();
cmd.reset();
cmd.beginRecord();

VkImageMemoryBarrier barrier{};
Expand Down

0 comments on commit 80e938c

Please sign in to comment.