Files
Seele/src/Engine/Graphics/Vulkan/Allocator.h
T

187 lines
4.6 KiB
C++
Raw Normal View History

2020-03-13 12:44:33 +01:00
#pragma once
#include "MinimalEngine.h"
2023-10-26 18:37:29 +02:00
#include "Enums.h"
2020-03-13 12:44:33 +01:00
#include "Containers/Map.h"
#include "Containers/Array.h"
2023-11-15 00:06:00 +01:00
#include "Graphics/Resources.h"
2020-06-02 11:46:18 +02:00
#include <mutex>
2020-03-13 12:44:33 +01:00
namespace Seele
{
2020-04-12 15:47:19 +02:00
namespace Vulkan
{
2021-04-01 16:40:14 +02:00
DECLARE_REF(Graphics)
2023-11-05 10:36:01 +01:00
DECLARE_REF(Allocator)
DECLARE_REF(Allocation)
2020-04-12 15:47:19 +02:00
class SubAllocation
{
public:
2023-11-08 23:27:21 +01:00
SubAllocation(PAllocation owner, VkDeviceSize requestedSize, VkDeviceSize allocatedOffset, VkDeviceSize allocatedSize, VkDeviceSize alignedOffset);
2020-04-12 15:47:19 +02:00
~SubAllocation();
2023-11-06 14:47:21 +01:00
VkDeviceMemory getHandle() const;
constexpr VkDeviceSize getSize() const
{
2023-11-08 23:27:21 +01:00
return requestedSize;
2023-11-06 14:47:21 +01:00
}
constexpr VkDeviceSize getOffset() const
{
return alignedOffset;
}
2023-11-15 00:06:00 +01:00
void *map();
2020-04-12 15:47:19 +02:00
void flushMemory();
2023-11-15 00:06:00 +01:00
void invalidate();
2020-04-12 15:47:19 +02:00
private:
2023-11-05 10:36:01 +01:00
PAllocation owner;
2023-11-08 23:27:21 +01:00
VkDeviceSize requestedSize;
2021-04-01 16:40:14 +02:00
VkDeviceSize allocatedOffset;
2020-04-12 15:47:19 +02:00
VkDeviceSize allocatedSize;
2023-11-12 16:11:27 +01:00
VkDeviceSize alignedOffset;
2020-04-12 15:47:19 +02:00
friend class Allocation;
friend class Allocator;
};
2021-04-01 16:40:14 +02:00
DEFINE_REF(SubAllocation)
2020-04-12 15:47:19 +02:00
class Allocation
{
public:
2023-11-15 17:42:57 +01:00
Allocation(PGraphics graphics, PAllocator pool, VkDeviceSize size, uint8 memoryTypeIndex,
2020-04-12 15:47:19 +02:00
VkMemoryPropertyFlags properties, VkMemoryDedicatedAllocateInfo *dedicatedInfo = nullptr);
~Allocation();
2023-11-05 10:36:01 +01:00
OSubAllocation getSuballocation(VkDeviceSize size, VkDeviceSize alignment);
void markFree(PSubAllocation alloc);
2023-11-06 14:47:21 +01:00
constexpr VkDeviceMemory getHandle() const
{
return allocatedMemory;
}
2023-11-15 00:06:00 +01:00
constexpr void* map()
2023-11-06 14:47:21 +01:00
{
if (!canMap)
{
return nullptr;
}
if (!isMapped)
{
vkMapMemory(device, allocatedMemory, 0, bytesAllocated, 0, &mappedPointer);
isMapped = true;
}
return mappedPointer;
}
2023-11-05 10:36:01 +01:00
void flushMemory();
2023-11-15 00:06:00 +01:00
void invalidate();
2020-04-12 15:47:19 +02:00
private:
VkDevice device;
2023-11-15 17:42:57 +01:00
PAllocator pool;
2020-04-12 15:47:19 +02:00
VkDeviceSize bytesAllocated;
VkDeviceSize bytesUsed;
2021-04-01 16:40:14 +02:00
VkDeviceMemory allocatedMemory;
2023-11-05 10:36:01 +01:00
Array<PSubAllocation> activeAllocations;
2023-11-08 23:27:21 +01:00
Map<VkDeviceSize, VkDeviceSize> freeRanges;
2020-04-12 15:47:19 +02:00
void *mappedPointer;
uint8 canMap : 1;
uint8 isMapped : 1;
2023-11-15 17:42:57 +01:00
uint8 isDedicated : 1;
2021-04-01 16:40:14 +02:00
VkMemoryPropertyFlags properties;
uint8 memoryTypeIndex;
2020-04-12 15:47:19 +02:00
friend class Allocator;
};
2021-04-01 16:40:14 +02:00
DEFINE_REF(Allocation)
2020-04-12 15:47:19 +02:00
class Allocator
{
public:
Allocator(PGraphics graphics);
~Allocator();
2023-11-01 23:12:30 +01:00
OSubAllocation allocate(const VkMemoryRequirements2 &requirements, VkMemoryPropertyFlags props,
2020-04-12 15:47:19 +02:00
VkMemoryDedicatedAllocateInfo *dedicatedInfo = nullptr);
2023-11-01 23:12:30 +01:00
OSubAllocation allocate(const VkMemoryRequirements2 &requirements, VkMemoryPropertyFlags props,
2020-04-12 15:47:19 +02:00
VkBuffer buffer)
{
2023-11-15 17:42:57 +01:00
VkMemoryDedicatedAllocateInfo allocInfo = {
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
.pNext = nullptr,
.image = VK_NULL_HANDLE,
.buffer = buffer,
};
2020-04-12 15:47:19 +02:00
return allocate(requirements, props, &allocInfo);
}
2023-11-01 23:12:30 +01:00
OSubAllocation allocate(const VkMemoryRequirements2 &requirements, VkMemoryPropertyFlags props,
2020-04-12 15:47:19 +02:00
VkImage image)
{
2023-11-15 17:42:57 +01:00
VkMemoryDedicatedAllocateInfo allocInfo = {
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
.pNext = nullptr,
.image = image,
.buffer = VK_NULL_HANDLE,
};
2020-04-12 15:47:19 +02:00
return allocate(requirements, props, &allocInfo);
}
2023-11-07 16:55:13 +01:00
void free(PAllocation allocation);
2020-04-12 15:47:19 +02:00
private:
2023-11-15 00:06:00 +01:00
static constexpr VkDeviceSize DEFAULT_ALLOCATION = 16 * 1024 * 1024; // 16MB
2020-04-12 15:47:19 +02:00
struct HeapInfo
{
VkDeviceSize maxSize = 0;
2022-04-17 09:10:20 +02:00
VkDeviceSize inUse = 0;
2023-11-05 10:36:01 +01:00
Array<OAllocation> allocations;
2023-11-15 00:06:00 +01:00
HeapInfo() = default;
2023-11-05 10:36:01 +01:00
HeapInfo(const HeapInfo& other) = delete;
HeapInfo(HeapInfo&& other) = default;
HeapInfo& operator=(const HeapInfo& other) = delete;
HeapInfo& operator=(HeapInfo&& other) = default;
2020-04-12 15:47:19 +02:00
};
Array<HeapInfo> heaps;
2023-02-13 14:56:13 +01:00
uint32 findMemoryType(uint32 typeBits, VkMemoryPropertyFlags properties);
2020-04-12 15:47:19 +02:00
PGraphics graphics;
VkPhysicalDeviceMemoryProperties memProperties;
};
2021-04-01 16:40:14 +02:00
DEFINE_REF(Allocator)
2023-11-15 00:06:00 +01:00
class StagingBuffer : public Gfx::QueueOwnedResource
2020-04-12 15:47:19 +02:00
{
public:
2023-12-14 09:04:23 +01:00
StagingBuffer(PGraphics graphics, OSubAllocation allocation, VkBuffer buffer, VkDeviceSize size, Gfx::QueueType owner);
2020-04-12 15:47:19 +02:00
~StagingBuffer();
2023-11-15 00:06:00 +01:00
void* map();
void flush();
void invalidate();
2023-11-05 10:36:01 +01:00
constexpr VkBuffer getHandle() const
2020-04-12 15:47:19 +02:00
{
return buffer;
}
2023-11-15 00:06:00 +01:00
VkDeviceMemory getMemory() const;
2023-11-06 14:47:21 +01:00
VkDeviceSize getOffset() const;
2023-11-15 00:06:00 +01:00
constexpr uint64 getSize() const
2023-11-06 14:47:21 +01:00
{
2023-11-15 00:06:00 +01:00
return size;
2023-11-06 14:47:21 +01:00
}
2020-04-12 15:47:19 +02:00
private:
2023-11-15 00:06:00 +01:00
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner) override;
virtual void executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePipelineStageFlags srcStage,
Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) override;
2023-11-05 10:36:01 +01:00
OSubAllocation allocation;
2023-11-15 00:06:00 +01:00
PGraphics graphics;
2020-04-12 15:47:19 +02:00
VkBuffer buffer;
2023-11-10 19:18:09 +01:00
VkDeviceSize size;
2020-04-12 15:47:19 +02:00
};
2021-04-01 16:40:14 +02:00
DEFINE_REF(StagingBuffer)
2020-04-12 15:47:19 +02:00
class StagingManager
{
public:
2023-11-15 17:42:57 +01:00
StagingManager(PGraphics graphics, PAllocator pool);
2020-04-12 15:47:19 +02:00
~StagingManager();
2023-12-14 09:04:23 +01:00
OStagingBuffer create(uint64 size, Gfx::QueueType owner);
2020-04-12 15:47:19 +02:00
private:
PGraphics graphics;
2023-11-15 17:42:57 +01:00
PAllocator pool;
2020-04-12 15:47:19 +02:00
};
2021-04-01 16:40:14 +02:00
DEFINE_REF(StagingManager)
2020-04-12 15:47:19 +02:00
} // namespace Vulkan
} // namespace Seele