66
77namespace sh ::render::impl
88{
9- VulkanBuffer::VulkanBuffer (VkDevice device, VkPhysicalDevice gpu) :
10- device (device), gpu(gpu),
9+ VulkanBuffer::VulkanBuffer (VkDevice device, VkPhysicalDevice gpu, VmaAllocator allocator ) :
10+ device (device), gpu(gpu), allocator(allocator),
1111 buffer (nullptr ), bufferMem(nullptr ), data(nullptr ),
1212 bufferInfo (),
1313 persistentMapping (false )
@@ -16,13 +16,14 @@ namespace sh::render::impl
1616 }
1717
1818 VulkanBuffer::VulkanBuffer (VulkanBuffer&& other) noexcept :
19- device (other.device), gpu(other.gpu),
19+ device (other.device), gpu(other.gpu), allocator(other.allocator),
2020 buffer (other.buffer), bufferMem(other.bufferMem), data(other.data),
2121 bufferInfo (other.bufferInfo),
2222 persistentMapping (other.persistentMapping)
2323 {
2424 other.device = nullptr ;
2525 other.gpu = nullptr ;
26+ other.allocator = nullptr ;
2627 other.buffer = nullptr ;
2728 other.bufferMem = nullptr ;
2829 other.data = nullptr ;
@@ -37,15 +38,17 @@ namespace sh::render::impl
3738 {
3839 if (buffer)
3940 {
40- vkDestroyBuffer (device, buffer, nullptr );
41+ vmaDestroyBuffer (allocator, buffer, bufferMem);
42+ // vkDestroyBuffer(device, buffer, nullptr);
4143 buffer = nullptr ;
44+ bufferMem = nullptr ;
4245 }
4346
44- if (bufferMem)
47+ /* if (bufferMem)
4548 {
4649 vkFreeMemory(device, bufferMem, nullptr);
4750 bufferMem = nullptr;
48- }
51+ }*/
4952 }
5053
5154 auto VulkanBuffer::FindMemoryType (uint32_t typeFilter, VkMemoryPropertyFlags properties) -> uint32_t
@@ -71,7 +74,7 @@ namespace sh::render::impl
7174 bufferInfo.usage = usageBits;
7275 bufferInfo.sharingMode = sharing;
7376
74- VkResult result = vkCreateBuffer (device, &bufferInfo, nullptr , &buffer);
77+ /* VkResult result = vkCreateBuffer(device, &bufferInfo, nullptr, &buffer);
7578 assert(result == VK_SUCCESS);
7679 if (result != VK_SUCCESS)
7780 return result;
@@ -86,16 +89,26 @@ namespace sh::render::impl
8689 allocInfo.allocationSize = memRequirements.size;
8790 allocInfo.memoryTypeIndex = idx;
8891
89- result = vkAllocateMemory (device, &allocInfo, nullptr , &bufferMem);
90- assert (result == VkResult::VK_SUCCESS );
91- if (result != VK_SUCCESS )
92- return result;
92+ result = vkAllocateMemory(device, &allocInfo, nullptr, &bufferMem);*/
93+
94+ bool bUseMap = (memPropFlagBits & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ) == VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ;
9395
94- result = vkBindBufferMemory (device, buffer, bufferMem, 0 );
96+ VmaAllocationCreateInfo allocCreateInfo{};
97+ allocCreateInfo.usage = VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO ;
98+ if (bUseMap)
99+ allocCreateInfo.flags = VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT ;
100+ if (persistentMapping)
101+ allocCreateInfo.flags |= VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT ;
102+
103+ VmaAllocationInfo allocInfo{};
104+ auto result = vmaCreateBuffer (allocator, &bufferInfo, &allocCreateInfo, &buffer, &bufferMem, &allocInfo);
95105 assert (result == VkResult::VK_SUCCESS );
96- if (persistentMapping)
97- vkMapMemory (device, bufferMem, 0 , bufferInfo.size , 0 , &data);
98106
107+ if (persistentMapping)
108+ data = allocInfo.pMappedData ;
109+ // result = vkBindBufferMemory(device, buffer, bufferMem, 0);
110+ // assert(result == VkResult::VK_SUCCESS);
111+
99112 return result;
100113 }
101114
@@ -107,9 +120,12 @@ namespace sh::render::impl
107120
108121 if (!persistentMapping)
109122 {
110- vkMapMemory (device, bufferMem, 0 , bufferInfo.size , 0 , &this ->data );
123+ // vkMapMemory(device, bufferMem, 0, bufferInfo.size, 0, &this->data);
124+ vmaMapMemory (allocator, bufferMem, &this ->data );
111125 std::memcpy (this ->data , data, static_cast <size_t >(bufferInfo.size ));
112- vkUnmapMemory (device, bufferMem);
126+ // vkUnmapMemory(device, bufferMem);
127+ vmaUnmapMemory (allocator, bufferMem);
128+
113129 }
114130 else
115131 std::memcpy (this ->data , data, static_cast <size_t >(bufferInfo.size ));
@@ -120,7 +136,7 @@ namespace sh::render::impl
120136 return buffer;
121137 }
122138
123- auto VulkanBuffer::GetBufferMemory () const -> VkDeviceMemory
139+ auto VulkanBuffer::GetBufferMemory () const -> VmaAllocation
124140 {
125141 return bufferMem;
126142 }
0 commit comments