Debian Codegraph @

libs/bluevk/tests/test_bluevk_sdl.cpp

raw
1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**********************************************************************************************
18 * This has been forked from SDL's vulkan_test by Sam Lantinga, and heavily modified.
19 **********************************************************************************************/
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25
26 #include "SDL.h"
27 #include "SDL_vulkan.h"
28
29 #include <bluevk/BlueVK.h>
30
31 #include <utils/Log.h>
32
33 #include <math/scalar.h>
34
35 using namespace filament::math;
36 using namespace bluevk;
37
38 struct VulkanDriver {
39 VkInstance instance;
40 VkDevice device;
41 VkSurfaceKHR surface;
42 VkSwapchainKHR swapchain;
43 VkPhysicalDeviceProperties physicalDeviceProperties;
44 VkPhysicalDeviceFeatures physicalDeviceFeatures;
45 uint32_t graphicsQueueFamilyIndex;
46 uint32_t presentQueueFamilyIndex;
47 VkPhysicalDevice physicalDevice;
48 VkQueue graphicsQueue;
49 VkQueue presentQueue;
50 VkSemaphore imageAvailableSemaphore;
51 VkSemaphore renderingFinishedSemaphore;
52 VkSurfaceCapabilitiesKHR surfaceCapabilities;
53 VkSurfaceFormatKHR *surfaceFormats;
54 uint32_t surfaceFormatsAllocatedCount;
55 uint32_t surfaceFormatsCount;
56 uint32_t swapchainDesiredImageCount;
57 VkSurfaceFormatKHR surfaceFormat;
58 VkExtent2D swapchainSize;
59 VkCommandPool commandPool;
60 uint32_t swapchainImageCount;
61 VkImage *swapchainImages;
62 VkCommandBuffer *commandBuffers;
63 VkFence *fences;
64 };
65
66 static SDL_Window* gWindow = nullptr;
67 static VulkanDriver gVulkanDriver = {0};
68
69 static void destroySwapchainThings(SDL_bool doDestroySwapchain);
70
71 static void quit(int rc) {
72 if (gVulkanDriver.device && vkDeviceWaitIdle) {
73 vkDeviceWaitIdle(gVulkanDriver.device);
74 }
75 destroySwapchainThings(SDL_TRUE);
76 if (gVulkanDriver.imageAvailableSemaphore && vkDestroySemaphore) {
77 vkDestroySemaphore(gVulkanDriver.device, gVulkanDriver.imageAvailableSemaphore, NULL);
78 }
79 if (gVulkanDriver.renderingFinishedSemaphore && vkDestroySemaphore) {
80 vkDestroySemaphore(gVulkanDriver.device, gVulkanDriver.renderingFinishedSemaphore, NULL);
81 }
82 if (gVulkanDriver.device && vkDestroyDevice) {
83 vkDestroyDevice(gVulkanDriver.device, NULL);
84 }
85 if (gVulkanDriver.surface && vkDestroySurfaceKHR) {
86 vkDestroySurfaceKHR(gVulkanDriver.instance, gVulkanDriver.surface, NULL);
87 }
88 if (gVulkanDriver.instance && vkDestroyInstance) {
89 vkDestroyInstance(gVulkanDriver.instance, NULL);
90 }
91 SDL_free(gVulkanDriver.surfaceFormats);
92 exit(rc);
93 }
94
95 static void createInstance() {
96 VkApplicationInfo appInfo = {};
97 VkInstanceCreateInfo instanceCreateInfo = {};
98 const char **extensions = NULL;
99 unsigned extensionCount = 0;
100 VkResult result;
101
102 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
103 appInfo.apiVersion = VK_API_VERSION_1_0;
104 instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
105 instanceCreateInfo.pApplicationInfo = &appInfo;
106 if (!SDL_Vulkan_GetInstanceExtensions(gWindow, &extensionCount, NULL)) {
107 utils::slog.e << "SDL_Vulkan_GetInstanceExtensions(): "
108 << SDL_GetError() << utils::io::endl;
109 quit(2);
110 }
111 extensions = (const char**) SDL_malloc(sizeof(const char *) * extensionCount);
112 if (!extensions) {
113 SDL_OutOfMemory();
114 quit(2);
115 }
116 if (!SDL_Vulkan_GetInstanceExtensions(gWindow, &extensionCount, extensions)) {
117 SDL_free((void*)extensions);
118 utils::slog.e << "SDL_Vulkan_GetInstanceExtensions(): "
119 << SDL_GetError() << utils::io::endl;
120 quit(2);
121 }
122 instanceCreateInfo.enabledExtensionCount = extensionCount;
123 instanceCreateInfo.ppEnabledExtensionNames = extensions;
124 result = vkCreateInstance(&instanceCreateInfo, NULL, &gVulkanDriver.instance);
125 SDL_free((void*)extensions);
126
127 if (result != VK_SUCCESS) {
128 gVulkanDriver.instance = VK_NULL_HANDLE;
129 utils::slog.e << "vkCreateInstance(): " << result << utils::io::endl;
130 quit(2);
131 }
132 }
133
134 static void createSurface() {
135 if (!SDL_Vulkan_CreateSurface(gWindow, gVulkanDriver.instance, &gVulkanDriver.surface)) {
136 gVulkanDriver.surface = VK_NULL_HANDLE;
137 utils::slog.e << "SDL_Vulkan_CreateSurface(): "
138 << SDL_GetError() << utils::io::endl;
139 quit(2);
140 }
141 }
142
143 static void findPhysicalDevice() {
144 uint32_t physicalDeviceCount = 0;
145 VkPhysicalDevice *physicalDevices;
146 VkQueueFamilyProperties *queueFamiliesProperties = NULL;
147 uint32_t queueFamiliesPropertiesAllocatedSize = 0;
148 VkExtensionProperties *deviceExtensions = NULL;
149 uint32_t deviceExtensionsAllocatedSize = 0;
150 uint32_t physicalDeviceIdx;
151
152 VkResult result =
153 vkEnumeratePhysicalDevices(gVulkanDriver.instance, &physicalDeviceCount, NULL);
154 if (result != VK_SUCCESS) {
155 utils::slog.e << "vkEnumeratePhysicalDevices(): " << result << utils::io::endl;
156 quit(2);
157 }
158 if (physicalDeviceCount == 0) {
159 utils::slog.e << "vkEnumeratePhysicalDevices(): no physical devices\n";
160 quit(2);
161 }
162 physicalDevices = (VkPhysicalDevice*) SDL_malloc(sizeof(VkPhysicalDevice) *
163 physicalDeviceCount);
164 if (!physicalDevices) {
165 SDL_OutOfMemory();
166 quit(2);
167 }
168 result =
169 vkEnumeratePhysicalDevices(gVulkanDriver.instance, &physicalDeviceCount, physicalDevices);
170 if (result != VK_SUCCESS) {
171 SDL_free(physicalDevices);
172 utils::slog.e << "vkEnumeratePhysicalDevices(): " << result << utils::io::endl;
173 quit(2);
174 }
175 gVulkanDriver.physicalDevice = NULL;
176 for (physicalDeviceIdx = 0; physicalDeviceIdx < physicalDeviceCount; physicalDeviceIdx++) {
177 uint32_t queueFamiliesCount = 0;
178 uint32_t queueFamilyIndex;
179 uint32_t deviceExtensionCount = 0;
180 SDL_bool hasSwapchainExtension = SDL_FALSE;
181 uint32_t i;
182
183 VkPhysicalDevice physicalDevice = physicalDevices[physicalDeviceIdx];
184 vkGetPhysicalDeviceProperties(physicalDevice, &gVulkanDriver.physicalDeviceProperties);
185 if (VK_VERSION_MAJOR(gVulkanDriver.physicalDeviceProperties.apiVersion) < 1) {
186 continue;
187 }
188 vkGetPhysicalDeviceFeatures(physicalDevice, &gVulkanDriver.physicalDeviceFeatures);
189 vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, NULL);
190 if (queueFamiliesCount == 0) {
191 continue;
192 }
193 if (queueFamiliesPropertiesAllocatedSize < queueFamiliesCount) {
194 SDL_free(queueFamiliesProperties);
195 queueFamiliesPropertiesAllocatedSize = queueFamiliesCount;
196 queueFamiliesProperties = (VkQueueFamilyProperties*)
197 SDL_malloc(sizeof(VkQueueFamilyProperties) * queueFamiliesPropertiesAllocatedSize);
198 if (!queueFamiliesProperties) {
199 SDL_free(physicalDevices);
200 SDL_free(deviceExtensions);
201 SDL_OutOfMemory();
202 quit(2);
203 }
204 }
205 vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount,
206 queueFamiliesProperties);
207 gVulkanDriver.graphicsQueueFamilyIndex = queueFamiliesCount;
208 gVulkanDriver.presentQueueFamilyIndex = queueFamiliesCount;
209 for (queueFamilyIndex = 0; queueFamilyIndex < queueFamiliesCount; queueFamilyIndex++) {
210 VkBool32 supported = 0;
211 if (queueFamiliesProperties[queueFamilyIndex].queueCount == 0) {
212 continue;
213 }
214 if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
215 gVulkanDriver.graphicsQueueFamilyIndex = queueFamilyIndex;
216 }
217 result = vkGetPhysicalDeviceSurfaceSupportKHR(
218 physicalDevice, queueFamilyIndex, gVulkanDriver.surface, &supported);
219 if (result != VK_SUCCESS) {
220 SDL_free(physicalDevices);
221 SDL_free(queueFamiliesProperties);
222 SDL_free(deviceExtensions);
223 utils::slog.e << "vkGetPhysicalDeviceSurfaceSupportKHR(): "
224 << result << utils::io::endl;
225 quit(2);
226 }
227 if (supported) {
228 gVulkanDriver.presentQueueFamilyIndex = queueFamilyIndex;
229 if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
230 break;
231 }
232 }
233 }
234 if (gVulkanDriver.graphicsQueueFamilyIndex == queueFamiliesCount) continue;
235 if (gVulkanDriver.presentQueueFamilyIndex == queueFamiliesCount) continue;
236 result =
237 vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, NULL);
238 if (result != VK_SUCCESS) {
239 SDL_free(physicalDevices);
240 SDL_free(queueFamiliesProperties);
241 SDL_free(deviceExtensions);
242 utils::slog.e << "vkEnumerateDeviceExtensionProperties(): " << result
243 << utils::io::endl;
244 quit(2);
245 }
246 if (deviceExtensionCount == 0) continue;
247 if (deviceExtensionsAllocatedSize < deviceExtensionCount) {
248 SDL_free(deviceExtensions);
249 deviceExtensionsAllocatedSize = deviceExtensionCount;
250 deviceExtensions = (VkExtensionProperties*)
251 SDL_malloc(sizeof(VkExtensionProperties) * deviceExtensionsAllocatedSize);
252 if (!deviceExtensions) {
253 SDL_free(physicalDevices);
254 SDL_free(queueFamiliesProperties);
255 SDL_OutOfMemory();
256 quit(2);
257 }
258 }
259 result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount,
260 deviceExtensions);
261 if (result != VK_SUCCESS) {
262 SDL_free(physicalDevices);
263 SDL_free(queueFamiliesProperties);
264 SDL_free(deviceExtensions);
265 utils::slog.e << "vkEnumerateDeviceExtensionProperties(): " << result
266 << utils::io::endl;
267 quit(2);
268 }
269 for (i = 0; i < deviceExtensionCount; i++) {
270 if (!SDL_strcmp(deviceExtensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
271 hasSwapchainExtension = SDL_TRUE;
272 break;
273 }
274 }
275 if (!hasSwapchainExtension) continue;
276 gVulkanDriver.physicalDevice = physicalDevice;
277 break;
278 }
279 SDL_free(physicalDevices);
280 SDL_free(queueFamiliesProperties);
281 SDL_free(deviceExtensions);
282 if (!gVulkanDriver.physicalDevice) {
283 utils::slog.e << "Vulkan: no viable physical devices found\n";
284 quit(2);
285 }
286 }
287
288 static void createDevice() {
289 VkDeviceQueueCreateInfo deviceQueueCreateInfo[1] = {};
290 static const float queuePriority[] = {1.0f};
291 VkDeviceCreateInfo deviceCreateInfo = {};
292 static const char *const deviceExtensionNames[] = {
293 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
294 };
295 VkResult result;
296 deviceQueueCreateInfo->sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
297 deviceQueueCreateInfo->queueFamilyIndex = gVulkanDriver.graphicsQueueFamilyIndex;
298 deviceQueueCreateInfo->queueCount = 1;
299 deviceQueueCreateInfo->pQueuePriorities = &queuePriority[0];
300 deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
301 deviceCreateInfo.queueCreateInfoCount = 1;
302 deviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfo;
303 deviceCreateInfo.pEnabledFeatures = NULL;
304 deviceCreateInfo.enabledExtensionCount = SDL_arraysize(deviceExtensionNames);
305 deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
306 result = vkCreateDevice(
307 gVulkanDriver.physicalDevice, &deviceCreateInfo, NULL, &gVulkanDriver.device);
308 if (result != VK_SUCCESS) {
309 gVulkanDriver.device = VK_NULL_HANDLE;
310 utils::slog.e << "vkCreateDevice(): " << result << utils::io::endl;
311 quit(2);
312 }
313 }
314
315 static void getQueues() {
316 vkGetDeviceQueue(gVulkanDriver.device, gVulkanDriver.graphicsQueueFamilyIndex, 0,
317 &gVulkanDriver.graphicsQueue);
318 if (gVulkanDriver.graphicsQueueFamilyIndex != gVulkanDriver.presentQueueFamilyIndex) {
319 vkGetDeviceQueue(gVulkanDriver.device, gVulkanDriver.presentQueueFamilyIndex, 0,
320 &gVulkanDriver.presentQueue);
321 } else {
322 gVulkanDriver.presentQueue = gVulkanDriver.graphicsQueue;
323 }
324 }
325
326 static void createSemaphore(VkSemaphore *semaphore) {
327 VkResult result;
328 VkSemaphoreCreateInfo createInfo = {};
329 createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
330 result = vkCreateSemaphore(gVulkanDriver.device, &createInfo, NULL, semaphore);
331 if (result != VK_SUCCESS) {
332 *semaphore = VK_NULL_HANDLE;
333 utils::slog.e << "vkCreateSemaphore(): " << result << utils::io::endl;
334 quit(2);
335 }
336 }
337
338 static void createSemaphores() {
339 createSemaphore(&gVulkanDriver.imageAvailableSemaphore);
340 createSemaphore(&gVulkanDriver.renderingFinishedSemaphore);
341 }
342
343 static void getSurfaceCaps() {
344 VkResult result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
345 gVulkanDriver.physicalDevice, gVulkanDriver.surface, &gVulkanDriver.surfaceCapabilities);
346 if (result != VK_SUCCESS) {
347 utils::slog.e << "vkGetPhysicalDeviceSurfaceCapabilitiesKHR(): " << result
348 << utils::io::endl;
349 quit(2);
350 }
351 if (!(gVulkanDriver.surfaceCapabilities.supportedUsageFlags &
352 VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
353 utils::slog.e << "Vulkan surface doesn't support VK_IMAGE_USAGE_TRANSFER_DST_BIT\n";
354 quit(2);
355 }
356 }
357
358 static void getSurfaceFormats() {
359 VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(gVulkanDriver.physicalDevice,
360 gVulkanDriver.surface, &gVulkanDriver.surfaceFormatsCount, NULL);
361 if (result != VK_SUCCESS) {
362 gVulkanDriver.surfaceFormatsCount = 0;
363 utils::slog.e << "vkGetPhysicalDeviceSurfaceFormatsKHR(): " << result << utils::io::endl;
364 quit(2);
365 }
366 if (gVulkanDriver.surfaceFormatsCount > gVulkanDriver.surfaceFormatsAllocatedCount) {
367 gVulkanDriver.surfaceFormatsAllocatedCount = gVulkanDriver.surfaceFormatsCount;
368 SDL_free(gVulkanDriver.surfaceFormats);
369 gVulkanDriver.surfaceFormats = (VkSurfaceFormatKHR *)
370 SDL_malloc(sizeof(VkSurfaceFormatKHR) * gVulkanDriver.surfaceFormatsAllocatedCount);
371 if (!gVulkanDriver.surfaceFormats) {
372 gVulkanDriver.surfaceFormatsCount = 0;
373 SDL_OutOfMemory();
374 quit(2);
375 }
376 }
377 result = vkGetPhysicalDeviceSurfaceFormatsKHR(gVulkanDriver.physicalDevice,
378 gVulkanDriver.surface, &gVulkanDriver.surfaceFormatsCount,
379 gVulkanDriver.surfaceFormats);
380 if (result != VK_SUCCESS) {
381 gVulkanDriver.surfaceFormatsCount = 0;
382 utils::slog.e << "vkGetPhysicalDeviceSurfaceFormatsKHR(): " << result << utils::io::endl;
383 quit(2);
384 }
385 }
386
387 static void getSwapchainImages() {
388 VkResult result;
389 SDL_free(gVulkanDriver.swapchainImages);
390 gVulkanDriver.swapchainImages = NULL;
391 result = vkGetSwapchainImagesKHR(
392 gVulkanDriver.device, gVulkanDriver.swapchain, &gVulkanDriver.swapchainImageCount, NULL);
393 if (result != VK_SUCCESS) {
394 gVulkanDriver.swapchainImageCount = 0;
395 utils::slog.e << "vkGetSwapchainImagesKHR(): " << result << utils::io::endl;
396 quit(2);
397 }
398 gVulkanDriver.swapchainImages = (VkImage*) SDL_malloc(sizeof(VkImage) *
399 gVulkanDriver.swapchainImageCount);
400 if (!gVulkanDriver.swapchainImages) {
401 SDL_OutOfMemory();
402 quit(2);
403 }
404 result = vkGetSwapchainImagesKHR(gVulkanDriver.device, gVulkanDriver.swapchain,
405 &gVulkanDriver.swapchainImageCount, gVulkanDriver.swapchainImages);
406 if (result != VK_SUCCESS) {
407 SDL_free(gVulkanDriver.swapchainImages);
408 gVulkanDriver.swapchainImages = NULL;
409 gVulkanDriver.swapchainImageCount = 0;
410 utils::slog.e << "vkGetSwapchainImagesKHR(): " << result << utils::io::endl;
411 quit(2);
412 }
413 }
414
415 static SDL_bool createSwapchain() {
416 uint32_t i;
417 int w, h;
418 VkSwapchainCreateInfoKHR createInfo = {};
419 VkResult result;
420
421 // pick an image count
422 gVulkanDriver.swapchainDesiredImageCount = gVulkanDriver.surfaceCapabilities.minImageCount + 1;
423 if (gVulkanDriver.swapchainDesiredImageCount > gVulkanDriver.surfaceCapabilities.maxImageCount
424 && gVulkanDriver.surfaceCapabilities.maxImageCount > 0) {
425 gVulkanDriver.swapchainDesiredImageCount = gVulkanDriver.surfaceCapabilities.maxImageCount;
426 }
427
428 // pick a format
429 if (gVulkanDriver.surfaceFormatsCount == 1
430 && gVulkanDriver.surfaceFormats[0].format == VK_FORMAT_UNDEFINED) {
431 // aren't any preferred formats, so we pick
432 gVulkanDriver.surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
433 gVulkanDriver.surfaceFormat.format = VK_FORMAT_R8G8B8A8_UNORM;
434 } else {
435 gVulkanDriver.surfaceFormat = gVulkanDriver.surfaceFormats[0];
436 for (i = 0; i < gVulkanDriver.surfaceFormatsCount; i++) {
437 if (gVulkanDriver.surfaceFormats[i].format == VK_FORMAT_R8G8B8A8_UNORM) {
438 gVulkanDriver.surfaceFormat = gVulkanDriver.surfaceFormats[i];
439 break;
440 }
441 }
442 }
443
444 // get size
445 SDL_Vulkan_GetDrawableSize(gWindow, &w, &h);
446 gVulkanDriver.swapchainSize.width = w;
447 gVulkanDriver.swapchainSize.height = h;
448 if (w == 0 || h == 0) return SDL_FALSE;
449
450 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
451 createInfo.surface = gVulkanDriver.surface;
452 createInfo.minImageCount = gVulkanDriver.swapchainDesiredImageCount;
453 createInfo.imageFormat = gVulkanDriver.surfaceFormat.format;
454 createInfo.imageColorSpace = gVulkanDriver.surfaceFormat.colorSpace;
455 createInfo.imageExtent = gVulkanDriver.swapchainSize;
456 createInfo.imageArrayLayers = 1;
457 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
458 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
459 createInfo.preTransform = gVulkanDriver.surfaceCapabilities.currentTransform;
460 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
461 createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
462 createInfo.clipped = VK_TRUE;
463 createInfo.oldSwapchain = gVulkanDriver.swapchain;
464 result =
465 vkCreateSwapchainKHR(gVulkanDriver.device, &createInfo, NULL, &gVulkanDriver.swapchain);
466 if (createInfo.oldSwapchain)
467 vkDestroySwapchainKHR(gVulkanDriver.device, createInfo.oldSwapchain, NULL);
468 if (result != VK_SUCCESS) {
469 gVulkanDriver.swapchain = VK_NULL_HANDLE;
470 utils::slog.e << "vkCreateSwapchainKHR(): " << result << utils::io::endl;
471 quit(2);
472 }
473 getSwapchainImages();
474 return SDL_TRUE;
475 }
476
477 static void destroySwapchain() {
478 if (gVulkanDriver.swapchain) {
479 vkDestroySwapchainKHR(gVulkanDriver.device, gVulkanDriver.swapchain, NULL);
480 }
481 gVulkanDriver.swapchain = VK_NULL_HANDLE;
482 SDL_free(gVulkanDriver.swapchainImages);
483 gVulkanDriver.swapchainImages = NULL;
484 }
485
486 static void destroyCommandBuffers() {
487 if (gVulkanDriver.commandBuffers) {
488 vkFreeCommandBuffers(gVulkanDriver.device,
489 gVulkanDriver.commandPool,
490 gVulkanDriver.swapchainImageCount,
491 gVulkanDriver.commandBuffers);
492 }
493 SDL_free(gVulkanDriver.commandBuffers);
494 gVulkanDriver.commandBuffers = NULL;
495 }
496
497 static void destroyCommandPool() {
498 if (gVulkanDriver.commandPool) {
499 vkDestroyCommandPool(gVulkanDriver.device, gVulkanDriver.commandPool, NULL);
500 }
501 gVulkanDriver.commandPool = VK_NULL_HANDLE;
502 }
503
504 static void createCommandPool() {
505 VkResult result;
506
507 VkCommandPoolCreateInfo createInfo = {};
508 createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
509 createInfo.flags =
510 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
511 createInfo.queueFamilyIndex = gVulkanDriver.graphicsQueueFamilyIndex;
512 result =
513 vkCreateCommandPool(gVulkanDriver.device, &createInfo, NULL, &gVulkanDriver.commandPool);
514 if (result != VK_SUCCESS) {
515 gVulkanDriver.commandPool = VK_NULL_HANDLE;
516 utils::slog.e << "vkCreateCommandPool(): " << result << utils::io::endl;
517 quit(2);
518 }
519 }
520
521 static void createCommandBuffers() {
522 VkResult result;
523 VkCommandBufferAllocateInfo allocateInfo = {};
524 allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
525 allocateInfo.commandPool = gVulkanDriver.commandPool;
526 allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
527 allocateInfo.commandBufferCount = gVulkanDriver.swapchainImageCount;
528 gVulkanDriver.commandBuffers = (VkCommandBuffer*)
529 SDL_malloc(sizeof(VkCommandBuffer) * gVulkanDriver.swapchainImageCount);
530 result =
531 vkAllocateCommandBuffers(gVulkanDriver.device, &allocateInfo, gVulkanDriver.commandBuffers);
532 if (result != VK_SUCCESS) {
533 SDL_free(gVulkanDriver.commandBuffers);
534 gVulkanDriver.commandBuffers = NULL;
535 utils::slog.e << "vkAllocateCommandBuffers(): " << result << utils::io::endl;
536 quit(2);
537 }
538 }
539
540 static void createFences() {
541 uint32_t i;
542 gVulkanDriver.fences = (VkFence*) SDL_malloc(sizeof(VkFence) *
543 gVulkanDriver.swapchainImageCount);
544 if (!gVulkanDriver.fences) {
545 SDL_OutOfMemory();
546 quit(2);
547 }
548 for (i = 0; i < gVulkanDriver.swapchainImageCount; i++) {
549 VkResult result;
550 VkFenceCreateInfo createInfo = {};
551 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
552 createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
553 result = vkCreateFence(gVulkanDriver.device, &createInfo, NULL, &gVulkanDriver.fences[i]);
554 if (result != VK_SUCCESS) {
555 for (; i > 0; i--) {
556 vkDestroyFence(gVulkanDriver.device, gVulkanDriver.fences[i - 1], NULL);
557 }
558 SDL_free(gVulkanDriver.fences);
559 gVulkanDriver.fences = NULL;
560 utils::slog.e << "vkCreateFence(): " << result << utils::io::endl;
561 quit(2);
562 }
563 }
564 }
565
566 static void destroyFences() {
567 uint32_t i;
568 if (!gVulkanDriver.fences) return;
569 for (i = 0; i < gVulkanDriver.swapchainImageCount; i++) {
570 vkDestroyFence(gVulkanDriver.device, gVulkanDriver.fences[i], NULL);
571 }
572 SDL_free(gVulkanDriver.fences);
573 gVulkanDriver.fences = NULL;
574 }
575
576 static void recordPipelineImageBarrier(VkCommandBuffer commandBuffer,
577 VkAccessFlags sourceAccessMask,
578 VkAccessFlags destAccessMask,
579 VkImageLayout sourceLayout,
580 VkImageLayout destLayout,
581 VkImage image) {
582 VkImageMemoryBarrier barrier = {};
583 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
584 barrier.srcAccessMask = sourceAccessMask;
585 barrier.dstAccessMask = destAccessMask;
586 barrier.oldLayout = sourceLayout;
587 barrier.newLayout = destLayout;
588 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
589 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
590 barrier.image = image;
591 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
592 barrier.subresourceRange.baseMipLevel = 0;
593 barrier.subresourceRange.levelCount = 1;
594 barrier.subresourceRange.baseArrayLayer = 0;
595 barrier.subresourceRange.layerCount = 1;
596 vkCmdPipelineBarrier(commandBuffer,
597 VK_PIPELINE_STAGE_TRANSFER_BIT,
598 VK_PIPELINE_STAGE_TRANSFER_BIT,
599 0,
600 0,
601 NULL,
602 0,
603 NULL,
604 1,
605 &barrier);
606 }
607
608 static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *clearColor) {
609 VkCommandBuffer commandBuffer = gVulkanDriver.commandBuffers[frameIndex];
610 VkImage image = gVulkanDriver.swapchainImages[frameIndex];
611 VkCommandBufferBeginInfo beginInfo = {};
612 VkImageSubresourceRange clearRange = {};
613
614 VkResult result = vkResetCommandBuffer(commandBuffer, 0);
615 if (result != VK_SUCCESS) {
616 utils::slog.e << "vkResetCommandBuffer(): " << result << utils::io::endl;
617 quit(2);
618 }
619 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
620 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
621 result = vkBeginCommandBuffer(commandBuffer, &beginInfo);
622 if (result != VK_SUCCESS) {
623 utils::slog.e << "vkBeginCommandBuffer(): " << result << utils::io::endl;
624 quit(2);
625 }
626 recordPipelineImageBarrier(commandBuffer,
627 0,
628 VK_ACCESS_TRANSFER_WRITE_BIT,
629 VK_IMAGE_LAYOUT_UNDEFINED,
630 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
631 image);
632 clearRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
633 clearRange.baseMipLevel = 0;
634 clearRange.levelCount = 1;
635 clearRange.baseArrayLayer = 0;
636 clearRange.layerCount = 1;
637 vkCmdClearColorImage(
638 commandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, clearColor, 1, &clearRange);
639 recordPipelineImageBarrier(commandBuffer,
640 VK_ACCESS_TRANSFER_WRITE_BIT,
641 VK_ACCESS_MEMORY_READ_BIT,
642 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
643 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
644 image);
645 result = vkEndCommandBuffer(commandBuffer);
646 if (result != VK_SUCCESS) {
647 utils::slog.e << "vkEndCommandBuffer(): " << result << utils::io::endl;
648 quit(2);
649 }
650 }
651
652 static void destroySwapchainThings(SDL_bool doDestroySwapchain) {
653 destroyFences();
654 destroyCommandBuffers();
655 destroyCommandPool();
656 if (doDestroySwapchain) destroySwapchain();
657 }
658
659 static SDL_bool createNewSwapchainThings() {
660 destroySwapchainThings(SDL_FALSE);
661 getSurfaceCaps();
662 getSurfaceFormats();
663 if (!createSwapchain()) {
664 return SDL_FALSE;
665 }
666 createCommandPool();
667 createCommandBuffers();
668 createFences();
669 return SDL_TRUE;
670 }
671
672 static void initVulkan() {
673 if (!bluevk::initialize()) {
674 utils::slog.e << "BlueVK is unable to load entry points.\n";
675 quit(2);
676 }
677 createInstance();
678 bluevk::bindInstance(gVulkanDriver.instance);
679 createSurface();
680 findPhysicalDevice();
681 createDevice();
682 getQueues();
683 createSemaphores();
684 createNewSwapchainThings();
685 }
686
687 static SDL_bool render() {
688 const float SPEED = 4;
689 uint32_t frameIndex;
690 VkResult result;
691 double currentTime;
692 VkClearColorValue clearColor = {};
693 VkPipelineStageFlags waitDestStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT;
694 VkSubmitInfo submitInfo = {};
695 VkPresentInfoKHR presentInfo = {};
696 int w, h;
697
698 if (!gVulkanDriver.swapchain) {
699 SDL_bool retval = createNewSwapchainThings();
700 if (!retval) SDL_Delay(100);
701 return retval;
702 }
703
704 result = vkAcquireNextImageKHR(gVulkanDriver.device, gVulkanDriver.swapchain, UINT64_MAX,
705 gVulkanDriver.imageAvailableSemaphore, VK_NULL_HANDLE, &frameIndex);
706 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
707 return createNewSwapchainThings();
708 }
709
710 if (result != VK_SUBOPTIMAL_KHR && result != VK_SUCCESS) {
711 utils::slog.e << "vkAcquireNextImageKHR(): " << result << utils::io::endl;
712 quit(2);
713 }
714
715 result = vkWaitForFences(
716 gVulkanDriver.device, 1, &gVulkanDriver.fences[frameIndex], VK_FALSE, UINT64_MAX);
717
718 if (result != VK_SUCCESS) {
719 utils::slog.e << "vkWaitForFences(): " << result << utils::io::endl;
720 quit(2);
721 }
722 result = vkResetFences(gVulkanDriver.device, 1, &gVulkanDriver.fences[frameIndex]);
723 if (result != VK_SUCCESS) {
724 utils::slog.e << "vkResetFences(): " << result << utils::io::endl;
725 quit(2);
726 }
727 currentTime = (double) SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
728 clearColor.float32[0] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime));
729 clearColor.float32[1] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime + F_PI * 2 / 3));
730 clearColor.float32[2] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime + F_PI * 4 / 3));
731 clearColor.float32[3] = 1;
732 rerecordCommandBuffer(frameIndex, &clearColor);
733 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
734 submitInfo.waitSemaphoreCount = 1;
735 submitInfo.pWaitSemaphores = &gVulkanDriver.imageAvailableSemaphore;
736 submitInfo.pWaitDstStageMask = &waitDestStageMask;
737 submitInfo.commandBufferCount = 1;
738 submitInfo.pCommandBuffers = &gVulkanDriver.commandBuffers[frameIndex];
739 submitInfo.signalSemaphoreCount = 1;
740 submitInfo.pSignalSemaphores = &gVulkanDriver.renderingFinishedSemaphore;
741 result = vkQueueSubmit(
742 gVulkanDriver.graphicsQueue, 1, &submitInfo, gVulkanDriver.fences[frameIndex]);
743 if (result != VK_SUCCESS) {
744 utils::slog.e << "vkQueueSubmit(): " << result << utils::io::endl;
745 quit(2);
746 }
747 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
748 presentInfo.waitSemaphoreCount = 1;
749 presentInfo.pWaitSemaphores = &gVulkanDriver.renderingFinishedSemaphore;
750 presentInfo.swapchainCount = 1;
751 presentInfo.pSwapchains = &gVulkanDriver.swapchain;
752 presentInfo.pImageIndices = &frameIndex;
753 result = vkQueuePresentKHR(gVulkanDriver.presentQueue, &presentInfo);
754 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
755 return createNewSwapchainThings();
756 }
757 if (result != VK_SUCCESS) {
758 utils::slog.e << "vkQueuePresentKHR(): " << result << utils::io::endl;
759 quit(2);
760 }
761 SDL_Vulkan_GetDrawableSize(gWindow, &w, &h);
762 if (w != (int)gVulkanDriver.swapchainSize.width ||
763 h != (int)gVulkanDriver.swapchainSize.height) {
764 return createNewSwapchainThings();
765 }
766 return SDL_TRUE;
767 }
768
769 int main(int argc, char *argv[]) {
770 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
771 gWindow = SDL_CreateWindow("Vulkan", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
772 512, 512, SDL_WINDOW_VULKAN | SDL_INIT_VIDEO);
773 SDL_DisplayMode mode;
774 SDL_GetCurrentDisplayMode(0, &mode);
775 SDL_Log("Screen BPP : %d\n", SDL_BITSPERPIXEL(mode.format));
776 initVulkan();
777 bool done = false;
778 while (!done) {
779 SDL_Event event;
780 while(SDL_PollEvent(&event)) {
781 switch (event.type) {
782 case SDL_QUIT:
783 done = true;
784 break;
785 }
786 }
787 if (!done) {
788 render();
789 }
790 }
791 quit(0);
792 return 0;
793 }