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