use vma for streambuffer implementation

This commit is contained in:
niki
2022-05-04 15:41:50 +02:00
parent 74c0c192f7
commit e6f8b465bc
5 changed files with 42 additions and 48 deletions
+10 -37
View File
@@ -2,22 +2,10 @@
#include "vulkan/vulkan.h"
namespace love {
namespace graphics {
namespace vulkan {
static uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFtiler, VkMemoryPropertyFlags properties) {
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
if ((typeFtiler & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
return i;
}
}
throw love::Exception("failed to find suitable memory type");
}
static VkBufferUsageFlags getUsageFlags(BufferUsage mode) {
switch (mode) {
case BUFFERUSAGE_VERTEX: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
@@ -28,47 +16,32 @@ namespace love {
}
}
StreamBuffer::StreamBuffer(VkDevice device, VkPhysicalDevice physicalDevice, BufferUsage mode, size_t size)
: love::graphics::StreamBuffer(mode, size),
device(device) {
StreamBuffer::StreamBuffer(VmaAllocator allocator, BufferUsage mode, size_t size)
: love::graphics::StreamBuffer(mode, size) {
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = getSize();
bufferInfo.usage = getUsageFlags(mode);
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
throw love::Exception("failed to create buffer");
}
VmaAllocationCreateInfo allocCreateInfo = {};
allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT; // always mapped
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(physicalDevice, memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
throw love::Exception("failed to allocate vertex buffer memory");
}
vkBindBufferMemory(device, buffer, bufferMemory, 0);
vmaCreateBuffer(allocator, &bufferInfo, &allocCreateInfo, &buffer, &allocation, &allocInfo);
}
StreamBuffer::~StreamBuffer() {
//vkDestroyBuffer(device, buffer, nullptr);
//vkFreeMemory(device, bufferMemory, nullptr);
// vmaDestroyBuffer(allocator, buffer, allocation);
// vmaDestroyAllocator(allocator);
}
love::graphics::StreamBuffer::MapInfo StreamBuffer::map(size_t minsize) {
(void)minsize;
vkMapMemory(device, bufferMemory, 0, getSize(), 0, &mappedMemory);
return love::graphics::StreamBuffer::MapInfo((uint8*) mappedMemory, getSize());
return love::graphics::StreamBuffer::MapInfo((uint8*) allocInfo.pMappedData, getSize());
}
size_t StreamBuffer::unmap(size_t usedSize) {
vkUnmapMemory(device, bufferMemory);
return usedSize;
}