/*
 * 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, bool allocateMem, const vk::MemoryPropertyFlags& memoryPropertyFlags)
	{
		this->device = device->device;
		image = device->device.createImage(imageCreateInfo);
		format = imageCreateInfo.format;
		extent = imageCreateInfo.extent;

		if (allocateMem)
		{
			// 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, const vk::MemoryPropertyFlags& memoryPropertyFlags)
	{
		vk::ImageCreateInfo imgCreateInfo { {}, vk::ImageType::e2D, format, resolution, 1, 1 };

		imgCreateInfo.usage = vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled;
		if (memoryPropertyFlags & vk::MemoryPropertyFlagBits::eHostVisible)
		{
			imgCreateInfo.tiling = vk::ImageTiling::eLinear;
			imgCreateInfo.initialLayout = vk::ImageLayout::ePreinitialized;
		}
		else
		{
			imgCreateInfo.tiling = vk::ImageTiling::eOptimal;
			imgCreateInfo.initialLayout = vk::ImageLayout::eUndefined;
		}
		imgCreateInfo.sharingMode = vk::SharingMode::eExclusive;

		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, true, memoryPropertyFlags);
	}

	void Image::Close()
	{
		if(view)
		{
			device.destroyImageView(view);
			view = vk::ImageView();
		}
		if(image)
		{
			device.destroyImage(image);
			image = vk::Image();
		}
		Buffer::Close();
	}
}
