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