blob: f21ec9c0cb5f74e282e28036dae07b737bef94d6 [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;
258 GrVkTextureInfo info;
259 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;
jvanverth9f372462016-04-06 06:08:59 -0700264 desc.fWidth = fWidth;
265 desc.fHeight = fHeight;
266 desc.fConfig = fPixelConfig;
267 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
268 desc.fSampleCnt = 0;
269 desc.fStencilBits = 0;
270 desc.fRenderTargetHandle = (GrBackendObject) &info;
brianosman05de2162016-05-06 13:28:57 -0700271 SkSurfaceProps props(GrPixelConfigIsSRGB(fPixelConfig)
272 ? SkSurfaceProps::kGammaCorrect_Flag : 0,
273 kUnknown_SkPixelGeometry);
jvanverth9f372462016-04-06 06:08:59 -0700274 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
275 }
276
277 // create the command pool for the command buffers
278 if (VK_NULL_HANDLE == fCommandPool) {
279 VkCommandPoolCreateInfo commandPoolInfo;
280 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
281 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
282 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700283 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700284 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
285 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
286 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
287 nullptr, &fCommandPool));
288 }
289
290 // set up the backbuffers
291 VkSemaphoreCreateInfo semaphoreInfo;
292 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
293 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
294 semaphoreInfo.pNext = nullptr;
295 semaphoreInfo.flags = 0;
296 VkCommandBufferAllocateInfo commandBuffersInfo;
297 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
298 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
299 commandBuffersInfo.pNext = nullptr;
300 commandBuffersInfo.commandPool = fCommandPool;
301 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
302 commandBuffersInfo.commandBufferCount = 2;
303 VkFenceCreateInfo fenceInfo;
304 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
305 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
306 fenceInfo.pNext = nullptr;
307 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
308
309 // we create one additional backbuffer structure here, because we want to
310 // give the command buffers they contain a chance to finish before we cycle back
311 fBackbuffers = new BackbufferInfo[fImageCount + 1];
312 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
313 fBackbuffers[i].fImageIndex = -1;
314 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
315 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
316 nullptr, &fBackbuffers[i].fAcquireSemaphore));
317 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
318 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
319 nullptr, &fBackbuffers[i].fRenderSemaphore));
320 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
321 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
322 fBackbuffers[i].fTransitionCmdBuffers));
323 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
324 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
325 &fBackbuffers[i].fUsageFences[0]));
326 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
327 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
328 &fBackbuffers[i].fUsageFences[1]));
329 }
330 fCurrentBackbufferIndex = fImageCount;
331}
332
jvanvertha8d0d6c2016-05-05 12:32:03 -0700333void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700334
335 if (fBackbuffers) {
336 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
337 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700338 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700339 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700340 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700341 fBackbuffers[i].fImageIndex = -1;
342 GR_VK_CALL(fBackendContext->fInterface,
343 DestroySemaphore(fBackendContext->fDevice,
344 fBackbuffers[i].fAcquireSemaphore,
345 nullptr));
346 GR_VK_CALL(fBackendContext->fInterface,
347 DestroySemaphore(fBackendContext->fDevice,
348 fBackbuffers[i].fRenderSemaphore,
349 nullptr));
350 GR_VK_CALL(fBackendContext->fInterface,
351 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
352 fBackbuffers[i].fTransitionCmdBuffers));
353 GR_VK_CALL(fBackendContext->fInterface,
354 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
355 GR_VK_CALL(fBackendContext->fInterface,
356 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
357 }
358 }
359
360 delete[] fBackbuffers;
361 fBackbuffers = nullptr;
362
363 delete[] fSurfaces;
364 fSurfaces = nullptr;
365 delete[] fImageLayouts;
366 fImageLayouts = nullptr;
367 delete[] fImages;
368 fImages = nullptr;
369}
370
jvanvertha8d0d6c2016-05-05 12:32:03 -0700371VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700372 this->destroyContext();
373}
374
jvanvertha8d0d6c2016-05-05 12:32:03 -0700375void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700376 if (!fBackendContext.get()) {
377 return;
378 }
379
380 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
381
382 this->destroyBuffers();
383
384 if (VK_NULL_HANDLE != fCommandPool) {
385 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
386 fCommandPool, nullptr));
387 fCommandPool = VK_NULL_HANDLE;
388 }
389
390 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700391 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700392 fSwapchain = VK_NULL_HANDLE;
393 }
394
395 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700396 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700397 fSurface = VK_NULL_HANDLE;
398 }
399
400 delete fContext;
401
402 fBackendContext.reset(nullptr);
403}
404
jvanvertha8d0d6c2016-05-05 12:32:03 -0700405VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700406 SkASSERT(fBackbuffers);
407
408 ++fCurrentBackbufferIndex;
409 if (fCurrentBackbufferIndex > fImageCount) {
410 fCurrentBackbufferIndex = 0;
411 }
412
413 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
414
415 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
416 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
417 true, UINT64_MAX));
418 return backbuffer;
419}
420
jvanvertha8d0d6c2016-05-05 12:32:03 -0700421SkSurface* VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700422 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
423 SkASSERT(backbuffer);
424
425 // reset the fence
426 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
427 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
428 // semaphores should be in unsignaled state
429
430 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700431 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
432 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
433 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700434 if (VK_ERROR_SURFACE_LOST_KHR == res) {
435 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700436 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700437 return nullptr;
438 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700439 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700440 // tear swapchain down and try again
brianosman05de2162016-05-06 13:28:57 -0700441 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700442 return nullptr;
443 }
444
445 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700446 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700447 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
448 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700449
450 if (VK_SUCCESS != res) {
451 return nullptr;
452 }
453 }
454
455 // set up layout transfer from initial to color attachment
456 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
457 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
458 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
459 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
460 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700461 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700462 0 : VK_ACCESS_MEMORY_READ_BIT;
463 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
464
465 VkImageMemoryBarrier imageMemoryBarrier = {
466 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
467 NULL, // pNext
468 srcAccessMask, // outputMask
469 dstAccessMask, // inputMask
470 layout, // oldLayout
471 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
472 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700473 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700474 fImages[backbuffer->fImageIndex], // image
475 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
476 };
477 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
478 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
479 VkCommandBufferBeginInfo info;
480 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
481 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
482 info.flags = 0;
483 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
484 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
485
jvanvertha8d0d6c2016-05-05 12:32:03 -0700486 GR_VK_CALL(fBackendContext->fInterface,
487 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
488 srcStageMask, dstStageMask, 0,
489 0, nullptr,
490 0, nullptr,
491 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700492
493 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700494 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700495
egdaniel58a8d922016-04-21 08:03:10 -0700496 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700497 // insert the layout transfer into the queue and wait on the acquire
498 VkSubmitInfo submitInfo;
499 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
500 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
501 submitInfo.waitSemaphoreCount = 1;
502 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700503 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700504 submitInfo.commandBufferCount = 1;
505 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
506 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700507
jvanverth9f372462016-04-06 06:08:59 -0700508 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700509 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700510 backbuffer->fUsageFences[0]));
511
512 return fSurfaces[backbuffer->fImageIndex].get();
513}
514
515
jvanvertha8d0d6c2016-05-05 12:32:03 -0700516void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700517
518 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
519
520 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
521 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
522 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
523 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
524 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
525
526 VkImageMemoryBarrier imageMemoryBarrier = {
527 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
528 NULL, // pNext
529 srcAccessMask, // outputMask
530 dstAccessMask, // inputMask
531 layout, // oldLayout
532 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700533 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700534 fPresentQueueIndex, // dstQueueFamilyIndex
535 fImages[backbuffer->fImageIndex], // image
536 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
537 };
538 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
539 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
540 VkCommandBufferBeginInfo info;
541 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
542 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
543 info.flags = 0;
544 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
545 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
546 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700547 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
548 srcStageMask, dstStageMask, 0,
549 0, nullptr,
550 0, nullptr,
551 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700552 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
553 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
554
555 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
556
557 // insert the layout transfer into the queue and wait on the acquire
558 VkSubmitInfo submitInfo;
559 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
560 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
561 submitInfo.waitSemaphoreCount = 0;
562 submitInfo.pWaitDstStageMask = 0;
563 submitInfo.commandBufferCount = 1;
564 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
565 submitInfo.signalSemaphoreCount = 1;
566 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
567
568 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700569 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700570 backbuffer->fUsageFences[1]));
571
572 // Submit present operation to present queue
573 const VkPresentInfoKHR presentInfo =
574 {
575 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
576 NULL, // pNext
577 1, // waitSemaphoreCount
578 &backbuffer->fRenderSemaphore, // pWaitSemaphores
579 1, // swapchainCount
580 &fSwapchain, // pSwapchains
581 &backbuffer->fImageIndex, // pImageIndices
582 NULL // pResults
583 };
584
jvanverthb0d43522016-04-21 11:46:23 -0700585 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700586
587}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700588
589} //namespace sk_app