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

205 lines
4.9 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"
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;
}
bool isReadable() const;
2020-04-12 15:47:19 +02:00
void *getMappedPointer();
void flushMemory();
void invalidateMemory();
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 alignedOffset;
VkDeviceSize allocatedSize;
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-05 10:36:01 +01:00
Allocation(PGraphics graphics, PAllocator allocator, 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;
}
constexpr void* getMappedPointer()
{
if (!canMap)
{
return nullptr;
}
if (!isMapped)
{
vkMapMemory(device, allocatedMemory, 0, bytesAllocated, 0, &mappedPointer);
isMapped = true;
}
return mappedPointer;
}
constexpr bool isReadable() const
{
return readable;
}
2023-11-05 10:36:01 +01:00
void flushMemory();
void invalidateMemory();
2020-04-12 15:47:19 +02:00
private:
VkDevice device;
2023-11-01 23:12:30 +01:00
PAllocator allocator;
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 isDedicated : 1;
uint8 canMap : 1;
uint8 isMapped : 1;
uint8 readable : 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)
{
VkMemoryDedicatedAllocateInfo allocInfo;
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
allocInfo.pNext = nullptr;
allocInfo.buffer = buffer;
allocInfo.image = VK_NULL_HANDLE;
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)
{
VkMemoryDedicatedAllocateInfo allocInfo;
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
allocInfo.pNext = nullptr;
allocInfo.buffer = VK_NULL_HANDLE;
allocInfo.image = image;
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:
enum
{
2022-02-24 22:38:26 +01:00
MemoryBlockSize = 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;
HeapInfo() {}
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-05 10:36:01 +01:00
DECLARE_REF(StagingManager)
2020-04-12 15:47:19 +02:00
class StagingBuffer
{
public:
2023-11-10 19:18:09 +01:00
StagingBuffer(OSubAllocation allocation, VkBuffer buffer, VkDeviceSize size, VkBufferUsageFlags usage, uint8 readable);
2020-04-12 15:47:19 +02:00
~StagingBuffer();
2023-11-05 10:36:01 +01:00
void* getMappedPointer();
void flushMappedMemory();
void invalidateMemory();
constexpr VkBuffer getHandle() const
2020-04-12 15:47:19 +02:00
{
return buffer;
}
2023-11-06 14:47:21 +01:00
VkDeviceMemory getMemoryHandle() const;
VkDeviceSize getOffset() const;
uint64 getSize() const;
constexpr bool isReadable() const
{
return readable;
}
constexpr VkBufferUsageFlags getUsage() const
{
return usage;
}
2020-04-12 15:47:19 +02:00
private:
2023-11-05 10:36:01 +01:00
OSubAllocation allocation;
2020-04-12 15:47:19 +02:00
VkBuffer buffer;
2023-11-10 19:18:09 +01:00
VkDeviceSize size;
VkBufferUsageFlags usage;
2023-11-05 10:36:01 +01:00
uint8 readable;
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:
StagingManager(PGraphics graphics, PAllocator allocator);
~StagingManager();
2023-11-05 10:36:01 +01:00
OStagingBuffer allocateStagingBuffer(uint64 size, VkBufferUsageFlags usageFlags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, bool bCPURead = false);
void releaseStagingBuffer(OStagingBuffer buffer);
2020-04-12 15:47:19 +02:00
void clearPending();
private:
PGraphics graphics;
PAllocator allocator;
2023-11-05 10:36:01 +01:00
Array<OStagingBuffer> freeBuffers;
Array<PStagingBuffer> activeBuffers;
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