blob: 0e07734fd13a00018c8b595b135e73d89913688d [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
jvanverth9f372462016-04-06 06:08:59 -070066 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070067 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
68 fPresentQueueIndex, fSurface,
69 &supported);
jvanverth9f372462016-04-06 06:08:59 -070070 if (VK_SUCCESS != res) {
71 this->destroyContext();
72 return;
73 }
74
jvanverth9fab59d2016-04-06 12:08:51 -070075 if (!this->createSwapchain(-1, -1)) {
jvanverth9f372462016-04-06 06:08:59 -070076 this->destroyContext();
77 return;
78 }
79
80 // create presentQueue
81 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070082}
83
84bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height)
85{
86 // check for capabilities
87 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070088 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
89 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070090 if (VK_SUCCESS != res) {
91 return false;
92 }
93
94 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -070095 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
96 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -070097 if (VK_SUCCESS != res) {
98 return false;
99 }
100
101 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
102 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700103 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
104 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700105 if (VK_SUCCESS != res) {
106 return false;
107 }
108
109 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700110 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
111 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700112 if (VK_SUCCESS != res) {
113 return false;
114 }
115
116 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
117 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700118 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
119 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700120 if (VK_SUCCESS != res) {
121 return false;
122 }
123
124 VkExtent2D extent = caps.currentExtent;
125 // use the hints
126 if (extent.width == (uint32_t)-1) {
127 extent.width = width;
128 extent.height = height;
129 }
130
jvanverth9fab59d2016-04-06 12:08:51 -0700131 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700132 if (extent.width < caps.minImageExtent.width) {
133 extent.width = caps.minImageExtent.width;
134 } else if (extent.width > caps.maxImageExtent.width) {
135 extent.width = caps.maxImageExtent.width;
136 }
137 // clamp height
138 if (extent.height < caps.minImageExtent.height) {
139 extent.height = caps.minImageExtent.height;
140 } else if (extent.height > caps.maxImageExtent.height) {
141 extent.height = caps.maxImageExtent.height;
142 }
143 fWidth = (int)extent.width;
144 fHeight = (int)extent.height;
145
146 uint32_t imageCount = caps.minImageCount + 2;
147 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
148 // Application must settle for fewer images than desired:
149 imageCount = caps.maxImageCount;
150 }
151
152 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
153 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
154 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
155 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
156 SkASSERT(caps.supportedTransforms & caps.currentTransform);
157 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
158 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
159 VkCompositeAlphaFlagBitsKHR composite_alpha =
160 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
161 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
162 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
163
jvanvertha4b0fed2016-04-27 11:42:21 -0700164 // Pick our surface format -- for now, the first one
165 VkFormat surfaceFormat = surfaceFormats[0].format;
166 VkColorSpaceKHR colorSpace = surfaceFormats[0].colorSpace;
167
jvanverth3d6ed3a2016-04-07 11:09:51 -0700168 // If mailbox mode is available, use it, as it is the lowest-latency non-
169 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700170 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700171 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700172 // use mailbox
173 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700174 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700175 break;
jvanverth9f372462016-04-06 06:08:59 -0700176 }
177 }
178
179 VkSwapchainCreateInfoKHR swapchainCreateInfo;
180 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
181 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
182 swapchainCreateInfo.surface = fSurface;
183 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700184 swapchainCreateInfo.imageFormat = surfaceFormat;
185 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700186 swapchainCreateInfo.imageExtent = extent;
187 swapchainCreateInfo.imageArrayLayers = 1;
188 swapchainCreateInfo.imageUsage = usageFlags;
189
jvanverthb0d43522016-04-21 11:46:23 -0700190 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
191 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700192 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
193 swapchainCreateInfo.queueFamilyIndexCount = 2;
194 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
195 } else {
196 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
197 swapchainCreateInfo.queueFamilyIndexCount = 0;
198 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
199 }
200
201 swapchainCreateInfo.preTransform = caps.currentTransform;;
202 swapchainCreateInfo.compositeAlpha = composite_alpha;
203 swapchainCreateInfo.presentMode = mode;
204 swapchainCreateInfo.clipped = true;
205 swapchainCreateInfo.oldSwapchain = fSwapchain;
206
jvanverthb0d43522016-04-21 11:46:23 -0700207 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700208 if (VK_SUCCESS != res) {
209 return false;
210 }
211
212 // destroy the old swapchain
213 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
214 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
215
216 this->destroyBuffers();
217
jvanverthb0d43522016-04-21 11:46:23 -0700218 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700219 }
220
egdaniel58a8d922016-04-21 08:03:10 -0700221 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700222
223 return true;
224}
225
egdaniel58a8d922016-04-21 08:03:10 -0700226void VulkanTestContext::createBuffers(VkFormat format) {
227 GrVkFormatToPixelConfig(format, &fPixelConfig);
228
jvanverthb0d43522016-04-21 11:46:23 -0700229 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700230 SkASSERT(fImageCount);
231 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700232 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700233
234 // set up initial image layouts and create surfaces
235 fImageLayouts = new VkImageLayout[fImageCount];
236 fSurfaces = new sk_sp<SkSurface>[fImageCount];
237 for (uint32_t i = 0; i < fImageCount; ++i) {
238 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
239
240 GrBackendRenderTargetDesc desc;
241 GrVkTextureInfo info;
242 info.fImage = fImages[i];
243 info.fAlloc = nullptr;
244 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
245 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700246 info.fFormat = format;
jvanverth9f372462016-04-06 06:08:59 -0700247 desc.fWidth = fWidth;
248 desc.fHeight = fHeight;
249 desc.fConfig = fPixelConfig;
250 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
251 desc.fSampleCnt = 0;
252 desc.fStencilBits = 0;
253 desc.fRenderTargetHandle = (GrBackendObject) &info;
254 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
255 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
256 }
257
258 // create the command pool for the command buffers
259 if (VK_NULL_HANDLE == fCommandPool) {
260 VkCommandPoolCreateInfo commandPoolInfo;
261 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
262 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
263 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700264 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700265 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
266 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
267 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
268 nullptr, &fCommandPool));
269 }
270
271 // set up the backbuffers
272 VkSemaphoreCreateInfo semaphoreInfo;
273 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
274 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
275 semaphoreInfo.pNext = nullptr;
276 semaphoreInfo.flags = 0;
277 VkCommandBufferAllocateInfo commandBuffersInfo;
278 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
279 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
280 commandBuffersInfo.pNext = nullptr;
281 commandBuffersInfo.commandPool = fCommandPool;
282 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
283 commandBuffersInfo.commandBufferCount = 2;
284 VkFenceCreateInfo fenceInfo;
285 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
286 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
287 fenceInfo.pNext = nullptr;
288 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
289
290 // we create one additional backbuffer structure here, because we want to
291 // give the command buffers they contain a chance to finish before we cycle back
292 fBackbuffers = new BackbufferInfo[fImageCount + 1];
293 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
294 fBackbuffers[i].fImageIndex = -1;
295 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
296 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
297 nullptr, &fBackbuffers[i].fAcquireSemaphore));
298 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
299 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
300 nullptr, &fBackbuffers[i].fRenderSemaphore));
301 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
302 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
303 fBackbuffers[i].fTransitionCmdBuffers));
304 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
305 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
306 &fBackbuffers[i].fUsageFences[0]));
307 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
308 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
309 &fBackbuffers[i].fUsageFences[1]));
310 }
311 fCurrentBackbufferIndex = fImageCount;
312}
313
314void VulkanTestContext::destroyBuffers() {
315
316 if (fBackbuffers) {
317 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
318 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
319 WaitForFences(fBackendContext->fDevice, 2,
320 fBackbuffers[i].fUsageFences,
321 true, UINT64_MAX));
322 fBackbuffers[i].fImageIndex = -1;
323 GR_VK_CALL(fBackendContext->fInterface,
324 DestroySemaphore(fBackendContext->fDevice,
325 fBackbuffers[i].fAcquireSemaphore,
326 nullptr));
327 GR_VK_CALL(fBackendContext->fInterface,
328 DestroySemaphore(fBackendContext->fDevice,
329 fBackbuffers[i].fRenderSemaphore,
330 nullptr));
331 GR_VK_CALL(fBackendContext->fInterface,
332 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
333 fBackbuffers[i].fTransitionCmdBuffers));
334 GR_VK_CALL(fBackendContext->fInterface,
335 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
336 GR_VK_CALL(fBackendContext->fInterface,
337 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
338 }
339 }
340
341 delete[] fBackbuffers;
342 fBackbuffers = nullptr;
343
344 delete[] fSurfaces;
345 fSurfaces = nullptr;
346 delete[] fImageLayouts;
347 fImageLayouts = nullptr;
348 delete[] fImages;
349 fImages = nullptr;
350}
351
352VulkanTestContext::~VulkanTestContext() {
353 this->destroyContext();
354}
355
356void VulkanTestContext::destroyContext() {
357 if (!fBackendContext.get()) {
358 return;
359 }
360
361 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
362
363 this->destroyBuffers();
364
365 if (VK_NULL_HANDLE != fCommandPool) {
366 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
367 fCommandPool, nullptr));
368 fCommandPool = VK_NULL_HANDLE;
369 }
370
371 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700372 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700373 fSwapchain = VK_NULL_HANDLE;
374 }
375
376 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700377 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700378 fSurface = VK_NULL_HANDLE;
379 }
380
381 delete fContext;
382
383 fBackendContext.reset(nullptr);
384}
385
386VulkanTestContext::BackbufferInfo* VulkanTestContext::getAvailableBackbuffer() {
387 SkASSERT(fBackbuffers);
388
389 ++fCurrentBackbufferIndex;
390 if (fCurrentBackbufferIndex > fImageCount) {
391 fCurrentBackbufferIndex = 0;
392 }
393
394 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
395
396 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
397 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
398 true, UINT64_MAX));
399 return backbuffer;
400}
401
402SkSurface* VulkanTestContext::getBackbufferSurface() {
403 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
404 SkASSERT(backbuffer);
405
406 // reset the fence
407 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
408 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
409 // semaphores should be in unsignaled state
410
411 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700412 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
413 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
414 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700415 if (VK_ERROR_SURFACE_LOST_KHR == res) {
416 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700417 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700418 return nullptr;
419 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700420 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700421 // tear swapchain down and try again
422 if (!this->createSwapchain(0, 0)) {
423 return nullptr;
424 }
425
426 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700427 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
428 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
429 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700430
431 if (VK_SUCCESS != res) {
432 return nullptr;
433 }
434 }
435
436 // set up layout transfer from initial to color attachment
437 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
438 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
439 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
440 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
441 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
442 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
443 0 : VK_ACCESS_MEMORY_READ_BIT;
444 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
445
446 VkImageMemoryBarrier imageMemoryBarrier = {
447 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
448 NULL, // pNext
449 srcAccessMask, // outputMask
450 dstAccessMask, // inputMask
451 layout, // oldLayout
452 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
453 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700454 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700455 fImages[backbuffer->fImageIndex], // image
456 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
457 };
458 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
459 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
460 VkCommandBufferBeginInfo info;
461 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
462 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
463 info.flags = 0;
464 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
465 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
466
467 GR_VK_CALL(fBackendContext->fInterface,
468 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
469 srcStageMask, dstStageMask, 0,
470 0, nullptr,
471 0, nullptr,
472 1, &imageMemoryBarrier));
473
474 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
475 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
476
egdaniel58a8d922016-04-21 08:03:10 -0700477 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700478 // insert the layout transfer into the queue and wait on the acquire
479 VkSubmitInfo submitInfo;
480 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
481 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
482 submitInfo.waitSemaphoreCount = 1;
483 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700484 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700485 submitInfo.commandBufferCount = 1;
486 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
487 submitInfo.signalSemaphoreCount = 0;
488
489 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
490 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
491 backbuffer->fUsageFences[0]));
492
493 return fSurfaces[backbuffer->fImageIndex].get();
494}
495
496
497void VulkanTestContext::swapBuffers() {
498
499 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
500
501 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
502 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
503 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
504 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
505 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
506
507 VkImageMemoryBarrier imageMemoryBarrier = {
508 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
509 NULL, // pNext
510 srcAccessMask, // outputMask
511 dstAccessMask, // inputMask
512 layout, // oldLayout
513 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700514 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700515 fPresentQueueIndex, // dstQueueFamilyIndex
516 fImages[backbuffer->fImageIndex], // image
517 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
518 };
519 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
520 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
521 VkCommandBufferBeginInfo info;
522 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
523 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
524 info.flags = 0;
525 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
526 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
527 GR_VK_CALL(fBackendContext->fInterface,
528 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
529 srcStageMask, dstStageMask, 0,
530 0, nullptr,
531 0, nullptr,
532 1, &imageMemoryBarrier));
533 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
534 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
535
536 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
537
538 // insert the layout transfer into the queue and wait on the acquire
539 VkSubmitInfo submitInfo;
540 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
541 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
542 submitInfo.waitSemaphoreCount = 0;
543 submitInfo.pWaitDstStageMask = 0;
544 submitInfo.commandBufferCount = 1;
545 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
546 submitInfo.signalSemaphoreCount = 1;
547 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
548
549 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
550 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
551 backbuffer->fUsageFences[1]));
552
553 // Submit present operation to present queue
554 const VkPresentInfoKHR presentInfo =
555 {
556 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
557 NULL, // pNext
558 1, // waitSemaphoreCount
559 &backbuffer->fRenderSemaphore, // pWaitSemaphores
560 1, // swapchainCount
561 &fSwapchain, // pSwapchains
562 &backbuffer->fImageIndex, // pImageIndices
563 NULL // pResults
564 };
565
jvanverthb0d43522016-04-21 11:46:23 -0700566 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700567
568}