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