MNN/source/backend/vulkan/component/VulkanMemoryPool.hpp

80 lines
1.9 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// VulkanMemoryPool.hpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef VulkanMemoryPool_hpp
#define VulkanMemoryPool_hpp
#include <map>
#include <memory>
#include <vector>
2019-12-27 22:16:57 +08:00
#include "core/NonCopyable.hpp"
2020-11-05 16:41:56 +08:00
#include "component/VulkanDevice.hpp"
#include "vulkan/vulkan_wrapper.h"
2020-12-15 14:12:35 +08:00
#include "core/BufferAllocator.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
class VulkanMemory : public NonCopyable {
public:
VulkanMemory(const VulkanDevice& dev, const VkMemoryAllocateInfo& info);
~VulkanMemory();
VkDeviceMemory get() const {
return mMemory;
}
uint32_t type() const {
return mTypeIndex;
}
VkDeviceSize size() const {
return mSize;
}
private:
VkDeviceMemory mMemory;
const VulkanDevice& mDevice;
uint32_t mTypeIndex;
VkDeviceSize mSize;
};
class VulkanMemoryPool : public NonCopyable {
public:
VulkanMemoryPool(const VulkanDevice& dev, bool permitFp16);
2020-12-15 14:12:35 +08:00
VulkanMemoryPool(const VulkanMemoryPool* parent);
2019-04-17 10:49:11 +08:00
virtual ~VulkanMemoryPool();
2020-12-15 14:12:35 +08:00
// VulkanMemory* , offset
2023-09-04 10:42:11 +08:00
MemChunk allocMemory(const VkMemoryRequirements& requirements, VkFlags extraMask, bool separate = false);
void returnMemory(MemChunk memory);
2019-04-17 10:49:11 +08:00
// Free Unuseful Memory
void clear();
const VulkanDevice& device() const {
return mDevice;
}
bool permitFp16() const {
return mPermitFp16;
}
2019-04-17 10:49:11 +08:00
// Return MB
float computeSize() const;
2020-11-05 16:41:56 +08:00
// For buffer fast alloc
VkBuffer allocBuffer(size_t size, VkBufferUsageFlags flags, VkSharingMode shared);
void returnBuffer(VkBuffer buffer, size_t size, VkBufferUsageFlags flags, VkSharingMode shared);
2019-04-17 10:49:11 +08:00
2020-11-05 16:41:56 +08:00
private:
2019-04-17 10:49:11 +08:00
// MemoryTypeIndex, Size, Memory
2020-12-15 14:12:35 +08:00
std::vector<std::shared_ptr<BufferAllocator>> mAllocators;
2019-04-17 10:49:11 +08:00
const VulkanDevice& mDevice;
bool mPermitFp16 = false;
2019-04-17 10:49:11 +08:00
};
} // namespace MNN
#endif /* VulkanMemoryPool_hpp */