Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 "VkSwapchainKHR.hpp" |
| 16 | |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 17 | #include "Vulkan/VkDeviceMemory.hpp" |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 18 | #include "Vulkan/VkFence.hpp" |
| 19 | #include "Vulkan/VkImage.hpp" |
| 20 | #include "Vulkan/VkSemaphore.hpp" |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 21 | |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 22 | #include <algorithm> |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 23 | #include <cstring> |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 24 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 25 | namespace vk { |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 26 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 27 | SwapchainKHR::SwapchainKHR(const VkSwapchainCreateInfoKHR *pCreateInfo, void *mem) |
| 28 | : surface(vk::Cast(pCreateInfo->surface)) |
| 29 | , images(reinterpret_cast<PresentImage *>(mem)) |
| 30 | , imageCount(pCreateInfo->minImageCount) |
| 31 | , retired(false) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 32 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 33 | memset(reinterpret_cast<void *>(images), 0, imageCount * sizeof(PresentImage)); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void SwapchainKHR::destroy(const VkAllocationCallbacks *pAllocator) |
| 37 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 38 | for(uint32_t i = 0; i < imageCount; i++) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 39 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 40 | PresentImage ¤tImage = images[i]; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 41 | if(currentImage.exists()) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 42 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 43 | surface->detachImage(¤tImage); |
| 44 | currentImage.clear(); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 47 | |
| 48 | if(!retired) |
| 49 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 50 | surface->disassociateSwapchain(); |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 51 | } |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 52 | |
| 53 | vk::deallocate(images, pAllocator); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | size_t SwapchainKHR::ComputeRequiredAllocationSize(const VkSwapchainCreateInfoKHR *pCreateInfo) |
| 57 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 58 | return pCreateInfo->minImageCount * sizeof(PresentImage); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 61 | void SwapchainKHR::retire() |
| 62 | { |
| 63 | if(!retired) |
| 64 | { |
| 65 | retired = true; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 66 | surface->disassociateSwapchain(); |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 67 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 68 | for(uint32_t i = 0; i < imageCount; i++) |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 69 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 70 | PresentImage ¤tImage = images[i]; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 71 | if(currentImage.isAvailable()) |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 72 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 73 | surface->detachImage(¤tImage); |
| 74 | currentImage.clear(); |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 80 | void SwapchainKHR::resetImages() |
| 81 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 82 | for(uint32_t i = 0; i < imageCount; i++) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 83 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 84 | images[i].clear(); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 88 | VkResult SwapchainKHR::createImages(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 89 | { |
| 90 | resetImages(); |
| 91 | |
| 92 | VkImageCreateInfo imageInfo = {}; |
| 93 | imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 94 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 95 | if(pCreateInfo->flags & VK_SWAPCHAIN_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT_KHR) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 96 | { |
| 97 | imageInfo.flags |= VK_IMAGE_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT; |
| 98 | } |
| 99 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 100 | if(pCreateInfo->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 101 | { |
| 102 | imageInfo.flags |= VK_IMAGE_CREATE_PROTECTED_BIT; |
| 103 | } |
| 104 | |
| 105 | imageInfo.imageType = VK_IMAGE_TYPE_2D; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 106 | imageInfo.format = pCreateInfo->imageFormat; |
| 107 | imageInfo.extent.height = pCreateInfo->imageExtent.height; |
| 108 | imageInfo.extent.width = pCreateInfo->imageExtent.width; |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 109 | imageInfo.extent.depth = 1; |
| 110 | imageInfo.mipLevels = 1; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 111 | imageInfo.arrayLayers = pCreateInfo->imageArrayLayers; |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 112 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; |
| 113 | imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 114 | imageInfo.usage = pCreateInfo->imageUsage; |
| 115 | imageInfo.sharingMode = pCreateInfo->imageSharingMode; |
| 116 | imageInfo.pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices; |
| 117 | imageInfo.queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount; |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 118 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 119 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 120 | VkMemoryAllocateInfo allocInfo = {}; |
| 121 | allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 122 | allocInfo.allocationSize = 0; |
| 123 | allocInfo.memoryTypeIndex = 0; |
| 124 | |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 125 | VkResult status; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 126 | for(uint32_t i = 0; i < imageCount; i++) |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 127 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 128 | PresentImage ¤tImage = images[i]; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 129 | |
| 130 | status = currentImage.allocateImage(device, imageInfo); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 131 | if(status != VK_SUCCESS) |
| 132 | { |
| 133 | return status; |
| 134 | } |
| 135 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 136 | allocInfo.allocationSize = currentImage.getImage()->getMemoryRequirements().size; |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 137 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 138 | status = currentImage.allocateAndBindImageMemory(device, allocInfo); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 139 | if(status != VK_SUCCESS) |
| 140 | { |
| 141 | return status; |
| 142 | } |
| 143 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 144 | surface->attachImage(¤tImage); |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return VK_SUCCESS; |
| 148 | } |
| 149 | |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 150 | uint32_t SwapchainKHR::getImageCount() const |
| 151 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 152 | return imageCount; |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | VkResult SwapchainKHR::getImages(uint32_t *pSwapchainImageCount, VkImage *pSwapchainImages) const |
| 156 | { |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 157 | uint32_t i; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 158 | for(i = 0; i < std::min(*pSwapchainImageCount, imageCount); i++) |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 159 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 160 | pSwapchainImages[i] = images[i].asVkImage(); |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | *pSwapchainImageCount = i; |
| 164 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 165 | if(*pSwapchainImageCount < imageCount) |
Hernan Liatis | d00375a | 2019-02-26 11:19:27 -0800 | [diff] [blame] | 166 | { |
| 167 | return VK_INCOMPLETE; |
| 168 | } |
| 169 | |
| 170 | return VK_SUCCESS; |
| 171 | } |
| 172 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 173 | VkResult SwapchainKHR::getNextImage(uint64_t timeout, Semaphore *semaphore, Fence *fence, uint32_t *pImageIndex) |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 174 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 175 | for(uint32_t i = 0; i < imageCount; i++) |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 176 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 177 | PresentImage ¤tImage = images[i]; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 178 | if(currentImage.isAvailable()) |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 179 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 180 | currentImage.setStatus(DRAWING); |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 181 | *pImageIndex = i; |
| 182 | |
| 183 | if(semaphore) |
| 184 | { |
Alexis Hetu | 7d96f51 | 2019-06-13 18:23:56 -0400 | [diff] [blame] | 185 | semaphore->signal(); |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if(fence) |
| 189 | { |
Alexis Hetu | 7d96f51 | 2019-06-13 18:23:56 -0400 | [diff] [blame] | 190 | fence->complete(); |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return VK_SUCCESS; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return VK_NOT_READY; |
| 198 | } |
| 199 | |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 200 | VkResult SwapchainKHR::present(uint32_t index) |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 201 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 202 | auto &image = images[index]; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 203 | image.setStatus(PRESENTING); |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 204 | VkResult result = surface->present(&image); |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 205 | image.setStatus(AVAILABLE); |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 206 | |
| 207 | if(retired) |
| 208 | { |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 209 | surface->detachImage(&image); |
| 210 | image.clear(); |
Hernan Liatis | 43be716 | 2019-03-08 17:57:41 -0800 | [diff] [blame] | 211 | } |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 212 | |
| 213 | return result; |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 216 | } // namespace vk |