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