MNN/source/backend/vulkan/component/VulkanImage.cpp

100 lines
3.3 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// VulkanImage.cpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
2019-12-27 22:16:57 +08:00
#include "backend/vulkan/component/VulkanImage.hpp"
2019-04-17 10:49:11 +08:00
#include <string.h>
namespace MNN {
VulkanSampler::VulkanSampler(const VulkanDevice& dev, VkFilter filter, VkSamplerAddressMode mode) : mDevice(dev) {
// Finally, create a sampler.
CALL_VK(mDevice.createSampler(mSampler, filter, mode));
}
VulkanSampler::~VulkanSampler() {
mDevice.destroySampler(mSampler);
}
void VulkanImage::release() {
2020-11-05 16:41:56 +08:00
if (nullptr == mMemory) {
2019-04-17 10:49:11 +08:00
return;
}
const_cast<VulkanMemoryPool&>(mPool).returnMemory(mMemory);
2020-11-05 16:41:56 +08:00
mMemory = nullptr;
2019-04-17 10:49:11 +08:00
}
static VkFormat _getFormat(halide_type_t type) {
switch (type.code) {
case halide_type_float:
return VK_FORMAT_R32G32B32A32_SFLOAT;
2019-04-17 10:49:11 +08:00
case halide_type_int: {
if (8 == type.bits) {
return VK_FORMAT_R8G8B8A8_SINT;
} else if (type.bits == 16) {
return VK_FORMAT_R16G16B16A16_SINT;
}
return VK_FORMAT_R32G32B32A32_SINT;
}
case halide_type_uint: {
if (8 == type.bits) {
return VK_FORMAT_R8G8B8A8_UINT;
} else if (type.bits == 16) {
return VK_FORMAT_R16G16B16A16_UINT;
}
return VK_FORMAT_R32G32B32A32_UINT;
}
default:
break;
}
return VK_FORMAT_R32G32B32A32_SFLOAT;
2019-04-17 10:49:11 +08:00
}
VulkanImage::VulkanImage(const VulkanMemoryPool& pool, bool seperate, const std::vector<int>& dims, halide_type_t type)
: mDevice(pool.device()), mPool(pool) {
2019-04-17 10:49:11 +08:00
MNN_ASSERT(dims.size() >= 1 && dims.size() <= 3);
auto imageType = VK_IMAGE_TYPE_1D;
auto viewType = VK_IMAGE_VIEW_TYPE_1D;
mDims = dims;
2020-11-05 16:41:56 +08:00
auto mWidth = dims[0];
auto mHeight = 1;
auto mDepth = 1;
2019-04-17 10:49:11 +08:00
if (dims.size() > 1) {
mHeight = dims[1];
imageType = VK_IMAGE_TYPE_2D;
viewType = VK_IMAGE_VIEW_TYPE_2D;
}
if (dims.size() > 2) {
mDepth = dims[2];
imageType = VK_IMAGE_TYPE_3D;
viewType = VK_IMAGE_VIEW_TYPE_3D;
}
auto format = _getFormat(type);
if (pool.permitFp16() && format == VK_FORMAT_R32G32B32A32_SFLOAT) {
// Use fp16 instead of fp32
format = VK_FORMAT_R16G16B16A16_SFLOAT;
}
2020-11-05 16:41:56 +08:00
auto mFormat = format;
mInfo = std::make_tuple(imageType, mWidth, mHeight, mDepth, mFormat);
2019-04-17 10:49:11 +08:00
// FUNC_PRINT(format);
2020-11-05 16:41:56 +08:00
mImage.first = const_cast<VulkanMemoryPool&>(mPool).allocImage(mInfo);
mLayout = VK_IMAGE_LAYOUT_UNDEFINED;
2019-04-17 10:49:11 +08:00
VkMemoryRequirements memRequirements;
2020-11-05 16:41:56 +08:00
mDevice.getImageMemoryRequirements(mImage.first, memRequirements);
2019-04-17 10:49:11 +08:00
mMemory = const_cast<VulkanMemoryPool&>(mPool).allocMemory(memRequirements, 0, seperate);
// FUNC_PRINT(mMemory->type());
2020-11-05 16:41:56 +08:00
mDevice.bindImageMemory(mImage.first, mMemory->get());
CALL_VK(mDevice.createImageView(mImage.second, mImage.first, viewType, format));
2019-04-17 10:49:11 +08:00
}
VulkanImage::~VulkanImage() {
2020-11-05 16:41:56 +08:00
mDevice.destroyImageView(mImage.second, nullptr);
const_cast<VulkanMemoryPool&>(mPool).returnImage(std::move(mImage.first), std::move(mInfo));
if (nullptr != mMemory) {
const_cast<VulkanMemoryPool&>(mPool).returnMemory(mMemory);
2019-04-17 10:49:11 +08:00
}
}
} // namespace MNN