1cb93a386Sopenharmony_ci// Copyright 2021 The SwiftShader Authors. All Rights Reserved.
2cb93a386Sopenharmony_ci//
3cb93a386Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4cb93a386Sopenharmony_ci// you may not use this file except in compliance with the License.
5cb93a386Sopenharmony_ci// You may obtain a copy of the License at
6cb93a386Sopenharmony_ci//
7cb93a386Sopenharmony_ci//    http://www.apache.org/licenses/LICENSE-2.0
8cb93a386Sopenharmony_ci//
9cb93a386Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10cb93a386Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11cb93a386Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb93a386Sopenharmony_ci// See the License for the specific language governing permissions and
13cb93a386Sopenharmony_ci// limitations under the License.
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci#include "Image.hpp"
16cb93a386Sopenharmony_ci#include "Util.hpp"
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ciImage::Image(vk::Device device, vk::PhysicalDevice physicalDevice, uint32_t width, uint32_t height, vk::Format format, vk::SampleCountFlagBits sampleCount /*= vk::SampleCountFlagBits::e1*/)
19cb93a386Sopenharmony_ci    : device(device)
20cb93a386Sopenharmony_ci{
21cb93a386Sopenharmony_ci	vk::ImageCreateInfo imageInfo;
22cb93a386Sopenharmony_ci	imageInfo.imageType = vk::ImageType::e2D;
23cb93a386Sopenharmony_ci	imageInfo.format = format;
24cb93a386Sopenharmony_ci	imageInfo.tiling = vk::ImageTiling::eOptimal;
25cb93a386Sopenharmony_ci	imageInfo.initialLayout = vk::ImageLayout::eGeneral;
26cb93a386Sopenharmony_ci	imageInfo.usage = vk::ImageUsageFlagBits::eColorAttachment;
27cb93a386Sopenharmony_ci	imageInfo.samples = sampleCount;
28cb93a386Sopenharmony_ci	imageInfo.extent = vk::Extent3D(width, height, 1);
29cb93a386Sopenharmony_ci	imageInfo.mipLevels = 1;
30cb93a386Sopenharmony_ci	imageInfo.arrayLayers = 1;
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci	image = device.createImage(imageInfo);
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci	vk::MemoryRequirements memoryRequirements = device.getImageMemoryRequirements(image);
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci	vk::MemoryAllocateInfo allocateInfo;
37cb93a386Sopenharmony_ci	allocateInfo.allocationSize = memoryRequirements.size;
38cb93a386Sopenharmony_ci	allocateInfo.memoryTypeIndex = Util::getMemoryTypeIndex(physicalDevice, memoryRequirements.memoryTypeBits);
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci	imageMemory = device.allocateMemory(allocateInfo);
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci	device.bindImageMemory(image, imageMemory, 0);
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci	vk::ImageViewCreateInfo imageViewInfo;
45cb93a386Sopenharmony_ci	imageViewInfo.image = image;
46cb93a386Sopenharmony_ci	imageViewInfo.viewType = vk::ImageViewType::e2D;
47cb93a386Sopenharmony_ci	imageViewInfo.format = format;
48cb93a386Sopenharmony_ci	imageViewInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
49cb93a386Sopenharmony_ci	imageViewInfo.subresourceRange.baseMipLevel = 0;
50cb93a386Sopenharmony_ci	imageViewInfo.subresourceRange.levelCount = 1;
51cb93a386Sopenharmony_ci	imageViewInfo.subresourceRange.baseArrayLayer = 0;
52cb93a386Sopenharmony_ci	imageViewInfo.subresourceRange.layerCount = 1;
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci	imageView = device.createImageView(imageViewInfo);
55cb93a386Sopenharmony_ci}
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ciImage::~Image()
58cb93a386Sopenharmony_ci{
59cb93a386Sopenharmony_ci	device.destroyImageView(imageView);
60cb93a386Sopenharmony_ci	device.freeMemory(imageMemory);
61cb93a386Sopenharmony_ci	device.destroyImage(image);
62cb93a386Sopenharmony_ci}
63