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