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