Skip to content

Commit e6ed390

Browse files
committed
working on buffers
1 parent 6cd3422 commit e6ed390

File tree

5 files changed

+27
-51
lines changed

5 files changed

+27
-51
lines changed

src/renderer/buffers/vk_buffer.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
9-
/* Updated: 2023/11/08 22:40:00 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/09 20:02:14 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -42,15 +42,13 @@ namespace mlx
4242
alloc_info.usage = VMA_MEMORY_USAGE_GPU_TO_CPU;
4343
}
4444

45-
_size = size;
46-
47-
createBuffer(_usage, alloc_info);
45+
createBuffer(_usage, alloc_info, size);
4846

4947
if(type == Buffer::kind::constant || data != nullptr)
5048
{
5149
void* mapped = nullptr;
5250
mapMem(&mapped);
53-
std::memcpy(mapped, data, _size);
51+
std::memcpy(mapped, data, size);
5452
unmapMem();
5553

5654
if(type == Buffer::kind::constant)
@@ -63,15 +61,15 @@ namespace mlx
6361
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
6462
}
6563

66-
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info)
64+
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size)
6765
{
6866
VkBufferCreateInfo bufferInfo{};
6967
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
70-
bufferInfo.size = _size;
68+
bufferInfo.size = size;
7169
bufferInfo.usage = usage;
7270
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
7371

74-
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer);
72+
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer, _alloc_infos);
7573
}
7674

7775
void Buffer::pushToGPU() noexcept
@@ -80,9 +78,8 @@ namespace mlx
8078
alloc_info.usage = VMA_MEMORY_USAGE_GPU_ONLY;
8179

8280
Buffer newBuffer;
83-
newBuffer._size = _size;
84-
newBuffer._usage = (this->_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
85-
newBuffer.createBuffer(newBuffer._usage, alloc_info);
81+
newBuffer._usage = (_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
82+
newBuffer.createBuffer(newBuffer._usage, alloc_info, _alloc_infos.size);
8683

8784
CmdPool cmdpool;
8885
cmdpool.init();
@@ -104,7 +101,7 @@ namespace mlx
104101
vkBeginCommandBuffer(commandBuffer, &beginInfo);
105102

106103
VkBufferCopy copyRegion{};
107-
copyRegion.size = _size;
104+
copyRegion.size = _alloc_infos.size;
108105
vkCmdCopyBuffer(commandBuffer, _buffer, newBuffer._buffer, 1, &copyRegion);
109106

110107
vkEndCommandBuffer(commandBuffer);
@@ -132,9 +129,9 @@ namespace mlx
132129
_buffer = buffer._buffer;
133130
buffer._buffer = temp_b;
134131

135-
VkDeviceSize temp_size = buffer._size;
136-
buffer._size = _size;
137-
_size = temp_size;
132+
VmaAllocationInfo temp_i = _alloc_infos;
133+
_alloc_infos = buffer._alloc_infos;
134+
buffer._alloc_infos = temp_i;
138135

139136
VkBufferUsageFlags temp_u = _usage;
140137
_usage = buffer._usage;

src/renderer/buffers/vk_buffer.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
9-
/* Updated: 2023/11/08 22:33:18 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/09 19:38:30 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -38,19 +38,20 @@ namespace mlx
3838

3939
inline VkBuffer& operator()() noexcept { return _buffer; }
4040
inline VkBuffer& get() noexcept { return _buffer; }
41-
inline VkDeviceSize getSize() const noexcept { return _size; }
41+
inline VkDeviceSize getSize() const noexcept { return _alloc_infos.size; }
42+
inline VkDeviceSize getOffset() const noexcept { return _alloc_infos.offset; }
4243

4344
protected:
4445
void pushToGPU() noexcept;
4546
void swap(Buffer& buffer) noexcept;
4647

4748
protected:
4849
VmaAllocation _allocation;
50+
VmaAllocationInfo _alloc_infos;
4951
VkBuffer _buffer = VK_NULL_HANDLE;
50-
VkDeviceSize _size = 0;
5152

5253
private:
53-
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info);
54+
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size);
5455

5556
private:
5657
VkBufferUsageFlags _usage = 0;

src/renderer/core/memory.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */
9-
/* Updated: 2023/11/08 22:41:46 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/09 19:58:06 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -27,10 +27,10 @@ namespace mlx
2727
void init() noexcept;
2828
void destroy() noexcept;
2929

30-
VmaAllocation createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer) noexcept;
30+
VmaAllocation createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, VmaAllocationInfo& allocinfo) noexcept;
3131
void destroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept;
3232

33-
VmaAllocation createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image) noexcept;
33+
VmaAllocation createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, VmaAllocationInfo& allocinfo) noexcept;
3434
void destroyImage(VmaAllocation allocation, VkImage image) noexcept;
3535

3636
void mapMemory(VmaAllocation allocation, void** data) noexcept;

src/renderer/images/vk_image.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */
9-
/* Updated: 2023/04/23 14:59:41 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/09 19:35:26 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -40,30 +40,10 @@ namespace mlx
4040
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
4141
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
4242

43-
if(vkCreateImage(Render_Core::get().getDevice().get(), &imageInfo, nullptr, &_image) != VK_SUCCESS)
44-
core::error::report(e_kind::fatal_error, "Vulkan : failed to create an image");
45-
46-
VkMemoryRequirements memRequirements;
47-
vkGetImageMemoryRequirements(Render_Core::get().getDevice().get(), _image, &memRequirements);
48-
49-
std::optional<uint32_t> memTypeIndex;
50-
for(auto prop : properties)
51-
{
52-
memTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, prop, false);
53-
if(memTypeIndex.has_value())
54-
break;
55-
}
56-
if(!memTypeIndex.has_value())
57-
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type for an image");
58-
VkMemoryAllocateInfo allocInfo{};
59-
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
60-
allocInfo.allocationSize = memRequirements.size;
61-
allocInfo.memoryTypeIndex = *memTypeIndex;
62-
63-
if(vkAllocateMemory(Render_Core::get().getDevice().get(), &allocInfo, nullptr, &_memory) != VK_SUCCESS)
64-
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate memory for an image");
43+
VmaAllocationCreateInfo alloc_info{};
44+
alloc_info.usage = VMA_MEMORY_USAGE_GPU_ONLY;
6545

66-
vkBindImageMemory(Render_Core::get().getDevice().get(), _image, _memory, 0);
46+
_allocation = Render_Core::get().getAllocator().createImage(&imageInfo, &alloc_info, _image);
6747

6848
_pool.init();
6949
}
@@ -215,8 +195,7 @@ namespace mlx
215195
if(_image_view != VK_NULL_HANDLE)
216196
vkDestroyImageView(Render_Core::get().getDevice().get(), _image_view, nullptr);
217197

218-
vkFreeMemory(Render_Core::get().getDevice().get(), _memory, nullptr);
219-
vkDestroyImage(Render_Core::get().getDevice().get(), _image, nullptr);
198+
Render_Core::get().getAllocator().destroyImage(_allocation, _image);
220199
if(_transfer_cmd.isInit())
221200
_transfer_cmd.destroy();
222201
_pool.destroy();

src/renderer/images/vk_image.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
9-
/* Updated: 2023/04/23 14:17:11 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/09 19:29:54 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -37,7 +37,6 @@ namespace mlx
3737

3838
inline VkImage get() noexcept { return _image; }
3939
inline VkImage operator()() noexcept { return _image; }
40-
inline VkDeviceMemory getDeviceMemory() noexcept { return _memory; }
4140
inline VkImageView getImageView() noexcept { return _image_view; }
4241
inline VkFormat getFormat() noexcept { return _format; }
4342
inline VkImageTiling getTiling() noexcept { return _tiling; }
@@ -50,8 +49,8 @@ namespace mlx
5049
private:
5150
CmdBuffer _transfer_cmd;
5251
CmdPool _pool;
52+
VmaAllocation _allocation;
5353
VkImage _image = VK_NULL_HANDLE;
54-
VkDeviceMemory _memory = VK_NULL_HANDLE;
5554
VkImageView _image_view = VK_NULL_HANDLE;
5655
VkSampler _sampler = VK_NULL_HANDLE;
5756
VkFormat _format;

0 commit comments

Comments
 (0)