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"
|
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:
|
2020-03-02 00:15:28 +08:00
|
|
|
VulkanMemoryPool(const VulkanDevice& dev, bool permitFp16);
|
2019-04-17 10:49:11 +08:00
|
|
|
virtual ~VulkanMemoryPool();
|
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
std::shared_ptr<VulkanMemory> allocMemory(const VkMemoryRequirements& requirements, VkFlags extraMask, bool seperate = false);
|
|
|
|
void returnMemory(std::shared_ptr<VulkanMemory> memory);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
// Free Unuseful Memory
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
const VulkanDevice& device() const {
|
|
|
|
return mDevice;
|
|
|
|
}
|
2020-03-02 00:15:28 +08:00
|
|
|
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
|
|
|
// For image fast alloc
|
|
|
|
VkImage allocImage(const std::tuple<VkImageType, uint32_t, uint32_t, uint32_t, VkFormat>& info);
|
|
|
|
void returnImage(VkImage dst, std::tuple<VkImageType, uint32_t, uint32_t, uint32_t, VkFormat>&& info);
|
|
|
|
private:
|
2019-04-17 10:49:11 +08:00
|
|
|
// MemoryTypeIndex, Size, Memory
|
2020-11-05 16:41:56 +08:00
|
|
|
std::vector<std::multimap<VkDeviceSize, std::shared_ptr<VulkanMemory>>> mFreeBuffers;
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
VkPhysicalDeviceMemoryProperties mPropty;
|
|
|
|
const VulkanDevice& mDevice;
|
2020-03-02 00:15:28 +08:00
|
|
|
bool mPermitFp16 = false;
|
2020-11-05 16:41:56 +08:00
|
|
|
float mAllocedSize = 0.0f;
|
|
|
|
|
|
|
|
// For buffer alloc, key: size - usage
|
|
|
|
std::multimap<std::tuple<size_t, VkBufferUsageFlags, VkSharingMode>, VkBuffer> mFreeVkBuffers;
|
|
|
|
|
|
|
|
// For Image alloc
|
|
|
|
std::multimap<std::tuple<VkImageType,uint32_t,uint32_t,uint32_t,VkFormat>, VkImage> mFreeImages;
|
2019-04-17 10:49:11 +08:00
|
|
|
};
|
|
|
|
} // namespace MNN
|
|
|
|
#endif /* VulkanMemoryPool_hpp */
|