Files
OpenVulkano/openVulkanoCpp/Vulkan/Image.cpp
2024-07-07 23:51:09 +02:00

90 lines
3.7 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Image.hpp"
namespace OpenVulkano::Vulkan
{
void Image::Init(const Device* device, const vk::ImageCreateInfo& imageCreateInfo, vk::ImageViewCreateInfo imageViewCreateInfo, const vk::MemoryPropertyFlags& memoryPropertyFlags)
{
this->device = device->device;
image = device->device.createImage(imageCreateInfo);
format = imageCreateInfo.format;
extent = imageCreateInfo.extent;
// TODO allocate from resource manager
const vk::MemoryRequirements memRequirements = device->device.getImageMemoryRequirements(image);
size = allocSize = memRequirements.size;
const vk::MemoryAllocateInfo memAllocInfo = { allocSize, device->GetMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags) };
memory = device->device.allocateMemory(memAllocInfo);
device->device.bindImageMemory(image, memory, 0);
imageViewCreateInfo.image = image;
view = device->device.createImageView(imageViewCreateInfo);
}
void Image::SetLayout(vk::CommandBuffer& cmdBuffer, const vk::ImageSubresourceRange& subResourceRange, vk::ImageLayout newLayout, vk::ImageLayout oldLayout) const
{
const vk::ImageMemoryBarrier imgMemBarrier(/*VulkanUtils::GetAccessFlagsForLayout(oldLayout)*/{}, /*VulkanUtils::GetAccessFlagsForLayout(newLayout)*/vk::AccessFlagBits::eTransferWrite, oldLayout, newLayout, 0, 0, image, subResourceRange);
cmdBuffer.pipelineBarrier(/*VulkanUtils::GetPipelineStageForLayout(oldLayout)*/vk::PipelineStageFlagBits::eTopOfPipe, /*VulkanUtils::GetPipelineStageForLayout(newLayout)*/ vk::PipelineStageFlagBits::eTransfer, {}, nullptr, nullptr, imgMemBarrier);
}
void Image::Init(const Device* device, const DataFormat& format, const vk::Extent3D& resolution)
{
vk::ImageCreateInfo imgCreateInfo { {}, vk::ImageType::e2D, format, resolution, 1, 1 };
imgCreateInfo.usage = vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled;
imgCreateInfo.tiling = vk::ImageTiling::eOptimal;
imgCreateInfo.sharingMode = vk::SharingMode::eExclusive;
imgCreateInfo.initialLayout = vk::ImageLayout::eUndefined;
vk::ImageViewCreateInfo imgViewCreateInfo { {}, image, vk::ImageViewType::e2D, imgCreateInfo.format };
imgViewCreateInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
imgViewCreateInfo.subresourceRange.baseMipLevel = 0;
imgViewCreateInfo.subresourceRange.levelCount = 1;
imgViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imgViewCreateInfo.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
Init(device, imgCreateInfo, imgViewCreateInfo);
CreateSampler();
}
void Image::CreateSampler()
{
vk::SamplerCreateInfo samplerCreateInfo;
samplerCreateInfo.magFilter = vk::Filter::eLinear;
samplerCreateInfo.minFilter = vk::Filter::eLinear;
samplerCreateInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
samplerCreateInfo.addressModeU = vk::SamplerAddressMode::eClampToEdge;
samplerCreateInfo.addressModeV = vk::SamplerAddressMode::eClampToEdge;
samplerCreateInfo.addressModeW = vk::SamplerAddressMode::eClampToEdge;
samplerCreateInfo.borderColor = vk::BorderColor::eFloatTransparentBlack;
samplerCreateInfo.unnormalizedCoordinates = false;
samplerCreateInfo.compareEnable = false;
sampler = this->device.createSampler(samplerCreateInfo);
}
void Image::Close()
{
if(sampler)
{
device.destroySampler(sampler);
sampler = vk::Sampler();
}
if(view)
{
device.destroyImageView(view);
view = vk::ImageView();
}
if(image)
{
device.destroyImage(image);
image = vk::Image();
}
Buffer::Close();
}
}