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