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