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