blob: 2570e826a387c873afa3529bc95bb968814a4173 [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"
10#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070011#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070012
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
jvanverthb0d43522016-04-21 11:46:23 -070022#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
jvanvertha8d0d6c2016-05-05 12:32:03 -070025namespace sk_app {
26
brianosman05de2162016-05-06 13:28:57 -070027VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams& params)
jvanvertha8d0d6c2016-05-05 12:32:03 -070028 : fSurface(VK_NULL_HANDLE)
29 , fSwapchain(VK_NULL_HANDLE)
30 , fCommandPool(VK_NULL_HANDLE)
31 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070032
33 // any config code here (particularly for msaa)?
34
brianosman05de2162016-05-06 13:28:57 -070035 this->initializeContext(platformData, params);
jvanverth9f372462016-04-06 06:08:59 -070036}
37
brianosman05de2162016-05-06 13:28:57 -070038void VulkanWindowContext::initializeContext(void* platformData, const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070039
jvanverthb0d43522016-04-21 11:46:23 -070040 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);
jvanverth9f372462016-04-06 06:08:59 -070059
jvanvertha8d0d6c2016-05-05 12:32:03 -070060 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070061
jvanverthb0d43522016-04-21 11:46:23 -070062 fSurface = createVkSurface(instance, platformData);
jvanverth9f372462016-04-06 06:08:59 -070063 if (VK_NULL_HANDLE == fSurface) {
64 fBackendContext.reset(nullptr);
65 return;
66 }
67
jvanverth9f372462016-04-06 06:08:59 -070068 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070069 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
70 fPresentQueueIndex, fSurface,
71 &supported);
jvanverth9f372462016-04-06 06:08:59 -070072 if (VK_SUCCESS != res) {
73 this->destroyContext();
74 return;
75 }
76
brianosman05de2162016-05-06 13:28:57 -070077 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070078 this->destroyContext();
79 return;
80 }
81
82 // create presentQueue
83 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070084}
85
brianosman05de2162016-05-06 13:28:57 -070086bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height,
87 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070088 // check for capabilities
89 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070090 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
91 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070092 if (VK_SUCCESS != res) {
93 return false;
94 }
95
96 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -070097 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
98 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -070099 if (VK_SUCCESS != res) {
100 return false;
101 }
102
103 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
104 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700105 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
106 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700107 if (VK_SUCCESS != res) {
108 return false;
109 }
110
111 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700112 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
113 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700114 if (VK_SUCCESS != res) {
115 return false;
116 }
117
118 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
119 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700120 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700121 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700122 if (VK_SUCCESS != res) {
123 return false;
124 }
125
126 VkExtent2D extent = caps.currentExtent;
127 // use the hints
128 if (extent.width == (uint32_t)-1) {
129 extent.width = width;
130 extent.height = height;
131 }
132
jvanverth9fab59d2016-04-06 12:08:51 -0700133 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700134 if (extent.width < caps.minImageExtent.width) {
135 extent.width = caps.minImageExtent.width;
136 } else if (extent.width > caps.maxImageExtent.width) {
137 extent.width = caps.maxImageExtent.width;
138 }
139 // clamp height
140 if (extent.height < caps.minImageExtent.height) {
141 extent.height = caps.minImageExtent.height;
142 } else if (extent.height > caps.maxImageExtent.height) {
143 extent.height = caps.maxImageExtent.height;
144 }
145 fWidth = (int)extent.width;
146 fHeight = (int)extent.height;
147
148 uint32_t imageCount = caps.minImageCount + 2;
149 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
150 // Application must settle for fewer images than desired:
151 imageCount = caps.maxImageCount;
152 }
153
154 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
155 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
156 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
157 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
158 SkASSERT(caps.supportedTransforms & caps.currentTransform);
159 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
160 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
161 VkCompositeAlphaFlagBitsKHR composite_alpha =
162 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
163 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
164 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
165
brianosman05de2162016-05-06 13:28:57 -0700166 // Pick our surface format. For now, just make sure it matches our sRGB request:
167 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
168 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
169 bool wantSRGB = kSRGB_SkColorProfileType == params.fProfileType;
170 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
171 GrPixelConfig config;
172 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
173 GrPixelConfigIsSRGB(config) == wantSRGB) {
174 surfaceFormat = surfaceFormats[i].format;
175 colorSpace = surfaceFormats[i].colorSpace;
176 break;
177 }
178 }
179 fDisplayParams = params;
180
181 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
182 return false;
183 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700184
jvanverth3d6ed3a2016-04-07 11:09:51 -0700185 // If mailbox mode is available, use it, as it is the lowest-latency non-
186 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700187 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700188 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700189 // use mailbox
190 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700191 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700192 break;
jvanverth9f372462016-04-06 06:08:59 -0700193 }
194 }
195
196 VkSwapchainCreateInfoKHR swapchainCreateInfo;
197 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
198 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
199 swapchainCreateInfo.surface = fSurface;
200 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700201 swapchainCreateInfo.imageFormat = surfaceFormat;
202 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700203 swapchainCreateInfo.imageExtent = extent;
204 swapchainCreateInfo.imageArrayLayers = 1;
205 swapchainCreateInfo.imageUsage = usageFlags;
206
jvanverthb0d43522016-04-21 11:46:23 -0700207 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
208 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700209 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
210 swapchainCreateInfo.queueFamilyIndexCount = 2;
211 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
212 } else {
213 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
214 swapchainCreateInfo.queueFamilyIndexCount = 0;
215 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
216 }
217
218 swapchainCreateInfo.preTransform = caps.currentTransform;;
219 swapchainCreateInfo.compositeAlpha = composite_alpha;
220 swapchainCreateInfo.presentMode = mode;
221 swapchainCreateInfo.clipped = true;
222 swapchainCreateInfo.oldSwapchain = fSwapchain;
223
jvanverthb0d43522016-04-21 11:46:23 -0700224 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700225 if (VK_SUCCESS != res) {
226 return false;
227 }
228
229 // destroy the old swapchain
230 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
231 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
232
233 this->destroyBuffers();
234
jvanverthb0d43522016-04-21 11:46:23 -0700235 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700236 }
237
egdaniel58a8d922016-04-21 08:03:10 -0700238 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700239
240 return true;
241}
242
jvanvertha8d0d6c2016-05-05 12:32:03 -0700243void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700244 GrVkFormatToPixelConfig(format, &fPixelConfig);
245
jvanverthb0d43522016-04-21 11:46:23 -0700246 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700247 SkASSERT(fImageCount);
248 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700249 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700250
251 // set up initial image layouts and create surfaces
252 fImageLayouts = new VkImageLayout[fImageCount];
253 fSurfaces = new sk_sp<SkSurface>[fImageCount];
254 for (uint32_t i = 0; i < fImageCount; ++i) {
255 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
256
257 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700258 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700259 info.fImage = fImages[i];
brianosman419ca642016-05-04 08:19:44 -0700260 info.fAlloc = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -0700261 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
262 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700263 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700264 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700265 desc.fWidth = fWidth;
266 desc.fHeight = fHeight;
267 desc.fConfig = fPixelConfig;
268 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
269 desc.fSampleCnt = 0;
270 desc.fStencilBits = 0;
271 desc.fRenderTargetHandle = (GrBackendObject) &info;
brianosman05de2162016-05-06 13:28:57 -0700272 SkSurfaceProps props(GrPixelConfigIsSRGB(fPixelConfig)
273 ? SkSurfaceProps::kGammaCorrect_Flag : 0,
274 kUnknown_SkPixelGeometry);
jvanverth9f372462016-04-06 06:08:59 -0700275 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
276 }
277
278 // create the command pool for the command buffers
279 if (VK_NULL_HANDLE == fCommandPool) {
280 VkCommandPoolCreateInfo commandPoolInfo;
281 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
282 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
283 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700284 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700285 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
286 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
287 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
288 nullptr, &fCommandPool));
289 }
290
291 // set up the backbuffers
292 VkSemaphoreCreateInfo semaphoreInfo;
293 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
294 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
295 semaphoreInfo.pNext = nullptr;
296 semaphoreInfo.flags = 0;
297 VkCommandBufferAllocateInfo commandBuffersInfo;
298 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
299 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
300 commandBuffersInfo.pNext = nullptr;
301 commandBuffersInfo.commandPool = fCommandPool;
302 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
303 commandBuffersInfo.commandBufferCount = 2;
304 VkFenceCreateInfo fenceInfo;
305 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
306 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
307 fenceInfo.pNext = nullptr;
308 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
309
310 // we create one additional backbuffer structure here, because we want to
311 // give the command buffers they contain a chance to finish before we cycle back
312 fBackbuffers = new BackbufferInfo[fImageCount + 1];
313 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
314 fBackbuffers[i].fImageIndex = -1;
315 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
316 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
317 nullptr, &fBackbuffers[i].fAcquireSemaphore));
318 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
319 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
320 nullptr, &fBackbuffers[i].fRenderSemaphore));
321 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
322 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
323 fBackbuffers[i].fTransitionCmdBuffers));
324 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
325 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
326 &fBackbuffers[i].fUsageFences[0]));
327 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
328 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
329 &fBackbuffers[i].fUsageFences[1]));
330 }
331 fCurrentBackbufferIndex = fImageCount;
332}
333
jvanvertha8d0d6c2016-05-05 12:32:03 -0700334void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700335
336 if (fBackbuffers) {
337 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
338 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700339 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700340 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700341 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700342 fBackbuffers[i].fImageIndex = -1;
343 GR_VK_CALL(fBackendContext->fInterface,
344 DestroySemaphore(fBackendContext->fDevice,
345 fBackbuffers[i].fAcquireSemaphore,
346 nullptr));
347 GR_VK_CALL(fBackendContext->fInterface,
348 DestroySemaphore(fBackendContext->fDevice,
349 fBackbuffers[i].fRenderSemaphore,
350 nullptr));
351 GR_VK_CALL(fBackendContext->fInterface,
352 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
353 fBackbuffers[i].fTransitionCmdBuffers));
354 GR_VK_CALL(fBackendContext->fInterface,
355 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
356 GR_VK_CALL(fBackendContext->fInterface,
357 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
358 }
359 }
360
361 delete[] fBackbuffers;
362 fBackbuffers = nullptr;
363
364 delete[] fSurfaces;
365 fSurfaces = nullptr;
366 delete[] fImageLayouts;
367 fImageLayouts = nullptr;
368 delete[] fImages;
369 fImages = nullptr;
370}
371
jvanvertha8d0d6c2016-05-05 12:32:03 -0700372VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700373 this->destroyContext();
374}
375
jvanvertha8d0d6c2016-05-05 12:32:03 -0700376void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700377 if (!fBackendContext.get()) {
378 return;
379 }
380
381 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
382
383 this->destroyBuffers();
384
385 if (VK_NULL_HANDLE != fCommandPool) {
386 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
387 fCommandPool, nullptr));
388 fCommandPool = VK_NULL_HANDLE;
389 }
390
391 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700392 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700393 fSwapchain = VK_NULL_HANDLE;
394 }
395
396 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700397 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700398 fSurface = VK_NULL_HANDLE;
399 }
400
401 delete fContext;
402
403 fBackendContext.reset(nullptr);
404}
405
jvanvertha8d0d6c2016-05-05 12:32:03 -0700406VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700407 SkASSERT(fBackbuffers);
408
409 ++fCurrentBackbufferIndex;
410 if (fCurrentBackbufferIndex > fImageCount) {
411 fCurrentBackbufferIndex = 0;
412 }
413
414 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
415
416 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
417 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
418 true, UINT64_MAX));
419 return backbuffer;
420}
421
jvanvertha8d0d6c2016-05-05 12:32:03 -0700422SkSurface* VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700423 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
424 SkASSERT(backbuffer);
425
426 // reset the fence
427 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
428 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
429 // semaphores should be in unsignaled state
430
431 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700432 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
433 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
434 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700435 if (VK_ERROR_SURFACE_LOST_KHR == res) {
436 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700437 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700438 return nullptr;
439 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700440 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700441 // tear swapchain down and try again
brianosman05de2162016-05-06 13:28:57 -0700442 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700443 return nullptr;
444 }
445
446 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700447 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700448 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
449 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700450
451 if (VK_SUCCESS != res) {
452 return nullptr;
453 }
454 }
455
456 // set up layout transfer from initial to color attachment
457 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
458 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
459 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
460 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
461 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700462 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700463 0 : VK_ACCESS_MEMORY_READ_BIT;
464 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
465
466 VkImageMemoryBarrier imageMemoryBarrier = {
467 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
468 NULL, // pNext
469 srcAccessMask, // outputMask
470 dstAccessMask, // inputMask
471 layout, // oldLayout
472 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
473 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700474 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700475 fImages[backbuffer->fImageIndex], // image
476 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
477 };
478 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
479 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
480 VkCommandBufferBeginInfo info;
481 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
482 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
483 info.flags = 0;
484 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
485 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
486
jvanvertha8d0d6c2016-05-05 12:32:03 -0700487 GR_VK_CALL(fBackendContext->fInterface,
488 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
489 srcStageMask, dstStageMask, 0,
490 0, nullptr,
491 0, nullptr,
492 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700493
494 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700495 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700496
egdaniel58a8d922016-04-21 08:03:10 -0700497 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700498 // insert the layout transfer into the queue and wait on the acquire
499 VkSubmitInfo submitInfo;
500 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
501 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
502 submitInfo.waitSemaphoreCount = 1;
503 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700504 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700505 submitInfo.commandBufferCount = 1;
506 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
507 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700508
jvanverth9f372462016-04-06 06:08:59 -0700509 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700510 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700511 backbuffer->fUsageFences[0]));
512
513 return fSurfaces[backbuffer->fImageIndex].get();
514}
515
516
jvanvertha8d0d6c2016-05-05 12:32:03 -0700517void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700518
519 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
520
521 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
522 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
523 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
524 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
525 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
526
527 VkImageMemoryBarrier imageMemoryBarrier = {
528 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
529 NULL, // pNext
530 srcAccessMask, // outputMask
531 dstAccessMask, // inputMask
532 layout, // oldLayout
533 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700534 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700535 fPresentQueueIndex, // dstQueueFamilyIndex
536 fImages[backbuffer->fImageIndex], // image
537 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
538 };
539 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
540 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
541 VkCommandBufferBeginInfo info;
542 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
543 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
544 info.flags = 0;
545 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
546 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
547 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700548 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
549 srcStageMask, dstStageMask, 0,
550 0, nullptr,
551 0, nullptr,
552 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700553 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
554 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
555
556 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
557
558 // insert the layout transfer into the queue and wait on the acquire
559 VkSubmitInfo submitInfo;
560 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
561 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
562 submitInfo.waitSemaphoreCount = 0;
563 submitInfo.pWaitDstStageMask = 0;
564 submitInfo.commandBufferCount = 1;
565 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
566 submitInfo.signalSemaphoreCount = 1;
567 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
568
569 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700570 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700571 backbuffer->fUsageFences[1]));
572
573 // Submit present operation to present queue
574 const VkPresentInfoKHR presentInfo =
575 {
576 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
577 NULL, // pNext
578 1, // waitSemaphoreCount
579 &backbuffer->fRenderSemaphore, // pWaitSemaphores
580 1, // swapchainCount
581 &fSwapchain, // pSwapchains
582 &backbuffer->fImageIndex, // pImageIndices
583 NULL // pResults
584 };
585
jvanverthb0d43522016-04-21 11:46:23 -0700586 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700587
588}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700589
590} //namespace sk_app