blob: da080f84ca1a9fd2b4d11dcf291b1afc7765739e [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"
11#include "VulkanTestContext.h"
12
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
jvanverth9f372462016-04-06 06:08:59 -070025VulkanTestContext::VulkanTestContext(void* platformData, int msaaSampleCount)
26 : fSurface(VK_NULL_HANDLE)
27 , fSwapchain(VK_NULL_HANDLE)
28 , fCommandPool(VK_NULL_HANDLE)
29 , fBackbuffers(nullptr) {
30
31 // any config code here (particularly for msaa)?
32
33 this->initializeContext(platformData);
34}
35
36void VulkanTestContext::initializeContext(void* platformData) {
37
jvanverthb0d43522016-04-21 11:46:23 -070038 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
39 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
40 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
41 fBackendContext.reset(nullptr);
42 return;
43 }
44
45 VkInstance instance = fBackendContext->fInstance;
46 VkDevice device = fBackendContext->fDevice;
47 GET_PROC(DestroySurfaceKHR);
48 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
49 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
50 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
51 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
52 GET_DEV_PROC(CreateSwapchainKHR);
53 GET_DEV_PROC(DestroySwapchainKHR);
54 GET_DEV_PROC(GetSwapchainImagesKHR);
55 GET_DEV_PROC(AcquireNextImageKHR);
56 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070057
58 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext)fBackendContext.get());
59
jvanverthb0d43522016-04-21 11:46:23 -070060 fSurface = createVkSurface(instance, platformData);
jvanverth9f372462016-04-06 06:08:59 -070061 if (VK_NULL_HANDLE == fSurface) {
62 fBackendContext.reset(nullptr);
63 return;
64 }
65
66 // query to get the initial queue props size
67 uint32_t queueCount;
68 GR_VK_CALL(fBackendContext->fInterface,
69 GetPhysicalDeviceQueueFamilyProperties(fBackendContext->fPhysicalDevice, &queueCount,
70 nullptr));
71 SkASSERT(queueCount >= 1);
72
73 SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties));
74 // now get the actual queue props
75 VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAlloc.get();
76
77 GR_VK_CALL(fBackendContext->fInterface,
78 GetPhysicalDeviceQueueFamilyProperties(fBackendContext->fPhysicalDevice, &queueCount,
79 queueProps));
80
jvanverth9f372462016-04-06 06:08:59 -070081 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070082 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
83 fPresentQueueIndex, fSurface,
84 &supported);
jvanverth9f372462016-04-06 06:08:59 -070085 if (VK_SUCCESS != res) {
86 this->destroyContext();
87 return;
88 }
89
jvanverth9fab59d2016-04-06 12:08:51 -070090 if (!this->createSwapchain(-1, -1)) {
jvanverth9f372462016-04-06 06:08:59 -070091 this->destroyContext();
92 return;
93 }
94
95 // create presentQueue
96 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070097}
98
99bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height)
100{
101 // check for capabilities
102 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -0700103 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
104 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700105 if (VK_SUCCESS != res) {
106 return false;
107 }
108
109 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700110 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
111 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700112 if (VK_SUCCESS != res) {
113 return false;
114 }
115
116 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
117 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700118 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
119 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700120 if (VK_SUCCESS != res) {
121 return false;
122 }
123
124 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700125 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
126 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700127 if (VK_SUCCESS != res) {
128 return false;
129 }
130
131 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
132 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700133 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
134 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700135 if (VK_SUCCESS != res) {
136 return false;
137 }
138
139 VkExtent2D extent = caps.currentExtent;
140 // use the hints
141 if (extent.width == (uint32_t)-1) {
142 extent.width = width;
143 extent.height = height;
144 }
145
jvanverth9fab59d2016-04-06 12:08:51 -0700146 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700147 if (extent.width < caps.minImageExtent.width) {
148 extent.width = caps.minImageExtent.width;
149 } else if (extent.width > caps.maxImageExtent.width) {
150 extent.width = caps.maxImageExtent.width;
151 }
152 // clamp height
153 if (extent.height < caps.minImageExtent.height) {
154 extent.height = caps.minImageExtent.height;
155 } else if (extent.height > caps.maxImageExtent.height) {
156 extent.height = caps.maxImageExtent.height;
157 }
158 fWidth = (int)extent.width;
159 fHeight = (int)extent.height;
160
161 uint32_t imageCount = caps.minImageCount + 2;
162 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
163 // Application must settle for fewer images than desired:
164 imageCount = caps.maxImageCount;
165 }
166
167 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
168 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
169 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
170 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
171 SkASSERT(caps.supportedTransforms & caps.currentTransform);
172 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
173 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
174 VkCompositeAlphaFlagBitsKHR composite_alpha =
175 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
176 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
177 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
178
jvanverth3d6ed3a2016-04-07 11:09:51 -0700179 // If mailbox mode is available, use it, as it is the lowest-latency non-
180 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700181 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700182 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700183 // use mailbox
184 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700185 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700186 break;
jvanverth9f372462016-04-06 06:08:59 -0700187 }
188 }
189
190 VkSwapchainCreateInfoKHR swapchainCreateInfo;
191 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
192 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
193 swapchainCreateInfo.surface = fSurface;
194 swapchainCreateInfo.minImageCount = imageCount;
195 swapchainCreateInfo.imageFormat = surfaceFormats[0].format; // for now, use the first one
196 swapchainCreateInfo.imageColorSpace = surfaceFormats[0].colorSpace;
197 swapchainCreateInfo.imageExtent = extent;
198 swapchainCreateInfo.imageArrayLayers = 1;
199 swapchainCreateInfo.imageUsage = usageFlags;
200
jvanverthb0d43522016-04-21 11:46:23 -0700201 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
202 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700203 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
204 swapchainCreateInfo.queueFamilyIndexCount = 2;
205 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
206 } else {
207 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
208 swapchainCreateInfo.queueFamilyIndexCount = 0;
209 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
210 }
211
212 swapchainCreateInfo.preTransform = caps.currentTransform;;
213 swapchainCreateInfo.compositeAlpha = composite_alpha;
214 swapchainCreateInfo.presentMode = mode;
215 swapchainCreateInfo.clipped = true;
216 swapchainCreateInfo.oldSwapchain = fSwapchain;
217
jvanverthb0d43522016-04-21 11:46:23 -0700218 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700219 if (VK_SUCCESS != res) {
220 return false;
221 }
222
223 // destroy the old swapchain
224 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
225 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
226
227 this->destroyBuffers();
228
jvanverthb0d43522016-04-21 11:46:23 -0700229 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700230 }
231
egdaniel58a8d922016-04-21 08:03:10 -0700232 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700233
234 return true;
235}
236
egdaniel58a8d922016-04-21 08:03:10 -0700237void VulkanTestContext::createBuffers(VkFormat format) {
238 GrVkFormatToPixelConfig(format, &fPixelConfig);
239
jvanverthb0d43522016-04-21 11:46:23 -0700240 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700241 SkASSERT(fImageCount);
242 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700243 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700244
245 // set up initial image layouts and create surfaces
246 fImageLayouts = new VkImageLayout[fImageCount];
247 fSurfaces = new sk_sp<SkSurface>[fImageCount];
248 for (uint32_t i = 0; i < fImageCount; ++i) {
249 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
250
251 GrBackendRenderTargetDesc desc;
252 GrVkTextureInfo info;
253 info.fImage = fImages[i];
254 info.fAlloc = nullptr;
255 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
256 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700257 info.fFormat = format;
jvanverth9f372462016-04-06 06:08:59 -0700258 desc.fWidth = fWidth;
259 desc.fHeight = fHeight;
260 desc.fConfig = fPixelConfig;
261 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
262 desc.fSampleCnt = 0;
263 desc.fStencilBits = 0;
264 desc.fRenderTargetHandle = (GrBackendObject) &info;
265 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
266 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
267 }
268
269 // create the command pool for the command buffers
270 if (VK_NULL_HANDLE == fCommandPool) {
271 VkCommandPoolCreateInfo commandPoolInfo;
272 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
273 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
274 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700275 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700276 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
277 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
278 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
279 nullptr, &fCommandPool));
280 }
281
282 // set up the backbuffers
283 VkSemaphoreCreateInfo semaphoreInfo;
284 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
285 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
286 semaphoreInfo.pNext = nullptr;
287 semaphoreInfo.flags = 0;
288 VkCommandBufferAllocateInfo commandBuffersInfo;
289 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
290 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
291 commandBuffersInfo.pNext = nullptr;
292 commandBuffersInfo.commandPool = fCommandPool;
293 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
294 commandBuffersInfo.commandBufferCount = 2;
295 VkFenceCreateInfo fenceInfo;
296 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
297 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
298 fenceInfo.pNext = nullptr;
299 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
300
301 // we create one additional backbuffer structure here, because we want to
302 // give the command buffers they contain a chance to finish before we cycle back
303 fBackbuffers = new BackbufferInfo[fImageCount + 1];
304 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
305 fBackbuffers[i].fImageIndex = -1;
306 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
307 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
308 nullptr, &fBackbuffers[i].fAcquireSemaphore));
309 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
310 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
311 nullptr, &fBackbuffers[i].fRenderSemaphore));
312 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
313 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
314 fBackbuffers[i].fTransitionCmdBuffers));
315 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
316 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
317 &fBackbuffers[i].fUsageFences[0]));
318 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
319 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
320 &fBackbuffers[i].fUsageFences[1]));
321 }
322 fCurrentBackbufferIndex = fImageCount;
323}
324
325void VulkanTestContext::destroyBuffers() {
326
327 if (fBackbuffers) {
328 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
329 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
330 WaitForFences(fBackendContext->fDevice, 2,
331 fBackbuffers[i].fUsageFences,
332 true, UINT64_MAX));
333 fBackbuffers[i].fImageIndex = -1;
334 GR_VK_CALL(fBackendContext->fInterface,
335 DestroySemaphore(fBackendContext->fDevice,
336 fBackbuffers[i].fAcquireSemaphore,
337 nullptr));
338 GR_VK_CALL(fBackendContext->fInterface,
339 DestroySemaphore(fBackendContext->fDevice,
340 fBackbuffers[i].fRenderSemaphore,
341 nullptr));
342 GR_VK_CALL(fBackendContext->fInterface,
343 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
344 fBackbuffers[i].fTransitionCmdBuffers));
345 GR_VK_CALL(fBackendContext->fInterface,
346 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
347 GR_VK_CALL(fBackendContext->fInterface,
348 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
349 }
350 }
351
352 delete[] fBackbuffers;
353 fBackbuffers = nullptr;
354
355 delete[] fSurfaces;
356 fSurfaces = nullptr;
357 delete[] fImageLayouts;
358 fImageLayouts = nullptr;
359 delete[] fImages;
360 fImages = nullptr;
361}
362
363VulkanTestContext::~VulkanTestContext() {
364 this->destroyContext();
365}
366
367void VulkanTestContext::destroyContext() {
368 if (!fBackendContext.get()) {
369 return;
370 }
371
372 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
373
374 this->destroyBuffers();
375
376 if (VK_NULL_HANDLE != fCommandPool) {
377 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
378 fCommandPool, nullptr));
379 fCommandPool = VK_NULL_HANDLE;
380 }
381
382 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700383 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700384 fSwapchain = VK_NULL_HANDLE;
385 }
386
387 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700388 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700389 fSurface = VK_NULL_HANDLE;
390 }
391
392 delete fContext;
393
394 fBackendContext.reset(nullptr);
395}
396
397VulkanTestContext::BackbufferInfo* VulkanTestContext::getAvailableBackbuffer() {
398 SkASSERT(fBackbuffers);
399
400 ++fCurrentBackbufferIndex;
401 if (fCurrentBackbufferIndex > fImageCount) {
402 fCurrentBackbufferIndex = 0;
403 }
404
405 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
406
407 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
408 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
409 true, UINT64_MAX));
410 return backbuffer;
411}
412
413SkSurface* VulkanTestContext::getBackbufferSurface() {
414 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
415 SkASSERT(backbuffer);
416
417 // reset the fence
418 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
419 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
420 // semaphores should be in unsignaled state
421
422 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700423 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
424 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
425 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700426 if (VK_ERROR_SURFACE_LOST_KHR == res) {
427 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700428 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700429 return nullptr;
430 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700431 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700432 // tear swapchain down and try again
433 if (!this->createSwapchain(0, 0)) {
434 return nullptr;
435 }
436
437 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700438 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
439 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
440 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700441
442 if (VK_SUCCESS != res) {
443 return nullptr;
444 }
445 }
446
447 // set up layout transfer from initial to color attachment
448 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
449 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
450 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
451 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
452 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
453 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
454 0 : VK_ACCESS_MEMORY_READ_BIT;
455 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
456
457 VkImageMemoryBarrier imageMemoryBarrier = {
458 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
459 NULL, // pNext
460 srcAccessMask, // outputMask
461 dstAccessMask, // inputMask
462 layout, // oldLayout
463 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
464 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700465 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700466 fImages[backbuffer->fImageIndex], // image
467 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
468 };
469 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
470 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
471 VkCommandBufferBeginInfo info;
472 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
473 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
474 info.flags = 0;
475 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
476 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
477
478 GR_VK_CALL(fBackendContext->fInterface,
479 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
480 srcStageMask, dstStageMask, 0,
481 0, nullptr,
482 0, nullptr,
483 1, &imageMemoryBarrier));
484
485 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
486 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
487
egdaniel58a8d922016-04-21 08:03:10 -0700488 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700489 // insert the layout transfer into the queue and wait on the acquire
490 VkSubmitInfo submitInfo;
491 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
492 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
493 submitInfo.waitSemaphoreCount = 1;
494 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700495 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700496 submitInfo.commandBufferCount = 1;
497 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
498 submitInfo.signalSemaphoreCount = 0;
499
500 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
501 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
502 backbuffer->fUsageFences[0]));
503
504 return fSurfaces[backbuffer->fImageIndex].get();
505}
506
507
508void VulkanTestContext::swapBuffers() {
509
510 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
511
512 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
513 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
514 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
515 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
516 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
517
518 VkImageMemoryBarrier imageMemoryBarrier = {
519 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
520 NULL, // pNext
521 srcAccessMask, // outputMask
522 dstAccessMask, // inputMask
523 layout, // oldLayout
524 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700525 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700526 fPresentQueueIndex, // dstQueueFamilyIndex
527 fImages[backbuffer->fImageIndex], // image
528 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
529 };
530 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
531 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
532 VkCommandBufferBeginInfo info;
533 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
534 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
535 info.flags = 0;
536 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
537 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
538 GR_VK_CALL(fBackendContext->fInterface,
539 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
540 srcStageMask, dstStageMask, 0,
541 0, nullptr,
542 0, nullptr,
543 1, &imageMemoryBarrier));
544 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
545 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
546
547 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
548
549 // insert the layout transfer into the queue and wait on the acquire
550 VkSubmitInfo submitInfo;
551 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
552 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
553 submitInfo.waitSemaphoreCount = 0;
554 submitInfo.pWaitDstStageMask = 0;
555 submitInfo.commandBufferCount = 1;
556 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
557 submitInfo.signalSemaphoreCount = 1;
558 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
559
560 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
561 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
562 backbuffer->fUsageFences[1]));
563
564 // Submit present operation to present queue
565 const VkPresentInfoKHR presentInfo =
566 {
567 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
568 NULL, // pNext
569 1, // waitSemaphoreCount
570 &backbuffer->fRenderSemaphore, // pWaitSemaphores
571 1, // swapchainCount
572 &fSwapchain, // pSwapchains
573 &backbuffer->fImageIndex, // pImageIndices
574 NULL // pResults
575 };
576
jvanverthb0d43522016-04-21 11:46:23 -0700577 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700578
579}