blob: b940656357bb5c53dbc66218ae8f4b8ea6c88e28 [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
22VulkanTestContext::VulkanTestContext(void* platformData, int msaaSampleCount)
23 : fSurface(VK_NULL_HANDLE)
24 , fSwapchain(VK_NULL_HANDLE)
25 , fCommandPool(VK_NULL_HANDLE)
26 , fBackbuffers(nullptr) {
27
28 // any config code here (particularly for msaa)?
29
30 this->initializeContext(platformData);
31}
32
33void VulkanTestContext::initializeContext(void* platformData) {
34
35 fBackendContext.reset(GrVkBackendContext::Create());
jvanverth9f372462016-04-06 06:08:59 -070036
37 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext)fBackendContext.get());
38
39 fSurface = createVkSurface(platformData);
40 if (VK_NULL_HANDLE == fSurface) {
41 fBackendContext.reset(nullptr);
42 return;
43 }
44
45 // query to get the initial queue props size
46 uint32_t queueCount;
47 GR_VK_CALL(fBackendContext->fInterface,
48 GetPhysicalDeviceQueueFamilyProperties(fBackendContext->fPhysicalDevice, &queueCount,
49 nullptr));
50 SkASSERT(queueCount >= 1);
51
52 SkAutoMalloc queuePropsAlloc(queueCount * sizeof(VkQueueFamilyProperties));
53 // now get the actual queue props
54 VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*)queuePropsAlloc.get();
55
56 GR_VK_CALL(fBackendContext->fInterface,
57 GetPhysicalDeviceQueueFamilyProperties(fBackendContext->fPhysicalDevice, &queueCount,
58 queueProps));
59
60 // iterate to find the present queue
61 fPresentQueueIndex = -1;
62 for (uint32_t i = 0; i < queueCount; i++) {
63 if ((queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && canPresent(i)) {
64 fPresentQueueIndex = i;
65 break;
66 }
67 }
jvanverth3d6ed3a2016-04-07 11:09:51 -070068 SkASSERT(fPresentQueueIndex < queueCount);
jvanverth9f372462016-04-06 06:08:59 -070069
70 VkBool32 supported;
71 VkResult res = GR_VK_CALL(fBackendContext->fInterface,
72 GetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
73 fPresentQueueIndex,
74 fSurface,
75 &supported));
76 if (VK_SUCCESS != res) {
77 this->destroyContext();
78 return;
79 }
80
jvanverth9fab59d2016-04-06 12:08:51 -070081 if (!this->createSwapchain(-1, -1)) {
jvanverth9f372462016-04-06 06:08:59 -070082 this->destroyContext();
83 return;
84 }
85
86 // create presentQueue
87 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
88
89
90}
91
92bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height)
93{
94 // check for capabilities
95 VkSurfaceCapabilitiesKHR caps;
96 VkResult res = GR_VK_CALL(fBackendContext->fInterface,
97 GetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
98 fSurface,
99 &caps));
100 if (VK_SUCCESS != res) {
101 return false;
102 }
103
104 uint32_t surfaceFormatCount;
105 res = GR_VK_CALL(fBackendContext->fInterface,
106 GetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice,
107 fSurface,
108 &surfaceFormatCount,
109 nullptr));
110 if (VK_SUCCESS != res) {
111 return false;
112 }
113
114 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
115 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
116 res = GR_VK_CALL(fBackendContext->fInterface,
117 GetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice,
118 fSurface,
119 &surfaceFormatCount,
120 surfaceFormats));
121 if (VK_SUCCESS != res) {
122 return false;
123 }
124
125 uint32_t presentModeCount;
126 res = GR_VK_CALL(fBackendContext->fInterface,
127 GetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice,
128 fSurface,
129 &presentModeCount,
130 nullptr));
131 if (VK_SUCCESS != res) {
132 return false;
133 }
134
135 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
136 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
137 res = GR_VK_CALL(fBackendContext->fInterface,
138 GetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice,
139 fSurface,
140 &presentModeCount,
141 presentModes));
142 if (VK_SUCCESS != res) {
143 return false;
144 }
145
146 VkExtent2D extent = caps.currentExtent;
147 // use the hints
148 if (extent.width == (uint32_t)-1) {
149 extent.width = width;
150 extent.height = height;
151 }
152
jvanverth9fab59d2016-04-06 12:08:51 -0700153 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700154 if (extent.width < caps.minImageExtent.width) {
155 extent.width = caps.minImageExtent.width;
156 } else if (extent.width > caps.maxImageExtent.width) {
157 extent.width = caps.maxImageExtent.width;
158 }
159 // clamp height
160 if (extent.height < caps.minImageExtent.height) {
161 extent.height = caps.minImageExtent.height;
162 } else if (extent.height > caps.maxImageExtent.height) {
163 extent.height = caps.maxImageExtent.height;
164 }
165 fWidth = (int)extent.width;
166 fHeight = (int)extent.height;
167
168 uint32_t imageCount = caps.minImageCount + 2;
169 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
170 // Application must settle for fewer images than desired:
171 imageCount = caps.maxImageCount;
172 }
173
174 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
175 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
176 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
177 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
178 SkASSERT(caps.supportedTransforms & caps.currentTransform);
179 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
180 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
181 VkCompositeAlphaFlagBitsKHR composite_alpha =
182 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
183 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
184 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
185
jvanverth3d6ed3a2016-04-07 11:09:51 -0700186 // If mailbox mode is available, use it, as it is the lowest-latency non-
187 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700188 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700189 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700190 // use mailbox
191 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700192 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700193 break;
jvanverth9f372462016-04-06 06:08:59 -0700194 }
195 }
196
197 VkSwapchainCreateInfoKHR swapchainCreateInfo;
198 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
199 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
200 swapchainCreateInfo.surface = fSurface;
201 swapchainCreateInfo.minImageCount = imageCount;
202 swapchainCreateInfo.imageFormat = surfaceFormats[0].format; // for now, use the first one
203 swapchainCreateInfo.imageColorSpace = surfaceFormats[0].colorSpace;
204 swapchainCreateInfo.imageExtent = extent;
205 swapchainCreateInfo.imageArrayLayers = 1;
206 swapchainCreateInfo.imageUsage = usageFlags;
207
208 uint32_t queueFamilies[] = { fBackendContext->fQueueFamilyIndex, fPresentQueueIndex };
209 if (fBackendContext->fQueueFamilyIndex != fPresentQueueIndex) {
210 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
211 swapchainCreateInfo.queueFamilyIndexCount = 2;
212 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
213 } else {
214 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
215 swapchainCreateInfo.queueFamilyIndexCount = 0;
216 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
217 }
218
219 swapchainCreateInfo.preTransform = caps.currentTransform;;
220 swapchainCreateInfo.compositeAlpha = composite_alpha;
221 swapchainCreateInfo.presentMode = mode;
222 swapchainCreateInfo.clipped = true;
223 swapchainCreateInfo.oldSwapchain = fSwapchain;
224
225 res = GR_VK_CALL(fBackendContext->fInterface,
226 CreateSwapchainKHR(fBackendContext->fDevice,
227 &swapchainCreateInfo, nullptr, &fSwapchain));
228 if (VK_SUCCESS != res) {
229 return false;
230 }
231
232 // destroy the old swapchain
233 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
234 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
235
236 this->destroyBuffers();
237
238 GR_VK_CALL(fBackendContext->fInterface, DestroySwapchainKHR(fBackendContext->fDevice,
239 swapchainCreateInfo.oldSwapchain,
240 nullptr));
241 }
242
243 GrVkFormatToPixelConfig(swapchainCreateInfo.imageFormat, &fPixelConfig);
244
245 this->createBuffers();
246
247 return true;
248}
249
250void VulkanTestContext::createBuffers() {
251 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, GetSwapchainImagesKHR(fBackendContext->fDevice,
252 fSwapchain,
253 &fImageCount,
254 nullptr));
255 SkASSERT(fImageCount);
256 fImages = new VkImage[fImageCount];
257 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, GetSwapchainImagesKHR(fBackendContext->fDevice,
258 fSwapchain,
259 &fImageCount,
260 fImages));
261
262 // set up initial image layouts and create surfaces
263 fImageLayouts = new VkImageLayout[fImageCount];
264 fSurfaces = new sk_sp<SkSurface>[fImageCount];
265 for (uint32_t i = 0; i < fImageCount; ++i) {
266 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
267
268 GrBackendRenderTargetDesc desc;
269 GrVkTextureInfo info;
270 info.fImage = fImages[i];
271 info.fAlloc = nullptr;
272 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
273 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
274 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;
281 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
282 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
283 }
284
285 // create the command pool for the command buffers
286 if (VK_NULL_HANDLE == fCommandPool) {
287 VkCommandPoolCreateInfo commandPoolInfo;
288 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
289 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
290 // this needs to be on the render queue
291 commandPoolInfo.queueFamilyIndex = fBackendContext->fQueueFamilyIndex;
292 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
293 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
294 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
295 nullptr, &fCommandPool));
296 }
297
298 // set up the backbuffers
299 VkSemaphoreCreateInfo semaphoreInfo;
300 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
301 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
302 semaphoreInfo.pNext = nullptr;
303 semaphoreInfo.flags = 0;
304 VkCommandBufferAllocateInfo commandBuffersInfo;
305 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
306 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
307 commandBuffersInfo.pNext = nullptr;
308 commandBuffersInfo.commandPool = fCommandPool;
309 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
310 commandBuffersInfo.commandBufferCount = 2;
311 VkFenceCreateInfo fenceInfo;
312 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
313 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
314 fenceInfo.pNext = nullptr;
315 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
316
317 // we create one additional backbuffer structure here, because we want to
318 // give the command buffers they contain a chance to finish before we cycle back
319 fBackbuffers = new BackbufferInfo[fImageCount + 1];
320 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
321 fBackbuffers[i].fImageIndex = -1;
322 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
323 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
324 nullptr, &fBackbuffers[i].fAcquireSemaphore));
325 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
326 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
327 nullptr, &fBackbuffers[i].fRenderSemaphore));
328 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
329 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
330 fBackbuffers[i].fTransitionCmdBuffers));
331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
332 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
333 &fBackbuffers[i].fUsageFences[0]));
334 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
335 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
336 &fBackbuffers[i].fUsageFences[1]));
337 }
338 fCurrentBackbufferIndex = fImageCount;
339}
340
341void VulkanTestContext::destroyBuffers() {
342
343 if (fBackbuffers) {
344 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
345 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
346 WaitForFences(fBackendContext->fDevice, 2,
347 fBackbuffers[i].fUsageFences,
348 true, UINT64_MAX));
349 fBackbuffers[i].fImageIndex = -1;
350 GR_VK_CALL(fBackendContext->fInterface,
351 DestroySemaphore(fBackendContext->fDevice,
352 fBackbuffers[i].fAcquireSemaphore,
353 nullptr));
354 GR_VK_CALL(fBackendContext->fInterface,
355 DestroySemaphore(fBackendContext->fDevice,
356 fBackbuffers[i].fRenderSemaphore,
357 nullptr));
358 GR_VK_CALL(fBackendContext->fInterface,
359 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
360 fBackbuffers[i].fTransitionCmdBuffers));
361 GR_VK_CALL(fBackendContext->fInterface,
362 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
363 GR_VK_CALL(fBackendContext->fInterface,
364 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
365 }
366 }
367
368 delete[] fBackbuffers;
369 fBackbuffers = nullptr;
370
371 delete[] fSurfaces;
372 fSurfaces = nullptr;
373 delete[] fImageLayouts;
374 fImageLayouts = nullptr;
375 delete[] fImages;
376 fImages = nullptr;
377}
378
379VulkanTestContext::~VulkanTestContext() {
380 this->destroyContext();
381}
382
383void VulkanTestContext::destroyContext() {
384 if (!fBackendContext.get()) {
385 return;
386 }
387
388 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
389
390 this->destroyBuffers();
391
392 if (VK_NULL_HANDLE != fCommandPool) {
393 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
394 fCommandPool, nullptr));
395 fCommandPool = VK_NULL_HANDLE;
396 }
397
398 if (VK_NULL_HANDLE != fSwapchain) {
399 GR_VK_CALL(fBackendContext->fInterface, DestroySwapchainKHR(fBackendContext->fDevice,
400 fSwapchain, nullptr));
401 fSwapchain = VK_NULL_HANDLE;
402 }
403
404 if (VK_NULL_HANDLE != fSurface) {
405 GR_VK_CALL(fBackendContext->fInterface, DestroySurfaceKHR(fBackendContext->fInstance,
406 fSurface, nullptr));
407 fSurface = VK_NULL_HANDLE;
408 }
409
410 delete fContext;
411
412 fBackendContext.reset(nullptr);
413}
414
415VulkanTestContext::BackbufferInfo* VulkanTestContext::getAvailableBackbuffer() {
416 SkASSERT(fBackbuffers);
417
418 ++fCurrentBackbufferIndex;
419 if (fCurrentBackbufferIndex > fImageCount) {
420 fCurrentBackbufferIndex = 0;
421 }
422
423 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
424
425 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
426 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
427 true, UINT64_MAX));
428 return backbuffer;
429}
430
431SkSurface* VulkanTestContext::getBackbufferSurface() {
432 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
433 SkASSERT(backbuffer);
434
435 // reset the fence
436 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
437 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
438 // semaphores should be in unsignaled state
439
440 // acquire the image
441 VkResult res = GR_VK_CALL(fBackendContext->fInterface,
442 AcquireNextImageKHR(fBackendContext->fDevice,
443 fSwapchain,
444 UINT64_MAX,
445 backbuffer->fAcquireSemaphore,
446 VK_NULL_HANDLE,
447 &backbuffer->fImageIndex));
448 if (VK_ERROR_SURFACE_LOST_KHR == res) {
449 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700450 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700451 return nullptr;
452 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700453 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700454 // tear swapchain down and try again
455 if (!this->createSwapchain(0, 0)) {
456 return nullptr;
457 }
458
459 // acquire the image
460 res = GR_VK_CALL(fBackendContext->fInterface,
461 AcquireNextImageKHR(fBackendContext->fDevice,
462 fSwapchain,
463 UINT64_MAX,
464 backbuffer->fAcquireSemaphore,
465 VK_NULL_HANDLE,
466 &backbuffer->fImageIndex));
467
468 if (VK_SUCCESS != res) {
469 return nullptr;
470 }
471 }
472
473 // set up layout transfer from initial to color attachment
474 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
475 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
476 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
477 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
478 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
479 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
480 0 : VK_ACCESS_MEMORY_READ_BIT;
481 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
482
483 VkImageMemoryBarrier imageMemoryBarrier = {
484 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
485 NULL, // pNext
486 srcAccessMask, // outputMask
487 dstAccessMask, // inputMask
488 layout, // oldLayout
489 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
490 fPresentQueueIndex, // srcQueueFamilyIndex
491 fBackendContext->fQueueFamilyIndex, // dstQueueFamilyIndex
492 fImages[backbuffer->fImageIndex], // image
493 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
494 };
495 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
496 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
497 VkCommandBufferBeginInfo info;
498 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
499 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
500 info.flags = 0;
501 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
502 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
503
504 GR_VK_CALL(fBackendContext->fInterface,
505 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
506 srcStageMask, dstStageMask, 0,
507 0, nullptr,
508 0, nullptr,
509 1, &imageMemoryBarrier));
510
511 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
512 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
513
514 // insert the layout transfer into the queue and wait on the acquire
515 VkSubmitInfo submitInfo;
516 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
517 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
518 submitInfo.waitSemaphoreCount = 1;
519 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
520 submitInfo.pWaitDstStageMask = 0;
521 submitInfo.commandBufferCount = 1;
522 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
523 submitInfo.signalSemaphoreCount = 0;
524
525 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
526 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
527 backbuffer->fUsageFences[0]));
528
529 return fSurfaces[backbuffer->fImageIndex].get();
530}
531
532
533void VulkanTestContext::swapBuffers() {
534
535 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
536
537 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
538 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
539 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
540 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
541 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
542
543 VkImageMemoryBarrier imageMemoryBarrier = {
544 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
545 NULL, // pNext
546 srcAccessMask, // outputMask
547 dstAccessMask, // inputMask
548 layout, // oldLayout
549 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
550 fBackendContext->fQueueFamilyIndex, // srcQueueFamilyIndex
551 fPresentQueueIndex, // dstQueueFamilyIndex
552 fImages[backbuffer->fImageIndex], // image
553 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
554 };
555 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
556 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
557 VkCommandBufferBeginInfo info;
558 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
559 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
560 info.flags = 0;
561 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
562 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
563 GR_VK_CALL(fBackendContext->fInterface,
564 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
565 srcStageMask, dstStageMask, 0,
566 0, nullptr,
567 0, nullptr,
568 1, &imageMemoryBarrier));
569 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
570 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
571
572 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
573
574 // insert the layout transfer into the queue and wait on the acquire
575 VkSubmitInfo submitInfo;
576 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
577 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
578 submitInfo.waitSemaphoreCount = 0;
579 submitInfo.pWaitDstStageMask = 0;
580 submitInfo.commandBufferCount = 1;
581 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
582 submitInfo.signalSemaphoreCount = 1;
583 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
584
585 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
586 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
587 backbuffer->fUsageFences[1]));
588
589 // Submit present operation to present queue
590 const VkPresentInfoKHR presentInfo =
591 {
592 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
593 NULL, // pNext
594 1, // waitSemaphoreCount
595 &backbuffer->fRenderSemaphore, // pWaitSemaphores
596 1, // swapchainCount
597 &fSwapchain, // pSwapchains
598 &backbuffer->fImageIndex, // pImageIndices
599 NULL // pResults
600 };
601
602 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
603 QueuePresentKHR(fPresentQueue, &presentInfo));
604
605}