xref: /aosp_15_r20/external/swiftshader/tests/VulkanWrapper/Swapchain.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
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 "Swapchain.hpp"
16 #include "Window.hpp"
17 
Swapchain(vk::PhysicalDevice physicalDevice,vk::Device device,Window & window)18 Swapchain::Swapchain(vk::PhysicalDevice physicalDevice, vk::Device device, Window &window)
19     : device(device)
20 {
21 	vk::SurfaceKHR surface = window.getSurface();
22 
23 	// Create the swapchain
24 	vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR(surface);
25 	extent = surfaceCapabilities.currentExtent;
26 
27 	vk::SwapchainCreateInfoKHR swapchainCreateInfo;
28 	swapchainCreateInfo.surface = surface;
29 	swapchainCreateInfo.minImageCount = 2;  // double-buffered
30 	swapchainCreateInfo.imageFormat = colorFormat;
31 	swapchainCreateInfo.imageColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
32 	swapchainCreateInfo.imageExtent = extent;
33 	swapchainCreateInfo.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
34 	swapchainCreateInfo.preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity;
35 	swapchainCreateInfo.imageArrayLayers = 1;
36 	swapchainCreateInfo.imageSharingMode = vk::SharingMode::eExclusive;
37 	swapchainCreateInfo.presentMode = vk::PresentModeKHR::eFifo;
38 	swapchainCreateInfo.clipped = VK_TRUE;
39 	swapchainCreateInfo.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
40 
41 	swapchain = device.createSwapchainKHR(swapchainCreateInfo);
42 
43 	// Obtain the images and create views for them
44 	images = device.getSwapchainImagesKHR(swapchain);
45 
46 	imageViews.resize(images.size());
47 	for(size_t i = 0; i < imageViews.size(); i++)
48 	{
49 		vk::ImageViewCreateInfo colorAttachmentView;
50 		colorAttachmentView.image = images[i];
51 		colorAttachmentView.viewType = vk::ImageViewType::e2D;
52 		colorAttachmentView.format = colorFormat;
53 		colorAttachmentView.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
54 		colorAttachmentView.subresourceRange.baseMipLevel = 0;
55 		colorAttachmentView.subresourceRange.levelCount = 1;
56 		colorAttachmentView.subresourceRange.baseArrayLayer = 0;
57 		colorAttachmentView.subresourceRange.layerCount = 1;
58 
59 		imageViews[i] = device.createImageView(colorAttachmentView);
60 	}
61 }
62 
~Swapchain()63 Swapchain::~Swapchain()
64 {
65 	for(auto &imageView : imageViews)
66 	{
67 		device.destroyImageView(imageView, nullptr);
68 	}
69 
70 	device.destroySwapchainKHR(swapchain, nullptr);
71 }
72 
acquireNextImage(vk::Semaphore presentCompleteSemaphore,uint32_t & imageIndex)73 void Swapchain::acquireNextImage(vk::Semaphore presentCompleteSemaphore, uint32_t &imageIndex)
74 {
75 	auto result = device.acquireNextImageKHR(swapchain, UINT64_MAX, presentCompleteSemaphore, vk::Fence());
76 	imageIndex = result.value;
77 }
78 
queuePresent(vk::Queue queue,uint32_t imageIndex,vk::Semaphore waitSemaphore)79 void Swapchain::queuePresent(vk::Queue queue, uint32_t imageIndex, vk::Semaphore waitSemaphore)
80 {
81 	vk::PresentInfoKHR presentInfo;
82 	presentInfo.pWaitSemaphores = &waitSemaphore;
83 	presentInfo.waitSemaphoreCount = 1;
84 	presentInfo.swapchainCount = 1;
85 	presentInfo.pSwapchains = &swapchain;
86 	presentInfo.pImageIndices = &imageIndex;
87 
88 	queue.presentKHR(presentInfo);
89 }
90