blob: 5087e5d4fd9c4df335bbf6ac3082f76a6805e390 [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) {
jvanverth1d155962016-05-23 13:13:36 -070044 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent,
45 platformData));
jvanverth9f372462016-04-06 06:08:59 -070046
jvanverthb0d43522016-04-21 11:46:23 -070047 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
48 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
49 fBackendContext.reset(nullptr);
50 return;
51 }
52
53 VkInstance instance = fBackendContext->fInstance;
54 VkDevice device = fBackendContext->fDevice;
55 GET_PROC(DestroySurfaceKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
57 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
58 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
59 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
60 GET_DEV_PROC(CreateSwapchainKHR);
61 GET_DEV_PROC(DestroySwapchainKHR);
62 GET_DEV_PROC(GetSwapchainImagesKHR);
63 GET_DEV_PROC(AcquireNextImageKHR);
64 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070065
jvanvertha8d0d6c2016-05-05 12:32:03 -070066 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070067
jvanverthb0d43522016-04-21 11:46:23 -070068 fSurface = createVkSurface(instance, platformData);
jvanverth9f372462016-04-06 06:08:59 -070069 if (VK_NULL_HANDLE == fSurface) {
70 fBackendContext.reset(nullptr);
71 return;
72 }
73
jvanverth9f372462016-04-06 06:08:59 -070074 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070075 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
76 fPresentQueueIndex, fSurface,
77 &supported);
jvanverth9f372462016-04-06 06:08:59 -070078 if (VK_SUCCESS != res) {
79 this->destroyContext();
80 return;
81 }
82
brianosman05de2162016-05-06 13:28:57 -070083 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070084 this->destroyContext();
85 return;
86 }
87
88 // create presentQueue
89 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070090}
91
brianosman05de2162016-05-06 13:28:57 -070092bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height,
93 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070094 // check for capabilities
95 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070096 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
97 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070098 if (VK_SUCCESS != res) {
99 return false;
100 }
101
102 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700103 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
104 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700105 if (VK_SUCCESS != res) {
106 return false;
107 }
108
109 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
110 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700111 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
112 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700113 if (VK_SUCCESS != res) {
114 return false;
115 }
116
117 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700118 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
119 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700120 if (VK_SUCCESS != res) {
121 return false;
122 }
123
124 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
125 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700126 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700127 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700128 if (VK_SUCCESS != res) {
129 return false;
130 }
131
132 VkExtent2D extent = caps.currentExtent;
133 // use the hints
134 if (extent.width == (uint32_t)-1) {
135 extent.width = width;
136 extent.height = height;
137 }
138
jvanverth9fab59d2016-04-06 12:08:51 -0700139 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700140 if (extent.width < caps.minImageExtent.width) {
141 extent.width = caps.minImageExtent.width;
142 } else if (extent.width > caps.maxImageExtent.width) {
143 extent.width = caps.maxImageExtent.width;
144 }
145 // clamp height
146 if (extent.height < caps.minImageExtent.height) {
147 extent.height = caps.minImageExtent.height;
148 } else if (extent.height > caps.maxImageExtent.height) {
149 extent.height = caps.maxImageExtent.height;
150 }
jvanverth1d155962016-05-23 13:13:36 -0700151
jvanverth9f372462016-04-06 06:08:59 -0700152 fWidth = (int)extent.width;
153 fHeight = (int)extent.height;
154
155 uint32_t imageCount = caps.minImageCount + 2;
156 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
157 // Application must settle for fewer images than desired:
158 imageCount = caps.maxImageCount;
159 }
160
161 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
162 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
163 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
164 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
165 SkASSERT(caps.supportedTransforms & caps.currentTransform);
166 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
167 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
168 VkCompositeAlphaFlagBitsKHR composite_alpha =
169 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
170 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
171 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
172
brianosman05de2162016-05-06 13:28:57 -0700173 // Pick our surface format. For now, just make sure it matches our sRGB request:
174 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
175 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
brianosmanb109b8c2016-06-16 13:03:24 -0700176 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
177 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700178 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
179 GrPixelConfig config;
180 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
181 GrPixelConfigIsSRGB(config) == wantSRGB) {
182 surfaceFormat = surfaceFormats[i].format;
183 colorSpace = surfaceFormats[i].colorSpace;
184 break;
185 }
186 }
187 fDisplayParams = params;
188
189 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
190 return false;
191 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700192
jvanverth3d6ed3a2016-04-07 11:09:51 -0700193 // If mailbox mode is available, use it, as it is the lowest-latency non-
194 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700195 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700196 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700197 // use mailbox
198 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700199 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700200 break;
jvanverth9f372462016-04-06 06:08:59 -0700201 }
202 }
203
204 VkSwapchainCreateInfoKHR swapchainCreateInfo;
205 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
206 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
207 swapchainCreateInfo.surface = fSurface;
208 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700209 swapchainCreateInfo.imageFormat = surfaceFormat;
210 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700211 swapchainCreateInfo.imageExtent = extent;
212 swapchainCreateInfo.imageArrayLayers = 1;
213 swapchainCreateInfo.imageUsage = usageFlags;
214
jvanverthb0d43522016-04-21 11:46:23 -0700215 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
216 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700217 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
218 swapchainCreateInfo.queueFamilyIndexCount = 2;
219 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
220 } else {
221 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
222 swapchainCreateInfo.queueFamilyIndexCount = 0;
223 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
224 }
225
226 swapchainCreateInfo.preTransform = caps.currentTransform;;
227 swapchainCreateInfo.compositeAlpha = composite_alpha;
228 swapchainCreateInfo.presentMode = mode;
229 swapchainCreateInfo.clipped = true;
230 swapchainCreateInfo.oldSwapchain = fSwapchain;
231
jvanverthb0d43522016-04-21 11:46:23 -0700232 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700233 if (VK_SUCCESS != res) {
234 return false;
235 }
236
237 // destroy the old swapchain
238 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
239 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
240
241 this->destroyBuffers();
242
jvanverthb0d43522016-04-21 11:46:23 -0700243 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700244 }
245
egdaniel58a8d922016-04-21 08:03:10 -0700246 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700247
248 return true;
249}
250
jvanvertha8d0d6c2016-05-05 12:32:03 -0700251void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700252 GrVkFormatToPixelConfig(format, &fPixelConfig);
253
jvanverthb0d43522016-04-21 11:46:23 -0700254 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700255 SkASSERT(fImageCount);
256 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700257 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700258
259 // set up initial image layouts and create surfaces
260 fImageLayouts = new VkImageLayout[fImageCount];
jvanverthaf236b52016-05-20 06:01:06 -0700261 fRenderTargets = new sk_sp<GrRenderTarget>[fImageCount];
jvanverth9f372462016-04-06 06:08:59 -0700262 fSurfaces = new sk_sp<SkSurface>[fImageCount];
263 for (uint32_t i = 0; i < fImageCount; ++i) {
264 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
265
266 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700267 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700268 info.fImage = fImages[i];
jvanverth6b6ffc42016-06-13 14:28:07 -0700269 info.fAlloc = { VK_NULL_HANDLE, 0, 0 };
jvanverth9f372462016-04-06 06:08:59 -0700270 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
271 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700272 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700273 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700274 desc.fWidth = fWidth;
275 desc.fHeight = fHeight;
276 desc.fConfig = fPixelConfig;
277 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
278 desc.fSampleCnt = 0;
279 desc.fStencilBits = 0;
280 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700281 fRenderTargets[i].reset(fContext->textureProvider()->wrapBackendRenderTarget(desc));
282
283 fSurfaces[i] = this->createRenderSurface(fRenderTargets[i], 24);
jvanverth9f372462016-04-06 06:08:59 -0700284 }
285
286 // create the command pool for the command buffers
287 if (VK_NULL_HANDLE == fCommandPool) {
288 VkCommandPoolCreateInfo commandPoolInfo;
289 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
290 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
291 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700292 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700293 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
294 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
295 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
296 nullptr, &fCommandPool));
297 }
298
299 // set up the backbuffers
300 VkSemaphoreCreateInfo semaphoreInfo;
301 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
302 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
303 semaphoreInfo.pNext = nullptr;
304 semaphoreInfo.flags = 0;
305 VkCommandBufferAllocateInfo commandBuffersInfo;
306 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
307 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
308 commandBuffersInfo.pNext = nullptr;
309 commandBuffersInfo.commandPool = fCommandPool;
310 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
311 commandBuffersInfo.commandBufferCount = 2;
312 VkFenceCreateInfo fenceInfo;
313 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
314 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
315 fenceInfo.pNext = nullptr;
316 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
317
318 // we create one additional backbuffer structure here, because we want to
319 // give the command buffers they contain a chance to finish before we cycle back
320 fBackbuffers = new BackbufferInfo[fImageCount + 1];
321 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
322 fBackbuffers[i].fImageIndex = -1;
323 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
324 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
325 nullptr, &fBackbuffers[i].fAcquireSemaphore));
326 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
327 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
328 nullptr, &fBackbuffers[i].fRenderSemaphore));
329 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
330 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
331 fBackbuffers[i].fTransitionCmdBuffers));
332 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
333 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
334 &fBackbuffers[i].fUsageFences[0]));
335 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
336 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
337 &fBackbuffers[i].fUsageFences[1]));
338 }
339 fCurrentBackbufferIndex = fImageCount;
340}
341
jvanvertha8d0d6c2016-05-05 12:32:03 -0700342void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700343
344 if (fBackbuffers) {
345 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
346 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700347 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700348 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700349 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700350 fBackbuffers[i].fImageIndex = -1;
351 GR_VK_CALL(fBackendContext->fInterface,
352 DestroySemaphore(fBackendContext->fDevice,
353 fBackbuffers[i].fAcquireSemaphore,
354 nullptr));
355 GR_VK_CALL(fBackendContext->fInterface,
356 DestroySemaphore(fBackendContext->fDevice,
357 fBackbuffers[i].fRenderSemaphore,
358 nullptr));
359 GR_VK_CALL(fBackendContext->fInterface,
360 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
361 fBackbuffers[i].fTransitionCmdBuffers));
362 GR_VK_CALL(fBackendContext->fInterface,
363 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
364 GR_VK_CALL(fBackendContext->fInterface,
365 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
366 }
367 }
368
369 delete[] fBackbuffers;
370 fBackbuffers = nullptr;
371
jvanverthaf236b52016-05-20 06:01:06 -0700372 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700373 delete[] fSurfaces;
374 fSurfaces = nullptr;
jvanverthaf236b52016-05-20 06:01:06 -0700375 delete[] fRenderTargets;
376 fRenderTargets = nullptr;
jvanverth9f372462016-04-06 06:08:59 -0700377 delete[] fImageLayouts;
378 fImageLayouts = nullptr;
379 delete[] fImages;
380 fImages = nullptr;
381}
382
jvanvertha8d0d6c2016-05-05 12:32:03 -0700383VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700384 this->destroyContext();
385}
386
jvanvertha8d0d6c2016-05-05 12:32:03 -0700387void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700388 if (!fBackendContext.get()) {
389 return;
390 }
391
jvanverth85f758c2016-05-27 06:47:08 -0700392 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700393 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
394
395 this->destroyBuffers();
396
397 if (VK_NULL_HANDLE != fCommandPool) {
398 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
399 fCommandPool, nullptr));
400 fCommandPool = VK_NULL_HANDLE;
401 }
402
403 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700404 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700405 fSwapchain = VK_NULL_HANDLE;
406 }
407
408 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700409 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700410 fSurface = VK_NULL_HANDLE;
411 }
412
jvanverthaf236b52016-05-20 06:01:06 -0700413 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700414
415 fBackendContext.reset(nullptr);
416}
417
jvanvertha8d0d6c2016-05-05 12:32:03 -0700418VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700419 SkASSERT(fBackbuffers);
420
421 ++fCurrentBackbufferIndex;
422 if (fCurrentBackbufferIndex > fImageCount) {
423 fCurrentBackbufferIndex = 0;
424 }
425
426 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
427
428 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
429 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
430 true, UINT64_MAX));
431 return backbuffer;
432}
433
jvanverthaf236b52016-05-20 06:01:06 -0700434sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700435 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
436 SkASSERT(backbuffer);
437
438 // reset the fence
439 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
440 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
441 // semaphores should be in unsignaled state
442
443 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700444 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
445 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
446 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700447 if (VK_ERROR_SURFACE_LOST_KHR == res) {
448 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700449 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700450 return nullptr;
451 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700452 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700453 // tear swapchain down and try again
brianosman05de2162016-05-06 13:28:57 -0700454 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700455 return nullptr;
456 }
457
458 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700459 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700460 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
461 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700462
463 if (VK_SUCCESS != res) {
464 return nullptr;
465 }
466 }
467
468 // set up layout transfer from initial to color attachment
469 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
470 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
471 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
472 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
473 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700474 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700475 0 : VK_ACCESS_MEMORY_READ_BIT;
476 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
477
478 VkImageMemoryBarrier imageMemoryBarrier = {
479 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
480 NULL, // pNext
481 srcAccessMask, // outputMask
482 dstAccessMask, // inputMask
483 layout, // oldLayout
484 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
485 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700486 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700487 fImages[backbuffer->fImageIndex], // image
488 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
489 };
490 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
491 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
492 VkCommandBufferBeginInfo info;
493 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
494 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
495 info.flags = 0;
496 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
497 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
498
jvanvertha8d0d6c2016-05-05 12:32:03 -0700499 GR_VK_CALL(fBackendContext->fInterface,
500 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
501 srcStageMask, dstStageMask, 0,
502 0, nullptr,
503 0, nullptr,
504 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700505
506 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700507 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700508
egdaniel58a8d922016-04-21 08:03:10 -0700509 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700510 // insert the layout transfer into the queue and wait on the acquire
511 VkSubmitInfo submitInfo;
512 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
513 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
514 submitInfo.waitSemaphoreCount = 1;
515 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700516 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700517 submitInfo.commandBufferCount = 1;
518 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
519 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700520
jvanverth9f372462016-04-06 06:08:59 -0700521 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700522 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700523 backbuffer->fUsageFences[0]));
524
jvanverthaf236b52016-05-20 06:01:06 -0700525 return sk_ref_sp(fSurfaces[backbuffer->fImageIndex].get());
jvanverth9f372462016-04-06 06:08:59 -0700526}
527
jvanvertha8d0d6c2016-05-05 12:32:03 -0700528void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700529
530 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
531
jvanverthaf236b52016-05-20 06:01:06 -0700532 this->presentRenderSurface(fSurfaces[backbuffer->fImageIndex],
533 fRenderTargets[backbuffer->fImageIndex], 24);
534
jvanverth9f372462016-04-06 06:08:59 -0700535 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
536 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
537 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
538 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
539 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
540
541 VkImageMemoryBarrier imageMemoryBarrier = {
542 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
543 NULL, // pNext
544 srcAccessMask, // outputMask
545 dstAccessMask, // inputMask
546 layout, // oldLayout
547 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700548 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700549 fPresentQueueIndex, // dstQueueFamilyIndex
550 fImages[backbuffer->fImageIndex], // image
551 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
552 };
553 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
554 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
555 VkCommandBufferBeginInfo info;
556 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
557 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
558 info.flags = 0;
559 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
560 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
561 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700562 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
563 srcStageMask, dstStageMask, 0,
564 0, nullptr,
565 0, nullptr,
566 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700567 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
568 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
569
570 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
571
572 // insert the layout transfer into the queue and wait on the acquire
573 VkSubmitInfo submitInfo;
574 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
575 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
576 submitInfo.waitSemaphoreCount = 0;
577 submitInfo.pWaitDstStageMask = 0;
578 submitInfo.commandBufferCount = 1;
579 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
580 submitInfo.signalSemaphoreCount = 1;
581 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
582
583 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700584 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700585 backbuffer->fUsageFences[1]));
586
587 // Submit present operation to present queue
588 const VkPresentInfoKHR presentInfo =
589 {
590 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
591 NULL, // pNext
592 1, // waitSemaphoreCount
593 &backbuffer->fRenderSemaphore, // pWaitSemaphores
594 1, // swapchainCount
595 &fSwapchain, // pSwapchains
596 &backbuffer->fImageIndex, // pImageIndices
597 NULL // pResults
598 };
599
jvanverthb0d43522016-04-21 11:46:23 -0700600 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700601
602}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700603
604} //namespace sk_app