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