blob: 8f5f4209a45c190375c2cbc9fcf52b2964d89c07 [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
27VulkanWindowContext::VulkanWindowContext(void* platformData, int msaaSampleCount)
28 : 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
35 this->initializeContext(platformData);
36}
37
jvanvertha8d0d6c2016-05-05 12:32:03 -070038void VulkanWindowContext::initializeContext(void* platformData) {
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
jvanverth9fab59d2016-04-06 12:08:51 -070077 if (!this->createSwapchain(-1, -1)) {
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
jvanvertha8d0d6c2016-05-05 12:32:03 -070086bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height) {
jvanverth9f372462016-04-06 06:08:59 -070087 // check for capabilities
88 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070089 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
90 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070091 if (VK_SUCCESS != res) {
92 return false;
93 }
94
95 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -070096 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
97 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -070098 if (VK_SUCCESS != res) {
99 return false;
100 }
101
102 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
103 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700104 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
105 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700106 if (VK_SUCCESS != res) {
107 return false;
108 }
109
110 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700111 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
112 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700113 if (VK_SUCCESS != res) {
114 return false;
115 }
116
117 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
118 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700119 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700120 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700121 if (VK_SUCCESS != res) {
122 return false;
123 }
124
125 VkExtent2D extent = caps.currentExtent;
126 // use the hints
127 if (extent.width == (uint32_t)-1) {
128 extent.width = width;
129 extent.height = height;
130 }
131
jvanverth9fab59d2016-04-06 12:08:51 -0700132 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700133 if (extent.width < caps.minImageExtent.width) {
134 extent.width = caps.minImageExtent.width;
135 } else if (extent.width > caps.maxImageExtent.width) {
136 extent.width = caps.maxImageExtent.width;
137 }
138 // clamp height
139 if (extent.height < caps.minImageExtent.height) {
140 extent.height = caps.minImageExtent.height;
141 } else if (extent.height > caps.maxImageExtent.height) {
142 extent.height = caps.maxImageExtent.height;
143 }
144 fWidth = (int)extent.width;
145 fHeight = (int)extent.height;
146
147 uint32_t imageCount = caps.minImageCount + 2;
148 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
149 // Application must settle for fewer images than desired:
150 imageCount = caps.maxImageCount;
151 }
152
153 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
154 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
155 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
156 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
157 SkASSERT(caps.supportedTransforms & caps.currentTransform);
158 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
159 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
160 VkCompositeAlphaFlagBitsKHR composite_alpha =
161 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
162 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
163 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
164
jvanvertha4b0fed2016-04-27 11:42:21 -0700165 // Pick our surface format -- for now, the first one
166 VkFormat surfaceFormat = surfaceFormats[0].format;
167 VkColorSpaceKHR colorSpace = surfaceFormats[0].colorSpace;
168
jvanverth3d6ed3a2016-04-07 11:09:51 -0700169 // If mailbox mode is available, use it, as it is the lowest-latency non-
170 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700171 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700172 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700173 // use mailbox
174 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700175 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700176 break;
jvanverth9f372462016-04-06 06:08:59 -0700177 }
178 }
179
180 VkSwapchainCreateInfoKHR swapchainCreateInfo;
181 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
182 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
183 swapchainCreateInfo.surface = fSurface;
184 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700185 swapchainCreateInfo.imageFormat = surfaceFormat;
186 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700187 swapchainCreateInfo.imageExtent = extent;
188 swapchainCreateInfo.imageArrayLayers = 1;
189 swapchainCreateInfo.imageUsage = usageFlags;
190
jvanverthb0d43522016-04-21 11:46:23 -0700191 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
192 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700193 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
194 swapchainCreateInfo.queueFamilyIndexCount = 2;
195 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
196 } else {
197 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
198 swapchainCreateInfo.queueFamilyIndexCount = 0;
199 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
200 }
201
202 swapchainCreateInfo.preTransform = caps.currentTransform;;
203 swapchainCreateInfo.compositeAlpha = composite_alpha;
204 swapchainCreateInfo.presentMode = mode;
205 swapchainCreateInfo.clipped = true;
206 swapchainCreateInfo.oldSwapchain = fSwapchain;
207
jvanverthb0d43522016-04-21 11:46:23 -0700208 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700209 if (VK_SUCCESS != res) {
210 return false;
211 }
212
213 // destroy the old swapchain
214 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
215 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
216
217 this->destroyBuffers();
218
jvanverthb0d43522016-04-21 11:46:23 -0700219 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700220 }
221
egdaniel58a8d922016-04-21 08:03:10 -0700222 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700223
224 return true;
225}
226
jvanvertha8d0d6c2016-05-05 12:32:03 -0700227void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700228 GrVkFormatToPixelConfig(format, &fPixelConfig);
229
jvanverthb0d43522016-04-21 11:46:23 -0700230 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700231 SkASSERT(fImageCount);
232 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700233 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700234
235 // set up initial image layouts and create surfaces
236 fImageLayouts = new VkImageLayout[fImageCount];
237 fSurfaces = new sk_sp<SkSurface>[fImageCount];
238 for (uint32_t i = 0; i < fImageCount; ++i) {
239 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
240
241 GrBackendRenderTargetDesc desc;
242 GrVkTextureInfo info;
243 info.fImage = fImages[i];
brianosman419ca642016-05-04 08:19:44 -0700244 info.fAlloc = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -0700245 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
246 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700247 info.fFormat = format;
jvanverth9f372462016-04-06 06:08:59 -0700248 desc.fWidth = fWidth;
249 desc.fHeight = fHeight;
250 desc.fConfig = fPixelConfig;
251 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
252 desc.fSampleCnt = 0;
253 desc.fStencilBits = 0;
254 desc.fRenderTargetHandle = (GrBackendObject) &info;
255 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
256 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
257 }
258
259 // create the command pool for the command buffers
260 if (VK_NULL_HANDLE == fCommandPool) {
261 VkCommandPoolCreateInfo commandPoolInfo;
262 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
263 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
264 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700265 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700266 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
267 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
268 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
269 nullptr, &fCommandPool));
270 }
271
272 // set up the backbuffers
273 VkSemaphoreCreateInfo semaphoreInfo;
274 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
275 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
276 semaphoreInfo.pNext = nullptr;
277 semaphoreInfo.flags = 0;
278 VkCommandBufferAllocateInfo commandBuffersInfo;
279 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
280 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
281 commandBuffersInfo.pNext = nullptr;
282 commandBuffersInfo.commandPool = fCommandPool;
283 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
284 commandBuffersInfo.commandBufferCount = 2;
285 VkFenceCreateInfo fenceInfo;
286 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
287 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
288 fenceInfo.pNext = nullptr;
289 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
290
291 // we create one additional backbuffer structure here, because we want to
292 // give the command buffers they contain a chance to finish before we cycle back
293 fBackbuffers = new BackbufferInfo[fImageCount + 1];
294 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
295 fBackbuffers[i].fImageIndex = -1;
296 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
297 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
298 nullptr, &fBackbuffers[i].fAcquireSemaphore));
299 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
300 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
301 nullptr, &fBackbuffers[i].fRenderSemaphore));
302 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
303 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
304 fBackbuffers[i].fTransitionCmdBuffers));
305 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
306 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
307 &fBackbuffers[i].fUsageFences[0]));
308 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
309 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
310 &fBackbuffers[i].fUsageFences[1]));
311 }
312 fCurrentBackbufferIndex = fImageCount;
313}
314
jvanvertha8d0d6c2016-05-05 12:32:03 -0700315void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700316
317 if (fBackbuffers) {
318 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
319 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700320 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700321 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700322 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700323 fBackbuffers[i].fImageIndex = -1;
324 GR_VK_CALL(fBackendContext->fInterface,
325 DestroySemaphore(fBackendContext->fDevice,
326 fBackbuffers[i].fAcquireSemaphore,
327 nullptr));
328 GR_VK_CALL(fBackendContext->fInterface,
329 DestroySemaphore(fBackendContext->fDevice,
330 fBackbuffers[i].fRenderSemaphore,
331 nullptr));
332 GR_VK_CALL(fBackendContext->fInterface,
333 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
334 fBackbuffers[i].fTransitionCmdBuffers));
335 GR_VK_CALL(fBackendContext->fInterface,
336 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
337 GR_VK_CALL(fBackendContext->fInterface,
338 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
339 }
340 }
341
342 delete[] fBackbuffers;
343 fBackbuffers = nullptr;
344
345 delete[] fSurfaces;
346 fSurfaces = nullptr;
347 delete[] fImageLayouts;
348 fImageLayouts = nullptr;
349 delete[] fImages;
350 fImages = nullptr;
351}
352
jvanvertha8d0d6c2016-05-05 12:32:03 -0700353VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700354 this->destroyContext();
355}
356
jvanvertha8d0d6c2016-05-05 12:32:03 -0700357void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700358 if (!fBackendContext.get()) {
359 return;
360 }
361
362 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
363
364 this->destroyBuffers();
365
366 if (VK_NULL_HANDLE != fCommandPool) {
367 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
368 fCommandPool, nullptr));
369 fCommandPool = VK_NULL_HANDLE;
370 }
371
372 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700373 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700374 fSwapchain = VK_NULL_HANDLE;
375 }
376
377 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700378 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700379 fSurface = VK_NULL_HANDLE;
380 }
381
382 delete fContext;
383
384 fBackendContext.reset(nullptr);
385}
386
jvanvertha8d0d6c2016-05-05 12:32:03 -0700387VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700388 SkASSERT(fBackbuffers);
389
390 ++fCurrentBackbufferIndex;
391 if (fCurrentBackbufferIndex > fImageCount) {
392 fCurrentBackbufferIndex = 0;
393 }
394
395 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
396
397 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
398 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
399 true, UINT64_MAX));
400 return backbuffer;
401}
402
jvanvertha8d0d6c2016-05-05 12:32:03 -0700403SkSurface* VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700404 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
405 SkASSERT(backbuffer);
406
407 // reset the fence
408 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
409 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
410 // semaphores should be in unsignaled state
411
412 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700413 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
414 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
415 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700416 if (VK_ERROR_SURFACE_LOST_KHR == res) {
417 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700418 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700419 return nullptr;
420 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700421 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700422 // tear swapchain down and try again
423 if (!this->createSwapchain(0, 0)) {
424 return nullptr;
425 }
426
427 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700428 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700429 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
430 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700431
432 if (VK_SUCCESS != res) {
433 return nullptr;
434 }
435 }
436
437 // set up layout transfer from initial to color attachment
438 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
439 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
440 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
441 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
442 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700443 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700444 0 : VK_ACCESS_MEMORY_READ_BIT;
445 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
446
447 VkImageMemoryBarrier imageMemoryBarrier = {
448 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
449 NULL, // pNext
450 srcAccessMask, // outputMask
451 dstAccessMask, // inputMask
452 layout, // oldLayout
453 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
454 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700455 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700456 fImages[backbuffer->fImageIndex], // image
457 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
458 };
459 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
460 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
461 VkCommandBufferBeginInfo info;
462 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
463 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
464 info.flags = 0;
465 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
466 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
467
jvanvertha8d0d6c2016-05-05 12:32:03 -0700468 GR_VK_CALL(fBackendContext->fInterface,
469 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
470 srcStageMask, dstStageMask, 0,
471 0, nullptr,
472 0, nullptr,
473 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700474
475 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700476 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700477
egdaniel58a8d922016-04-21 08:03:10 -0700478 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700479 // insert the layout transfer into the queue and wait on the acquire
480 VkSubmitInfo submitInfo;
481 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
482 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
483 submitInfo.waitSemaphoreCount = 1;
484 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700485 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700486 submitInfo.commandBufferCount = 1;
487 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
488 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700489
jvanverth9f372462016-04-06 06:08:59 -0700490 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700491 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700492 backbuffer->fUsageFences[0]));
493
494 return fSurfaces[backbuffer->fImageIndex].get();
495}
496
497
jvanvertha8d0d6c2016-05-05 12:32:03 -0700498void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700499
500 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
501
502 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
503 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
504 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
505 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
506 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
507
508 VkImageMemoryBarrier imageMemoryBarrier = {
509 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
510 NULL, // pNext
511 srcAccessMask, // outputMask
512 dstAccessMask, // inputMask
513 layout, // oldLayout
514 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700515 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700516 fPresentQueueIndex, // dstQueueFamilyIndex
517 fImages[backbuffer->fImageIndex], // image
518 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
519 };
520 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
521 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
522 VkCommandBufferBeginInfo info;
523 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
524 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
525 info.flags = 0;
526 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
527 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
528 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700529 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
530 srcStageMask, dstStageMask, 0,
531 0, nullptr,
532 0, nullptr,
533 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700534 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
535 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
536
537 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
538
539 // insert the layout transfer into the queue and wait on the acquire
540 VkSubmitInfo submitInfo;
541 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
542 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
543 submitInfo.waitSemaphoreCount = 0;
544 submitInfo.pWaitDstStageMask = 0;
545 submitInfo.commandBufferCount = 1;
546 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
547 submitInfo.signalSemaphoreCount = 1;
548 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
549
550 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700551 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700552 backbuffer->fUsageFences[1]));
553
554 // Submit present operation to present queue
555 const VkPresentInfoKHR presentInfo =
556 {
557 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
558 NULL, // pNext
559 1, // waitSemaphoreCount
560 &backbuffer->fRenderSemaphore, // pWaitSemaphores
561 1, // swapchainCount
562 &fSwapchain, // pSwapchains
563 &backbuffer->fImageIndex, // pImageIndices
564 NULL // pResults
565 };
566
jvanverthb0d43522016-04-21 11:46:23 -0700567 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700568
569}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700570
571} //namespace sk_app