jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
Greg Daniel | e79b473 | 2017-04-20 14:07:46 -0400 | [diff] [blame] | 9 | #include "GrBackendSurface.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 11 | #include "SkAutoMalloc.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 12 | #include "SkSurface.h" |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 13 | #include "VulkanWindowContext.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 14 | |
Greg Daniel | a782dcb | 2018-06-28 20:18:59 +0000 | [diff] [blame] | 15 | #include "vk/GrVkExtensions.h" |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 16 | #include "vk/GrVkImage.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | #include "vk/GrVkUtil.h" |
| 18 | #include "vk/GrVkTypes.h" |
| 19 | |
| 20 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
| 21 | // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW |
| 22 | #undef CreateSemaphore |
| 23 | #endif |
| 24 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 25 | #define GET_PROC(F) f ## F = (PFN_vk ## F) fGetInstanceProcAddr(fInstance, "vk" #F) |
| 26 | #define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) fGetDeviceProcAddr(fDevice, "vk" #F) |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 27 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 28 | namespace sk_app { |
| 29 | |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 30 | VulkanWindowContext::VulkanWindowContext(const DisplayParams& params, |
| 31 | CreateVkSurfaceFn createVkSurface, |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 32 | CanPresentFn canPresent, |
| 33 | PFN_vkGetInstanceProcAddr instProc, |
| 34 | PFN_vkGetDeviceProcAddr devProc) |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 35 | : WindowContext(params) |
| 36 | , fCreateVkSurfaceFn(createVkSurface) |
| 37 | , fCanPresentFn(canPresent) |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 38 | , fSurface(VK_NULL_HANDLE) |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 39 | , fSwapchain(VK_NULL_HANDLE) |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 40 | , fImages(nullptr) |
| 41 | , fImageLayouts(nullptr) |
| 42 | , fSurfaces(nullptr) |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 43 | , fCommandPool(VK_NULL_HANDLE) |
| 44 | , fBackbuffers(nullptr) { |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 45 | fGetInstanceProcAddr = instProc; |
| 46 | fGetDeviceProcAddr = devProc; |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 47 | this->initializeContext(); |
| 48 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 49 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 50 | void VulkanWindowContext::initializeContext() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 51 | // any config code here (particularly for msaa)? |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 52 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 53 | GrVkBackendContext backendContext; |
| 54 | if (!sk_gpu_test::CreateVkBackendContext(fGetInstanceProcAddr, fGetDeviceProcAddr, |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 55 | &backendContext, &fDebugCallback, |
| 56 | &fPresentQueueIndex, fCanPresentFn)) { |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
Greg Daniel | a782dcb | 2018-06-28 20:18:59 +0000 | [diff] [blame] | 60 | if (!backendContext.fInterface->fExtensions.hasExtension(VK_KHR_SURFACE_EXTENSION_NAME) || |
| 61 | !backendContext.fInterface->fExtensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) { |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | fInstance = backendContext.fInstance; |
| 66 | fPhysicalDevice = backendContext.fPhysicalDevice; |
| 67 | fDevice = backendContext.fDevice; |
| 68 | fGraphicsQueueIndex = backendContext.fGraphicsQueueIndex; |
| 69 | fGraphicsQueue = backendContext.fQueue; |
| 70 | fInterface = backendContext.fInterface; |
| 71 | |
| 72 | GET_PROC(DestroyInstance); |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 73 | GET_PROC(DestroySurfaceKHR); |
| 74 | GET_PROC(GetPhysicalDeviceSurfaceSupportKHR); |
| 75 | GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 76 | GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR); |
| 77 | GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 78 | GET_DEV_PROC(DeviceWaitIdle); |
| 79 | GET_DEV_PROC(QueueWaitIdle); |
| 80 | GET_DEV_PROC(DestroyDevice); |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 81 | GET_DEV_PROC(CreateSwapchainKHR); |
| 82 | GET_DEV_PROC(DestroySwapchainKHR); |
| 83 | GET_DEV_PROC(GetSwapchainImagesKHR); |
| 84 | GET_DEV_PROC(AcquireNextImageKHR); |
| 85 | GET_DEV_PROC(QueuePresentKHR); |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 86 | GET_DEV_PROC(GetDeviceQueue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 87 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 88 | fContext = GrContext::MakeVulkan(backendContext, fDisplayParams.fGrContextOptions); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 89 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 90 | fSurface = fCreateVkSurfaceFn(fInstance); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 91 | if (VK_NULL_HANDLE == fSurface) { |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 92 | this->destroyContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 96 | VkBool32 supported; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 97 | VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fPhysicalDevice, fPresentQueueIndex, |
| 98 | fSurface, &supported); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 99 | if (VK_SUCCESS != res) { |
| 100 | this->destroyContext(); |
| 101 | return; |
| 102 | } |
| 103 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 104 | if (!this->createSwapchain(-1, -1, fDisplayParams)) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 105 | this->destroyContext(); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // create presentQueue |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 110 | fGetDeviceQueue(fDevice, fPresentQueueIndex, 0, &fPresentQueue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 111 | } |
| 112 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 113 | bool VulkanWindowContext::createSwapchain(int width, int height, |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 114 | const DisplayParams& params) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 115 | // check for capabilities |
| 116 | VkSurfaceCapabilitiesKHR caps; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 117 | VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fSurface, &caps); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 118 | if (VK_SUCCESS != res) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | uint32_t surfaceFormatCount; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 123 | res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount, |
| 124 | nullptr); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 125 | if (VK_SUCCESS != res) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR)); |
| 130 | VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get(); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 131 | res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount, |
| 132 | surfaceFormats); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 133 | if (VK_SUCCESS != res) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | uint32_t presentModeCount; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 138 | res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount, |
| 139 | nullptr); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 140 | if (VK_SUCCESS != res) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR)); |
| 145 | VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get(); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 146 | res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount, |
| 147 | presentModes); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 148 | if (VK_SUCCESS != res) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | VkExtent2D extent = caps.currentExtent; |
| 153 | // use the hints |
| 154 | if (extent.width == (uint32_t)-1) { |
| 155 | extent.width = width; |
| 156 | extent.height = height; |
| 157 | } |
| 158 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 159 | // clamp width; to protect us from broken hints |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 160 | if (extent.width < caps.minImageExtent.width) { |
| 161 | extent.width = caps.minImageExtent.width; |
| 162 | } else if (extent.width > caps.maxImageExtent.width) { |
| 163 | extent.width = caps.maxImageExtent.width; |
| 164 | } |
| 165 | // clamp height |
| 166 | if (extent.height < caps.minImageExtent.height) { |
| 167 | extent.height = caps.minImageExtent.height; |
| 168 | } else if (extent.height > caps.maxImageExtent.height) { |
| 169 | extent.height = caps.maxImageExtent.height; |
| 170 | } |
jvanverth | 1d15596 | 2016-05-23 13:13:36 -0700 | [diff] [blame] | 171 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 172 | fWidth = (int)extent.width; |
| 173 | fHeight = (int)extent.height; |
| 174 | |
| 175 | uint32_t imageCount = caps.minImageCount + 2; |
| 176 | if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) { |
| 177 | // Application must settle for fewer images than desired: |
| 178 | imageCount = caps.maxImageCount; |
| 179 | } |
| 180 | |
| 181 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 182 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 183 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 184 | SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); |
| 185 | SkASSERT(caps.supportedTransforms & caps.currentTransform); |
| 186 | SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | |
| 187 | VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)); |
| 188 | VkCompositeAlphaFlagBitsKHR composite_alpha = |
| 189 | (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ? |
| 190 | VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR : |
| 191 | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 192 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 193 | // Pick our surface format. For now, just make sure it matches our sRGB request: |
| 194 | VkFormat surfaceFormat = VK_FORMAT_UNDEFINED; |
| 195 | VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 196 | for (uint32_t i = 0; i < surfaceFormatCount; ++i) { |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 197 | VkFormat localFormat = surfaceFormats[i].format; |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 198 | if (GrVkFormatIsSupported(localFormat)) { |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 199 | surfaceFormat = localFormat; |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 200 | colorSpace = surfaceFormats[i].colorSpace; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | fDisplayParams = params; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 205 | fSampleCount = params.fMSAASampleCount; |
| 206 | fStencilBits = 8; |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 207 | |
| 208 | if (VK_FORMAT_UNDEFINED == surfaceFormat) { |
| 209 | return false; |
| 210 | } |
jvanverth | a4b0fed | 2016-04-27 11:42:21 -0700 | [diff] [blame] | 211 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 212 | SkColorType colorType; |
| 213 | switch (surfaceFormat) { |
| 214 | case VK_FORMAT_R8G8B8A8_UNORM: // fall through |
| 215 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 216 | colorType = kRGBA_8888_SkColorType; |
| 217 | break; |
| 218 | case VK_FORMAT_B8G8R8A8_UNORM: // fall through |
| 219 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 220 | colorType = kBGRA_8888_SkColorType; |
| 221 | break; |
| 222 | default: |
| 223 | return false; |
| 224 | } |
| 225 | |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 226 | // If mailbox mode is available, use it, as it is the lowest-latency non- |
| 227 | // tearing mode. If not, fall back to FIFO which is always available. |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 228 | VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 229 | for (uint32_t i = 0; i < presentModeCount; ++i) { |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 230 | // use mailbox |
| 231 | if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 232 | mode = presentModes[i]; |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 233 | break; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | VkSwapchainCreateInfoKHR swapchainCreateInfo; |
| 238 | memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR)); |
| 239 | swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 240 | swapchainCreateInfo.surface = fSurface; |
| 241 | swapchainCreateInfo.minImageCount = imageCount; |
jvanverth | a4b0fed | 2016-04-27 11:42:21 -0700 | [diff] [blame] | 242 | swapchainCreateInfo.imageFormat = surfaceFormat; |
| 243 | swapchainCreateInfo.imageColorSpace = colorSpace; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 244 | swapchainCreateInfo.imageExtent = extent; |
| 245 | swapchainCreateInfo.imageArrayLayers = 1; |
| 246 | swapchainCreateInfo.imageUsage = usageFlags; |
| 247 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 248 | uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex }; |
| 249 | if (fGraphicsQueueIndex != fPresentQueueIndex) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 250 | swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; |
| 251 | swapchainCreateInfo.queueFamilyIndexCount = 2; |
| 252 | swapchainCreateInfo.pQueueFamilyIndices = queueFamilies; |
| 253 | } else { |
| 254 | swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 255 | swapchainCreateInfo.queueFamilyIndexCount = 0; |
| 256 | swapchainCreateInfo.pQueueFamilyIndices = nullptr; |
| 257 | } |
| 258 | |
Greg Daniel | e897b97 | 2016-10-06 13:57:57 -0400 | [diff] [blame] | 259 | swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 260 | swapchainCreateInfo.compositeAlpha = composite_alpha; |
| 261 | swapchainCreateInfo.presentMode = mode; |
| 262 | swapchainCreateInfo.clipped = true; |
| 263 | swapchainCreateInfo.oldSwapchain = fSwapchain; |
| 264 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 265 | res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo, nullptr, &fSwapchain); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 266 | if (VK_SUCCESS != res) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | // destroy the old swapchain |
| 271 | if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) { |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 272 | fDeviceWaitIdle(fDevice); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 273 | |
| 274 | this->destroyBuffers(); |
| 275 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 276 | fDestroySwapchainKHR(fDevice, swapchainCreateInfo.oldSwapchain, nullptr); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 279 | this->createBuffers(swapchainCreateInfo.imageFormat, colorType); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 284 | void VulkanWindowContext::createBuffers(VkFormat format, SkColorType colorType) { |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 285 | fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, nullptr); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 286 | SkASSERT(fImageCount); |
| 287 | fImages = new VkImage[fImageCount]; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 288 | fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, fImages); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 289 | |
| 290 | // set up initial image layouts and create surfaces |
| 291 | fImageLayouts = new VkImageLayout[fImageCount]; |
| 292 | fSurfaces = new sk_sp<SkSurface>[fImageCount]; |
| 293 | for (uint32_t i = 0; i < fImageCount; ++i) { |
| 294 | fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED; |
| 295 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 296 | GrVkImageInfo info; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 297 | info.fImage = fImages[i]; |
Greg Daniel | 8385a8a | 2018-02-26 13:29:37 -0500 | [diff] [blame] | 298 | info.fAlloc = GrVkAlloc(); |
egdaniel | 580fa59 | 2016-08-31 11:03:50 -0700 | [diff] [blame] | 299 | info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 300 | info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 301 | info.fFormat = format; |
jvanverth | 3622a17 | 2016-05-10 06:42:18 -0700 | [diff] [blame] | 302 | info.fLevelCount = 1; |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 303 | |
Brian Salomon | afdc6b1 | 2018-03-09 12:02:32 -0500 | [diff] [blame] | 304 | GrBackendRenderTarget backendRT(fWidth, fHeight, fSampleCount, info); |
Greg Daniel | e79b473 | 2017-04-20 14:07:46 -0400 | [diff] [blame] | 305 | |
Brian Salomon | afdc6b1 | 2018-03-09 12:02:32 -0500 | [diff] [blame] | 306 | fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext.get(), |
| 307 | backendRT, |
| 308 | kTopLeft_GrSurfaceOrigin, |
| 309 | colorType, |
| 310 | fDisplayParams.fColorSpace, |
Ben Wagner | 37c5403 | 2018-04-13 14:30:23 -0400 | [diff] [blame] | 311 | &fDisplayParams.fSurfaceProps); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // create the command pool for the command buffers |
| 315 | if (VK_NULL_HANDLE == fCommandPool) { |
| 316 | VkCommandPoolCreateInfo commandPoolInfo; |
| 317 | memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); |
| 318 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 319 | // this needs to be on the render queue |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 320 | commandPoolInfo.queueFamilyIndex = fGraphicsQueueIndex; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 321 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 322 | GR_VK_CALL_ERRCHECK(fInterface, |
| 323 | CreateCommandPool(fDevice, &commandPoolInfo, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 324 | nullptr, &fCommandPool)); |
| 325 | } |
| 326 | |
| 327 | // set up the backbuffers |
| 328 | VkSemaphoreCreateInfo semaphoreInfo; |
| 329 | memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo)); |
| 330 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 331 | semaphoreInfo.pNext = nullptr; |
| 332 | semaphoreInfo.flags = 0; |
| 333 | VkCommandBufferAllocateInfo commandBuffersInfo; |
| 334 | memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo)); |
| 335 | commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 336 | commandBuffersInfo.pNext = nullptr; |
| 337 | commandBuffersInfo.commandPool = fCommandPool; |
| 338 | commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 339 | commandBuffersInfo.commandBufferCount = 2; |
| 340 | VkFenceCreateInfo fenceInfo; |
| 341 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 342 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 343 | fenceInfo.pNext = nullptr; |
| 344 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
| 345 | |
Greg Daniel | 1f05f44 | 2016-10-27 16:37:17 -0400 | [diff] [blame] | 346 | // we create one additional backbuffer structure here, because we want to |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 347 | // give the command buffers they contain a chance to finish before we cycle back |
| 348 | fBackbuffers = new BackbufferInfo[fImageCount + 1]; |
| 349 | for (uint32_t i = 0; i < fImageCount + 1; ++i) { |
| 350 | fBackbuffers[i].fImageIndex = -1; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 351 | GR_VK_CALL_ERRCHECK(fInterface, |
| 352 | CreateSemaphore(fDevice, &semaphoreInfo, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 353 | nullptr, &fBackbuffers[i].fAcquireSemaphore)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 354 | GR_VK_CALL_ERRCHECK(fInterface, |
| 355 | CreateSemaphore(fDevice, &semaphoreInfo, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 356 | nullptr, &fBackbuffers[i].fRenderSemaphore)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 357 | GR_VK_CALL_ERRCHECK(fInterface, |
| 358 | AllocateCommandBuffers(fDevice, &commandBuffersInfo, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 359 | fBackbuffers[i].fTransitionCmdBuffers)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 360 | GR_VK_CALL_ERRCHECK(fInterface, |
| 361 | CreateFence(fDevice, &fenceInfo, nullptr, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 362 | &fBackbuffers[i].fUsageFences[0])); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 363 | GR_VK_CALL_ERRCHECK(fInterface, |
| 364 | CreateFence(fDevice, &fenceInfo, nullptr, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 365 | &fBackbuffers[i].fUsageFences[1])); |
| 366 | } |
| 367 | fCurrentBackbufferIndex = fImageCount; |
| 368 | } |
| 369 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 370 | void VulkanWindowContext::destroyBuffers() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 371 | |
| 372 | if (fBackbuffers) { |
| 373 | for (uint32_t i = 0; i < fImageCount + 1; ++i) { |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 374 | GR_VK_CALL_ERRCHECK(fInterface, |
| 375 | WaitForFences(fDevice, 2, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 376 | fBackbuffers[i].fUsageFences, |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 377 | true, UINT64_MAX)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 378 | fBackbuffers[i].fImageIndex = -1; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 379 | GR_VK_CALL(fInterface, |
| 380 | DestroySemaphore(fDevice, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 381 | fBackbuffers[i].fAcquireSemaphore, |
| 382 | nullptr)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 383 | GR_VK_CALL(fInterface, |
| 384 | DestroySemaphore(fDevice, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 385 | fBackbuffers[i].fRenderSemaphore, |
| 386 | nullptr)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 387 | GR_VK_CALL(fInterface, |
| 388 | FreeCommandBuffers(fDevice, fCommandPool, 2, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 389 | fBackbuffers[i].fTransitionCmdBuffers)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 390 | GR_VK_CALL(fInterface, |
| 391 | DestroyFence(fDevice, fBackbuffers[i].fUsageFences[0], 0)); |
| 392 | GR_VK_CALL(fInterface, |
| 393 | DestroyFence(fDevice, fBackbuffers[i].fUsageFences[1], 0)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | delete[] fBackbuffers; |
| 398 | fBackbuffers = nullptr; |
| 399 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 400 | // Does this actually free the surfaces? |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 401 | delete[] fSurfaces; |
| 402 | fSurfaces = nullptr; |
| 403 | delete[] fImageLayouts; |
| 404 | fImageLayouts = nullptr; |
| 405 | delete[] fImages; |
| 406 | fImages = nullptr; |
| 407 | } |
| 408 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 409 | VulkanWindowContext::~VulkanWindowContext() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 410 | this->destroyContext(); |
| 411 | } |
| 412 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 413 | void VulkanWindowContext::destroyContext() { |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 414 | if (this->isValid()) { |
| 415 | fQueueWaitIdle(fPresentQueue); |
| 416 | fDeviceWaitIdle(fDevice); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 417 | |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 418 | this->destroyBuffers(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 419 | |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 420 | if (VK_NULL_HANDLE != fCommandPool) { |
| 421 | GR_VK_CALL(fInterface, DestroyCommandPool(fDevice, fCommandPool, nullptr)); |
| 422 | fCommandPool = VK_NULL_HANDLE; |
| 423 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 424 | |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 425 | if (VK_NULL_HANDLE != fSwapchain) { |
| 426 | fDestroySwapchainKHR(fDevice, fSwapchain, nullptr); |
| 427 | fSwapchain = VK_NULL_HANDLE; |
| 428 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 429 | |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 430 | if (VK_NULL_HANDLE != fSurface) { |
| 431 | fDestroySurfaceKHR(fInstance, fSurface, nullptr); |
| 432 | fSurface = VK_NULL_HANDLE; |
| 433 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 436 | fContext.reset(); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 437 | fInterface.reset(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 438 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 439 | if (VK_NULL_HANDLE != fDevice) { |
| 440 | fDestroyDevice(fDevice, nullptr); |
| 441 | fDevice = VK_NULL_HANDLE; |
| 442 | } |
Greg Daniel | be0ab88 | 2018-06-28 17:34:28 +0000 | [diff] [blame] | 443 | |
| 444 | #ifdef SK_ENABLE_VK_LAYERS |
| 445 | if (fDebugCallback != VK_NULL_HANDLE) { |
| 446 | GR_VK_CALL(fInterface, DestroyDebugReportCallbackEXT(fInstance, fDebugCallback, |
| 447 | nullptr)); |
| 448 | } |
| 449 | #endif |
| 450 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 451 | fPhysicalDevice = VK_NULL_HANDLE; |
| 452 | |
| 453 | if (VK_NULL_HANDLE != fInstance) { |
| 454 | fDestroyInstance(fInstance, nullptr); |
| 455 | fInstance = VK_NULL_HANDLE; |
| 456 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 457 | } |
| 458 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 459 | VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 460 | SkASSERT(fBackbuffers); |
| 461 | |
| 462 | ++fCurrentBackbufferIndex; |
egdaniel | 804af7d | 2016-09-26 12:30:46 -0700 | [diff] [blame] | 463 | if (fCurrentBackbufferIndex > fImageCount) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 464 | fCurrentBackbufferIndex = 0; |
| 465 | } |
| 466 | |
| 467 | BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 468 | GR_VK_CALL_ERRCHECK(fInterface, |
| 469 | WaitForFences(fDevice, 2, backbuffer->fUsageFences, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 470 | true, UINT64_MAX)); |
| 471 | return backbuffer; |
| 472 | } |
| 473 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 474 | sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 475 | BackbufferInfo* backbuffer = this->getAvailableBackbuffer(); |
| 476 | SkASSERT(backbuffer); |
| 477 | |
| 478 | // reset the fence |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 479 | GR_VK_CALL_ERRCHECK(fInterface, |
| 480 | ResetFences(fDevice, 2, backbuffer->fUsageFences)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 481 | // semaphores should be in unsignaled state |
| 482 | |
| 483 | // acquire the image |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 484 | VkResult res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX, |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 485 | backbuffer->fAcquireSemaphore, VK_NULL_HANDLE, |
| 486 | &backbuffer->fImageIndex); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 487 | if (VK_ERROR_SURFACE_LOST_KHR == res) { |
| 488 | // need to figure out how to create a new vkSurface without the platformData* |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 489 | // maybe use attach somehow? but need a Window |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 490 | return nullptr; |
| 491 | } |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 492 | if (VK_ERROR_OUT_OF_DATE_KHR == res) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 493 | // tear swapchain down and try again |
Greg Daniel | 1f05f44 | 2016-10-27 16:37:17 -0400 | [diff] [blame] | 494 | if (!this->createSwapchain(-1, -1, fDisplayParams)) { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 495 | return nullptr; |
| 496 | } |
Jim Van Verth | d63c102 | 2017-01-05 13:50:49 -0500 | [diff] [blame] | 497 | backbuffer = this->getAvailableBackbuffer(); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 498 | GR_VK_CALL_ERRCHECK(fInterface, |
| 499 | ResetFences(fDevice, 2, backbuffer->fUsageFences)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 500 | |
| 501 | // acquire the image |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 502 | res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX, |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 503 | backbuffer->fAcquireSemaphore, VK_NULL_HANDLE, |
| 504 | &backbuffer->fImageIndex); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 505 | |
| 506 | if (VK_SUCCESS != res) { |
| 507 | return nullptr; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // set up layout transfer from initial to color attachment |
| 512 | VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex]; |
egdaniel | 580fa59 | 2016-08-31 11:03:50 -0700 | [diff] [blame] | 513 | SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 514 | VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ? |
| 515 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT : |
| 516 | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 517 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 518 | VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ? |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 519 | 0 : VK_ACCESS_MEMORY_READ_BIT; |
| 520 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 521 | |
| 522 | VkImageMemoryBarrier imageMemoryBarrier = { |
| 523 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 524 | NULL, // pNext |
| 525 | srcAccessMask, // outputMask |
| 526 | dstAccessMask, // inputMask |
| 527 | layout, // oldLayout |
| 528 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout |
| 529 | fPresentQueueIndex, // srcQueueFamilyIndex |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 530 | fGraphicsQueueIndex, // dstQueueFamilyIndex |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 531 | fImages[backbuffer->fImageIndex], // image |
| 532 | { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange |
| 533 | }; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 534 | GR_VK_CALL_ERRCHECK(fInterface, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 535 | ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0)); |
| 536 | VkCommandBufferBeginInfo info; |
| 537 | memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); |
| 538 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 539 | info.flags = 0; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 540 | GR_VK_CALL_ERRCHECK(fInterface, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 541 | BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info)); |
| 542 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 543 | GR_VK_CALL(fInterface, |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 544 | CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0], |
| 545 | srcStageMask, dstStageMask, 0, |
| 546 | 0, nullptr, |
| 547 | 0, nullptr, |
| 548 | 1, &imageMemoryBarrier)); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 549 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 550 | GR_VK_CALL_ERRCHECK(fInterface, |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 551 | EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0])); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 552 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 553 | VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 554 | // insert the layout transfer into the queue and wait on the acquire |
| 555 | VkSubmitInfo submitInfo; |
| 556 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 557 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 558 | submitInfo.waitSemaphoreCount = 1; |
| 559 | submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 560 | submitInfo.pWaitDstStageMask = &waitDstStageFlags; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 561 | submitInfo.commandBufferCount = 1; |
| 562 | submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0]; |
| 563 | submitInfo.signalSemaphoreCount = 0; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 564 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 565 | GR_VK_CALL_ERRCHECK(fInterface, |
| 566 | QueueSubmit(fGraphicsQueue, 1, &submitInfo, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 567 | backbuffer->fUsageFences[0])); |
| 568 | |
egdaniel | 580fa59 | 2016-08-31 11:03:50 -0700 | [diff] [blame] | 569 | SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get(); |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 570 | GrBackendRenderTarget backendRT = surface->getBackendRenderTarget( |
| 571 | SkSurface::kFlushRead_BackendHandleAccess); |
| 572 | backendRT.setVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 573 | |
egdaniel | 580fa59 | 2016-08-31 11:03:50 -0700 | [diff] [blame] | 574 | |
| 575 | return sk_ref_sp(surface); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 576 | } |
| 577 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 578 | void VulkanWindowContext::swapBuffers() { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 579 | |
| 580 | BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; |
egdaniel | 580fa59 | 2016-08-31 11:03:50 -0700 | [diff] [blame] | 581 | SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 582 | |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 583 | GrBackendRenderTarget backendRT = surface->getBackendRenderTarget( |
| 584 | SkSurface::kFlushRead_BackendHandleAccess); |
| 585 | GrVkImageInfo imageInfo; |
| 586 | SkAssertResult(backendRT.getVkImageInfo(&imageInfo)); |
| 587 | // Check to make sure we never change the actually wrapped image |
| 588 | SkASSERT(imageInfo.fImage == fImages[backbuffer->fImageIndex]); |
| 589 | |
| 590 | VkImageLayout layout = imageInfo.fImageLayout; |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 591 | VkPipelineStageFlags srcStageMask = GrVkImage::LayoutToPipelineStageFlags(layout); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 592 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 593 | VkAccessFlags srcAccessMask = GrVkImage::LayoutToSrcAccessMask(layout); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 594 | VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; |
| 595 | |
| 596 | VkImageMemoryBarrier imageMemoryBarrier = { |
| 597 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 598 | NULL, // pNext |
| 599 | srcAccessMask, // outputMask |
| 600 | dstAccessMask, // inputMask |
| 601 | layout, // oldLayout |
| 602 | VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 603 | fGraphicsQueueIndex, // srcQueueFamilyIndex |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 604 | fPresentQueueIndex, // dstQueueFamilyIndex |
| 605 | fImages[backbuffer->fImageIndex], // image |
| 606 | { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange |
| 607 | }; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 608 | GR_VK_CALL_ERRCHECK(fInterface, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 609 | ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0)); |
| 610 | VkCommandBufferBeginInfo info; |
| 611 | memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); |
| 612 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 613 | info.flags = 0; |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 614 | GR_VK_CALL_ERRCHECK(fInterface, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 615 | BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 616 | GR_VK_CALL(fInterface, |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 617 | CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1], |
| 618 | srcStageMask, dstStageMask, 0, |
| 619 | 0, nullptr, |
| 620 | 0, nullptr, |
| 621 | 1, &imageMemoryBarrier)); |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 622 | GR_VK_CALL_ERRCHECK(fInterface, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 623 | EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1])); |
| 624 | |
| 625 | fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
| 626 | |
| 627 | // insert the layout transfer into the queue and wait on the acquire |
| 628 | VkSubmitInfo submitInfo; |
| 629 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 630 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 631 | submitInfo.waitSemaphoreCount = 0; |
| 632 | submitInfo.pWaitDstStageMask = 0; |
| 633 | submitInfo.commandBufferCount = 1; |
| 634 | submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1]; |
| 635 | submitInfo.signalSemaphoreCount = 1; |
| 636 | submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore; |
| 637 | |
Greg Daniel | 0db3a88 | 2018-06-28 16:47:48 +0000 | [diff] [blame] | 638 | GR_VK_CALL_ERRCHECK(fInterface, |
| 639 | QueueSubmit(fGraphicsQueue, 1, &submitInfo, |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 640 | backbuffer->fUsageFences[1])); |
| 641 | |
| 642 | // Submit present operation to present queue |
| 643 | const VkPresentInfoKHR presentInfo = |
| 644 | { |
| 645 | VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType |
| 646 | NULL, // pNext |
| 647 | 1, // waitSemaphoreCount |
| 648 | &backbuffer->fRenderSemaphore, // pWaitSemaphores |
| 649 | 1, // swapchainCount |
| 650 | &fSwapchain, // pSwapchains |
| 651 | &backbuffer->fImageIndex, // pImageIndices |
| 652 | NULL // pResults |
| 653 | }; |
| 654 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 655 | fQueuePresentKHR(fPresentQueue, &presentInfo); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 656 | } |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 657 | |
| 658 | } //namespace sk_app |