mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-12 07:03:34 +00:00
working on better command buffers management
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/16 17:10:17 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 17:35:03 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -22,6 +22,15 @@ namespace mlx
|
||||
{
|
||||
void Buffer::create(Buffer::kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data)
|
||||
{
|
||||
CmdResource::setDestroyer([this]()
|
||||
{
|
||||
if(_is_mapped)
|
||||
unmapMem();
|
||||
if(_buffer != VK_NULL_HANDLE)
|
||||
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
|
||||
_buffer = VK_NULL_HANDLE;
|
||||
});
|
||||
|
||||
_usage = usage;
|
||||
if(type == Buffer::kind::constant || type == Buffer::kind::dynamic_device_local)
|
||||
{
|
||||
@@ -52,11 +61,7 @@ namespace mlx
|
||||
|
||||
void Buffer::destroy() noexcept
|
||||
{
|
||||
if(_is_mapped)
|
||||
unmapMem();
|
||||
if(_buffer != VK_NULL_HANDLE)
|
||||
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
|
||||
_buffer = VK_NULL_HANDLE;
|
||||
CmdResource::requireDestroy();
|
||||
}
|
||||
|
||||
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, [[maybe_unused]] const char* name)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/12 21:12:44 by kbz_8 ### ########.fr */
|
||||
/* Updated: 2023/12/17 17:35:35 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
#include <volk.h>
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <mlx_profile.h>
|
||||
#include <renderer/core/cmd_resource.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class Buffer
|
||||
class Buffer : public CmdResource
|
||||
{
|
||||
public:
|
||||
enum class kind { dynamic, dynamic_device_local, uniform, constant };
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 17:50:52 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/02 17:51:46 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 20:10:45 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace mlx
|
||||
{
|
||||
_cmd_pool.init();
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
_cmd_buffers[i].init(this);
|
||||
_cmd_buffers[i].init(CmdBuffer::kind::long_time, this);
|
||||
}
|
||||
|
||||
void CmdManager::beginRecord(int active_image_index)
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/15 19:57:49 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/16 18:46:26 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 20:10:25 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <algorithm>
|
||||
#include <renderer/command/single_time_cmd_manager.h>
|
||||
#include <renderer/command/vk_cmd_buffer.h>
|
||||
#include <renderer/core/render_core.h>
|
||||
|
||||
namespace mlx
|
||||
@@ -24,7 +25,7 @@ namespace mlx
|
||||
for(int i = 0; i < MIN_POOL_SIZE; i++)
|
||||
{
|
||||
_buffers.emplace_back();
|
||||
_buffers.back().init(&_pool);
|
||||
_buffers.back().init(CmdBuffer::kind::single_time, &_pool);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +39,7 @@ namespace mlx
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
_buffers.emplace_back().init(&_pool);
|
||||
_buffers.emplace_back().init(CmdBuffer::kind::single_time, &_pool);
|
||||
return _buffers.back();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/15 18:25:57 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/16 18:09:56 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 17:36:18 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
#include <vector>
|
||||
|
||||
#include <renderer/command/vk_cmd_pool.h>
|
||||
#include <renderer/command/vk_cmd_buffer.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class CmdBuffer;
|
||||
|
||||
class SingleTimeCmdManager
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:26:06 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/16 18:51:03 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 20:01:46 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,16 +14,18 @@
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <renderer/command/cmd_manager.h>
|
||||
#include <renderer/core/vk_semaphore.h>
|
||||
#include <renderer/buffers/vk_buffer.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void CmdBuffer::init(CmdManager* manager)
|
||||
void CmdBuffer::init(kind type, CmdManager* manager)
|
||||
{
|
||||
init(&manager->getCmdPool());
|
||||
init(type, &manager->getCmdPool());
|
||||
}
|
||||
|
||||
void CmdBuffer::init(CmdPool* pool)
|
||||
void CmdBuffer::init(kind type, CmdPool* pool)
|
||||
{
|
||||
_type = type;
|
||||
_pool = pool;
|
||||
|
||||
VkCommandBufferAllocateInfo allocInfo{};
|
||||
@@ -58,6 +60,18 @@ namespace mlx
|
||||
_state = state::recording;
|
||||
}
|
||||
|
||||
void CmdBuffer::bindVertexBuffer(Buffer& buffer) const noexcept
|
||||
{
|
||||
if(!isRecording())
|
||||
{
|
||||
core::error::report(e_kind::warning, "Vulkan : trying to bind a vertex buffer to a non recording command buffer");
|
||||
return;
|
||||
}
|
||||
VkDeviceSize offset[] = { buffer.getOffset() };
|
||||
vkCmdBindVertexBuffers(_cmd_buffer, 0, 1, &buffer.get(), offset);
|
||||
buffer.recordedInCmdBuffer();
|
||||
}
|
||||
|
||||
void CmdBuffer::endRecord()
|
||||
{
|
||||
if(!isInit())
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/16 18:44:48 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 20:01:55 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class Buffer;
|
||||
class Image;
|
||||
|
||||
class CmdBuffer
|
||||
{
|
||||
public:
|
||||
@@ -31,9 +34,15 @@ namespace mlx
|
||||
submitted, // buffer has been submitted
|
||||
};
|
||||
|
||||
enum class kind
|
||||
{
|
||||
single_time = 0,
|
||||
long_time
|
||||
};
|
||||
|
||||
public:
|
||||
void init(class CmdManager* manager);
|
||||
void init(class CmdPool* pool);
|
||||
void init(kind type, class CmdManager* manager);
|
||||
void init(kind type, class CmdPool* pool);
|
||||
void destroy() noexcept;
|
||||
|
||||
void beginRecord(VkCommandBufferUsageFlags usage = 0);
|
||||
@@ -43,6 +52,12 @@ namespace mlx
|
||||
inline void reset() noexcept { vkResetCommandBuffer(_cmd_buffer, 0); }
|
||||
void endRecord();
|
||||
|
||||
void bindVertexBuffer(Buffer& buffer) const noexcept;
|
||||
void bindIndexBuffer(Buffer& buffer) const noexcept;
|
||||
void copyBuffer(Buffer& dst, Buffer& src) const noexcept;
|
||||
void copyBufferToImage(Buffer& buffer, Image& image) const noexcept;
|
||||
void copyImagetoBuffer(Image& image, Buffer& buffer) const noexcept;
|
||||
|
||||
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; }
|
||||
@@ -58,6 +73,7 @@ namespace mlx
|
||||
VkCommandBuffer _cmd_buffer = VK_NULL_HANDLE;
|
||||
class CmdPool* _pool = nullptr;
|
||||
state _state = state::uninit;
|
||||
kind _type;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
62
src/renderer/core/cmd_resource.h
git.filemode.normal_file
62
src/renderer/core/cmd_resource.h
git.filemode.normal_file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cmd_resource.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/16 20:44:29 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/17 17:10:03 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_COMMAND_RESOURCE__
|
||||
#define __MLX_COMMAND_RESOURCE__
|
||||
|
||||
#include <function.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class CmdResource
|
||||
{
|
||||
friend class SingleTimeCmdManager;
|
||||
public:
|
||||
enum class state
|
||||
{
|
||||
in_cmd_buffer = 0,
|
||||
out_cmd_buffer,
|
||||
};
|
||||
|
||||
public:
|
||||
CmdResource() = default;
|
||||
inline void recordedInCmdBuffer() noexcept { _state = state::in_cmd_buffer; }
|
||||
inline void removedFromCmdBuffer() noexcept
|
||||
{
|
||||
_state = state::out_cmd_buffer;
|
||||
if(_destroy_required && _destroy_required)
|
||||
{
|
||||
_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;
|
||||
}
|
||||
virtual ~CmdResource() = default;
|
||||
|
||||
private:
|
||||
void realDestroy();
|
||||
|
||||
private:
|
||||
state _state = state::out_cmd_buffer;
|
||||
func::function<void(void)> _destroyer;
|
||||
bool _destroy_required = false;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/14 14:37:08 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 17:23:56 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace mlx
|
||||
void setPixel(int x, int y, uint32_t color) noexcept;
|
||||
int getPixel(int x, int y) noexcept;
|
||||
|
||||
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
|
||||
inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
|
||||
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
|
||||
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); _has_been_updated = true; }
|
||||
inline bool hasBeenUpdated() const noexcept { return _has_been_updated; }
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/16 17:10:33 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 17:16:15 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -138,6 +138,16 @@ namespace mlx
|
||||
|
||||
void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, const char* name, bool dedicated_memory)
|
||||
{
|
||||
CmdResource::setDestroyer([this]()
|
||||
{
|
||||
this->destroySampler();
|
||||
this->destroyImageView();
|
||||
|
||||
if(_image != VK_NULL_HANDLE)
|
||||
Render_Core::get().getAllocator().destroyImage(_allocation, _image);
|
||||
_image = VK_NULL_HANDLE;
|
||||
});
|
||||
|
||||
_width = width;
|
||||
_height = height;
|
||||
_format = format;
|
||||
@@ -367,12 +377,7 @@ namespace mlx
|
||||
|
||||
void Image::destroy() noexcept
|
||||
{
|
||||
destroySampler();
|
||||
destroyImageView();
|
||||
|
||||
if(_image != VK_NULL_HANDLE)
|
||||
Render_Core::get().getAllocator().destroyImage(_allocation, _image);
|
||||
_image = VK_NULL_HANDLE;
|
||||
CmdResource::requireDestroy();
|
||||
}
|
||||
|
||||
uint32_t formatSize(VkFormat format)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/15 21:44:30 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/12/17 17:13:23 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include <vma.h>
|
||||
#include <renderer/core/cmd_resource.h>
|
||||
#include <renderer/command/vk_cmd_buffer.h>
|
||||
#include <renderer/command/vk_cmd_pool.h>
|
||||
#include <mlx_profile.h>
|
||||
@@ -25,7 +26,7 @@ namespace mlx
|
||||
{
|
||||
uint32_t formatSize(VkFormat format);
|
||||
|
||||
class Image
|
||||
class Image : public CmdResource
|
||||
{
|
||||
friend class SwapChain;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user