Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "../../src/gpu/vk/GrVkMemory.h" | |
|
bsalomon
2016/04/05 14:24:43
You could probably update the gyp file to have src
jvanverth1
2016/04/05 18:16:49
Done.
| |
| 14 #include "vk/GrVkInterface.h" | |
| 15 #include "../../src/gpu/vk/GrVkUtil.h" | |
| 16 #include "vk/GrVkTypes.h" | |
| 17 | |
| 18 #ifdef VK_USE_PLATFORM_WIN32_KHR | |
| 19 // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW | |
| 20 #undef CreateSemaphore | |
| 21 #endif | |
| 22 | |
| 23 VulkanTestContext::VulkanTestContext(void* platformData, int msaaSampleCount) | |
| 24 : fSurface(VK_NULL_HANDLE) | |
| 25 , fSwapchain(VK_NULL_HANDLE) | |
| 26 , fCommandPool(VK_NULL_HANDLE) | |
| 27 , fBackbuffers(nullptr) | |
| 28 { | |
| 29 | |
| 30 // any config code here (particularly for msaa)? | |
| 31 | |
| 32 this->initializeContext(platformData); | |
| 33 } | |
| 34 | |
| 35 void VulkanTestContext::initializeContext(void* platformData) { | |
| 36 | |
| 37 fBackendContext.reset(GrVkBackendContext::Create()); | |
| 38 fBackendContext->ref(); | |
| 39 | |
| 40 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext)fBackendCo ntext.get()); | |
| 41 | |
| 42 fSurface = createVkSurface(platformData); | |
| 43 if (VK_NULL_HANDLE == fSurface) { | |
| 44 fBackendContext.reset(nullptr); | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 // query to get the initial queue props size | |
| 49 uint32_t queueCount; | |
| 50 GR_VK_CALL(fBackendContext->fInterface, | |
| 51 GetPhysicalDeviceQueueFamilyProperties(fBackendContext->fPhysical Device, &queueCount, | |
| 52 nullptr)); | |
| 53 SkASSERT(queueCount >= 1); | |
| 54 | |
| 55 SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties)); | |
| 56 // now get the actual queue props | |
| 57 VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAl loc.get(); | |
| 58 | |
| 59 GR_VK_CALL(fBackendContext->fInterface, | |
| 60 GetPhysicalDeviceQueueFamilyProperties(fBackendContext->fPhysical Device, &queueCount, | |
| 61 queueProps)); | |
| 62 | |
| 63 // iterate to find the present queue | |
| 64 fPresentQueueIndex = -1; | |
| 65 for (uint32_t i = 0; i < queueCount; i++) { | |
| 66 if ((queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && canPresent(i)) { | |
| 67 fPresentQueueIndex = i; | |
| 68 break; | |
| 69 } | |
| 70 } | |
| 71 SkASSERT(fPresentQueueIndex < queueCount); | |
|
egdaniel
2016/04/05 16:50:28
do we want to assert that QueueIndex != -1 or >= 0
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 72 | |
| 73 VkBool32 supported; | |
| 74 VkResult res = GR_VK_CALL(fBackendContext->fInterface, | |
|
egdaniel
2016/04/05 16:50:28
this line is very hard to read what is happening.
jvanverth1
2016/04/05 18:16:49
Done.
| |
| 75 GetPhysicalDeviceSurfaceSupportKHR( | |
| 76 fBackendContext->fPhysicalDevice, | |
| 77 fPresentQueueIndex, | |
| 78 fSurface, | |
| 79 &supported)); | |
| 80 if (VK_SUCCESS != res) { | |
| 81 this->destroyContext(); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 // get this info from somewhere? | |
| 86 if (!this->createSwapchain(1024, 768)) { | |
| 87 this->destroyContext(); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 // create presentQueue | |
| 92 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQ ueue); | |
| 93 | |
| 94 | |
| 95 } | |
| 96 | |
| 97 bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height) | |
| 98 { | |
| 99 // check for capabilities | |
| 100 VkSurfaceCapabilitiesKHR caps; | |
| 101 VkResult res = GR_VK_CALL(fBackendContext->fInterface, | |
| 102 GetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendCo ntext->fPhysicalDevice, | |
|
egdaniel
2016/04/05 16:50:28
100 chars
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 103 fSurface, | |
| 104 &caps)); | |
| 105 if (VK_SUCCESS != res) { | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 uint32_t surfaceFormatCount; | |
| 110 res = GR_VK_CALL(fBackendContext->fInterface, | |
| 111 GetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysic alDevice, | |
| 112 fSurface, | |
| 113 &surfaceFormatCount, | |
|
egdaniel
2016/04/05 16:50:28
is it possible for this to return 0 surfaceFormatC
jvanverth1
2016/04/05 18:16:49
It has to support at least one surface format. So
| |
| 114 nullptr)); | |
| 115 if (VK_SUCCESS != res) { | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatK HR)); | |
| 120 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc .get(); | |
| 121 res = GR_VK_CALL(fBackendContext->fInterface, | |
| 122 GetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysic alDevice, | |
| 123 fSurface, | |
|
egdaniel
2016/04/05 16:50:28
align these with fBackendContext
jvanverth1
2016/04/05 18:16:48
I hate Visual Studio.
| |
| 124 &surfaceFormatCount, | |
| 125 surfaceFormats)); | |
| 126 if (VK_SUCCESS != res) { | |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 uint32_t presentModeCount; | |
| 131 res = GR_VK_CALL(fBackendContext->fInterface, | |
| 132 GetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fP hysicalDevice, | |
| 133 fSurface, | |
|
egdaniel
2016/04/05 16:50:28
align
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 134 &presentModeCount, | |
|
egdaniel
2016/04/05 16:50:28
again question on is a count of 0 bad?
jvanverth1
2016/04/05 18:16:48
Same as above.
| |
| 135 nullptr)); | |
| 136 if (VK_SUCCESS != res) { | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR)); | |
| 141 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get(); | |
| 142 res = GR_VK_CALL(fBackendContext->fInterface, | |
| 143 GetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fP hysicalDevice, | |
| 144 fSurface, | |
|
egdaniel
2016/04/05 16:50:28
align
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 145 &presentModeCount, | |
| 146 presentModes)); | |
| 147 if (VK_SUCCESS != res) { | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 VkExtent2D extent = caps.currentExtent; | |
| 152 // use the hints | |
| 153 if (extent.width == (uint32_t)-1) { | |
| 154 extent.width = width; | |
| 155 extent.height = height; | |
| 156 } | |
| 157 | |
| 158 // clamp width; to protect us from broken hints? | |
| 159 if (extent.width < caps.minImageExtent.width) { | |
| 160 extent.width = caps.minImageExtent.width; | |
| 161 } else if (extent.width > caps.maxImageExtent.width) { | |
| 162 extent.width = caps.maxImageExtent.width; | |
| 163 } | |
| 164 // clamp height | |
| 165 if (extent.height < caps.minImageExtent.height) { | |
| 166 extent.height = caps.minImageExtent.height; | |
| 167 } else if (extent.height > caps.maxImageExtent.height) { | |
| 168 extent.height = caps.maxImageExtent.height; | |
| 169 } | |
| 170 fWidth = (int)extent.width; | |
| 171 fHeight = (int)extent.height; | |
| 172 | |
| 173 // not sure what the point of this is | |
| 174 //if (ctx_.extent.width == extent.width && ctx_.extent.height == extent.heig ht) | |
|
egdaniel
2016/04/05 16:50:28
what is ctx referring to in this call?
jvanverth1
2016/04/05 18:16:48
Removed
| |
| 175 // return; | |
| 176 | |
| 177 uint32_t imageCount = caps.minImageCount + 2; | |
| 178 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) { | |
| 179 // Application must settle for fewer images than desired: | |
| 180 imageCount = caps.maxImageCount; | |
| 181 } | |
| 182 | |
| 183 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;// | | |
| 184 //VK_IMAGE_USAGE_TRANSFER_SRC_BIT | | |
|
egdaniel
2016/04/05 16:50:28
why are these commented out?
jvanverth1
2016/04/05 18:16:49
I was trying to track down why things weren't work
| |
| 185 //VK_IMAGE_USAGE_TRANSFER_DST_BIT; | |
| 186 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); | |
| 187 SkASSERT(caps.supportedTransforms & caps.currentTransform); | |
| 188 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | | |
| 189 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)); | |
|
egdaniel
2016/04/05 16:50:28
align
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 190 VkCompositeAlphaFlagBitsKHR composite_alpha = | |
| 191 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ? | |
| 192 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR : | |
| 193 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; | |
| 194 | |
| 195 // FIFO is the only mode universally supported | |
| 196 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; | |
| 197 bool vsync = false; | |
| 198 for (uint32_t i = 0; i < presentModeCount; ++i) { | |
| 199 if ((vsync && VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) || | |
| 200 (!vsync && VK_PRESENT_MODE_IMMEDIATE_KHR == presentModes[i])) { | |
| 201 mode = presentModes[i]; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 VkSwapchainCreateInfoKHR swapchainCreateInfo; | |
| 206 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR)); | |
| 207 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; | |
| 208 swapchainCreateInfo.surface = fSurface; | |
| 209 swapchainCreateInfo.minImageCount = imageCount; | |
| 210 swapchainCreateInfo.imageFormat = surfaceFormats[0].format; // for now , use the first one | |
|
egdaniel
2016/04/05 16:50:28
is this something we'll search through at some poi
jvanverth1
2016/04/05 18:16:48
Yes, this is where we'd search for sRGB support --
| |
| 211 swapchainCreateInfo.imageColorSpace = surfaceFormats[0].colorSpace; | |
| 212 swapchainCreateInfo.imageExtent = extent; | |
| 213 swapchainCreateInfo.imageArrayLayers = 1; | |
| 214 swapchainCreateInfo.imageUsage = usageFlags; | |
| 215 | |
| 216 uint32_t queueFamilies[] = { fBackendContext->fQueueFamilyIndex, fPresentQue ueIndex }; | |
| 217 if (fBackendContext->fQueueFamilyIndex != fPresentQueueIndex) { | |
| 218 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; | |
| 219 swapchainCreateInfo.queueFamilyIndexCount = 2; | |
| 220 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies; | |
| 221 } else { | |
| 222 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; | |
| 223 swapchainCreateInfo.queueFamilyIndexCount = 0; | |
| 224 swapchainCreateInfo.pQueueFamilyIndices = nullptr; | |
| 225 } | |
| 226 | |
| 227 swapchainCreateInfo.preTransform = caps.currentTransform;; | |
| 228 swapchainCreateInfo.compositeAlpha = composite_alpha; | |
| 229 swapchainCreateInfo.presentMode = mode; | |
| 230 swapchainCreateInfo.clipped = true; | |
| 231 swapchainCreateInfo.oldSwapchain = fSwapchain; | |
| 232 | |
| 233 res = GR_VK_CALL(fBackendContext->fInterface, | |
| 234 CreateSwapchainKHR(fBackendContext->fDevice, | |
| 235 &swapchainCreateInfo, nullptr, &fSwapcha in)); | |
| 236 if (VK_SUCCESS != res) { | |
| 237 return false; | |
| 238 } | |
| 239 | |
| 240 // destroy the old swapchain | |
| 241 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) { | |
| 242 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext-> fDevice)); | |
| 243 | |
| 244 this->destroyBuffers(); | |
| 245 | |
| 246 GR_VK_CALL(fBackendContext->fInterface, DestroySwapchainKHR(fBackendCont ext->fDevice, | |
| 247 swapchainCrea teInfo.oldSwapchain, | |
| 248 nullptr)); | |
| 249 } | |
| 250 | |
| 251 GrVkFormatToPixelConfig(swapchainCreateInfo.imageFormat, &fPixelConfig); | |
| 252 | |
| 253 this->createBuffers(); | |
| 254 | |
| 255 return true; | |
| 256 } | |
| 257 | |
| 258 void VulkanTestContext::createBuffers() { | |
| 259 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, GetSwapchainImagesKHR(fBack endContext->fDevice, | |
| 260 fSwap chain, | |
| 261 &fIma geCount, | |
| 262 nullp tr)); | |
| 263 SkASSERT(fImageCount); | |
| 264 fImages = new VkImage[fImageCount]; | |
| 265 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, GetSwapchainImagesKHR(fBack endContext->fDevice, | |
| 266 fSwap chain, | |
| 267 &fIma geCount, | |
| 268 fImag es)); | |
| 269 | |
| 270 // set up initial image layouts and create surfaces | |
| 271 fImageLayouts = new VkImageLayout[fImageCount]; | |
| 272 fSurfaces = new sk_sp<SkSurface>[fImageCount]; | |
| 273 for (uint32_t i = 0; i < fImageCount; ++i) { | |
| 274 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED; | |
| 275 | |
| 276 GrBackendRenderTargetDesc desc; | |
| 277 GrVkTextureInfo info; | |
| 278 info.fImage = fImages[i]; | |
| 279 info.fAlloc = nullptr; | |
| 280 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; | |
| 281 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; | |
| 282 desc.fWidth = fWidth; | |
| 283 desc.fHeight = fHeight; | |
| 284 desc.fConfig = fPixelConfig; | |
| 285 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | |
| 286 desc.fSampleCnt = 0; | |
| 287 desc.fStencilBits = 0; | |
| 288 desc.fRenderTargetHandle = (GrBackendObject) &info; | |
| 289 SkSurfaceProps props(0, kUnknown_SkPixelGeometry); | |
| 290 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &p rops); | |
| 291 } | |
| 292 | |
| 293 // create the command pool for the command buffers | |
| 294 if (VK_NULL_HANDLE == fCommandPool) { | |
| 295 VkCommandPoolCreateInfo commandPoolInfo; | |
| 296 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); | |
| 297 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; | |
| 298 // this needs to be on the render queue | |
| 299 commandPoolInfo.queueFamilyIndex = fBackendContext->fQueueFamilyIndex; | |
| 300 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; | |
| 301 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 302 CreateCommandPool(fBackendContext->fDevice, &command PoolInfo, | |
| 303 nullptr, &fCommandPool)); | |
| 304 } | |
| 305 | |
| 306 // set up the backbuffers | |
| 307 VkSemaphoreCreateInfo semaphoreInfo; | |
| 308 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo)); | |
| 309 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; | |
| 310 semaphoreInfo.pNext = nullptr; | |
| 311 semaphoreInfo.flags = 0; | |
| 312 VkCommandBufferAllocateInfo commandBuffersInfo; | |
| 313 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo)); | |
| 314 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; | |
| 315 commandBuffersInfo.pNext = nullptr; | |
| 316 commandBuffersInfo.commandPool = fCommandPool; | |
| 317 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; | |
| 318 commandBuffersInfo.commandBufferCount = 2; | |
| 319 VkFenceCreateInfo fenceInfo; | |
| 320 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); | |
| 321 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; | |
| 322 fenceInfo.pNext = nullptr; | |
| 323 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; | |
| 324 | |
| 325 fBackbuffers = new BackbufferInfo[fImageCount + 1]; | |
|
egdaniel
2016/04/05 16:50:28
comment on why the +1 here?
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 326 for (uint32_t i = 0; i < fImageCount + 1; ++i) { | |
| 327 fBackbuffers[i].fImageIndex = -1; | |
| 328 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 329 CreateSemaphore(fBackendContext->fDevice, &semaphore Info, | |
| 330 nullptr, &fBackbuffers[i].fAcquireSe maphore)); | |
| 331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 332 CreateSemaphore(fBackendContext->fDevice, &semaphore Info, | |
| 333 nullptr, &fBackbuffers[i].fRenderSem aphore)); | |
| 334 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 335 AllocateCommandBuffers(fBackendContext->fDevice, &co mmandBuffersInfo, | |
| 336 fBackbuffers[i].fTransitionCm dBuffers)); | |
| 337 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 338 CreateFence(fBackendContext->fDevice, &fenceInfo, nu llptr, | |
| 339 &fBackbuffers[i].fUsageFences[0])); | |
| 340 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 341 CreateFence(fBackendContext->fDevice, &fenceInfo, nu llptr, | |
| 342 &fBackbuffers[i].fUsageFences[1])); | |
| 343 } | |
| 344 fCurrentBackbufferIndex = fImageCount; | |
| 345 } | |
| 346 | |
| 347 void VulkanTestContext::destroyBuffers() { | |
| 348 | |
| 349 if (fBackbuffers) { | |
| 350 for (uint32_t i = 0; i < fImageCount + 1; ++i) { | |
| 351 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 352 WaitForFences(fBackendContext->fDevice, 2, | |
| 353 fBackbuffers[i].fUsageFences, | |
| 354 true, UINT64_MAX)); | |
| 355 fBackbuffers[i].fImageIndex = -1; | |
| 356 GR_VK_CALL(fBackendContext->fInterface, | |
| 357 DestroySemaphore(fBackendContext->fDevice, | |
| 358 fBackbuffers[i].fAcquireSemaphore, | |
| 359 nullptr)); | |
| 360 GR_VK_CALL(fBackendContext->fInterface, | |
| 361 DestroySemaphore(fBackendContext->fDevice, | |
| 362 fBackbuffers[i].fRenderSemaphore, | |
| 363 nullptr)); | |
| 364 GR_VK_CALL(fBackendContext->fInterface, | |
| 365 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool , 2, | |
| 366 fBackbuffers[i].fTransitionCmdBuffers) ); | |
| 367 GR_VK_CALL(fBackendContext->fInterface, | |
| 368 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fU sageFences[0], 0)); | |
| 369 GR_VK_CALL(fBackendContext->fInterface, | |
| 370 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fU sageFences[1], 0)); | |
| 371 } | |
| 372 } | |
| 373 | |
| 374 delete[] fBackbuffers; | |
| 375 fBackbuffers = nullptr; | |
| 376 | |
| 377 delete[] fSurfaces; //*** will this unref properly? | |
|
bsalomon
2016/04/05 14:24:43
I believe it will since it will call ~sk_skp
jvanverth1
2016/04/05 18:16:48
Done.
| |
| 378 fSurfaces = nullptr; | |
| 379 delete[] fImageLayouts; | |
| 380 fImageLayouts = nullptr; | |
| 381 delete[] fImages; | |
| 382 fImages = nullptr; | |
| 383 } | |
| 384 | |
| 385 VulkanTestContext::~VulkanTestContext() { | |
| 386 this->destroyContext(); | |
| 387 } | |
| 388 | |
| 389 void VulkanTestContext::destroyContext() { | |
| 390 if (!fBackendContext.get()) { | |
| 391 return; | |
| 392 } | |
| 393 | |
| 394 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDev ice)); | |
| 395 | |
| 396 this->destroyBuffers(); | |
| 397 | |
| 398 if (VK_NULL_HANDLE != fCommandPool) { | |
| 399 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendConte xt->fDevice, | |
| 400 fCommandPool, nullptr)); | |
| 401 fCommandPool = VK_NULL_HANDLE; | |
| 402 } | |
| 403 | |
| 404 if (VK_NULL_HANDLE != fSwapchain) { | |
| 405 GR_VK_CALL(fBackendContext->fInterface, DestroySwapchainKHR(fBackendCont ext->fDevice, | |
| 406 fSwapchain, nullptr)); | |
| 407 fSwapchain = VK_NULL_HANDLE; | |
| 408 } | |
| 409 | |
| 410 if (VK_NULL_HANDLE != fSurface) { | |
| 411 GR_VK_CALL(fBackendContext->fInterface, DestroySurfaceKHR(fBackendContex t->fInstance, | |
| 412 fSurface, null ptr)); | |
| 413 fSurface = VK_NULL_HANDLE; | |
| 414 } | |
| 415 | |
| 416 delete fContext; | |
| 417 | |
| 418 fBackendContext.reset(nullptr); | |
| 419 } | |
| 420 | |
| 421 VulkanTestContext::BackbufferInfo* VulkanTestContext::getAvailableBackbuffer() { | |
| 422 SkASSERT(fBackbuffers); | |
| 423 | |
| 424 ++fCurrentBackbufferIndex; | |
| 425 if (fCurrentBackbufferIndex > fImageCount) { | |
| 426 fCurrentBackbufferIndex = 0; | |
| 427 } | |
| 428 | |
| 429 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; | |
| 430 | |
| 431 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 432 WaitForFences(fBackendContext->fDevice, 2, backbuffer->f UsageFences, | |
| 433 true, UINT64_MAX)); | |
| 434 return backbuffer; | |
| 435 } | |
| 436 | |
| 437 SkSurface* VulkanTestContext::getBackbufferSurface() { | |
| 438 BackbufferInfo* backbuffer = this->getAvailableBackbuffer(); | |
| 439 SkASSERT(backbuffer); | |
| 440 | |
| 441 // reset the fence | |
| 442 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 443 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUs ageFences)); | |
| 444 // semaphores should be in unsignaled state | |
| 445 | |
| 446 // acquire the image | |
| 447 VkResult res = GR_VK_CALL(fBackendContext->fInterface, | |
| 448 AcquireNextImageKHR(fBackendContext->fDevice, | |
| 449 fSwapchain, | |
| 450 UINT64_MAX, | |
| 451 backbuffer->fAcquireSemaphore, | |
| 452 VK_NULL_HANDLE, | |
| 453 &backbuffer->fImageIndex)); | |
| 454 if (VK_ERROR_SURFACE_LOST_KHR == res) { | |
| 455 // need to figure out how to create a new vkSurface without the platform Data* | |
| 456 return nullptr; | |
| 457 } | |
| 458 if (VK_ERROR_OUT_OF_DATE_KHR == res || VK_ERROR_SURFACE_LOST_KHR == res) { | |
| 459 // tear swapchain down and try again | |
| 460 if (!this->createSwapchain(0, 0)) { | |
| 461 return nullptr; | |
| 462 } | |
| 463 | |
| 464 // acquire the image | |
| 465 res = GR_VK_CALL(fBackendContext->fInterface, | |
| 466 AcquireNextImageKHR(fBackendContext->fDevice, | |
| 467 fSwapchain, | |
| 468 UINT64_MAX, | |
| 469 backbuffer->fAcquireSemaphore, | |
| 470 VK_NULL_HANDLE, | |
| 471 &backbuffer->fImageIndex)); | |
| 472 | |
| 473 if (VK_SUCCESS != res) { | |
| 474 return nullptr; | |
| 475 } | |
| 476 } | |
| 477 | |
| 478 // set up layout transfer from initial to color attachment | |
| 479 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex]; | |
| 480 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(l ayout); | |
| 481 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPU T_BIT; | |
| 482 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); | |
| 483 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; | |
| 484 | |
| 485 VkImageMemoryBarrier imageMemoryBarrier = { | |
| 486 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType | |
| 487 NULL, // pNext | |
| 488 srcAccessMask, // outputMask | |
| 489 dstAccessMask, // inputMask | |
| 490 layout, // oldLayout | |
| 491 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout | |
| 492 fPresentQueueIndex, // srcQueueFamilyIndex | |
| 493 fBackendContext->fQueueFamilyIndex, // dstQueueFamilyIndex | |
| 494 fImages[backbuffer->fImageIndex], // image | |
| 495 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange | |
| 496 }; | |
| 497 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 498 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0)); | |
| 499 VkCommandBufferBeginInfo info; | |
| 500 memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); | |
| 501 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; | |
| 502 info.flags = 0; | |
| 503 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 504 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info)); | |
| 505 | |
| 506 GR_VK_CALL(fBackendContext->fInterface, | |
| 507 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0], | |
| 508 srcStageMask, dstStageMask, 0, | |
| 509 0, nullptr, | |
| 510 0, nullptr, | |
| 511 1, &imageMemoryBarrier)); | |
| 512 | |
| 513 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | |
| 514 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0])); | |
| 515 | |
| 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; | |
| 522 submitInfo.pWaitDstStageMask = 0; | |
| 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_OUTPU T_BIT; | |
| 541 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; | |
| 542 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); | |
| 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 } | |
| OLD | NEW |