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