blob: bc200de42704a57ece5760bf082b1bbdf5490a02 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001
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"
jvanverthaf236b52016-05-20 06:01:06 -070010#include "GrRenderTarget.h"
jvanverth9f372462016-04-06 06:08:59 -070011#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070012#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070013
14#include "vk/GrVkInterface.h"
15#include "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
jvanverthb0d43522016-04-21 11:46:23 -070023#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
24#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
25
jvanvertha8d0d6c2016-05-05 12:32:03 -070026namespace sk_app {
27
brianosman05de2162016-05-06 13:28:57 -070028VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams& params)
jvanverthaf236b52016-05-20 06:01:06 -070029 : WindowContext()
30 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070031 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070032 , fImages(nullptr)
33 , fImageLayouts(nullptr)
34 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070035 , fCommandPool(VK_NULL_HANDLE)
36 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070037
38 // any config code here (particularly for msaa)?
39
brianosman05de2162016-05-06 13:28:57 -070040 this->initializeContext(platformData, params);
jvanverth9f372462016-04-06 06:08:59 -070041}
42
brianosman05de2162016-05-06 13:28:57 -070043void VulkanWindowContext::initializeContext(void* platformData, const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070044
jvanverthb0d43522016-04-21 11:46:23 -070045 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
46 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
47 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
48 fBackendContext.reset(nullptr);
49 return;
50 }
51
52 VkInstance instance = fBackendContext->fInstance;
53 VkDevice device = fBackendContext->fDevice;
54 GET_PROC(DestroySurfaceKHR);
55 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
57 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
58 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
59 GET_DEV_PROC(CreateSwapchainKHR);
60 GET_DEV_PROC(DestroySwapchainKHR);
61 GET_DEV_PROC(GetSwapchainImagesKHR);
62 GET_DEV_PROC(AcquireNextImageKHR);
63 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070064
jvanvertha8d0d6c2016-05-05 12:32:03 -070065 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070066
jvanverthb0d43522016-04-21 11:46:23 -070067 fSurface = createVkSurface(instance, platformData);
jvanverth9f372462016-04-06 06:08:59 -070068 if (VK_NULL_HANDLE == fSurface) {
69 fBackendContext.reset(nullptr);
70 return;
71 }
72
jvanverth9f372462016-04-06 06:08:59 -070073 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070074 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
75 fPresentQueueIndex, fSurface,
76 &supported);
jvanverth9f372462016-04-06 06:08:59 -070077 if (VK_SUCCESS != res) {
78 this->destroyContext();
79 return;
80 }
81
brianosman05de2162016-05-06 13:28:57 -070082 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070083 this->destroyContext();
84 return;
85 }
86
87 // create presentQueue
88 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070089}
90
brianosman05de2162016-05-06 13:28:57 -070091bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height,
92 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070093 // check for capabilities
94 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070095 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
96 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070097 if (VK_SUCCESS != res) {
98 return false;
99 }
100
101 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700102 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
103 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700104 if (VK_SUCCESS != res) {
105 return false;
106 }
107
108 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
109 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700110 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
111 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700112 if (VK_SUCCESS != res) {
113 return false;
114 }
115
116 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700117 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
118 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700119 if (VK_SUCCESS != res) {
120 return false;
121 }
122
123 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
124 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700125 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700126 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700127 if (VK_SUCCESS != res) {
128 return false;
129 }
130
131 VkExtent2D extent = caps.currentExtent;
132 // use the hints
133 if (extent.width == (uint32_t)-1) {
134 extent.width = width;
135 extent.height = height;
136 }
137
jvanverth9fab59d2016-04-06 12:08:51 -0700138 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700139 if (extent.width < caps.minImageExtent.width) {
140 extent.width = caps.minImageExtent.width;
141 } else if (extent.width > caps.maxImageExtent.width) {
142 extent.width = caps.maxImageExtent.width;
143 }
144 // clamp height
145 if (extent.height < caps.minImageExtent.height) {
146 extent.height = caps.minImageExtent.height;
147 } else if (extent.height > caps.maxImageExtent.height) {
148 extent.height = caps.maxImageExtent.height;
149 }
150 fWidth = (int)extent.width;
151 fHeight = (int)extent.height;
152
153 uint32_t imageCount = caps.minImageCount + 2;
154 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
155 // Application must settle for fewer images than desired:
156 imageCount = caps.maxImageCount;
157 }
158
159 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
160 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
161 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
162 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
163 SkASSERT(caps.supportedTransforms & caps.currentTransform);
164 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
165 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
166 VkCompositeAlphaFlagBitsKHR composite_alpha =
167 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
168 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
169 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
170
brianosman05de2162016-05-06 13:28:57 -0700171 // Pick our surface format. For now, just make sure it matches our sRGB request:
172 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
173 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
174 bool wantSRGB = kSRGB_SkColorProfileType == params.fProfileType;
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 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700189
jvanverth3d6ed3a2016-04-07 11:09:51 -0700190 // 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.
jvanverth9f372462016-04-06 06:08:59 -0700192 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700193 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700194 // use mailbox
195 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700196 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700197 break;
jvanverth9f372462016-04-06 06:08:59 -0700198 }
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;
jvanvertha4b0fed2016-04-27 11:42:21 -0700206 swapchainCreateInfo.imageFormat = surfaceFormat;
207 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700208 swapchainCreateInfo.imageExtent = extent;
209 swapchainCreateInfo.imageArrayLayers = 1;
210 swapchainCreateInfo.imageUsage = usageFlags;
211
jvanverthb0d43522016-04-21 11:46:23 -0700212 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
213 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700214 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
223 swapchainCreateInfo.preTransform = caps.currentTransform;;
224 swapchainCreateInfo.compositeAlpha = composite_alpha;
225 swapchainCreateInfo.presentMode = mode;
226 swapchainCreateInfo.clipped = true;
227 swapchainCreateInfo.oldSwapchain = fSwapchain;
228
jvanverthb0d43522016-04-21 11:46:23 -0700229 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700230 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
jvanverthb0d43522016-04-21 11:46:23 -0700240 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700241 }
242
egdaniel58a8d922016-04-21 08:03:10 -0700243 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700244
245 return true;
246}
247
jvanvertha8d0d6c2016-05-05 12:32:03 -0700248void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700249 GrVkFormatToPixelConfig(format, &fPixelConfig);
250
jvanverthb0d43522016-04-21 11:46:23 -0700251 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700252 SkASSERT(fImageCount);
253 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700254 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700255
256 // set up initial image layouts and create surfaces
257 fImageLayouts = new VkImageLayout[fImageCount];
jvanverthaf236b52016-05-20 06:01:06 -0700258 fRenderTargets = new sk_sp<GrRenderTarget>[fImageCount];
jvanverth9f372462016-04-06 06:08:59 -0700259 fSurfaces = new sk_sp<SkSurface>[fImageCount];
260 for (uint32_t i = 0; i < fImageCount; ++i) {
261 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
262
263 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700264 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700265 info.fImage = fImages[i];
brianosman419ca642016-05-04 08:19:44 -0700266 info.fAlloc = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -0700267 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
268 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700269 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700270 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700271 desc.fWidth = fWidth;
272 desc.fHeight = fHeight;
273 desc.fConfig = fPixelConfig;
274 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
275 desc.fSampleCnt = 0;
276 desc.fStencilBits = 0;
277 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700278 fRenderTargets[i].reset(fContext->textureProvider()->wrapBackendRenderTarget(desc));
279
280 fSurfaces[i] = this->createRenderSurface(fRenderTargets[i], 24);
jvanverth9f372462016-04-06 06:08:59 -0700281 }
282
283 // create the command pool for the command buffers
284 if (VK_NULL_HANDLE == fCommandPool) {
285 VkCommandPoolCreateInfo commandPoolInfo;
286 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
287 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
288 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700289 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700290 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
291 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
292 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
293 nullptr, &fCommandPool));
294 }
295
296 // set up the backbuffers
297 VkSemaphoreCreateInfo semaphoreInfo;
298 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
299 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
300 semaphoreInfo.pNext = nullptr;
301 semaphoreInfo.flags = 0;
302 VkCommandBufferAllocateInfo commandBuffersInfo;
303 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
304 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
305 commandBuffersInfo.pNext = nullptr;
306 commandBuffersInfo.commandPool = fCommandPool;
307 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
308 commandBuffersInfo.commandBufferCount = 2;
309 VkFenceCreateInfo fenceInfo;
310 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
311 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
312 fenceInfo.pNext = nullptr;
313 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
314
315 // we create one additional backbuffer structure here, because we want to
316 // give the command buffers they contain a chance to finish before we cycle back
317 fBackbuffers = new BackbufferInfo[fImageCount + 1];
318 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
319 fBackbuffers[i].fImageIndex = -1;
320 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
321 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
322 nullptr, &fBackbuffers[i].fAcquireSemaphore));
323 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
324 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
325 nullptr, &fBackbuffers[i].fRenderSemaphore));
326 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
327 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
328 fBackbuffers[i].fTransitionCmdBuffers));
329 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
330 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
331 &fBackbuffers[i].fUsageFences[0]));
332 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
333 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
334 &fBackbuffers[i].fUsageFences[1]));
335 }
336 fCurrentBackbufferIndex = fImageCount;
337}
338
jvanvertha8d0d6c2016-05-05 12:32:03 -0700339void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700340
341 if (fBackbuffers) {
342 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
343 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700344 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700345 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700346 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700347 fBackbuffers[i].fImageIndex = -1;
348 GR_VK_CALL(fBackendContext->fInterface,
349 DestroySemaphore(fBackendContext->fDevice,
350 fBackbuffers[i].fAcquireSemaphore,
351 nullptr));
352 GR_VK_CALL(fBackendContext->fInterface,
353 DestroySemaphore(fBackendContext->fDevice,
354 fBackbuffers[i].fRenderSemaphore,
355 nullptr));
356 GR_VK_CALL(fBackendContext->fInterface,
357 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
358 fBackbuffers[i].fTransitionCmdBuffers));
359 GR_VK_CALL(fBackendContext->fInterface,
360 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
361 GR_VK_CALL(fBackendContext->fInterface,
362 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
363 }
364 }
365
366 delete[] fBackbuffers;
367 fBackbuffers = nullptr;
368
jvanverthaf236b52016-05-20 06:01:06 -0700369 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700370 delete[] fSurfaces;
371 fSurfaces = nullptr;
jvanverthaf236b52016-05-20 06:01:06 -0700372 delete[] fRenderTargets;
373 fRenderTargets = nullptr;
jvanverth9f372462016-04-06 06:08:59 -0700374 delete[] fImageLayouts;
375 fImageLayouts = nullptr;
376 delete[] fImages;
377 fImages = nullptr;
378}
379
jvanvertha8d0d6c2016-05-05 12:32:03 -0700380VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700381 this->destroyContext();
382}
383
jvanvertha8d0d6c2016-05-05 12:32:03 -0700384void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700385 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) {
jvanverthb0d43522016-04-21 11:46:23 -0700400 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700401 fSwapchain = VK_NULL_HANDLE;
402 }
403
404 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700405 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700406 fSurface = VK_NULL_HANDLE;
407 }
408
jvanverthaf236b52016-05-20 06:01:06 -0700409 fContext->abandonContext();
410 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700411
412 fBackendContext.reset(nullptr);
413}
414
jvanvertha8d0d6c2016-05-05 12:32:03 -0700415VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700416 SkASSERT(fBackbuffers);
417
418 ++fCurrentBackbufferIndex;
419 if (fCurrentBackbufferIndex > fImageCount) {
420 fCurrentBackbufferIndex = 0;
421 }
422
423 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
424
425 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
426 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
427 true, UINT64_MAX));
428 return backbuffer;
429}
430
jvanverthaf236b52016-05-20 06:01:06 -0700431sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700432 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
433 SkASSERT(backbuffer);
434
435 // reset the fence
436 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
437 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
438 // semaphores should be in unsignaled state
439
440 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700441 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
442 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
443 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700444 if (VK_ERROR_SURFACE_LOST_KHR == res) {
445 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700446 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700447 return nullptr;
448 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700449 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700450 // tear swapchain down and try again
brianosman05de2162016-05-06 13:28:57 -0700451 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700452 return nullptr;
453 }
454
455 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700456 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700457 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
458 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700459
460 if (VK_SUCCESS != res) {
461 return nullptr;
462 }
463 }
464
465 // set up layout transfer from initial to color attachment
466 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
467 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
468 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
469 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
470 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700471 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700472 0 : VK_ACCESS_MEMORY_READ_BIT;
473 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
474
475 VkImageMemoryBarrier imageMemoryBarrier = {
476 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
477 NULL, // pNext
478 srcAccessMask, // outputMask
479 dstAccessMask, // inputMask
480 layout, // oldLayout
481 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
482 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700483 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700484 fImages[backbuffer->fImageIndex], // image
485 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
486 };
487 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
488 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
489 VkCommandBufferBeginInfo info;
490 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
491 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
492 info.flags = 0;
493 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
494 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
495
jvanvertha8d0d6c2016-05-05 12:32:03 -0700496 GR_VK_CALL(fBackendContext->fInterface,
497 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
498 srcStageMask, dstStageMask, 0,
499 0, nullptr,
500 0, nullptr,
501 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700502
503 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700504 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700505
egdaniel58a8d922016-04-21 08:03:10 -0700506 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700507 // insert the layout transfer into the queue and wait on the acquire
508 VkSubmitInfo submitInfo;
509 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
510 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
511 submitInfo.waitSemaphoreCount = 1;
512 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700513 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700514 submitInfo.commandBufferCount = 1;
515 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
516 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700517
jvanverth9f372462016-04-06 06:08:59 -0700518 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700519 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700520 backbuffer->fUsageFences[0]));
521
jvanverthaf236b52016-05-20 06:01:06 -0700522 return sk_ref_sp(fSurfaces[backbuffer->fImageIndex].get());
jvanverth9f372462016-04-06 06:08:59 -0700523}
524
jvanvertha8d0d6c2016-05-05 12:32:03 -0700525void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700526
527 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
528
jvanverthaf236b52016-05-20 06:01:06 -0700529 this->presentRenderSurface(fSurfaces[backbuffer->fImageIndex],
530 fRenderTargets[backbuffer->fImageIndex], 24);
531
jvanverth9f372462016-04-06 06:08:59 -0700532 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
533 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
534 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
535 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
536 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
537
538 VkImageMemoryBarrier imageMemoryBarrier = {
539 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
540 NULL, // pNext
541 srcAccessMask, // outputMask
542 dstAccessMask, // inputMask
543 layout, // oldLayout
544 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700545 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700546 fPresentQueueIndex, // dstQueueFamilyIndex
547 fImages[backbuffer->fImageIndex], // image
548 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
549 };
550 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
551 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
552 VkCommandBufferBeginInfo info;
553 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
554 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
555 info.flags = 0;
556 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
557 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
558 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700559 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
560 srcStageMask, dstStageMask, 0,
561 0, nullptr,
562 0, nullptr,
563 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700564 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
565 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
566
567 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
568
569 // insert the layout transfer into the queue and wait on the acquire
570 VkSubmitInfo submitInfo;
571 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
572 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
573 submitInfo.waitSemaphoreCount = 0;
574 submitInfo.pWaitDstStageMask = 0;
575 submitInfo.commandBufferCount = 1;
576 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
577 submitInfo.signalSemaphoreCount = 1;
578 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
579
580 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700581 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700582 backbuffer->fUsageFences[1]));
583
584 // Submit present operation to present queue
585 const VkPresentInfoKHR presentInfo =
586 {
587 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
588 NULL, // pNext
589 1, // waitSemaphoreCount
590 &backbuffer->fRenderSemaphore, // pWaitSemaphores
591 1, // swapchainCount
592 &fSwapchain, // pSwapchains
593 &backbuffer->fImageIndex, // pImageIndices
594 NULL // pResults
595 };
596
jvanverthb0d43522016-04-21 11:46:23 -0700597 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700598
599}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700600
601} //namespace sk_app