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