Files
Seele/src/Engine/Graphics/Vulkan/VulkanAllocator.cpp
T

362 lines
12 KiB
C++
Raw Normal View History

2020-03-13 12:44:33 +01:00
#include "VulkanAllocator.h"
#include "VulkanGraphics.h"
#include "VulkanInitializer.h"
using namespace Seele::Vulkan;
2020-05-05 01:51:13 +02:00
SubAllocation::SubAllocation(Allocation *owner, VkDeviceSize allocatedOffset, VkDeviceSize size, VkDeviceSize alignedOffset, VkDeviceSize allocatedSize)
: owner(owner)
, size(size)
, allocatedOffset(allocatedOffset)
, alignedOffset(alignedOffset)
, allocatedSize(allocatedSize)
{
}
2020-04-12 15:47:19 +02:00
SubAllocation::~SubAllocation()
{
owner->markFree(this);
2020-04-12 15:47:19 +02:00
}
VkDeviceMemory SubAllocation::getHandle() const
{
return owner->getHandle();
2020-04-12 15:47:19 +02:00
}
bool SubAllocation::isReadable() const
{
return owner->isReadable();
2020-04-12 15:47:19 +02:00
}
2020-05-05 01:51:13 +02:00
void *SubAllocation::getMappedPointer()
2020-04-12 15:47:19 +02:00
{
return (uint8 *)owner->getMappedPointer() + alignedOffset;
2020-04-12 15:47:19 +02:00
}
void SubAllocation::flushMemory()
{
owner->flushMemory();
2020-04-12 15:47:19 +02:00
}
void SubAllocation::invalidateMemory()
{
owner->invalidateMemory();
2020-04-12 15:47:19 +02:00
}
2020-05-05 01:51:13 +02:00
Allocation::Allocation(PGraphics graphics, Allocator *allocator, VkDeviceSize size, uint8 memoryTypeIndex,
VkMemoryPropertyFlags properties, VkMemoryDedicatedAllocateInfo *dedicatedInfo)
: device(graphics->getDevice())
, allocator(allocator)
, bytesAllocated(0)
, bytesUsed(0)
, readable(properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
, properties(properties)
, memoryTypeIndex(memoryTypeIndex)
{
VkMemoryAllocateInfo allocInfo =
init::MemoryAllocateInfo();
allocInfo.allocationSize = size;
allocInfo.memoryTypeIndex = memoryTypeIndex;
isDedicated = dedicatedInfo != nullptr;
allocInfo.pNext = dedicatedInfo;
VK_CHECK(vkAllocateMemory(device, &allocInfo, nullptr, &allocatedMemory));
bytesAllocated = size;
PSubAllocation freeRange = new SubAllocation(this, 0, size, 0, size);
freeRanges[0] = freeRange;
2020-04-12 15:47:19 +02:00
canMap = (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
isMapped = false;
}
2020-04-12 15:47:19 +02:00
Allocation::~Allocation()
{
}
PSubAllocation Allocation::getSuballocation(VkDeviceSize requestedSize, VkDeviceSize alignment)
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lck(lock);
if (isDedicated)
{
if (activeAllocations.empty() && requestedSize == bytesAllocated)
{
PSubAllocation suballoc = freeRanges[0];
activeAllocations[0] = suballoc.getHandle();
freeRanges.clear();
bytesUsed += requestedSize;
return suballoc;
}
else
{
return nullptr;
}
}
2022-04-15 23:45:44 +02:00
for (auto& [allocatedOffset, freeAllocation] : freeRanges)
{
2022-04-15 23:45:44 +02:00
assert(allocatedOffset == freeAllocation->allocatedOffset);
VkDeviceSize alignedOffset = align(allocatedOffset, alignment);
VkDeviceSize alignmentAdjustment = alignedOffset - allocatedOffset;
VkDeviceSize size = alignmentAdjustment + requestedSize;
if (freeAllocation->size == size)
{
freeRanges.erase(allocatedOffset);
activeAllocations[allocatedOffset] = freeAllocation.getHandle();
bytesUsed += size;
return freeAllocation;
}
else if (size < freeAllocation->allocatedSize)
{
freeAllocation->size -= size;
freeAllocation->allocatedSize -= size;
freeAllocation->allocatedOffset += size;
freeAllocation->alignedOffset += size;
PSubAllocation subAlloc = new SubAllocation(this, allocatedOffset, size, alignedOffset, size);
activeAllocations[allocatedOffset] = subAlloc.getHandle();
freeRanges[freeAllocation->allocatedOffset] = freeAllocation;
2022-04-15 23:45:44 +02:00
freeRanges.erase(allocatedOffset);
bytesUsed += size;
return subAlloc;
}
}
return nullptr;
}
2020-05-05 01:51:13 +02:00
void Allocation::markFree(SubAllocation *allocation)
2020-04-12 15:47:19 +02:00
{
// Dont free if it is already a free allocation, since they also mark themselves on deletion
if (freeRanges.find(allocation->allocatedOffset) != freeRanges.end())
{
return;
}
VkDeviceSize lowerBound = allocation->allocatedOffset;
VkDeviceSize upperBound = allocation->allocatedOffset + allocation->allocatedSize;
PSubAllocation allocHandle;
PSubAllocation freeRangeToDelete;
2020-06-02 11:46:18 +02:00
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lck(lock);
2021-11-19 15:08:56 +01:00
//Join lower bound
2022-04-15 23:45:44 +02:00
for (auto& [allocatedOffset, freeAlloc] : freeRanges)
{
2021-11-19 15:08:56 +01:00
if (freeAlloc->allocatedOffset <= lowerBound
&& freeAlloc->allocatedOffset + freeAlloc->allocatedSize >= upperBound)
{
// allocation is already in a free region
return;
}
if (freeAlloc->allocatedOffset + freeAlloc->allocatedSize == lowerBound)
{
//extend freeAlloc by the allocatedSize
freeAlloc->allocatedSize += allocation->allocatedSize;
allocHandle = freeAlloc;
break;
}
}
2021-11-19 15:08:56 +01:00
//Join upper bound
auto foundAlloc = freeRanges.find(upperBound);
if (foundAlloc != freeRanges.end())
{
2022-04-15 23:45:44 +02:00
freeRangeToDelete = foundAlloc->second;
2021-11-19 15:08:56 +01:00
// There is a free allocation ending where the new free one ends
if (allocHandle != nullptr)
{
// extend allocHandle by another foundAlloc->allocatedSize bytes
2022-04-15 23:45:44 +02:00
allocHandle->allocatedSize += foundAlloc->second->allocatedSize;
freeRanges.erase(foundAlloc->first);
2021-11-19 15:08:56 +01:00
}
else
{
// set foundAlloc back by size amount
2022-04-15 23:45:44 +02:00
allocHandle = foundAlloc->second;
2021-11-19 15:08:56 +01:00
allocHandle->allocatedOffset -= allocation->allocatedSize;
allocHandle->alignedOffset -= allocation->allocatedSize;
// place back at correct offset
freeRanges[allocHandle->allocatedOffset] = allocHandle;
// remove from offset map since key changes
2022-04-15 23:45:44 +02:00
freeRanges.erase(foundAlloc->first);
2021-11-19 15:08:56 +01:00
}
}
2021-11-19 15:08:56 +01:00
if (allocHandle == nullptr)
{
allocHandle = new SubAllocation(this, allocation->alignedOffset, allocation->size, allocation->alignedOffset, allocation->allocatedSize);
freeRanges[allocation->allocatedOffset] = allocHandle;
}
activeAllocations.erase(allocation->allocatedOffset);
}
bytesUsed -= allocation->allocatedSize;
// TODO: delete allocation when bytesUsed == 0
2020-04-12 15:47:19 +02:00
}
2020-04-01 02:17:49 +02:00
Allocator::Allocator(PGraphics graphics)
: graphics(graphics)
2020-03-13 12:44:33 +01:00
{
vkGetPhysicalDeviceMemoryProperties(graphics->getPhysicalDevice(), &memProperties);
heaps.resize(memProperties.memoryHeapCount);
for (size_t i = 0; i < memProperties.memoryHeapCount; ++i)
{
VkMemoryHeap memoryHeap = memProperties.memoryHeaps[i];
HeapInfo &heapInfo = heaps[i];
heapInfo.maxSize = memoryHeap.size;
}
}
Allocator::~Allocator()
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lck(lock);
for (auto heap : heaps)
{
for (auto alloc : heap.allocations)
{
assert(alloc->activeAllocations.empty());
assert(alloc->freeRanges.size() == 1);
}
heap.allocations.clear();
}
graphics = nullptr;
}
2020-05-05 01:51:13 +02:00
PSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2, VkMemoryPropertyFlags properties, VkMemoryDedicatedAllocateInfo *dedicatedInfo)
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lck(lock);
const VkMemoryRequirements &requirements = memRequirements2.memoryRequirements;
uint8 memoryTypeIndex;
VK_CHECK(findMemoryType(requirements.memoryTypeBits, properties, &memoryTypeIndex));
uint32 heapIndex = memProperties.memoryTypes[memoryTypeIndex].heapIndex;
2020-05-05 01:51:13 +02:00
if (memRequirements2.pNext != nullptr)
{
VkMemoryDedicatedRequirements *dedicatedReq = (VkMemoryDedicatedRequirements *)memRequirements2.pNext;
if (dedicatedReq->prefersDedicatedAllocation)
{
PAllocation newAllocation = new Allocation(graphics, this, requirements.size, memoryTypeIndex, properties, dedicatedInfo);
heaps[heapIndex].allocations.add(newAllocation);
return newAllocation->getSuballocation(requirements.size, requirements.alignment);
2021-12-22 11:42:07 +01:00
}
}
for (auto alloc : heaps[heapIndex].allocations)
{
PSubAllocation suballoc = alloc->getSuballocation(requirements.size, requirements.alignment);
if (suballoc != nullptr)
{
return suballoc;
}
}
2020-05-05 01:51:13 +02:00
// no suitable allocations found, allocate new block
PAllocation newAllocation = new Allocation(graphics, this, (requirements.size > MemoryBlockSize) ? requirements.size : (VkDeviceSize)MemoryBlockSize, memoryTypeIndex, properties, nullptr);
heaps[heapIndex].allocations.add(newAllocation);
return newAllocation->getSuballocation(requirements.size, requirements.alignment);
}
2020-05-05 01:51:13 +02:00
void Allocator::free(Allocation *allocation)
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lck(lock);
for (auto heap : heaps)
{
for (uint32 i = 0; i < heap.allocations.size(); ++i)
{
if (heap.allocations[i] == allocation)
{
2022-04-15 23:45:44 +02:00
heap.allocations.removeAt(i, false);
return;
}
}
}
2020-04-12 15:47:19 +02:00
}
2020-05-05 01:51:13 +02:00
VkResult Allocator::findMemoryType(uint32 typeBits, VkMemoryPropertyFlags properties, uint8 *typeIndex)
2020-04-12 15:47:19 +02:00
{
for (uint8 memoryIndex = 0; memoryIndex < memProperties.memoryTypeCount && typeBits; ++memoryIndex)
{
if ((typeBits & 1) == 1)
{
if ((memProperties.memoryTypes[memoryIndex].propertyFlags & properties) == properties)
{
*typeIndex = memoryIndex;
return VK_SUCCESS;
}
}
typeBits >>= 1;
}
2020-05-05 01:51:13 +02:00
return VK_ERROR_FORMAT_NOT_SUPPORTED;
2020-04-12 15:47:19 +02:00
}
StagingBuffer::StagingBuffer()
{
}
StagingBuffer::~StagingBuffer()
{
}
StagingManager::StagingManager(PGraphics graphics, PAllocator allocator)
: graphics(graphics), allocator(allocator)
2020-04-12 15:47:19 +02:00
{
}
StagingManager::~StagingManager()
{
}
void StagingManager::clearPending()
{
}
PStagingBuffer StagingManager::allocateStagingBuffer(uint32 size, VkBufferUsageFlags usage, bool bCPURead)
{
2022-01-12 14:40:26 +01:00
std::scoped_lock l(lock);
for (auto it = freeBuffers.begin(); it != freeBuffers.end(); ++it)
{
auto freeBuffer = *it;
if (freeBuffer->getSize() == size && freeBuffer->isReadable() == bCPURead && freeBuffer->usage == usage)
{
//std::cout << "Reusing staging buffer" << std::endl;
activeBuffers.add(freeBuffer.getHandle());
freeBuffers.remove(it, false);
return freeBuffer;
}
}
//std::cout << "Creating new stagingbuffer" << std::endl;
PStagingBuffer stagingBuffer = new StagingBuffer();
VkBufferCreateInfo stagingBufferCreateInfo = init::BufferCreateInfo(usage, size);
stagingBufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
2022-01-12 14:40:26 +01:00
uint32 queueIndex = graphics->getFamilyMapping().getQueueTypeFamilyIndex(Gfx::QueueType::DEDICATED_TRANSFER);
stagingBufferCreateInfo.queueFamilyIndexCount = 1;
stagingBufferCreateInfo.pQueueFamilyIndices = &queueIndex;
VkDevice vulkanDevice = graphics->getDevice();
2020-04-12 15:47:19 +02:00
VK_CHECK(vkCreateBuffer(vulkanDevice, &stagingBufferCreateInfo, nullptr, &stagingBuffer->buffer));
2020-04-12 15:47:19 +02:00
VkMemoryDedicatedRequirements dedicatedReqs;
dedicatedReqs.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;
dedicatedReqs.pNext = nullptr;
VkMemoryRequirements2 memReqs;
memReqs.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
memReqs.pNext = &dedicatedReqs;
VkBufferMemoryRequirementsInfo2 bufferQuery;
bufferQuery.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2;
bufferQuery.pNext = nullptr;
bufferQuery.buffer = stagingBuffer->buffer;
vkGetBufferMemoryRequirements2(vulkanDevice, &bufferQuery, &memReqs);
2020-04-12 15:47:19 +02:00
memReqs.memoryRequirements.alignment =
(16 > memReqs.memoryRequirements.alignment) ? 16 : memReqs.memoryRequirements.alignment;
stagingBuffer->allocation = allocator->allocate(memReqs, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | (bCPURead ? VK_MEMORY_PROPERTY_HOST_COHERENT_BIT : VK_MEMORY_PROPERTY_HOST_CACHED_BIT), stagingBuffer->buffer);
stagingBuffer->bReadable = bCPURead;
stagingBuffer->size = size;
stagingBuffer->usage = usage;
vkBindBufferMemory(graphics->getDevice(), stagingBuffer->buffer, stagingBuffer->getMemoryHandle(), stagingBuffer->getOffset());
2020-05-05 01:51:13 +02:00
activeBuffers.add(stagingBuffer.getHandle());
return stagingBuffer;
2020-04-12 15:47:19 +02:00
}
void StagingManager::releaseStagingBuffer(PStagingBuffer buffer)
{
2022-01-12 14:40:26 +01:00
std::scoped_lock l(lock);
freeBuffers.add(buffer);
activeBuffers.remove(activeBuffers.find(buffer.getHandle()));
}