mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
working on buffers
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/11/08 22:40:00 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/11/09 20:02:14 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -42,15 +42,13 @@ namespace mlx
|
||||
alloc_info.usage = VMA_MEMORY_USAGE_GPU_TO_CPU;
|
||||
}
|
||||
|
||||
_size = size;
|
||||
|
||||
createBuffer(_usage, alloc_info);
|
||||
createBuffer(_usage, alloc_info, size);
|
||||
|
||||
if(type == Buffer::kind::constant || data != nullptr)
|
||||
{
|
||||
void* mapped = nullptr;
|
||||
mapMem(&mapped);
|
||||
std::memcpy(mapped, data, _size);
|
||||
std::memcpy(mapped, data, size);
|
||||
unmapMem();
|
||||
|
||||
if(type == Buffer::kind::constant)
|
||||
@@ -63,15 +61,15 @@ namespace mlx
|
||||
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
|
||||
}
|
||||
|
||||
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info)
|
||||
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size)
|
||||
{
|
||||
VkBufferCreateInfo bufferInfo{};
|
||||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
bufferInfo.size = _size;
|
||||
bufferInfo.size = size;
|
||||
bufferInfo.usage = usage;
|
||||
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
|
||||
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer);
|
||||
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer, _alloc_infos);
|
||||
}
|
||||
|
||||
void Buffer::pushToGPU() noexcept
|
||||
@@ -80,9 +78,8 @@ namespace mlx
|
||||
alloc_info.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||
|
||||
Buffer newBuffer;
|
||||
newBuffer._size = _size;
|
||||
newBuffer._usage = (this->_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
newBuffer.createBuffer(newBuffer._usage, alloc_info);
|
||||
newBuffer._usage = (_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
newBuffer.createBuffer(newBuffer._usage, alloc_info, _alloc_infos.size);
|
||||
|
||||
CmdPool cmdpool;
|
||||
cmdpool.init();
|
||||
@@ -104,7 +101,7 @@ namespace mlx
|
||||
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
||||
|
||||
VkBufferCopy copyRegion{};
|
||||
copyRegion.size = _size;
|
||||
copyRegion.size = _alloc_infos.size;
|
||||
vkCmdCopyBuffer(commandBuffer, _buffer, newBuffer._buffer, 1, ©Region);
|
||||
|
||||
vkEndCommandBuffer(commandBuffer);
|
||||
@@ -132,9 +129,9 @@ namespace mlx
|
||||
_buffer = buffer._buffer;
|
||||
buffer._buffer = temp_b;
|
||||
|
||||
VkDeviceSize temp_size = buffer._size;
|
||||
buffer._size = _size;
|
||||
_size = temp_size;
|
||||
VmaAllocationInfo temp_i = _alloc_infos;
|
||||
_alloc_infos = buffer._alloc_infos;
|
||||
buffer._alloc_infos = temp_i;
|
||||
|
||||
VkBufferUsageFlags temp_u = _usage;
|
||||
_usage = buffer._usage;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
|
||||
/* Updated: 2023/11/08 22:33:18 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/11/09 19:38:30 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -38,7 +38,8 @@ namespace mlx
|
||||
|
||||
inline VkBuffer& operator()() noexcept { return _buffer; }
|
||||
inline VkBuffer& get() noexcept { return _buffer; }
|
||||
inline VkDeviceSize getSize() const noexcept { return _size; }
|
||||
inline VkDeviceSize getSize() const noexcept { return _alloc_infos.size; }
|
||||
inline VkDeviceSize getOffset() const noexcept { return _alloc_infos.offset; }
|
||||
|
||||
protected:
|
||||
void pushToGPU() noexcept;
|
||||
@@ -46,11 +47,11 @@ namespace mlx
|
||||
|
||||
protected:
|
||||
VmaAllocation _allocation;
|
||||
VmaAllocationInfo _alloc_infos;
|
||||
VkBuffer _buffer = VK_NULL_HANDLE;
|
||||
VkDeviceSize _size = 0;
|
||||
|
||||
private:
|
||||
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info);
|
||||
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size);
|
||||
|
||||
private:
|
||||
VkBufferUsageFlags _usage = 0;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */
|
||||
/* Updated: 2023/11/08 22:41:46 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/11/09 19:58:06 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -27,10 +27,10 @@ namespace mlx
|
||||
void init() noexcept;
|
||||
void destroy() noexcept;
|
||||
|
||||
VmaAllocation createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer) noexcept;
|
||||
VmaAllocation createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, VmaAllocationInfo& allocinfo) noexcept;
|
||||
void destroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept;
|
||||
|
||||
VmaAllocation createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image) noexcept;
|
||||
VmaAllocation createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, VmaAllocationInfo& allocinfo) noexcept;
|
||||
void destroyImage(VmaAllocation allocation, VkImage image) noexcept;
|
||||
|
||||
void mapMemory(VmaAllocation allocation, void** data) noexcept;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/23 14:59:41 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/11/09 19:35:26 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -40,30 +40,10 @@ namespace mlx
|
||||
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
|
||||
if(vkCreateImage(Render_Core::get().getDevice().get(), &imageInfo, nullptr, &_image) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create an image");
|
||||
VmaAllocationCreateInfo alloc_info{};
|
||||
alloc_info.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||
|
||||
VkMemoryRequirements memRequirements;
|
||||
vkGetImageMemoryRequirements(Render_Core::get().getDevice().get(), _image, &memRequirements);
|
||||
|
||||
std::optional<uint32_t> memTypeIndex;
|
||||
for(auto prop : properties)
|
||||
{
|
||||
memTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, prop, false);
|
||||
if(memTypeIndex.has_value())
|
||||
break;
|
||||
}
|
||||
if(!memTypeIndex.has_value())
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type for an image");
|
||||
VkMemoryAllocateInfo allocInfo{};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||
allocInfo.allocationSize = memRequirements.size;
|
||||
allocInfo.memoryTypeIndex = *memTypeIndex;
|
||||
|
||||
if(vkAllocateMemory(Render_Core::get().getDevice().get(), &allocInfo, nullptr, &_memory) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate memory for an image");
|
||||
|
||||
vkBindImageMemory(Render_Core::get().getDevice().get(), _image, _memory, 0);
|
||||
_allocation = Render_Core::get().getAllocator().createImage(&imageInfo, &alloc_info, _image);
|
||||
|
||||
_pool.init();
|
||||
}
|
||||
@@ -215,8 +195,7 @@ namespace mlx
|
||||
if(_image_view != VK_NULL_HANDLE)
|
||||
vkDestroyImageView(Render_Core::get().getDevice().get(), _image_view, nullptr);
|
||||
|
||||
vkFreeMemory(Render_Core::get().getDevice().get(), _memory, nullptr);
|
||||
vkDestroyImage(Render_Core::get().getDevice().get(), _image, nullptr);
|
||||
Render_Core::get().getAllocator().destroyImage(_allocation, _image);
|
||||
if(_transfer_cmd.isInit())
|
||||
_transfer_cmd.destroy();
|
||||
_pool.destroy();
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/23 14:17:11 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/11/09 19:29:54 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,7 +37,6 @@ namespace mlx
|
||||
|
||||
inline VkImage get() noexcept { return _image; }
|
||||
inline VkImage operator()() noexcept { return _image; }
|
||||
inline VkDeviceMemory getDeviceMemory() noexcept { return _memory; }
|
||||
inline VkImageView getImageView() noexcept { return _image_view; }
|
||||
inline VkFormat getFormat() noexcept { return _format; }
|
||||
inline VkImageTiling getTiling() noexcept { return _tiling; }
|
||||
@@ -50,8 +49,8 @@ namespace mlx
|
||||
private:
|
||||
CmdBuffer _transfer_cmd;
|
||||
CmdPool _pool;
|
||||
VmaAllocation _allocation;
|
||||
VkImage _image = VK_NULL_HANDLE;
|
||||
VkDeviceMemory _memory = VK_NULL_HANDLE;
|
||||
VkImageView _image_view = VK_NULL_HANDLE;
|
||||
VkSampler _sampler = VK_NULL_HANDLE;
|
||||
VkFormat _format;
|
||||
|
||||
Reference in New Issue
Block a user